blob: eeb9e5f29d1e40cf206324d510961c474c0a8949 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 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 <OBJECT_IDENTIFIER.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00007#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <assert.h>
9#include <errno.h>
10
11/*
12 * OBJECT IDENTIFIER basic type description.
13 */
Lev Walkin5e033762004-09-29 13:26:15 +000014static ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
16};
Lev Walkin5e033762004-09-29 13:26:15 +000017asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000018 "OBJECT IDENTIFIER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000019 "OBJECT_IDENTIFIER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 OBJECT_IDENTIFIER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 OBJECT_IDENTIFIER_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
24 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000025 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 OBJECT_IDENTIFIER_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_OBJECT_IDENTIFIER_tags,
29 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
30 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
31 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
32 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
33 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000034 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000035 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000036};
37
38
Lev Walkinf15320b2004-06-03 03:38:44 +000039int
Lev Walkin5e033762004-09-29 13:26:15 +000040OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000041 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000042 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000043
44 if(st && st->buf) {
45 if(st->size < 1) {
Lev Walkinba4e5182004-08-11 09:44:13 +000046 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000047 "%s: at least one numerical value "
48 "expected (%s:%d)",
49 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000050 return -1;
51 }
52 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000053 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000054 "%s: value not given (%s:%d)",
55 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000056 return -1;
57 }
58
59 return 0;
60}
61
Lev Walkinc4c61962004-06-14 08:17:27 +000062
Lev Walkinf15320b2004-06-03 03:38:44 +000063int
Lev Walkin8e8078a2004-09-26 13:10:40 +000064OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
Lev Walkin188ed2c2004-09-13 08:31:01 +000065 unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
Lev Walkin29a044b2004-06-14 07:24:36 +000066 uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000067 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000068 unsigned char *rvbuf = (unsigned char *)rvbufp;
69 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000070 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000071
Lev Walkin29a044b2004-06-14 07:24:36 +000072 rvsize *= CHAR_BIT; /* bytes to bits */
73 arclen *= 7; /* bytes to bits */
74
75 /*
76 * The arc has the number of bits
77 * cannot be represented using supplied return value type.
78 */
79 if(arclen > rvsize) {
80 if(arclen > (rvsize + CHAR_BIT)) {
81 errno = ERANGE; /* Overflow */
82 return -1;
83 } else {
84 /*
85 * Even if the number of bits in the arc representation
86 * is higher than the width of supplied * return value
87 * type, there is still possible to fit it when there
88 * are few unused high bits in the arc value
89 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000090 *
91 * Moreover, there is a possibility that the
92 * number could actually fit the arc space, given
93 * that add is negative, but we don't handle
94 * such "temporary lack of precision" situation here.
95 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +000096 */
97 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
98 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +000099 errno = ERANGE; /* Overflow */
100 return -1;
101 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000102 /* Fool the routine computing unused bits */
103 arclen -= 7;
104 cache = *arcbuf & 0x7f;
105 arcbuf++;
106 }
107 }
108
Lev Walkinc4c61962004-06-14 08:17:27 +0000109 /* Faster path for common size */
110 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
111 unsigned long accum;
112 /* Gather all bits into the accumulator */
113 for(accum = cache; arcbuf < arcend; arcbuf++)
114 accum = (accum << 7) | (*arcbuf & ~0x80);
115 if(accum < (unsigned)-add) {
116 errno = ERANGE; /* Overflow */
117 return -1;
118 }
Lev Walkin8e8078a2004-09-26 13:10:40 +0000119 *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000120 return 0;
121 }
122
Lev Walkin29a044b2004-06-14 07:24:36 +0000123#ifndef WORDS_BIGENDIAN
124 if(*(unsigned char *)&LE) { /* Little endian (x86) */
125 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000126 rvbuf += rvsize / CHAR_BIT - 1;
127 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000128 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000129 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000130#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000131 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000132
Lev Walkinc4c61962004-06-14 08:17:27 +0000133 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000134 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000135
Lev Walkin29a044b2004-06-14 07:24:36 +0000136 /* Clear the high unused bits */
137 for(bits = rvsize - arclen;
138 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000139 rvbuf += inc, bits -= CHAR_BIT)
140 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000141
Lev Walkin29a044b2004-06-14 07:24:36 +0000142 /* Fill the body of a value */
143 for(; arcbuf < arcend; arcbuf++) {
144 cache = (cache << 7) | (*arcbuf & 0x7f);
145 bits += 7;
146 if(bits >= CHAR_BIT) {
147 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000148 *rvbuf = (cache >> bits);
149 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000150 }
151 }
152 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000153 *rvbuf = cache;
154 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000155 }
156 }
157
158 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000159 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
160 int v = add + *rvbuf;
Lev Walkin29a044b2004-06-14 07:24:36 +0000161 if(v & (-1 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000162 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000163 add = -1;
164 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000165 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000166 break;
167 }
168 }
169 if(rvbuf == rvstart) {
170 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 errno = ERANGE; /* Overflow */
172 return -1;
173 }
174 }
175
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 return 0;
177}
178
Lev Walkina9cc46e2004-09-22 16:06:28 +0000179ssize_t
180OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 asn_app_consume_bytes_f *cb, void *app_key) {
182 char scratch[64]; /* Conservative estimate */
183 unsigned long accum; /* Bits accumulator */
184 char *p; /* Position in the scratch buffer */
185
Lev Walkin29a044b2004-06-14 07:24:36 +0000186 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
187 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 return -1;
189
Lev Walkin3251b8e2004-08-23 09:23:02 +0000190 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000191 ssize_t len;
192
Lev Walkin3251b8e2004-08-23 09:23:02 +0000193 /* Fill the scratch buffer in reverse. */
194 p = scratch + sizeof(scratch);
195 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000196 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000197
Lev Walkina9cc46e2004-09-22 16:06:28 +0000198 len = sizeof(scratch) - (p - scratch);
199 if(cb(p, len, app_key) < 0)
200 return -1;
201 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000202 } else {
203 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204 if(cb(scratch, 1, app_key) < 0)
205 return -1;
206 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000207 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000208}
209
210int
Lev Walkina9cc46e2004-09-22 16:06:28 +0000211OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
212 asn_app_consume_bytes_f *cb, void *app_key) {
213
214 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
215 return -1;
216
217 return 0;
218}
219
220static ssize_t
221OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
222 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 int startn;
224 int add = 0;
225 int i;
226
Lev Walkinf15320b2004-06-03 03:38:44 +0000227 for(i = 0, startn = 0; i < st->size; i++) {
228 uint8_t b = st->buf[i];
229 if((b & 0x80)) /* Continuation expected */
230 continue;
231
232 if(startn == 0) {
233 /*
234 * First two arcs are encoded through the backdoor.
235 */
236 if(i) {
237 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000238 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 } else if(b <= 39) {
240 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000241 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 } else if(b < 79) {
243 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000244 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 } else {
246 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000247 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000249 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 }
251
Lev Walkina9cc46e2004-09-22 16:06:28 +0000252 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000253 return -1;
254
Lev Walkina9cc46e2004-09-22 16:06:28 +0000255 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
256 i - startn + 1, add, cb, app_key);
257 if(add < 0) return -1;
258 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 startn = i + 1;
260 add = 0;
261 }
262
Lev Walkina9cc46e2004-09-22 16:06:28 +0000263 return wrote_len;
264}
265
Lev Walkin0fab1a62005-03-09 22:19:25 +0000266static enum xer_pbd_rval
267OBJECT_IDENTIFIER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin92302252004-10-23 10:16:51 +0000268 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
269 char *endptr;
270 long s_arcs[10];
271 long *arcs = s_arcs;
272 int arcs_count;
273 int ret;
274
Lev Walkine0b56e02005-02-25 12:10:27 +0000275 (void)td;
276
Lev Walkin92302252004-10-23 10:16:51 +0000277 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000278 (const char *)chunk_buf, chunk_size, arcs,
279 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
280 if(arcs_count <= 0) {
281 /* Expecting more than zero arcs */
282 return XPBD_BROKEN_ENCODING;
283 }
284
285 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000286 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000287 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000288 ret = OBJECT_IDENTIFIER_parse_arcs(
289 (const char *)chunk_buf, chunk_size,
290 arcs, arcs_count, &endptr);
291 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000292 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000293 }
294
295 /*
296 * Convert arcs into BER representation.
297 */
298 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000299 if(ret) return XPBD_BROKEN_ENCODING;
Lev Walkin92302252004-10-23 10:16:51 +0000300 if(arcs != s_arcs) FREEMEM(arcs);
301
Lev Walkin0fab1a62005-03-09 22:19:25 +0000302 return XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000303}
304
305asn_dec_rval_t
306OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
307 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
308 void *buf_ptr, size_t size) {
309
310 return xer_decode_primitive(opt_codec_ctx, td,
311 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
312 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
313}
314
Lev Walkina9cc46e2004-09-22 16:06:28 +0000315asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000316OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000317 int ilevel, enum xer_encoder_flags_e flags,
318 asn_app_consume_bytes_f *cb, void *app_key) {
319 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
320 asn_enc_rval_t er;
321
322 (void)ilevel;
323 (void)flags;
324
325 if(!st || !st->buf)
326 _ASN_ENCODE_FAILED;
327
328 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
329 if(er.encoded < 0) _ASN_ENCODE_FAILED;
330
331 return er;
332}
333
334int
Lev Walkin5e033762004-09-29 13:26:15 +0000335OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000336 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
337 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
338
339 (void)td; /* Unused argument */
340 (void)ilevel; /* Unused argument */
341
342 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000343 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000344
345 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000346 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000347 return -1;
348
349 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
350 return -1;
351
Lev Walkin8e8078a2004-09-26 13:10:40 +0000352 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000353}
354
355int
Lev Walkin29a044b2004-06-14 07:24:36 +0000356OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
357 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000358 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000359 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000360 int startn = 0;
361 int add = 0;
362 int i;
363
Lev Walkin0787ff02004-06-17 23:43:39 +0000364 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000365 errno = EINVAL;
366 return -1;
367 }
368
369 for(i = 0; i < oid->size; i++) {
370 uint8_t b = oid->buf[i];
371 if((b & 0x80)) /* Continuation expected */
372 continue;
373
Lev Walkin29a044b2004-06-14 07:24:36 +0000374 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000375 /*
376 * First two arcs are encoded through the backdoor.
377 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000378 unsigned LE = 1; /* Little endian */
379 int first_arc;
380 num_arcs++;
381 if(!arc_slots) { num_arcs++; continue; }
382
383 if(i) first_arc = 2;
384 else if(b <= 39) first_arc = 0;
385 else if(b < 79) first_arc = 1;
386 else first_arc = 2;
387
388 add = -40 * first_arc;
389 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000390 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000391 + ((*(char *)&LE)?0:(arc_type_size - 1)))
392 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000393 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000394 }
395
Lev Walkin29a044b2004-06-14 07:24:36 +0000396 /* Decode, if has space */
397 if(arcs < arcs_end) {
398 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
399 i - startn + 1, add,
400 arcs, arc_type_size))
401 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000403 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000404 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000406 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000407 }
408
Lev Walkin29a044b2004-06-14 07:24:36 +0000409 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000410}
411
Lev Walkin0787ff02004-06-17 23:43:39 +0000412
413/*
414 * Save the single value as an object identifier arc.
415 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000416int
Lev Walkin0787ff02004-06-17 23:43:39 +0000417OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
418 /*
419 * The following conditions must hold:
420 * assert(arcval);
421 * assert(arcval_size > 0);
422 * assert(arcbuf);
423 */
424#ifdef WORDS_BIGENDIAN
425 const unsigned isLittleEndian = 0;
426#else
427 unsigned LE = 1;
428 unsigned isLittleEndian = *(char *)&LE;
429#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000430 uint8_t *tp, *tend;
431 unsigned int cache;
432 uint8_t *bp = arcbuf;
433 int bits;
Lev Walkin64399722004-08-11 07:17:22 +0000434#ifdef __GNUC__
435 uint8_t buffer[arcval_size];
436#else
437 uint8_t *buffer = alloca(arcval_size);
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000438 if(!buffer) { errno = ENOMEM; return -1; }
Lev Walkin64399722004-08-11 07:17:22 +0000439#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000440
441 if(isLittleEndian && !prepared_order) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000442 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
Lev Walkinc2346572004-08-11 09:07:36 +0000443 uint8_t *aend = (uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000444 uint8_t *msb = buffer + arcval_size - 1;
445 for(tp = buffer; a >= aend; tp++, a--)
446 if((*tp = *a) && (tp < msb))
447 msb = tp;
448 tend = &buffer[arcval_size];
449 tp = msb; /* Most significant non-zero byte */
450 } else {
451 /* Look for most significant non-zero byte */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000452 tend = (unsigned char *)arcval + arcval_size;
Lev Walkinc2346572004-08-11 09:07:36 +0000453 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000454 if(*tp) break;
455 }
456
457 /*
458 * Split the value in 7-bits chunks.
459 */
460 bits = ((tend - tp) * CHAR_BIT) % 7;
461 if(bits) {
462 cache = *tp >> (CHAR_BIT - bits);
463 if(cache) {
464 *bp++ = cache | 0x80;
465 cache = *tp++;
466 bits = CHAR_BIT - bits;
467 } else {
468 bits = -bits;
469 }
470 } else {
471 cache = 0;
472 }
473 for(; tp < tend; tp++) {
474 cache = (cache << CHAR_BIT) + *tp;
475 bits += CHAR_BIT;
476 while(bits >= 7) {
477 bits -= 7;
478 *bp++ = 0x80 | (cache >> bits);
479 }
480 }
481 if(bits) *bp++ = cache;
482 bp[-1] &= 0x7f; /* Clear the last bit */
483
484 return bp - arcbuf;
485}
486
Lev Walkinf15320b2004-06-03 03:38:44 +0000487int
Lev Walkin0787ff02004-06-17 23:43:39 +0000488OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 uint8_t *buf;
490 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000491 unsigned LE = 1; /* Little endian (x86) */
492 unsigned isLittleEndian = *((char *)&LE);
493 unsigned int arc0;
494 unsigned int arc1;
495 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000496 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000497
Lev Walkin0787ff02004-06-17 23:43:39 +0000498 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000499 errno = EINVAL;
500 return -1;
501 }
502
Lev Walkin0787ff02004-06-17 23:43:39 +0000503 switch(arc_type_size) {
504 case sizeof(char):
505 arc0 = ((unsigned char *)arcs)[0];
506 arc1 = ((unsigned char *)arcs)[1];
507 break;
508 case sizeof(short):
509 arc0 = ((unsigned short *)arcs)[0];
510 arc1 = ((unsigned short *)arcs)[1];
511 break;
512 case sizeof(int):
513 arc0 = ((unsigned int *)arcs)[0];
514 arc1 = ((unsigned int *)arcs)[1];
515 break;
516 default:
517 arc1 = arc0 = 0;
518 if(isLittleEndian) { /* Little endian (x86) */
519 unsigned char *ps, *pe;
520 /* If more significant bytes are present,
521 * make them > 255 quick */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000522 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
523 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000524 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
525 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
526 arc0 = *((unsigned char *)arcs + 0);
527 arc1 = *((unsigned char *)arcs + arc_type_size);
528 } else {
529 unsigned char *ps, *pe;
530 /* If more significant bytes are present,
531 * make them > 255 quick */
Lev Walkinc2346572004-08-11 09:07:36 +0000532 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000533 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
534 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
535 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
536 }
537 }
538
539 /*
540 * The previous chapter left us with the first and the second arcs.
541 * The values are not precise (that is, they are valid only if
542 * they're less than 255), but OK for the purposes of making
543 * the sanity test below.
544 */
545 if(arc0 <= 1) {
546 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000547 /* 8.19.4: At most 39 subsequent values (including 0) */
548 errno = ERANGE;
549 return -1;
550 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000551 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 /* 8.19.4: Only three values are allocated from the root node */
553 errno = ERANGE;
554 return -1;
555 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000556 /*
557 * After above tests it is known that the value of arc0 is completely
558 * trustworthy (0..2). However, the arc1's value is still meaningless.
559 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000560
561 /*
562 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000563 * This estimation implicitly takes in account the following facts,
564 * that cancel each other:
565 * * the first two arcs are encoded in a single value.
566 * * the first value may require more space (+1 byte)
567 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000568 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000569 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000570 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 if(!buf) {
572 /* ENOMEM */
573 return -1;
574 }
575
576 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000577 * Encode the first two arcs.
578 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000581 uint8_t *tp;
Lev Walkin90fcd442004-08-11 07:48:19 +0000582#ifdef __GNUC__
583 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000584 uint8_t *fv = first_value;
Lev Walkin90fcd442004-08-11 07:48:19 +0000585#else
586 uint8_t *first_value = alloca(1 + arc_type_size);
Lev Walkin90fcd442004-08-11 07:48:19 +0000587 uint8_t *fv = first_value;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000588 if(!first_value) {
589 errno = ENOMEM;
590 return -1;
591 }
592#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000593
Lev Walkin0787ff02004-06-17 23:43:39 +0000594 /*
595 * Simulate first_value = arc0 * 40 + arc1;
596 */
597 /* Copy the second (1'st) arcs[1] into the first_value */
598 *fv++ = 0;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000599 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000600 if(isLittleEndian) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000601 uint8_t *aend = (unsigned char *)arcs - 1;
602 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000603 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000605 uint8_t *a1 = (uint8_t *)arcs;
Lev Walkin0787ff02004-06-17 23:43:39 +0000606 uint8_t *aend = a1 + arc_type_size;
607 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000608 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000609 /* Increase the first_value by arc0 */
610 arc0 *= 40; /* (0..80) */
611 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
612 unsigned int v = *tp;
613 v += arc0;
614 *tp = v;
615 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
616 else break;
617 }
618
619 assert(tp >= first_value);
620
621 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
622 fv - first_value, 1);
623 }
624
625 /*
626 * Save the rest of arcs.
627 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000628 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
629 i < arc_slots;
630 i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000631 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
632 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000633 }
634
Lev Walkin34b2a932004-06-17 23:46:45 +0000635 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000636
637 /*
638 * Replace buffer.
639 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000640 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000641 bp = oid->buf;
642 oid->buf = buf;
643 if(bp) FREEMEM(bp);
644
645 return 0;
646}
Lev Walkinc4c61962004-06-14 08:17:27 +0000647
Lev Walkin92302252004-10-23 10:16:51 +0000648
649int
650OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
651 long *arcs, unsigned int arcs_slots, char **oid_text_end) {
652 unsigned int arcs_count = 0;
653 const char *oid_end;
654 long value = 0;
655 enum {
656 ST_SKIPSPACE,
657 ST_WAITDIGITS, /* Next character is expected to be a digit */
658 ST_DIGITS,
659 } state = ST_SKIPSPACE;
660
661 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000662 if(oid_text_end) *(const char **)oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000663 errno = EINVAL;
664 return -1;
665 }
666
667 if(oid_txt_length == -1)
668 oid_txt_length = strlen(oid_text);
669
670 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
671 switch(*oid_text) {
672 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
673 if(state == ST_SKIPSPACE) {
674 continue;
675 } else {
676 break; /* Finish */
677 }
678 case 0x2e: /* '.' */
679 if(state != ST_DIGITS
680 || (oid_text + 1) == oid_end) {
681 state = ST_WAITDIGITS;
682 break;
683 }
684 if(arcs_count < arcs_slots)
685 arcs[arcs_count] = value;
686 arcs_count++;
687 state = ST_WAITDIGITS;
688 continue;
689 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
690 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
691 if(state != ST_DIGITS) {
692 state = ST_DIGITS;
693 value = 0;
694 }
695 if(1) {
696 long new_value = value * 10;
697 if(new_value / 10 != value
698 || (value = new_value + (*oid_text - 0x30)) < 0) {
699 /* Overflow */
700 state = ST_WAITDIGITS;
701 break;
702 }
703 continue;
704 }
705 default:
706 /* Unexpected symbols */
707 state = ST_WAITDIGITS;
708 break;
709 } /* switch() */
710 break;
711 } /* for() */
712
713
Lev Walkinc17d90f2005-01-17 14:32:45 +0000714 if(oid_text_end) *(const char **)oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000715
716 /* Finalize last arc */
717 switch(state) {
718 case ST_WAITDIGITS:
719 errno = EINVAL;
720 return -1;
721 case ST_DIGITS:
722 if(arcs_count < arcs_slots)
723 arcs[arcs_count] = value;
724 arcs_count++;
725 /* Fall through */
726 default:
727 return arcs_count;
728 }
729}
730
731