blob: c55410d5d46af3a32bbd5a3dc8ec7f8fb4cbbab1 [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]),
vlm72425de2004-09-13 08:31:01 +000027 asn1_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
28 sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
29 / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
vlmb42843a2004-06-05 08:17:50 +000030 0, /* Always in primitive form */
vlme413c122004-08-20 13:23:42 +000031 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000032 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000033};
34
35
36/*
37 * Encode OBJECT IDENTIFIER type using DER.
38 */
39der_enc_rval_t
40OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
41 int tag_mode, ber_tlv_tag_t tag,
42 asn_app_consume_bytes_f *cb, void *app_key) {
43 der_enc_rval_t erval;
vlmda674682004-08-11 09:07:36 +000044 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr;
vlmfa67ddc2004-06-03 03:38:44 +000045
46 ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)",
47 cb?"Encoding":"Estimating", sd->name, tag_mode);
48
49 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
50 cb, app_key);
51 ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d",
52 sd->name, (int)erval.encoded);
53 if(erval.encoded == -1) {
54 erval.failed_type = sd;
55 erval.structure_ptr = ptr;
56 return erval;
57 }
58
59 if(cb && st->buf) {
60 ssize_t ret;
61
62 ret = cb(st->buf, st->size, app_key);
63 if(ret == -1) {
64 erval.encoded = -1;
65 erval.failed_type = sd;
66 erval.structure_ptr = ptr;
67 return erval;
68 }
69 } else {
70 assert(st->buf || st->size == 0);
71 }
72
73 erval.encoded += st->size;
74
75 return erval;
76}
77
78int
79OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
80 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +000081 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000082
83 if(st && st->buf) {
84 if(st->size < 1) {
vlme3f0f282004-08-11 09:44:13 +000085 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000086 "%s: at least one numerical value "
87 "expected (%s:%d)",
88 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000089 return -1;
90 }
91 } else {
vlme3f0f282004-08-11 09:44:13 +000092 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000093 "%s: value not given (%s:%d)",
94 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000095 return -1;
96 }
97
98 return 0;
99}
100
vlm3717fb32004-06-14 08:17:27 +0000101
vlmfa67ddc2004-06-03 03:38:44 +0000102int
vlm2e3dd3b2004-06-14 07:24:36 +0000103OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
vlm72425de2004-09-13 08:31:01 +0000104 unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
vlm2e3dd3b2004-06-14 07:24:36 +0000105 uint8_t *arcend = arcbuf + arclen; /* End of arc */
106 void *rvstart = rvbuf; /* Original start of the value buffer */
107 unsigned int cache = 0; /* No more than 14 significant bits */
108 int inc; /* Return value growth direction */
vlmfa67ddc2004-06-03 03:38:44 +0000109
vlm2e3dd3b2004-06-14 07:24:36 +0000110 rvsize *= CHAR_BIT; /* bytes to bits */
111 arclen *= 7; /* bytes to bits */
112
113 /*
114 * The arc has the number of bits
115 * cannot be represented using supplied return value type.
116 */
117 if(arclen > rvsize) {
118 if(arclen > (rvsize + CHAR_BIT)) {
119 errno = ERANGE; /* Overflow */
120 return -1;
121 } else {
122 /*
123 * Even if the number of bits in the arc representation
124 * is higher than the width of supplied * return value
125 * type, there is still possible to fit it when there
126 * are few unused high bits in the arc value
127 * representaion.
vlm12557712004-06-17 23:43:39 +0000128 *
129 * Moreover, there is a possibility that the
130 * number could actually fit the arc space, given
131 * that add is negative, but we don't handle
132 * such "temporary lack of precision" situation here.
133 * May be considered as a bug.
vlm2e3dd3b2004-06-14 07:24:36 +0000134 */
135 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
136 if((*arcbuf & mask)) {
vlmfa67ddc2004-06-03 03:38:44 +0000137 errno = ERANGE; /* Overflow */
138 return -1;
139 }
vlm2e3dd3b2004-06-14 07:24:36 +0000140 /* Fool the routine computing unused bits */
141 arclen -= 7;
142 cache = *arcbuf & 0x7f;
143 arcbuf++;
144 }
145 }
146
vlm3717fb32004-06-14 08:17:27 +0000147 /* Faster path for common size */
148 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
149 unsigned long accum;
150 /* Gather all bits into the accumulator */
151 for(accum = cache; arcbuf < arcend; arcbuf++)
152 accum = (accum << 7) | (*arcbuf & ~0x80);
153 if(accum < (unsigned)-add) {
154 errno = ERANGE; /* Overflow */
155 return -1;
156 }
157 *(unsigned long *)rvbuf = accum + add;
158 return 0;
159 }
160
vlm2e3dd3b2004-06-14 07:24:36 +0000161#ifndef WORDS_BIGENDIAN
162 if(*(unsigned char *)&LE) { /* Little endian (x86) */
163 /* "Convert" to big endian */
vlm1ff928d2004-08-11 08:10:13 +0000164 (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
vlm3717fb32004-06-14 08:17:27 +0000165 ((unsigned char *)rvstart)--;
vlm2e3dd3b2004-06-14 07:24:36 +0000166 inc = -1; /* Descending */
vlm3717fb32004-06-14 08:17:27 +0000167 } else
vlm2e3dd3b2004-06-14 07:24:36 +0000168#endif /* !WORDS_BIGENDIAN */
vlm3717fb32004-06-14 08:17:27 +0000169 inc = +1; /* Big endian is known [at compile time] */
vlm2e3dd3b2004-06-14 07:24:36 +0000170
vlm3717fb32004-06-14 08:17:27 +0000171 {
vlm12557712004-06-17 23:43:39 +0000172 int bits; /* typically no more than 3-4 bits */
vlm3717fb32004-06-14 08:17:27 +0000173
vlm2e3dd3b2004-06-14 07:24:36 +0000174 /* Clear the high unused bits */
175 for(bits = rvsize - arclen;
176 bits > CHAR_BIT;
vlm1ff928d2004-08-11 08:10:13 +0000177 (unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
vlm2e3dd3b2004-06-14 07:24:36 +0000178 *(unsigned char *)rvbuf = 0;
vlm3717fb32004-06-14 08:17:27 +0000179
vlm2e3dd3b2004-06-14 07:24:36 +0000180 /* Fill the body of a value */
181 for(; arcbuf < arcend; arcbuf++) {
182 cache = (cache << 7) | (*arcbuf & 0x7f);
183 bits += 7;
184 if(bits >= CHAR_BIT) {
185 bits -= CHAR_BIT;
186 *(unsigned char *)rvbuf = (cache >> bits);
vlm1ff928d2004-08-11 08:10:13 +0000187 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000188 }
189 }
190 if(bits) {
191 *(unsigned char *)rvbuf = cache;
vlm1ff928d2004-08-11 08:10:13 +0000192 (unsigned char *)rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000193 }
194 }
195
196 if(add) {
vlm1ff928d2004-08-11 08:10:13 +0000197 for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
vlm2e3dd3b2004-06-14 07:24:36 +0000198 int v = add + *(unsigned char *)rvbuf;
199 if(v & (-1 << CHAR_BIT)) {
200 *(unsigned char *)rvbuf
vlm754284a2004-08-11 09:17:15 +0000201 = (unsigned char)(v + (1 << CHAR_BIT));
vlm2e3dd3b2004-06-14 07:24:36 +0000202 add = -1;
203 } else {
204 *(unsigned char *)rvbuf = v;
205 break;
206 }
207 }
208 if(rvbuf == rvstart) {
209 /* No space to carry over */
vlmfa67ddc2004-06-03 03:38:44 +0000210 errno = ERANGE; /* Overflow */
211 return -1;
212 }
213 }
214
vlmfa67ddc2004-06-03 03:38:44 +0000215 return 0;
216}
217
vlm2e3dd3b2004-06-14 07:24:36 +0000218
vlmfa67ddc2004-06-03 03:38:44 +0000219int
220OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
221 asn_app_consume_bytes_f *cb, void *app_key) {
222 char scratch[64]; /* Conservative estimate */
223 unsigned long accum; /* Bits accumulator */
224 char *p; /* Position in the scratch buffer */
225
vlm2e3dd3b2004-06-14 07:24:36 +0000226 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
227 &accum, sizeof(accum)))
vlmfa67ddc2004-06-03 03:38:44 +0000228 return -1;
229
vlm3fff06b2004-08-23 09:23:02 +0000230 if(accum) {
231 /* Fill the scratch buffer in reverse. */
232 p = scratch + sizeof(scratch);
233 for(; accum; accum /= 10)
234 *(--p) = (char)(accum % 10) + 0x30;
vlmfa67ddc2004-06-03 03:38:44 +0000235
vlm3fff06b2004-08-23 09:23:02 +0000236 return cb(p, sizeof(scratch) - (p - scratch), app_key);
237 } else {
238 *scratch = 0x30;
239 return cb(scratch, 1, app_key);
240 }
vlmfa67ddc2004-06-03 03:38:44 +0000241}
242
243int
244OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
245 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000246 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000247 int startn;
248 int add = 0;
249 int i;
250
vlmb42843a2004-06-05 08:17:50 +0000251 (void)td; /* Unused argument */
252 (void)ilevel; /* Unused argument */
253
vlmfa67ddc2004-06-03 03:38:44 +0000254 if(!st || !st->buf)
255 return cb("<absent>", 8, app_key);
256
257 /* Dump preamble */
258 if(cb("{ ", 2, app_key))
259 return -1;
260
261 for(i = 0, startn = 0; i < st->size; i++) {
262 uint8_t b = st->buf[i];
263 if((b & 0x80)) /* Continuation expected */
264 continue;
265
266 if(startn == 0) {
267 /*
268 * First two arcs are encoded through the backdoor.
269 */
270 if(i) {
271 add = -80;
272 if(cb("2", 1, app_key)) return -1;
273 } else if(b <= 39) {
274 add = 0;
275 if(cb("0", 1, app_key)) return -1;
276 } else if(b < 79) {
277 add = -40;
278 if(cb("1", 1, app_key)) return -1;
279 } else {
280 add = -80;
281 if(cb("2", 1, app_key)) return -1;
282 }
283 }
284
285 if(cb(" ", 1, app_key)) /* Separate arcs */
286 return -1;
287
288 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
289 i - startn + 1, add,
290 cb, app_key))
291 return -1;
292 startn = i + 1;
293 add = 0;
294 }
295
296 return cb(" }", 2, app_key);
297}
298
299int
vlm2e3dd3b2004-06-14 07:24:36 +0000300OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
301 unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000302 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
vlm2e3dd3b2004-06-14 07:24:36 +0000303 int num_arcs = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000304 int startn = 0;
305 int add = 0;
306 int i;
307
vlm12557712004-06-17 23:43:39 +0000308 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000309 errno = EINVAL;
310 return -1;
311 }
312
313 for(i = 0; i < oid->size; i++) {
314 uint8_t b = oid->buf[i];
315 if((b & 0x80)) /* Continuation expected */
316 continue;
317
vlm2e3dd3b2004-06-14 07:24:36 +0000318 if(num_arcs == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000319 /*
320 * First two arcs are encoded through the backdoor.
321 */
vlm2e3dd3b2004-06-14 07:24:36 +0000322 unsigned LE = 1; /* Little endian */
323 int first_arc;
324 num_arcs++;
325 if(!arc_slots) { num_arcs++; continue; }
326
327 if(i) first_arc = 2;
328 else if(b <= 39) first_arc = 0;
329 else if(b < 79) first_arc = 1;
330 else first_arc = 2;
331
332 add = -40 * first_arc;
333 memset(arcs, 0, arc_type_size);
vlm1ff928d2004-08-11 08:10:13 +0000334 *(unsigned char *)((char *)arcs
vlm2e3dd3b2004-06-14 07:24:36 +0000335 + ((*(char *)&LE)?0:(arc_type_size - 1)))
336 = first_arc;
vlmd86c9252004-08-25 01:34:11 +0000337 arcs = ((char *)arcs) + arc_type_size;
vlmfa67ddc2004-06-03 03:38:44 +0000338 }
339
vlm2e3dd3b2004-06-14 07:24:36 +0000340 /* Decode, if has space */
341 if(arcs < arcs_end) {
342 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
343 i - startn + 1, add,
344 arcs, arc_type_size))
345 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000346 startn = i + 1;
vlmd86c9252004-08-25 01:34:11 +0000347 arcs = ((char *)arcs) + arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000348 add = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000349 }
vlm2e3dd3b2004-06-14 07:24:36 +0000350 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000351 }
352
vlm2e3dd3b2004-06-14 07:24:36 +0000353 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000354}
355
vlm12557712004-06-17 23:43:39 +0000356
357/*
358 * Save the single value as an object identifier arc.
359 */
vlme55716a2004-08-11 09:10:59 +0000360int
vlm12557712004-06-17 23:43:39 +0000361OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
362 /*
363 * The following conditions must hold:
364 * assert(arcval);
365 * assert(arcval_size > 0);
366 * assert(arcbuf);
367 */
368#ifdef WORDS_BIGENDIAN
369 const unsigned isLittleEndian = 0;
370#else
371 unsigned LE = 1;
372 unsigned isLittleEndian = *(char *)&LE;
373#endif
vlm12557712004-06-17 23:43:39 +0000374 uint8_t *tp, *tend;
375 unsigned int cache;
376 uint8_t *bp = arcbuf;
377 int bits;
vlm6e73a042004-08-11 07:17:22 +0000378#ifdef __GNUC__
379 uint8_t buffer[arcval_size];
380#else
381 uint8_t *buffer = alloca(arcval_size);
vlm7a6a60e2004-08-11 07:41:45 +0000382 if(!buffer) { errno = ENOMEM; return -1; }
vlm6e73a042004-08-11 07:17:22 +0000383#endif
vlm12557712004-06-17 23:43:39 +0000384
385 if(isLittleEndian && !prepared_order) {
vlm1ff928d2004-08-11 08:10:13 +0000386 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
vlmda674682004-08-11 09:07:36 +0000387 uint8_t *aend = (uint8_t *)arcval;
vlm12557712004-06-17 23:43:39 +0000388 uint8_t *msb = buffer + arcval_size - 1;
389 for(tp = buffer; a >= aend; tp++, a--)
390 if((*tp = *a) && (tp < msb))
391 msb = tp;
392 tend = &buffer[arcval_size];
393 tp = msb; /* Most significant non-zero byte */
394 } else {
395 /* Look for most significant non-zero byte */
vlm1ff928d2004-08-11 08:10:13 +0000396 tend = (unsigned char *)arcval + arcval_size;
vlmda674682004-08-11 09:07:36 +0000397 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
vlm12557712004-06-17 23:43:39 +0000398 if(*tp) break;
399 }
400
401 /*
402 * Split the value in 7-bits chunks.
403 */
404 bits = ((tend - tp) * CHAR_BIT) % 7;
405 if(bits) {
406 cache = *tp >> (CHAR_BIT - bits);
407 if(cache) {
408 *bp++ = cache | 0x80;
409 cache = *tp++;
410 bits = CHAR_BIT - bits;
411 } else {
412 bits = -bits;
413 }
414 } else {
415 cache = 0;
416 }
417 for(; tp < tend; tp++) {
418 cache = (cache << CHAR_BIT) + *tp;
419 bits += CHAR_BIT;
420 while(bits >= 7) {
421 bits -= 7;
422 *bp++ = 0x80 | (cache >> bits);
423 }
424 }
425 if(bits) *bp++ = cache;
426 bp[-1] &= 0x7f; /* Clear the last bit */
427
428 return bp - arcbuf;
429}
430
vlmfa67ddc2004-06-03 03:38:44 +0000431int
vlm12557712004-06-17 23:43:39 +0000432OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000433 uint8_t *buf;
434 uint8_t *bp;
vlm12557712004-06-17 23:43:39 +0000435 unsigned LE = 1; /* Little endian (x86) */
436 unsigned isLittleEndian = *((char *)&LE);
437 unsigned int arc0;
438 unsigned int arc1;
439 unsigned size;
vlm3717fb32004-06-14 08:17:27 +0000440 unsigned i;
vlmfa67ddc2004-06-03 03:38:44 +0000441
vlm12557712004-06-17 23:43:39 +0000442 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000443 errno = EINVAL;
444 return -1;
445 }
446
vlm12557712004-06-17 23:43:39 +0000447 switch(arc_type_size) {
448 case sizeof(char):
449 arc0 = ((unsigned char *)arcs)[0];
450 arc1 = ((unsigned char *)arcs)[1];
451 break;
452 case sizeof(short):
453 arc0 = ((unsigned short *)arcs)[0];
454 arc1 = ((unsigned short *)arcs)[1];
455 break;
456 case sizeof(int):
457 arc0 = ((unsigned int *)arcs)[0];
458 arc1 = ((unsigned int *)arcs)[1];
459 break;
460 default:
461 arc1 = arc0 = 0;
462 if(isLittleEndian) { /* Little endian (x86) */
463 unsigned char *ps, *pe;
464 /* If more significant bytes are present,
465 * make them > 255 quick */
vlm1ff928d2004-08-11 08:10:13 +0000466 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
467 ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000468 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
469 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
470 arc0 = *((unsigned char *)arcs + 0);
471 arc1 = *((unsigned char *)arcs + arc_type_size);
472 } else {
473 unsigned char *ps, *pe;
474 /* If more significant bytes are present,
475 * make them > 255 quick */
vlmda674682004-08-11 09:07:36 +0000476 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000477 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
478 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
479 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
480 }
481 }
482
483 /*
484 * The previous chapter left us with the first and the second arcs.
485 * The values are not precise (that is, they are valid only if
486 * they're less than 255), but OK for the purposes of making
487 * the sanity test below.
488 */
489 if(arc0 <= 1) {
490 if(arc1 >= 39) {
vlmfa67ddc2004-06-03 03:38:44 +0000491 /* 8.19.4: At most 39 subsequent values (including 0) */
492 errno = ERANGE;
493 return -1;
494 }
vlm12557712004-06-17 23:43:39 +0000495 } else if(arc0 > 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000496 /* 8.19.4: Only three values are allocated from the root node */
497 errno = ERANGE;
498 return -1;
499 }
vlm12557712004-06-17 23:43:39 +0000500 /*
501 * After above tests it is known that the value of arc0 is completely
502 * trustworthy (0..2). However, the arc1's value is still meaningless.
503 */
vlmfa67ddc2004-06-03 03:38:44 +0000504
505 /*
506 * Roughly estimate the maximum size necessary to encode these arcs.
vlm12557712004-06-17 23:43:39 +0000507 * This estimation implicitly takes in account the following facts,
508 * that cancel each other:
509 * * the first two arcs are encoded in a single value.
510 * * the first value may require more space (+1 byte)
511 * * the value of the first arc which is in range (0..2)
vlmfa67ddc2004-06-03 03:38:44 +0000512 */
vlm12557712004-06-17 23:43:39 +0000513 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
vlmda674682004-08-11 09:07:36 +0000514 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000515 if(!buf) {
516 /* ENOMEM */
517 return -1;
518 }
519
520 /*
vlm12557712004-06-17 23:43:39 +0000521 * Encode the first two arcs.
522 * These require special treatment.
vlmfa67ddc2004-06-03 03:38:44 +0000523 */
vlmfa67ddc2004-06-03 03:38:44 +0000524 {
vlm12557712004-06-17 23:43:39 +0000525 uint8_t *tp;
vlma51f7ce2004-08-11 07:48:19 +0000526#ifdef __GNUC__
527 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
vlm1ff928d2004-08-11 08:10:13 +0000528 uint8_t *fv = first_value;
vlma51f7ce2004-08-11 07:48:19 +0000529#else
530 uint8_t *first_value = alloca(1 + arc_type_size);
vlma51f7ce2004-08-11 07:48:19 +0000531 uint8_t *fv = first_value;
vlm1ff928d2004-08-11 08:10:13 +0000532 if(!first_value) {
533 errno = ENOMEM;
534 return -1;
535 }
536#endif
vlmfa67ddc2004-06-03 03:38:44 +0000537
vlm12557712004-06-17 23:43:39 +0000538 /*
539 * Simulate first_value = arc0 * 40 + arc1;
540 */
541 /* Copy the second (1'st) arcs[1] into the first_value */
542 *fv++ = 0;
vlmd86c9252004-08-25 01:34:11 +0000543 arcs = ((char *)arcs) + arc_type_size;
vlm12557712004-06-17 23:43:39 +0000544 if(isLittleEndian) {
vlm1ff928d2004-08-11 08:10:13 +0000545 uint8_t *aend = (unsigned char *)arcs - 1;
546 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
vlm12557712004-06-17 23:43:39 +0000547 for(; a1 > aend; fv++, a1--) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000548 } else {
vlmda674682004-08-11 09:07:36 +0000549 uint8_t *a1 = (uint8_t *)arcs;
vlm12557712004-06-17 23:43:39 +0000550 uint8_t *aend = a1 + arc_type_size;
551 for(; a1 < aend; fv++, a1++) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000552 }
vlm12557712004-06-17 23:43:39 +0000553 /* Increase the first_value by arc0 */
554 arc0 *= 40; /* (0..80) */
555 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
556 unsigned int v = *tp;
557 v += arc0;
558 *tp = v;
559 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
560 else break;
561 }
562
563 assert(tp >= first_value);
564
565 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
566 fv - first_value, 1);
567 }
568
569 /*
570 * Save the rest of arcs.
571 */
vlmd86c9252004-08-25 01:34:11 +0000572 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
573 i < arc_slots;
574 i++, arcs = ((char *)arcs) + arc_type_size) {
vlm12557712004-06-17 23:43:39 +0000575 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
576 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000577 }
578
vlma63e0292004-06-17 23:46:45 +0000579 assert((unsigned)(bp - buf) <= size);
vlmfa67ddc2004-06-03 03:38:44 +0000580
581 /*
582 * Replace buffer.
583 */
vlm12557712004-06-17 23:43:39 +0000584 oid->size = bp - buf;
vlmfa67ddc2004-06-03 03:38:44 +0000585 bp = oid->buf;
586 oid->buf = buf;
587 if(bp) FREEMEM(bp);
588
589 return 0;
590}
vlm3717fb32004-06-14 08:17:27 +0000591