blob: 46191bc64b1150a5e65e40808fabbb96719ef1e5 [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,
vlm758530a2004-08-22 13:47:59 +000084 "%s: at least one numerical value "
85 "expected (%s:%d)",
86 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000087 return -1;
88 }
89 } else {
vlme3f0f282004-08-11 09:44:13 +000090 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000091 "%s: value not given (%s:%d)",
92 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000093 return -1;
94 }
95
96 return 0;
97}
98
vlm3717fb32004-06-14 08:17:27 +000099
vlmfa67ddc2004-06-03 03:38:44 +0000100int
vlm2e3dd3b2004-06-14 07:24:36 +0000101OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
102 unsigned LE = 1; /* Little endian (x86) */
103 uint8_t *arcend = arcbuf + arclen; /* End of arc */
104 void *rvstart = rvbuf; /* Original start of the value buffer */
105 unsigned int cache = 0; /* No more than 14 significant bits */
106 int inc; /* Return value growth direction */
vlmfa67ddc2004-06-03 03:38:44 +0000107
vlm2e3dd3b2004-06-14 07:24:36 +0000108 rvsize *= CHAR_BIT; /* bytes to bits */
109 arclen *= 7; /* bytes to bits */
110
111 /*
112 * The arc has the number of bits
113 * cannot be represented using supplied return value type.
114 */
115 if(arclen > rvsize) {
116 if(arclen > (rvsize + CHAR_BIT)) {
117 errno = ERANGE; /* Overflow */
118 return -1;
119 } else {
120 /*
121 * Even if the number of bits in the arc representation
122 * is higher than the width of supplied * return value
123 * type, there is still possible to fit it when there
124 * are few unused high bits in the arc value
125 * representaion.
vlm12557712004-06-17 23:43:39 +0000126 *
127 * Moreover, there is a possibility that the
128 * number could actually fit the arc space, given
129 * that add is negative, but we don't handle
130 * such "temporary lack of precision" situation here.
131 * May be considered as a bug.
vlm2e3dd3b2004-06-14 07:24:36 +0000132 */
133 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
134 if((*arcbuf & mask)) {
vlmfa67ddc2004-06-03 03:38:44 +0000135 errno = ERANGE; /* Overflow */
136 return -1;
137 }
vlm2e3dd3b2004-06-14 07:24:36 +0000138 /* Fool the routine computing unused bits */
139 arclen -= 7;
140 cache = *arcbuf & 0x7f;
141 arcbuf++;
142 }
143 }
144
vlm3717fb32004-06-14 08:17:27 +0000145 /* Faster path for common size */
146 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
147 unsigned long accum;
148 /* Gather all bits into the accumulator */
149 for(accum = cache; arcbuf < arcend; arcbuf++)
150 accum = (accum << 7) | (*arcbuf & ~0x80);
151 if(accum < (unsigned)-add) {
152 errno = ERANGE; /* Overflow */
153 return -1;
154 }
155 *(unsigned long *)rvbuf = accum + add;
156 return 0;
157 }
158
vlm2e3dd3b2004-06-14 07:24:36 +0000159#ifndef WORDS_BIGENDIAN
160 if(*(unsigned char *)&LE) { /* Little endian (x86) */
161 /* "Convert" to big endian */
vlm1ff928d2004-08-11 08:10:13 +0000162 (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
vlm3717fb32004-06-14 08:17:27 +0000163 ((unsigned char *)rvstart)--;
vlm2e3dd3b2004-06-14 07:24:36 +0000164 inc = -1; /* Descending */
vlm3717fb32004-06-14 08:17:27 +0000165 } else
vlm2e3dd3b2004-06-14 07:24:36 +0000166#endif /* !WORDS_BIGENDIAN */
vlm3717fb32004-06-14 08:17:27 +0000167 inc = +1; /* Big endian is known [at compile time] */
vlm2e3dd3b2004-06-14 07:24:36 +0000168
vlm3717fb32004-06-14 08:17:27 +0000169 {
vlm12557712004-06-17 23:43:39 +0000170 int bits; /* typically no more than 3-4 bits */
vlm3717fb32004-06-14 08:17:27 +0000171
vlm2e3dd3b2004-06-14 07:24:36 +0000172 /* Clear the high unused bits */
173 for(bits = rvsize - arclen;
174 bits > CHAR_BIT;
vlm1ff928d2004-08-11 08:10:13 +0000175 (unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
vlm2e3dd3b2004-06-14 07:24:36 +0000176 *(unsigned char *)rvbuf = 0;
vlm3717fb32004-06-14 08:17:27 +0000177
vlm2e3dd3b2004-06-14 07:24:36 +0000178 /* Fill the body of a value */
179 for(; arcbuf < arcend; arcbuf++) {
180 cache = (cache << 7) | (*arcbuf & 0x7f);
181 bits += 7;
182 if(bits >= CHAR_BIT) {
183 bits -= CHAR_BIT;
184 *(unsigned char *)rvbuf = (cache >> bits);
vlm1ff928d2004-08-11 08:10:13 +0000185 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000186 }
187 }
188 if(bits) {
189 *(unsigned char *)rvbuf = cache;
vlm1ff928d2004-08-11 08:10:13 +0000190 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000191 }
192 }
193
194 if(add) {
vlm1ff928d2004-08-11 08:10:13 +0000195 for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
vlm2e3dd3b2004-06-14 07:24:36 +0000196 int v = add + *(unsigned char *)rvbuf;
197 if(v & (-1 << CHAR_BIT)) {
198 *(unsigned char *)rvbuf
vlm754284a2004-08-11 09:17:15 +0000199 = (unsigned char)(v + (1 << CHAR_BIT));
vlm2e3dd3b2004-06-14 07:24:36 +0000200 add = -1;
201 } else {
202 *(unsigned char *)rvbuf = v;
203 break;
204 }
205 }
206 if(rvbuf == rvstart) {
207 /* No space to carry over */
vlmfa67ddc2004-06-03 03:38:44 +0000208 errno = ERANGE; /* Overflow */
209 return -1;
210 }
211 }
212
vlmfa67ddc2004-06-03 03:38:44 +0000213 return 0;
214}
215
vlm2e3dd3b2004-06-14 07:24:36 +0000216
vlmfa67ddc2004-06-03 03:38:44 +0000217int
218OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
219 asn_app_consume_bytes_f *cb, void *app_key) {
220 char scratch[64]; /* Conservative estimate */
221 unsigned long accum; /* Bits accumulator */
222 char *p; /* Position in the scratch buffer */
223
vlm2e3dd3b2004-06-14 07:24:36 +0000224 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
225 &accum, sizeof(accum)))
vlmfa67ddc2004-06-03 03:38:44 +0000226 return -1;
227
228 /* Fill the scratch buffer in reverse. */
229 p = scratch + sizeof(scratch);
230 for(; accum; accum /= 10)
vlm754284a2004-08-11 09:17:15 +0000231 *(--p) = (char)(accum % 10) + 0x30;
vlmfa67ddc2004-06-03 03:38:44 +0000232
233 return cb(p, sizeof(scratch) - (p - scratch), app_key);
234}
235
236int
237OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
238 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000239 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000240 int startn;
241 int add = 0;
242 int i;
243
vlmb42843a2004-06-05 08:17:50 +0000244 (void)td; /* Unused argument */
245 (void)ilevel; /* Unused argument */
246
vlmfa67ddc2004-06-03 03:38:44 +0000247 if(!st || !st->buf)
248 return cb("<absent>", 8, app_key);
249
250 /* Dump preamble */
251 if(cb("{ ", 2, app_key))
252 return -1;
253
254 for(i = 0, startn = 0; i < st->size; i++) {
255 uint8_t b = st->buf[i];
256 if((b & 0x80)) /* Continuation expected */
257 continue;
258
259 if(startn == 0) {
260 /*
261 * First two arcs are encoded through the backdoor.
262 */
263 if(i) {
264 add = -80;
265 if(cb("2", 1, app_key)) return -1;
266 } else if(b <= 39) {
267 add = 0;
268 if(cb("0", 1, app_key)) return -1;
269 } else if(b < 79) {
270 add = -40;
271 if(cb("1", 1, app_key)) return -1;
272 } else {
273 add = -80;
274 if(cb("2", 1, app_key)) return -1;
275 }
276 }
277
278 if(cb(" ", 1, app_key)) /* Separate arcs */
279 return -1;
280
281 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
282 i - startn + 1, add,
283 cb, app_key))
284 return -1;
285 startn = i + 1;
286 add = 0;
287 }
288
289 return cb(" }", 2, app_key);
290}
291
292int
vlm2e3dd3b2004-06-14 07:24:36 +0000293OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
294 unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000295 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
vlm2e3dd3b2004-06-14 07:24:36 +0000296 int num_arcs = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000297 int startn = 0;
298 int add = 0;
299 int i;
300
vlm12557712004-06-17 23:43:39 +0000301 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000302 errno = EINVAL;
303 return -1;
304 }
305
306 for(i = 0; i < oid->size; i++) {
307 uint8_t b = oid->buf[i];
308 if((b & 0x80)) /* Continuation expected */
309 continue;
310
vlm2e3dd3b2004-06-14 07:24:36 +0000311 if(num_arcs == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000312 /*
313 * First two arcs are encoded through the backdoor.
314 */
vlm2e3dd3b2004-06-14 07:24:36 +0000315 unsigned LE = 1; /* Little endian */
316 int first_arc;
317 num_arcs++;
318 if(!arc_slots) { num_arcs++; continue; }
319
320 if(i) first_arc = 2;
321 else if(b <= 39) first_arc = 0;
322 else if(b < 79) first_arc = 1;
323 else first_arc = 2;
324
325 add = -40 * first_arc;
326 memset(arcs, 0, arc_type_size);
vlm1ff928d2004-08-11 08:10:13 +0000327 *(unsigned char *)((char *)arcs
vlm2e3dd3b2004-06-14 07:24:36 +0000328 + ((*(char *)&LE)?0:(arc_type_size - 1)))
329 = first_arc;
vlm1ff928d2004-08-11 08:10:13 +0000330 (char *)arcs += arc_type_size;
vlmfa67ddc2004-06-03 03:38:44 +0000331 }
332
vlm2e3dd3b2004-06-14 07:24:36 +0000333 /* Decode, if has space */
334 if(arcs < arcs_end) {
335 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
336 i - startn + 1, add,
337 arcs, arc_type_size))
338 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000339 startn = i + 1;
vlm1ff928d2004-08-11 08:10:13 +0000340 (char *)arcs += arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000341 add = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000342 }
vlm2e3dd3b2004-06-14 07:24:36 +0000343 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000344 }
345
vlm2e3dd3b2004-06-14 07:24:36 +0000346 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000347}
348
vlm12557712004-06-17 23:43:39 +0000349
350/*
351 * Save the single value as an object identifier arc.
352 */
vlme55716a2004-08-11 09:10:59 +0000353int
vlm12557712004-06-17 23:43:39 +0000354OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
355 /*
356 * The following conditions must hold:
357 * assert(arcval);
358 * assert(arcval_size > 0);
359 * assert(arcbuf);
360 */
361#ifdef WORDS_BIGENDIAN
362 const unsigned isLittleEndian = 0;
363#else
364 unsigned LE = 1;
365 unsigned isLittleEndian = *(char *)&LE;
366#endif
vlm12557712004-06-17 23:43:39 +0000367 uint8_t *tp, *tend;
368 unsigned int cache;
369 uint8_t *bp = arcbuf;
370 int bits;
vlm6e73a042004-08-11 07:17:22 +0000371#ifdef __GNUC__
372 uint8_t buffer[arcval_size];
373#else
374 uint8_t *buffer = alloca(arcval_size);
vlm7a6a60e2004-08-11 07:41:45 +0000375 if(!buffer) { errno = ENOMEM; return -1; }
vlm6e73a042004-08-11 07:17:22 +0000376#endif
vlm12557712004-06-17 23:43:39 +0000377
378 if(isLittleEndian && !prepared_order) {
vlm1ff928d2004-08-11 08:10:13 +0000379 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
vlmda674682004-08-11 09:07:36 +0000380 uint8_t *aend = (uint8_t *)arcval;
vlm12557712004-06-17 23:43:39 +0000381 uint8_t *msb = buffer + arcval_size - 1;
382 for(tp = buffer; a >= aend; tp++, a--)
383 if((*tp = *a) && (tp < msb))
384 msb = tp;
385 tend = &buffer[arcval_size];
386 tp = msb; /* Most significant non-zero byte */
387 } else {
388 /* Look for most significant non-zero byte */
vlm1ff928d2004-08-11 08:10:13 +0000389 tend = (unsigned char *)arcval + arcval_size;
vlmda674682004-08-11 09:07:36 +0000390 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
vlm12557712004-06-17 23:43:39 +0000391 if(*tp) break;
392 }
393
394 /*
395 * Split the value in 7-bits chunks.
396 */
397 bits = ((tend - tp) * CHAR_BIT) % 7;
398 if(bits) {
399 cache = *tp >> (CHAR_BIT - bits);
400 if(cache) {
401 *bp++ = cache | 0x80;
402 cache = *tp++;
403 bits = CHAR_BIT - bits;
404 } else {
405 bits = -bits;
406 }
407 } else {
408 cache = 0;
409 }
410 for(; tp < tend; tp++) {
411 cache = (cache << CHAR_BIT) + *tp;
412 bits += CHAR_BIT;
413 while(bits >= 7) {
414 bits -= 7;
415 *bp++ = 0x80 | (cache >> bits);
416 }
417 }
418 if(bits) *bp++ = cache;
419 bp[-1] &= 0x7f; /* Clear the last bit */
420
421 return bp - arcbuf;
422}
423
vlmfa67ddc2004-06-03 03:38:44 +0000424int
vlm12557712004-06-17 23:43:39 +0000425OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000426 uint8_t *buf;
427 uint8_t *bp;
vlm12557712004-06-17 23:43:39 +0000428 unsigned LE = 1; /* Little endian (x86) */
429 unsigned isLittleEndian = *((char *)&LE);
430 unsigned int arc0;
431 unsigned int arc1;
432 unsigned size;
vlm3717fb32004-06-14 08:17:27 +0000433 unsigned i;
vlmfa67ddc2004-06-03 03:38:44 +0000434
vlm12557712004-06-17 23:43:39 +0000435 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000436 errno = EINVAL;
437 return -1;
438 }
439
vlm12557712004-06-17 23:43:39 +0000440 switch(arc_type_size) {
441 case sizeof(char):
442 arc0 = ((unsigned char *)arcs)[0];
443 arc1 = ((unsigned char *)arcs)[1];
444 break;
445 case sizeof(short):
446 arc0 = ((unsigned short *)arcs)[0];
447 arc1 = ((unsigned short *)arcs)[1];
448 break;
449 case sizeof(int):
450 arc0 = ((unsigned int *)arcs)[0];
451 arc1 = ((unsigned int *)arcs)[1];
452 break;
453 default:
454 arc1 = arc0 = 0;
455 if(isLittleEndian) { /* Little endian (x86) */
456 unsigned char *ps, *pe;
457 /* If more significant bytes are present,
458 * make them > 255 quick */
vlm1ff928d2004-08-11 08:10:13 +0000459 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
460 ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000461 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
462 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
463 arc0 = *((unsigned char *)arcs + 0);
464 arc1 = *((unsigned char *)arcs + arc_type_size);
465 } else {
466 unsigned char *ps, *pe;
467 /* If more significant bytes are present,
468 * make them > 255 quick */
vlmda674682004-08-11 09:07:36 +0000469 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000470 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
471 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
472 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
473 }
474 }
475
476 /*
477 * The previous chapter left us with the first and the second arcs.
478 * The values are not precise (that is, they are valid only if
479 * they're less than 255), but OK for the purposes of making
480 * the sanity test below.
481 */
482 if(arc0 <= 1) {
483 if(arc1 >= 39) {
vlmfa67ddc2004-06-03 03:38:44 +0000484 /* 8.19.4: At most 39 subsequent values (including 0) */
485 errno = ERANGE;
486 return -1;
487 }
vlm12557712004-06-17 23:43:39 +0000488 } else if(arc0 > 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000489 /* 8.19.4: Only three values are allocated from the root node */
490 errno = ERANGE;
491 return -1;
492 }
vlm12557712004-06-17 23:43:39 +0000493 /*
494 * After above tests it is known that the value of arc0 is completely
495 * trustworthy (0..2). However, the arc1's value is still meaningless.
496 */
vlmfa67ddc2004-06-03 03:38:44 +0000497
498 /*
499 * Roughly estimate the maximum size necessary to encode these arcs.
vlm12557712004-06-17 23:43:39 +0000500 * This estimation implicitly takes in account the following facts,
501 * that cancel each other:
502 * * the first two arcs are encoded in a single value.
503 * * the first value may require more space (+1 byte)
504 * * the value of the first arc which is in range (0..2)
vlmfa67ddc2004-06-03 03:38:44 +0000505 */
vlm12557712004-06-17 23:43:39 +0000506 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
vlmda674682004-08-11 09:07:36 +0000507 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000508 if(!buf) {
509 /* ENOMEM */
510 return -1;
511 }
512
513 /*
vlm12557712004-06-17 23:43:39 +0000514 * Encode the first two arcs.
515 * These require special treatment.
vlmfa67ddc2004-06-03 03:38:44 +0000516 */
vlmfa67ddc2004-06-03 03:38:44 +0000517 {
vlm12557712004-06-17 23:43:39 +0000518 uint8_t *tp;
vlma51f7ce2004-08-11 07:48:19 +0000519#ifdef __GNUC__
520 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
vlm1ff928d2004-08-11 08:10:13 +0000521 uint8_t *fv = first_value;
vlma51f7ce2004-08-11 07:48:19 +0000522#else
523 uint8_t *first_value = alloca(1 + arc_type_size);
vlma51f7ce2004-08-11 07:48:19 +0000524 uint8_t *fv = first_value;
vlm1ff928d2004-08-11 08:10:13 +0000525 if(!first_value) {
526 errno = ENOMEM;
527 return -1;
528 }
529#endif
vlmfa67ddc2004-06-03 03:38:44 +0000530
vlm12557712004-06-17 23:43:39 +0000531 /*
532 * Simulate first_value = arc0 * 40 + arc1;
533 */
534 /* Copy the second (1'st) arcs[1] into the first_value */
535 *fv++ = 0;
536 (char *)arcs += arc_type_size;
537 if(isLittleEndian) {
vlm1ff928d2004-08-11 08:10:13 +0000538 uint8_t *aend = (unsigned char *)arcs - 1;
539 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
vlm12557712004-06-17 23:43:39 +0000540 for(; a1 > aend; fv++, a1--) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000541 } else {
vlmda674682004-08-11 09:07:36 +0000542 uint8_t *a1 = (uint8_t *)arcs;
vlm12557712004-06-17 23:43:39 +0000543 uint8_t *aend = a1 + arc_type_size;
544 for(; a1 < aend; fv++, a1++) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000545 }
vlm12557712004-06-17 23:43:39 +0000546 /* Increase the first_value by arc0 */
547 arc0 *= 40; /* (0..80) */
548 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
549 unsigned int v = *tp;
550 v += arc0;
551 *tp = v;
552 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
553 else break;
554 }
555
556 assert(tp >= first_value);
557
558 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
559 fv - first_value, 1);
560 }
561
562 /*
563 * Save the rest of arcs.
564 */
565 for((char *)arcs += arc_type_size, i = 2;
566 i < arc_slots; i++, (char *)arcs += arc_type_size) {
567 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
568 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000569 }
570
vlma63e0292004-06-17 23:46:45 +0000571 assert((unsigned)(bp - buf) <= size);
vlmfa67ddc2004-06-03 03:38:44 +0000572
573 /*
574 * Replace buffer.
575 */
vlm12557712004-06-17 23:43:39 +0000576 oid->size = bp - buf;
vlmfa67ddc2004-06-03 03:38:44 +0000577 bp = oid->buf;
578 oid->buf = buf;
579 if(bp) FREEMEM(bp);
580
581 return 0;
582}
vlm3717fb32004-06-14 08:17:27 +0000583