blob: ffca36ed2d170395d44fa705cd9d3fe28b735c0e [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};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080018asn_TYPE_operation_t asn_OP_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 Walkincd2f48e2017-08-10 02:14:59 -070021 OCTET_STRING_compare, /* Implemented in terms of a string comparison */
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 Walkincc159472017-07-06 08:26:36 -070026#ifdef ASN_DISABLE_OER_SUPPORT
27 0,
28 0,
29#else
30 0,
31 0,
32#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040033#ifdef ASN_DISABLE_PER_SUPPORT
34 0,
35 0,
36#else
37 OCTET_STRING_decode_uper,
38 OCTET_STRING_encode_uper,
39#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080040 0 /* Use generic outmost tag fetcher */
41};
42asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
43 "OBJECT IDENTIFIER",
44 "OBJECT_IDENTIFIER",
45 &asn_OP_OBJECT_IDENTIFIER,
46 OBJECT_IDENTIFIER_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000047 asn_DEF_OBJECT_IDENTIFIER_tags,
48 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
49 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
50 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
51 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
52 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070053 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000054 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000055 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000056 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000057};
58
59
Lev Walkinf15320b2004-06-03 03:38:44 +000060int
Lev Walkin5e033762004-09-29 13:26:15 +000061OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000062 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000063 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000064
65 if(st && st->buf) {
66 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070067 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000068 "%s: at least one numerical value "
69 "expected (%s:%d)",
70 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000071 return -1;
72 }
73 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070074 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000075 "%s: value not given (%s:%d)",
76 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000077 return -1;
78 }
79
80 return 0;
81}
82
Lev Walkinc4c61962004-06-14 08:17:27 +000083
Lev Walkinf15320b2004-06-03 03:38:44 +000084int
Wim Lewis14e6b162014-07-23 16:06:01 -070085OBJECT_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 +000086 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070087 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000088 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000089 unsigned char *rvbuf = (unsigned char *)rvbufp;
90 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000091 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000092
Lev Walkin29a044b2004-06-14 07:24:36 +000093 rvsize *= CHAR_BIT; /* bytes to bits */
94 arclen *= 7; /* bytes to bits */
95
96 /*
97 * The arc has the number of bits
98 * cannot be represented using supplied return value type.
99 */
100 if(arclen > rvsize) {
101 if(arclen > (rvsize + CHAR_BIT)) {
102 errno = ERANGE; /* Overflow */
103 return -1;
104 } else {
105 /*
106 * Even if the number of bits in the arc representation
107 * is higher than the width of supplied * return value
108 * type, there is still possible to fit it when there
109 * are few unused high bits in the arc value
110 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000111 *
112 * Moreover, there is a possibility that the
113 * number could actually fit the arc space, given
114 * that add is negative, but we don't handle
115 * such "temporary lack of precision" situation here.
116 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000117 */
118 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
119 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 errno = ERANGE; /* Overflow */
121 return -1;
122 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000123 /* Fool the routine computing unused bits */
124 arclen -= 7;
125 cache = *arcbuf & 0x7f;
126 arcbuf++;
127 }
128 }
129
Lev Walkinc4c61962004-06-14 08:17:27 +0000130 /* Faster path for common size */
131 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
132 unsigned long accum;
133 /* Gather all bits into the accumulator */
134 for(accum = cache; arcbuf < arcend; arcbuf++)
135 accum = (accum << 7) | (*arcbuf & ~0x80);
136 if(accum < (unsigned)-add) {
137 errno = ERANGE; /* Overflow */
138 return -1;
139 }
Lev Walkin70732a22013-12-07 10:55:14 -0800140 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000141 return 0;
142 }
143
Lev Walkin29a044b2004-06-14 07:24:36 +0000144#ifndef WORDS_BIGENDIAN
145 if(*(unsigned char *)&LE) { /* Little endian (x86) */
146 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000147 rvbuf += rvsize / CHAR_BIT - 1;
148 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000149 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000150 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000151#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000152 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000153
Lev Walkinc4c61962004-06-14 08:17:27 +0000154 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000155 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000156
Lev Walkin29a044b2004-06-14 07:24:36 +0000157 /* Clear the high unused bits */
158 for(bits = rvsize - arclen;
159 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000160 rvbuf += inc, bits -= CHAR_BIT)
161 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000162
Lev Walkin29a044b2004-06-14 07:24:36 +0000163 /* Fill the body of a value */
164 for(; arcbuf < arcend; arcbuf++) {
165 cache = (cache << 7) | (*arcbuf & 0x7f);
166 bits += 7;
167 if(bits >= CHAR_BIT) {
168 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000169 *rvbuf = (cache >> bits);
170 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000171 }
172 }
173 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000174 *rvbuf = cache;
175 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000176 }
177 }
178
179 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000180 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
181 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700182 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000183 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000184 add = -1;
185 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000186 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000187 break;
188 }
189 }
190 if(rvbuf == rvstart) {
191 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 errno = ERANGE; /* Overflow */
193 return -1;
194 }
195 }
196
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 return 0;
198}
199
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700201OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000202 asn_app_consume_bytes_f *cb, void *app_key) {
203 char scratch[64]; /* Conservative estimate */
204 unsigned long accum; /* Bits accumulator */
205 char *p; /* Position in the scratch buffer */
206
Lev Walkin29a044b2004-06-14 07:24:36 +0000207 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
208 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000209 return -1;
210
Lev Walkin3251b8e2004-08-23 09:23:02 +0000211 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212 ssize_t len;
213
Lev Walkin3251b8e2004-08-23 09:23:02 +0000214 /* Fill the scratch buffer in reverse. */
215 p = scratch + sizeof(scratch);
216 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000217 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000218
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219 len = sizeof(scratch) - (p - scratch);
220 if(cb(p, len, app_key) < 0)
221 return -1;
222 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000223 } else {
224 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000225 if(cb(scratch, 1, app_key) < 0)
226 return -1;
227 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000228 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000229}
230
231int
Wim Lewis14e6b162014-07-23 16:06:01 -0700232OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000233 asn_app_consume_bytes_f *cb, void *app_key) {
234
235 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
236 return -1;
237
238 return 0;
239}
240
241static ssize_t
242OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
243 ssize_t wrote_len = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700244 size_t startn;
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700246 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000247
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 for(i = 0, startn = 0; i < st->size; i++) {
249 uint8_t b = st->buf[i];
250 if((b & 0x80)) /* Continuation expected */
251 continue;
252
253 if(startn == 0) {
254 /*
255 * First two arcs are encoded through the backdoor.
256 */
257 if(i) {
258 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000259 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 } else if(b <= 39) {
261 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000262 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 } else if(b < 79) {
264 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000265 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 } else {
267 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000268 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000270 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 }
272
Lev Walkina9cc46e2004-09-22 16:06:28 +0000273 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000274 return -1;
275
Lev Walkina9cc46e2004-09-22 16:06:28 +0000276 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
277 i - startn + 1, add, cb, app_key);
278 if(add < 0) return -1;
279 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000280 startn = i + 1;
281 add = 0;
282 }
283
Lev Walkina9cc46e2004-09-22 16:06:28 +0000284 return wrote_len;
285}
286
Lev Walkin0fab1a62005-03-09 22:19:25 +0000287static enum xer_pbd_rval
288OBJECT_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 +0000289 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000290 const char *chunk_end = (const char *)chunk_buf + chunk_size;
291 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000292 long s_arcs[10];
293 long *arcs = s_arcs;
294 int arcs_count;
295 int ret;
296
Lev Walkine0b56e02005-02-25 12:10:27 +0000297 (void)td;
298
Lev Walkin92302252004-10-23 10:16:51 +0000299 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000300 (const char *)chunk_buf, chunk_size, arcs,
301 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700302 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000303 /* Expecting more than zero arcs */
304 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700305 } else if(arcs_count == 0) {
306 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000307 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700308 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000309
310 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000311 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000312 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000313 ret = OBJECT_IDENTIFIER_parse_arcs(
314 (const char *)chunk_buf, chunk_size,
315 arcs, arcs_count, &endptr);
316 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000317 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000318 }
319
320 /*
321 * Convert arcs into BER representation.
322 */
323 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000324 if(arcs != s_arcs) FREEMEM(arcs);
325
Lev Walkin2c34aad2005-03-10 11:50:24 +0000326 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000327}
328
329asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700330OBJECT_IDENTIFIER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin92302252004-10-23 10:16:51 +0000331 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000332 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000333
334 return xer_decode_primitive(opt_codec_ctx, td,
335 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
336 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
337}
338
Lev Walkina9cc46e2004-09-22 16:06:28 +0000339asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000340OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000341 int ilevel, enum xer_encoder_flags_e flags,
342 asn_app_consume_bytes_f *cb, void *app_key) {
343 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
344 asn_enc_rval_t er;
345
346 (void)ilevel;
347 (void)flags;
348
349 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700350 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000351
352 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700353 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000354
Lev Walkin7c1dc052016-03-14 03:08:15 -0700355 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356}
357
358int
Lev Walkin5e033762004-09-29 13:26:15 +0000359OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000360 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
361 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
362
363 (void)td; /* Unused argument */
364 (void)ilevel; /* Unused argument */
365
366 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000367 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000368
369 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000370 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000371 return -1;
372
373 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
374 return -1;
375
Lev Walkin8e8078a2004-09-26 13:10:40 +0000376 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000377}
378
379int
Wim Lewis14e6b162014-07-23 16:06:01 -0700380OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000381 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000382 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000383 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 int startn = 0;
385 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700386 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000387
Lev Walkin0787ff02004-06-17 23:43:39 +0000388 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 errno = EINVAL;
390 return -1;
391 }
392
393 for(i = 0; i < oid->size; i++) {
394 uint8_t b = oid->buf[i];
395 if((b & 0x80)) /* Continuation expected */
396 continue;
397
Lev Walkin29a044b2004-06-14 07:24:36 +0000398 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000399 /*
400 * First two arcs are encoded through the backdoor.
401 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000402 unsigned LE = 1; /* Little endian */
403 int first_arc;
404 num_arcs++;
405 if(!arc_slots) { num_arcs++; continue; }
406
407 if(i) first_arc = 2;
408 else if(b <= 39) first_arc = 0;
409 else if(b < 79) first_arc = 1;
410 else first_arc = 2;
411
412 add = -40 * first_arc;
413 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000414 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000415 + ((*(char *)&LE)?0:(arc_type_size - 1)))
416 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000417 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 }
419
Lev Walkin29a044b2004-06-14 07:24:36 +0000420 /* Decode, if has space */
421 if(arcs < arcs_end) {
422 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
423 i - startn + 1, add,
424 arcs, arc_type_size))
425 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000427 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000428 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000430 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 }
432
Lev Walkin29a044b2004-06-14 07:24:36 +0000433 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434}
435
Lev Walkin0787ff02004-06-17 23:43:39 +0000436
437/*
438 * Save the single value as an object identifier arc.
439 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000440int
Lev Walkinb1a15552005-12-22 21:39:44 +0000441OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000442 /*
443 * The following conditions must hold:
444 * assert(arcval);
445 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000446 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000447 * assert(arcbuf);
448 */
449#ifdef WORDS_BIGENDIAN
450 const unsigned isLittleEndian = 0;
451#else
452 unsigned LE = 1;
453 unsigned isLittleEndian = *(char *)&LE;
454#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000455 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000456 unsigned int cache;
457 uint8_t *bp = arcbuf;
458 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000459 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000460
461 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000462 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
463 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000464 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000465 uint8_t *tb;
466 for(tb = buffer; a >= aend; tb++, a--)
467 if((*tb = *a) && (tb < msb))
468 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000469 tend = &buffer[arcval_size];
470 tp = msb; /* Most significant non-zero byte */
471 } else {
472 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000473 tend = (const unsigned char *)arcval + arcval_size;
474 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000475 if(*tp) break;
476 }
477
478 /*
479 * Split the value in 7-bits chunks.
480 */
481 bits = ((tend - tp) * CHAR_BIT) % 7;
482 if(bits) {
483 cache = *tp >> (CHAR_BIT - bits);
484 if(cache) {
485 *bp++ = cache | 0x80;
486 cache = *tp++;
487 bits = CHAR_BIT - bits;
488 } else {
489 bits = -bits;
490 }
491 } else {
492 cache = 0;
493 }
494 for(; tp < tend; tp++) {
495 cache = (cache << CHAR_BIT) + *tp;
496 bits += CHAR_BIT;
497 while(bits >= 7) {
498 bits -= 7;
499 *bp++ = 0x80 | (cache >> bits);
500 }
501 }
502 if(bits) *bp++ = cache;
503 bp[-1] &= 0x7f; /* Clear the last bit */
504
505 return bp - arcbuf;
506}
507
Lev Walkinf15320b2004-06-03 03:38:44 +0000508int
Lev Walkinb1a15552005-12-22 21:39:44 +0000509OBJECT_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 +0000510 uint8_t *buf;
511 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000512 unsigned LE = 1; /* Little endian (x86) */
513 unsigned isLittleEndian = *((char *)&LE);
514 unsigned int arc0;
515 unsigned int arc1;
516 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000517 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000518
Lev Walkin523de9e2006-08-18 01:34:18 +0000519 if(!oid || !arcs || arc_type_size < 1
520 || arc_type_size > 16
521 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000522 errno = EINVAL;
523 return -1;
524 }
525
Lev Walkin0787ff02004-06-17 23:43:39 +0000526 switch(arc_type_size) {
527 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000528 arc0 = ((const unsigned char *)arcs)[0];
529 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000530 break;
531 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000532 arc0 = ((const unsigned short *)arcs)[0];
533 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000534 break;
535 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000536 arc0 = ((const unsigned int *)arcs)[0];
537 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000538 break;
539 default:
540 arc1 = arc0 = 0;
541 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000542 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000543 /* If more significant bytes are present,
544 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000545 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000546 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000547 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
548 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000549 arc0 = *((const unsigned char *)arcs + 0);
550 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000551 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000552 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000553 /* If more significant bytes are present,
554 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000555 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000556 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000557 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
558 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000559 }
560 }
561
562 /*
563 * The previous chapter left us with the first and the second arcs.
564 * The values are not precise (that is, they are valid only if
565 * they're less than 255), but OK for the purposes of making
566 * the sanity test below.
567 */
568 if(arc0 <= 1) {
569 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 /* 8.19.4: At most 39 subsequent values (including 0) */
571 errno = ERANGE;
572 return -1;
573 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000574 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000575 /* 8.19.4: Only three values are allocated from the root node */
576 errno = ERANGE;
577 return -1;
578 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000579 /*
580 * After above tests it is known that the value of arc0 is completely
581 * trustworthy (0..2). However, the arc1's value is still meaningless.
582 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000583
584 /*
585 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000586 * This estimation implicitly takes in account the following facts,
587 * that cancel each other:
588 * * the first two arcs are encoded in a single value.
589 * * the first value may require more space (+1 byte)
590 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000592 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000593 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000594 if(!buf) {
595 /* ENOMEM */
596 return -1;
597 }
598
599 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000600 * Encode the first two arcs.
601 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000602 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000603 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000604 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000605 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000606 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000607
Lev Walkin0787ff02004-06-17 23:43:39 +0000608 /*
609 * Simulate first_value = arc0 * 40 + arc1;
610 */
611 /* Copy the second (1'st) arcs[1] into the first_value */
612 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000613 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000614 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000615 const uint8_t *aend = (const unsigned char *)arcs - 1;
616 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000617 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000618 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000619 const uint8_t *a1 = (const uint8_t *)arcs;
620 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000621 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000622 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000623 /* Increase the first_value by arc0 */
624 arc0 *= 40; /* (0..80) */
625 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
626 unsigned int v = *tp;
627 v += arc0;
628 *tp = v;
629 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
630 else break;
631 }
632
633 assert(tp >= first_value);
634
635 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
636 fv - first_value, 1);
637 }
638
639 /*
640 * Save the rest of arcs.
641 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000642 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000643 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000644 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000645 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
646 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000647 }
648
Lev Walkin34b2a932004-06-17 23:46:45 +0000649 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000650
651 /*
652 * Replace buffer.
653 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000654 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000655 bp = oid->buf;
656 oid->buf = buf;
657 if(bp) FREEMEM(bp);
658
659 return 0;
660}
Lev Walkinc4c61962004-06-14 08:17:27 +0000661
Lev Walkin92302252004-10-23 10:16:51 +0000662
663int
664OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000665 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000666 unsigned int arcs_count = 0;
667 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000668 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700669 ST_LEADSPACE,
670 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700671 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800672 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700673 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000674
675 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000676 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000677 errno = EINVAL;
678 return -1;
679 }
680
681 if(oid_txt_length == -1)
682 oid_txt_length = strlen(oid_text);
683
Lev Walkincad560a2013-03-16 07:00:58 -0700684#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
685 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700686 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700687 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700688 case ASN_STRTOX_EXTRA_DATA: \
689 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700690 if(arcs_count < arcs_slots) \
691 arcs[arcs_count] = value; \
692 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700693 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700694 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700695 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700696 if(opt_oid_text_end) \
697 *opt_oid_text_end = oid_text; \
698 errno = ERANGE; \
699 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700700 case ASN_STRTOX_ERROR_INVAL: \
701 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700702 if(opt_oid_text_end) \
703 *opt_oid_text_end = oid_text; \
704 errno = EINVAL; \
705 return -1; \
706 } \
707 } while(0)
708
Lev Walkin92302252004-10-23 10:16:51 +0000709 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
710 switch(*oid_text) {
711 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700712 switch(state) {
713 case ST_LEADSPACE:
714 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000715 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700716 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700717 state = ST_TAILSPACE;
718 continue;
719 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700720 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000721 }
Lev Walkincad560a2013-03-16 07:00:58 -0700722 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700723 case 0x2e: /* '.' */
724 switch(state) {
725 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700726 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700727 case ST_WAITDIGITS:
728 if(opt_oid_text_end)
729 *opt_oid_text_end = oid_text;
730 errno = EINVAL; /* Broken OID */
731 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700732 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700733 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700734 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000735 continue;
736 }
Lev Walkinf5927112012-09-03 00:48:45 -0700737 break;
738 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
739 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
740 switch(state) {
741 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700742 case ST_AFTERVALUE:
743 if(opt_oid_text_end)
744 *opt_oid_text_end = oid_text;
745 errno = EINVAL; /* "1. 1" => broken OID */
746 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700747 case ST_LEADSPACE:
748 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700749 _OID_CAPTURE_ARC(oid_text, oid_end);
750 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700751 continue;
752 }
753 break;
Lev Walkin92302252004-10-23 10:16:51 +0000754 default:
755 /* Unexpected symbols */
756 state = ST_WAITDIGITS;
757 break;
758 } /* switch() */
759 break;
760 } /* for() */
761
762
Lev Walkin093ba2e2005-04-04 21:10:06 +0000763 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000764
765 /* Finalize last arc */
766 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700767 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700768 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700769 case ST_WAITDIGITS:
770 errno = EINVAL; /* Broken OID */
771 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700772 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700773 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000774 return arcs_count;
775 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700776
777 errno = EINVAL; /* Broken OID */
778 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000779}
780
781