blob: 43ced796d8cea83e6ce72d9ed7b7687c60a3dea7 [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 Walkinf15320b2004-06-03 03:38:44 +00006#include <OBJECT_IDENTIFIER.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00007#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <assert.h>
9#include <errno.h>
10
11/*
12 * OBJECT IDENTIFIER basic type description.
13 */
14static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = {
15 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
16};
17asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = {
18 "OBJECT IDENTIFIER",
Lev Walkina9cc46e2004-09-22 16:06:28 +000019 INTEGER_free,
20 OBJECT_IDENTIFIER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000021 OBJECT_IDENTIFIER_constraint,
22 INTEGER_decode_ber, /* Implemented in terms of INTEGER type */
23 OBJECT_IDENTIFIER_encode_der,
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 0, /* Not implemented yet */
25 OBJECT_IDENTIFIER_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 0, /* Use generic outmost tag fetcher */
27 asn1_DEF_OBJECT_IDENTIFIER_tags,
28 sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
29 / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkin188ed2c2004-09-13 08:31:01 +000030 asn1_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
31 sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
32 / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
Lev Walkind9bd7752004-06-05 08:17:50 +000033 0, /* Always in primitive form */
Lev Walkin449f8322004-08-20 13:23:42 +000034 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000035 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000036};
37
38
39/*
40 * Encode OBJECT IDENTIFIER type using DER.
41 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000042asn_enc_rval_t
Lev Walkinf15320b2004-06-03 03:38:44 +000043OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
44 int tag_mode, ber_tlv_tag_t tag,
45 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +000046 asn_enc_rval_t erval;
Lev Walkinc2346572004-08-11 09:07:36 +000047 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000048
49 ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)",
50 cb?"Encoding":"Estimating", sd->name, tag_mode);
51
52 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
53 cb, app_key);
54 ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d",
55 sd->name, (int)erval.encoded);
56 if(erval.encoded == -1) {
57 erval.failed_type = sd;
58 erval.structure_ptr = ptr;
59 return erval;
60 }
61
62 if(cb && st->buf) {
Lev Walkina9cc46e2004-09-22 16:06:28 +000063 int ret = cb(st->buf, st->size, app_key);
64 if(ret < 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +000065 erval.encoded = -1;
66 erval.failed_type = sd;
67 erval.structure_ptr = ptr;
68 return erval;
69 }
70 } else {
71 assert(st->buf || st->size == 0);
72 }
73
74 erval.encoded += st->size;
75
76 return erval;
77}
78
79int
80OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
81 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000082 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000083
84 if(st && st->buf) {
85 if(st->size < 1) {
Lev Walkinba4e5182004-08-11 09:44:13 +000086 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000087 "%s: at least one numerical value "
88 "expected (%s:%d)",
89 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000090 return -1;
91 }
92 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000093 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000094 "%s: value not given (%s:%d)",
95 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000096 return -1;
97 }
98
99 return 0;
100}
101
Lev Walkinc4c61962004-06-14 08:17:27 +0000102
Lev Walkinf15320b2004-06-03 03:38:44 +0000103int
Lev Walkin29a044b2004-06-14 07:24:36 +0000104OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000105 unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
Lev Walkin29a044b2004-06-14 07:24:36 +0000106 uint8_t *arcend = arcbuf + arclen; /* End of arc */
107 void *rvstart = rvbuf; /* Original start of the value buffer */
108 unsigned int cache = 0; /* No more than 14 significant bits */
109 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +0000110
Lev Walkin29a044b2004-06-14 07:24:36 +0000111 rvsize *= CHAR_BIT; /* bytes to bits */
112 arclen *= 7; /* bytes to bits */
113
114 /*
115 * The arc has the number of bits
116 * cannot be represented using supplied return value type.
117 */
118 if(arclen > rvsize) {
119 if(arclen > (rvsize + CHAR_BIT)) {
120 errno = ERANGE; /* Overflow */
121 return -1;
122 } else {
123 /*
124 * Even if the number of bits in the arc representation
125 * is higher than the width of supplied * return value
126 * type, there is still possible to fit it when there
127 * are few unused high bits in the arc value
128 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000129 *
130 * Moreover, there is a possibility that the
131 * number could actually fit the arc space, given
132 * that add is negative, but we don't handle
133 * such "temporary lack of precision" situation here.
134 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000135 */
136 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
137 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 errno = ERANGE; /* Overflow */
139 return -1;
140 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000141 /* Fool the routine computing unused bits */
142 arclen -= 7;
143 cache = *arcbuf & 0x7f;
144 arcbuf++;
145 }
146 }
147
Lev Walkinc4c61962004-06-14 08:17:27 +0000148 /* Faster path for common size */
149 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
150 unsigned long accum;
151 /* Gather all bits into the accumulator */
152 for(accum = cache; arcbuf < arcend; arcbuf++)
153 accum = (accum << 7) | (*arcbuf & ~0x80);
154 if(accum < (unsigned)-add) {
155 errno = ERANGE; /* Overflow */
156 return -1;
157 }
158 *(unsigned long *)rvbuf = accum + add;
159 return 0;
160 }
161
Lev Walkin29a044b2004-06-14 07:24:36 +0000162#ifndef WORDS_BIGENDIAN
163 if(*(unsigned char *)&LE) { /* Little endian (x86) */
164 /* "Convert" to big endian */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000165 (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
Lev Walkinc4c61962004-06-14 08:17:27 +0000166 ((unsigned char *)rvstart)--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000168 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000169#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000170 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000171
Lev Walkinc4c61962004-06-14 08:17:27 +0000172 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000173 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000174
Lev Walkin29a044b2004-06-14 07:24:36 +0000175 /* Clear the high unused bits */
176 for(bits = rvsize - arclen;
177 bits > CHAR_BIT;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000178 (unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
Lev Walkin29a044b2004-06-14 07:24:36 +0000179 *(unsigned char *)rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000180
Lev Walkin29a044b2004-06-14 07:24:36 +0000181 /* Fill the body of a value */
182 for(; arcbuf < arcend; arcbuf++) {
183 cache = (cache << 7) | (*arcbuf & 0x7f);
184 bits += 7;
185 if(bits >= CHAR_BIT) {
186 bits -= CHAR_BIT;
187 *(unsigned char *)rvbuf = (cache >> bits);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000188 (unsigned char *)rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000189 }
190 }
191 if(bits) {
192 *(unsigned char *)rvbuf = cache;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000193 (unsigned char *)rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000194 }
195 }
196
197 if(add) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000198 for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
Lev Walkin29a044b2004-06-14 07:24:36 +0000199 int v = add + *(unsigned char *)rvbuf;
200 if(v & (-1 << CHAR_BIT)) {
201 *(unsigned char *)rvbuf
Lev Walkin74057d52004-08-11 09:17:15 +0000202 = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000203 add = -1;
204 } else {
205 *(unsigned char *)rvbuf = v;
206 break;
207 }
208 }
209 if(rvbuf == rvstart) {
210 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 errno = ERANGE; /* Overflow */
212 return -1;
213 }
214 }
215
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 return 0;
217}
218
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219ssize_t
220OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 asn_app_consume_bytes_f *cb, void *app_key) {
222 char scratch[64]; /* Conservative estimate */
223 unsigned long accum; /* Bits accumulator */
224 char *p; /* Position in the scratch buffer */
225
Lev Walkin29a044b2004-06-14 07:24:36 +0000226 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
227 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000228 return -1;
229
Lev Walkin3251b8e2004-08-23 09:23:02 +0000230 if(accum) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000231 ssize_t len;
232
Lev Walkin3251b8e2004-08-23 09:23:02 +0000233 /* Fill the scratch buffer in reverse. */
234 p = scratch + sizeof(scratch);
235 for(; accum; accum /= 10)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000236 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
Lev Walkinf15320b2004-06-03 03:38:44 +0000237
Lev Walkina9cc46e2004-09-22 16:06:28 +0000238 len = sizeof(scratch) - (p - scratch);
239 if(cb(p, len, app_key) < 0)
240 return -1;
241 return len;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000242 } else {
243 *scratch = 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000244 if(cb(scratch, 1, app_key) < 0)
245 return -1;
246 return 1;
Lev Walkin3251b8e2004-08-23 09:23:02 +0000247 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000248}
249
250int
Lev Walkina9cc46e2004-09-22 16:06:28 +0000251OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
252 asn_app_consume_bytes_f *cb, void *app_key) {
253
254 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
255 return -1;
256
257 return 0;
258}
259
260static ssize_t
261OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
262 ssize_t wrote_len = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 int startn;
264 int add = 0;
265 int i;
266
Lev Walkinf15320b2004-06-03 03:38:44 +0000267 for(i = 0, startn = 0; i < st->size; i++) {
268 uint8_t b = st->buf[i];
269 if((b & 0x80)) /* Continuation expected */
270 continue;
271
272 if(startn == 0) {
273 /*
274 * First two arcs are encoded through the backdoor.
275 */
276 if(i) {
277 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000278 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000279 } else if(b <= 39) {
280 add = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000281 if(cb("0", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000282 } else if(b < 79) {
283 add = -40;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000284 if(cb("1", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000285 } else {
286 add = -80;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000287 if(cb("2", 1, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000288 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000289 wrote_len += 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000290 }
291
Lev Walkina9cc46e2004-09-22 16:06:28 +0000292 if(cb(".", 1, app_key) < 0) /* Separate arcs */
Lev Walkinf15320b2004-06-03 03:38:44 +0000293 return -1;
294
Lev Walkina9cc46e2004-09-22 16:06:28 +0000295 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
296 i - startn + 1, add, cb, app_key);
297 if(add < 0) return -1;
298 wrote_len += 1 + add;
Lev Walkinf15320b2004-06-03 03:38:44 +0000299 startn = i + 1;
300 add = 0;
301 }
302
Lev Walkina9cc46e2004-09-22 16:06:28 +0000303 return wrote_len;
304}
305
306asn_enc_rval_t
307OBJECT_IDENTIFIER_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
308 int ilevel, enum xer_encoder_flags_e flags,
309 asn_app_consume_bytes_f *cb, void *app_key) {
310 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
311 asn_enc_rval_t er;
312
313 (void)ilevel;
314 (void)flags;
315
316 if(!st || !st->buf)
317 _ASN_ENCODE_FAILED;
318
319 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
320 if(er.encoded < 0) _ASN_ENCODE_FAILED;
321
322 return er;
323}
324
325int
326OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
327 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
328 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
329
330 (void)td; /* Unused argument */
331 (void)ilevel; /* Unused argument */
332
333 if(!st || !st->buf)
334 return cb("<absent>", 8, app_key);
335
336 /* Dump preamble */
337 if(cb("{ ", 2, app_key))
338 return -1;
339
340 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
341 return -1;
342
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 return cb(" }", 2, app_key);
344}
345
346int
Lev Walkin29a044b2004-06-14 07:24:36 +0000347OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
348 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000349 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000350 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000351 int startn = 0;
352 int add = 0;
353 int i;
354
Lev Walkin0787ff02004-06-17 23:43:39 +0000355 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000356 errno = EINVAL;
357 return -1;
358 }
359
360 for(i = 0; i < oid->size; i++) {
361 uint8_t b = oid->buf[i];
362 if((b & 0x80)) /* Continuation expected */
363 continue;
364
Lev Walkin29a044b2004-06-14 07:24:36 +0000365 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 /*
367 * First two arcs are encoded through the backdoor.
368 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000369 unsigned LE = 1; /* Little endian */
370 int first_arc;
371 num_arcs++;
372 if(!arc_slots) { num_arcs++; continue; }
373
374 if(i) first_arc = 2;
375 else if(b <= 39) first_arc = 0;
376 else if(b < 79) first_arc = 1;
377 else first_arc = 2;
378
379 add = -40 * first_arc;
380 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000381 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000382 + ((*(char *)&LE)?0:(arc_type_size - 1)))
383 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000384 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000385 }
386
Lev Walkin29a044b2004-06-14 07:24:36 +0000387 /* Decode, if has space */
388 if(arcs < arcs_end) {
389 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
390 i - startn + 1, add,
391 arcs, arc_type_size))
392 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000393 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000394 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000395 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000396 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000397 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000398 }
399
Lev Walkin29a044b2004-06-14 07:24:36 +0000400 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000401}
402
Lev Walkin0787ff02004-06-17 23:43:39 +0000403
404/*
405 * Save the single value as an object identifier arc.
406 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000407int
Lev Walkin0787ff02004-06-17 23:43:39 +0000408OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
409 /*
410 * The following conditions must hold:
411 * assert(arcval);
412 * assert(arcval_size > 0);
413 * assert(arcbuf);
414 */
415#ifdef WORDS_BIGENDIAN
416 const unsigned isLittleEndian = 0;
417#else
418 unsigned LE = 1;
419 unsigned isLittleEndian = *(char *)&LE;
420#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000421 uint8_t *tp, *tend;
422 unsigned int cache;
423 uint8_t *bp = arcbuf;
424 int bits;
Lev Walkin64399722004-08-11 07:17:22 +0000425#ifdef __GNUC__
426 uint8_t buffer[arcval_size];
427#else
428 uint8_t *buffer = alloca(arcval_size);
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000429 if(!buffer) { errno = ENOMEM; return -1; }
Lev Walkin64399722004-08-11 07:17:22 +0000430#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000431
432 if(isLittleEndian && !prepared_order) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000433 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
Lev Walkinc2346572004-08-11 09:07:36 +0000434 uint8_t *aend = (uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000435 uint8_t *msb = buffer + arcval_size - 1;
436 for(tp = buffer; a >= aend; tp++, a--)
437 if((*tp = *a) && (tp < msb))
438 msb = tp;
439 tend = &buffer[arcval_size];
440 tp = msb; /* Most significant non-zero byte */
441 } else {
442 /* Look for most significant non-zero byte */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000443 tend = (unsigned char *)arcval + arcval_size;
Lev Walkinc2346572004-08-11 09:07:36 +0000444 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000445 if(*tp) break;
446 }
447
448 /*
449 * Split the value in 7-bits chunks.
450 */
451 bits = ((tend - tp) * CHAR_BIT) % 7;
452 if(bits) {
453 cache = *tp >> (CHAR_BIT - bits);
454 if(cache) {
455 *bp++ = cache | 0x80;
456 cache = *tp++;
457 bits = CHAR_BIT - bits;
458 } else {
459 bits = -bits;
460 }
461 } else {
462 cache = 0;
463 }
464 for(; tp < tend; tp++) {
465 cache = (cache << CHAR_BIT) + *tp;
466 bits += CHAR_BIT;
467 while(bits >= 7) {
468 bits -= 7;
469 *bp++ = 0x80 | (cache >> bits);
470 }
471 }
472 if(bits) *bp++ = cache;
473 bp[-1] &= 0x7f; /* Clear the last bit */
474
475 return bp - arcbuf;
476}
477
Lev Walkinf15320b2004-06-03 03:38:44 +0000478int
Lev Walkin0787ff02004-06-17 23:43:39 +0000479OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 uint8_t *buf;
481 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000482 unsigned LE = 1; /* Little endian (x86) */
483 unsigned isLittleEndian = *((char *)&LE);
484 unsigned int arc0;
485 unsigned int arc1;
486 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000487 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000488
Lev Walkin0787ff02004-06-17 23:43:39 +0000489 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000490 errno = EINVAL;
491 return -1;
492 }
493
Lev Walkin0787ff02004-06-17 23:43:39 +0000494 switch(arc_type_size) {
495 case sizeof(char):
496 arc0 = ((unsigned char *)arcs)[0];
497 arc1 = ((unsigned char *)arcs)[1];
498 break;
499 case sizeof(short):
500 arc0 = ((unsigned short *)arcs)[0];
501 arc1 = ((unsigned short *)arcs)[1];
502 break;
503 case sizeof(int):
504 arc0 = ((unsigned int *)arcs)[0];
505 arc1 = ((unsigned int *)arcs)[1];
506 break;
507 default:
508 arc1 = arc0 = 0;
509 if(isLittleEndian) { /* Little endian (x86) */
510 unsigned char *ps, *pe;
511 /* If more significant bytes are present,
512 * make them > 255 quick */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000513 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
514 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000515 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
516 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
517 arc0 = *((unsigned char *)arcs + 0);
518 arc1 = *((unsigned char *)arcs + arc_type_size);
519 } else {
520 unsigned char *ps, *pe;
521 /* If more significant bytes are present,
522 * make them > 255 quick */
Lev Walkinc2346572004-08-11 09:07:36 +0000523 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000524 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
525 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
526 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
527 }
528 }
529
530 /*
531 * The previous chapter left us with the first and the second arcs.
532 * The values are not precise (that is, they are valid only if
533 * they're less than 255), but OK for the purposes of making
534 * the sanity test below.
535 */
536 if(arc0 <= 1) {
537 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 /* 8.19.4: At most 39 subsequent values (including 0) */
539 errno = ERANGE;
540 return -1;
541 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000542 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000543 /* 8.19.4: Only three values are allocated from the root node */
544 errno = ERANGE;
545 return -1;
546 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000547 /*
548 * After above tests it is known that the value of arc0 is completely
549 * trustworthy (0..2). However, the arc1's value is still meaningless.
550 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000551
552 /*
553 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000554 * This estimation implicitly takes in account the following facts,
555 * that cancel each other:
556 * * the first two arcs are encoded in a single value.
557 * * the first value may require more space (+1 byte)
558 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000559 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000560 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000561 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 if(!buf) {
563 /* ENOMEM */
564 return -1;
565 }
566
567 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000568 * Encode the first two arcs.
569 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000570 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000572 uint8_t *tp;
Lev Walkin90fcd442004-08-11 07:48:19 +0000573#ifdef __GNUC__
574 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000575 uint8_t *fv = first_value;
Lev Walkin90fcd442004-08-11 07:48:19 +0000576#else
577 uint8_t *first_value = alloca(1 + arc_type_size);
Lev Walkin90fcd442004-08-11 07:48:19 +0000578 uint8_t *fv = first_value;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000579 if(!first_value) {
580 errno = ENOMEM;
581 return -1;
582 }
583#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000584
Lev Walkin0787ff02004-06-17 23:43:39 +0000585 /*
586 * Simulate first_value = arc0 * 40 + arc1;
587 */
588 /* Copy the second (1'st) arcs[1] into the first_value */
589 *fv++ = 0;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000590 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000591 if(isLittleEndian) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000592 uint8_t *aend = (unsigned char *)arcs - 1;
593 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000594 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000595 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000596 uint8_t *a1 = (uint8_t *)arcs;
Lev Walkin0787ff02004-06-17 23:43:39 +0000597 uint8_t *aend = a1 + arc_type_size;
598 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000599 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000600 /* Increase the first_value by arc0 */
601 arc0 *= 40; /* (0..80) */
602 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
603 unsigned int v = *tp;
604 v += arc0;
605 *tp = v;
606 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
607 else break;
608 }
609
610 assert(tp >= first_value);
611
612 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
613 fv - first_value, 1);
614 }
615
616 /*
617 * Save the rest of arcs.
618 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000619 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
620 i < arc_slots;
621 i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000622 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
623 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000624 }
625
Lev Walkin34b2a932004-06-17 23:46:45 +0000626 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000627
628 /*
629 * Replace buffer.
630 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000631 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000632 bp = oid->buf;
633 oid->buf = buf;
634 if(bp) FREEMEM(bp);
635
636 return 0;
637}
Lev Walkinc4c61962004-06-14 08:17:27 +0000638