blob: 063ae978e295346afd61af4e4de8c0d7f81f70f5 [file] [log] [blame]
vlmfa67ddc2004-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>
vlm2e3dd3b2004-06-14 07:24:36 +00006#include <limits.h> /* for CHAR_BIT */
vlmfa67ddc2004-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 */
vlmb42843a2004-06-05 08:17:50 +000028 0, /* Always in primitive form */
vlme413c122004-08-20 13:23:42 +000029 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000030 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000031};
32
33
34/*
35 * Encode OBJECT IDENTIFIER type using DER.
36 */
37der_enc_rval_t
38OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
39 int tag_mode, ber_tlv_tag_t tag,
40 asn_app_consume_bytes_f *cb, void *app_key) {
41 der_enc_rval_t erval;
vlmda674682004-08-11 09:07:36 +000042 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr;
vlmfa67ddc2004-06-03 03:38:44 +000043
44 ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)",
45 cb?"Encoding":"Estimating", sd->name, tag_mode);
46
47 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
48 cb, app_key);
49 ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d",
50 sd->name, (int)erval.encoded);
51 if(erval.encoded == -1) {
52 erval.failed_type = sd;
53 erval.structure_ptr = ptr;
54 return erval;
55 }
56
57 if(cb && st->buf) {
58 ssize_t ret;
59
60 ret = cb(st->buf, st->size, app_key);
61 if(ret == -1) {
62 erval.encoded = -1;
63 erval.failed_type = sd;
64 erval.structure_ptr = ptr;
65 return erval;
66 }
67 } else {
68 assert(st->buf || st->size == 0);
69 }
70
71 erval.encoded += st->size;
72
73 return erval;
74}
75
76int
77OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
78 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +000079 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000080
81 if(st && st->buf) {
82 if(st->size < 1) {
vlme3f0f282004-08-11 09:44:13 +000083 _ASN_ERRLOG(app_errlog, app_key,
84 "%s: at least one numerical value expected",
vlmfa67ddc2004-06-03 03:38:44 +000085 td->name);
86 return -1;
87 }
88 } else {
vlme3f0f282004-08-11 09:44:13 +000089 _ASN_ERRLOG(app_errlog, app_key,
90 "%s: value not given", td->name);
vlmfa67ddc2004-06-03 03:38:44 +000091 return -1;
92 }
93
94 return 0;
95}
96
vlm3717fb32004-06-14 08:17:27 +000097
vlmfa67ddc2004-06-03 03:38:44 +000098int
vlm2e3dd3b2004-06-14 07:24:36 +000099OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
100 unsigned LE = 1; /* Little endian (x86) */
101 uint8_t *arcend = arcbuf + arclen; /* End of arc */
102 void *rvstart = rvbuf; /* Original start of the value buffer */
103 unsigned int cache = 0; /* No more than 14 significant bits */
104 int inc; /* Return value growth direction */
vlmfa67ddc2004-06-03 03:38:44 +0000105
vlm2e3dd3b2004-06-14 07:24:36 +0000106 rvsize *= CHAR_BIT; /* bytes to bits */
107 arclen *= 7; /* bytes to bits */
108
109 /*
110 * The arc has the number of bits
111 * cannot be represented using supplied return value type.
112 */
113 if(arclen > rvsize) {
114 if(arclen > (rvsize + CHAR_BIT)) {
115 errno = ERANGE; /* Overflow */
116 return -1;
117 } else {
118 /*
119 * Even if the number of bits in the arc representation
120 * is higher than the width of supplied * return value
121 * type, there is still possible to fit it when there
122 * are few unused high bits in the arc value
123 * representaion.
vlm12557712004-06-17 23:43:39 +0000124 *
125 * Moreover, there is a possibility that the
126 * number could actually fit the arc space, given
127 * that add is negative, but we don't handle
128 * such "temporary lack of precision" situation here.
129 * May be considered as a bug.
vlm2e3dd3b2004-06-14 07:24:36 +0000130 */
131 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
132 if((*arcbuf & mask)) {
vlmfa67ddc2004-06-03 03:38:44 +0000133 errno = ERANGE; /* Overflow */
134 return -1;
135 }
vlm2e3dd3b2004-06-14 07:24:36 +0000136 /* Fool the routine computing unused bits */
137 arclen -= 7;
138 cache = *arcbuf & 0x7f;
139 arcbuf++;
140 }
141 }
142
vlm3717fb32004-06-14 08:17:27 +0000143 /* Faster path for common size */
144 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
145 unsigned long accum;
146 /* Gather all bits into the accumulator */
147 for(accum = cache; arcbuf < arcend; arcbuf++)
148 accum = (accum << 7) | (*arcbuf & ~0x80);
149 if(accum < (unsigned)-add) {
150 errno = ERANGE; /* Overflow */
151 return -1;
152 }
153 *(unsigned long *)rvbuf = accum + add;
154 return 0;
155 }
156
vlm2e3dd3b2004-06-14 07:24:36 +0000157#ifndef WORDS_BIGENDIAN
158 if(*(unsigned char *)&LE) { /* Little endian (x86) */
159 /* "Convert" to big endian */
vlm1ff928d2004-08-11 08:10:13 +0000160 (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
vlm3717fb32004-06-14 08:17:27 +0000161 ((unsigned char *)rvstart)--;
vlm2e3dd3b2004-06-14 07:24:36 +0000162 inc = -1; /* Descending */
vlm3717fb32004-06-14 08:17:27 +0000163 } else
vlm2e3dd3b2004-06-14 07:24:36 +0000164#endif /* !WORDS_BIGENDIAN */
vlm3717fb32004-06-14 08:17:27 +0000165 inc = +1; /* Big endian is known [at compile time] */
vlm2e3dd3b2004-06-14 07:24:36 +0000166
vlm3717fb32004-06-14 08:17:27 +0000167 {
vlm12557712004-06-17 23:43:39 +0000168 int bits; /* typically no more than 3-4 bits */
vlm3717fb32004-06-14 08:17:27 +0000169
vlm2e3dd3b2004-06-14 07:24:36 +0000170 /* Clear the high unused bits */
171 for(bits = rvsize - arclen;
172 bits > CHAR_BIT;
vlm1ff928d2004-08-11 08:10:13 +0000173 (unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
vlm2e3dd3b2004-06-14 07:24:36 +0000174 *(unsigned char *)rvbuf = 0;
vlm3717fb32004-06-14 08:17:27 +0000175
vlm2e3dd3b2004-06-14 07:24:36 +0000176 /* Fill the body of a value */
177 for(; arcbuf < arcend; arcbuf++) {
178 cache = (cache << 7) | (*arcbuf & 0x7f);
179 bits += 7;
180 if(bits >= CHAR_BIT) {
181 bits -= CHAR_BIT;
182 *(unsigned char *)rvbuf = (cache >> bits);
vlm1ff928d2004-08-11 08:10:13 +0000183 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000184 }
185 }
186 if(bits) {
187 *(unsigned char *)rvbuf = cache;
vlm1ff928d2004-08-11 08:10:13 +0000188 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000189 }
190 }
191
192 if(add) {
vlm1ff928d2004-08-11 08:10:13 +0000193 for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
vlm2e3dd3b2004-06-14 07:24:36 +0000194 int v = add + *(unsigned char *)rvbuf;
195 if(v & (-1 << CHAR_BIT)) {
196 *(unsigned char *)rvbuf
vlm754284a2004-08-11 09:17:15 +0000197 = (unsigned char)(v + (1 << CHAR_BIT));
vlm2e3dd3b2004-06-14 07:24:36 +0000198 add = -1;
199 } else {
200 *(unsigned char *)rvbuf = v;
201 break;
202 }
203 }
204 if(rvbuf == rvstart) {
205 /* No space to carry over */
vlmfa67ddc2004-06-03 03:38:44 +0000206 errno = ERANGE; /* Overflow */
207 return -1;
208 }
209 }
210
vlmfa67ddc2004-06-03 03:38:44 +0000211 return 0;
212}
213
vlm2e3dd3b2004-06-14 07:24:36 +0000214
vlmfa67ddc2004-06-03 03:38:44 +0000215int
216OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
217 asn_app_consume_bytes_f *cb, void *app_key) {
218 char scratch[64]; /* Conservative estimate */
219 unsigned long accum; /* Bits accumulator */
220 char *p; /* Position in the scratch buffer */
221
vlm2e3dd3b2004-06-14 07:24:36 +0000222 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
223 &accum, sizeof(accum)))
vlmfa67ddc2004-06-03 03:38:44 +0000224 return -1;
225
226 /* Fill the scratch buffer in reverse. */
227 p = scratch + sizeof(scratch);
228 for(; accum; accum /= 10)
vlm754284a2004-08-11 09:17:15 +0000229 *(--p) = (char)(accum % 10) + 0x30;
vlmfa67ddc2004-06-03 03:38:44 +0000230
231 return cb(p, sizeof(scratch) - (p - scratch), app_key);
232}
233
234int
235OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
236 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000237 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000238 int startn;
239 int add = 0;
240 int i;
241
vlmb42843a2004-06-05 08:17:50 +0000242 (void)td; /* Unused argument */
243 (void)ilevel; /* Unused argument */
244
vlmfa67ddc2004-06-03 03:38:44 +0000245 if(!st || !st->buf)
246 return cb("<absent>", 8, app_key);
247
248 /* Dump preamble */
249 if(cb("{ ", 2, app_key))
250 return -1;
251
252 for(i = 0, startn = 0; i < st->size; i++) {
253 uint8_t b = st->buf[i];
254 if((b & 0x80)) /* Continuation expected */
255 continue;
256
257 if(startn == 0) {
258 /*
259 * First two arcs are encoded through the backdoor.
260 */
261 if(i) {
262 add = -80;
263 if(cb("2", 1, app_key)) return -1;
264 } else if(b <= 39) {
265 add = 0;
266 if(cb("0", 1, app_key)) return -1;
267 } else if(b < 79) {
268 add = -40;
269 if(cb("1", 1, app_key)) return -1;
270 } else {
271 add = -80;
272 if(cb("2", 1, app_key)) return -1;
273 }
274 }
275
276 if(cb(" ", 1, app_key)) /* Separate arcs */
277 return -1;
278
279 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
280 i - startn + 1, add,
281 cb, app_key))
282 return -1;
283 startn = i + 1;
284 add = 0;
285 }
286
287 return cb(" }", 2, app_key);
288}
289
290int
vlm2e3dd3b2004-06-14 07:24:36 +0000291OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
292 unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000293 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
vlm2e3dd3b2004-06-14 07:24:36 +0000294 int num_arcs = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000295 int startn = 0;
296 int add = 0;
297 int i;
298
vlm12557712004-06-17 23:43:39 +0000299 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000300 errno = EINVAL;
301 return -1;
302 }
303
304 for(i = 0; i < oid->size; i++) {
305 uint8_t b = oid->buf[i];
306 if((b & 0x80)) /* Continuation expected */
307 continue;
308
vlm2e3dd3b2004-06-14 07:24:36 +0000309 if(num_arcs == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000310 /*
311 * First two arcs are encoded through the backdoor.
312 */
vlm2e3dd3b2004-06-14 07:24:36 +0000313 unsigned LE = 1; /* Little endian */
314 int first_arc;
315 num_arcs++;
316 if(!arc_slots) { num_arcs++; continue; }
317
318 if(i) first_arc = 2;
319 else if(b <= 39) first_arc = 0;
320 else if(b < 79) first_arc = 1;
321 else first_arc = 2;
322
323 add = -40 * first_arc;
324 memset(arcs, 0, arc_type_size);
vlm1ff928d2004-08-11 08:10:13 +0000325 *(unsigned char *)((char *)arcs
vlm2e3dd3b2004-06-14 07:24:36 +0000326 + ((*(char *)&LE)?0:(arc_type_size - 1)))
327 = first_arc;
vlm1ff928d2004-08-11 08:10:13 +0000328 (char *)arcs += arc_type_size;
vlmfa67ddc2004-06-03 03:38:44 +0000329 }
330
vlm2e3dd3b2004-06-14 07:24:36 +0000331 /* Decode, if has space */
332 if(arcs < arcs_end) {
333 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
334 i - startn + 1, add,
335 arcs, arc_type_size))
336 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000337 startn = i + 1;
vlm1ff928d2004-08-11 08:10:13 +0000338 (char *)arcs += arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000339 add = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000340 }
vlm2e3dd3b2004-06-14 07:24:36 +0000341 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000342 }
343
vlm2e3dd3b2004-06-14 07:24:36 +0000344 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000345}
346
vlm12557712004-06-17 23:43:39 +0000347
348/*
349 * Save the single value as an object identifier arc.
350 */
vlme55716a2004-08-11 09:10:59 +0000351int
vlm12557712004-06-17 23:43:39 +0000352OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
353 /*
354 * The following conditions must hold:
355 * assert(arcval);
356 * assert(arcval_size > 0);
357 * assert(arcbuf);
358 */
359#ifdef WORDS_BIGENDIAN
360 const unsigned isLittleEndian = 0;
361#else
362 unsigned LE = 1;
363 unsigned isLittleEndian = *(char *)&LE;
364#endif
vlm12557712004-06-17 23:43:39 +0000365 uint8_t *tp, *tend;
366 unsigned int cache;
367 uint8_t *bp = arcbuf;
368 int bits;
vlm6e73a042004-08-11 07:17:22 +0000369#ifdef __GNUC__
370 uint8_t buffer[arcval_size];
371#else
372 uint8_t *buffer = alloca(arcval_size);
vlm7a6a60e2004-08-11 07:41:45 +0000373 if(!buffer) { errno = ENOMEM; return -1; }
vlm6e73a042004-08-11 07:17:22 +0000374#endif
vlm12557712004-06-17 23:43:39 +0000375
376 if(isLittleEndian && !prepared_order) {
vlm1ff928d2004-08-11 08:10:13 +0000377 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
vlmda674682004-08-11 09:07:36 +0000378 uint8_t *aend = (uint8_t *)arcval;
vlm12557712004-06-17 23:43:39 +0000379 uint8_t *msb = buffer + arcval_size - 1;
380 for(tp = buffer; a >= aend; tp++, a--)
381 if((*tp = *a) && (tp < msb))
382 msb = tp;
383 tend = &buffer[arcval_size];
384 tp = msb; /* Most significant non-zero byte */
385 } else {
386 /* Look for most significant non-zero byte */
vlm1ff928d2004-08-11 08:10:13 +0000387 tend = (unsigned char *)arcval + arcval_size;
vlmda674682004-08-11 09:07:36 +0000388 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
vlm12557712004-06-17 23:43:39 +0000389 if(*tp) break;
390 }
391
392 /*
393 * Split the value in 7-bits chunks.
394 */
395 bits = ((tend - tp) * CHAR_BIT) % 7;
396 if(bits) {
397 cache = *tp >> (CHAR_BIT - bits);
398 if(cache) {
399 *bp++ = cache | 0x80;
400 cache = *tp++;
401 bits = CHAR_BIT - bits;
402 } else {
403 bits = -bits;
404 }
405 } else {
406 cache = 0;
407 }
408 for(; tp < tend; tp++) {
409 cache = (cache << CHAR_BIT) + *tp;
410 bits += CHAR_BIT;
411 while(bits >= 7) {
412 bits -= 7;
413 *bp++ = 0x80 | (cache >> bits);
414 }
415 }
416 if(bits) *bp++ = cache;
417 bp[-1] &= 0x7f; /* Clear the last bit */
418
419 return bp - arcbuf;
420}
421
vlmfa67ddc2004-06-03 03:38:44 +0000422int
vlm12557712004-06-17 23:43:39 +0000423OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000424 uint8_t *buf;
425 uint8_t *bp;
vlm12557712004-06-17 23:43:39 +0000426 unsigned LE = 1; /* Little endian (x86) */
427 unsigned isLittleEndian = *((char *)&LE);
428 unsigned int arc0;
429 unsigned int arc1;
430 unsigned size;
vlm3717fb32004-06-14 08:17:27 +0000431 unsigned i;
vlmfa67ddc2004-06-03 03:38:44 +0000432
vlm12557712004-06-17 23:43:39 +0000433 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000434 errno = EINVAL;
435 return -1;
436 }
437
vlm12557712004-06-17 23:43:39 +0000438 switch(arc_type_size) {
439 case sizeof(char):
440 arc0 = ((unsigned char *)arcs)[0];
441 arc1 = ((unsigned char *)arcs)[1];
442 break;
443 case sizeof(short):
444 arc0 = ((unsigned short *)arcs)[0];
445 arc1 = ((unsigned short *)arcs)[1];
446 break;
447 case sizeof(int):
448 arc0 = ((unsigned int *)arcs)[0];
449 arc1 = ((unsigned int *)arcs)[1];
450 break;
451 default:
452 arc1 = arc0 = 0;
453 if(isLittleEndian) { /* Little endian (x86) */
454 unsigned char *ps, *pe;
455 /* If more significant bytes are present,
456 * make them > 255 quick */
vlm1ff928d2004-08-11 08:10:13 +0000457 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
458 ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000459 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
460 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
461 arc0 = *((unsigned char *)arcs + 0);
462 arc1 = *((unsigned char *)arcs + arc_type_size);
463 } else {
464 unsigned char *ps, *pe;
465 /* If more significant bytes are present,
466 * make them > 255 quick */
vlmda674682004-08-11 09:07:36 +0000467 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000468 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
469 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
470 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
471 }
472 }
473
474 /*
475 * The previous chapter left us with the first and the second arcs.
476 * The values are not precise (that is, they are valid only if
477 * they're less than 255), but OK for the purposes of making
478 * the sanity test below.
479 */
480 if(arc0 <= 1) {
481 if(arc1 >= 39) {
vlmfa67ddc2004-06-03 03:38:44 +0000482 /* 8.19.4: At most 39 subsequent values (including 0) */
483 errno = ERANGE;
484 return -1;
485 }
vlm12557712004-06-17 23:43:39 +0000486 } else if(arc0 > 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000487 /* 8.19.4: Only three values are allocated from the root node */
488 errno = ERANGE;
489 return -1;
490 }
vlm12557712004-06-17 23:43:39 +0000491 /*
492 * After above tests it is known that the value of arc0 is completely
493 * trustworthy (0..2). However, the arc1's value is still meaningless.
494 */
vlmfa67ddc2004-06-03 03:38:44 +0000495
496 /*
497 * Roughly estimate the maximum size necessary to encode these arcs.
vlm12557712004-06-17 23:43:39 +0000498 * This estimation implicitly takes in account the following facts,
499 * that cancel each other:
500 * * the first two arcs are encoded in a single value.
501 * * the first value may require more space (+1 byte)
502 * * the value of the first arc which is in range (0..2)
vlmfa67ddc2004-06-03 03:38:44 +0000503 */
vlm12557712004-06-17 23:43:39 +0000504 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
vlmda674682004-08-11 09:07:36 +0000505 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000506 if(!buf) {
507 /* ENOMEM */
508 return -1;
509 }
510
511 /*
vlm12557712004-06-17 23:43:39 +0000512 * Encode the first two arcs.
513 * These require special treatment.
vlmfa67ddc2004-06-03 03:38:44 +0000514 */
vlmfa67ddc2004-06-03 03:38:44 +0000515 {
vlm12557712004-06-17 23:43:39 +0000516 uint8_t *tp;
vlma51f7ce2004-08-11 07:48:19 +0000517#ifdef __GNUC__
518 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
vlm1ff928d2004-08-11 08:10:13 +0000519 uint8_t *fv = first_value;
vlma51f7ce2004-08-11 07:48:19 +0000520#else
521 uint8_t *first_value = alloca(1 + arc_type_size);
vlma51f7ce2004-08-11 07:48:19 +0000522 uint8_t *fv = first_value;
vlm1ff928d2004-08-11 08:10:13 +0000523 if(!first_value) {
524 errno = ENOMEM;
525 return -1;
526 }
527#endif
vlmfa67ddc2004-06-03 03:38:44 +0000528
vlm12557712004-06-17 23:43:39 +0000529 /*
530 * Simulate first_value = arc0 * 40 + arc1;
531 */
532 /* Copy the second (1'st) arcs[1] into the first_value */
533 *fv++ = 0;
534 (char *)arcs += arc_type_size;
535 if(isLittleEndian) {
vlm1ff928d2004-08-11 08:10:13 +0000536 uint8_t *aend = (unsigned char *)arcs - 1;
537 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
vlm12557712004-06-17 23:43:39 +0000538 for(; a1 > aend; fv++, a1--) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000539 } else {
vlmda674682004-08-11 09:07:36 +0000540 uint8_t *a1 = (uint8_t *)arcs;
vlm12557712004-06-17 23:43:39 +0000541 uint8_t *aend = a1 + arc_type_size;
542 for(; a1 < aend; fv++, a1++) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000543 }
vlm12557712004-06-17 23:43:39 +0000544 /* Increase the first_value by arc0 */
545 arc0 *= 40; /* (0..80) */
546 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
547 unsigned int v = *tp;
548 v += arc0;
549 *tp = v;
550 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
551 else break;
552 }
553
554 assert(tp >= first_value);
555
556 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
557 fv - first_value, 1);
558 }
559
560 /*
561 * Save the rest of arcs.
562 */
563 for((char *)arcs += arc_type_size, i = 2;
564 i < arc_slots; i++, (char *)arcs += arc_type_size) {
565 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
566 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000567 }
568
vlma63e0292004-06-17 23:46:45 +0000569 assert((unsigned)(bp - buf) <= size);
vlmfa67ddc2004-06-03 03:38:44 +0000570
571 /*
572 * Replace buffer.
573 */
vlm12557712004-06-17 23:43:39 +0000574 oid->size = bp - buf;
vlmfa67ddc2004-06-03 03:38:44 +0000575 bp = oid->buf;
576 oid->buf = buf;
577 if(bp) FREEMEM(bp);
578
579 return 0;
580}
vlm3717fb32004-06-14 08:17:27 +0000581