blob: c1f58843b56311e440e93a322729d9560b28cbf3 [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 Walkinf5927112012-09-03 00:48:45 -07006#include <INTEGER.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <OBJECT_IDENTIFIER.h>
Lev Walkin725883b2006-10-09 12:07:58 +00008#include <OCTET_STRING.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00009#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +000010#include <errno.h>
11
12/*
13 * OBJECT IDENTIFIER basic type description.
14 */
Lev Walkin5e033762004-09-29 13:26:15 +000015static ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
17};
Lev Walkin5e033762004-09-29 13:26:15 +000018asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000019 "OBJECT IDENTIFIER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000020 "OBJECT_IDENTIFIER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000021 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000022 OBJECT_IDENTIFIER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 OBJECT_IDENTIFIER_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000024 ber_decode_primitive,
25 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000026 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 OBJECT_IDENTIFIER_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +000028 OCTET_STRING_decode_uper,
29 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +000030 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000031 asn_DEF_OBJECT_IDENTIFIER_tags,
32 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
33 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
34 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
35 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
36 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000037 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000038 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000039 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000040};
41
42
Lev Walkinf15320b2004-06-03 03:38:44 +000043int
Lev Walkin5e033762004-09-29 13:26:15 +000044OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000045 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000046 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000047
48 if(st && st->buf) {
49 if(st->size < 1) {
Lev Walkined9019a2006-10-16 12:18:41 +000050 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000051 "%s: at least one numerical value "
52 "expected (%s:%d)",
53 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000054 return -1;
55 }
56 } else {
Lev Walkined9019a2006-10-16 12:18:41 +000057 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000058 "%s: value not given (%s:%d)",
59 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000060 return -1;
61 }
62
63 return 0;
64}
65
Lev Walkinc4c61962004-06-14 08:17:27 +000066
Lev Walkinf15320b2004-06-03 03:38:44 +000067int
Lev Walkin8e8078a2004-09-26 13:10:40 +000068OBJECT_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 +000069 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Lev Walkin29a044b2004-06-14 07:24:36 +000070 uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000071 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000072 unsigned char *rvbuf = (unsigned char *)rvbufp;
73 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000074 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000075
Lev Walkin29a044b2004-06-14 07:24:36 +000076 rvsize *= CHAR_BIT; /* bytes to bits */
77 arclen *= 7; /* bytes to bits */
78
79 /*
80 * The arc has the number of bits
81 * cannot be represented using supplied return value type.
82 */
83 if(arclen > rvsize) {
84 if(arclen > (rvsize + CHAR_BIT)) {
85 errno = ERANGE; /* Overflow */
86 return -1;
87 } else {
88 /*
89 * Even if the number of bits in the arc representation
90 * is higher than the width of supplied * return value
91 * type, there is still possible to fit it when there
92 * are few unused high bits in the arc value
93 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000094 *
95 * Moreover, there is a possibility that the
96 * number could actually fit the arc space, given
97 * that add is negative, but we don't handle
98 * such "temporary lack of precision" situation here.
99 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000100 */
101 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
102 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 errno = ERANGE; /* Overflow */
104 return -1;
105 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000106 /* Fool the routine computing unused bits */
107 arclen -= 7;
108 cache = *arcbuf & 0x7f;
109 arcbuf++;
110 }
111 }
112
Lev Walkinc4c61962004-06-14 08:17:27 +0000113 /* Faster path for common size */
114 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
115 unsigned long accum;
116 /* Gather all bits into the accumulator */
117 for(accum = cache; arcbuf < arcend; arcbuf++)
118 accum = (accum << 7) | (*arcbuf & ~0x80);
119 if(accum < (unsigned)-add) {
120 errno = ERANGE; /* Overflow */
121 return -1;
122 }
Lev Walkin8e8078a2004-09-26 13:10:40 +0000123 *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000124 return 0;
125 }
126
Lev Walkin29a044b2004-06-14 07:24:36 +0000127#ifndef WORDS_BIGENDIAN
128 if(*(unsigned char *)&LE) { /* Little endian (x86) */
129 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000130 rvbuf += rvsize / CHAR_BIT - 1;
131 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000132 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000133 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000134#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000135 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000136
Lev Walkinc4c61962004-06-14 08:17:27 +0000137 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000138 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000139
Lev Walkin29a044b2004-06-14 07:24:36 +0000140 /* Clear the high unused bits */
141 for(bits = rvsize - arclen;
142 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000143 rvbuf += inc, bits -= CHAR_BIT)
144 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000145
Lev Walkin29a044b2004-06-14 07:24:36 +0000146 /* Fill the body of a value */
147 for(; arcbuf < arcend; arcbuf++) {
148 cache = (cache << 7) | (*arcbuf & 0x7f);
149 bits += 7;
150 if(bits >= CHAR_BIT) {
151 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000152 *rvbuf = (cache >> bits);
153 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000154 }
155 }
156 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 *rvbuf = cache;
158 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000159 }
160 }
161
162 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000163 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
164 int v = add + *rvbuf;
Lev Walkin29a044b2004-06-14 07:24:36 +0000165 if(v & (-1 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000166 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 add = -1;
168 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000169 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000170 break;
171 }
172 }
173 if(rvbuf == rvstart) {
174 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 errno = ERANGE; /* Overflow */
176 return -1;
177 }
178 }
179
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 return 0;
181}
182
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183ssize_t
184OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 asn_app_consume_bytes_f *cb, void *app_key) {
186 char scratch[64]; /* Conservative estimate */
187 unsigned long accum; /* Bits accumulator */
188 char *p; /* Position in the scratch buffer */
189
Lev Walkin29a044b2004-06-14 07:24:36 +0000190 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
191 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 return -1;
193
Lev Walkin3251b8e2004-08-23 09:23:02 +0000194 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 ssize_t len;
196
Lev Walkin3251b8e2004-08-23 09:23:02 +0000197 /* Fill the scratch buffer in reverse. */
198 p = scratch + sizeof(scratch);
199 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000201
Lev Walkina9cc46e2004-09-22 16:06:28 +0000202 len = sizeof(scratch) - (p - scratch);
203 if(cb(p, len, app_key) < 0)
204 return -1;
205 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000206 } else {
207 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208 if(cb(scratch, 1, app_key) < 0)
209 return -1;
210 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000211 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000212}
213
214int
Lev Walkina9cc46e2004-09-22 16:06:28 +0000215OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
216 asn_app_consume_bytes_f *cb, void *app_key) {
217
218 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
219 return -1;
220
221 return 0;
222}
223
224static ssize_t
225OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
226 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000227 int startn;
228 int add = 0;
229 int i;
230
Lev Walkinf15320b2004-06-03 03:38:44 +0000231 for(i = 0, startn = 0; i < st->size; i++) {
232 uint8_t b = st->buf[i];
233 if((b & 0x80)) /* Continuation expected */
234 continue;
235
236 if(startn == 0) {
237 /*
238 * First two arcs are encoded through the backdoor.
239 */
240 if(i) {
241 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000242 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 } else if(b <= 39) {
244 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000245 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 } else if(b < 79) {
247 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000248 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 } else {
250 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000253 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 }
255
Lev Walkina9cc46e2004-09-22 16:06:28 +0000256 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 return -1;
258
Lev Walkina9cc46e2004-09-22 16:06:28 +0000259 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
260 i - startn + 1, add, cb, app_key);
261 if(add < 0) return -1;
262 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 startn = i + 1;
264 add = 0;
265 }
266
Lev Walkina9cc46e2004-09-22 16:06:28 +0000267 return wrote_len;
268}
269
Lev Walkin0fab1a62005-03-09 22:19:25 +0000270static enum xer_pbd_rval
271OBJECT_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 +0000272 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000273 const char *chunk_end = (const char *)chunk_buf + chunk_size;
274 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000275 long s_arcs[10];
276 long *arcs = s_arcs;
277 int arcs_count;
278 int ret;
279
Lev Walkine0b56e02005-02-25 12:10:27 +0000280 (void)td;
281
Lev Walkin92302252004-10-23 10:16:51 +0000282 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000283 (const char *)chunk_buf, chunk_size, arcs,
284 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
285 if(arcs_count <= 0) {
286 /* Expecting more than zero arcs */
287 return XPBD_BROKEN_ENCODING;
288 }
Lev Walkin2c34aad2005-03-10 11:50:24 +0000289 if(endptr < chunk_end) {
290 /* We have a tail of unrecognized data. Check its safety. */
291 if(!xer_is_whitespace(endptr, chunk_end - endptr))
292 return XPBD_BROKEN_ENCODING;
293 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000294
295 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000296 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000297 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000298 ret = OBJECT_IDENTIFIER_parse_arcs(
299 (const char *)chunk_buf, chunk_size,
300 arcs, arcs_count, &endptr);
301 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000302 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000303 }
304
305 /*
306 * Convert arcs into BER representation.
307 */
308 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000309 if(arcs != s_arcs) FREEMEM(arcs);
310
Lev Walkin2c34aad2005-03-10 11:50:24 +0000311 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000312}
313
314asn_dec_rval_t
315OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
316 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000317 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000318
319 return xer_decode_primitive(opt_codec_ctx, td,
320 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
321 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
322}
323
Lev Walkina9cc46e2004-09-22 16:06:28 +0000324asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000325OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000326 int ilevel, enum xer_encoder_flags_e flags,
327 asn_app_consume_bytes_f *cb, void *app_key) {
328 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
329 asn_enc_rval_t er;
330
331 (void)ilevel;
332 (void)flags;
333
334 if(!st || !st->buf)
335 _ASN_ENCODE_FAILED;
336
337 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
338 if(er.encoded < 0) _ASN_ENCODE_FAILED;
339
Lev Walkin59b176e2005-11-26 11:25:14 +0000340 _ASN_ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000341}
342
343int
Lev Walkin5e033762004-09-29 13:26:15 +0000344OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000345 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
346 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
347
348 (void)td; /* Unused argument */
349 (void)ilevel; /* Unused argument */
350
351 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000352 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000353
354 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000355 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356 return -1;
357
358 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
359 return -1;
360
Lev Walkin8e8078a2004-09-26 13:10:40 +0000361 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000362}
363
364int
Lev Walkin29a044b2004-06-14 07:24:36 +0000365OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
366 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000367 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000368 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 int startn = 0;
370 int add = 0;
371 int i;
372
Lev Walkin0787ff02004-06-17 23:43:39 +0000373 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000374 errno = EINVAL;
375 return -1;
376 }
377
378 for(i = 0; i < oid->size; i++) {
379 uint8_t b = oid->buf[i];
380 if((b & 0x80)) /* Continuation expected */
381 continue;
382
Lev Walkin29a044b2004-06-14 07:24:36 +0000383 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 /*
385 * First two arcs are encoded through the backdoor.
386 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000387 unsigned LE = 1; /* Little endian */
388 int first_arc;
389 num_arcs++;
390 if(!arc_slots) { num_arcs++; continue; }
391
392 if(i) first_arc = 2;
393 else if(b <= 39) first_arc = 0;
394 else if(b < 79) first_arc = 1;
395 else first_arc = 2;
396
397 add = -40 * first_arc;
398 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000399 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000400 + ((*(char *)&LE)?0:(arc_type_size - 1)))
401 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000402 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000403 }
404
Lev Walkin29a044b2004-06-14 07:24:36 +0000405 /* Decode, if has space */
406 if(arcs < arcs_end) {
407 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
408 i - startn + 1, add,
409 arcs, arc_type_size))
410 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000411 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000412 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000413 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000415 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000416 }
417
Lev Walkin29a044b2004-06-14 07:24:36 +0000418 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000419}
420
Lev Walkin0787ff02004-06-17 23:43:39 +0000421
422/*
423 * Save the single value as an object identifier arc.
424 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000425int
Lev Walkinb1a15552005-12-22 21:39:44 +0000426OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000427 /*
428 * The following conditions must hold:
429 * assert(arcval);
430 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000431 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000432 * assert(arcbuf);
433 */
434#ifdef WORDS_BIGENDIAN
435 const unsigned isLittleEndian = 0;
436#else
437 unsigned LE = 1;
438 unsigned isLittleEndian = *(char *)&LE;
439#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000440 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000441 unsigned int cache;
442 uint8_t *bp = arcbuf;
443 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000444 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000445
446 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000447 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
448 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000449 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000450 uint8_t *tb;
451 for(tb = buffer; a >= aend; tb++, a--)
452 if((*tb = *a) && (tb < msb))
453 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000454 tend = &buffer[arcval_size];
455 tp = msb; /* Most significant non-zero byte */
456 } else {
457 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000458 tend = (const unsigned char *)arcval + arcval_size;
459 for(tp = (const 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 Walkinb1a15552005-12-22 21:39:44 +0000494OBJECT_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 +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 Walkin523de9e2006-08-18 01:34:18 +0000504 if(!oid || !arcs || arc_type_size < 1
505 || arc_type_size > 16
506 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 errno = EINVAL;
508 return -1;
509 }
510
Lev Walkin0787ff02004-06-17 23:43:39 +0000511 switch(arc_type_size) {
512 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000513 arc0 = ((const unsigned char *)arcs)[0];
514 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000515 break;
516 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000517 arc0 = ((const unsigned short *)arcs)[0];
518 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000519 break;
520 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000521 arc0 = ((const unsigned int *)arcs)[0];
522 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000523 break;
524 default:
525 arc1 = arc0 = 0;
526 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000527 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000528 /* If more significant bytes are present,
529 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000530 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000531 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000532 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
533 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000534 arc0 = *((const unsigned char *)arcs + 0);
535 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000536 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000537 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000538 /* If more significant bytes are present,
539 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000540 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000541 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000542 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
543 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000544 }
545 }
546
547 /*
548 * The previous chapter left us with the first and the second arcs.
549 * The values are not precise (that is, they are valid only if
550 * they're less than 255), but OK for the purposes of making
551 * the sanity test below.
552 */
553 if(arc0 <= 1) {
554 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 /* 8.19.4: At most 39 subsequent values (including 0) */
556 errno = ERANGE;
557 return -1;
558 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000559 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 /* 8.19.4: Only three values are allocated from the root node */
561 errno = ERANGE;
562 return -1;
563 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000564 /*
565 * After above tests it is known that the value of arc0 is completely
566 * trustworthy (0..2). However, the arc1's value is still meaningless.
567 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000568
569 /*
570 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000571 * This estimation implicitly takes in account the following facts,
572 * that cancel each other:
573 * * the first two arcs are encoded in a single value.
574 * * the first value may require more space (+1 byte)
575 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000576 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000577 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000578 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000579 if(!buf) {
580 /* ENOMEM */
581 return -1;
582 }
583
584 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000585 * Encode the first two arcs.
586 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000589 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000590 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000591 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000592
Lev Walkin0787ff02004-06-17 23:43:39 +0000593 /*
594 * Simulate first_value = arc0 * 40 + arc1;
595 */
596 /* Copy the second (1'st) arcs[1] into the first_value */
597 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000598 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000599 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000600 const uint8_t *aend = (const unsigned char *)arcs - 1;
601 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000602 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000603 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000604 const uint8_t *a1 = (const uint8_t *)arcs;
605 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000606 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000607 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000608 /* Increase the first_value by arc0 */
609 arc0 *= 40; /* (0..80) */
610 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
611 unsigned int v = *tp;
612 v += arc0;
613 *tp = v;
614 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
615 else break;
616 }
617
618 assert(tp >= first_value);
619
620 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
621 fv - first_value, 1);
622 }
623
624 /*
625 * Save the rest of arcs.
626 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000627 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000628 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000629 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000630 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
631 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000632 }
633
Lev Walkin34b2a932004-06-17 23:46:45 +0000634 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000635
636 /*
637 * Replace buffer.
638 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000639 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000640 bp = oid->buf;
641 oid->buf = buf;
642 if(bp) FREEMEM(bp);
643
644 return 0;
645}
Lev Walkinc4c61962004-06-14 08:17:27 +0000646
Lev Walkin92302252004-10-23 10:16:51 +0000647
648int
649OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000650 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000651 unsigned int arcs_count = 0;
652 const char *oid_end;
Lev Walkinf5927112012-09-03 00:48:45 -0700653 const char *value_start;
Lev Walkin92302252004-10-23 10:16:51 +0000654 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700655 ST_LEADSPACE,
656 ST_TAILSPACE,
Lev Walkin92302252004-10-23 10:16:51 +0000657 ST_WAITDIGITS, /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700658 ST_DIGITS /* INVARIANT: value_start != 0 in this state */
659 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000660
661 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000662 if(opt_oid_text_end) *opt_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
Lev Walkinf5927112012-09-03 00:48:45 -0700670#define _OID_CAPTURE_ARC(value_start, oid_text) do { \
671 long value; \
672 switch(asn_strtol(value_start, oid_text, &value)) { \
673 case ASN_STRTOL_OK: \
674 if(arcs_count < arcs_slots) \
675 arcs[arcs_count] = value; \
676 arcs_count++; \
677 break; \
678 case ASN_STRTOL_ERROR_RANGE: \
679 if(opt_oid_text_end) \
680 *opt_oid_text_end = oid_text; \
681 errno = ERANGE; \
682 return -1; \
683 case ASN_STRTOL_ERROR_INVAL: \
684 if(opt_oid_text_end) \
685 *opt_oid_text_end = oid_text; \
686 errno = EINVAL; \
687 return -1; \
688 } \
689 } while(0)
690
Lev Walkin92302252004-10-23 10:16:51 +0000691 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
692 switch(*oid_text) {
693 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700694 switch(state) {
695 case ST_LEADSPACE:
696 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000697 continue;
Lev Walkinf5927112012-09-03 00:48:45 -0700698 case ST_DIGITS:
699 _OID_CAPTURE_ARC(value_start, oid_text);
700 state = ST_TAILSPACE;
701 continue;
702 case ST_WAITDIGITS:
Lev Walkin92302252004-10-23 10:16:51 +0000703 break;
704 }
Lev Walkinf5927112012-09-03 00:48:45 -0700705 case 0x2e: /* '.' */
706 switch(state) {
707 case ST_LEADSPACE:
708 case ST_WAITDIGITS:
709 break;
710 case ST_TAILSPACE:
711 state = ST_WAITDIGITS;
712 break;
713 case ST_DIGITS:
714 _OID_CAPTURE_ARC(value_start, oid_text);
715 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000716 continue;
717 }
Lev Walkinf5927112012-09-03 00:48:45 -0700718 break;
719 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
720 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
721 switch(state) {
722 case ST_TAILSPACE:
723 state = ST_WAITDIGITS;
724 break;
725 case ST_LEADSPACE:
726 case ST_WAITDIGITS:
727 state = ST_DIGITS;
728 value_start = oid_text;
729 continue;
730 case ST_DIGITS:
731 continue;
732 }
733 break;
Lev Walkin92302252004-10-23 10:16:51 +0000734 default:
735 /* Unexpected symbols */
736 state = ST_WAITDIGITS;
737 break;
738 } /* switch() */
739 break;
740 } /* for() */
741
742
Lev Walkin093ba2e2005-04-04 21:10:06 +0000743 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000744
745 /* Finalize last arc */
746 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700747 case ST_LEADSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000748 case ST_WAITDIGITS:
749 errno = EINVAL;
750 return -1;
751 case ST_DIGITS:
Lev Walkinf5927112012-09-03 00:48:45 -0700752 _OID_CAPTURE_ARC(value_start, oid_text);
Lev Walkin92302252004-10-23 10:16:51 +0000753 /* Fall through */
Lev Walkinf5927112012-09-03 00:48:45 -0700754 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000755 default:
756 return arcs_count;
757 }
758}
759
760