blob: a44349e2d3d637117437d9edbf35432e15b3430b [file] [log] [blame]
Lev Walkinf15320b2004-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>
Lev Walkin29a044b2004-06-14 07:24:36 +00006#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-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]),
Lev Walkind9bd7752004-06-05 08:17:50 +000027 0, /* Always in primitive form */
Lev Walkin449f8322004-08-20 13:23:42 +000028 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000029 0 /* No specifics */
Lev Walkinf15320b2004-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;
Lev Walkinc2346572004-08-11 09:07:36 +000041 OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr;
Lev Walkinf15320b2004-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) {
Lev Walkinc2346572004-08-11 09:07:36 +000078 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000079
80 if(st && st->buf) {
81 if(st->size < 1) {
Lev Walkinba4e5182004-08-11 09:44:13 +000082 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000083 "%s: at least one numerical value "
84 "expected (%s:%d)",
85 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000086 return -1;
87 }
88 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000089 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000090 "%s: value not given (%s:%d)",
91 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000092 return -1;
93 }
94
95 return 0;
96}
97
Lev Walkinc4c61962004-06-14 08:17:27 +000098
Lev Walkinf15320b2004-06-03 03:38:44 +000099int
Lev Walkin29a044b2004-06-14 07:24:36 +0000100OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
101 unsigned LE = 1; /* Little endian (x86) */
102 uint8_t *arcend = arcbuf + arclen; /* End of arc */
103 void *rvstart = rvbuf; /* Original start of the value buffer */
104 unsigned int cache = 0; /* No more than 14 significant bits */
105 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +0000106
Lev Walkin29a044b2004-06-14 07:24:36 +0000107 rvsize *= CHAR_BIT; /* bytes to bits */
108 arclen *= 7; /* bytes to bits */
109
110 /*
111 * The arc has the number of bits
112 * cannot be represented using supplied return value type.
113 */
114 if(arclen > rvsize) {
115 if(arclen > (rvsize + CHAR_BIT)) {
116 errno = ERANGE; /* Overflow */
117 return -1;
118 } else {
119 /*
120 * Even if the number of bits in the arc representation
121 * is higher than the width of supplied * return value
122 * type, there is still possible to fit it when there
123 * are few unused high bits in the arc value
124 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000125 *
126 * Moreover, there is a possibility that the
127 * number could actually fit the arc space, given
128 * that add is negative, but we don't handle
129 * such "temporary lack of precision" situation here.
130 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000131 */
132 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
133 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 errno = ERANGE; /* Overflow */
135 return -1;
136 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000137 /* Fool the routine computing unused bits */
138 arclen -= 7;
139 cache = *arcbuf & 0x7f;
140 arcbuf++;
141 }
142 }
143
Lev Walkinc4c61962004-06-14 08:17:27 +0000144 /* Faster path for common size */
145 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
146 unsigned long accum;
147 /* Gather all bits into the accumulator */
148 for(accum = cache; arcbuf < arcend; arcbuf++)
149 accum = (accum << 7) | (*arcbuf & ~0x80);
150 if(accum < (unsigned)-add) {
151 errno = ERANGE; /* Overflow */
152 return -1;
153 }
154 *(unsigned long *)rvbuf = accum + add;
155 return 0;
156 }
157
Lev Walkin29a044b2004-06-14 07:24:36 +0000158#ifndef WORDS_BIGENDIAN
159 if(*(unsigned char *)&LE) { /* Little endian (x86) */
160 /* "Convert" to big endian */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000161 (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
Lev Walkinc4c61962004-06-14 08:17:27 +0000162 ((unsigned char *)rvstart)--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000163 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000164 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000165#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000166 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000167
Lev Walkinc4c61962004-06-14 08:17:27 +0000168 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000169 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000170
Lev Walkin29a044b2004-06-14 07:24:36 +0000171 /* Clear the high unused bits */
172 for(bits = rvsize - arclen;
173 bits > CHAR_BIT;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000174 (unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
Lev Walkin29a044b2004-06-14 07:24:36 +0000175 *(unsigned char *)rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000176
Lev Walkin29a044b2004-06-14 07:24:36 +0000177 /* Fill the body of a value */
178 for(; arcbuf < arcend; arcbuf++) {
179 cache = (cache << 7) | (*arcbuf & 0x7f);
180 bits += 7;
181 if(bits >= CHAR_BIT) {
182 bits -= CHAR_BIT;
183 *(unsigned char *)rvbuf = (cache >> bits);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000184 (unsigned char *)rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000185 }
186 }
187 if(bits) {
188 *(unsigned char *)rvbuf = cache;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000189 (unsigned char *)rvbuf += inc;
Lev Walkin29a044b2004-06-14 07:24:36 +0000190 }
191 }
192
193 if(add) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000194 for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
Lev Walkin29a044b2004-06-14 07:24:36 +0000195 int v = add + *(unsigned char *)rvbuf;
196 if(v & (-1 << CHAR_BIT)) {
197 *(unsigned char *)rvbuf
Lev Walkin74057d52004-08-11 09:17:15 +0000198 = (unsigned char)(v + (1 << CHAR_BIT));
Lev Walkin29a044b2004-06-14 07:24:36 +0000199 add = -1;
200 } else {
201 *(unsigned char *)rvbuf = v;
202 break;
203 }
204 }
205 if(rvbuf == rvstart) {
206 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 errno = ERANGE; /* Overflow */
208 return -1;
209 }
210 }
211
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 return 0;
213}
214
Lev Walkin29a044b2004-06-14 07:24:36 +0000215
Lev Walkinf15320b2004-06-03 03:38:44 +0000216int
217OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
218 asn_app_consume_bytes_f *cb, void *app_key) {
219 char scratch[64]; /* Conservative estimate */
220 unsigned long accum; /* Bits accumulator */
221 char *p; /* Position in the scratch buffer */
222
Lev Walkin29a044b2004-06-14 07:24:36 +0000223 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
224 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 return -1;
226
Lev Walkin3251b8e2004-08-23 09:23:02 +0000227 if(accum) {
228 /* Fill the scratch buffer in reverse. */
229 p = scratch + sizeof(scratch);
230 for(; accum; accum /= 10)
231 *(--p) = (char)(accum % 10) + 0x30;
Lev Walkinf15320b2004-06-03 03:38:44 +0000232
Lev Walkin3251b8e2004-08-23 09:23:02 +0000233 return cb(p, sizeof(scratch) - (p - scratch), app_key);
234 } else {
235 *scratch = 0x30;
236 return cb(scratch, 1, app_key);
237 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000238}
239
240int
241OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
242 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000243 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 int startn;
245 int add = 0;
246 int i;
247
Lev Walkind9bd7752004-06-05 08:17:50 +0000248 (void)td; /* Unused argument */
249 (void)ilevel; /* Unused argument */
250
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 if(!st || !st->buf)
252 return cb("<absent>", 8, app_key);
253
254 /* Dump preamble */
255 if(cb("{ ", 2, app_key))
256 return -1;
257
258 for(i = 0, startn = 0; i < st->size; i++) {
259 uint8_t b = st->buf[i];
260 if((b & 0x80)) /* Continuation expected */
261 continue;
262
263 if(startn == 0) {
264 /*
265 * First two arcs are encoded through the backdoor.
266 */
267 if(i) {
268 add = -80;
269 if(cb("2", 1, app_key)) return -1;
270 } else if(b <= 39) {
271 add = 0;
272 if(cb("0", 1, app_key)) return -1;
273 } else if(b < 79) {
274 add = -40;
275 if(cb("1", 1, app_key)) return -1;
276 } else {
277 add = -80;
278 if(cb("2", 1, app_key)) return -1;
279 }
280 }
281
282 if(cb(" ", 1, app_key)) /* Separate arcs */
283 return -1;
284
285 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
286 i - startn + 1, add,
287 cb, app_key))
288 return -1;
289 startn = i + 1;
290 add = 0;
291 }
292
293 return cb(" }", 2, app_key);
294}
295
296int
Lev Walkin29a044b2004-06-14 07:24:36 +0000297OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
298 unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000299 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
Lev Walkin29a044b2004-06-14 07:24:36 +0000300 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 int startn = 0;
302 int add = 0;
303 int i;
304
Lev Walkin0787ff02004-06-17 23:43:39 +0000305 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000306 errno = EINVAL;
307 return -1;
308 }
309
310 for(i = 0; i < oid->size; i++) {
311 uint8_t b = oid->buf[i];
312 if((b & 0x80)) /* Continuation expected */
313 continue;
314
Lev Walkin29a044b2004-06-14 07:24:36 +0000315 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000316 /*
317 * First two arcs are encoded through the backdoor.
318 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000319 unsigned LE = 1; /* Little endian */
320 int first_arc;
321 num_arcs++;
322 if(!arc_slots) { num_arcs++; continue; }
323
324 if(i) first_arc = 2;
325 else if(b <= 39) first_arc = 0;
326 else if(b < 79) first_arc = 1;
327 else first_arc = 2;
328
329 add = -40 * first_arc;
330 memset(arcs, 0, arc_type_size);
Lev Walkin4d9528c2004-08-11 08:10:13 +0000331 *(unsigned char *)((char *)arcs
Lev Walkin29a044b2004-06-14 07:24:36 +0000332 + ((*(char *)&LE)?0:(arc_type_size - 1)))
333 = first_arc;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000334 arcs = ((char *)arcs) + arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 }
336
Lev Walkin29a044b2004-06-14 07:24:36 +0000337 /* Decode, if has space */
338 if(arcs < arcs_end) {
339 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
340 i - startn + 1, add,
341 arcs, arc_type_size))
342 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 startn = i + 1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000344 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000345 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000346 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000347 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000348 }
349
Lev Walkin29a044b2004-06-14 07:24:36 +0000350 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000351}
352
Lev Walkin0787ff02004-06-17 23:43:39 +0000353
354/*
355 * Save the single value as an object identifier arc.
356 */
Lev Walkin91f5cd02004-08-11 09:10:59 +0000357int
Lev Walkin0787ff02004-06-17 23:43:39 +0000358OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
359 /*
360 * The following conditions must hold:
361 * assert(arcval);
362 * assert(arcval_size > 0);
363 * assert(arcbuf);
364 */
365#ifdef WORDS_BIGENDIAN
366 const unsigned isLittleEndian = 0;
367#else
368 unsigned LE = 1;
369 unsigned isLittleEndian = *(char *)&LE;
370#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000371 uint8_t *tp, *tend;
372 unsigned int cache;
373 uint8_t *bp = arcbuf;
374 int bits;
Lev Walkin64399722004-08-11 07:17:22 +0000375#ifdef __GNUC__
376 uint8_t buffer[arcval_size];
377#else
378 uint8_t *buffer = alloca(arcval_size);
Lev Walkin7e0d2cb2004-08-11 07:41:45 +0000379 if(!buffer) { errno = ENOMEM; return -1; }
Lev Walkin64399722004-08-11 07:17:22 +0000380#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000381
382 if(isLittleEndian && !prepared_order) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000383 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
Lev Walkinc2346572004-08-11 09:07:36 +0000384 uint8_t *aend = (uint8_t *)arcval;
Lev Walkin0787ff02004-06-17 23:43:39 +0000385 uint8_t *msb = buffer + arcval_size - 1;
386 for(tp = buffer; a >= aend; tp++, a--)
387 if((*tp = *a) && (tp < msb))
388 msb = tp;
389 tend = &buffer[arcval_size];
390 tp = msb; /* Most significant non-zero byte */
391 } else {
392 /* Look for most significant non-zero byte */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000393 tend = (unsigned char *)arcval + arcval_size;
Lev Walkinc2346572004-08-11 09:07:36 +0000394 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000395 if(*tp) break;
396 }
397
398 /*
399 * Split the value in 7-bits chunks.
400 */
401 bits = ((tend - tp) * CHAR_BIT) % 7;
402 if(bits) {
403 cache = *tp >> (CHAR_BIT - bits);
404 if(cache) {
405 *bp++ = cache | 0x80;
406 cache = *tp++;
407 bits = CHAR_BIT - bits;
408 } else {
409 bits = -bits;
410 }
411 } else {
412 cache = 0;
413 }
414 for(; tp < tend; tp++) {
415 cache = (cache << CHAR_BIT) + *tp;
416 bits += CHAR_BIT;
417 while(bits >= 7) {
418 bits -= 7;
419 *bp++ = 0x80 | (cache >> bits);
420 }
421 }
422 if(bits) *bp++ = cache;
423 bp[-1] &= 0x7f; /* Clear the last bit */
424
425 return bp - arcbuf;
426}
427
Lev Walkinf15320b2004-06-03 03:38:44 +0000428int
Lev Walkin0787ff02004-06-17 23:43:39 +0000429OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 uint8_t *buf;
431 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000432 unsigned LE = 1; /* Little endian (x86) */
433 unsigned isLittleEndian = *((char *)&LE);
434 unsigned int arc0;
435 unsigned int arc1;
436 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000437 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000438
Lev Walkin0787ff02004-06-17 23:43:39 +0000439 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000440 errno = EINVAL;
441 return -1;
442 }
443
Lev Walkin0787ff02004-06-17 23:43:39 +0000444 switch(arc_type_size) {
445 case sizeof(char):
446 arc0 = ((unsigned char *)arcs)[0];
447 arc1 = ((unsigned char *)arcs)[1];
448 break;
449 case sizeof(short):
450 arc0 = ((unsigned short *)arcs)[0];
451 arc1 = ((unsigned short *)arcs)[1];
452 break;
453 case sizeof(int):
454 arc0 = ((unsigned int *)arcs)[0];
455 arc1 = ((unsigned int *)arcs)[1];
456 break;
457 default:
458 arc1 = arc0 = 0;
459 if(isLittleEndian) { /* Little endian (x86) */
460 unsigned char *ps, *pe;
461 /* If more significant bytes are present,
462 * make them > 255 quick */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000463 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
464 ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000465 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
466 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
467 arc0 = *((unsigned char *)arcs + 0);
468 arc1 = *((unsigned char *)arcs + arc_type_size);
469 } else {
470 unsigned char *ps, *pe;
471 /* If more significant bytes are present,
472 * make them > 255 quick */
Lev Walkinc2346572004-08-11 09:07:36 +0000473 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
Lev Walkin0787ff02004-06-17 23:43:39 +0000474 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
475 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
476 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
477 }
478 }
479
480 /*
481 * The previous chapter left us with the first and the second arcs.
482 * The values are not precise (that is, they are valid only if
483 * they're less than 255), but OK for the purposes of making
484 * the sanity test below.
485 */
486 if(arc0 <= 1) {
487 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 /* 8.19.4: At most 39 subsequent values (including 0) */
489 errno = ERANGE;
490 return -1;
491 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000492 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000493 /* 8.19.4: Only three values are allocated from the root node */
494 errno = ERANGE;
495 return -1;
496 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000497 /*
498 * After above tests it is known that the value of arc0 is completely
499 * trustworthy (0..2). However, the arc1's value is still meaningless.
500 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000501
502 /*
503 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000504 * This estimation implicitly takes in account the following facts,
505 * that cancel each other:
506 * * the first two arcs are encoded in a single value.
507 * * the first value may require more space (+1 byte)
508 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000509 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000510 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000511 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000512 if(!buf) {
513 /* ENOMEM */
514 return -1;
515 }
516
517 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000518 * Encode the first two arcs.
519 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000522 uint8_t *tp;
Lev Walkin90fcd442004-08-11 07:48:19 +0000523#ifdef __GNUC__
524 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000525 uint8_t *fv = first_value;
Lev Walkin90fcd442004-08-11 07:48:19 +0000526#else
527 uint8_t *first_value = alloca(1 + arc_type_size);
Lev Walkin90fcd442004-08-11 07:48:19 +0000528 uint8_t *fv = first_value;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000529 if(!first_value) {
530 errno = ENOMEM;
531 return -1;
532 }
533#endif
Lev Walkinf15320b2004-06-03 03:38:44 +0000534
Lev Walkin0787ff02004-06-17 23:43:39 +0000535 /*
536 * Simulate first_value = arc0 * 40 + arc1;
537 */
538 /* Copy the second (1'st) arcs[1] into the first_value */
539 *fv++ = 0;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000540 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin0787ff02004-06-17 23:43:39 +0000541 if(isLittleEndian) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000542 uint8_t *aend = (unsigned char *)arcs - 1;
543 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
Lev Walkin0787ff02004-06-17 23:43:39 +0000544 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000545 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000546 uint8_t *a1 = (uint8_t *)arcs;
Lev Walkin0787ff02004-06-17 23:43:39 +0000547 uint8_t *aend = a1 + arc_type_size;
548 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000549 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000550 /* Increase the first_value by arc0 */
551 arc0 *= 40; /* (0..80) */
552 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
553 unsigned int v = *tp;
554 v += arc0;
555 *tp = v;
556 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
557 else break;
558 }
559
560 assert(tp >= first_value);
561
562 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
563 fv - first_value, 1);
564 }
565
566 /*
567 * Save the rest of arcs.
568 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000569 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
570 i < arc_slots;
571 i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000572 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
573 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 }
575
Lev Walkin34b2a932004-06-17 23:46:45 +0000576 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000577
578 /*
579 * Replace buffer.
580 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000581 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000582 bp = oid->buf;
583 oid->buf = buf;
584 if(bp) FREEMEM(bp);
585
586 return 0;
587}
Lev Walkinc4c61962004-06-14 08:17:27 +0000588