blob: 7110b0bde70e9be45a9fce6cac808fbfbd874a68 [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 Walkinf15320b2004-06-03 03:38:44 +000022 OBJECT_IDENTIFIER_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
24 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000025 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 OBJECT_IDENTIFIER_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070027#ifdef ASN_DISABLE_OER_SUPPORT
28 0,
29 0,
30#else
31 0,
32 0,
33#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040034#ifdef ASN_DISABLE_PER_SUPPORT
35 0,
36 0,
37#else
38 OCTET_STRING_decode_uper,
39 OCTET_STRING_encode_uper,
40#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080041 0 /* Use generic outmost tag fetcher */
42};
43asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
44 "OBJECT IDENTIFIER",
45 "OBJECT_IDENTIFIER",
46 &asn_OP_OBJECT_IDENTIFIER,
47 OBJECT_IDENTIFIER_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000048 asn_DEF_OBJECT_IDENTIFIER_tags,
49 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
50 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
51 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
52 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
53 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070054 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000055 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000056 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000057 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000058};
59
60
Lev Walkinf15320b2004-06-03 03:38:44 +000061int
Lev Walkin5e033762004-09-29 13:26:15 +000062OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000063 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000064 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000065
66 if(st && st->buf) {
67 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070068 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000069 "%s: at least one numerical value "
70 "expected (%s:%d)",
71 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000072 return -1;
73 }
74 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070075 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000076 "%s: value not given (%s:%d)",
77 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000078 return -1;
79 }
80
81 return 0;
82}
83
Lev Walkinc4c61962004-06-14 08:17:27 +000084
Lev Walkinf15320b2004-06-03 03:38:44 +000085int
Wim Lewis14e6b162014-07-23 16:06:01 -070086OBJECT_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 +000087 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070088 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000089 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000090 unsigned char *rvbuf = (unsigned char *)rvbufp;
91 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000092 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000093
Lev Walkin29a044b2004-06-14 07:24:36 +000094 rvsize *= CHAR_BIT; /* bytes to bits */
95 arclen *= 7; /* bytes to bits */
96
97 /*
98 * The arc has the number of bits
99 * cannot be represented using supplied return value type.
100 */
101 if(arclen > rvsize) {
102 if(arclen > (rvsize + CHAR_BIT)) {
103 errno = ERANGE; /* Overflow */
104 return -1;
105 } else {
106 /*
107 * Even if the number of bits in the arc representation
108 * is higher than the width of supplied * return value
109 * type, there is still possible to fit it when there
110 * are few unused high bits in the arc value
111 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000112 *
113 * Moreover, there is a possibility that the
114 * number could actually fit the arc space, given
115 * that add is negative, but we don't handle
116 * such "temporary lack of precision" situation here.
117 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000118 */
119 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
120 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 errno = ERANGE; /* Overflow */
122 return -1;
123 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000124 /* Fool the routine computing unused bits */
125 arclen -= 7;
126 cache = *arcbuf & 0x7f;
127 arcbuf++;
128 }
129 }
130
Lev Walkinc4c61962004-06-14 08:17:27 +0000131 /* Faster path for common size */
132 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
133 unsigned long accum;
134 /* Gather all bits into the accumulator */
135 for(accum = cache; arcbuf < arcend; arcbuf++)
136 accum = (accum << 7) | (*arcbuf & ~0x80);
137 if(accum < (unsigned)-add) {
138 errno = ERANGE; /* Overflow */
139 return -1;
140 }
Lev Walkin70732a22013-12-07 10:55:14 -0800141 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000142 return 0;
143 }
144
Lev Walkin29a044b2004-06-14 07:24:36 +0000145#ifndef WORDS_BIGENDIAN
146 if(*(unsigned char *)&LE) { /* Little endian (x86) */
147 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000148 rvbuf += rvsize / CHAR_BIT - 1;
149 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000150 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000151 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000152#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000153 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000154
Lev Walkinc4c61962004-06-14 08:17:27 +0000155 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000156 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000157
Lev Walkin29a044b2004-06-14 07:24:36 +0000158 /* Clear the high unused bits */
159 for(bits = rvsize - arclen;
160 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000161 rvbuf += inc, bits -= CHAR_BIT)
162 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000163
Lev Walkin29a044b2004-06-14 07:24:36 +0000164 /* Fill the body of a value */
165 for(; arcbuf < arcend; arcbuf++) {
166 cache = (cache << 7) | (*arcbuf & 0x7f);
167 bits += 7;
168 if(bits >= CHAR_BIT) {
169 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000170 *rvbuf = (cache >> bits);
171 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000172 }
173 }
174 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000175 *rvbuf = cache;
176 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000177 }
178 }
179
180 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000181 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
182 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700183 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000184 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000185 add = -1;
186 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000187 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000188 break;
189 }
190 }
191 if(rvbuf == rvstart) {
192 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 errno = ERANGE; /* Overflow */
194 return -1;
195 }
196 }
197
Lev Walkinf15320b2004-06-03 03:38:44 +0000198 return 0;
199}
200
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700202OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 asn_app_consume_bytes_f *cb, void *app_key) {
204 char scratch[64]; /* Conservative estimate */
205 unsigned long accum; /* Bits accumulator */
206 char *p; /* Position in the scratch buffer */
207
Lev Walkin29a044b2004-06-14 07:24:36 +0000208 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
209 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 return -1;
211
Lev Walkin3251b8e2004-08-23 09:23:02 +0000212 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213 ssize_t len;
214
Lev Walkin3251b8e2004-08-23 09:23:02 +0000215 /* Fill the scratch buffer in reverse. */
216 p = scratch + sizeof(scratch);
217 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000218 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000219
Lev Walkina9cc46e2004-09-22 16:06:28 +0000220 len = sizeof(scratch) - (p - scratch);
221 if(cb(p, len, app_key) < 0)
222 return -1;
223 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000224 } else {
225 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000226 if(cb(scratch, 1, app_key) < 0)
227 return -1;
228 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000229 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000230}
231
232int
Wim Lewis14e6b162014-07-23 16:06:01 -0700233OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000234 asn_app_consume_bytes_f *cb, void *app_key) {
235
236 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
237 return -1;
238
239 return 0;
240}
241
242static ssize_t
243OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
244 ssize_t wrote_len = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700245 size_t startn;
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700247 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000248
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 for(i = 0, startn = 0; i < st->size; i++) {
250 uint8_t b = st->buf[i];
251 if((b & 0x80)) /* Continuation expected */
252 continue;
253
254 if(startn == 0) {
255 /*
256 * First two arcs are encoded through the backdoor.
257 */
258 if(i) {
259 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000260 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000261 } else if(b <= 39) {
262 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000263 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000264 } else if(b < 79) {
265 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000266 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000267 } else {
268 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000269 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000270 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000271 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000272 }
273
Lev Walkina9cc46e2004-09-22 16:06:28 +0000274 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000275 return -1;
276
Lev Walkina9cc46e2004-09-22 16:06:28 +0000277 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
278 i - startn + 1, add, cb, app_key);
279 if(add < 0) return -1;
280 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000281 startn = i + 1;
282 add = 0;
283 }
284
Lev Walkina9cc46e2004-09-22 16:06:28 +0000285 return wrote_len;
286}
287
Lev Walkin0fab1a62005-03-09 22:19:25 +0000288static enum xer_pbd_rval
289OBJECT_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 +0000290 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000291 const char *chunk_end = (const char *)chunk_buf + chunk_size;
292 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000293 long s_arcs[10];
294 long *arcs = s_arcs;
295 int arcs_count;
296 int ret;
297
Lev Walkine0b56e02005-02-25 12:10:27 +0000298 (void)td;
299
Lev Walkin92302252004-10-23 10:16:51 +0000300 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000301 (const char *)chunk_buf, chunk_size, arcs,
302 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700303 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000304 /* Expecting more than zero arcs */
305 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700306 } else if(arcs_count == 0) {
307 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000308 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700309 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000310
311 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000312 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000313 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000314 ret = OBJECT_IDENTIFIER_parse_arcs(
315 (const char *)chunk_buf, chunk_size,
316 arcs, arcs_count, &endptr);
317 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000318 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000319 }
320
321 /*
322 * Convert arcs into BER representation.
323 */
324 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000325 if(arcs != s_arcs) FREEMEM(arcs);
326
Lev Walkin2c34aad2005-03-10 11:50:24 +0000327 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000328}
329
330asn_dec_rval_t
331OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
332 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000333 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000334
335 return xer_decode_primitive(opt_codec_ctx, td,
336 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
337 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
338}
339
Lev Walkina9cc46e2004-09-22 16:06:28 +0000340asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000341OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000342 int ilevel, enum xer_encoder_flags_e flags,
343 asn_app_consume_bytes_f *cb, void *app_key) {
344 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
345 asn_enc_rval_t er;
346
347 (void)ilevel;
348 (void)flags;
349
350 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700351 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000352
353 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700354 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000355
Lev Walkin7c1dc052016-03-14 03:08:15 -0700356 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000357}
358
359int
Lev Walkin5e033762004-09-29 13:26:15 +0000360OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000361 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
362 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
363
364 (void)td; /* Unused argument */
365 (void)ilevel; /* Unused argument */
366
367 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000368 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000369
370 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000371 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000372 return -1;
373
374 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
375 return -1;
376
Lev Walkin8e8078a2004-09-26 13:10:40 +0000377 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000378}
379
380int
Wim Lewis14e6b162014-07-23 16:06:01 -0700381OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000382 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000383 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000384 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000385 int startn = 0;
386 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700387 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000388
Lev Walkin0787ff02004-06-17 23:43:39 +0000389 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000390 errno = EINVAL;
391 return -1;
392 }
393
394 for(i = 0; i < oid->size; i++) {
395 uint8_t b = oid->buf[i];
396 if((b & 0x80)) /* Continuation expected */
397 continue;
398
Lev Walkin29a044b2004-06-14 07:24:36 +0000399 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 /*
401 * First two arcs are encoded through the backdoor.
402 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000403 unsigned LE = 1; /* Little endian */
404 int first_arc;
405 num_arcs++;
406 if(!arc_slots) { num_arcs++; continue; }
407
408 if(i) first_arc = 2;
409 else if(b <= 39) first_arc = 0;
410 else if(b < 79) first_arc = 1;
411 else first_arc = 2;
412
413 add = -40 * first_arc;
414 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000415 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000416 + ((*(char *)&LE)?0:(arc_type_size - 1)))
417 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000418 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 }
420
Lev Walkin29a044b2004-06-14 07:24:36 +0000421 /* Decode, if has space */
422 if(arcs < arcs_end) {
423 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
424 i - startn + 1, add,
425 arcs, arc_type_size))
426 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000428 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000429 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000431 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 }
433
Lev Walkin29a044b2004-06-14 07:24:36 +0000434 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000435}
436
Lev Walkin0787ff02004-06-17 23:43:39 +0000437
438/*
439 * Save the single value as an object identifier arc.
440 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000441int
Lev Walkinb1a15552005-12-22 21:39:44 +0000442OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000443 /*
444 * The following conditions must hold:
445 * assert(arcval);
446 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000447 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000448 * assert(arcbuf);
449 */
450#ifdef WORDS_BIGENDIAN
451 const unsigned isLittleEndian = 0;
452#else
453 unsigned LE = 1;
454 unsigned isLittleEndian = *(char *)&LE;
455#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000456 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000457 unsigned int cache;
458 uint8_t *bp = arcbuf;
459 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000460 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000461
462 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000463 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
464 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000465 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000466 uint8_t *tb;
467 for(tb = buffer; a >= aend; tb++, a--)
468 if((*tb = *a) && (tb < msb))
469 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000470 tend = &buffer[arcval_size];
471 tp = msb; /* Most significant non-zero byte */
472 } else {
473 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000474 tend = (const unsigned char *)arcval + arcval_size;
475 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000476 if(*tp) break;
477 }
478
479 /*
480 * Split the value in 7-bits chunks.
481 */
482 bits = ((tend - tp) * CHAR_BIT) % 7;
483 if(bits) {
484 cache = *tp >> (CHAR_BIT - bits);
485 if(cache) {
486 *bp++ = cache | 0x80;
487 cache = *tp++;
488 bits = CHAR_BIT - bits;
489 } else {
490 bits = -bits;
491 }
492 } else {
493 cache = 0;
494 }
495 for(; tp < tend; tp++) {
496 cache = (cache << CHAR_BIT) + *tp;
497 bits += CHAR_BIT;
498 while(bits >= 7) {
499 bits -= 7;
500 *bp++ = 0x80 | (cache >> bits);
501 }
502 }
503 if(bits) *bp++ = cache;
504 bp[-1] &= 0x7f; /* Clear the last bit */
505
506 return bp - arcbuf;
507}
508
Lev Walkinf15320b2004-06-03 03:38:44 +0000509int
Lev Walkinb1a15552005-12-22 21:39:44 +0000510OBJECT_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 +0000511 uint8_t *buf;
512 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000513 unsigned LE = 1; /* Little endian (x86) */
514 unsigned isLittleEndian = *((char *)&LE);
515 unsigned int arc0;
516 unsigned int arc1;
517 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000518 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000519
Lev Walkin523de9e2006-08-18 01:34:18 +0000520 if(!oid || !arcs || arc_type_size < 1
521 || arc_type_size > 16
522 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 errno = EINVAL;
524 return -1;
525 }
526
Lev Walkin0787ff02004-06-17 23:43:39 +0000527 switch(arc_type_size) {
528 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000529 arc0 = ((const unsigned char *)arcs)[0];
530 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000531 break;
532 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000533 arc0 = ((const unsigned short *)arcs)[0];
534 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000535 break;
536 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000537 arc0 = ((const unsigned int *)arcs)[0];
538 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000539 break;
540 default:
541 arc1 = arc0 = 0;
542 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000543 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000544 /* If more significant bytes are present,
545 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000546 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000547 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000548 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
549 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000550 arc0 = *((const unsigned char *)arcs + 0);
551 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000552 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000553 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000554 /* If more significant bytes are present,
555 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000556 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000557 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000558 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
559 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000560 }
561 }
562
563 /*
564 * The previous chapter left us with the first and the second arcs.
565 * The values are not precise (that is, they are valid only if
566 * they're less than 255), but OK for the purposes of making
567 * the sanity test below.
568 */
569 if(arc0 <= 1) {
570 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 /* 8.19.4: At most 39 subsequent values (including 0) */
572 errno = ERANGE;
573 return -1;
574 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000575 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000576 /* 8.19.4: Only three values are allocated from the root node */
577 errno = ERANGE;
578 return -1;
579 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000580 /*
581 * After above tests it is known that the value of arc0 is completely
582 * trustworthy (0..2). However, the arc1's value is still meaningless.
583 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000584
585 /*
586 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000587 * This estimation implicitly takes in account the following facts,
588 * that cancel each other:
589 * * the first two arcs are encoded in a single value.
590 * * the first value may require more space (+1 byte)
591 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000592 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000593 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000594 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000595 if(!buf) {
596 /* ENOMEM */
597 return -1;
598 }
599
600 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000601 * Encode the first two arcs.
602 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000603 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000605 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000606 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000607 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000608
Lev Walkin0787ff02004-06-17 23:43:39 +0000609 /*
610 * Simulate first_value = arc0 * 40 + arc1;
611 */
612 /* Copy the second (1'st) arcs[1] into the first_value */
613 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000614 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000615 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000616 const uint8_t *aend = (const unsigned char *)arcs - 1;
617 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000618 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000619 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000620 const uint8_t *a1 = (const uint8_t *)arcs;
621 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000622 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000623 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000624 /* Increase the first_value by arc0 */
625 arc0 *= 40; /* (0..80) */
626 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
627 unsigned int v = *tp;
628 v += arc0;
629 *tp = v;
630 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
631 else break;
632 }
633
634 assert(tp >= first_value);
635
636 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
637 fv - first_value, 1);
638 }
639
640 /*
641 * Save the rest of arcs.
642 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000643 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000644 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000645 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000646 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
647 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000648 }
649
Lev Walkin34b2a932004-06-17 23:46:45 +0000650 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000651
652 /*
653 * Replace buffer.
654 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000655 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000656 bp = oid->buf;
657 oid->buf = buf;
658 if(bp) FREEMEM(bp);
659
660 return 0;
661}
Lev Walkinc4c61962004-06-14 08:17:27 +0000662
Lev Walkin92302252004-10-23 10:16:51 +0000663
664int
665OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000666 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000667 unsigned int arcs_count = 0;
668 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000669 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700670 ST_LEADSPACE,
671 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700672 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800673 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700674 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000675
676 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000677 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000678 errno = EINVAL;
679 return -1;
680 }
681
682 if(oid_txt_length == -1)
683 oid_txt_length = strlen(oid_text);
684
Lev Walkincad560a2013-03-16 07:00:58 -0700685#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
686 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700687 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700688 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700689 case ASN_STRTOX_EXTRA_DATA: \
690 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700691 if(arcs_count < arcs_slots) \
692 arcs[arcs_count] = value; \
693 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700694 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700695 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700696 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700697 if(opt_oid_text_end) \
698 *opt_oid_text_end = oid_text; \
699 errno = ERANGE; \
700 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700701 case ASN_STRTOX_ERROR_INVAL: \
702 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700703 if(opt_oid_text_end) \
704 *opt_oid_text_end = oid_text; \
705 errno = EINVAL; \
706 return -1; \
707 } \
708 } while(0)
709
Lev Walkin92302252004-10-23 10:16:51 +0000710 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
711 switch(*oid_text) {
712 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700713 switch(state) {
714 case ST_LEADSPACE:
715 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000716 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700717 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700718 state = ST_TAILSPACE;
719 continue;
720 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700721 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000722 }
Lev Walkincad560a2013-03-16 07:00:58 -0700723 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700724 case 0x2e: /* '.' */
725 switch(state) {
726 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700727 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700728 case ST_WAITDIGITS:
729 if(opt_oid_text_end)
730 *opt_oid_text_end = oid_text;
731 errno = EINVAL; /* Broken OID */
732 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700733 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700734 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700735 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000736 continue;
737 }
Lev Walkinf5927112012-09-03 00:48:45 -0700738 break;
739 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
740 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
741 switch(state) {
742 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700743 case ST_AFTERVALUE:
744 if(opt_oid_text_end)
745 *opt_oid_text_end = oid_text;
746 errno = EINVAL; /* "1. 1" => broken OID */
747 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700748 case ST_LEADSPACE:
749 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700750 _OID_CAPTURE_ARC(oid_text, oid_end);
751 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700752 continue;
753 }
754 break;
Lev Walkin92302252004-10-23 10:16:51 +0000755 default:
756 /* Unexpected symbols */
757 state = ST_WAITDIGITS;
758 break;
759 } /* switch() */
760 break;
761 } /* for() */
762
763
Lev Walkin093ba2e2005-04-04 21:10:06 +0000764 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000765
766 /* Finalize last arc */
767 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700768 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700769 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700770 case ST_WAITDIGITS:
771 errno = EINVAL; /* Broken OID */
772 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700773 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700774 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000775 return arcs_count;
776 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700777
778 errno = EINVAL; /* Broken OID */
779 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000780}
781
782