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