blob: fbd927243aeac358a59372b19e5166b61d9db516 [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>
Lev Walkin8e8078a2004-09-26 13:10:40 +00007#include <ber_codec_prim.h> /* Encoder and decoder of a primitive */
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <assert.h>
9#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 Walkina9cc46e2004-09-22 16:06:28 +000025 0, /* Not implemented yet */
26 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
95/*
96 * INTEGER specific human-readable output.
97 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000098static ssize_t
99INTEGER__dump(const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkindb13f512004-07-19 17:30:25 +0000100 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 uint8_t *buf = st->buf;
102 uint8_t *buf_end = st->buf + st->size;
103 signed long accum;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000104 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 char *p;
106 int ret;
107
Lev Walkina9cc46e2004-09-22 16:06:28 +0000108 if(st->size == 0) {
109 return (cb("0", 1, app_key) < 0) ? -1 : 1;
110 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000111
Lev Walkindb13f512004-07-19 17:30:25 +0000112 /*
113 * Advance buf pointer until the start of the value's body.
114 * This will make us able to process large integers using simple case,
115 * when the actual value is small
116 * (0x0000000000abcdef would yield a fine 0x00abcdef)
117 */
118 /* Skip the insignificant leading bytes */
119 for(; buf < buf_end-1; buf++) {
120 switch(*buf) {
121 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
122 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
123 }
124 break;
125 }
126
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 /* Simple case: the integer size is small */
Lev Walkindb13f512004-07-19 17:30:25 +0000128 if((size_t)(buf_end - buf) <= sizeof(accum)) {
129 accum = (*buf & 0x80) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 for(; buf < buf_end; buf++)
131 accum = (accum << 8) | *buf;
132 ret = snprintf(scratch, sizeof(scratch), "%ld", accum);
Lev Walkind9bd7752004-06-05 08:17:50 +0000133 assert(ret > 0 && ret < (int)sizeof(scratch));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000134 return (cb(scratch, ret, app_key) < 0) ? -1 : ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 }
136
137 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000138 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000139 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000140 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000141 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000143 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000145 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 p = scratch;
147 }
148 *p++ = h2c[*buf >> 4];
149 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000150 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 }
Lev Walkindb13f512004-07-19 17:30:25 +0000152 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000153 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000154
Lev Walkina9cc46e2004-09-22 16:06:28 +0000155 wrote += p - scratch;
156 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
157}
158
159/*
160 * INTEGER specific human-readable output.
161 */
162int
Lev Walkin5e033762004-09-29 13:26:15 +0000163INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000164 asn_app_consume_bytes_f *cb, void *app_key) {
165 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000166 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000167
168 (void)td;
169 (void)ilevel;
170
Lev Walkin8e8078a2004-09-26 13:10:40 +0000171 if(!st && !st->buf)
172 ret = cb("<absent>", 8, app_key);
173 else
174 ret = INTEGER__dump(st, cb, app_key);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000175
Lev Walkin8e8078a2004-09-26 13:10:40 +0000176 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000177}
178
179asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000180INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000181 int ilevel, enum xer_encoder_flags_e flags,
182 asn_app_consume_bytes_f *cb, void *app_key) {
183 const INTEGER_t *st = (const INTEGER_t *)sptr;
184 asn_enc_rval_t er;
185
186 (void)ilevel;
187 (void)flags;
188
189 if(!st && !st->buf)
190 _ASN_ENCODE_FAILED;
191
192 er.encoded = INTEGER__dump(st, cb, app_key);
193 if(er.encoded < 0) _ASN_ENCODE_FAILED;
194
195 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000196}
197
Lev Walkinf15320b2004-06-03 03:38:44 +0000198int
Lev Walkin5e033762004-09-29 13:26:15 +0000199asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 uint8_t *b, *end;
201 size_t size;
202 long l;
203
204 /* Sanity checking */
205 if(!iptr || !iptr->buf || !lptr) {
206 errno = EINVAL;
207 return -1;
208 }
209
210 /* Cache the begin/end of the buffer */
211 b = iptr->buf; /* Start of the INTEGER buffer */
212 size = iptr->size;
213 end = b + size; /* Where to stop */
214
215 if(size > sizeof(long)) {
216 uint8_t *end1 = end - 1;
217 /*
218 * Slightly more advanced processing,
219 * able to >sizeof(long) bytes,
220 * when the actual value is small
221 * (0x0000000000abcdef would yield a fine 0x00abcdef)
222 */
223 /* Skip out the insignificant leading bytes */
224 for(; b < end1; b++) {
225 switch(*b) {
226 case 0x00: if((b[1] & 0x80) == 0) continue; break;
227 case 0xff: if((b[1] & 0x80) != 0) continue; break;
228 }
229 break;
230 }
231
232 size = end - b;
233 if(size > sizeof(long)) {
234 /* Still cannot fit the long */
235 errno = ERANGE;
236 return -1;
237 }
238 }
239
240 /* Shortcut processing of a corner case */
241 if(end == b) {
242 *lptr = 0;
243 return 0;
244 }
245
246 /* Perform the sign initialization */
247 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
248 if((*b >> 7)) l = -1; else l = 0;
249
250 /* Conversion engine */
251 for(; b < end; b++)
252 l = (l << 8) | *b;
253
254 *lptr = l;
255 return 0;
256}