blob: fe209056d5898315a7f24edccd29b361e448583e [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
vlm3fff06b2004-08-23 09:23:02 +0000228 if(accum) {
229 /* Fill the scratch buffer in reverse. */
230 p = scratch + sizeof(scratch);
231 for(; accum; accum /= 10)
232 *(--p) = (char)(accum % 10) + 0x30;
vlmfa67ddc2004-06-03 03:38:44 +0000233
vlm3fff06b2004-08-23 09:23:02 +0000234 return cb(p, sizeof(scratch) - (p - scratch), app_key);
235 } else {
236 *scratch = 0x30;
237 return cb(scratch, 1, app_key);
238 }
vlmfa67ddc2004-06-03 03:38:44 +0000239}
240
241int
242OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
243 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000244 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000245 int startn;
246 int add = 0;
247 int i;
248
vlmb42843a2004-06-05 08:17:50 +0000249 (void)td; /* Unused argument */
250 (void)ilevel; /* Unused argument */
251
vlmfa67ddc2004-06-03 03:38:44 +0000252 if(!st || !st->buf)
253 return cb("<absent>", 8, app_key);
254
255 /* Dump preamble */
256 if(cb("{ ", 2, app_key))
257 return -1;
258
259 for(i = 0, startn = 0; i < st->size; i++) {
260 uint8_t b = st->buf[i];
261 if((b & 0x80)) /* Continuation expected */
262 continue;
263
264 if(startn == 0) {
265 /*
266 * First two arcs are encoded through the backdoor.
267 */
268 if(i) {
269 add = -80;
270 if(cb("2", 1, app_key)) return -1;
271 } else if(b <= 39) {
272 add = 0;
273 if(cb("0", 1, app_key)) return -1;
274 } else if(b < 79) {
275 add = -40;
276 if(cb("1", 1, app_key)) return -1;
277 } else {
278 add = -80;
279 if(cb("2", 1, app_key)) return -1;
280 }
281 }
282
283 if(cb(" ", 1, app_key)) /* Separate arcs */
284 return -1;
285
286 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
287 i - startn + 1, add,
288 cb, app_key))
289 return -1;
290 startn = i + 1;
291 add = 0;
292 }
293
294 return cb(" }", 2, app_key);
295}
296
297int
vlm2e3dd3b2004-06-14 07:24:36 +0000298OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
299 unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000300 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
vlm2e3dd3b2004-06-14 07:24:36 +0000301 int num_arcs = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000302 int startn = 0;
303 int add = 0;
304 int i;
305
vlm12557712004-06-17 23:43:39 +0000306 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000307 errno = EINVAL;
308 return -1;
309 }
310
311 for(i = 0; i < oid->size; i++) {
312 uint8_t b = oid->buf[i];
313 if((b & 0x80)) /* Continuation expected */
314 continue;
315
vlm2e3dd3b2004-06-14 07:24:36 +0000316 if(num_arcs == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000317 /*
318 * First two arcs are encoded through the backdoor.
319 */
vlm2e3dd3b2004-06-14 07:24:36 +0000320 unsigned LE = 1; /* Little endian */
321 int first_arc;
322 num_arcs++;
323 if(!arc_slots) { num_arcs++; continue; }
324
325 if(i) first_arc = 2;
326 else if(b <= 39) first_arc = 0;
327 else if(b < 79) first_arc = 1;
328 else first_arc = 2;
329
330 add = -40 * first_arc;
331 memset(arcs, 0, arc_type_size);
vlm1ff928d2004-08-11 08:10:13 +0000332 *(unsigned char *)((char *)arcs
vlm2e3dd3b2004-06-14 07:24:36 +0000333 + ((*(char *)&LE)?0:(arc_type_size - 1)))
334 = first_arc;
vlmd86c9252004-08-25 01:34:11 +0000335 arcs = ((char *)arcs) + arc_type_size;
vlmfa67ddc2004-06-03 03:38:44 +0000336 }
337
vlm2e3dd3b2004-06-14 07:24:36 +0000338 /* Decode, if has space */
339 if(arcs < arcs_end) {
340 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
341 i - startn + 1, add,
342 arcs, arc_type_size))
343 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000344 startn = i + 1;
vlmd86c9252004-08-25 01:34:11 +0000345 arcs = ((char *)arcs) + arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000346 add = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000347 }
vlm2e3dd3b2004-06-14 07:24:36 +0000348 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000349 }
350
vlm2e3dd3b2004-06-14 07:24:36 +0000351 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000352}
353
vlm12557712004-06-17 23:43:39 +0000354
355/*
356 * Save the single value as an object identifier arc.
357 */
vlme55716a2004-08-11 09:10:59 +0000358int
vlm12557712004-06-17 23:43:39 +0000359OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
360 /*
361 * The following conditions must hold:
362 * assert(arcval);
363 * assert(arcval_size > 0);
364 * assert(arcbuf);
365 */
366#ifdef WORDS_BIGENDIAN
367 const unsigned isLittleEndian = 0;
368#else
369 unsigned LE = 1;
370 unsigned isLittleEndian = *(char *)&LE;
371#endif
vlm12557712004-06-17 23:43:39 +0000372 uint8_t *tp, *tend;
373 unsigned int cache;
374 uint8_t *bp = arcbuf;
375 int bits;
vlm6e73a042004-08-11 07:17:22 +0000376#ifdef __GNUC__
377 uint8_t buffer[arcval_size];
378#else
379 uint8_t *buffer = alloca(arcval_size);
vlm7a6a60e2004-08-11 07:41:45 +0000380 if(!buffer) { errno = ENOMEM; return -1; }
vlm6e73a042004-08-11 07:17:22 +0000381#endif
vlm12557712004-06-17 23:43:39 +0000382
383 if(isLittleEndian && !prepared_order) {
vlm1ff928d2004-08-11 08:10:13 +0000384 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
vlmda674682004-08-11 09:07:36 +0000385 uint8_t *aend = (uint8_t *)arcval;
vlm12557712004-06-17 23:43:39 +0000386 uint8_t *msb = buffer + arcval_size - 1;
387 for(tp = buffer; a >= aend; tp++, a--)
388 if((*tp = *a) && (tp < msb))
389 msb = tp;
390 tend = &buffer[arcval_size];
391 tp = msb; /* Most significant non-zero byte */
392 } else {
393 /* Look for most significant non-zero byte */
vlm1ff928d2004-08-11 08:10:13 +0000394 tend = (unsigned char *)arcval + arcval_size;
vlmda674682004-08-11 09:07:36 +0000395 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
vlm12557712004-06-17 23:43:39 +0000396 if(*tp) break;
397 }
398
399 /*
400 * Split the value in 7-bits chunks.
401 */
402 bits = ((tend - tp) * CHAR_BIT) % 7;
403 if(bits) {
404 cache = *tp >> (CHAR_BIT - bits);
405 if(cache) {
406 *bp++ = cache | 0x80;
407 cache = *tp++;
408 bits = CHAR_BIT - bits;
409 } else {
410 bits = -bits;
411 }
412 } else {
413 cache = 0;
414 }
415 for(; tp < tend; tp++) {
416 cache = (cache << CHAR_BIT) + *tp;
417 bits += CHAR_BIT;
418 while(bits >= 7) {
419 bits -= 7;
420 *bp++ = 0x80 | (cache >> bits);
421 }
422 }
423 if(bits) *bp++ = cache;
424 bp[-1] &= 0x7f; /* Clear the last bit */
425
426 return bp - arcbuf;
427}
428
vlmfa67ddc2004-06-03 03:38:44 +0000429int
vlm12557712004-06-17 23:43:39 +0000430OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000431 uint8_t *buf;
432 uint8_t *bp;
vlm12557712004-06-17 23:43:39 +0000433 unsigned LE = 1; /* Little endian (x86) */
434 unsigned isLittleEndian = *((char *)&LE);
435 unsigned int arc0;
436 unsigned int arc1;
437 unsigned size;
vlm3717fb32004-06-14 08:17:27 +0000438 unsigned i;
vlmfa67ddc2004-06-03 03:38:44 +0000439
vlm12557712004-06-17 23:43:39 +0000440 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000441 errno = EINVAL;
442 return -1;
443 }
444
vlm12557712004-06-17 23:43:39 +0000445 switch(arc_type_size) {
446 case sizeof(char):
447 arc0 = ((unsigned char *)arcs)[0];
448 arc1 = ((unsigned char *)arcs)[1];
449 break;
450 case sizeof(short):
451 arc0 = ((unsigned short *)arcs)[0];
452 arc1 = ((unsigned short *)arcs)[1];
453 break;
454 case sizeof(int):
455 arc0 = ((unsigned int *)arcs)[0];
456 arc1 = ((unsigned int *)arcs)[1];
457 break;
458 default:
459 arc1 = arc0 = 0;
460 if(isLittleEndian) { /* Little endian (x86) */
461 unsigned char *ps, *pe;
462 /* If more significant bytes are present,
463 * make them > 255 quick */
vlm1ff928d2004-08-11 08:10:13 +0000464 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
465 ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000466 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
467 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
468 arc0 = *((unsigned char *)arcs + 0);
469 arc1 = *((unsigned char *)arcs + arc_type_size);
470 } else {
471 unsigned char *ps, *pe;
472 /* If more significant bytes are present,
473 * make them > 255 quick */
vlmda674682004-08-11 09:07:36 +0000474 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000475 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
476 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
477 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
478 }
479 }
480
481 /*
482 * The previous chapter left us with the first and the second arcs.
483 * The values are not precise (that is, they are valid only if
484 * they're less than 255), but OK for the purposes of making
485 * the sanity test below.
486 */
487 if(arc0 <= 1) {
488 if(arc1 >= 39) {
vlmfa67ddc2004-06-03 03:38:44 +0000489 /* 8.19.4: At most 39 subsequent values (including 0) */
490 errno = ERANGE;
491 return -1;
492 }
vlm12557712004-06-17 23:43:39 +0000493 } else if(arc0 > 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000494 /* 8.19.4: Only three values are allocated from the root node */
495 errno = ERANGE;
496 return -1;
497 }
vlm12557712004-06-17 23:43:39 +0000498 /*
499 * After above tests it is known that the value of arc0 is completely
500 * trustworthy (0..2). However, the arc1's value is still meaningless.
501 */
vlmfa67ddc2004-06-03 03:38:44 +0000502
503 /*
504 * Roughly estimate the maximum size necessary to encode these arcs.
vlm12557712004-06-17 23:43:39 +0000505 * This estimation implicitly takes in account the following facts,
506 * that cancel each other:
507 * * the first two arcs are encoded in a single value.
508 * * the first value may require more space (+1 byte)
509 * * the value of the first arc which is in range (0..2)
vlmfa67ddc2004-06-03 03:38:44 +0000510 */
vlm12557712004-06-17 23:43:39 +0000511 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
vlmda674682004-08-11 09:07:36 +0000512 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000513 if(!buf) {
514 /* ENOMEM */
515 return -1;
516 }
517
518 /*
vlm12557712004-06-17 23:43:39 +0000519 * Encode the first two arcs.
520 * These require special treatment.
vlmfa67ddc2004-06-03 03:38:44 +0000521 */
vlmfa67ddc2004-06-03 03:38:44 +0000522 {
vlm12557712004-06-17 23:43:39 +0000523 uint8_t *tp;
vlma51f7ce2004-08-11 07:48:19 +0000524#ifdef __GNUC__
525 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
vlm1ff928d2004-08-11 08:10:13 +0000526 uint8_t *fv = first_value;
vlma51f7ce2004-08-11 07:48:19 +0000527#else
528 uint8_t *first_value = alloca(1 + arc_type_size);
vlma51f7ce2004-08-11 07:48:19 +0000529 uint8_t *fv = first_value;
vlm1ff928d2004-08-11 08:10:13 +0000530 if(!first_value) {
531 errno = ENOMEM;
532 return -1;
533 }
534#endif
vlmfa67ddc2004-06-03 03:38:44 +0000535
vlm12557712004-06-17 23:43:39 +0000536 /*
537 * Simulate first_value = arc0 * 40 + arc1;
538 */
539 /* Copy the second (1'st) arcs[1] into the first_value */
540 *fv++ = 0;
vlmd86c9252004-08-25 01:34:11 +0000541 arcs = ((char *)arcs) + arc_type_size;
vlm12557712004-06-17 23:43:39 +0000542 if(isLittleEndian) {
vlm1ff928d2004-08-11 08:10:13 +0000543 uint8_t *aend = (unsigned char *)arcs - 1;
544 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
vlm12557712004-06-17 23:43:39 +0000545 for(; a1 > aend; fv++, a1--) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000546 } else {
vlmda674682004-08-11 09:07:36 +0000547 uint8_t *a1 = (uint8_t *)arcs;
vlm12557712004-06-17 23:43:39 +0000548 uint8_t *aend = a1 + arc_type_size;
549 for(; a1 < aend; fv++, a1++) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000550 }
vlm12557712004-06-17 23:43:39 +0000551 /* Increase the first_value by arc0 */
552 arc0 *= 40; /* (0..80) */
553 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
554 unsigned int v = *tp;
555 v += arc0;
556 *tp = v;
557 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
558 else break;
559 }
560
561 assert(tp >= first_value);
562
563 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
564 fv - first_value, 1);
565 }
566
567 /*
568 * Save the rest of arcs.
569 */
vlmd86c9252004-08-25 01:34:11 +0000570 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
571 i < arc_slots;
572 i++, arcs = ((char *)arcs) + arc_type_size) {
vlm12557712004-06-17 23:43:39 +0000573 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
574 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000575 }
576
vlma63e0292004-06-17 23:46:45 +0000577 assert((unsigned)(bp - buf) <= size);
vlmfa67ddc2004-06-03 03:38:44 +0000578
579 /*
580 * Replace buffer.
581 */
vlm12557712004-06-17 23:43:39 +0000582 oid->size = bp - buf;
vlmfa67ddc2004-06-03 03:38:44 +0000583 bp = oid->buf;
584 oid->buf = buf;
585 if(bp) FREEMEM(bp);
586
587 return 0;
588}
vlm3717fb32004-06-14 08:17:27 +0000589