blob: e4e52d416233c93a58dd75e4b427ea2f53ff16e0 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkine0b56e02005-02-25 12:10:27 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <INTEGER.h>
Lev Walkind703ff42004-10-21 11:21:25 +00008#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
10
11/*
12 * INTEGER basic type description.
13 */
Lev Walkin5e033762004-09-29 13:26:15 +000014static ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
16};
Lev Walkin5e033762004-09-29 13:26:15 +000017asn_TYPE_descriptor_t asn_DEF_INTEGER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000018 "INTEGER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000019 "INTEGER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000025 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 INTEGER_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000028 asn_DEF_INTEGER_tags,
29 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
30 asn_DEF_INTEGER_tags, /* Same as above */
31 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000032 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000033 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000034};
35
36/*
Lev Walkinf15320b2004-06-03 03:38:44 +000037 * Encode INTEGER type using DER.
38 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000039asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000040INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000041 int tag_mode, ber_tlv_tag_t tag,
42 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000043 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000044
45 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000046 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000047
48 /*
49 * Canonicalize integer in the buffer.
50 * (Remove too long sign extension, remove some first 0x00 bytes)
51 */
52 if(st->buf) {
53 uint8_t *buf = st->buf;
54 uint8_t *end1 = buf + st->size - 1;
55 int shift;
56
57 /* Compute the number of superfluous leading bytes */
58 for(; buf < end1; buf++) {
59 /*
60 * If the contents octets of an integer value encoding
61 * consist of more than one octet, then the bits of the
62 * first octet and bit 8 of the second octet:
63 * a) shall not all be ones; and
64 * b) shall not all be zero.
65 */
66 switch(*buf) {
67 case 0x00: if((buf[1] & 0x80) == 0)
68 continue;
69 break;
70 case 0xff: if((buf[1] & 0x80))
71 continue;
72 break;
73 }
74 break;
75 }
76
77 /* Remove leading superfluous bytes from the integer */
78 shift = buf - st->buf;
79 if(shift) {
80 uint8_t *nb = st->buf;
81 uint8_t *end;
82
83 st->size -= shift; /* New size, minus bad bytes */
84 end = nb + st->size;
85
86 for(; nb < end; nb++, buf++)
87 *nb = *buf;
88 }
89
90 } /* if(1) */
91
Lev Walkin8e8078a2004-09-26 13:10:40 +000092 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +000093}
94
Lev Walkinc2350112005-03-29 17:19:53 +000095static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +000096
Lev Walkinf15320b2004-06-03 03:38:44 +000097/*
98 * INTEGER specific human-readable output.
99 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000100static ssize_t
Lev Walkine0b56e02005-02-25 12:10:27 +0000101INTEGER__dump(asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
102 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000103 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 uint8_t *buf = st->buf;
105 uint8_t *buf_end = st->buf + st->size;
106 signed long accum;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000107 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 char *p;
109 int ret;
110
Lev Walkindb13f512004-07-19 17:30:25 +0000111 /*
112 * Advance buf pointer until the start of the value's body.
113 * This will make us able to process large integers using simple case,
114 * when the actual value is small
115 * (0x0000000000abcdef would yield a fine 0x00abcdef)
116 */
117 /* Skip the insignificant leading bytes */
118 for(; buf < buf_end-1; buf++) {
119 switch(*buf) {
120 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
121 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
122 }
123 break;
124 }
125
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 /* Simple case: the integer size is small */
Lev Walkindb13f512004-07-19 17:30:25 +0000127 if((size_t)(buf_end - buf) <= sizeof(accum)) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000128 const asn_INTEGER_enum_map_t *el;
129 size_t scrsize;
130 char *scr;
131
132 if(buf == buf_end) {
133 accum = 0;
134 } else {
135 accum = (*buf & 0x80) ? -1 : 0;
136 for(; buf < buf_end; buf++)
137 accum = (accum << 8) | *buf;
138 }
139
Lev Walkinc2350112005-03-29 17:19:53 +0000140 el = INTEGER_map_value2enum(specs, accum);
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 if(el) {
142 scrsize = el->enum_len + 32;
143 scr = (char *)alloca(scrsize);
144 if(plainOrXER == 0)
145 ret = snprintf(scr, scrsize,
146 "%ld (%s)", accum, el->enum_name);
147 else
148 ret = snprintf(scr, scrsize,
149 "<%s/>", el->enum_name);
150 } else if(plainOrXER && specs && specs->strict_enumeration) {
151 ASN_DEBUG("ASN.1 forbids dealing with "
152 "unknown value of ENUMERATED type");
153 errno = EPERM;
154 return -1;
155 } else {
156 scrsize = sizeof(scratch);
157 scr = scratch;
158 ret = snprintf(scr, scrsize, "%ld", accum);
159 }
160 assert(ret > 0 && (size_t)ret < scrsize);
161 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
162 } else if(plainOrXER && specs && specs->strict_enumeration) {
163 /*
164 * Here and earlier, we cannot encode the ENUMERATED values
165 * if there is no corresponding identifier.
166 */
167 ASN_DEBUG("ASN.1 forbids dealing with "
168 "unknown value of ENUMERATED type");
169 errno = EPERM;
170 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 }
172
173 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000174 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000176 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000177 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000179 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000181 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 p = scratch;
183 }
184 *p++ = h2c[*buf >> 4];
185 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000186 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 }
Lev Walkindb13f512004-07-19 17:30:25 +0000188 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000189 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000190
Lev Walkina9cc46e2004-09-22 16:06:28 +0000191 wrote += p - scratch;
192 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
193}
194
195/*
196 * INTEGER specific human-readable output.
197 */
198int
Lev Walkin5e033762004-09-29 13:26:15 +0000199INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 asn_app_consume_bytes_f *cb, void *app_key) {
201 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000202 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000203
204 (void)td;
205 (void)ilevel;
206
Lev Walkin8e8078a2004-09-26 13:10:40 +0000207 if(!st && !st->buf)
208 ret = cb("<absent>", 8, app_key);
209 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000210 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000211
Lev Walkin8e8078a2004-09-26 13:10:40 +0000212 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213}
214
Lev Walkine0b56e02005-02-25 12:10:27 +0000215struct e2v_key {
216 const char *start;
217 const char *stop;
218 asn_INTEGER_enum_map_t *vemap;
219 unsigned int *evmap;
220};
221static int
222INTEGER__compar_enum2value(const void *kp, const void *am) {
223 const struct e2v_key *key = (const struct e2v_key *)kp;
224 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
225 const char *ptr, *end, *name;
226
227 /* Remap the element (sort by different criterion) */
228 el = key->vemap + key->evmap[el - key->vemap];
229
230 /* Compare strings */
231 for(ptr = key->start, end = key->stop, name = el->enum_name;
232 ptr < end; ptr++, name++) {
233 if(*ptr != *name)
234 return *(const unsigned char *)ptr
235 - *(const unsigned char *)name;
236 }
237 return name[0] ? -1 : 0;
238}
239
240static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000241INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Lev Walkin9332b652005-03-04 11:18:44 +0000242 asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000243 int count = specs ? specs->map_count : 0;
244 struct e2v_key key;
245 const char *lp;
246
247 if(!count) return NULL;
248
249 /* Guaranteed: assert(lstart < lstop); */
250 /* Figure out the tag name */
251 for(lstart++, lp = lstart; lp < lstop; lp++) {
252 switch(*lp) {
253 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
254 case 0x2f: /* '/' */ case 0x3e: /* '>' */
255 break;
256 default:
257 continue;
258 }
259 break;
260 }
261 if(lp == lstop) return NULL; /* No tag found */
262 lstop = lp;
263
264 key.start = lstart;
265 key.stop = lstop;
266 key.vemap = specs->value2enum;
267 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000268 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
269 specs->value2enum, count, sizeof(specs->value2enum[0]),
270 INTEGER__compar_enum2value);
271 if(el_found) {
272 /* Remap enum2value into value2enum */
273 el_found = key.vemap + key.evmap[el_found - key.vemap];
274 }
275 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000276}
277
278static int
279INTEGER__compar_value2enum(const void *kp, const void *am) {
280 long a = *(const long *)kp;
281 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
282 long b = el->nat_value;
283 if(a < b) return -1;
284 else if(a == b) return 0;
285 else return 1;
286}
287
Lev Walkinc2350112005-03-29 17:19:53 +0000288const asn_INTEGER_enum_map_t *
289INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000290 int count = specs ? specs->map_count : 0;
291 if(!count) return 0;
292 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
293 count, sizeof(specs->value2enum[0]),
294 INTEGER__compar_value2enum);
295}
296
Lev Walkind703ff42004-10-21 11:21:25 +0000297/*
298 * Decode the chunk of XML text encoding INTEGER.
299 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000300static enum xer_pbd_rval
301INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000302 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkind703ff42004-10-21 11:21:25 +0000303 long sign = 1;
304 long value;
Lev Walkine0b56e02005-02-25 12:10:27 +0000305 const char *lp;
306 const char *lstart = (const char *)chunk_buf;
307 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000308 enum {
309 ST_SKIPSPACE,
310 ST_WAITDIGITS,
311 ST_DIGITS,
Lev Walkin806b0bb2005-03-09 22:31:22 +0000312 ST_EXTRASTUFF,
Lev Walkind703ff42004-10-21 11:21:25 +0000313 } state = ST_SKIPSPACE;
Lev Walkin0be3a992004-10-21 12:11:57 +0000314
Lev Walkind703ff42004-10-21 11:21:25 +0000315 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000316 * We may have received a tag here. It will be processed inline.
317 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000318 */
Lev Walkin0be3a992004-10-21 12:11:57 +0000319 for(value = 0, lp = lstart; lp < lstop; lp++) {
320 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000321 switch(lv) {
322 case 0x09: case 0x0a: case 0x0d: case 0x20:
323 if(state == ST_SKIPSPACE) continue;
324 break;
325 case 0x2d: /* '-' */
326 if(state == ST_SKIPSPACE) {
327 sign = -1;
328 state = ST_WAITDIGITS;
329 continue;
330 }
331 break;
332 case 0x2b: /* '+' */
333 if(state == ST_SKIPSPACE) {
334 state = ST_WAITDIGITS;
335 continue;
336 }
337 break;
338 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
339 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
340 if(state != ST_DIGITS) state = ST_DIGITS;
341
Lev Walkin0be3a992004-10-21 12:11:57 +0000342 {
343 long new_value = value * 10;
344
345 if(new_value / 10 != value)
346 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000347 return XPBD_DECODER_LIMIT;
Lev Walkin0be3a992004-10-21 12:11:57 +0000348
349 value = new_value + (lv - 0x30);
Lev Walkind703ff42004-10-21 11:21:25 +0000350 /* Check for two's complement overflow */
351 if(value < 0) {
352 /* Check whether it is a LONG_MIN */
353 if(sign == -1
Lev Walkin8471cec2004-10-21 14:02:19 +0000354 && (unsigned long)value
355 == ~((unsigned long)-1 >> 1)) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000356 sign = 1;
Lev Walkind703ff42004-10-21 11:21:25 +0000357 } else {
358 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000359 return XPBD_DECODER_LIMIT;
Lev Walkind703ff42004-10-21 11:21:25 +0000360 }
361 }
Lev Walkin0be3a992004-10-21 12:11:57 +0000362 }
Lev Walkind703ff42004-10-21 11:21:25 +0000363 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000364 case 0x3c: /* '<' */
365 if(state == ST_SKIPSPACE) {
366 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000367 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000368 (asn_INTEGER_specifics_t *)
369 td->specifics, lstart, lstop);
370 if(el) {
371 ASN_DEBUG("Found \"%s\" => %ld",
372 el->enum_name, el->nat_value);
373 state = ST_DIGITS;
374 value = el->nat_value;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000375 lp = lstop - 1;
376 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000377 }
378 ASN_DEBUG("Unknown identifier for INTEGER");
379 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000380 return XPBD_BROKEN_ENCODING;
Lev Walkind703ff42004-10-21 11:21:25 +0000381 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000382
383 /* Found extra non-numeric stuff */
384 state = ST_EXTRASTUFF;
Lev Walkin0be3a992004-10-21 12:11:57 +0000385 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000386 }
387
Lev Walkin0fab1a62005-03-09 22:19:25 +0000388 if(state != ST_DIGITS) {
Lev Walkin806b0bb2005-03-09 22:31:22 +0000389 if(xer_is_whitespace(lp, lstop - lp)) {
390 if(state != ST_EXTRASTUFF)
391 return XPBD_NOT_BODY_IGNORE;
392 /* Fall through */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000393 } else {
394 ASN_DEBUG("No useful digits in output");
395 return XPBD_BROKEN_ENCODING; /* No digits */
396 }
397 }
Lev Walkind703ff42004-10-21 11:21:25 +0000398
399 value *= sign; /* Change sign, if needed */
400
401 if(asn_long2INTEGER(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000402 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000403
Lev Walkin0fab1a62005-03-09 22:19:25 +0000404 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000405}
406
407asn_dec_rval_t
408INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
409 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000410 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000411
412 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000413 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000414 buf_ptr, size, INTEGER__xer_body_decode);
415}
416
Lev Walkina9cc46e2004-09-22 16:06:28 +0000417asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000418INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000419 int ilevel, enum xer_encoder_flags_e flags,
420 asn_app_consume_bytes_f *cb, void *app_key) {
421 const INTEGER_t *st = (const INTEGER_t *)sptr;
422 asn_enc_rval_t er;
423
424 (void)ilevel;
425 (void)flags;
426
427 if(!st && !st->buf)
428 _ASN_ENCODE_FAILED;
429
Lev Walkine0b56e02005-02-25 12:10:27 +0000430 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000431 if(er.encoded < 0) _ASN_ENCODE_FAILED;
432
433 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434}
435
Lev Walkinf15320b2004-06-03 03:38:44 +0000436int
Lev Walkin5e033762004-09-29 13:26:15 +0000437asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 uint8_t *b, *end;
439 size_t size;
440 long l;
441
442 /* Sanity checking */
443 if(!iptr || !iptr->buf || !lptr) {
444 errno = EINVAL;
445 return -1;
446 }
447
448 /* Cache the begin/end of the buffer */
449 b = iptr->buf; /* Start of the INTEGER buffer */
450 size = iptr->size;
451 end = b + size; /* Where to stop */
452
453 if(size > sizeof(long)) {
454 uint8_t *end1 = end - 1;
455 /*
456 * Slightly more advanced processing,
457 * able to >sizeof(long) bytes,
458 * when the actual value is small
459 * (0x0000000000abcdef would yield a fine 0x00abcdef)
460 */
461 /* Skip out the insignificant leading bytes */
462 for(; b < end1; b++) {
463 switch(*b) {
464 case 0x00: if((b[1] & 0x80) == 0) continue; break;
465 case 0xff: if((b[1] & 0x80) != 0) continue; break;
466 }
467 break;
468 }
469
470 size = end - b;
471 if(size > sizeof(long)) {
472 /* Still cannot fit the long */
473 errno = ERANGE;
474 return -1;
475 }
476 }
477
478 /* Shortcut processing of a corner case */
479 if(end == b) {
480 *lptr = 0;
481 return 0;
482 }
483
484 /* Perform the sign initialization */
485 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
486 if((*b >> 7)) l = -1; else l = 0;
487
488 /* Conversion engine */
489 for(; b < end; b++)
490 l = (l << 8) | *b;
491
492 *lptr = l;
493 return 0;
494}
Lev Walkind703ff42004-10-21 11:21:25 +0000495
496int
497asn_long2INTEGER(INTEGER_t *st, long value) {
498 uint8_t *buf, *bp;
499 uint8_t *p;
500 uint8_t *pstart;
501 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000502 int littleEndian = 1; /* Run-time detection */
503 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000504
505 if(!st) {
506 errno = EINVAL;
507 return -1;
508 }
509
Lev Walkin8471cec2004-10-21 14:02:19 +0000510 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000511 if(!buf) return -1;
512
Lev Walkind7ad5612004-10-26 08:20:46 +0000513 if(*(char *)&littleEndian) {
514 pstart = (uint8_t *)&value + sizeof(value) - 1;
515 pend1 = (uint8_t *)&value;
516 add = -1;
517 } else {
518 pstart = (uint8_t *)&value;
519 pend1 = pstart + sizeof(value) - 1;
520 add = 1;
521 }
522
Lev Walkind703ff42004-10-21 11:21:25 +0000523 /*
524 * If the contents octet consists of more than one octet,
525 * then bits of the first octet and bit 8 of the second octet:
526 * a) shall not all be ones; and
527 * b) shall not all be zero.
528 */
Lev Walkin33700162004-10-26 09:03:31 +0000529 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000530 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000531 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000532 continue;
533 break;
Lev Walkin33700162004-10-26 09:03:31 +0000534 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000535 continue;
536 break;
537 }
538 break;
539 }
540 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000541 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
542 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000543
544 if(st->buf) FREEMEM(st->buf);
545 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000546 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000547
548 return 0;
549}