blob: b428acad30d24f7307b980933f6df7e5e85e3cfa [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 */
29 0 /* No specifics */
vlmfa67ddc2004-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;
vlmda674682004-08-11 09:07:36 +000041 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr;
vlmfa67ddc2004-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) {
vlmda674682004-08-11 09:07:36 +000078 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000079
80 if(st && st->buf) {
81 if(st->size < 1) {
82 _ASN_ERRLOG("%s: at least one numerical value expected",
83 td->name);
84 return -1;
85 }
86 } else {
87 _ASN_ERRLOG("%s: value not given", td->name);
88 return -1;
89 }
90
91 return 0;
92}
93
vlm3717fb32004-06-14 08:17:27 +000094
vlmfa67ddc2004-06-03 03:38:44 +000095int
vlm2e3dd3b2004-06-14 07:24:36 +000096OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
97 unsigned LE = 1; /* Little endian (x86) */
98 uint8_t *arcend = arcbuf + arclen; /* End of arc */
99 void *rvstart = rvbuf; /* Original start of the value buffer */
100 unsigned int cache = 0; /* No more than 14 significant bits */
101 int inc; /* Return value growth direction */
vlmfa67ddc2004-06-03 03:38:44 +0000102
vlm2e3dd3b2004-06-14 07:24:36 +0000103 rvsize *= CHAR_BIT; /* bytes to bits */
104 arclen *= 7; /* bytes to bits */
105
106 /*
107 * The arc has the number of bits
108 * cannot be represented using supplied return value type.
109 */
110 if(arclen > rvsize) {
111 if(arclen > (rvsize + CHAR_BIT)) {
112 errno = ERANGE; /* Overflow */
113 return -1;
114 } else {
115 /*
116 * Even if the number of bits in the arc representation
117 * is higher than the width of supplied * return value
118 * type, there is still possible to fit it when there
119 * are few unused high bits in the arc value
120 * representaion.
vlm12557712004-06-17 23:43:39 +0000121 *
122 * Moreover, there is a possibility that the
123 * number could actually fit the arc space, given
124 * that add is negative, but we don't handle
125 * such "temporary lack of precision" situation here.
126 * May be considered as a bug.
vlm2e3dd3b2004-06-14 07:24:36 +0000127 */
128 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
129 if((*arcbuf & mask)) {
vlmfa67ddc2004-06-03 03:38:44 +0000130 errno = ERANGE; /* Overflow */
131 return -1;
132 }
vlm2e3dd3b2004-06-14 07:24:36 +0000133 /* Fool the routine computing unused bits */
134 arclen -= 7;
135 cache = *arcbuf & 0x7f;
136 arcbuf++;
137 }
138 }
139
vlm3717fb32004-06-14 08:17:27 +0000140 /* Faster path for common size */
141 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
142 unsigned long accum;
143 /* Gather all bits into the accumulator */
144 for(accum = cache; arcbuf < arcend; arcbuf++)
145 accum = (accum << 7) | (*arcbuf & ~0x80);
146 if(accum < (unsigned)-add) {
147 errno = ERANGE; /* Overflow */
148 return -1;
149 }
150 *(unsigned long *)rvbuf = accum + add;
151 return 0;
152 }
153
vlm2e3dd3b2004-06-14 07:24:36 +0000154#ifndef WORDS_BIGENDIAN
155 if(*(unsigned char *)&LE) { /* Little endian (x86) */
156 /* "Convert" to big endian */
vlm1ff928d2004-08-11 08:10:13 +0000157 (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
vlm3717fb32004-06-14 08:17:27 +0000158 ((unsigned char *)rvstart)--;
vlm2e3dd3b2004-06-14 07:24:36 +0000159 inc = -1; /* Descending */
vlm3717fb32004-06-14 08:17:27 +0000160 } else
vlm2e3dd3b2004-06-14 07:24:36 +0000161#endif /* !WORDS_BIGENDIAN */
vlm3717fb32004-06-14 08:17:27 +0000162 inc = +1; /* Big endian is known [at compile time] */
vlm2e3dd3b2004-06-14 07:24:36 +0000163
vlm3717fb32004-06-14 08:17:27 +0000164 {
vlm12557712004-06-17 23:43:39 +0000165 int bits; /* typically no more than 3-4 bits */
vlm3717fb32004-06-14 08:17:27 +0000166
vlm2e3dd3b2004-06-14 07:24:36 +0000167 /* Clear the high unused bits */
168 for(bits = rvsize - arclen;
169 bits > CHAR_BIT;
vlm1ff928d2004-08-11 08:10:13 +0000170 (unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
vlm2e3dd3b2004-06-14 07:24:36 +0000171 *(unsigned char *)rvbuf = 0;
vlm3717fb32004-06-14 08:17:27 +0000172
vlm2e3dd3b2004-06-14 07:24:36 +0000173 /* Fill the body of a value */
174 for(; arcbuf < arcend; arcbuf++) {
175 cache = (cache << 7) | (*arcbuf & 0x7f);
176 bits += 7;
177 if(bits >= CHAR_BIT) {
178 bits -= CHAR_BIT;
179 *(unsigned char *)rvbuf = (cache >> bits);
vlm1ff928d2004-08-11 08:10:13 +0000180 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000181 }
182 }
183 if(bits) {
184 *(unsigned char *)rvbuf = cache;
vlm1ff928d2004-08-11 08:10:13 +0000185 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000186 }
187 }
188
189 if(add) {
vlm1ff928d2004-08-11 08:10:13 +0000190 for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
vlm2e3dd3b2004-06-14 07:24:36 +0000191 int v = add + *(unsigned char *)rvbuf;
192 if(v & (-1 << CHAR_BIT)) {
193 *(unsigned char *)rvbuf
194 = v + (1 << CHAR_BIT);
195 add = -1;
196 } else {
197 *(unsigned char *)rvbuf = v;
198 break;
199 }
200 }
201 if(rvbuf == rvstart) {
202 /* No space to carry over */
vlmfa67ddc2004-06-03 03:38:44 +0000203 errno = ERANGE; /* Overflow */
204 return -1;
205 }
206 }
207
vlmfa67ddc2004-06-03 03:38:44 +0000208 return 0;
209}
210
vlm2e3dd3b2004-06-14 07:24:36 +0000211
vlmfa67ddc2004-06-03 03:38:44 +0000212int
213OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
214 asn_app_consume_bytes_f *cb, void *app_key) {
215 char scratch[64]; /* Conservative estimate */
216 unsigned long accum; /* Bits accumulator */
217 char *p; /* Position in the scratch buffer */
218
vlm2e3dd3b2004-06-14 07:24:36 +0000219 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
220 &accum, sizeof(accum)))
vlmfa67ddc2004-06-03 03:38:44 +0000221 return -1;
222
223 /* Fill the scratch buffer in reverse. */
224 p = scratch + sizeof(scratch);
225 for(; accum; accum /= 10)
226 *(--p) = (accum % 10) + 0x30;
227
228 return cb(p, sizeof(scratch) - (p - scratch), app_key);
229}
230
231int
232OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
233 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000234 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000235 int startn;
236 int add = 0;
237 int i;
238
vlmb42843a2004-06-05 08:17:50 +0000239 (void)td; /* Unused argument */
240 (void)ilevel; /* Unused argument */
241
vlmfa67ddc2004-06-03 03:38:44 +0000242 if(!st || !st->buf)
243 return cb("<absent>", 8, app_key);
244
245 /* Dump preamble */
246 if(cb("{ ", 2, app_key))
247 return -1;
248
249 for(i = 0, startn = 0; i < st->size; i++) {
250 uint8_t b = st->buf[i];
251 if((b & 0x80)) /* Continuation expected */
252 continue;
253
254 if(startn == 0) {
255 /*
256 * First two arcs are encoded through the backdoor.
257 */
258 if(i) {
259 add = -80;
260 if(cb("2", 1, app_key)) return -1;
261 } else if(b <= 39) {
262 add = 0;
263 if(cb("0", 1, app_key)) return -1;
264 } else if(b < 79) {
265 add = -40;
266 if(cb("1", 1, app_key)) return -1;
267 } else {
268 add = -80;
269 if(cb("2", 1, app_key)) return -1;
270 }
271 }
272
273 if(cb(" ", 1, app_key)) /* Separate arcs */
274 return -1;
275
276 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
277 i - startn + 1, add,
278 cb, app_key))
279 return -1;
280 startn = i + 1;
281 add = 0;
282 }
283
284 return cb(" }", 2, app_key);
285}
286
287int
vlm2e3dd3b2004-06-14 07:24:36 +0000288OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
289 unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000290 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
vlm2e3dd3b2004-06-14 07:24:36 +0000291 int num_arcs = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000292 int startn = 0;
293 int add = 0;
294 int i;
295
vlm12557712004-06-17 23:43:39 +0000296 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000297 errno = EINVAL;
298 return -1;
299 }
300
301 for(i = 0; i < oid->size; i++) {
302 uint8_t b = oid->buf[i];
303 if((b & 0x80)) /* Continuation expected */
304 continue;
305
vlm2e3dd3b2004-06-14 07:24:36 +0000306 if(num_arcs == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000307 /*
308 * First two arcs are encoded through the backdoor.
309 */
vlm2e3dd3b2004-06-14 07:24:36 +0000310 unsigned LE = 1; /* Little endian */
311 int first_arc;
312 num_arcs++;
313 if(!arc_slots) { num_arcs++; continue; }
314
315 if(i) first_arc = 2;
316 else if(b <= 39) first_arc = 0;
317 else if(b < 79) first_arc = 1;
318 else first_arc = 2;
319
320 add = -40 * first_arc;
321 memset(arcs, 0, arc_type_size);
vlm1ff928d2004-08-11 08:10:13 +0000322 *(unsigned char *)((char *)arcs
vlm2e3dd3b2004-06-14 07:24:36 +0000323 + ((*(char *)&LE)?0:(arc_type_size - 1)))
324 = first_arc;
vlm1ff928d2004-08-11 08:10:13 +0000325 (char *)arcs += arc_type_size;
vlmfa67ddc2004-06-03 03:38:44 +0000326 }
327
vlm2e3dd3b2004-06-14 07:24:36 +0000328 /* Decode, if has space */
329 if(arcs < arcs_end) {
330 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
331 i - startn + 1, add,
332 arcs, arc_type_size))
333 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000334 startn = i + 1;
vlm1ff928d2004-08-11 08:10:13 +0000335 (char *)arcs += arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000336 add = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000337 }
vlm2e3dd3b2004-06-14 07:24:36 +0000338 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000339 }
340
vlm2e3dd3b2004-06-14 07:24:36 +0000341 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000342}
343
vlm12557712004-06-17 23:43:39 +0000344
345/*
346 * Save the single value as an object identifier arc.
347 */
348inline int
349OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
350 /*
351 * The following conditions must hold:
352 * assert(arcval);
353 * assert(arcval_size > 0);
354 * assert(arcbuf);
355 */
356#ifdef WORDS_BIGENDIAN
357 const unsigned isLittleEndian = 0;
358#else
359 unsigned LE = 1;
360 unsigned isLittleEndian = *(char *)&LE;
361#endif
vlm12557712004-06-17 23:43:39 +0000362 uint8_t *tp, *tend;
363 unsigned int cache;
364 uint8_t *bp = arcbuf;
365 int bits;
vlm6e73a042004-08-11 07:17:22 +0000366#ifdef __GNUC__
367 uint8_t buffer[arcval_size];
368#else
369 uint8_t *buffer = alloca(arcval_size);
vlm7a6a60e2004-08-11 07:41:45 +0000370 if(!buffer) { errno = ENOMEM; return -1; }
vlm6e73a042004-08-11 07:17:22 +0000371#endif
vlm12557712004-06-17 23:43:39 +0000372
373 if(isLittleEndian && !prepared_order) {
vlm1ff928d2004-08-11 08:10:13 +0000374 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
vlmda674682004-08-11 09:07:36 +0000375 uint8_t *aend = (uint8_t *)arcval;
vlm12557712004-06-17 23:43:39 +0000376 uint8_t *msb = buffer + arcval_size - 1;
377 for(tp = buffer; a >= aend; tp++, a--)
378 if((*tp = *a) && (tp < msb))
379 msb = tp;
380 tend = &buffer[arcval_size];
381 tp = msb; /* Most significant non-zero byte */
382 } else {
383 /* Look for most significant non-zero byte */
vlm1ff928d2004-08-11 08:10:13 +0000384 tend = (unsigned char *)arcval + arcval_size;
vlmda674682004-08-11 09:07:36 +0000385 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
vlm12557712004-06-17 23:43:39 +0000386 if(*tp) break;
387 }
388
389 /*
390 * Split the value in 7-bits chunks.
391 */
392 bits = ((tend - tp) * CHAR_BIT) % 7;
393 if(bits) {
394 cache = *tp >> (CHAR_BIT - bits);
395 if(cache) {
396 *bp++ = cache | 0x80;
397 cache = *tp++;
398 bits = CHAR_BIT - bits;
399 } else {
400 bits = -bits;
401 }
402 } else {
403 cache = 0;
404 }
405 for(; tp < tend; tp++) {
406 cache = (cache << CHAR_BIT) + *tp;
407 bits += CHAR_BIT;
408 while(bits >= 7) {
409 bits -= 7;
410 *bp++ = 0x80 | (cache >> bits);
411 }
412 }
413 if(bits) *bp++ = cache;
414 bp[-1] &= 0x7f; /* Clear the last bit */
415
416 return bp - arcbuf;
417}
418
vlmfa67ddc2004-06-03 03:38:44 +0000419int
vlm12557712004-06-17 23:43:39 +0000420OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000421 uint8_t *buf;
422 uint8_t *bp;
vlm12557712004-06-17 23:43:39 +0000423 unsigned LE = 1; /* Little endian (x86) */
424 unsigned isLittleEndian = *((char *)&LE);
425 unsigned int arc0;
426 unsigned int arc1;
427 unsigned size;
vlm3717fb32004-06-14 08:17:27 +0000428 unsigned i;
vlmfa67ddc2004-06-03 03:38:44 +0000429
vlm12557712004-06-17 23:43:39 +0000430 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000431 errno = EINVAL;
432 return -1;
433 }
434
vlm12557712004-06-17 23:43:39 +0000435 switch(arc_type_size) {
436 case sizeof(char):
437 arc0 = ((unsigned char *)arcs)[0];
438 arc1 = ((unsigned char *)arcs)[1];
439 break;
440 case sizeof(short):
441 arc0 = ((unsigned short *)arcs)[0];
442 arc1 = ((unsigned short *)arcs)[1];
443 break;
444 case sizeof(int):
445 arc0 = ((unsigned int *)arcs)[0];
446 arc1 = ((unsigned int *)arcs)[1];
447 break;
448 default:
449 arc1 = arc0 = 0;
450 if(isLittleEndian) { /* Little endian (x86) */
451 unsigned char *ps, *pe;
452 /* If more significant bytes are present,
453 * make them > 255 quick */
vlm1ff928d2004-08-11 08:10:13 +0000454 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
455 ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000456 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
457 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
458 arc0 = *((unsigned char *)arcs + 0);
459 arc1 = *((unsigned char *)arcs + arc_type_size);
460 } else {
461 unsigned char *ps, *pe;
462 /* If more significant bytes are present,
463 * make them > 255 quick */
vlmda674682004-08-11 09:07:36 +0000464 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000465 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
466 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
467 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
468 }
469 }
470
471 /*
472 * The previous chapter left us with the first and the second arcs.
473 * The values are not precise (that is, they are valid only if
474 * they're less than 255), but OK for the purposes of making
475 * the sanity test below.
476 */
477 if(arc0 <= 1) {
478 if(arc1 >= 39) {
vlmfa67ddc2004-06-03 03:38:44 +0000479 /* 8.19.4: At most 39 subsequent values (including 0) */
480 errno = ERANGE;
481 return -1;
482 }
vlm12557712004-06-17 23:43:39 +0000483 } else if(arc0 > 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000484 /* 8.19.4: Only three values are allocated from the root node */
485 errno = ERANGE;
486 return -1;
487 }
vlm12557712004-06-17 23:43:39 +0000488 /*
489 * After above tests it is known that the value of arc0 is completely
490 * trustworthy (0..2). However, the arc1's value is still meaningless.
491 */
vlmfa67ddc2004-06-03 03:38:44 +0000492
493 /*
494 * Roughly estimate the maximum size necessary to encode these arcs.
vlm12557712004-06-17 23:43:39 +0000495 * This estimation implicitly takes in account the following facts,
496 * that cancel each other:
497 * * the first two arcs are encoded in a single value.
498 * * the first value may require more space (+1 byte)
499 * * the value of the first arc which is in range (0..2)
vlmfa67ddc2004-06-03 03:38:44 +0000500 */
vlm12557712004-06-17 23:43:39 +0000501 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
vlmda674682004-08-11 09:07:36 +0000502 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000503 if(!buf) {
504 /* ENOMEM */
505 return -1;
506 }
507
508 /*
vlm12557712004-06-17 23:43:39 +0000509 * Encode the first two arcs.
510 * These require special treatment.
vlmfa67ddc2004-06-03 03:38:44 +0000511 */
vlmfa67ddc2004-06-03 03:38:44 +0000512 {
vlm12557712004-06-17 23:43:39 +0000513 uint8_t *tp;
vlma51f7ce2004-08-11 07:48:19 +0000514#ifdef __GNUC__
515 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
vlm1ff928d2004-08-11 08:10:13 +0000516 uint8_t *fv = first_value;
vlma51f7ce2004-08-11 07:48:19 +0000517#else
518 uint8_t *first_value = alloca(1 + arc_type_size);
vlma51f7ce2004-08-11 07:48:19 +0000519 uint8_t *fv = first_value;
vlm1ff928d2004-08-11 08:10:13 +0000520 if(!first_value) {
521 errno = ENOMEM;
522 return -1;
523 }
524#endif
vlmfa67ddc2004-06-03 03:38:44 +0000525
vlm12557712004-06-17 23:43:39 +0000526 /*
527 * Simulate first_value = arc0 * 40 + arc1;
528 */
529 /* Copy the second (1'st) arcs[1] into the first_value */
530 *fv++ = 0;
531 (char *)arcs += arc_type_size;
532 if(isLittleEndian) {
vlm1ff928d2004-08-11 08:10:13 +0000533 uint8_t *aend = (unsigned char *)arcs - 1;
534 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
vlm12557712004-06-17 23:43:39 +0000535 for(; a1 > aend; fv++, a1--) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000536 } else {
vlmda674682004-08-11 09:07:36 +0000537 uint8_t *a1 = (uint8_t *)arcs;
vlm12557712004-06-17 23:43:39 +0000538 uint8_t *aend = a1 + arc_type_size;
539 for(; a1 < aend; fv++, a1++) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000540 }
vlm12557712004-06-17 23:43:39 +0000541 /* Increase the first_value by arc0 */
542 arc0 *= 40; /* (0..80) */
543 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
544 unsigned int v = *tp;
545 v += arc0;
546 *tp = v;
547 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
548 else break;
549 }
550
551 assert(tp >= first_value);
552
553 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
554 fv - first_value, 1);
555 }
556
557 /*
558 * Save the rest of arcs.
559 */
560 for((char *)arcs += arc_type_size, i = 2;
561 i < arc_slots; i++, (char *)arcs += arc_type_size) {
562 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
563 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000564 }
565
vlma63e0292004-06-17 23:46:45 +0000566 assert((unsigned)(bp - buf) <= size);
vlmfa67ddc2004-06-03 03:38:44 +0000567
568 /*
569 * Replace buffer.
570 */
vlm12557712004-06-17 23:43:39 +0000571 oid->size = bp - buf;
vlmfa67ddc2004-06-03 03:38:44 +0000572 bp = oid->buf;
573 oid->buf = buf;
574 if(bp) FREEMEM(bp);
575
576 return 0;
577}
vlm3717fb32004-06-14 08:17:27 +0000578