blob: d42540efb4e9b36783665da54527ba6e61cf4d66 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Please read the NativeInteger.h for the explanation wrt. differences between
7 * INTEGER and NativeInteger.
8 * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
9 * implementation deals with the standard (machine-specific) representation
10 * of them instead of using the platform-independent buffer.
11 */
12#include <NativeInteger.h>
13#include <INTEGER.h>
14#include <assert.h>
15
16/*
17 * NativeInteger basic type description.
18 */
19static ber_tlv_tag_t asn1_DEF_NativeInteger_tags[] = {
20 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
21};
22asn1_TYPE_descriptor_t asn1_DEF_NativeInteger = {
23 "INTEGER", /* The ASN.1 type is still INTEGER */
24 asn_generic_no_constraint,
25 NativeInteger_decode_ber,
26 NativeInteger_encode_der,
27 NativeInteger_print,
28 NativeInteger_free,
29 0, /* Use generic outmost tag fetcher */
30 asn1_DEF_NativeInteger_tags,
31 sizeof(asn1_DEF_NativeInteger_tags)/sizeof(asn1_DEF_NativeInteger_tags[0]),
32 1, /* Single UNIVERSAL tag may be implicitly overriden */
33 0 /* Always in primitive form */
34};
35
36/*
37 * Decode INTEGER type.
38 */
39ber_dec_rval_t
40NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td,
41 void **int_ptr, void *buf_ptr, size_t size, int tag_mode) {
42 int *Int = *int_ptr;
43 ber_dec_rval_t rval;
44 ber_dec_ctx_t ctx = { 0 };
45 ber_tlv_len_t length;
46
47 /*
48 * If the structure is not there, allocate it.
49 */
50 if(Int == NULL) {
51 Int = *int_ptr = CALLOC(1, sizeof(*Int));
52 if(Int == NULL) {
53 rval.code = RC_FAIL;
54 rval.consumed = 0;
55 return rval;
56 }
57 }
58
59 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
60 td->name, tag_mode);
61
62 /*
63 * Check tags.
64 */
65 rval = ber_check_tags(td, &ctx,
66 buf_ptr, size, tag_mode, &length, 0);
67 if(rval.code != RC_OK)
68 return rval;
69
70 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
71
72 /*
73 * Make sure we have this length.
74 */
75 buf_ptr += rval.consumed;
76 size -= rval.consumed;
77 if(length > size) {
78 rval.code = RC_WMORE;
79 rval.consumed = 0;
80 return rval;
81 }
82
83 /*
84 * ASN.1 encoded INTEGER: buf_ptr, length
85 * Fill the Int, at the same time checking for overflow.
86 * If overflow occured, return with RC_FAIL.
87 */
88 {
89 INTEGER_t tmp;
90 long l;
91 tmp.buf = buf_ptr;
92 tmp.size = length;
93
94 if(asn1_INTEGER2long(&tmp, &l)) {
95 rval.code = RC_FAIL;
96 rval.consumed = 0;
97 return rval;
98 }
99
100 *Int = l;
101
102 /*
103 * Note that int might be shorter than long.
104 * This expression hopefully will be optimized away
105 * by compiler.
106 */
107 if(sizeof(int) != sizeof(long) && (*Int != l)) {
108 *Int = 0; /* Safe value */
109 rval.code = RC_FAIL;
110 rval.consumed = 0;
111 return rval;
112 }
113 }
114
115 rval.code = RC_OK;
116 rval.consumed += length;
117
118 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
119 (long)rval.consumed, (long)length, td->name, *Int);
120
121 return rval;
122}
123
124/*
125 * Encode the NativeInteger using the standard INTEGER type DER encoder.
126 */
127der_enc_rval_t
128NativeInteger_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
129 int tag_mode, ber_tlv_tag_t tag,
130 asn_app_consume_bytes_f *cb, void *app_key) {
131 unsigned int Int = *(unsigned int *)ptr; /* Disable sign ext. */
132 der_enc_rval_t erval;
133 INTEGER_t tmp;
134
135#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
136
137 tmp.buf = &Int;
138 tmp.size = sizeof(Int);
139
140#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
141 uint8_t buf[sizeof(int)];
142 uint8_t *p;
143
144 /* Prepare fake INTEGER */
145 for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8)
146 *p = Int & 0xff;
147
148 tmp.buf = buf;
149 tmp.size = sizeof(buf);
150#endif /* WORDS_BIGENDIAN */
151
152 /* Encode fake INTEGER */
153 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
154 if(erval.encoded == -1) {
155 assert(erval.structure_ptr == &tmp);
156 erval.structure_ptr = ptr;
157 }
158 return erval;
159}
160
161/*
162 * INTEGER specific human-readable output.
163 */
164int
165NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
166 asn_app_consume_bytes_f *cb, void *app_key) {
167 const int *Int = sptr;
168 char scratch[32];
169 int ret;
170
171 if(Int) {
172 ret = snprintf(scratch, sizeof(scratch), "%d", *Int);
173 assert(ret > 0 && ret < sizeof(scratch));
174 return cb(scratch, ret, app_key);
175 } else {
176 return cb("<absent>", 8, app_key);
177 }
178}
179
180void
181NativeInteger_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
182
183 if(!td || !ptr)
184 return;
185
186 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
187 td->name, contents_only, ptr);
188
189 if(!contents_only) {
190 FREEMEM(ptr);
191 }
192}
193