blob: 1af06c7c42083a92f0a9540284e578609eba3640 [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 */
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 */
Lev Walkind9bd7752004-06-05 08:17:50 +000026 0, /* Always in primitive form */
27 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000028};
29
30/*
31 * Decode INTEGER type.
32 */
33ber_dec_rval_t
34INTEGER_decode_ber(asn1_TYPE_descriptor_t *td,
35 void **int_structure, void *buf_ptr, size_t size, int tag_mode) {
36 INTEGER_t *st = *int_structure;
37 ber_dec_rval_t rval;
Lev Walkind9bd7752004-06-05 08:17:50 +000038 ber_dec_ctx_t ctx = { 0, 0, 0, 0 };
Lev Walkinf15320b2004-06-03 03:38:44 +000039 ber_tlv_len_t length;
40
41 /*
42 * If the structure is not there, allocate it.
43 */
44 if(st == NULL) {
45 st = *int_structure = CALLOC(1, sizeof(*st));
46 if(st == NULL) {
47 rval.code = RC_FAIL;
48 rval.consumed = 0;
49 return rval;
50 }
51 }
52
53 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
54 td->name, tag_mode);
55
56 /*
57 * Check tags.
58 */
59 rval = ber_check_tags(td, &ctx,
60 buf_ptr, size, tag_mode, &length, 0);
61 if(rval.code != RC_OK)
62 return rval;
63
64 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
65
66 /*
67 * Make sure we have this length.
68 */
Lev Walkin4d9528c2004-08-11 08:10:13 +000069 (char *)buf_ptr += rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000070 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +000071 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000072 rval.code = RC_WMORE;
73 rval.consumed = 0;
74 return rval;
75 }
76
77 st->buf = MALLOC(length);
78 if(st->buf) {
79 st->size = length;
80 } else {
81 rval.code = RC_FAIL;
82 rval.consumed = 0;
83 return rval;
84 }
85
86 memcpy(st->buf, buf_ptr, st->size);
87
88 rval.code = RC_OK;
89 rval.consumed += length;
90
91 ASN_DEBUG("Took %ld/%ld bytes to encode %s",
92 (long)rval.consumed,
93 (long)length, td->name);
94
95 return rval;
96}
97
98/*
99 * Encode INTEGER type using DER.
100 */
101der_enc_rval_t
102INTEGER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
103 int tag_mode, ber_tlv_tag_t tag,
104 asn_app_consume_bytes_f *cb, void *app_key) {
105 der_enc_rval_t erval;
106 INTEGER_t *st = ptr;
107
108 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
109 cb?"Encoding":"Estimating", sd->name, tag_mode);
110
111 /*
112 * Canonicalize integer in the buffer.
113 * (Remove too long sign extension, remove some first 0x00 bytes)
114 */
115 if(st->buf) {
116 uint8_t *buf = st->buf;
117 uint8_t *end1 = buf + st->size - 1;
118 int shift;
119
120 /* Compute the number of superfluous leading bytes */
121 for(; buf < end1; buf++) {
122 /*
123 * If the contents octets of an integer value encoding
124 * consist of more than one octet, then the bits of the
125 * first octet and bit 8 of the second octet:
126 * a) shall not all be ones; and
127 * b) shall not all be zero.
128 */
129 switch(*buf) {
130 case 0x00: if((buf[1] & 0x80) == 0)
131 continue;
132 break;
133 case 0xff: if((buf[1] & 0x80))
134 continue;
135 break;
136 }
137 break;
138 }
139
140 /* Remove leading superfluous bytes from the integer */
141 shift = buf - st->buf;
142 if(shift) {
143 uint8_t *nb = st->buf;
144 uint8_t *end;
145
146 st->size -= shift; /* New size, minus bad bytes */
147 end = nb + st->size;
148
149 for(; nb < end; nb++, buf++)
150 *nb = *buf;
151 }
152
153 } /* if(1) */
154
155 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
156 cb, app_key);
157 ASN_DEBUG("INTEGER %s wrote tags %d", sd->name, (int)erval.encoded);
158 if(erval.encoded == -1) {
159 erval.failed_type = sd;
160 erval.structure_ptr = ptr;
161 return erval;
162 }
163
164 if(cb && st->buf) {
165 ssize_t ret;
166
167 ret = cb(st->buf, st->size, app_key);
168 if(ret == -1) {
169 erval.encoded = -1;
170 erval.failed_type = sd;
171 erval.structure_ptr = ptr;
172 return erval;
173 }
174 } else {
175 assert(st->buf || st->size == 0);
176 }
177
178 erval.encoded += st->size;
179
180 return erval;
181}
182
183/*
184 * INTEGER specific human-readable output.
185 */
186int
187INTEGER_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
188 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkindb13f512004-07-19 17:30:25 +0000189 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 const INTEGER_t *st = sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 uint8_t *buf = st->buf;
192 uint8_t *buf_end = st->buf + st->size;
193 signed long accum;
194 char *p;
195 int ret;
196
Lev Walkind9bd7752004-06-05 08:17:50 +0000197 (void)td; /* Unused argument */
198 (void)ilevel; /* Unused argument */
199
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 if(!st && !st->buf) return cb("<absent>", 8, app_key);
201
202 if(st->size == 0)
203 return cb("0", 1, app_key);
204
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 Walkinf15320b2004-06-03 03:38:44 +0000227 return cb(scratch, ret, app_key);
228 }
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 */
236 if(cb(scratch, p - scratch, app_key))
237 return -1;
238 p = scratch;
239 }
240 *p++ = h2c[*buf >> 4];
241 *p++ = h2c[*buf & 0x0F];
242 *p++ = ':';
243 }
Lev Walkindb13f512004-07-19 17:30:25 +0000244 if(p != scratch)
245 p--; /* Remove the last ':' */
Lev Walkinf15320b2004-06-03 03:38:44 +0000246
247 return cb(scratch, p - scratch, app_key);
248}
249
250void
251INTEGER_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
252 INTEGER_t *st = ptr;
253
254 if(!td || !st)
255 return;
256
257 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, %p)",
258 td->name, contents_only, st, st->buf);
259
260 if(st->buf) {
261 FREEMEM(st->buf);
262 }
263
264 if(!contents_only) {
265 FREEMEM(st);
266 }
267}
268
269int
270asn1_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
271 uint8_t *b, *end;
272 size_t size;
273 long l;
274
275 /* Sanity checking */
276 if(!iptr || !iptr->buf || !lptr) {
277 errno = EINVAL;
278 return -1;
279 }
280
281 /* Cache the begin/end of the buffer */
282 b = iptr->buf; /* Start of the INTEGER buffer */
283 size = iptr->size;
284 end = b + size; /* Where to stop */
285
286 if(size > sizeof(long)) {
287 uint8_t *end1 = end - 1;
288 /*
289 * Slightly more advanced processing,
290 * able to >sizeof(long) bytes,
291 * when the actual value is small
292 * (0x0000000000abcdef would yield a fine 0x00abcdef)
293 */
294 /* Skip out the insignificant leading bytes */
295 for(; b < end1; b++) {
296 switch(*b) {
297 case 0x00: if((b[1] & 0x80) == 0) continue; break;
298 case 0xff: if((b[1] & 0x80) != 0) continue; break;
299 }
300 break;
301 }
302
303 size = end - b;
304 if(size > sizeof(long)) {
305 /* Still cannot fit the long */
306 errno = ERANGE;
307 return -1;
308 }
309 }
310
311 /* Shortcut processing of a corner case */
312 if(end == b) {
313 *lptr = 0;
314 return 0;
315 }
316
317 /* Perform the sign initialization */
318 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
319 if((*b >> 7)) l = -1; else l = 0;
320
321 /* Conversion engine */
322 for(; b < end; b++)
323 l = (l << 8) | *b;
324
325 *lptr = l;
326 return 0;
327}