blob: 18a23db8e0a7fa56e5cd720ba14c4888d89f7032 [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 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070015static const 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 Walkincd2f48e2017-08-10 02:14:59 -070023 OCTET_STRING_compare, /* Implemented in terms of a string comparison */
Lev Walkinf15320b2004-06-03 03:38:44 +000024 OBJECT_IDENTIFIER_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000025 ber_decode_primitive,
26 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000027 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000028 OBJECT_IDENTIFIER_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070029#ifdef ASN_DISABLE_OER_SUPPORT
30 0,
31 0,
32#else
33 0,
34 0,
35#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040036#ifdef ASN_DISABLE_PER_SUPPORT
37 0,
38 0,
39#else
40 OCTET_STRING_decode_uper,
41 OCTET_STRING_encode_uper,
42#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000043 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000044 asn_DEF_OBJECT_IDENTIFIER_tags,
45 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
46 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
47 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
48 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
49 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070050 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000051 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000052 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000053 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000054};
55
56
Lev Walkinf15320b2004-06-03 03:38:44 +000057int
Lev Walkin5e033762004-09-29 13:26:15 +000058OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000059 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000060 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000061
62 if(st && st->buf) {
63 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070064 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000065 "%s: at least one numerical value "
66 "expected (%s:%d)",
67 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000068 return -1;
69 }
70 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070071 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000072 "%s: value not given (%s:%d)",
73 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000074 return -1;
75 }
76
77 return 0;
78}
79
Lev Walkinc4c61962004-06-14 08:17:27 +000080
Lev Walkinf15320b2004-06-03 03:38:44 +000081int
Wim Lewis14e6b162014-07-23 16:06:01 -070082OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
Lev Walkin50b0f6c2006-10-16 12:01:36 +000083 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070084 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000085 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000086 unsigned char *rvbuf = (unsigned char *)rvbufp;
87 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000088 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000089
Lev Walkin29a044b2004-06-14 07:24:36 +000090 rvsize *= CHAR_BIT; /* bytes to bits */
91 arclen *= 7; /* bytes to bits */
92
93 /*
94 * The arc has the number of bits
95 * cannot be represented using supplied return value type.
96 */
97 if(arclen > rvsize) {
98 if(arclen > (rvsize + CHAR_BIT)) {
99 errno = ERANGE; /* Overflow */
100 return -1;
101 } else {
102 /*
103 * Even if the number of bits in the arc representation
104 * is higher than the width of supplied * return value
105 * type, there is still possible to fit it when there
106 * are few unused high bits in the arc value
107 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000108 *
109 * Moreover, there is a possibility that the
110 * number could actually fit the arc space, given
111 * that add is negative, but we don't handle
112 * such "temporary lack of precision" situation here.
113 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000114 */
115 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
116 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 errno = ERANGE; /* Overflow */
118 return -1;
119 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000120 /* Fool the routine computing unused bits */
121 arclen -= 7;
122 cache = *arcbuf & 0x7f;
123 arcbuf++;
124 }
125 }
126
Lev Walkinc4c61962004-06-14 08:17:27 +0000127 /* Faster path for common size */
128 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
129 unsigned long accum;
130 /* Gather all bits into the accumulator */
131 for(accum = cache; arcbuf < arcend; arcbuf++)
132 accum = (accum << 7) | (*arcbuf & ~0x80);
133 if(accum < (unsigned)-add) {
134 errno = ERANGE; /* Overflow */
135 return -1;
136 }
Lev Walkin70732a22013-12-07 10:55:14 -0800137 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000138 return 0;
139 }
140
Lev Walkin29a044b2004-06-14 07:24:36 +0000141#ifndef WORDS_BIGENDIAN
142 if(*(unsigned char *)&LE) { /* Little endian (x86) */
143 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000144 rvbuf += rvsize / CHAR_BIT - 1;
145 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000146 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000147 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000148#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000149 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000150
Lev Walkinc4c61962004-06-14 08:17:27 +0000151 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000152 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000153
Lev Walkin29a044b2004-06-14 07:24:36 +0000154 /* Clear the high unused bits */
155 for(bits = rvsize - arclen;
156 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 rvbuf += inc, bits -= CHAR_BIT)
158 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000159
Lev Walkin29a044b2004-06-14 07:24:36 +0000160 /* Fill the body of a value */
161 for(; arcbuf < arcend; arcbuf++) {
162 cache = (cache << 7) | (*arcbuf & 0x7f);
163 bits += 7;
164 if(bits >= CHAR_BIT) {
165 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000166 *rvbuf = (cache >> bits);
167 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000168 }
169 }
170 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000171 *rvbuf = cache;
172 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000173 }
174 }
175
176 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000177 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
178 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700179 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000180 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000181 add = -1;
182 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000183 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000184 break;
185 }
186 }
187 if(rvbuf == rvstart) {
188 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 errno = ERANGE; /* Overflow */
190 return -1;
191 }
192 }
193
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 return 0;
195}
196
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700198OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 asn_app_consume_bytes_f *cb, void *app_key) {
200 char scratch[64]; /* Conservative estimate */
201 unsigned long accum; /* Bits accumulator */
202 char *p; /* Position in the scratch buffer */
203
Lev Walkin29a044b2004-06-14 07:24:36 +0000204 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
205 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000206 return -1;
207
Lev Walkin3251b8e2004-08-23 09:23:02 +0000208 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000209 ssize_t len;
210
Lev Walkin3251b8e2004-08-23 09:23:02 +0000211 /* Fill the scratch buffer in reverse. */
212 p = scratch + sizeof(scratch);
213 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000215
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216 len = sizeof(scratch) - (p - scratch);
217 if(cb(p, len, app_key) < 0)
218 return -1;
219 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000220 } else {
221 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000222 if(cb(scratch, 1, app_key) < 0)
223 return -1;
224 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000225 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000226}
227
228int
Wim Lewis14e6b162014-07-23 16:06:01 -0700229OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000230 asn_app_consume_bytes_f *cb, void *app_key) {
231
232 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
233 return -1;
234
235 return 0;
236}
237
238static ssize_t
239OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
240 ssize_t wrote_len = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700241 size_t startn;
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700243 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000244
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 for(i = 0, startn = 0; i < st->size; i++) {
246 uint8_t b = st->buf[i];
247 if((b & 0x80)) /* Continuation expected */
248 continue;
249
250 if(startn == 0) {
251 /*
252 * First two arcs are encoded through the backdoor.
253 */
254 if(i) {
255 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000256 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 } else if(b <= 39) {
258 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000259 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 } else if(b < 79) {
261 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000262 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 } else {
264 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000265 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000267 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000268 }
269
Lev Walkina9cc46e2004-09-22 16:06:28 +0000270 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 return -1;
272
Lev Walkina9cc46e2004-09-22 16:06:28 +0000273 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
274 i - startn + 1, add, cb, app_key);
275 if(add < 0) return -1;
276 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000277 startn = i + 1;
278 add = 0;
279 }
280
Lev Walkina9cc46e2004-09-22 16:06:28 +0000281 return wrote_len;
282}
283
Lev Walkin0fab1a62005-03-09 22:19:25 +0000284static enum xer_pbd_rval
285OBJECT_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 +0000286 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000287 const char *chunk_end = (const char *)chunk_buf + chunk_size;
288 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000289 long s_arcs[10];
290 long *arcs = s_arcs;
291 int arcs_count;
292 int ret;
293
Lev Walkine0b56e02005-02-25 12:10:27 +0000294 (void)td;
295
Lev Walkin92302252004-10-23 10:16:51 +0000296 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000297 (const char *)chunk_buf, chunk_size, arcs,
298 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700299 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000300 /* Expecting more than zero arcs */
301 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700302 } else if(arcs_count == 0) {
303 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000304 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700305 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000306
307 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000308 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000309 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000310 ret = OBJECT_IDENTIFIER_parse_arcs(
311 (const char *)chunk_buf, chunk_size,
312 arcs, arcs_count, &endptr);
313 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000314 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000315 }
316
317 /*
318 * Convert arcs into BER representation.
319 */
320 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000321 if(arcs != s_arcs) FREEMEM(arcs);
322
Lev Walkin2c34aad2005-03-10 11:50:24 +0000323 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000324}
325
326asn_dec_rval_t
327OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
328 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000329 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000330
331 return xer_decode_primitive(opt_codec_ctx, td,
332 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
333 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
334}
335
Lev Walkina9cc46e2004-09-22 16:06:28 +0000336asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000337OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000338 int ilevel, enum xer_encoder_flags_e flags,
339 asn_app_consume_bytes_f *cb, void *app_key) {
340 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
341 asn_enc_rval_t er;
342
343 (void)ilevel;
344 (void)flags;
345
346 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700347 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000348
349 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700350 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000351
Lev Walkin7c1dc052016-03-14 03:08:15 -0700352 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000353}
354
355int
Lev Walkin5e033762004-09-29 13:26:15 +0000356OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000357 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
358 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
359
360 (void)td; /* Unused argument */
361 (void)ilevel; /* Unused argument */
362
363 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000364 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000365
366 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000367 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000368 return -1;
369
370 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
371 return -1;
372
Lev Walkin8e8078a2004-09-26 13:10:40 +0000373 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000374}
375
376int
Wim Lewis14e6b162014-07-23 16:06:01 -0700377OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000378 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000379 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000380 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 int startn = 0;
382 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700383 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000384
Lev Walkin0787ff02004-06-17 23:43:39 +0000385 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 errno = EINVAL;
387 return -1;
388 }
389
390 for(i = 0; i < oid->size; i++) {
391 uint8_t b = oid->buf[i];
392 if((b & 0x80)) /* Continuation expected */
393 continue;
394
Lev Walkin29a044b2004-06-14 07:24:36 +0000395 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000396 /*
397 * First two arcs are encoded through the backdoor.
398 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000399 unsigned LE = 1; /* Little endian */
400 int first_arc;
401 num_arcs++;
402 if(!arc_slots) { num_arcs++; continue; }
403
404 if(i) first_arc = 2;
405 else if(b <= 39) first_arc = 0;
406 else if(b < 79) first_arc = 1;
407 else first_arc = 2;
408
409 add = -40 * first_arc;
410 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000411 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000412 + ((*(char *)&LE)?0:(arc_type_size - 1)))
413 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000414 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 }
416
Lev Walkin29a044b2004-06-14 07:24:36 +0000417 /* Decode, if has space */
418 if(arcs < arcs_end) {
419 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
420 i - startn + 1, add,
421 arcs, arc_type_size))
422 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000423 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000424 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000425 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000427 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 }
429
Lev Walkin29a044b2004-06-14 07:24:36 +0000430 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000431}
432
Lev Walkin0787ff02004-06-17 23:43:39 +0000433
434/*
435 * Save the single value as an object identifier arc.
436 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000437int
Lev Walkinb1a15552005-12-22 21:39:44 +0000438OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000439 /*
440 * The following conditions must hold:
441 * assert(arcval);
442 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000443 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000444 * assert(arcbuf);
445 */
446#ifdef WORDS_BIGENDIAN
447 const unsigned isLittleEndian = 0;
448#else
449 unsigned LE = 1;
450 unsigned isLittleEndian = *(char *)&LE;
451#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000452 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000453 unsigned int cache;
454 uint8_t *bp = arcbuf;
455 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000456 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000457
458 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000459 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
460 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000461 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000462 uint8_t *tb;
463 for(tb = buffer; a >= aend; tb++, a--)
464 if((*tb = *a) && (tb < msb))
465 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000466 tend = &buffer[arcval_size];
467 tp = msb; /* Most significant non-zero byte */
468 } else {
469 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000470 tend = (const unsigned char *)arcval + arcval_size;
471 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000472 if(*tp) break;
473 }
474
475 /*
476 * Split the value in 7-bits chunks.
477 */
478 bits = ((tend - tp) * CHAR_BIT) % 7;
479 if(bits) {
480 cache = *tp >> (CHAR_BIT - bits);
481 if(cache) {
482 *bp++ = cache | 0x80;
483 cache = *tp++;
484 bits = CHAR_BIT - bits;
485 } else {
486 bits = -bits;
487 }
488 } else {
489 cache = 0;
490 }
491 for(; tp < tend; tp++) {
492 cache = (cache << CHAR_BIT) + *tp;
493 bits += CHAR_BIT;
494 while(bits >= 7) {
495 bits -= 7;
496 *bp++ = 0x80 | (cache >> bits);
497 }
498 }
499 if(bits) *bp++ = cache;
500 bp[-1] &= 0x7f; /* Clear the last bit */
501
502 return bp - arcbuf;
503}
504
Lev Walkinf15320b2004-06-03 03:38:44 +0000505int
Lev Walkinb1a15552005-12-22 21:39:44 +0000506OBJECT_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 +0000507 uint8_t *buf;
508 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000509 unsigned LE = 1; /* Little endian (x86) */
510 unsigned isLittleEndian = *((char *)&LE);
511 unsigned int arc0;
512 unsigned int arc1;
513 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000514 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000515
Lev Walkin523de9e2006-08-18 01:34:18 +0000516 if(!oid || !arcs || arc_type_size < 1
517 || arc_type_size > 16
518 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000519 errno = EINVAL;
520 return -1;
521 }
522
Lev Walkin0787ff02004-06-17 23:43:39 +0000523 switch(arc_type_size) {
524 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000525 arc0 = ((const unsigned char *)arcs)[0];
526 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000527 break;
528 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000529 arc0 = ((const unsigned short *)arcs)[0];
530 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000531 break;
532 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000533 arc0 = ((const unsigned int *)arcs)[0];
534 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000535 break;
536 default:
537 arc1 = arc0 = 0;
538 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000539 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000540 /* If more significant bytes are present,
541 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000542 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000543 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000544 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
545 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000546 arc0 = *((const unsigned char *)arcs + 0);
547 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000548 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000549 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000550 /* If more significant bytes are present,
551 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000552 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000553 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000554 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
555 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000556 }
557 }
558
559 /*
560 * The previous chapter left us with the first and the second arcs.
561 * The values are not precise (that is, they are valid only if
562 * they're less than 255), but OK for the purposes of making
563 * the sanity test below.
564 */
565 if(arc0 <= 1) {
566 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 /* 8.19.4: At most 39 subsequent values (including 0) */
568 errno = ERANGE;
569 return -1;
570 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000571 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 /* 8.19.4: Only three values are allocated from the root node */
573 errno = ERANGE;
574 return -1;
575 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000576 /*
577 * After above tests it is known that the value of arc0 is completely
578 * trustworthy (0..2). However, the arc1's value is still meaningless.
579 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000580
581 /*
582 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000583 * This estimation implicitly takes in account the following facts,
584 * that cancel each other:
585 * * the first two arcs are encoded in a single value.
586 * * the first value may require more space (+1 byte)
587 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000589 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000590 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 if(!buf) {
592 /* ENOMEM */
593 return -1;
594 }
595
596 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000597 * Encode the first two arcs.
598 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000599 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000600 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000601 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000602 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000603 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000604
Lev Walkin0787ff02004-06-17 23:43:39 +0000605 /*
606 * Simulate first_value = arc0 * 40 + arc1;
607 */
608 /* Copy the second (1'st) arcs[1] into the first_value */
609 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000610 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000611 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000612 const uint8_t *aend = (const unsigned char *)arcs - 1;
613 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000614 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000615 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000616 const uint8_t *a1 = (const uint8_t *)arcs;
617 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000618 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000619 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000620 /* Increase the first_value by arc0 */
621 arc0 *= 40; /* (0..80) */
622 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
623 unsigned int v = *tp;
624 v += arc0;
625 *tp = v;
626 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
627 else break;
628 }
629
630 assert(tp >= first_value);
631
632 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
633 fv - first_value, 1);
634 }
635
636 /*
637 * Save the rest of arcs.
638 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000639 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000640 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000641 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000642 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
643 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000644 }
645
Lev Walkin34b2a932004-06-17 23:46:45 +0000646 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000647
648 /*
649 * Replace buffer.
650 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000651 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000652 bp = oid->buf;
653 oid->buf = buf;
654 if(bp) FREEMEM(bp);
655
656 return 0;
657}
Lev Walkinc4c61962004-06-14 08:17:27 +0000658
Lev Walkin92302252004-10-23 10:16:51 +0000659
660int
661OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000662 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000663 unsigned int arcs_count = 0;
664 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000665 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700666 ST_LEADSPACE,
667 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700668 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800669 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700670 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000671
672 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000673 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000674 errno = EINVAL;
675 return -1;
676 }
677
678 if(oid_txt_length == -1)
679 oid_txt_length = strlen(oid_text);
680
Lev Walkincad560a2013-03-16 07:00:58 -0700681#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
682 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700683 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700684 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700685 case ASN_STRTOX_EXTRA_DATA: \
686 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700687 if(arcs_count < arcs_slots) \
688 arcs[arcs_count] = value; \
689 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700690 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700691 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700692 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700693 if(opt_oid_text_end) \
694 *opt_oid_text_end = oid_text; \
695 errno = ERANGE; \
696 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700697 case ASN_STRTOX_ERROR_INVAL: \
698 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700699 if(opt_oid_text_end) \
700 *opt_oid_text_end = oid_text; \
701 errno = EINVAL; \
702 return -1; \
703 } \
704 } while(0)
705
Lev Walkin92302252004-10-23 10:16:51 +0000706 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
707 switch(*oid_text) {
708 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700709 switch(state) {
710 case ST_LEADSPACE:
711 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000712 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700713 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700714 state = ST_TAILSPACE;
715 continue;
716 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700717 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000718 }
Lev Walkincad560a2013-03-16 07:00:58 -0700719 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700720 case 0x2e: /* '.' */
721 switch(state) {
722 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700723 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700724 case ST_WAITDIGITS:
725 if(opt_oid_text_end)
726 *opt_oid_text_end = oid_text;
727 errno = EINVAL; /* Broken OID */
728 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700729 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700730 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700731 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000732 continue;
733 }
Lev Walkinf5927112012-09-03 00:48:45 -0700734 break;
735 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
736 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
737 switch(state) {
738 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700739 case ST_AFTERVALUE:
740 if(opt_oid_text_end)
741 *opt_oid_text_end = oid_text;
742 errno = EINVAL; /* "1. 1" => broken OID */
743 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700744 case ST_LEADSPACE:
745 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700746 _OID_CAPTURE_ARC(oid_text, oid_end);
747 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700748 continue;
749 }
750 break;
Lev Walkin92302252004-10-23 10:16:51 +0000751 default:
752 /* Unexpected symbols */
753 state = ST_WAITDIGITS;
754 break;
755 } /* switch() */
756 break;
757 } /* for() */
758
759
Lev Walkin093ba2e2005-04-04 21:10:06 +0000760 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000761
762 /* Finalize last arc */
763 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700764 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700765 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700766 case ST_WAITDIGITS:
767 errno = EINVAL; /* Broken OID */
768 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700769 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700770 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000771 return arcs_count;
772 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700773
774 errno = EINVAL; /* Broken OID */
775 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000776}
777
778