blob: 0d7043e6b9fd20f595b45469c9dfac791e02045e [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 Walkin725883b2006-10-09 12:07:58 +00007#include <OCTET_STRING.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00008#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#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 Walkin725883b2006-10-09 12:07:58 +000027 OCTET_STRING_decode_uper,
28 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +000029 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000030 asn_DEF_OBJECT_IDENTIFIER_tags,
31 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
32 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
33 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
34 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
35 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000036 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000037 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000038 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000039};
40
41
Lev Walkinf15320b2004-06-03 03:38:44 +000042int
Lev Walkin5e033762004-09-29 13:26:15 +000043OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000044 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000045 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000046
47 if(st && st->buf) {
48 if(st->size < 1) {
Lev Walkined9019a2006-10-16 12:18:41 +000049 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000050 "%s: at least one numerical value "
51 "expected (%s:%d)",
52 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000053 return -1;
54 }
55 } else {
Lev Walkined9019a2006-10-16 12:18:41 +000056 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000057 "%s: value not given (%s:%d)",
58 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000059 return -1;
60 }
61
62 return 0;
63}
64
Lev Walkinc4c61962004-06-14 08:17:27 +000065
Lev Walkinf15320b2004-06-03 03:38:44 +000066int
Lev Walkin8e8078a2004-09-26 13:10:40 +000067OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
Lev Walkin50b0f6c2006-10-16 12:01:36 +000068 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Lev Walkin29a044b2004-06-14 07:24:36 +000069 uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000070 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000071 unsigned char *rvbuf = (unsigned char *)rvbufp;
72 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000073 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000074
Lev Walkin29a044b2004-06-14 07:24:36 +000075 rvsize *= CHAR_BIT; /* bytes to bits */
76 arclen *= 7; /* bytes to bits */
77
78 /*
79 * The arc has the number of bits
80 * cannot be represented using supplied return value type.
81 */
82 if(arclen > rvsize) {
83 if(arclen > (rvsize + CHAR_BIT)) {
84 errno = ERANGE; /* Overflow */
85 return -1;
86 } else {
87 /*
88 * Even if the number of bits in the arc representation
89 * is higher than the width of supplied * return value
90 * type, there is still possible to fit it when there
91 * are few unused high bits in the arc value
92 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000093 *
94 * Moreover, there is a possibility that the
95 * number could actually fit the arc space, given
96 * that add is negative, but we don't handle
97 * such "temporary lack of precision" situation here.
98 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +000099 */
100 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
101 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 errno = ERANGE; /* Overflow */
103 return -1;
104 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000105 /* Fool the routine computing unused bits */
106 arclen -= 7;
107 cache = *arcbuf & 0x7f;
108 arcbuf++;
109 }
110 }
111
Lev Walkinc4c61962004-06-14 08:17:27 +0000112 /* Faster path for common size */
113 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
114 unsigned long accum;
115 /* Gather all bits into the accumulator */
116 for(accum = cache; arcbuf < arcend; arcbuf++)
117 accum = (accum << 7) | (*arcbuf & ~0x80);
118 if(accum < (unsigned)-add) {
119 errno = ERANGE; /* Overflow */
120 return -1;
121 }
Lev Walkin8e8078a2004-09-26 13:10:40 +0000122 *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000123 return 0;
124 }
125
Lev Walkin29a044b2004-06-14 07:24:36 +0000126#ifndef WORDS_BIGENDIAN
127 if(*(unsigned char *)&LE) { /* Little endian (x86) */
128 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000129 rvbuf += rvsize / CHAR_BIT - 1;
130 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000131 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000132 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000133#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000134 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000135
Lev Walkinc4c61962004-06-14 08:17:27 +0000136 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000137 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000138
Lev Walkin29a044b2004-06-14 07:24:36 +0000139 /* Clear the high unused bits */
140 for(bits = rvsize - arclen;
141 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000142 rvbuf += inc, bits -= CHAR_BIT)
143 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000144
Lev Walkin29a044b2004-06-14 07:24:36 +0000145 /* Fill the body of a value */
146 for(; arcbuf < arcend; arcbuf++) {
147 cache = (cache << 7) | (*arcbuf & 0x7f);
148 bits += 7;
149 if(bits >= CHAR_BIT) {
150 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000151 *rvbuf = (cache >> bits);
152 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000153 }
154 }
155 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000156 *rvbuf = cache;
157 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000158 }
159 }
160
161 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000162 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
163 int v = add + *rvbuf;
Lev Walkin29a044b2004-06-14 07:24:36 +0000164 if(v & (-1 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000165 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000166 add = -1;
167 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000168 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000169 break;
170 }
171 }
172 if(rvbuf == rvstart) {
173 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 errno = ERANGE; /* Overflow */
175 return -1;
176 }
177 }
178
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 return 0;
180}
181
Lev Walkina9cc46e2004-09-22 16:06:28 +0000182ssize_t
183OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 asn_app_consume_bytes_f *cb, void *app_key) {
185 char scratch[64]; /* Conservative estimate */
186 unsigned long accum; /* Bits accumulator */
187 char *p; /* Position in the scratch buffer */
188
Lev Walkin29a044b2004-06-14 07:24:36 +0000189 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
190 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 return -1;
192
Lev Walkin3251b8e2004-08-23 09:23:02 +0000193 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000194 ssize_t len;
195
Lev Walkin3251b8e2004-08-23 09:23:02 +0000196 /* Fill the scratch buffer in reverse. */
197 p = scratch + sizeof(scratch);
198 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000199 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000200
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201 len = sizeof(scratch) - (p - scratch);
202 if(cb(p, len, app_key) < 0)
203 return -1;
204 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000205 } else {
206 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207 if(cb(scratch, 1, app_key) < 0)
208 return -1;
209 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000210 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000211}
212
213int
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
215 asn_app_consume_bytes_f *cb, void *app_key) {
216
217 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
218 return -1;
219
220 return 0;
221}
222
223static ssize_t
224OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
225 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 int startn;
227 int add = 0;
228 int i;
229
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 for(i = 0, startn = 0; i < st->size; i++) {
231 uint8_t b = st->buf[i];
232 if((b & 0x80)) /* Continuation expected */
233 continue;
234
235 if(startn == 0) {
236 /*
237 * First two arcs are encoded through the backdoor.
238 */
239 if(i) {
240 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000241 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 } else if(b <= 39) {
243 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000244 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 } else if(b < 79) {
246 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000247 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 } else {
249 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000252 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000253 }
254
Lev Walkina9cc46e2004-09-22 16:06:28 +0000255 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000256 return -1;
257
Lev Walkina9cc46e2004-09-22 16:06:28 +0000258 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
259 i - startn + 1, add, cb, app_key);
260 if(add < 0) return -1;
261 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 startn = i + 1;
263 add = 0;
264 }
265
Lev Walkina9cc46e2004-09-22 16:06:28 +0000266 return wrote_len;
267}
268
Lev Walkin0fab1a62005-03-09 22:19:25 +0000269static enum xer_pbd_rval
270OBJECT_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 +0000271 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000272 const char *chunk_end = (const char *)chunk_buf + chunk_size;
273 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000274 long s_arcs[10];
275 long *arcs = s_arcs;
276 int arcs_count;
277 int ret;
278
Lev Walkine0b56e02005-02-25 12:10:27 +0000279 (void)td;
280
Lev Walkin92302252004-10-23 10:16:51 +0000281 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000282 (const char *)chunk_buf, chunk_size, arcs,
283 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
284 if(arcs_count <= 0) {
285 /* Expecting more than zero arcs */
286 return XPBD_BROKEN_ENCODING;
287 }
Lev Walkin2c34aad2005-03-10 11:50:24 +0000288 if(endptr < chunk_end) {
289 /* We have a tail of unrecognized data. Check its safety. */
290 if(!xer_is_whitespace(endptr, chunk_end - endptr))
291 return XPBD_BROKEN_ENCODING;
292 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000293
294 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000295 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000296 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000297 ret = OBJECT_IDENTIFIER_parse_arcs(
298 (const char *)chunk_buf, chunk_size,
299 arcs, arcs_count, &endptr);
300 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000301 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000302 }
303
304 /*
305 * Convert arcs into BER representation.
306 */
307 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000308 if(arcs != s_arcs) FREEMEM(arcs);
309
Lev Walkin2c34aad2005-03-10 11:50:24 +0000310 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000311}
312
313asn_dec_rval_t
314OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
315 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000316 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000317
318 return xer_decode_primitive(opt_codec_ctx, td,
319 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
320 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
321}
322
Lev Walkina9cc46e2004-09-22 16:06:28 +0000323asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000324OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000325 int ilevel, enum xer_encoder_flags_e flags,
326 asn_app_consume_bytes_f *cb, void *app_key) {
327 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
328 asn_enc_rval_t er;
329
330 (void)ilevel;
331 (void)flags;
332
333 if(!st || !st->buf)
334 _ASN_ENCODE_FAILED;
335
336 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
337 if(er.encoded < 0) _ASN_ENCODE_FAILED;
338
Lev Walkin59b176e2005-11-26 11:25:14 +0000339 _ASN_ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000340}
341
342int
Lev Walkin5e033762004-09-29 13:26:15 +0000343OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000344 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
345 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
346
347 (void)td; /* Unused argument */
348 (void)ilevel; /* Unused argument */
349
350 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000351 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000352
353 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000354 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000355 return -1;
356
357 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
358 return -1;
359
Lev Walkin8e8078a2004-09-26 13:10:40 +0000360 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000361}
362
363int
Lev Walkin29a044b2004-06-14 07:24:36 +0000364OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
365 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000366 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000367 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 int startn = 0;
369 int add = 0;
370 int i;
371
Lev Walkin0787ff02004-06-17 23:43:39 +0000372 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 errno = EINVAL;
374 return -1;
375 }
376
377 for(i = 0; i < oid->size; i++) {
378 uint8_t b = oid->buf[i];
379 if((b & 0x80)) /* Continuation expected */
380 continue;
381
Lev Walkin29a044b2004-06-14 07:24:36 +0000382 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000383 /*
384 * First two arcs are encoded through the backdoor.
385 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000386 unsigned LE = 1; /* Little endian */
387 int first_arc;
388 num_arcs++;
389 if(!arc_slots) { num_arcs++; continue; }
390
391 if(i) first_arc = 2;
392 else if(b <= 39) first_arc = 0;
393 else if(b < 79) first_arc = 1;
394 else first_arc = 2;
395
396 add = -40 * first_arc;
397 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000398 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000399 + ((*(char *)&LE)?0:(arc_type_size - 1)))
400 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000401 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000402 }
403
Lev Walkin29a044b2004-06-14 07:24:36 +0000404 /* Decode, if has space */
405 if(arcs < arcs_end) {
406 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
407 i - startn + 1, add,
408 arcs, arc_type_size))
409 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000410 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000411 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000412 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000414 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 }
416
Lev Walkin29a044b2004-06-14 07:24:36 +0000417 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000418}
419
Lev Walkin0787ff02004-06-17 23:43:39 +0000420
421/*
422 * Save the single value as an object identifier arc.
423 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000424int
Lev Walkinb1a15552005-12-22 21:39:44 +0000425OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000426 /*
427 * The following conditions must hold:
428 * assert(arcval);
429 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000430 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000431 * assert(arcbuf);
432 */
433#ifdef WORDS_BIGENDIAN
434 const unsigned isLittleEndian = 0;
435#else
436 unsigned LE = 1;
437 unsigned isLittleEndian = *(char *)&LE;
438#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000439 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000440 unsigned int cache;
441 uint8_t *bp = arcbuf;
442 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000443 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000444
445 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000446 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
447 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000448 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000449 uint8_t *tb;
450 for(tb = buffer; a >= aend; tb++, a--)
451 if((*tb = *a) && (tb < msb))
452 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000453 tend = &buffer[arcval_size];
454 tp = msb; /* Most significant non-zero byte */
455 } else {
456 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000457 tend = (const unsigned char *)arcval + arcval_size;
458 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000459 if(*tp) break;
460 }
461
462 /*
463 * Split the value in 7-bits chunks.
464 */
465 bits = ((tend - tp) * CHAR_BIT) % 7;
466 if(bits) {
467 cache = *tp >> (CHAR_BIT - bits);
468 if(cache) {
469 *bp++ = cache | 0x80;
470 cache = *tp++;
471 bits = CHAR_BIT - bits;
472 } else {
473 bits = -bits;
474 }
475 } else {
476 cache = 0;
477 }
478 for(; tp < tend; tp++) {
479 cache = (cache << CHAR_BIT) + *tp;
480 bits += CHAR_BIT;
481 while(bits >= 7) {
482 bits -= 7;
483 *bp++ = 0x80 | (cache >> bits);
484 }
485 }
486 if(bits) *bp++ = cache;
487 bp[-1] &= 0x7f; /* Clear the last bit */
488
489 return bp - arcbuf;
490}
491
Lev Walkinf15320b2004-06-03 03:38:44 +0000492int
Lev Walkinb1a15552005-12-22 21:39:44 +0000493OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, const void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 uint8_t *buf;
495 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000496 unsigned LE = 1; /* Little endian (x86) */
497 unsigned isLittleEndian = *((char *)&LE);
498 unsigned int arc0;
499 unsigned int arc1;
500 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000501 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000502
Lev Walkin523de9e2006-08-18 01:34:18 +0000503 if(!oid || !arcs || arc_type_size < 1
504 || arc_type_size > 16
505 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 errno = EINVAL;
507 return -1;
508 }
509
Lev Walkin0787ff02004-06-17 23:43:39 +0000510 switch(arc_type_size) {
511 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000512 arc0 = ((const unsigned char *)arcs)[0];
513 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000514 break;
515 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000516 arc0 = ((const unsigned short *)arcs)[0];
517 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000518 break;
519 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000520 arc0 = ((const unsigned int *)arcs)[0];
521 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000522 break;
523 default:
524 arc1 = arc0 = 0;
525 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000526 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000527 /* If more significant bytes are present,
528 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000529 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000530 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000531 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
532 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000533 arc0 = *((const unsigned char *)arcs + 0);
534 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000535 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000536 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000537 /* If more significant bytes are present,
538 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000539 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000540 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000541 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
542 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000543 }
544 }
545
546 /*
547 * The previous chapter left us with the first and the second arcs.
548 * The values are not precise (that is, they are valid only if
549 * they're less than 255), but OK for the purposes of making
550 * the sanity test below.
551 */
552 if(arc0 <= 1) {
553 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000554 /* 8.19.4: At most 39 subsequent values (including 0) */
555 errno = ERANGE;
556 return -1;
557 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000558 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000559 /* 8.19.4: Only three values are allocated from the root node */
560 errno = ERANGE;
561 return -1;
562 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000563 /*
564 * After above tests it is known that the value of arc0 is completely
565 * trustworthy (0..2). However, the arc1's value is still meaningless.
566 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000567
568 /*
569 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000570 * This estimation implicitly takes in account the following facts,
571 * that cancel each other:
572 * * the first two arcs are encoded in a single value.
573 * * the first value may require more space (+1 byte)
574 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000575 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000576 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000577 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 if(!buf) {
579 /* ENOMEM */
580 return -1;
581 }
582
583 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000584 * Encode the first two arcs.
585 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000588 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000589 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000590 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000591
Lev Walkin0787ff02004-06-17 23:43:39 +0000592 /*
593 * Simulate first_value = arc0 * 40 + arc1;
594 */
595 /* Copy the second (1'st) arcs[1] into the first_value */
596 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000597 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000598 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000599 const uint8_t *aend = (const unsigned char *)arcs - 1;
600 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000601 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000602 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000603 const uint8_t *a1 = (const uint8_t *)arcs;
604 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000605 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000606 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000607 /* Increase the first_value by arc0 */
608 arc0 *= 40; /* (0..80) */
609 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
610 unsigned int v = *tp;
611 v += arc0;
612 *tp = v;
613 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
614 else break;
615 }
616
617 assert(tp >= first_value);
618
619 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
620 fv - first_value, 1);
621 }
622
623 /*
624 * Save the rest of arcs.
625 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000626 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000627 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000628 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000629 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
630 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000631 }
632
Lev Walkin34b2a932004-06-17 23:46:45 +0000633 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000634
635 /*
636 * Replace buffer.
637 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000638 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000639 bp = oid->buf;
640 oid->buf = buf;
641 if(bp) FREEMEM(bp);
642
643 return 0;
644}
Lev Walkinc4c61962004-06-14 08:17:27 +0000645
Lev Walkin92302252004-10-23 10:16:51 +0000646
647int
648OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000649 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000650 unsigned int arcs_count = 0;
651 const char *oid_end;
652 long value = 0;
653 enum {
654 ST_SKIPSPACE,
655 ST_WAITDIGITS, /* Next character is expected to be a digit */
Lev Walkinfdb25922005-07-21 09:32:49 +0000656 ST_DIGITS
Lev Walkin92302252004-10-23 10:16:51 +0000657 } state = ST_SKIPSPACE;
658
659 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000660 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000661 errno = EINVAL;
662 return -1;
663 }
664
665 if(oid_txt_length == -1)
666 oid_txt_length = strlen(oid_text);
667
668 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
669 switch(*oid_text) {
670 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
671 if(state == ST_SKIPSPACE) {
672 continue;
673 } else {
674 break; /* Finish */
675 }
676 case 0x2e: /* '.' */
677 if(state != ST_DIGITS
678 || (oid_text + 1) == oid_end) {
679 state = ST_WAITDIGITS;
680 break;
681 }
682 if(arcs_count < arcs_slots)
683 arcs[arcs_count] = value;
684 arcs_count++;
685 state = ST_WAITDIGITS;
686 continue;
687 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
688 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
689 if(state != ST_DIGITS) {
690 state = ST_DIGITS;
691 value = 0;
692 }
693 if(1) {
694 long new_value = value * 10;
695 if(new_value / 10 != value
696 || (value = new_value + (*oid_text - 0x30)) < 0) {
697 /* Overflow */
698 state = ST_WAITDIGITS;
699 break;
700 }
701 continue;
702 }
703 default:
704 /* Unexpected symbols */
705 state = ST_WAITDIGITS;
706 break;
707 } /* switch() */
708 break;
709 } /* for() */
710
711
Lev Walkin093ba2e2005-04-04 21:10:06 +0000712 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000713
714 /* Finalize last arc */
715 switch(state) {
716 case ST_WAITDIGITS:
717 errno = EINVAL;
718 return -1;
719 case ST_DIGITS:
720 if(arcs_count < arcs_slots)
721 arcs[arcs_count] = value;
722 arcs_count++;
723 /* Fall through */
724 default:
725 return arcs_count;
726 }
727}
728
729