blob: 0c7a81afbcdc7a288c2b208cfcaaf5f3dfa709aa [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <INTEGER.h>
6#include <assert.h>
7#include <errno.h>
8
9/*
10 * INTEGER basic type description.
11 */
12static ber_tlv_tag_t asn1_DEF_INTEGER_tags[] = {
13 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
14};
15asn1_TYPE_descriptor_t asn1_DEF_INTEGER = {
16 "INTEGER",
17 asn_generic_no_constraint,
18 INTEGER_decode_ber,
19 INTEGER_encode_der,
20 INTEGER_print,
21 INTEGER_free,
22 0, /* Use generic outmost tag fetcher */
23 asn1_DEF_INTEGER_tags,
24 sizeof(asn1_DEF_INTEGER_tags)/sizeof(asn1_DEF_INTEGER_tags[0]),
25 1, /* Single UNIVERSAL tag may be implicitly overriden */
vlmb42843a2004-06-05 08:17:50 +000026 0, /* Always in primitive form */
vlme413c122004-08-20 13:23:42 +000027 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000028 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000029};
30
31/*
32 * Decode INTEGER type.
33 */
34ber_dec_rval_t
35INTEGER_decode_ber(asn1_TYPE_descriptor_t *td,
36 void **int_structure, void *buf_ptr, size_t size, int tag_mode) {
vlmda674682004-08-11 09:07:36 +000037 INTEGER_t *st = (INTEGER_t *)*int_structure;
vlmfa67ddc2004-06-03 03:38:44 +000038 ber_dec_rval_t rval;
vlmb42843a2004-06-05 08:17:50 +000039 ber_dec_ctx_t ctx = { 0, 0, 0, 0 };
vlmfa67ddc2004-06-03 03:38:44 +000040 ber_tlv_len_t length;
41
42 /*
43 * If the structure is not there, allocate it.
44 */
45 if(st == NULL) {
vlmda674682004-08-11 09:07:36 +000046 (void *)st = *int_structure = CALLOC(1, sizeof(*st));
vlmfa67ddc2004-06-03 03:38:44 +000047 if(st == NULL) {
48 rval.code = RC_FAIL;
49 rval.consumed = 0;
50 return rval;
51 }
52 }
53
54 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
55 td->name, tag_mode);
56
57 /*
58 * Check tags.
59 */
60 rval = ber_check_tags(td, &ctx,
61 buf_ptr, size, tag_mode, &length, 0);
62 if(rval.code != RC_OK)
63 return rval;
64
65 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
66
67 /*
68 * Make sure we have this length.
69 */
vlm1ff928d2004-08-11 08:10:13 +000070 (char *)buf_ptr += rval.consumed;
vlmfa67ddc2004-06-03 03:38:44 +000071 size -= rval.consumed;
vlmb42843a2004-06-05 08:17:50 +000072 if(length > (ber_tlv_len_t)size) {
vlmfa67ddc2004-06-03 03:38:44 +000073 rval.code = RC_WMORE;
74 rval.consumed = 0;
75 return rval;
76 }
77
vlmda674682004-08-11 09:07:36 +000078 st->buf = (uint8_t *)MALLOC(length);
vlmfa67ddc2004-06-03 03:38:44 +000079 if(st->buf) {
80 st->size = length;
81 } else {
82 rval.code = RC_FAIL;
83 rval.consumed = 0;
84 return rval;
85 }
86
87 memcpy(st->buf, buf_ptr, st->size);
88
89 rval.code = RC_OK;
90 rval.consumed += length;
91
92 ASN_DEBUG("Took %ld/%ld bytes to encode %s",
93 (long)rval.consumed,
94 (long)length, td->name);
95
96 return rval;
97}
98
99/*
100 * Encode INTEGER type using DER.
101 */
102der_enc_rval_t
103INTEGER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
104 int tag_mode, ber_tlv_tag_t tag,
105 asn_app_consume_bytes_f *cb, void *app_key) {
106 der_enc_rval_t erval;
vlmda674682004-08-11 09:07:36 +0000107 INTEGER_t *st = (INTEGER_t *)ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000108
109 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
110 cb?"Encoding":"Estimating", sd->name, tag_mode);
111
112 /*
113 * Canonicalize integer in the buffer.
114 * (Remove too long sign extension, remove some first 0x00 bytes)
115 */
116 if(st->buf) {
117 uint8_t *buf = st->buf;
118 uint8_t *end1 = buf + st->size - 1;
119 int shift;
120
121 /* Compute the number of superfluous leading bytes */
122 for(; buf < end1; buf++) {
123 /*
124 * If the contents octets of an integer value encoding
125 * consist of more than one octet, then the bits of the
126 * first octet and bit 8 of the second octet:
127 * a) shall not all be ones; and
128 * b) shall not all be zero.
129 */
130 switch(*buf) {
131 case 0x00: if((buf[1] & 0x80) == 0)
132 continue;
133 break;
134 case 0xff: if((buf[1] & 0x80))
135 continue;
136 break;
137 }
138 break;
139 }
140
141 /* Remove leading superfluous bytes from the integer */
142 shift = buf - st->buf;
143 if(shift) {
144 uint8_t *nb = st->buf;
145 uint8_t *end;
146
147 st->size -= shift; /* New size, minus bad bytes */
148 end = nb + st->size;
149
150 for(; nb < end; nb++, buf++)
151 *nb = *buf;
152 }
153
154 } /* if(1) */
155
156 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
157 cb, app_key);
158 ASN_DEBUG("INTEGER %s wrote tags %d", sd->name, (int)erval.encoded);
159 if(erval.encoded == -1) {
160 erval.failed_type = sd;
161 erval.structure_ptr = ptr;
162 return erval;
163 }
164
165 if(cb && st->buf) {
166 ssize_t ret;
167
168 ret = cb(st->buf, st->size, app_key);
169 if(ret == -1) {
170 erval.encoded = -1;
171 erval.failed_type = sd;
172 erval.structure_ptr = ptr;
173 return erval;
174 }
175 } else {
176 assert(st->buf || st->size == 0);
177 }
178
179 erval.encoded += st->size;
180
181 return erval;
182}
183
184/*
185 * INTEGER specific human-readable output.
186 */
187int
188INTEGER_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
189 asn_app_consume_bytes_f *cb, void *app_key) {
vlm7d278c42004-07-19 17:30:25 +0000190 char scratch[32]; /* Enough for 64-bit integer */
vlmda674682004-08-11 09:07:36 +0000191 const INTEGER_t *st = (const INTEGER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000192 uint8_t *buf = st->buf;
193 uint8_t *buf_end = st->buf + st->size;
194 signed long accum;
195 char *p;
196 int ret;
197
vlmb42843a2004-06-05 08:17:50 +0000198 (void)td; /* Unused argument */
199 (void)ilevel; /* Unused argument */
200
vlmfa67ddc2004-06-03 03:38:44 +0000201 if(!st && !st->buf) return cb("<absent>", 8, app_key);
202
203 if(st->size == 0)
204 return cb("0", 1, app_key);
205
vlm7d278c42004-07-19 17:30:25 +0000206 /*
207 * Advance buf pointer until the start of the value's body.
208 * This will make us able to process large integers using simple case,
209 * when the actual value is small
210 * (0x0000000000abcdef would yield a fine 0x00abcdef)
211 */
212 /* Skip the insignificant leading bytes */
213 for(; buf < buf_end-1; buf++) {
214 switch(*buf) {
215 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
216 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
217 }
218 break;
219 }
220
vlmfa67ddc2004-06-03 03:38:44 +0000221 /* Simple case: the integer size is small */
vlm7d278c42004-07-19 17:30:25 +0000222 if((size_t)(buf_end - buf) <= sizeof(accum)) {
223 accum = (*buf & 0x80) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000224 for(; buf < buf_end; buf++)
225 accum = (accum << 8) | *buf;
226 ret = snprintf(scratch, sizeof(scratch), "%ld", accum);
vlmb42843a2004-06-05 08:17:50 +0000227 assert(ret > 0 && ret < (int)sizeof(scratch));
vlmfa67ddc2004-06-03 03:38:44 +0000228 return cb(scratch, ret, app_key);
229 }
230
231 /* Output in the long xx:yy:zz... format */
vlm7d278c42004-07-19 17:30:25 +0000232 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
vlmfa67ddc2004-06-03 03:38:44 +0000233 for(p = scratch; buf < buf_end; buf++) {
vlm1ff928d2004-08-11 08:10:13 +0000234 static const char *h2c = "0123456789ABCDEF";
vlm7d278c42004-07-19 17:30:25 +0000235 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
vlmfa67ddc2004-06-03 03:38:44 +0000236 /* Flush buffer */
237 if(cb(scratch, p - scratch, app_key))
238 return -1;
239 p = scratch;
240 }
241 *p++ = h2c[*buf >> 4];
242 *p++ = h2c[*buf & 0x0F];
243 *p++ = ':';
244 }
vlm7d278c42004-07-19 17:30:25 +0000245 if(p != scratch)
246 p--; /* Remove the last ':' */
vlmfa67ddc2004-06-03 03:38:44 +0000247
248 return cb(scratch, p - scratch, app_key);
249}
250
251void
252INTEGER_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlmda674682004-08-11 09:07:36 +0000253 INTEGER_t *st = (INTEGER_t *)ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000254
255 if(!td || !st)
256 return;
257
258 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, %p)",
259 td->name, contents_only, st, st->buf);
260
261 if(st->buf) {
262 FREEMEM(st->buf);
263 }
264
265 if(!contents_only) {
266 FREEMEM(st);
267 }
268}
269
270int
271asn1_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
272 uint8_t *b, *end;
273 size_t size;
274 long l;
275
276 /* Sanity checking */
277 if(!iptr || !iptr->buf || !lptr) {
278 errno = EINVAL;
279 return -1;
280 }
281
282 /* Cache the begin/end of the buffer */
283 b = iptr->buf; /* Start of the INTEGER buffer */
284 size = iptr->size;
285 end = b + size; /* Where to stop */
286
287 if(size > sizeof(long)) {
288 uint8_t *end1 = end - 1;
289 /*
290 * Slightly more advanced processing,
291 * able to >sizeof(long) bytes,
292 * when the actual value is small
293 * (0x0000000000abcdef would yield a fine 0x00abcdef)
294 */
295 /* Skip out the insignificant leading bytes */
296 for(; b < end1; b++) {
297 switch(*b) {
298 case 0x00: if((b[1] & 0x80) == 0) continue; break;
299 case 0xff: if((b[1] & 0x80) != 0) continue; break;
300 }
301 break;
302 }
303
304 size = end - b;
305 if(size > sizeof(long)) {
306 /* Still cannot fit the long */
307 errno = ERANGE;
308 return -1;
309 }
310 }
311
312 /* Shortcut processing of a corner case */
313 if(end == b) {
314 *lptr = 0;
315 return 0;
316 }
317
318 /* Perform the sign initialization */
319 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
320 if((*b >> 7)) l = -1; else l = 0;
321
322 /* Conversion engine */
323 for(; b < end; b++)
324 l = (l << 8) | *b;
325
326 *lptr = l;
327 return 0;
328}