blob: e815d480b31d023db1c97d00c898ff2af78d9f4b [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 <errno.h>
9
10/*
11 * OBJECT IDENTIFIER basic type description.
12 */
Lev Walkin5e033762004-09-29 13:26:15 +000013static ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000014 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
15};
Lev Walkin5e033762004-09-29 13:26:15 +000016asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000017 "OBJECT IDENTIFIER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000018 "OBJECT_IDENTIFIER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000019 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000020 OBJECT_IDENTIFIER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000021 OBJECT_IDENTIFIER_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000022 ber_decode_primitive,
23 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000024 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 OBJECT_IDENTIFIER_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_OBJECT_IDENTIFIER_tags,
28 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
29 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
30 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
31 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
32 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000033 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000034 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000035};
36
37
Lev Walkinf15320b2004-06-03 03:38:44 +000038int
Lev Walkin5e033762004-09-29 13:26:15 +000039OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000040 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000041 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000042
43 if(st && st->buf) {
44 if(st->size < 1) {
Lev Walkinba4e5182004-08-11 09:44:13 +000045 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000046 "%s: at least one numerical value "
47 "expected (%s:%d)",
48 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000049 return -1;
50 }
51 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000052 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000053 "%s: value not given (%s:%d)",
54 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000055 return -1;
56 }
57
58 return 0;
59}
60
Lev Walkinc4c61962004-06-14 08:17:27 +000061
Lev Walkinf15320b2004-06-03 03:38:44 +000062int
Lev Walkin8e8078a2004-09-26 13:10:40 +000063OBJECT_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 +000064 unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
Lev Walkin29a044b2004-06-14 07:24:36 +000065 uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000066 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000067 unsigned char *rvbuf = (unsigned char *)rvbufp;
68 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000069 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000070
Lev Walkin29a044b2004-06-14 07:24:36 +000071 rvsize *= CHAR_BIT; /* bytes to bits */
72 arclen *= 7; /* bytes to bits */
73
74 /*
75 * The arc has the number of bits
76 * cannot be represented using supplied return value type.
77 */
78 if(arclen > rvsize) {
79 if(arclen > (rvsize + CHAR_BIT)) {
80 errno = ERANGE; /* Overflow */
81 return -1;
82 } else {
83 /*
84 * Even if the number of bits in the arc representation
85 * is higher than the width of supplied * return value
86 * type, there is still possible to fit it when there
87 * are few unused high bits in the arc value
88 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000089 *
90 * Moreover, there is a possibility that the
91 * number could actually fit the arc space, given
92 * that add is negative, but we don't handle
93 * such "temporary lack of precision" situation here.
94 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +000095 */
96 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
97 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +000098 errno = ERANGE; /* Overflow */
99 return -1;
100 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000101 /* Fool the routine computing unused bits */
102 arclen -= 7;
103 cache = *arcbuf & 0x7f;
104 arcbuf++;
105 }
106 }
107
Lev Walkinc4c61962004-06-14 08:17:27 +0000108 /* Faster path for common size */
109 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
110 unsigned long accum;
111 /* Gather all bits into the accumulator */
112 for(accum = cache; arcbuf < arcend; arcbuf++)
113 accum = (accum << 7) | (*arcbuf & ~0x80);
114 if(accum < (unsigned)-add) {
115 errno = ERANGE; /* Overflow */
116 return -1;
117 }
Lev Walkin8e8078a2004-09-26 13:10:40 +0000118 *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000119 return 0;
120 }
121
Lev Walkin29a044b2004-06-14 07:24:36 +0000122#ifndef WORDS_BIGENDIAN
123 if(*(unsigned char *)&LE) { /* Little endian (x86) */
124 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000125 rvbuf += rvsize / CHAR_BIT - 1;
126 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000127 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000128 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000129#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000130 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000131
Lev Walkinc4c61962004-06-14 08:17:27 +0000132 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000133 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000134
Lev Walkin29a044b2004-06-14 07:24:36 +0000135 /* Clear the high unused bits */
136 for(bits = rvsize - arclen;
137 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000138 rvbuf += inc, bits -= CHAR_BIT)
139 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000140
Lev Walkin29a044b2004-06-14 07:24:36 +0000141 /* Fill the body of a value */
142 for(; arcbuf < arcend; arcbuf++) {
143 cache = (cache << 7) | (*arcbuf & 0x7f);
144 bits += 7;
145 if(bits >= CHAR_BIT) {
146 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000147 *rvbuf = (cache >> bits);
148 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000149 }
150 }
151 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000152 *rvbuf = cache;
153 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000154 }
155 }
156
157 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000158 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
159 int v = add + *rvbuf;
Lev Walkin29a044b2004-06-14 07:24:36 +0000160 if(v & (-1 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000161 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000162 add = -1;
163 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000164 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000165 break;
166 }
167 }
168 if(rvbuf == rvstart) {
169 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 errno = ERANGE; /* Overflow */
171 return -1;
172 }
173 }
174
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 return 0;
176}
177
Lev Walkina9cc46e2004-09-22 16:06:28 +0000178ssize_t
179OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 asn_app_consume_bytes_f *cb, void *app_key) {
181 char scratch[64]; /* Conservative estimate */
182 unsigned long accum; /* Bits accumulator */
183 char *p; /* Position in the scratch buffer */
184
Lev Walkin29a044b2004-06-14 07:24:36 +0000185 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
186 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 return -1;
188
Lev Walkin3251b8e2004-08-23 09:23:02 +0000189 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000190 ssize_t len;
191
Lev Walkin3251b8e2004-08-23 09:23:02 +0000192 /* Fill the scratch buffer in reverse. */
193 p = scratch + sizeof(scratch);
194 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000196
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 len = sizeof(scratch) - (p - scratch);
198 if(cb(p, len, app_key) < 0)
199 return -1;
200 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000201 } else {
202 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000203 if(cb(scratch, 1, app_key) < 0)
204 return -1;
205 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000206 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000207}
208
209int
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
211 asn_app_consume_bytes_f *cb, void *app_key) {
212
213 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
214 return -1;
215
216 return 0;
217}
218
219static ssize_t
220OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
221 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000222 int startn;
223 int add = 0;
224 int i;
225
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 for(i = 0, startn = 0; i < st->size; i++) {
227 uint8_t b = st->buf[i];
228 if((b & 0x80)) /* Continuation expected */
229 continue;
230
231 if(startn == 0) {
232 /*
233 * First two arcs are encoded through the backdoor.
234 */
235 if(i) {
236 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000237 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 } else if(b <= 39) {
239 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000240 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000241 } else if(b < 79) {
242 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000243 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 } else {
245 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000246 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000248 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 }
250
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 return -1;
253
Lev Walkina9cc46e2004-09-22 16:06:28 +0000254 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
255 i - startn + 1, add, cb, app_key);
256 if(add < 0) return -1;
257 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000258 startn = i + 1;
259 add = 0;
260 }
261
Lev Walkina9cc46e2004-09-22 16:06:28 +0000262 return wrote_len;
263}
264
Lev Walkin0fab1a62005-03-09 22:19:25 +0000265static enum xer_pbd_rval
266OBJECT_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 +0000267 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000268 const char *chunk_end = (const char *)chunk_buf + chunk_size;
269 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000270 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 }
Lev Walkin2c34aad2005-03-10 11:50:24 +0000284 if(endptr < chunk_end) {
285 /* We have a tail of unrecognized data. Check its safety. */
286 if(!xer_is_whitespace(endptr, chunk_end - endptr))
287 return XPBD_BROKEN_ENCODING;
288 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000289
290 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000291 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000292 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000293 ret = OBJECT_IDENTIFIER_parse_arcs(
294 (const char *)chunk_buf, chunk_size,
295 arcs, arcs_count, &endptr);
296 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000297 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000298 }
299
300 /*
301 * Convert arcs into BER representation.
302 */
303 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000304 if(arcs != s_arcs) FREEMEM(arcs);
305
Lev Walkin2c34aad2005-03-10 11:50:24 +0000306 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000307}
308
309asn_dec_rval_t
310OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
311 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000312 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000313
314 return xer_decode_primitive(opt_codec_ctx, td,
315 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
316 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
317}
318
Lev Walkina9cc46e2004-09-22 16:06:28 +0000319asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000320OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000321 int ilevel, enum xer_encoder_flags_e flags,
322 asn_app_consume_bytes_f *cb, void *app_key) {
323 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
324 asn_enc_rval_t er;
325
326 (void)ilevel;
327 (void)flags;
328
329 if(!st || !st->buf)
330 _ASN_ENCODE_FAILED;
331
332 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
333 if(er.encoded < 0) _ASN_ENCODE_FAILED;
334
Lev Walkin4b95ab92005-11-13 09:19:11 +0000335 er.structure_ptr = 0;
336 er.failed_type = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000337 return er;
338}
339
340int
Lev Walkin5e033762004-09-29 13:26:15 +0000341OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000342 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
343 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
344
345 (void)td; /* Unused argument */
346 (void)ilevel; /* Unused argument */
347
348 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000349 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000350
351 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000352 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000353 return -1;
354
355 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
356 return -1;
357
Lev Walkin8e8078a2004-09-26 13:10:40 +0000358 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000359}
360
361int
Lev Walkin29a044b2004-06-14 07:24:36 +0000362OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
363 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000364 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000365 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 int startn = 0;
367 int add = 0;
368 int i;
369
Lev Walkin0787ff02004-06-17 23:43:39 +0000370 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 errno = EINVAL;
372 return -1;
373 }
374
375 for(i = 0; i < oid->size; i++) {
376 uint8_t b = oid->buf[i];
377 if((b & 0x80)) /* Continuation expected */
378 continue;
379
Lev Walkin29a044b2004-06-14 07:24:36 +0000380 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 /*
382 * First two arcs are encoded through the backdoor.
383 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000384 unsigned LE = 1; /* Little endian */
385 int first_arc;
386 num_arcs++;
387 if(!arc_slots) { num_arcs++; continue; }
388
389 if(i) first_arc = 2;
390 else if(b <= 39) first_arc = 0;
391 else if(b < 79) first_arc = 1;
392 else first_arc = 2;
393
394 add = -40 * first_arc;
395 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000396 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000397 + ((*(char *)&LE)?0:(arc_type_size - 1)))
398 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000399 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 }
401
Lev Walkin29a044b2004-06-14 07:24:36 +0000402 /* Decode, if has space */
403 if(arcs < arcs_end) {
404 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
405 i - startn + 1, add,
406 arcs, arc_type_size))
407 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000409 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000410 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000411 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000412 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 }
414
Lev Walkin29a044b2004-06-14 07:24:36 +0000415 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000416}
417
Lev Walkin0787ff02004-06-17 23:43:39 +0000418
419/*
420 * Save the single value as an object identifier arc.
421 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000422int
Lev Walkin0787ff02004-06-17 23:43:39 +0000423OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
424 /*
425 * The following conditions must hold:
426 * assert(arcval);
427 * assert(arcval_size > 0);
428 * assert(arcbuf);
429 */
430#ifdef WORDS_BIGENDIAN
431 const unsigned isLittleEndian = 0;
432#else
433 unsigned LE = 1;
434 unsigned isLittleEndian = *(char *)&LE;
435#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000436 uint8_t *tp, *tend;
437 unsigned int cache;
438 uint8_t *bp = arcbuf;
439 int bits;
Lev Walkin64399722004-08-11 07:17:22 +0000440#ifdef __GNUC__
441 uint8_t buffer[arcval_size];
442#else
443 uint8_t *buffer = alloca(arcval_size);
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000444 if(!buffer) { errno = ENOMEM; return -1; }
Lev Walkin64399722004-08-11 07:17:22 +0000445#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000446
447 if(isLittleEndian && !prepared_order) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000448 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
Lev Walkinc2346572004-08-11 09:07:36 +0000449 uint8_t *aend = (uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000450 uint8_t *msb = buffer + arcval_size - 1;
451 for(tp = buffer; a >= aend; tp++, a--)
452 if((*tp = *a) && (tp < msb))
453 msb = tp;
454 tend = &buffer[arcval_size];
455 tp = msb; /* Most significant non-zero byte */
456 } else {
457 /* Look for most significant non-zero byte */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000458 tend = (unsigned char *)arcval + arcval_size;
Lev Walkinc2346572004-08-11 09:07:36 +0000459 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000460 if(*tp) break;
461 }
462
463 /*
464 * Split the value in 7-bits chunks.
465 */
466 bits = ((tend - tp) * CHAR_BIT) % 7;
467 if(bits) {
468 cache = *tp >> (CHAR_BIT - bits);
469 if(cache) {
470 *bp++ = cache | 0x80;
471 cache = *tp++;
472 bits = CHAR_BIT - bits;
473 } else {
474 bits = -bits;
475 }
476 } else {
477 cache = 0;
478 }
479 for(; tp < tend; tp++) {
480 cache = (cache << CHAR_BIT) + *tp;
481 bits += CHAR_BIT;
482 while(bits >= 7) {
483 bits -= 7;
484 *bp++ = 0x80 | (cache >> bits);
485 }
486 }
487 if(bits) *bp++ = cache;
488 bp[-1] &= 0x7f; /* Clear the last bit */
489
490 return bp - arcbuf;
491}
492
Lev Walkinf15320b2004-06-03 03:38:44 +0000493int
Lev Walkin0787ff02004-06-17 23:43:39 +0000494OBJECT_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 +0000495 uint8_t *buf;
496 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000497 unsigned LE = 1; /* Little endian (x86) */
498 unsigned isLittleEndian = *((char *)&LE);
499 unsigned int arc0;
500 unsigned int arc1;
501 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000502 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000503
Lev Walkin0787ff02004-06-17 23:43:39 +0000504 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 errno = EINVAL;
506 return -1;
507 }
508
Lev Walkin0787ff02004-06-17 23:43:39 +0000509 switch(arc_type_size) {
510 case sizeof(char):
511 arc0 = ((unsigned char *)arcs)[0];
512 arc1 = ((unsigned char *)arcs)[1];
513 break;
514 case sizeof(short):
515 arc0 = ((unsigned short *)arcs)[0];
516 arc1 = ((unsigned short *)arcs)[1];
517 break;
518 case sizeof(int):
519 arc0 = ((unsigned int *)arcs)[0];
520 arc1 = ((unsigned int *)arcs)[1];
521 break;
522 default:
523 arc1 = arc0 = 0;
524 if(isLittleEndian) { /* Little endian (x86) */
525 unsigned char *ps, *pe;
526 /* If more significant bytes are present,
527 * make them > 255 quick */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000528 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
529 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000530 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
531 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
532 arc0 = *((unsigned char *)arcs + 0);
533 arc1 = *((unsigned char *)arcs + arc_type_size);
534 } else {
535 unsigned char *ps, *pe;
536 /* If more significant bytes are present,
537 * make them > 255 quick */
Lev Walkinc2346572004-08-11 09:07:36 +0000538 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000539 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
540 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
541 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
542 }
543 }
544
545 /*
546 * The previous chapter left us with the first and the second arcs.
547 * The values are not precise (that is, they are valid only if
548 * they're less than 255), but OK for the purposes of making
549 * the sanity test below.
550 */
551 if(arc0 <= 1) {
552 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 /* 8.19.4: At most 39 subsequent values (including 0) */
554 errno = ERANGE;
555 return -1;
556 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000557 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 /* 8.19.4: Only three values are allocated from the root node */
559 errno = ERANGE;
560 return -1;
561 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000562 /*
563 * After above tests it is known that the value of arc0 is completely
564 * trustworthy (0..2). However, the arc1's value is still meaningless.
565 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000566
567 /*
568 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000569 * This estimation implicitly takes in account the following facts,
570 * that cancel each other:
571 * * the first two arcs are encoded in a single value.
572 * * the first value may require more space (+1 byte)
573 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000575 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000576 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 if(!buf) {
578 /* ENOMEM */
579 return -1;
580 }
581
582 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000583 * Encode the first two arcs.
584 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000585 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000587 uint8_t *tp;
Lev Walkin90fcd442004-08-11 07:48:19 +0000588#ifdef __GNUC__
589 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000590 uint8_t *fv = first_value;
Lev Walkin90fcd442004-08-11 07:48:19 +0000591#else
592 uint8_t *first_value = alloca(1 + arc_type_size);
Lev Walkin90fcd442004-08-11 07:48:19 +0000593 uint8_t *fv = first_value;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000594 if(!first_value) {
595 errno = ENOMEM;
596 return -1;
597 }
598#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000599
Lev Walkin0787ff02004-06-17 23:43:39 +0000600 /*
601 * Simulate first_value = arc0 * 40 + arc1;
602 */
603 /* Copy the second (1'st) arcs[1] into the first_value */
604 *fv++ = 0;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000605 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000606 if(isLittleEndian) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000607 uint8_t *aend = (unsigned char *)arcs - 1;
608 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000609 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000610 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000611 uint8_t *a1 = (uint8_t *)arcs;
Lev Walkin0787ff02004-06-17 23:43:39 +0000612 uint8_t *aend = a1 + arc_type_size;
613 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000614 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000615 /* Increase the first_value by arc0 */
616 arc0 *= 40; /* (0..80) */
617 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
618 unsigned int v = *tp;
619 v += arc0;
620 *tp = v;
621 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
622 else break;
623 }
624
625 assert(tp >= first_value);
626
627 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
628 fv - first_value, 1);
629 }
630
631 /*
632 * Save the rest of arcs.
633 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000634 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
635 i < arc_slots;
636 i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000637 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
638 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000639 }
640
Lev Walkin34b2a932004-06-17 23:46:45 +0000641 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000642
643 /*
644 * Replace buffer.
645 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000646 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000647 bp = oid->buf;
648 oid->buf = buf;
649 if(bp) FREEMEM(bp);
650
651 return 0;
652}
Lev Walkinc4c61962004-06-14 08:17:27 +0000653
Lev Walkin92302252004-10-23 10:16:51 +0000654
655int
656OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000657 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000658 unsigned int arcs_count = 0;
659 const char *oid_end;
660 long value = 0;
661 enum {
662 ST_SKIPSPACE,
663 ST_WAITDIGITS, /* Next character is expected to be a digit */
Lev Walkinfdb25922005-07-21 09:32:49 +0000664 ST_DIGITS
Lev Walkin92302252004-10-23 10:16:51 +0000665 } state = ST_SKIPSPACE;
666
667 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000668 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000669 errno = EINVAL;
670 return -1;
671 }
672
673 if(oid_txt_length == -1)
674 oid_txt_length = strlen(oid_text);
675
676 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
677 switch(*oid_text) {
678 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
679 if(state == ST_SKIPSPACE) {
680 continue;
681 } else {
682 break; /* Finish */
683 }
684 case 0x2e: /* '.' */
685 if(state != ST_DIGITS
686 || (oid_text + 1) == oid_end) {
687 state = ST_WAITDIGITS;
688 break;
689 }
690 if(arcs_count < arcs_slots)
691 arcs[arcs_count] = value;
692 arcs_count++;
693 state = ST_WAITDIGITS;
694 continue;
695 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
696 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
697 if(state != ST_DIGITS) {
698 state = ST_DIGITS;
699 value = 0;
700 }
701 if(1) {
702 long new_value = value * 10;
703 if(new_value / 10 != value
704 || (value = new_value + (*oid_text - 0x30)) < 0) {
705 /* Overflow */
706 state = ST_WAITDIGITS;
707 break;
708 }
709 continue;
710 }
711 default:
712 /* Unexpected symbols */
713 state = ST_WAITDIGITS;
714 break;
715 } /* switch() */
716 break;
717 } /* for() */
718
719
Lev Walkin093ba2e2005-04-04 21:10:06 +0000720 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000721
722 /* Finalize last arc */
723 switch(state) {
724 case ST_WAITDIGITS:
725 errno = EINVAL;
726 return -1;
727 case ST_DIGITS:
728 if(arcs_count < arcs_slots)
729 arcs[arcs_count] = value;
730 arcs_count++;
731 /* Fall through */
732 default:
733 return arcs_count;
734 }
735}
736
737