blob: d0f8b31171213247e3551c608152c66ef4b39209 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf5927112012-09-03 00:48:45 -07006#include <INTEGER.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <OBJECT_IDENTIFIER.h>
Lev Walkin725883b2006-10-09 12:07:58 +00008#include <OCTET_STRING.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00009#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +000010#include <errno.h>
11
12/*
13 * OBJECT IDENTIFIER basic type description.
14 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070015static const ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
17};
Lev Walkin5e033762004-09-29 13:26:15 +000018asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000019 "OBJECT IDENTIFIER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000020 "OBJECT_IDENTIFIER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000021 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000022 OBJECT_IDENTIFIER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 OBJECT_IDENTIFIER_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000024 ber_decode_primitive,
25 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000026 OBJECT_IDENTIFIER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 OBJECT_IDENTIFIER_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +000028 OCTET_STRING_decode_uper,
29 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +000030 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000031 asn_DEF_OBJECT_IDENTIFIER_tags,
32 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
33 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
34 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
35 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
36 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000037 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000038 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000039 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000040};
41
42
Lev Walkinf15320b2004-06-03 03:38:44 +000043int
Lev Walkin5e033762004-09-29 13:26:15 +000044OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000045 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000046 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000047
48 if(st && st->buf) {
49 if(st->size < 1) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070050 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000051 "%s: at least one numerical value "
52 "expected (%s:%d)",
53 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000054 return -1;
55 }
56 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070057 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000058 "%s: value not given (%s:%d)",
59 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000060 return -1;
61 }
62
63 return 0;
64}
65
Lev Walkinc4c61962004-06-14 08:17:27 +000066
Lev Walkinf15320b2004-06-03 03:38:44 +000067int
Wim Lewis14e6b162014-07-23 16:06:01 -070068OBJECT_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 +000069 unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
Wim Lewis14e6b162014-07-23 16:06:01 -070070 const uint8_t *arcend = arcbuf + arclen; /* End of arc */
Lev Walkin29a044b2004-06-14 07:24:36 +000071 unsigned int cache = 0; /* No more than 14 significant bits */
Lev Walkin8e8078a2004-09-26 13:10:40 +000072 unsigned char *rvbuf = (unsigned char *)rvbufp;
73 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
Lev Walkin29a044b2004-06-14 07:24:36 +000074 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +000075
Lev Walkin29a044b2004-06-14 07:24:36 +000076 rvsize *= CHAR_BIT; /* bytes to bits */
77 arclen *= 7; /* bytes to bits */
78
79 /*
80 * The arc has the number of bits
81 * cannot be represented using supplied return value type.
82 */
83 if(arclen > rvsize) {
84 if(arclen > (rvsize + CHAR_BIT)) {
85 errno = ERANGE; /* Overflow */
86 return -1;
87 } else {
88 /*
89 * Even if the number of bits in the arc representation
90 * is higher than the width of supplied * return value
91 * type, there is still possible to fit it when there
92 * are few unused high bits in the arc value
93 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +000094 *
95 * Moreover, there is a possibility that the
96 * number could actually fit the arc space, given
97 * that add is negative, but we don't handle
98 * such "temporary lack of precision" situation here.
99 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000100 */
101 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
102 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 errno = ERANGE; /* Overflow */
104 return -1;
105 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000106 /* Fool the routine computing unused bits */
107 arclen -= 7;
108 cache = *arcbuf & 0x7f;
109 arcbuf++;
110 }
111 }
112
Lev Walkinc4c61962004-06-14 08:17:27 +0000113 /* Faster path for common size */
114 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
115 unsigned long accum;
116 /* Gather all bits into the accumulator */
117 for(accum = cache; arcbuf < arcend; arcbuf++)
118 accum = (accum << 7) | (*arcbuf & ~0x80);
119 if(accum < (unsigned)-add) {
120 errno = ERANGE; /* Overflow */
121 return -1;
122 }
Lev Walkin70732a22013-12-07 10:55:14 -0800123 *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
Lev Walkinc4c61962004-06-14 08:17:27 +0000124 return 0;
125 }
126
Lev Walkin29a044b2004-06-14 07:24:36 +0000127#ifndef WORDS_BIGENDIAN
128 if(*(unsigned char *)&LE) { /* Little endian (x86) */
129 /* "Convert" to big endian */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000130 rvbuf += rvsize / CHAR_BIT - 1;
131 rvstart--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000132 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000133 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000134#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000135 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000136
Lev Walkinc4c61962004-06-14 08:17:27 +0000137 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000138 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000139
Lev Walkin29a044b2004-06-14 07:24:36 +0000140 /* Clear the high unused bits */
141 for(bits = rvsize - arclen;
142 bits > CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000143 rvbuf += inc, bits -= CHAR_BIT)
144 *rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000145
Lev Walkin29a044b2004-06-14 07:24:36 +0000146 /* Fill the body of a value */
147 for(; arcbuf < arcend; arcbuf++) {
148 cache = (cache << 7) | (*arcbuf & 0x7f);
149 bits += 7;
150 if(bits >= CHAR_BIT) {
151 bits -= CHAR_BIT;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000152 *rvbuf = (cache >> bits);
153 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000154 }
155 }
156 if(bits) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 *rvbuf = cache;
158 rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000159 }
160 }
161
162 if(add) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000163 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
164 int v = add + *rvbuf;
Lev Walkin74757c82016-07-02 23:02:59 -0700165 if(v & ((unsigned)~0 << CHAR_BIT)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000166 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 add = -1;
168 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000169 *rvbuf = v;
Lev Walkin29a044b2004-06-14 07:24:36 +0000170 break;
171 }
172 }
173 if(rvbuf == rvstart) {
174 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 errno = ERANGE; /* Overflow */
176 return -1;
177 }
178 }
179
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 return 0;
181}
182
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700184OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 asn_app_consume_bytes_f *cb, void *app_key) {
186 char scratch[64]; /* Conservative estimate */
187 unsigned long accum; /* Bits accumulator */
188 char *p; /* Position in the scratch buffer */
189
Lev Walkin29a044b2004-06-14 07:24:36 +0000190 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
191 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 return -1;
193
Lev Walkin3251b8e2004-08-23 09:23:02 +0000194 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 ssize_t len;
196
Lev Walkin3251b8e2004-08-23 09:23:02 +0000197 /* Fill the scratch buffer in reverse. */
198 p = scratch + sizeof(scratch);
199 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000201
Lev Walkina9cc46e2004-09-22 16:06:28 +0000202 len = sizeof(scratch) - (p - scratch);
203 if(cb(p, len, app_key) < 0)
204 return -1;
205 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000206 } else {
207 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208 if(cb(scratch, 1, app_key) < 0)
209 return -1;
210 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000211 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000212}
213
214int
Wim Lewis14e6b162014-07-23 16:06:01 -0700215OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216 asn_app_consume_bytes_f *cb, void *app_key) {
217
218 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
219 return -1;
220
221 return 0;
222}
223
224static ssize_t
225OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
226 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000227 int startn;
228 int add = 0;
229 int i;
230
Lev Walkinf15320b2004-06-03 03:38:44 +0000231 for(i = 0, startn = 0; i < st->size; i++) {
232 uint8_t b = st->buf[i];
233 if((b & 0x80)) /* Continuation expected */
234 continue;
235
236 if(startn == 0) {
237 /*
238 * First two arcs are encoded through the backdoor.
239 */
240 if(i) {
241 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000242 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 } else if(b <= 39) {
244 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000245 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 } else if(b < 79) {
247 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000248 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000249 } else {
250 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000253 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 }
255
Lev Walkina9cc46e2004-09-22 16:06:28 +0000256 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 return -1;
258
Lev Walkina9cc46e2004-09-22 16:06:28 +0000259 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
260 i - startn + 1, add, cb, app_key);
261 if(add < 0) return -1;
262 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 startn = i + 1;
264 add = 0;
265 }
266
Lev Walkina9cc46e2004-09-22 16:06:28 +0000267 return wrote_len;
268}
269
Lev Walkin0fab1a62005-03-09 22:19:25 +0000270static enum xer_pbd_rval
271OBJECT_IDENTIFIER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin92302252004-10-23 10:16:51 +0000272 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000273 const char *chunk_end = (const char *)chunk_buf + chunk_size;
274 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000275 long s_arcs[10];
276 long *arcs = s_arcs;
277 int arcs_count;
278 int ret;
279
Lev Walkine0b56e02005-02-25 12:10:27 +0000280 (void)td;
281
Lev Walkin92302252004-10-23 10:16:51 +0000282 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
Lev Walkin0fab1a62005-03-09 22:19:25 +0000283 (const char *)chunk_buf, chunk_size, arcs,
284 sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
Lev Walkinaed43c82012-09-04 14:56:27 -0700285 if(arcs_count < 0) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000286 /* Expecting more than zero arcs */
287 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700288 } else if(arcs_count == 0) {
289 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000290 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700291 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000292
293 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000294 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000295 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000296 ret = OBJECT_IDENTIFIER_parse_arcs(
297 (const char *)chunk_buf, chunk_size,
298 arcs, arcs_count, &endptr);
299 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000300 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000301 }
302
303 /*
304 * Convert arcs into BER representation.
305 */
306 ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000307 if(arcs != s_arcs) FREEMEM(arcs);
308
Lev Walkin2c34aad2005-03-10 11:50:24 +0000309 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000310}
311
312asn_dec_rval_t
313OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
314 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000315 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000316
317 return xer_decode_primitive(opt_codec_ctx, td,
318 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
319 buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
320}
321
Lev Walkina9cc46e2004-09-22 16:06:28 +0000322asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000323OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000324 int ilevel, enum xer_encoder_flags_e flags,
325 asn_app_consume_bytes_f *cb, void *app_key) {
326 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
327 asn_enc_rval_t er;
328
329 (void)ilevel;
330 (void)flags;
331
332 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700333 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000334
335 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700336 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000337
Lev Walkin7c1dc052016-03-14 03:08:15 -0700338 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000339}
340
341int
Lev Walkin5e033762004-09-29 13:26:15 +0000342OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000343 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
344 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
345
346 (void)td; /* Unused argument */
347 (void)ilevel; /* Unused argument */
348
349 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000350 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000351
352 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000353 if(cb("{ ", 2, app_key) < 0)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000354 return -1;
355
356 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
357 return -1;
358
Lev Walkin8e8078a2004-09-26 13:10:40 +0000359 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000360}
361
362int
Wim Lewis14e6b162014-07-23 16:06:01 -0700363OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs,
Lev Walkin29a044b2004-06-14 07:24:36 +0000364 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000365 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000366 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 int startn = 0;
368 int add = 0;
369 int i;
370
Lev Walkin0787ff02004-06-17 23:43:39 +0000371 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000372 errno = EINVAL;
373 return -1;
374 }
375
376 for(i = 0; i < oid->size; i++) {
377 uint8_t b = oid->buf[i];
378 if((b & 0x80)) /* Continuation expected */
379 continue;
380
Lev Walkin29a044b2004-06-14 07:24:36 +0000381 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000382 /*
383 * First two arcs are encoded through the backdoor.
384 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000385 unsigned LE = 1; /* Little endian */
386 int first_arc;
387 num_arcs++;
388 if(!arc_slots) { num_arcs++; continue; }
389
390 if(i) first_arc = 2;
391 else if(b <= 39) first_arc = 0;
392 else if(b < 79) first_arc = 1;
393 else first_arc = 2;
394
395 add = -40 * first_arc;
396 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000397 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000398 + ((*(char *)&LE)?0:(arc_type_size - 1)))
399 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000400 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 }
402
Lev Walkin29a044b2004-06-14 07:24:36 +0000403 /* Decode, if has space */
404 if(arcs < arcs_end) {
405 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
406 i - startn + 1, add,
407 arcs, arc_type_size))
408 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000409 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000410 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000411 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000412 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000413 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 }
415
Lev Walkin29a044b2004-06-14 07:24:36 +0000416 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000417}
418
Lev Walkin0787ff02004-06-17 23:43:39 +0000419
420/*
421 * Save the single value as an object identifier arc.
422 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000423int
Lev Walkinb1a15552005-12-22 21:39:44 +0000424OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000425 /*
426 * The following conditions must hold:
427 * assert(arcval);
428 * assert(arcval_size > 0);
Lev Walkin523de9e2006-08-18 01:34:18 +0000429 * assert(arcval_size <= 16);
Lev Walkin0787ff02004-06-17 23:43:39 +0000430 * assert(arcbuf);
431 */
432#ifdef WORDS_BIGENDIAN
433 const unsigned isLittleEndian = 0;
434#else
435 unsigned LE = 1;
436 unsigned isLittleEndian = *(char *)&LE;
437#endif
Lev Walkinb1a15552005-12-22 21:39:44 +0000438 const uint8_t *tend, *tp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000439 unsigned int cache;
440 uint8_t *bp = arcbuf;
441 int bits;
Lev Walkin523de9e2006-08-18 01:34:18 +0000442 uint8_t buffer[16];
Lev Walkin0787ff02004-06-17 23:43:39 +0000443
444 if(isLittleEndian && !prepared_order) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000445 const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1;
446 const uint8_t *aend = (const uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000447 uint8_t *msb = buffer + arcval_size - 1;
Lev Walkinb1a15552005-12-22 21:39:44 +0000448 uint8_t *tb;
449 for(tb = buffer; a >= aend; tb++, a--)
450 if((*tb = *a) && (tb < msb))
451 msb = tb;
Lev Walkin0787ff02004-06-17 23:43:39 +0000452 tend = &buffer[arcval_size];
453 tp = msb; /* Most significant non-zero byte */
454 } else {
455 /* Look for most significant non-zero byte */
Lev Walkinb1a15552005-12-22 21:39:44 +0000456 tend = (const unsigned char *)arcval + arcval_size;
457 for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000458 if(*tp) break;
459 }
460
461 /*
462 * Split the value in 7-bits chunks.
463 */
464 bits = ((tend - tp) * CHAR_BIT) % 7;
465 if(bits) {
466 cache = *tp >> (CHAR_BIT - bits);
467 if(cache) {
468 *bp++ = cache | 0x80;
469 cache = *tp++;
470 bits = CHAR_BIT - bits;
471 } else {
472 bits = -bits;
473 }
474 } else {
475 cache = 0;
476 }
477 for(; tp < tend; tp++) {
478 cache = (cache << CHAR_BIT) + *tp;
479 bits += CHAR_BIT;
480 while(bits >= 7) {
481 bits -= 7;
482 *bp++ = 0x80 | (cache >> bits);
483 }
484 }
485 if(bits) *bp++ = cache;
486 bp[-1] &= 0x7f; /* Clear the last bit */
487
488 return bp - arcbuf;
489}
490
Lev Walkinf15320b2004-06-03 03:38:44 +0000491int
Lev Walkinb1a15552005-12-22 21:39:44 +0000492OBJECT_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 +0000493 uint8_t *buf;
494 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000495 unsigned LE = 1; /* Little endian (x86) */
496 unsigned isLittleEndian = *((char *)&LE);
497 unsigned int arc0;
498 unsigned int arc1;
499 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000500 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000501
Lev Walkin523de9e2006-08-18 01:34:18 +0000502 if(!oid || !arcs || arc_type_size < 1
503 || arc_type_size > 16
504 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 errno = EINVAL;
506 return -1;
507 }
508
Lev Walkin0787ff02004-06-17 23:43:39 +0000509 switch(arc_type_size) {
510 case sizeof(char):
Lev Walkinb1a15552005-12-22 21:39:44 +0000511 arc0 = ((const unsigned char *)arcs)[0];
512 arc1 = ((const unsigned char *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000513 break;
514 case sizeof(short):
Lev Walkinb1a15552005-12-22 21:39:44 +0000515 arc0 = ((const unsigned short *)arcs)[0];
516 arc1 = ((const unsigned short *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000517 break;
518 case sizeof(int):
Lev Walkinb1a15552005-12-22 21:39:44 +0000519 arc0 = ((const unsigned int *)arcs)[0];
520 arc1 = ((const unsigned int *)arcs)[1];
Lev Walkin0787ff02004-06-17 23:43:39 +0000521 break;
522 default:
523 arc1 = arc0 = 0;
524 if(isLittleEndian) { /* Little endian (x86) */
Lev Walkinb1a15552005-12-22 21:39:44 +0000525 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000526 /* If more significant bytes are present,
527 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000528 for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000529 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000530 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
531 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
Lev Walkinb1a15552005-12-22 21:39:44 +0000532 arc0 = *((const unsigned char *)arcs + 0);
533 arc1 = *((const unsigned char *)arcs + arc_type_size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000534 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000535 const unsigned char *ps, *pe;
Lev Walkin0787ff02004-06-17 23:43:39 +0000536 /* If more significant bytes are present,
537 * make them > 255 quick */
Lev Walkinb1a15552005-12-22 21:39:44 +0000538 for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000539 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
Lev Walkinb1a15552005-12-22 21:39:44 +0000540 arc0 = *((const unsigned char *)arcs + arc_type_size - 1);
541 arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1);
Lev Walkin0787ff02004-06-17 23:43:39 +0000542 }
543 }
544
545 /*
546 * The previous chapter left us with the first and the second arcs.
547 * The values are not precise (that is, they are valid only if
548 * they're less than 255), but OK for the purposes of making
549 * the sanity test below.
550 */
551 if(arc0 <= 1) {
552 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 /* 8.19.4: At most 39 subsequent values (including 0) */
554 errno = ERANGE;
555 return -1;
556 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000557 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 /* 8.19.4: Only three values are allocated from the root node */
559 errno = ERANGE;
560 return -1;
561 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000562 /*
563 * After above tests it is known that the value of arc0 is completely
564 * trustworthy (0..2). However, the arc1's value is still meaningless.
565 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000566
567 /*
568 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000569 * This estimation implicitly takes in account the following facts,
570 * that cancel each other:
571 * * the first two arcs are encoded in a single value.
572 * * the first value may require more space (+1 byte)
573 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000575 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000576 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 if(!buf) {
578 /* ENOMEM */
579 return -1;
580 }
581
582 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000583 * Encode the first two arcs.
584 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000585 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000587 uint8_t *tp;
Lev Walkin523de9e2006-08-18 01:34:18 +0000588 uint8_t first_value[1 + 16]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000589 uint8_t *fv = first_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000590
Lev Walkin0787ff02004-06-17 23:43:39 +0000591 /*
592 * Simulate first_value = arc0 * 40 + arc1;
593 */
594 /* Copy the second (1'st) arcs[1] into the first_value */
595 *fv++ = 0;
Lev Walkinb1a15552005-12-22 21:39:44 +0000596 arcs = ((const char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000597 if(isLittleEndian) {
Lev Walkinb1a15552005-12-22 21:39:44 +0000598 const uint8_t *aend = (const unsigned char *)arcs - 1;
599 const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000600 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000601 } else {
Lev Walkinb1a15552005-12-22 21:39:44 +0000602 const uint8_t *a1 = (const uint8_t *)arcs;
603 const uint8_t *aend = a1 + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000604 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000605 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000606 /* Increase the first_value by arc0 */
607 arc0 *= 40; /* (0..80) */
608 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
609 unsigned int v = *tp;
610 v += arc0;
611 *tp = v;
612 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
613 else break;
614 }
615
616 assert(tp >= first_value);
617
618 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
619 fv - first_value, 1);
620 }
621
622 /*
623 * Save the rest of arcs.
624 */
Lev Walkinb1a15552005-12-22 21:39:44 +0000625 for(arcs = ((const char *)arcs) + arc_type_size, i = 2;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000626 i < arc_slots;
Lev Walkinb1a15552005-12-22 21:39:44 +0000627 i++, arcs = ((const char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000628 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
629 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000630 }
631
Lev Walkin34b2a932004-06-17 23:46:45 +0000632 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000633
634 /*
635 * Replace buffer.
636 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000637 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000638 bp = oid->buf;
639 oid->buf = buf;
640 if(bp) FREEMEM(bp);
641
642 return 0;
643}
Lev Walkinc4c61962004-06-14 08:17:27 +0000644
Lev Walkin92302252004-10-23 10:16:51 +0000645
646int
647OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
Lev Walkin093ba2e2005-04-04 21:10:06 +0000648 long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) {
Lev Walkin92302252004-10-23 10:16:51 +0000649 unsigned int arcs_count = 0;
650 const char *oid_end;
Lev Walkin92302252004-10-23 10:16:51 +0000651 enum {
Lev Walkinf5927112012-09-03 00:48:45 -0700652 ST_LEADSPACE,
653 ST_TAILSPACE,
Lev Walkincad560a2013-03-16 07:00:58 -0700654 ST_AFTERVALUE, /* Next character ought to be '.' or a space */
Lev Walkin104af192016-01-24 22:13:27 -0800655 ST_WAITDIGITS /* Next character is expected to be a digit */
Lev Walkinf5927112012-09-03 00:48:45 -0700656 } state = ST_LEADSPACE;
Lev Walkin92302252004-10-23 10:16:51 +0000657
658 if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) {
Lev Walkin093ba2e2005-04-04 21:10:06 +0000659 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000660 errno = EINVAL;
661 return -1;
662 }
663
664 if(oid_txt_length == -1)
665 oid_txt_length = strlen(oid_text);
666
Lev Walkincad560a2013-03-16 07:00:58 -0700667#define _OID_CAPTURE_ARC(oid_text, oid_end) do { \
668 const char *endp = oid_end; \
Lev Walkinf5927112012-09-03 00:48:45 -0700669 long value; \
Lev Walkincad560a2013-03-16 07:00:58 -0700670 switch(asn_strtol_lim(oid_text, &endp, &value)) { \
671 case ASN_STRTOL_EXTRA_DATA: \
Lev Walkinf5927112012-09-03 00:48:45 -0700672 case ASN_STRTOL_OK: \
673 if(arcs_count < arcs_slots) \
674 arcs[arcs_count] = value; \
675 arcs_count++; \
Lev Walkincad560a2013-03-16 07:00:58 -0700676 oid_text = endp - 1; \
Lev Walkinf5927112012-09-03 00:48:45 -0700677 break; \
678 case ASN_STRTOL_ERROR_RANGE: \
679 if(opt_oid_text_end) \
680 *opt_oid_text_end = oid_text; \
681 errno = ERANGE; \
682 return -1; \
683 case ASN_STRTOL_ERROR_INVAL: \
Lev Walkincad560a2013-03-16 07:00:58 -0700684 case ASN_STRTOL_EXPECT_MORE: \
Lev Walkinf5927112012-09-03 00:48:45 -0700685 if(opt_oid_text_end) \
686 *opt_oid_text_end = oid_text; \
687 errno = EINVAL; \
688 return -1; \
689 } \
690 } while(0)
691
Lev Walkin92302252004-10-23 10:16:51 +0000692 for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
693 switch(*oid_text) {
694 case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
Lev Walkinf5927112012-09-03 00:48:45 -0700695 switch(state) {
696 case ST_LEADSPACE:
697 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000698 continue;
Lev Walkincad560a2013-03-16 07:00:58 -0700699 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700700 state = ST_TAILSPACE;
701 continue;
702 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700703 break; /* Digits expected after ".", got whitespace */
Lev Walkin92302252004-10-23 10:16:51 +0000704 }
Lev Walkincad560a2013-03-16 07:00:58 -0700705 break;
Lev Walkinf5927112012-09-03 00:48:45 -0700706 case 0x2e: /* '.' */
707 switch(state) {
708 case ST_LEADSPACE:
Lev Walkinf5927112012-09-03 00:48:45 -0700709 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700710 case ST_WAITDIGITS:
711 if(opt_oid_text_end)
712 *opt_oid_text_end = oid_text;
713 errno = EINVAL; /* Broken OID */
714 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700715 break;
Lev Walkincad560a2013-03-16 07:00:58 -0700716 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700717 state = ST_WAITDIGITS;
Lev Walkin92302252004-10-23 10:16:51 +0000718 continue;
719 }
Lev Walkinf5927112012-09-03 00:48:45 -0700720 break;
721 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
722 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
723 switch(state) {
724 case ST_TAILSPACE:
Lev Walkincad560a2013-03-16 07:00:58 -0700725 case ST_AFTERVALUE:
726 if(opt_oid_text_end)
727 *opt_oid_text_end = oid_text;
728 errno = EINVAL; /* "1. 1" => broken OID */
729 return -1;
Lev Walkinf5927112012-09-03 00:48:45 -0700730 case ST_LEADSPACE:
731 case ST_WAITDIGITS:
Lev Walkincad560a2013-03-16 07:00:58 -0700732 _OID_CAPTURE_ARC(oid_text, oid_end);
733 state = ST_AFTERVALUE;
Lev Walkinf5927112012-09-03 00:48:45 -0700734 continue;
735 }
736 break;
Lev Walkin92302252004-10-23 10:16:51 +0000737 default:
738 /* Unexpected symbols */
739 state = ST_WAITDIGITS;
740 break;
741 } /* switch() */
742 break;
743 } /* for() */
744
745
Lev Walkin093ba2e2005-04-04 21:10:06 +0000746 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
Lev Walkin92302252004-10-23 10:16:51 +0000747
748 /* Finalize last arc */
749 switch(state) {
Lev Walkinf5927112012-09-03 00:48:45 -0700750 case ST_LEADSPACE:
Lev Walkinaed43c82012-09-04 14:56:27 -0700751 return 0; /* No OID found in input data */
Lev Walkinaed43c82012-09-04 14:56:27 -0700752 case ST_WAITDIGITS:
753 errno = EINVAL; /* Broken OID */
754 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700755 case ST_AFTERVALUE:
Lev Walkinf5927112012-09-03 00:48:45 -0700756 case ST_TAILSPACE:
Lev Walkin92302252004-10-23 10:16:51 +0000757 return arcs_count;
758 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700759
760 errno = EINVAL; /* Broken OID */
761 return -1;
Lev Walkin92302252004-10-23 10:16:51 +0000762}
763
764