blob: 75782a689c7decbc41301016dedb0389ad55c6d8 [file] [log] [blame]
Lev Walkinf15320b2004-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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <INTEGER.h>
7#include <assert.h>
8#include <errno.h>
9
10/*
11 * INTEGER basic type description.
12 */
13static ber_tlv_tag_t asn1_DEF_INTEGER_tags[] = {
14 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
15};
16asn1_TYPE_descriptor_t asn1_DEF_INTEGER = {
17 "INTEGER",
Lev Walkina9cc46e2004-09-22 16:06:28 +000018 INTEGER_free,
19 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000020 asn_generic_no_constraint,
21 INTEGER_decode_ber,
22 INTEGER_encode_der,
Lev Walkina9cc46e2004-09-22 16:06:28 +000023 0, /* Not implemented yet */
24 INTEGER_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000025 0, /* Use generic outmost tag fetcher */
26 asn1_DEF_INTEGER_tags,
Lev Walkin188ed2c2004-09-13 08:31:01 +000027 sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
28 asn1_DEF_INTEGER_tags, /* Same as above */
29 sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
Lev Walkind9bd7752004-06-05 08:17:50 +000030 0, /* Always in primitive form */
Lev Walkin449f8322004-08-20 13:23:42 +000031 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000032 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000033};
34
35/*
36 * Decode INTEGER type.
37 */
38ber_dec_rval_t
39INTEGER_decode_ber(asn1_TYPE_descriptor_t *td,
40 void **int_structure, void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinc2346572004-08-11 09:07:36 +000041 INTEGER_t *st = (INTEGER_t *)*int_structure;
Lev Walkinf15320b2004-06-03 03:38:44 +000042 ber_dec_rval_t rval;
Lev Walkind9bd7752004-06-05 08:17:50 +000043 ber_dec_ctx_t ctx = { 0, 0, 0, 0 };
Lev Walkinf15320b2004-06-03 03:38:44 +000044 ber_tlv_len_t length;
45
46 /*
47 * If the structure is not there, allocate it.
48 */
49 if(st == NULL) {
Lev Walkinc2346572004-08-11 09:07:36 +000050 (void *)st = *int_structure = CALLOC(1, sizeof(*st));
Lev Walkinf15320b2004-06-03 03:38:44 +000051 if(st == NULL) {
52 rval.code = RC_FAIL;
53 rval.consumed = 0;
54 return rval;
55 }
56 }
57
58 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
59 td->name, tag_mode);
60
61 /*
62 * Check tags.
63 */
64 rval = ber_check_tags(td, &ctx,
65 buf_ptr, size, tag_mode, &length, 0);
66 if(rval.code != RC_OK)
67 return rval;
68
69 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
70
71 /*
72 * Make sure we have this length.
73 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +000074 buf_ptr = ((char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000075 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +000076 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000077 rval.code = RC_WMORE;
78 rval.consumed = 0;
79 return rval;
80 }
81
Lev Walkinc2346572004-08-11 09:07:36 +000082 st->buf = (uint8_t *)MALLOC(length);
Lev Walkinf15320b2004-06-03 03:38:44 +000083 if(st->buf) {
84 st->size = length;
85 } else {
86 rval.code = RC_FAIL;
87 rval.consumed = 0;
88 return rval;
89 }
90
91 memcpy(st->buf, buf_ptr, st->size);
92
93 rval.code = RC_OK;
94 rval.consumed += length;
95
96 ASN_DEBUG("Took %ld/%ld bytes to encode %s",
97 (long)rval.consumed,
98 (long)length, td->name);
99
100 return rval;
101}
102
103/*
104 * Encode INTEGER type using DER.
105 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000106asn_enc_rval_t
Lev Walkinf15320b2004-06-03 03:38:44 +0000107INTEGER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
108 int tag_mode, ber_tlv_tag_t tag,
109 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000110 asn_enc_rval_t erval;
Lev Walkinc2346572004-08-11 09:07:36 +0000111 INTEGER_t *st = (INTEGER_t *)ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000112
113 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
114 cb?"Encoding":"Estimating", sd->name, tag_mode);
115
116 /*
117 * Canonicalize integer in the buffer.
118 * (Remove too long sign extension, remove some first 0x00 bytes)
119 */
120 if(st->buf) {
121 uint8_t *buf = st->buf;
122 uint8_t *end1 = buf + st->size - 1;
123 int shift;
124
125 /* Compute the number of superfluous leading bytes */
126 for(; buf < end1; buf++) {
127 /*
128 * If the contents octets of an integer value encoding
129 * consist of more than one octet, then the bits of the
130 * first octet and bit 8 of the second octet:
131 * a) shall not all be ones; and
132 * b) shall not all be zero.
133 */
134 switch(*buf) {
135 case 0x00: if((buf[1] & 0x80) == 0)
136 continue;
137 break;
138 case 0xff: if((buf[1] & 0x80))
139 continue;
140 break;
141 }
142 break;
143 }
144
145 /* Remove leading superfluous bytes from the integer */
146 shift = buf - st->buf;
147 if(shift) {
148 uint8_t *nb = st->buf;
149 uint8_t *end;
150
151 st->size -= shift; /* New size, minus bad bytes */
152 end = nb + st->size;
153
154 for(; nb < end; nb++, buf++)
155 *nb = *buf;
156 }
157
158 } /* if(1) */
159
160 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
161 cb, app_key);
162 ASN_DEBUG("INTEGER %s wrote tags %d", sd->name, (int)erval.encoded);
163 if(erval.encoded == -1) {
164 erval.failed_type = sd;
165 erval.structure_ptr = ptr;
166 return erval;
167 }
168
169 if(cb && st->buf) {
170 ssize_t ret;
171
172 ret = cb(st->buf, st->size, app_key);
173 if(ret == -1) {
174 erval.encoded = -1;
175 erval.failed_type = sd;
176 erval.structure_ptr = ptr;
177 return erval;
178 }
179 } else {
180 assert(st->buf || st->size == 0);
181 }
182
183 erval.encoded += st->size;
184
185 return erval;
186}
187
188/*
189 * INTEGER specific human-readable output.
190 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000191static ssize_t
192INTEGER__dump(const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkindb13f512004-07-19 17:30:25 +0000193 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 uint8_t *buf = st->buf;
195 uint8_t *buf_end = st->buf + st->size;
196 signed long accum;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000198 char *p;
199 int ret;
200
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201 if(st->size == 0) {
202 return (cb("0", 1, app_key) < 0) ? -1 : 1;
203 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000204
Lev Walkindb13f512004-07-19 17:30:25 +0000205 /*
206 * Advance buf pointer until the start of the value's body.
207 * This will make us able to process large integers using simple case,
208 * when the actual value is small
209 * (0x0000000000abcdef would yield a fine 0x00abcdef)
210 */
211 /* Skip the insignificant leading bytes */
212 for(; buf < buf_end-1; buf++) {
213 switch(*buf) {
214 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
215 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
216 }
217 break;
218 }
219
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 /* Simple case: the integer size is small */
Lev Walkindb13f512004-07-19 17:30:25 +0000221 if((size_t)(buf_end - buf) <= sizeof(accum)) {
222 accum = (*buf & 0x80) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 for(; buf < buf_end; buf++)
224 accum = (accum << 8) | *buf;
225 ret = snprintf(scratch, sizeof(scratch), "%ld", accum);
Lev Walkind9bd7752004-06-05 08:17:50 +0000226 assert(ret > 0 && ret < (int)sizeof(scratch));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000227 return (cb(scratch, ret, app_key) < 0) ? -1 : ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000228 }
229
230 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000231 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000233 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000234 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000236 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000237 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000238 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 p = scratch;
240 }
241 *p++ = h2c[*buf >> 4];
242 *p++ = h2c[*buf & 0x0F];
243 *p++ = ':';
244 }
Lev Walkindb13f512004-07-19 17:30:25 +0000245 if(p != scratch)
246 p--; /* Remove the last ':' */
Lev Walkinf15320b2004-06-03 03:38:44 +0000247
Lev Walkina9cc46e2004-09-22 16:06:28 +0000248 wrote += p - scratch;
249 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
250}
251
252/*
253 * INTEGER specific human-readable output.
254 */
255int
256INTEGER_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
257 asn_app_consume_bytes_f *cb, void *app_key) {
258 const INTEGER_t *st = (const INTEGER_t *)sptr;
259
260 (void)td;
261 (void)ilevel;
262
263 if(!st && !st->buf) return cb("<absent>", 8, app_key);
264
265 return (INTEGER__dump(st, cb, app_key) < 0) ? -1 : 0;
266}
267
268asn_enc_rval_t
269INTEGER_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
270 int ilevel, enum xer_encoder_flags_e flags,
271 asn_app_consume_bytes_f *cb, void *app_key) {
272 const INTEGER_t *st = (const INTEGER_t *)sptr;
273 asn_enc_rval_t er;
274
275 (void)ilevel;
276 (void)flags;
277
278 if(!st && !st->buf)
279 _ASN_ENCODE_FAILED;
280
281 er.encoded = INTEGER__dump(st, cb, app_key);
282 if(er.encoded < 0) _ASN_ENCODE_FAILED;
283
284 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000285}
286
287void
288INTEGER_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +0000289 INTEGER_t *st = (INTEGER_t *)ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000290
291 if(!td || !st)
292 return;
293
294 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, %p)",
295 td->name, contents_only, st, st->buf);
296
297 if(st->buf) {
298 FREEMEM(st->buf);
299 }
300
301 if(!contents_only) {
302 FREEMEM(st);
303 }
304}
305
306int
307asn1_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
308 uint8_t *b, *end;
309 size_t size;
310 long l;
311
312 /* Sanity checking */
313 if(!iptr || !iptr->buf || !lptr) {
314 errno = EINVAL;
315 return -1;
316 }
317
318 /* Cache the begin/end of the buffer */
319 b = iptr->buf; /* Start of the INTEGER buffer */
320 size = iptr->size;
321 end = b + size; /* Where to stop */
322
323 if(size > sizeof(long)) {
324 uint8_t *end1 = end - 1;
325 /*
326 * Slightly more advanced processing,
327 * able to >sizeof(long) bytes,
328 * when the actual value is small
329 * (0x0000000000abcdef would yield a fine 0x00abcdef)
330 */
331 /* Skip out the insignificant leading bytes */
332 for(; b < end1; b++) {
333 switch(*b) {
334 case 0x00: if((b[1] & 0x80) == 0) continue; break;
335 case 0xff: if((b[1] & 0x80) != 0) continue; break;
336 }
337 break;
338 }
339
340 size = end - b;
341 if(size > sizeof(long)) {
342 /* Still cannot fit the long */
343 errno = ERANGE;
344 return -1;
345 }
346 }
347
348 /* Shortcut processing of a corner case */
349 if(end == b) {
350 *lptr = 0;
351 return 0;
352 }
353
354 /* Perform the sign initialization */
355 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
356 if((*b >> 7)) l = -1; else l = 0;
357
358 /* Conversion engine */
359 for(; b < end; b++)
360 l = (l << 8) | *b;
361
362 *lptr = l;
363 return 0;
364}