blob: f485bacbc22958c461762af742d5aa4b21a0e046 [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 Walkin8e8078a2004-09-26 13:10:40 +000019 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000020 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000021 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000022 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 INTEGER_encode_der,
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 0, /* Not implemented yet */
25 INTEGER_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000027 asn_DEF_INTEGER_tags,
28 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
29 asn_DEF_INTEGER_tags, /* Same as above */
30 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
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/*
Lev Walkinf15320b2004-06-03 03:38:44 +000036 * Encode INTEGER type using DER.
37 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000038asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000039INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000040 int tag_mode, ber_tlv_tag_t tag,
41 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000042 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000043
44 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000045 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000046
47 /*
48 * Canonicalize integer in the buffer.
49 * (Remove too long sign extension, remove some first 0x00 bytes)
50 */
51 if(st->buf) {
52 uint8_t *buf = st->buf;
53 uint8_t *end1 = buf + st->size - 1;
54 int shift;
55
56 /* Compute the number of superfluous leading bytes */
57 for(; buf < end1; buf++) {
58 /*
59 * If the contents octets of an integer value encoding
60 * consist of more than one octet, then the bits of the
61 * first octet and bit 8 of the second octet:
62 * a) shall not all be ones; and
63 * b) shall not all be zero.
64 */
65 switch(*buf) {
66 case 0x00: if((buf[1] & 0x80) == 0)
67 continue;
68 break;
69 case 0xff: if((buf[1] & 0x80))
70 continue;
71 break;
72 }
73 break;
74 }
75
76 /* Remove leading superfluous bytes from the integer */
77 shift = buf - st->buf;
78 if(shift) {
79 uint8_t *nb = st->buf;
80 uint8_t *end;
81
82 st->size -= shift; /* New size, minus bad bytes */
83 end = nb + st->size;
84
85 for(; nb < end; nb++, buf++)
86 *nb = *buf;
87 }
88
89 } /* if(1) */
90
Lev Walkin8e8078a2004-09-26 13:10:40 +000091 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +000092}
93
94/*
95 * INTEGER specific human-readable output.
96 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000097static ssize_t
98INTEGER__dump(const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkindb13f512004-07-19 17:30:25 +000099 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000100 uint8_t *buf = st->buf;
101 uint8_t *buf_end = st->buf + st->size;
102 signed long accum;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000103 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 char *p;
105 int ret;
106
Lev Walkina9cc46e2004-09-22 16:06:28 +0000107 if(st->size == 0) {
108 return (cb("0", 1, app_key) < 0) ? -1 : 1;
109 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000110
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)) {
128 accum = (*buf & 0x80) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 for(; buf < buf_end; buf++)
130 accum = (accum << 8) | *buf;
131 ret = snprintf(scratch, sizeof(scratch), "%ld", accum);
Lev Walkind9bd7752004-06-05 08:17:50 +0000132 assert(ret > 0 && ret < (int)sizeof(scratch));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000133 return (cb(scratch, ret, app_key) < 0) ? -1 : ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 }
135
136 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000137 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000139 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000140 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000141 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000142 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000144 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 p = scratch;
146 }
147 *p++ = h2c[*buf >> 4];
148 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000149 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 }
Lev Walkindb13f512004-07-19 17:30:25 +0000151 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000152 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000153
Lev Walkina9cc46e2004-09-22 16:06:28 +0000154 wrote += p - scratch;
155 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
156}
157
158/*
159 * INTEGER specific human-readable output.
160 */
161int
Lev Walkin5e033762004-09-29 13:26:15 +0000162INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000163 asn_app_consume_bytes_f *cb, void *app_key) {
164 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000165 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000166
167 (void)td;
168 (void)ilevel;
169
Lev Walkin8e8078a2004-09-26 13:10:40 +0000170 if(!st && !st->buf)
171 ret = cb("<absent>", 8, app_key);
172 else
173 ret = INTEGER__dump(st, cb, app_key);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174
Lev Walkin8e8078a2004-09-26 13:10:40 +0000175 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000176}
177
178asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000179INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000180 int ilevel, enum xer_encoder_flags_e flags,
181 asn_app_consume_bytes_f *cb, void *app_key) {
182 const INTEGER_t *st = (const INTEGER_t *)sptr;
183 asn_enc_rval_t er;
184
185 (void)ilevel;
186 (void)flags;
187
188 if(!st && !st->buf)
189 _ASN_ENCODE_FAILED;
190
191 er.encoded = INTEGER__dump(st, cb, app_key);
192 if(er.encoded < 0) _ASN_ENCODE_FAILED;
193
194 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195}
196
Lev Walkinf15320b2004-06-03 03:38:44 +0000197int
Lev Walkin5e033762004-09-29 13:26:15 +0000198asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 uint8_t *b, *end;
200 size_t size;
201 long l;
202
203 /* Sanity checking */
204 if(!iptr || !iptr->buf || !lptr) {
205 errno = EINVAL;
206 return -1;
207 }
208
209 /* Cache the begin/end of the buffer */
210 b = iptr->buf; /* Start of the INTEGER buffer */
211 size = iptr->size;
212 end = b + size; /* Where to stop */
213
214 if(size > sizeof(long)) {
215 uint8_t *end1 = end - 1;
216 /*
217 * Slightly more advanced processing,
218 * able to >sizeof(long) bytes,
219 * when the actual value is small
220 * (0x0000000000abcdef would yield a fine 0x00abcdef)
221 */
222 /* Skip out the insignificant leading bytes */
223 for(; b < end1; b++) {
224 switch(*b) {
225 case 0x00: if((b[1] & 0x80) == 0) continue; break;
226 case 0xff: if((b[1] & 0x80) != 0) continue; break;
227 }
228 break;
229 }
230
231 size = end - b;
232 if(size > sizeof(long)) {
233 /* Still cannot fit the long */
234 errno = ERANGE;
235 return -1;
236 }
237 }
238
239 /* Shortcut processing of a corner case */
240 if(end == b) {
241 *lptr = 0;
242 return 0;
243 }
244
245 /* Perform the sign initialization */
246 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
247 if((*b >> 7)) l = -1; else l = 0;
248
249 /* Conversion engine */
250 for(; b < end; b++)
251 l = (l << 8) | *b;
252
253 *lptr = l;
254 return 0;
255}