blob: a7cd52d5b7553b260d4adb5c5476f71f69c4f9f1 [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 Walkin59b176e2005-11-26 11:25:14 +000026 0,
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 Walkin59b176e2005-11-26 11:25:14 +000034 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000035 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000036 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000037};
38
39
Lev Walkinf15320b2004-06-03 03:38:44 +000040int
Lev Walkin5e033762004-09-29 13:26:15 +000041OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000042 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000043 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000044
45 if(st && st->buf) {
46 if(st->size < 1) {
Lev Walkinba4e5182004-08-11 09:44:13 +000047 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000048 "%s: at least one numerical value "
49 "expected (%s:%d)",
50 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000051 return -1;
52 }
53 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000054 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000055 "%s: value not given (%s:%d)",
56 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000057 return -1;
58 }
59
60 return 0;
61}
62
Lev Walkinc4c61962004-06-14 08:17:27 +000063
Lev Walkinf15320b2004-06-03 03:38:44 +000064int
Lev Walkin8e8078a2004-09-26 13:10:40 +000065OBJECT_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 +000066 unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
Lev Walkin29a044b2004-06-14 07:24:36 +000067 uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000068 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000069 unsigned char *rvbuf = (unsigned char *)rvbufp;
70 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000071 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000072
Lev Walkin29a044b2004-06-14 07:24:36 +000073 rvsize *= CHAR_BIT; /* bytes to bits */
74 arclen *= 7; /* bytes to bits */
75
76 /*
77 * The arc has the number of bits
78 * cannot be represented using supplied return value type.
79 */
80 if(arclen > rvsize) {
81 if(arclen > (rvsize + CHAR_BIT)) {
82 errno = ERANGE; /* Overflow */
83 return -1;
84 } else {
85 /*
86 * Even if the number of bits in the arc representation
87 * is higher than the width of supplied * return value
88 * type, there is still possible to fit it when there
89 * are few unused high bits in the arc value
90 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000091 *
92 * Moreover, there is a possibility that the
93 * number could actually fit the arc space, given
94 * that add is negative, but we don't handle
95 * such "temporary lack of precision" situation here.
96 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +000097 */
98 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
99 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000100 errno = ERANGE; /* Overflow */
101 return -1;
102 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000103 /* Fool the routine computing unused bits */
104 arclen -= 7;
105 cache = *arcbuf & 0x7f;
106 arcbuf++;
107 }
108 }
109
Lev Walkinc4c61962004-06-14 08:17:27 +0000110 /* Faster path for common size */
111 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
112 unsigned long accum;
113 /* Gather all bits into the accumulator */
114 for(accum = cache; arcbuf < arcend; arcbuf++)
115 accum = (accum << 7) | (*arcbuf & ~0x80);
116 if(accum < (unsigned)-add) {
117 errno = ERANGE; /* Overflow */
118 return -1;
119 }
Lev Walkin8e8078a2004-09-26 13:10:40 +0000120 *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000121 return 0;
122 }
123
Lev Walkin29a044b2004-06-14 07:24:36 +0000124#ifndef WORDS_BIGENDIAN
125 if(*(unsigned char *)&LE) { /* Little endian (x86) */
126 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000127 rvbuf += rvsize / CHAR_BIT - 1;
128 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000129 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000130 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000131#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000132 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000133
Lev Walkinc4c61962004-06-14 08:17:27 +0000134 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000135 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000136
Lev Walkin29a044b2004-06-14 07:24:36 +0000137 /* Clear the high unused bits */
138 for(bits = rvsize - arclen;
139 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000140 rvbuf += inc, bits -= CHAR_BIT)
141 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000142
Lev Walkin29a044b2004-06-14 07:24:36 +0000143 /* Fill the body of a value */
144 for(; arcbuf < arcend; arcbuf++) {
145 cache = (cache << 7) | (*arcbuf & 0x7f);
146 bits += 7;
147 if(bits >= CHAR_BIT) {
148 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000149 *rvbuf = (cache >> bits);
150 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000151 }
152 }
153 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000154 *rvbuf = cache;
155 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000156 }
157 }
158
159 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000160 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
161 int v = add + *rvbuf;
Lev Walkin29a044b2004-06-14 07:24:36 +0000162 if(v & (-1 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000163 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000164 add = -1;
165 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000166 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 break;
168 }
169 }
170 if(rvbuf == rvstart) {
171 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 errno = ERANGE; /* Overflow */
173 return -1;
174 }
175 }
176
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 return 0;
178}
179
Lev Walkina9cc46e2004-09-22 16:06:28 +0000180ssize_t
181OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 asn_app_consume_bytes_f *cb, void *app_key) {
183 char scratch[64]; /* Conservative estimate */
184 unsigned long accum; /* Bits accumulator */
185 char *p; /* Position in the scratch buffer */
186
Lev Walkin29a044b2004-06-14 07:24:36 +0000187 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
188 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 return -1;
190
Lev Walkin3251b8e2004-08-23 09:23:02 +0000191 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000192 ssize_t len;
193
Lev Walkin3251b8e2004-08-23 09:23:02 +0000194 /* Fill the scratch buffer in reverse. */
195 p = scratch + sizeof(scratch);
196 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000198
Lev Walkina9cc46e2004-09-22 16:06:28 +0000199 len = sizeof(scratch) - (p - scratch);
200 if(cb(p, len, app_key) < 0)
201 return -1;
202 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000203 } else {
204 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205 if(cb(scratch, 1, app_key) < 0)
206 return -1;
207 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000208 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000209}
210
211int
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
213 asn_app_consume_bytes_f *cb, void *app_key) {
214
215 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
216 return -1;
217
218 return 0;
219}
220
221static ssize_t
222OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
223 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 int startn;
225 int add = 0;
226 int i;
227
Lev Walkinf15320b2004-06-03 03:38:44 +0000228 for(i = 0, startn = 0; i < st->size; i++) {
229 uint8_t b = st->buf[i];
230 if((b & 0x80)) /* Continuation expected */
231 continue;
232
233 if(startn == 0) {
234 /*
235 * First two arcs are encoded through the backdoor.
236 */
237 if(i) {
238 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000239 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000240 } else if(b <= 39) {
241 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000242 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 } else if(b < 79) {
244 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000245 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 } else {
247 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000248 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000250 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 }
252
Lev Walkina9cc46e2004-09-22 16:06:28 +0000253 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 return -1;
255
Lev Walkina9cc46e2004-09-22 16:06:28 +0000256 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
257 i - startn + 1, add, cb, app_key);
258 if(add < 0) return -1;
259 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 startn = i + 1;
261 add = 0;
262 }
263
Lev Walkina9cc46e2004-09-22 16:06:28 +0000264 return wrote_len;
265}
266
Lev Walkin0fab1a62005-03-09 22:19:25 +0000267static enum xer_pbd_rval
268OBJECT_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 +0000269 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000270 const char *chunk_end = (const char *)chunk_buf + chunk_size;
271 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000272 long s_arcs[10];
273 long *arcs = s_arcs;
274 int arcs_count;
275 int ret;
276
Lev Walkine0b56e02005-02-25 12:10:27 +0000277 (void)td;
278
Lev Walkin92302252004-10-23 10:16:51 +0000279 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000280 (const char *)chunk_buf, chunk_size, arcs,
281 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
282 if(arcs_count <= 0) {
283 /* Expecting more than zero arcs */
284 return XPBD_BROKEN_ENCODING;
285 }
Lev Walkin2c34aad2005-03-10 11:50:24 +0000286 if(endptr < chunk_end) {
287 /* We have a tail of unrecognized data. Check its safety. */
288 if(!xer_is_whitespace(endptr, chunk_end - endptr))
289 return XPBD_BROKEN_ENCODING;
290 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000291
292 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000293 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000294 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000295 ret = OBJECT_IDENTIFIER_parse_arcs(
296 (const char *)chunk_buf, chunk_size,
297 arcs, arcs_count, &endptr);
298 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000299 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000300 }
301
302 /*
303 * Convert arcs into BER representation.
304 */
305 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000306 if(arcs != s_arcs) FREEMEM(arcs);
307
Lev Walkin2c34aad2005-03-10 11:50:24 +0000308 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000309}
310
311asn_dec_rval_t
312OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
313 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000314 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000315
316 return xer_decode_primitive(opt_codec_ctx, td,
317 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
318 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
319}
320
Lev Walkina9cc46e2004-09-22 16:06:28 +0000321asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000322OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000323 int ilevel, enum xer_encoder_flags_e flags,
324 asn_app_consume_bytes_f *cb, void *app_key) {
325 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
326 asn_enc_rval_t er;
327
328 (void)ilevel;
329 (void)flags;
330
331 if(!st || !st->buf)
332 _ASN_ENCODE_FAILED;
333
334 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
335 if(er.encoded < 0) _ASN_ENCODE_FAILED;
336
Lev Walkin59b176e2005-11-26 11:25:14 +0000337 _ASN_ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000338}
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 Walkinb1a15552005-12-22 21:39:44 +0000423OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000424 /*
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 Walkinb1a15552005-12-22 21:39:44 +0000436 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000437 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 Walkinb1a15552005-12-22 21:39:44 +0000448 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
449 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000450 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000451 uint8_t *tb;
452 for(tb = buffer; a >= aend; tb++, a--)
453 if((*tb = *a) && (tb < msb))
454 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000455 tend = &buffer[arcval_size];
456 tp = msb; /* Most significant non-zero byte */
457 } else {
458 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000459 tend = (const unsigned char *)arcval + arcval_size;
460 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000461 if(*tp) break;
462 }
463
464 /*
465 * Split the value in 7-bits chunks.
466 */
467 bits = ((tend - tp) * CHAR_BIT) % 7;
468 if(bits) {
469 cache = *tp >> (CHAR_BIT - bits);
470 if(cache) {
471 *bp++ = cache | 0x80;
472 cache = *tp++;
473 bits = CHAR_BIT - bits;
474 } else {
475 bits = -bits;
476 }
477 } else {
478 cache = 0;
479 }
480 for(; tp < tend; tp++) {
481 cache = (cache << CHAR_BIT) + *tp;
482 bits += CHAR_BIT;
483 while(bits >= 7) {
484 bits -= 7;
485 *bp++ = 0x80 | (cache >> bits);
486 }
487 }
488 if(bits) *bp++ = cache;
489 bp[-1] &= 0x7f; /* Clear the last bit */
490
491 return bp - arcbuf;
492}
493
Lev Walkinf15320b2004-06-03 03:38:44 +0000494int
Lev Walkinb1a15552005-12-22 21:39:44 +0000495OBJECT_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 +0000496 uint8_t *buf;
497 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000498 unsigned LE = 1; /* Little endian (x86) */
499 unsigned isLittleEndian = *((char *)&LE);
500 unsigned int arc0;
501 unsigned int arc1;
502 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000503 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000504
Lev Walkin0787ff02004-06-17 23:43:39 +0000505 if(!oid || !arcs || arc_type_size < 1 || 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 Walkin90fcd442004-08-11 07:48:19 +0000589#ifdef __GNUC__
590 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000591 uint8_t *fv = first_value;
Lev Walkin90fcd442004-08-11 07:48:19 +0000592#else
593 uint8_t *first_value = alloca(1 + arc_type_size);
Lev Walkin90fcd442004-08-11 07:48:19 +0000594 uint8_t *fv = first_value;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000595 if(!first_value) {
596 errno = ENOMEM;
597 return -1;
598 }
599#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000600
Lev Walkin0787ff02004-06-17 23:43:39 +0000601 /*
602 * Simulate first_value = arc0 * 40 + arc1;
603 */
604 /* Copy the second (1'st) arcs[1] into the first_value */
605 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000606 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000607 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000608 const uint8_t *aend = (const unsigned char *)arcs - 1;
609 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000610 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000611 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000612 const uint8_t *a1 = (const uint8_t *)arcs;
613 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000614 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000615 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000616 /* Increase the first_value by arc0 */
617 arc0 *= 40; /* (0..80) */
618 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
619 unsigned int v = *tp;
620 v += arc0;
621 *tp = v;
622 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
623 else break;
624 }
625
626 assert(tp >= first_value);
627
628 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
629 fv - first_value, 1);
630 }
631
632 /*
633 * Save the rest of arcs.
634 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000635 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000636 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000637 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000638 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
639 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000640 }
641
Lev Walkin34b2a932004-06-17 23:46:45 +0000642 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000643
644 /*
645 * Replace buffer.
646 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000647 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000648 bp = oid->buf;
649 oid->buf = buf;
650 if(bp) FREEMEM(bp);
651
652 return 0;
653}
Lev Walkinc4c61962004-06-14 08:17:27 +0000654
Lev Walkin92302252004-10-23 10:16:51 +0000655
656int
657OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000658 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000659 unsigned int arcs_count = 0;
660 const char *oid_end;
661 long value = 0;
662 enum {
663 ST_SKIPSPACE,
664 ST_WAITDIGITS, /* Next character is expected to be a digit */
Lev Walkinfdb25922005-07-21 09:32:49 +0000665 ST_DIGITS
Lev Walkin92302252004-10-23 10:16:51 +0000666 } state = ST_SKIPSPACE;
667
668 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000669 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000670 errno = EINVAL;
671 return -1;
672 }
673
674 if(oid_txt_length == -1)
675 oid_txt_length = strlen(oid_text);
676
677 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
678 switch(*oid_text) {
679 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
680 if(state == ST_SKIPSPACE) {
681 continue;
682 } else {
683 break; /* Finish */
684 }
685 case 0x2e: /* '.' */
686 if(state != ST_DIGITS
687 || (oid_text + 1) == oid_end) {
688 state = ST_WAITDIGITS;
689 break;
690 }
691 if(arcs_count < arcs_slots)
692 arcs[arcs_count] = value;
693 arcs_count++;
694 state = ST_WAITDIGITS;
695 continue;
696 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
697 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
698 if(state != ST_DIGITS) {
699 state = ST_DIGITS;
700 value = 0;
701 }
702 if(1) {
703 long new_value = value * 10;
704 if(new_value / 10 != value
705 || (value = new_value + (*oid_text - 0x30)) < 0) {
706 /* Overflow */
707 state = ST_WAITDIGITS;
708 break;
709 }
710 continue;
711 }
712 default:
713 /* Unexpected symbols */
714 state = ST_WAITDIGITS;
715 break;
716 } /* switch() */
717 break;
718 } /* for() */
719
720
Lev Walkin093ba2e2005-04-04 21:10:06 +0000721 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000722
723 /* Finalize last arc */
724 switch(state) {
725 case ST_WAITDIGITS:
726 errno = EINVAL;
727 return -1;
728 case ST_DIGITS:
729 if(arcs_count < arcs_slots)
730 arcs[arcs_count] = value;
731 arcs_count++;
732 /* Fall through */
733 default:
734 return arcs_count;
735 }
736}
737
738