blob: 89e2e327c2d2b815d6fb480419266dd855cdfefb [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 */
5#include <OBJECT_IDENTIFIER.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00006#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <assert.h>
8#include <errno.h>
9
10/*
11 * OBJECT IDENTIFIER basic type description.
12 */
13static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = {
14 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
15};
16asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = {
17 "OBJECT IDENTIFIER",
18 OBJECT_IDENTIFIER_constraint,
19 INTEGER_decode_ber, /* Implemented in terms of INTEGER type */
20 OBJECT_IDENTIFIER_encode_der,
21 OBJECT_IDENTIFIER_print,
22 INTEGER_free,
23 0, /* Use generic outmost tag fetcher */
24 asn1_DEF_OBJECT_IDENTIFIER_tags,
25 sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
26 / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
27 1, /* Single UNIVERSAL tag may be implicitly overriden */
Lev Walkind9bd7752004-06-05 08:17:50 +000028 0, /* Always in primitive form */
29 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000030};
31
32
33/*
34 * Encode OBJECT IDENTIFIER type using DER.
35 */
36der_enc_rval_t
37OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
38 int tag_mode, ber_tlv_tag_t tag,
39 asn_app_consume_bytes_f *cb, void *app_key) {
40 der_enc_rval_t erval;
Lev Walkinc2346572004-08-11 09:07:36 +000041 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000042
43 ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)",
44 cb?"Encoding":"Estimating", sd->name, tag_mode);
45
46 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
47 cb, app_key);
48 ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d",
49 sd->name, (int)erval.encoded);
50 if(erval.encoded == -1) {
51 erval.failed_type = sd;
52 erval.structure_ptr = ptr;
53 return erval;
54 }
55
56 if(cb && st->buf) {
57 ssize_t ret;
58
59 ret = cb(st->buf, st->size, app_key);
60 if(ret == -1) {
61 erval.encoded = -1;
62 erval.failed_type = sd;
63 erval.structure_ptr = ptr;
64 return erval;
65 }
66 } else {
67 assert(st->buf || st->size == 0);
68 }
69
70 erval.encoded += st->size;
71
72 return erval;
73}
74
75int
76OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
77 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000078 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000079
80 if(st && st->buf) {
81 if(st->size < 1) {
Lev Walkinba4e5182004-08-11 09:44:13 +000082 _ASN_ERRLOG(app_errlog, app_key,
83 "%s: at least one numerical value expected",
Lev Walkinf15320b2004-06-03 03:38:44 +000084 td->name);
85 return -1;
86 }
87 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000088 _ASN_ERRLOG(app_errlog, app_key,
89 "%s: value not given", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000090 return -1;
91 }
92
93 return 0;
94}
95
Lev Walkinc4c61962004-06-14 08:17:27 +000096
Lev Walkinf15320b2004-06-03 03:38:44 +000097int
Lev Walkin29a044b2004-06-14 07:24:36 +000098OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
99 unsigned LE = 1; /* Little endian (x86) */
100 uint8_t *arcend = arcbuf + arclen; /* End of arc */
101 void *rvstart = rvbuf; /* Original start of the value buffer */
102 unsigned int cache = 0; /* No more than 14 significant bits */
103 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +0000104
Lev Walkin29a044b2004-06-14 07:24:36 +0000105 rvsize *= CHAR_BIT; /* bytes to bits */
106 arclen *= 7; /* bytes to bits */
107
108 /*
109 * The arc has the number of bits
110 * cannot be represented using supplied return value type.
111 */
112 if(arclen > rvsize) {
113 if(arclen > (rvsize + CHAR_BIT)) {
114 errno = ERANGE; /* Overflow */
115 return -1;
116 } else {
117 /*
118 * Even if the number of bits in the arc representation
119 * is higher than the width of supplied * return value
120 * type, there is still possible to fit it when there
121 * are few unused high bits in the arc value
122 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000123 *
124 * Moreover, there is a possibility that the
125 * number could actually fit the arc space, given
126 * that add is negative, but we don't handle
127 * such "temporary lack of precision" situation here.
128 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000129 */
130 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
131 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 errno = ERANGE; /* Overflow */
133 return -1;
134 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000135 /* Fool the routine computing unused bits */
136 arclen -= 7;
137 cache = *arcbuf & 0x7f;
138 arcbuf++;
139 }
140 }
141
Lev Walkinc4c61962004-06-14 08:17:27 +0000142 /* Faster path for common size */
143 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
144 unsigned long accum;
145 /* Gather all bits into the accumulator */
146 for(accum = cache; arcbuf < arcend; arcbuf++)
147 accum = (accum << 7) | (*arcbuf & ~0x80);
148 if(accum < (unsigned)-add) {
149 errno = ERANGE; /* Overflow */
150 return -1;
151 }
152 *(unsigned long *)rvbuf = accum + add;
153 return 0;
154 }
155
Lev Walkin29a044b2004-06-14 07:24:36 +0000156#ifndef WORDS_BIGENDIAN
157 if(*(unsigned char *)&LE) { /* Little endian (x86) */
158 /* "Convert" to big endian */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000159 (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
Lev Walkinc4c61962004-06-14 08:17:27 +0000160 ((unsigned char *)rvstart)--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000161 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000162 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000163#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000164 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000165
Lev Walkinc4c61962004-06-14 08:17:27 +0000166 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000167 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000168
Lev Walkin29a044b2004-06-14 07:24:36 +0000169 /* Clear the high unused bits */
170 for(bits = rvsize - arclen;
171 bits > CHAR_BIT;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000172 (unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
Lev Walkin29a044b2004-06-14 07:24:36 +0000173 *(unsigned char *)rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000174
Lev Walkin29a044b2004-06-14 07:24:36 +0000175 /* Fill the body of a value */
176 for(; arcbuf < arcend; arcbuf++) {
177 cache = (cache << 7) | (*arcbuf & 0x7f);
178 bits += 7;
179 if(bits >= CHAR_BIT) {
180 bits -= CHAR_BIT;
181 *(unsigned char *)rvbuf = (cache >> bits);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000182 (unsigned char *)rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000183 }
184 }
185 if(bits) {
186 *(unsigned char *)rvbuf = cache;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000187 (unsigned char *)rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000188 }
189 }
190
191 if(add) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000192 for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
Lev Walkin29a044b2004-06-14 07:24:36 +0000193 int v = add + *(unsigned char *)rvbuf;
194 if(v & (-1 << CHAR_BIT)) {
195 *(unsigned char *)rvbuf
Lev Walkin74057d52004-08-11 09:17:15 +0000196 = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000197 add = -1;
198 } else {
199 *(unsigned char *)rvbuf = v;
200 break;
201 }
202 }
203 if(rvbuf == rvstart) {
204 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000205 errno = ERANGE; /* Overflow */
206 return -1;
207 }
208 }
209
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 return 0;
211}
212
Lev Walkin29a044b2004-06-14 07:24:36 +0000213
Lev Walkinf15320b2004-06-03 03:38:44 +0000214int
215OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
216 asn_app_consume_bytes_f *cb, void *app_key) {
217 char scratch[64]; /* Conservative estimate */
218 unsigned long accum; /* Bits accumulator */
219 char *p; /* Position in the scratch buffer */
220
Lev Walkin29a044b2004-06-14 07:24:36 +0000221 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
222 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 return -1;
224
225 /* Fill the scratch buffer in reverse. */
226 p = scratch + sizeof(scratch);
227 for(; accum; accum /= 10)
Lev Walkin74057d52004-08-11 09:17:15 +0000228 *(--p) = (char)(accum % 10) + 0x30;
Lev Walkinf15320b2004-06-03 03:38:44 +0000229
230 return cb(p, sizeof(scratch) - (p - scratch), app_key);
231}
232
233int
234OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
235 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000236 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000237 int startn;
238 int add = 0;
239 int i;
240
Lev Walkind9bd7752004-06-05 08:17:50 +0000241 (void)td; /* Unused argument */
242 (void)ilevel; /* Unused argument */
243
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 if(!st || !st->buf)
245 return cb("<absent>", 8, app_key);
246
247 /* Dump preamble */
248 if(cb("{ ", 2, app_key))
249 return -1;
250
251 for(i = 0, startn = 0; i < st->size; i++) {
252 uint8_t b = st->buf[i];
253 if((b & 0x80)) /* Continuation expected */
254 continue;
255
256 if(startn == 0) {
257 /*
258 * First two arcs are encoded through the backdoor.
259 */
260 if(i) {
261 add = -80;
262 if(cb("2", 1, app_key)) return -1;
263 } else if(b <= 39) {
264 add = 0;
265 if(cb("0", 1, app_key)) return -1;
266 } else if(b < 79) {
267 add = -40;
268 if(cb("1", 1, app_key)) return -1;
269 } else {
270 add = -80;
271 if(cb("2", 1, app_key)) return -1;
272 }
273 }
274
275 if(cb(" ", 1, app_key)) /* Separate arcs */
276 return -1;
277
278 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
279 i - startn + 1, add,
280 cb, app_key))
281 return -1;
282 startn = i + 1;
283 add = 0;
284 }
285
286 return cb(" }", 2, app_key);
287}
288
289int
Lev Walkin29a044b2004-06-14 07:24:36 +0000290OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
291 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000292 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000293 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000294 int startn = 0;
295 int add = 0;
296 int i;
297
Lev Walkin0787ff02004-06-17 23:43:39 +0000298 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000299 errno = EINVAL;
300 return -1;
301 }
302
303 for(i = 0; i < oid->size; i++) {
304 uint8_t b = oid->buf[i];
305 if((b & 0x80)) /* Continuation expected */
306 continue;
307
Lev Walkin29a044b2004-06-14 07:24:36 +0000308 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000309 /*
310 * First two arcs are encoded through the backdoor.
311 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000312 unsigned LE = 1; /* Little endian */
313 int first_arc;
314 num_arcs++;
315 if(!arc_slots) { num_arcs++; continue; }
316
317 if(i) first_arc = 2;
318 else if(b <= 39) first_arc = 0;
319 else if(b < 79) first_arc = 1;
320 else first_arc = 2;
321
322 add = -40 * first_arc;
323 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000324 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000325 + ((*(char *)&LE)?0:(arc_type_size - 1)))
326 = first_arc;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000327 (char *)arcs += arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000328 }
329
Lev Walkin29a044b2004-06-14 07:24:36 +0000330 /* Decode, if has space */
331 if(arcs < arcs_end) {
332 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
333 i - startn + 1, add,
334 arcs, arc_type_size))
335 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 startn = i + 1;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000337 (char *)arcs += arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000338 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000339 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000340 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000341 }
342
Lev Walkin29a044b2004-06-14 07:24:36 +0000343 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000344}
345
Lev Walkin0787ff02004-06-17 23:43:39 +0000346
347/*
348 * Save the single value as an object identifier arc.
349 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000350int
Lev Walkin0787ff02004-06-17 23:43:39 +0000351OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
352 /*
353 * The following conditions must hold:
354 * assert(arcval);
355 * assert(arcval_size > 0);
356 * assert(arcbuf);
357 */
358#ifdef WORDS_BIGENDIAN
359 const unsigned isLittleEndian = 0;
360#else
361 unsigned LE = 1;
362 unsigned isLittleEndian = *(char *)&LE;
363#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000364 uint8_t *tp, *tend;
365 unsigned int cache;
366 uint8_t *bp = arcbuf;
367 int bits;
Lev Walkin64399722004-08-11 07:17:22 +0000368#ifdef __GNUC__
369 uint8_t buffer[arcval_size];
370#else
371 uint8_t *buffer = alloca(arcval_size);
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000372 if(!buffer) { errno = ENOMEM; return -1; }
Lev Walkin64399722004-08-11 07:17:22 +0000373#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000374
375 if(isLittleEndian && !prepared_order) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000376 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
Lev Walkinc2346572004-08-11 09:07:36 +0000377 uint8_t *aend = (uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000378 uint8_t *msb = buffer + arcval_size - 1;
379 for(tp = buffer; a >= aend; tp++, a--)
380 if((*tp = *a) && (tp < msb))
381 msb = tp;
382 tend = &buffer[arcval_size];
383 tp = msb; /* Most significant non-zero byte */
384 } else {
385 /* Look for most significant non-zero byte */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000386 tend = (unsigned char *)arcval + arcval_size;
Lev Walkinc2346572004-08-11 09:07:36 +0000387 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000388 if(*tp) break;
389 }
390
391 /*
392 * Split the value in 7-bits chunks.
393 */
394 bits = ((tend - tp) * CHAR_BIT) % 7;
395 if(bits) {
396 cache = *tp >> (CHAR_BIT - bits);
397 if(cache) {
398 *bp++ = cache | 0x80;
399 cache = *tp++;
400 bits = CHAR_BIT - bits;
401 } else {
402 bits = -bits;
403 }
404 } else {
405 cache = 0;
406 }
407 for(; tp < tend; tp++) {
408 cache = (cache << CHAR_BIT) + *tp;
409 bits += CHAR_BIT;
410 while(bits >= 7) {
411 bits -= 7;
412 *bp++ = 0x80 | (cache >> bits);
413 }
414 }
415 if(bits) *bp++ = cache;
416 bp[-1] &= 0x7f; /* Clear the last bit */
417
418 return bp - arcbuf;
419}
420
Lev Walkinf15320b2004-06-03 03:38:44 +0000421int
Lev Walkin0787ff02004-06-17 23:43:39 +0000422OBJECT_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 +0000423 uint8_t *buf;
424 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000425 unsigned LE = 1; /* Little endian (x86) */
426 unsigned isLittleEndian = *((char *)&LE);
427 unsigned int arc0;
428 unsigned int arc1;
429 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000430 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000431
Lev Walkin0787ff02004-06-17 23:43:39 +0000432 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 errno = EINVAL;
434 return -1;
435 }
436
Lev Walkin0787ff02004-06-17 23:43:39 +0000437 switch(arc_type_size) {
438 case sizeof(char):
439 arc0 = ((unsigned char *)arcs)[0];
440 arc1 = ((unsigned char *)arcs)[1];
441 break;
442 case sizeof(short):
443 arc0 = ((unsigned short *)arcs)[0];
444 arc1 = ((unsigned short *)arcs)[1];
445 break;
446 case sizeof(int):
447 arc0 = ((unsigned int *)arcs)[0];
448 arc1 = ((unsigned int *)arcs)[1];
449 break;
450 default:
451 arc1 = arc0 = 0;
452 if(isLittleEndian) { /* Little endian (x86) */
453 unsigned char *ps, *pe;
454 /* If more significant bytes are present,
455 * make them > 255 quick */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000456 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
457 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000458 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
459 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
460 arc0 = *((unsigned char *)arcs + 0);
461 arc1 = *((unsigned char *)arcs + arc_type_size);
462 } else {
463 unsigned char *ps, *pe;
464 /* If more significant bytes are present,
465 * make them > 255 quick */
Lev Walkinc2346572004-08-11 09:07:36 +0000466 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000467 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
468 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
469 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
470 }
471 }
472
473 /*
474 * The previous chapter left us with the first and the second arcs.
475 * The values are not precise (that is, they are valid only if
476 * they're less than 255), but OK for the purposes of making
477 * the sanity test below.
478 */
479 if(arc0 <= 1) {
480 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 /* 8.19.4: At most 39 subsequent values (including 0) */
482 errno = ERANGE;
483 return -1;
484 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000485 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000486 /* 8.19.4: Only three values are allocated from the root node */
487 errno = ERANGE;
488 return -1;
489 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000490 /*
491 * After above tests it is known that the value of arc0 is completely
492 * trustworthy (0..2). However, the arc1's value is still meaningless.
493 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000494
495 /*
496 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000497 * This estimation implicitly takes in account the following facts,
498 * that cancel each other:
499 * * the first two arcs are encoded in a single value.
500 * * the first value may require more space (+1 byte)
501 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000502 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000503 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000504 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 if(!buf) {
506 /* ENOMEM */
507 return -1;
508 }
509
510 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000511 * Encode the first two arcs.
512 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000513 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000514 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000515 uint8_t *tp;
Lev Walkin90fcd442004-08-11 07:48:19 +0000516#ifdef __GNUC__
517 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000518 uint8_t *fv = first_value;
Lev Walkin90fcd442004-08-11 07:48:19 +0000519#else
520 uint8_t *first_value = alloca(1 + arc_type_size);
Lev Walkin90fcd442004-08-11 07:48:19 +0000521 uint8_t *fv = first_value;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000522 if(!first_value) {
523 errno = ENOMEM;
524 return -1;
525 }
526#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000527
Lev Walkin0787ff02004-06-17 23:43:39 +0000528 /*
529 * Simulate first_value = arc0 * 40 + arc1;
530 */
531 /* Copy the second (1'st) arcs[1] into the first_value */
532 *fv++ = 0;
533 (char *)arcs += arc_type_size;
534 if(isLittleEndian) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000535 uint8_t *aend = (unsigned char *)arcs - 1;
536 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000537 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000539 uint8_t *a1 = (uint8_t *)arcs;
Lev Walkin0787ff02004-06-17 23:43:39 +0000540 uint8_t *aend = a1 + arc_type_size;
541 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000543 /* Increase the first_value by arc0 */
544 arc0 *= 40; /* (0..80) */
545 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
546 unsigned int v = *tp;
547 v += arc0;
548 *tp = v;
549 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
550 else break;
551 }
552
553 assert(tp >= first_value);
554
555 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
556 fv - first_value, 1);
557 }
558
559 /*
560 * Save the rest of arcs.
561 */
562 for((char *)arcs += arc_type_size, i = 2;
563 i < arc_slots; i++, (char *)arcs += arc_type_size) {
564 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
565 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000566 }
567
Lev Walkin34b2a932004-06-17 23:46:45 +0000568 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000569
570 /*
571 * Replace buffer.
572 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000573 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 bp = oid->buf;
575 oid->buf = buf;
576 if(bp) FREEMEM(bp);
577
578 return 0;
579}
Lev Walkinc4c61962004-06-14 08:17:27 +0000580