blob: e7478a03005d08e8a8c9a94bdc114ef7c61c8acf [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 */
Lev Walkina5972be2017-09-29 23:15:58 -070040 OBJECT_IDENTIFIER_random_fill,
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,
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 Walkina5972be2017-09-29 23:15:58 -070053 { 0, 0, OBJECT_IDENTIFIER_constraint },
Lev Walkin449f8322004-08-20 13:23:42 +000054 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000055 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000056};
57
58
Lev Walkinf15320b2004-06-03 03:38:44 +000059int
Lev Walkin5e033762004-09-29 13:26:15 +000060OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000061 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000062 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000063
64 if(st && st->buf) {
65 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070066 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000067 "%s: at least one numerical value "
68 "expected (%s:%d)",
69 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000070 return -1;
71 }
72 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070073 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000074 "%s: value not given (%s:%d)",
75 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000076 return -1;
77 }
78
79 return 0;
80}
81
82int
Wim Lewis14e6b162014-07-23 16:06:01 -070083OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
Lev Walkin3f995632017-09-26 18:27:32 -070084 unsigned LE CC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070085 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000086 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000087 unsigned char *rvbuf = (unsigned char *)rvbufp;
88 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000089 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000090
Lev Walkin29a044b2004-06-14 07:24:36 +000091 rvsize *= CHAR_BIT; /* bytes to bits */
92 arclen *= 7; /* bytes to bits */
93
Lev Walkin0995f352017-09-17 23:16:38 -070094 assert(add <= 0);
95
Lev Walkin29a044b2004-06-14 07:24:36 +000096 /*
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);
Lev Walkin0995f352017-09-17 23:16:38 -0700136 if(accum < (unsigned)-add
137 || accum > (ULONG_MAX-(unsigned long)(-add))) {
Lev Walkinc4c61962004-06-14 08:17:27 +0000138 errno = ERANGE; /* Overflow */
139 return -1;
140 }
Lev Walkin0995f352017-09-17 23:16:38 -0700141 *(unsigned long *)(void *)rvbuf =
142 accum - (unsigned long)(-add); /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000143 return 0;
144 }
145
Lev Walkin29a044b2004-06-14 07:24:36 +0000146#ifndef WORDS_BIGENDIAN
147 if(*(unsigned char *)&LE) { /* Little endian (x86) */
148 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000149 rvbuf += rvsize / CHAR_BIT - 1;
150 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000151 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000152 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000153#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000154 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000155
Lev Walkinc4c61962004-06-14 08:17:27 +0000156 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000157 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000158
Lev Walkin29a044b2004-06-14 07:24:36 +0000159 /* Clear the high unused bits */
160 for(bits = rvsize - arclen;
161 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000162 rvbuf += inc, bits -= CHAR_BIT)
163 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000164
Lev Walkin29a044b2004-06-14 07:24:36 +0000165 /* Fill the body of a value */
166 for(; arcbuf < arcend; arcbuf++) {
167 cache = (cache << 7) | (*arcbuf & 0x7f);
168 bits += 7;
169 if(bits >= CHAR_BIT) {
170 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000171 *rvbuf = (cache >> bits);
172 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000173 }
174 }
175 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000176 *rvbuf = cache;
177 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000178 }
179 }
180
181 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000182 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
183 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700184 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000185 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000186 add = -1;
187 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000188 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000189 break;
190 }
191 }
192 if(rvbuf == rvstart) {
193 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 errno = ERANGE; /* Overflow */
195 return -1;
196 }
197 }
198
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 return 0;
200}
201
Lev Walkina9cc46e2004-09-22 16:06:28 +0000202ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700203OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 asn_app_consume_bytes_f *cb, void *app_key) {
205 char scratch[64]; /* Conservative estimate */
206 unsigned long accum; /* Bits accumulator */
207 char *p; /* Position in the scratch buffer */
208
Lev Walkin29a044b2004-06-14 07:24:36 +0000209 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
210 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 return -1;
212
Lev Walkin3251b8e2004-08-23 09:23:02 +0000213 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214 ssize_t len;
215
Lev Walkin3251b8e2004-08-23 09:23:02 +0000216 /* Fill the scratch buffer in reverse. */
217 p = scratch + sizeof(scratch);
218 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000220
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221 len = sizeof(scratch) - (p - scratch);
222 if(cb(p, len, app_key) < 0)
223 return -1;
224 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000225 } else {
226 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000227 if(cb(scratch, 1, app_key) < 0)
228 return -1;
229 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000230 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000231}
232
233int
Wim Lewis14e6b162014-07-23 16:06:01 -0700234OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000235 asn_app_consume_bytes_f *cb, void *app_key) {
236
237 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
238 return -1;
239
240 return 0;
241}
242
243static ssize_t
244OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
245 ssize_t wrote_len = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700246 size_t startn;
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700248 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000249
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 for(i = 0, startn = 0; i < st->size; i++) {
251 uint8_t b = st->buf[i];
252 if((b & 0x80)) /* Continuation expected */
253 continue;
254
255 if(startn == 0) {
256 /*
257 * First two arcs are encoded through the backdoor.
258 */
259 if(i) {
260 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000261 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 } else if(b <= 39) {
263 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000264 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 } else if(b < 79) {
266 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000267 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000268 } else {
269 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000270 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000272 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000273 }
274
Lev Walkina9cc46e2004-09-22 16:06:28 +0000275 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000276 return -1;
277
Lev Walkina9cc46e2004-09-22 16:06:28 +0000278 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
279 i - startn + 1, add, cb, app_key);
280 if(add < 0) return -1;
281 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000282 startn = i + 1;
283 add = 0;
284 }
285
Lev Walkina9cc46e2004-09-22 16:06:28 +0000286 return wrote_len;
287}
288
Lev Walkin0fab1a62005-03-09 22:19:25 +0000289static enum xer_pbd_rval
290OBJECT_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 +0000291 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000292 const char *chunk_end = (const char *)chunk_buf + chunk_size;
293 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000294 long s_arcs[10];
295 long *arcs = s_arcs;
296 int arcs_count;
297 int ret;
298
Lev Walkine0b56e02005-02-25 12:10:27 +0000299 (void)td;
300
Lev Walkin92302252004-10-23 10:16:51 +0000301 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000302 (const char *)chunk_buf, chunk_size, arcs,
303 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700304 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000305 /* Expecting more than zero arcs */
306 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700307 } else if(arcs_count == 0) {
308 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000309 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700310 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000311
312 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000313 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000314 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000315 ret = OBJECT_IDENTIFIER_parse_arcs(
316 (const char *)chunk_buf, chunk_size,
317 arcs, arcs_count, &endptr);
318 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000319 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000320 }
321
322 /*
323 * Convert arcs into BER representation.
324 */
325 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000326 if(arcs != s_arcs) FREEMEM(arcs);
327
Lev Walkin2c34aad2005-03-10 11:50:24 +0000328 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000329}
330
331asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700332OBJECT_IDENTIFIER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin92302252004-10-23 10:16:51 +0000333 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000334 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000335
336 return xer_decode_primitive(opt_codec_ctx, td,
337 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
338 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
339}
340
Lev Walkina9cc46e2004-09-22 16:06:28 +0000341asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000342OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000343 int ilevel, enum xer_encoder_flags_e flags,
344 asn_app_consume_bytes_f *cb, void *app_key) {
345 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
346 asn_enc_rval_t er;
347
348 (void)ilevel;
349 (void)flags;
350
351 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700352 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000353
354 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700355 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356
Lev Walkin7c1dc052016-03-14 03:08:15 -0700357 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000358}
359
360int
Lev Walkin5e033762004-09-29 13:26:15 +0000361OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000362 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
363 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
364
365 (void)td; /* Unused argument */
366 (void)ilevel; /* Unused argument */
367
368 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000369 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000370
371 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000372 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000373 return -1;
374
375 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
376 return -1;
377
Lev Walkin8e8078a2004-09-26 13:10:40 +0000378 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000379}
380
381int
Wim Lewis14e6b162014-07-23 16:06:01 -0700382OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000383 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000384 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000385 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 int startn = 0;
387 int add = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700388 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000389
Lev Walkin0787ff02004-06-17 23:43:39 +0000390 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000391 errno = EINVAL;
392 return -1;
393 }
394
395 for(i = 0; i < oid->size; i++) {
396 uint8_t b = oid->buf[i];
397 if((b & 0x80)) /* Continuation expected */
398 continue;
399
Lev Walkin29a044b2004-06-14 07:24:36 +0000400 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 /*
402 * First two arcs are encoded through the backdoor.
403 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000404 unsigned LE = 1; /* Little endian */
405 int first_arc;
406 num_arcs++;
407 if(!arc_slots) { num_arcs++; continue; }
408
409 if(i) first_arc = 2;
410 else if(b <= 39) first_arc = 0;
411 else if(b < 79) first_arc = 1;
412 else first_arc = 2;
413
414 add = -40 * first_arc;
415 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000416 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000417 + ((*(char *)&LE)?0:(arc_type_size - 1)))
418 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000419 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000420 }
421
Lev Walkin29a044b2004-06-14 07:24:36 +0000422 /* Decode, if has space */
423 if(arcs < arcs_end) {
424 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
425 i - startn + 1, add,
426 arcs, arc_type_size))
427 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000429 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000430 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000432 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 }
434
Lev Walkin29a044b2004-06-14 07:24:36 +0000435 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000436}
437
Lev Walkin0787ff02004-06-17 23:43:39 +0000438
439/*
440 * Save the single value as an object identifier arc.
441 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000442int
Lev Walkinb1a15552005-12-22 21:39:44 +0000443OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000444 /*
445 * The following conditions must hold:
446 * assert(arcval);
447 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000448 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000449 * assert(arcbuf);
450 */
451#ifdef WORDS_BIGENDIAN
452 const unsigned isLittleEndian = 0;
453#else
454 unsigned LE = 1;
455 unsigned isLittleEndian = *(char *)&LE;
456#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000457 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000458 unsigned int cache;
459 uint8_t *bp = arcbuf;
460 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000461 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000462
463 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000464 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
465 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000466 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000467 uint8_t *tb;
468 for(tb = buffer; a >= aend; tb++, a--)
469 if((*tb = *a) && (tb < msb))
470 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000471 tend = &buffer[arcval_size];
472 tp = msb; /* Most significant non-zero byte */
473 } else {
474 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000475 tend = (const unsigned char *)arcval + arcval_size;
476 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000477 if(*tp) break;
478 }
479
480 /*
481 * Split the value in 7-bits chunks.
482 */
483 bits = ((tend - tp) * CHAR_BIT) % 7;
484 if(bits) {
485 cache = *tp >> (CHAR_BIT - bits);
486 if(cache) {
487 *bp++ = cache | 0x80;
488 cache = *tp++;
489 bits = CHAR_BIT - bits;
490 } else {
491 bits = -bits;
492 }
493 } else {
494 cache = 0;
495 }
496 for(; tp < tend; tp++) {
497 cache = (cache << CHAR_BIT) + *tp;
498 bits += CHAR_BIT;
499 while(bits >= 7) {
500 bits -= 7;
501 *bp++ = 0x80 | (cache >> bits);
502 }
503 }
504 if(bits) *bp++ = cache;
505 bp[-1] &= 0x7f; /* Clear the last bit */
506
507 return bp - arcbuf;
508}
509
Lev Walkinf15320b2004-06-03 03:38:44 +0000510int
Lev Walkinb1a15552005-12-22 21:39:44 +0000511OBJECT_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 +0000512 uint8_t *buf;
513 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000514 unsigned LE = 1; /* Little endian (x86) */
515 unsigned isLittleEndian = *((char *)&LE);
516 unsigned int arc0;
517 unsigned int arc1;
518 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000519 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000520
Lev Walkin523de9e2006-08-18 01:34:18 +0000521 if(!oid || !arcs || arc_type_size < 1
522 || arc_type_size > 16
523 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000524 errno = EINVAL;
525 return -1;
526 }
527
Lev Walkin0787ff02004-06-17 23:43:39 +0000528 switch(arc_type_size) {
529 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000530 arc0 = ((const unsigned char *)arcs)[0];
531 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000532 break;
533 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000534 arc0 = ((const unsigned short *)arcs)[0];
535 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000536 break;
537 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000538 arc0 = ((const unsigned int *)arcs)[0];
539 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000540 break;
541 default:
542 arc1 = arc0 = 0;
543 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000544 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000545 /* If more significant bytes are present,
546 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000547 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000548 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000549 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
550 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000551 arc0 = *((const unsigned char *)arcs + 0);
552 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000553 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000554 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000555 /* If more significant bytes are present,
556 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000557 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000558 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000559 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
560 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000561 }
562 }
563
564 /*
565 * The previous chapter left us with the first and the second arcs.
566 * The values are not precise (that is, they are valid only if
567 * they're less than 255), but OK for the purposes of making
568 * the sanity test below.
569 */
570 if(arc0 <= 1) {
571 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 /* 8.19.4: At most 39 subsequent values (including 0) */
573 errno = ERANGE;
574 return -1;
575 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000576 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 /* 8.19.4: Only three values are allocated from the root node */
578 errno = ERANGE;
579 return -1;
580 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000581 /*
582 * After above tests it is known that the value of arc0 is completely
583 * trustworthy (0..2). However, the arc1's value is still meaningless.
584 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000585
586 /*
587 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000588 * This estimation implicitly takes in account the following facts,
589 * that cancel each other:
590 * * the first two arcs are encoded in a single value.
591 * * the first value may require more space (+1 byte)
592 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000594 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000595 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000596 if(!buf) {
597 /* ENOMEM */
598 return -1;
599 }
600
601 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000602 * Encode the first two arcs.
603 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000604 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000605 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000606 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000607 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000608 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000609
Lev Walkin0787ff02004-06-17 23:43:39 +0000610 /*
611 * Simulate first_value = arc0 * 40 + arc1;
612 */
613 /* Copy the second (1'st) arcs[1] into the first_value */
614 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000615 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000616 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000617 const uint8_t *aend = (const unsigned char *)arcs - 1;
618 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000619 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000620 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000621 const uint8_t *a1 = (const uint8_t *)arcs;
622 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000623 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000624 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000625 /* Increase the first_value by arc0 */
626 arc0 *= 40; /* (0..80) */
627 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
628 unsigned int v = *tp;
629 v += arc0;
630 *tp = v;
631 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
632 else break;
633 }
634
635 assert(tp >= first_value);
636
637 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
638 fv - first_value, 1);
639 }
640
641 /*
642 * Save the rest of arcs.
643 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000644 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000645 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000646 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000647 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
648 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000649 }
650
Lev Walkin34b2a932004-06-17 23:46:45 +0000651 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000652
653 /*
654 * Replace buffer.
655 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000656 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000657 bp = oid->buf;
658 oid->buf = buf;
659 if(bp) FREEMEM(bp);
660
661 return 0;
662}
Lev Walkinc4c61962004-06-14 08:17:27 +0000663
Lev Walkin92302252004-10-23 10:16:51 +0000664
665int
666OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000667 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000668 unsigned int arcs_count = 0;
669 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000670 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700671 ST_LEADSPACE,
672 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700673 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800674 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700675 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000676
677 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000678 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000679 errno = EINVAL;
680 return -1;
681 }
682
683 if(oid_txt_length == -1)
684 oid_txt_length = strlen(oid_text);
685
Lev Walkincad560a2013-03-16 07:00:58 -0700686#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
687 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700688 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700689 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
Lev Walkin72ec9092017-07-05 05:49:12 -0700690 case ASN_STRTOX_EXTRA_DATA: \
691 case ASN_STRTOX_OK: \
Lev Walkinf5927112012-09-03 00:48:45 -0700692 if(arcs_count < arcs_slots) \
693 arcs[arcs_count] = value; \
694 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700695 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700696 break; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700697 case ASN_STRTOX_ERROR_RANGE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700698 if(opt_oid_text_end) \
699 *opt_oid_text_end = oid_text; \
700 errno = ERANGE; \
701 return -1; \
Lev Walkin72ec9092017-07-05 05:49:12 -0700702 case ASN_STRTOX_ERROR_INVAL: \
703 case ASN_STRTOX_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700704 if(opt_oid_text_end) \
705 *opt_oid_text_end = oid_text; \
706 errno = EINVAL; \
707 return -1; \
708 } \
709 } while(0)
710
Lev Walkin92302252004-10-23 10:16:51 +0000711 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
712 switch(*oid_text) {
713 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700714 switch(state) {
715 case ST_LEADSPACE:
716 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000717 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700718 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700719 state = ST_TAILSPACE;
720 continue;
721 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700722 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000723 }
Lev Walkincad560a2013-03-16 07:00:58 -0700724 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700725 case 0x2e: /* '.' */
726 switch(state) {
727 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700728 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700729 case ST_WAITDIGITS:
730 if(opt_oid_text_end)
731 *opt_oid_text_end = oid_text;
732 errno = EINVAL; /* Broken OID */
733 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700734 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700735 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700736 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000737 continue;
738 }
Lev Walkinf5927112012-09-03 00:48:45 -0700739 break;
740 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
741 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
742 switch(state) {
743 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700744 case ST_AFTERVALUE:
745 if(opt_oid_text_end)
746 *opt_oid_text_end = oid_text;
747 errno = EINVAL; /* "1. 1" => broken OID */
748 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700749 case ST_LEADSPACE:
750 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700751 _OID_CAPTURE_ARC(oid_text, oid_end);
752 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700753 continue;
754 }
755 break;
Lev Walkin92302252004-10-23 10:16:51 +0000756 default:
757 /* Unexpected symbols */
758 state = ST_WAITDIGITS;
759 break;
760 } /* switch() */
761 break;
762 } /* for() */
763
764
Lev Walkin093ba2e2005-04-04 21:10:06 +0000765 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000766
767 /* Finalize last arc */
768 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700769 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700770 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700771 case ST_WAITDIGITS:
772 errno = EINVAL; /* Broken OID */
773 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700774 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700775 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000776 return arcs_count;
777 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700778
779 errno = EINVAL; /* Broken OID */
780 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000781}
782
Lev Walkina5972be2017-09-29 23:15:58 -0700783/*
784 * Generate values from the list of interesting values, or just a random
785 * value up to the upper limit.
786 */
787static uint32_t
788OBJECT_IDENTIFIER__biased_random_arc(int32_t upper_bound) {
789 static const uint16_t values[] = {0, 1, 127, 128, 129, 254, 255, 256};
Lev Walkin92302252004-10-23 10:16:51 +0000790
Lev Walkina5972be2017-09-29 23:15:58 -0700791 size_t idx = asn_random_between(0, 2 * sizeof(values)/sizeof(values[0]));
792 if(idx < sizeof(values) / sizeof(values[0])) {
793 if(values[idx] < upper_bound) {
794 return values[idx];
795 }
796 }
797
798 return asn_random_between(0, upper_bound);
799}
800
801asn_random_fill_result_t
802OBJECT_IDENTIFIER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
803 const asn_encoding_constraints_t *constraints,
804 size_t max_length) {
805 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
806 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
807 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
808 OBJECT_IDENTIFIER_t *st;
809 uint32_t arcs[5];
810 size_t arcs_len = asn_random_between(2, 5);
811 size_t i;
812
813 (void)constraints;
814
815 if(max_length < arcs_len) return result_skipped;
816
817 if(*sptr) {
818 st = *sptr;
819 } else {
820 st = CALLOC(1, sizeof(*st));
821 }
822
823 arcs[0] = asn_random_between(0, 2);
824 arcs[1] =
825 OBJECT_IDENTIFIER__biased_random_arc(arcs[0] <= 1 ? 39 : INT32_MAX);
826 for(i = 2; i < arcs_len; i++) {
827 arcs[i] = OBJECT_IDENTIFIER__biased_random_arc(INT32_MAX);
828 }
829
830 if(OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(arcs[0]), arcs_len)) {
831 if(st != *sptr) {
832 ASN_STRUCT_FREE(*td, st);
833 }
834 return result_failed;
835 }
836
837 *sptr = st;
838
839 return result_ok;
840}