blob: 9e8f2e2a20b78083d2bbad354510e090c35975b6 [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 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <OBJECT_IDENTIFIER.h>
vlm2e3dd3b2004-06-14 07:24:36 +00007#include <limits.h> /* for CHAR_BIT */
vlmfa67ddc2004-06-03 03:38:44 +00008#include <assert.h>
9#include <errno.h>
10
11/*
12 * OBJECT IDENTIFIER basic type description.
13 */
14static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = {
15 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
16};
17asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = {
18 "OBJECT IDENTIFIER",
vlm6678cb12004-09-26 13:10:40 +000019 ASN__PRIMITIVE_TYPE_free,
vlm39ba4c42004-09-22 16:06:28 +000020 OBJECT_IDENTIFIER_print,
vlmfa67ddc2004-06-03 03:38:44 +000021 OBJECT_IDENTIFIER_constraint,
vlm6678cb12004-09-26 13:10:40 +000022 ber_decode_primitive,
23 der_encode_primitive,
vlm39ba4c42004-09-22 16:06:28 +000024 0, /* Not implemented yet */
25 OBJECT_IDENTIFIER_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +000026 0, /* Use generic outmost tag fetcher */
27 asn1_DEF_OBJECT_IDENTIFIER_tags,
28 sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
29 / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
vlm72425de2004-09-13 08:31:01 +000030 asn1_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
31 sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
32 / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
vlme413c122004-08-20 13:23:42 +000033 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000034 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000035};
36
37
vlmfa67ddc2004-06-03 03:38:44 +000038int
39OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
40 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +000041 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000042
43 if(st && st->buf) {
44 if(st->size < 1) {
vlme3f0f282004-08-11 09:44:13 +000045 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000046 "%s: at least one numerical value "
47 "expected (%s:%d)",
48 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000049 return -1;
50 }
51 } else {
vlme3f0f282004-08-11 09:44:13 +000052 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000053 "%s: value not given (%s:%d)",
54 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000055 return -1;
56 }
57
58 return 0;
59}
60
vlm3717fb32004-06-14 08:17:27 +000061
vlmfa67ddc2004-06-03 03:38:44 +000062int
vlm6678cb12004-09-26 13:10:40 +000063OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
vlm72425de2004-09-13 08:31:01 +000064 unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
vlm2e3dd3b2004-06-14 07:24:36 +000065 uint8_t *arcend = arcbuf + arclen; /* End of arc */
vlm2e3dd3b2004-06-14 07:24:36 +000066 unsigned int cache = 0; /* No more than 14 significant bits */
vlm6678cb12004-09-26 13:10:40 +000067 unsigned char *rvbuf = (unsigned char *)rvbufp;
68 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
vlm2e3dd3b2004-06-14 07:24:36 +000069 int inc; /* Return value growth direction */
vlmfa67ddc2004-06-03 03:38:44 +000070
vlm2e3dd3b2004-06-14 07:24:36 +000071 rvsize *= CHAR_BIT; /* bytes to bits */
72 arclen *= 7; /* bytes to bits */
73
74 /*
75 * The arc has the number of bits
76 * cannot be represented using supplied return value type.
77 */
78 if(arclen > rvsize) {
79 if(arclen > (rvsize + CHAR_BIT)) {
80 errno = ERANGE; /* Overflow */
81 return -1;
82 } else {
83 /*
84 * Even if the number of bits in the arc representation
85 * is higher than the width of supplied * return value
86 * type, there is still possible to fit it when there
87 * are few unused high bits in the arc value
88 * representaion.
vlm12557712004-06-17 23:43:39 +000089 *
90 * Moreover, there is a possibility that the
91 * number could actually fit the arc space, given
92 * that add is negative, but we don't handle
93 * such "temporary lack of precision" situation here.
94 * May be considered as a bug.
vlm2e3dd3b2004-06-14 07:24:36 +000095 */
96 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
97 if((*arcbuf & mask)) {
vlmfa67ddc2004-06-03 03:38:44 +000098 errno = ERANGE; /* Overflow */
99 return -1;
100 }
vlm2e3dd3b2004-06-14 07:24:36 +0000101 /* Fool the routine computing unused bits */
102 arclen -= 7;
103 cache = *arcbuf & 0x7f;
104 arcbuf++;
105 }
106 }
107
vlm3717fb32004-06-14 08:17:27 +0000108 /* Faster path for common size */
109 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
110 unsigned long accum;
111 /* Gather all bits into the accumulator */
112 for(accum = cache; arcbuf < arcend; arcbuf++)
113 accum = (accum << 7) | (*arcbuf & ~0x80);
114 if(accum < (unsigned)-add) {
115 errno = ERANGE; /* Overflow */
116 return -1;
117 }
vlm6678cb12004-09-26 13:10:40 +0000118 *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
vlm3717fb32004-06-14 08:17:27 +0000119 return 0;
120 }
121
vlm2e3dd3b2004-06-14 07:24:36 +0000122#ifndef WORDS_BIGENDIAN
123 if(*(unsigned char *)&LE) { /* Little endian (x86) */
124 /* "Convert" to big endian */
vlm6678cb12004-09-26 13:10:40 +0000125 rvbuf += rvsize / CHAR_BIT - 1;
126 rvstart--;
vlm2e3dd3b2004-06-14 07:24:36 +0000127 inc = -1; /* Descending */
vlm3717fb32004-06-14 08:17:27 +0000128 } else
vlm2e3dd3b2004-06-14 07:24:36 +0000129#endif /* !WORDS_BIGENDIAN */
vlm3717fb32004-06-14 08:17:27 +0000130 inc = +1; /* Big endian is known [at compile time] */
vlm2e3dd3b2004-06-14 07:24:36 +0000131
vlm3717fb32004-06-14 08:17:27 +0000132 {
vlm12557712004-06-17 23:43:39 +0000133 int bits; /* typically no more than 3-4 bits */
vlm3717fb32004-06-14 08:17:27 +0000134
vlm2e3dd3b2004-06-14 07:24:36 +0000135 /* Clear the high unused bits */
136 for(bits = rvsize - arclen;
137 bits > CHAR_BIT;
vlm6678cb12004-09-26 13:10:40 +0000138 rvbuf += inc, bits -= CHAR_BIT)
139 *rvbuf = 0;
vlm3717fb32004-06-14 08:17:27 +0000140
vlm2e3dd3b2004-06-14 07:24:36 +0000141 /* Fill the body of a value */
142 for(; arcbuf < arcend; arcbuf++) {
143 cache = (cache << 7) | (*arcbuf & 0x7f);
144 bits += 7;
145 if(bits >= CHAR_BIT) {
146 bits -= CHAR_BIT;
vlm6678cb12004-09-26 13:10:40 +0000147 *rvbuf = (cache >> bits);
148 rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000149 }
150 }
151 if(bits) {
vlm6678cb12004-09-26 13:10:40 +0000152 *rvbuf = cache;
153 rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000154 }
155 }
156
157 if(add) {
vlm6678cb12004-09-26 13:10:40 +0000158 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
159 int v = add + *rvbuf;
vlm2e3dd3b2004-06-14 07:24:36 +0000160 if(v & (-1 << CHAR_BIT)) {
vlm6678cb12004-09-26 13:10:40 +0000161 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
vlm2e3dd3b2004-06-14 07:24:36 +0000162 add = -1;
163 } else {
vlm6678cb12004-09-26 13:10:40 +0000164 *rvbuf = v;
vlm2e3dd3b2004-06-14 07:24:36 +0000165 break;
166 }
167 }
168 if(rvbuf == rvstart) {
169 /* No space to carry over */
vlmfa67ddc2004-06-03 03:38:44 +0000170 errno = ERANGE; /* Overflow */
171 return -1;
172 }
173 }
174
vlmfa67ddc2004-06-03 03:38:44 +0000175 return 0;
176}
177
vlm39ba4c42004-09-22 16:06:28 +0000178ssize_t
179OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
vlmfa67ddc2004-06-03 03:38:44 +0000180 asn_app_consume_bytes_f *cb, void *app_key) {
181 char scratch[64]; /* Conservative estimate */
182 unsigned long accum; /* Bits accumulator */
183 char *p; /* Position in the scratch buffer */
184
vlm2e3dd3b2004-06-14 07:24:36 +0000185 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
186 &accum, sizeof(accum)))
vlmfa67ddc2004-06-03 03:38:44 +0000187 return -1;
188
vlm3fff06b2004-08-23 09:23:02 +0000189 if(accum) {
vlm39ba4c42004-09-22 16:06:28 +0000190 ssize_t len;
191
vlm3fff06b2004-08-23 09:23:02 +0000192 /* Fill the scratch buffer in reverse. */
193 p = scratch + sizeof(scratch);
194 for(; accum; accum /= 10)
vlm39ba4c42004-09-22 16:06:28 +0000195 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
vlmfa67ddc2004-06-03 03:38:44 +0000196
vlm39ba4c42004-09-22 16:06:28 +0000197 len = sizeof(scratch) - (p - scratch);
198 if(cb(p, len, app_key) < 0)
199 return -1;
200 return len;
vlm3fff06b2004-08-23 09:23:02 +0000201 } else {
202 *scratch = 0x30;
vlm39ba4c42004-09-22 16:06:28 +0000203 if(cb(scratch, 1, app_key) < 0)
204 return -1;
205 return 1;
vlm3fff06b2004-08-23 09:23:02 +0000206 }
vlmfa67ddc2004-06-03 03:38:44 +0000207}
208
209int
vlm39ba4c42004-09-22 16:06:28 +0000210OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
211 asn_app_consume_bytes_f *cb, void *app_key) {
212
213 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
214 return -1;
215
216 return 0;
217}
218
219static ssize_t
220OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
221 ssize_t wrote_len = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000222 int startn;
223 int add = 0;
224 int i;
225
vlmfa67ddc2004-06-03 03:38:44 +0000226 for(i = 0, startn = 0; i < st->size; i++) {
227 uint8_t b = st->buf[i];
228 if((b & 0x80)) /* Continuation expected */
229 continue;
230
231 if(startn == 0) {
232 /*
233 * First two arcs are encoded through the backdoor.
234 */
235 if(i) {
236 add = -80;
vlm39ba4c42004-09-22 16:06:28 +0000237 if(cb("2", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000238 } else if(b <= 39) {
239 add = 0;
vlm39ba4c42004-09-22 16:06:28 +0000240 if(cb("0", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000241 } else if(b < 79) {
242 add = -40;
vlm39ba4c42004-09-22 16:06:28 +0000243 if(cb("1", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000244 } else {
245 add = -80;
vlm39ba4c42004-09-22 16:06:28 +0000246 if(cb("2", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000247 }
vlm39ba4c42004-09-22 16:06:28 +0000248 wrote_len += 1;
vlmfa67ddc2004-06-03 03:38:44 +0000249 }
250
vlm39ba4c42004-09-22 16:06:28 +0000251 if(cb(".", 1, app_key) < 0) /* Separate arcs */
vlmfa67ddc2004-06-03 03:38:44 +0000252 return -1;
253
vlm39ba4c42004-09-22 16:06:28 +0000254 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
255 i - startn + 1, add, cb, app_key);
256 if(add < 0) return -1;
257 wrote_len += 1 + add;
vlmfa67ddc2004-06-03 03:38:44 +0000258 startn = i + 1;
259 add = 0;
260 }
261
vlm39ba4c42004-09-22 16:06:28 +0000262 return wrote_len;
263}
264
265asn_enc_rval_t
266OBJECT_IDENTIFIER_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
267 int ilevel, enum xer_encoder_flags_e flags,
268 asn_app_consume_bytes_f *cb, void *app_key) {
269 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
270 asn_enc_rval_t er;
271
272 (void)ilevel;
273 (void)flags;
274
275 if(!st || !st->buf)
276 _ASN_ENCODE_FAILED;
277
278 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
279 if(er.encoded < 0) _ASN_ENCODE_FAILED;
280
281 return er;
282}
283
284int
285OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
286 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
287 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
288
289 (void)td; /* Unused argument */
290 (void)ilevel; /* Unused argument */
291
292 if(!st || !st->buf)
vlm6678cb12004-09-26 13:10:40 +0000293 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlm39ba4c42004-09-22 16:06:28 +0000294
295 /* Dump preamble */
vlm6678cb12004-09-26 13:10:40 +0000296 if(cb("{ ", 2, app_key) < 0)
vlm39ba4c42004-09-22 16:06:28 +0000297 return -1;
298
299 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
300 return -1;
301
vlm6678cb12004-09-26 13:10:40 +0000302 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000303}
304
305int
vlm2e3dd3b2004-06-14 07:24:36 +0000306OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
307 unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000308 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
vlm2e3dd3b2004-06-14 07:24:36 +0000309 int num_arcs = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000310 int startn = 0;
311 int add = 0;
312 int i;
313
vlm12557712004-06-17 23:43:39 +0000314 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000315 errno = EINVAL;
316 return -1;
317 }
318
319 for(i = 0; i < oid->size; i++) {
320 uint8_t b = oid->buf[i];
321 if((b & 0x80)) /* Continuation expected */
322 continue;
323
vlm2e3dd3b2004-06-14 07:24:36 +0000324 if(num_arcs == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000325 /*
326 * First two arcs are encoded through the backdoor.
327 */
vlm2e3dd3b2004-06-14 07:24:36 +0000328 unsigned LE = 1; /* Little endian */
329 int first_arc;
330 num_arcs++;
331 if(!arc_slots) { num_arcs++; continue; }
332
333 if(i) first_arc = 2;
334 else if(b <= 39) first_arc = 0;
335 else if(b < 79) first_arc = 1;
336 else first_arc = 2;
337
338 add = -40 * first_arc;
339 memset(arcs, 0, arc_type_size);
vlm1ff928d2004-08-11 08:10:13 +0000340 *(unsigned char *)((char *)arcs
vlm2e3dd3b2004-06-14 07:24:36 +0000341 + ((*(char *)&LE)?0:(arc_type_size - 1)))
342 = first_arc;
vlmd86c9252004-08-25 01:34:11 +0000343 arcs = ((char *)arcs) + arc_type_size;
vlmfa67ddc2004-06-03 03:38:44 +0000344 }
345
vlm2e3dd3b2004-06-14 07:24:36 +0000346 /* Decode, if has space */
347 if(arcs < arcs_end) {
348 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
349 i - startn + 1, add,
350 arcs, arc_type_size))
351 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000352 startn = i + 1;
vlmd86c9252004-08-25 01:34:11 +0000353 arcs = ((char *)arcs) + arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000354 add = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000355 }
vlm2e3dd3b2004-06-14 07:24:36 +0000356 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000357 }
358
vlm2e3dd3b2004-06-14 07:24:36 +0000359 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000360}
361
vlm12557712004-06-17 23:43:39 +0000362
363/*
364 * Save the single value as an object identifier arc.
365 */
vlme55716a2004-08-11 09:10:59 +0000366int
vlm12557712004-06-17 23:43:39 +0000367OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
368 /*
369 * The following conditions must hold:
370 * assert(arcval);
371 * assert(arcval_size > 0);
372 * assert(arcbuf);
373 */
374#ifdef WORDS_BIGENDIAN
375 const unsigned isLittleEndian = 0;
376#else
377 unsigned LE = 1;
378 unsigned isLittleEndian = *(char *)&LE;
379#endif
vlm12557712004-06-17 23:43:39 +0000380 uint8_t *tp, *tend;
381 unsigned int cache;
382 uint8_t *bp = arcbuf;
383 int bits;
vlm6e73a042004-08-11 07:17:22 +0000384#ifdef __GNUC__
385 uint8_t buffer[arcval_size];
386#else
387 uint8_t *buffer = alloca(arcval_size);
vlm7a6a60e2004-08-11 07:41:45 +0000388 if(!buffer) { errno = ENOMEM; return -1; }
vlm6e73a042004-08-11 07:17:22 +0000389#endif
vlm12557712004-06-17 23:43:39 +0000390
391 if(isLittleEndian && !prepared_order) {
vlm1ff928d2004-08-11 08:10:13 +0000392 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
vlmda674682004-08-11 09:07:36 +0000393 uint8_t *aend = (uint8_t *)arcval;
vlm12557712004-06-17 23:43:39 +0000394 uint8_t *msb = buffer + arcval_size - 1;
395 for(tp = buffer; a >= aend; tp++, a--)
396 if((*tp = *a) && (tp < msb))
397 msb = tp;
398 tend = &buffer[arcval_size];
399 tp = msb; /* Most significant non-zero byte */
400 } else {
401 /* Look for most significant non-zero byte */
vlm1ff928d2004-08-11 08:10:13 +0000402 tend = (unsigned char *)arcval + arcval_size;
vlmda674682004-08-11 09:07:36 +0000403 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
vlm12557712004-06-17 23:43:39 +0000404 if(*tp) break;
405 }
406
407 /*
408 * Split the value in 7-bits chunks.
409 */
410 bits = ((tend - tp) * CHAR_BIT) % 7;
411 if(bits) {
412 cache = *tp >> (CHAR_BIT - bits);
413 if(cache) {
414 *bp++ = cache | 0x80;
415 cache = *tp++;
416 bits = CHAR_BIT - bits;
417 } else {
418 bits = -bits;
419 }
420 } else {
421 cache = 0;
422 }
423 for(; tp < tend; tp++) {
424 cache = (cache << CHAR_BIT) + *tp;
425 bits += CHAR_BIT;
426 while(bits >= 7) {
427 bits -= 7;
428 *bp++ = 0x80 | (cache >> bits);
429 }
430 }
431 if(bits) *bp++ = cache;
432 bp[-1] &= 0x7f; /* Clear the last bit */
433
434 return bp - arcbuf;
435}
436
vlmfa67ddc2004-06-03 03:38:44 +0000437int
vlm12557712004-06-17 23:43:39 +0000438OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000439 uint8_t *buf;
440 uint8_t *bp;
vlm12557712004-06-17 23:43:39 +0000441 unsigned LE = 1; /* Little endian (x86) */
442 unsigned isLittleEndian = *((char *)&LE);
443 unsigned int arc0;
444 unsigned int arc1;
445 unsigned size;
vlm3717fb32004-06-14 08:17:27 +0000446 unsigned i;
vlmfa67ddc2004-06-03 03:38:44 +0000447
vlm12557712004-06-17 23:43:39 +0000448 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000449 errno = EINVAL;
450 return -1;
451 }
452
vlm12557712004-06-17 23:43:39 +0000453 switch(arc_type_size) {
454 case sizeof(char):
455 arc0 = ((unsigned char *)arcs)[0];
456 arc1 = ((unsigned char *)arcs)[1];
457 break;
458 case sizeof(short):
459 arc0 = ((unsigned short *)arcs)[0];
460 arc1 = ((unsigned short *)arcs)[1];
461 break;
462 case sizeof(int):
463 arc0 = ((unsigned int *)arcs)[0];
464 arc1 = ((unsigned int *)arcs)[1];
465 break;
466 default:
467 arc1 = arc0 = 0;
468 if(isLittleEndian) { /* Little endian (x86) */
469 unsigned char *ps, *pe;
470 /* If more significant bytes are present,
471 * make them > 255 quick */
vlm1ff928d2004-08-11 08:10:13 +0000472 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
473 ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000474 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
475 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
476 arc0 = *((unsigned char *)arcs + 0);
477 arc1 = *((unsigned char *)arcs + arc_type_size);
478 } else {
479 unsigned char *ps, *pe;
480 /* If more significant bytes are present,
481 * make them > 255 quick */
vlmda674682004-08-11 09:07:36 +0000482 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000483 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
484 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
485 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
486 }
487 }
488
489 /*
490 * The previous chapter left us with the first and the second arcs.
491 * The values are not precise (that is, they are valid only if
492 * they're less than 255), but OK for the purposes of making
493 * the sanity test below.
494 */
495 if(arc0 <= 1) {
496 if(arc1 >= 39) {
vlmfa67ddc2004-06-03 03:38:44 +0000497 /* 8.19.4: At most 39 subsequent values (including 0) */
498 errno = ERANGE;
499 return -1;
500 }
vlm12557712004-06-17 23:43:39 +0000501 } else if(arc0 > 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000502 /* 8.19.4: Only three values are allocated from the root node */
503 errno = ERANGE;
504 return -1;
505 }
vlm12557712004-06-17 23:43:39 +0000506 /*
507 * After above tests it is known that the value of arc0 is completely
508 * trustworthy (0..2). However, the arc1's value is still meaningless.
509 */
vlmfa67ddc2004-06-03 03:38:44 +0000510
511 /*
512 * Roughly estimate the maximum size necessary to encode these arcs.
vlm12557712004-06-17 23:43:39 +0000513 * This estimation implicitly takes in account the following facts,
514 * that cancel each other:
515 * * the first two arcs are encoded in a single value.
516 * * the first value may require more space (+1 byte)
517 * * the value of the first arc which is in range (0..2)
vlmfa67ddc2004-06-03 03:38:44 +0000518 */
vlm12557712004-06-17 23:43:39 +0000519 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
vlmda674682004-08-11 09:07:36 +0000520 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000521 if(!buf) {
522 /* ENOMEM */
523 return -1;
524 }
525
526 /*
vlm12557712004-06-17 23:43:39 +0000527 * Encode the first two arcs.
528 * These require special treatment.
vlmfa67ddc2004-06-03 03:38:44 +0000529 */
vlmfa67ddc2004-06-03 03:38:44 +0000530 {
vlm12557712004-06-17 23:43:39 +0000531 uint8_t *tp;
vlma51f7ce2004-08-11 07:48:19 +0000532#ifdef __GNUC__
533 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
vlm1ff928d2004-08-11 08:10:13 +0000534 uint8_t *fv = first_value;
vlma51f7ce2004-08-11 07:48:19 +0000535#else
536 uint8_t *first_value = alloca(1 + arc_type_size);
vlma51f7ce2004-08-11 07:48:19 +0000537 uint8_t *fv = first_value;
vlm1ff928d2004-08-11 08:10:13 +0000538 if(!first_value) {
539 errno = ENOMEM;
540 return -1;
541 }
542#endif
vlmfa67ddc2004-06-03 03:38:44 +0000543
vlm12557712004-06-17 23:43:39 +0000544 /*
545 * Simulate first_value = arc0 * 40 + arc1;
546 */
547 /* Copy the second (1'st) arcs[1] into the first_value */
548 *fv++ = 0;
vlmd86c9252004-08-25 01:34:11 +0000549 arcs = ((char *)arcs) + arc_type_size;
vlm12557712004-06-17 23:43:39 +0000550 if(isLittleEndian) {
vlm1ff928d2004-08-11 08:10:13 +0000551 uint8_t *aend = (unsigned char *)arcs - 1;
552 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
vlm12557712004-06-17 23:43:39 +0000553 for(; a1 > aend; fv++, a1--) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000554 } else {
vlmda674682004-08-11 09:07:36 +0000555 uint8_t *a1 = (uint8_t *)arcs;
vlm12557712004-06-17 23:43:39 +0000556 uint8_t *aend = a1 + arc_type_size;
557 for(; a1 < aend; fv++, a1++) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000558 }
vlm12557712004-06-17 23:43:39 +0000559 /* Increase the first_value by arc0 */
560 arc0 *= 40; /* (0..80) */
561 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
562 unsigned int v = *tp;
563 v += arc0;
564 *tp = v;
565 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
566 else break;
567 }
568
569 assert(tp >= first_value);
570
571 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
572 fv - first_value, 1);
573 }
574
575 /*
576 * Save the rest of arcs.
577 */
vlmd86c9252004-08-25 01:34:11 +0000578 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
579 i < arc_slots;
580 i++, arcs = ((char *)arcs) + arc_type_size) {
vlm12557712004-06-17 23:43:39 +0000581 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
582 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000583 }
584
vlma63e0292004-06-17 23:46:45 +0000585 assert((unsigned)(bp - buf) <= size);
vlmfa67ddc2004-06-03 03:38:44 +0000586
587 /*
588 * Replace buffer.
589 */
vlm12557712004-06-17 23:43:39 +0000590 oid->size = bp - buf;
vlmfa67ddc2004-06-03 03:38:44 +0000591 bp = oid->buf;
592 oid->buf = buf;
593 if(bp) FREEMEM(bp);
594
595 return 0;
596}
vlm3717fb32004-06-14 08:17:27 +0000597