blob: a08f18a95a25704fa7df50eb9c24e885b8247794 [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 */
vlmef6355b2004-09-29 13:26:15 +000014static ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
vlmfa67ddc2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
16};
vlmef6355b2004-09-29 13:26:15 +000017asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
vlmfa67ddc2004-06-03 03:38:44 +000018 "OBJECT IDENTIFIER",
vlm9de248e2004-10-20 15:50:55 +000019 "OBJECT_IDENTIFIER",
vlm6678cb12004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
vlm39ba4c42004-09-22 16:06:28 +000021 OBJECT_IDENTIFIER_print,
vlmfa67ddc2004-06-03 03:38:44 +000022 OBJECT_IDENTIFIER_constraint,
vlm6678cb12004-09-26 13:10:40 +000023 ber_decode_primitive,
24 der_encode_primitive,
vlm39ba4c42004-09-22 16:06:28 +000025 0, /* Not implemented yet */
26 OBJECT_IDENTIFIER_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +000027 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +000028 asn_DEF_OBJECT_IDENTIFIER_tags,
29 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
30 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
31 asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
32 sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
33 / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
vlme413c122004-08-20 13:23:42 +000034 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000035 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000036};
37
38
vlmfa67ddc2004-06-03 03:38:44 +000039int
vlmef6355b2004-09-29 13:26:15 +000040OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +000041 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +000042 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000043
44 if(st && st->buf) {
45 if(st->size < 1) {
vlme3f0f282004-08-11 09:44:13 +000046 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000047 "%s: at least one numerical value "
48 "expected (%s:%d)",
49 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000050 return -1;
51 }
52 } else {
vlme3f0f282004-08-11 09:44:13 +000053 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000054 "%s: value not given (%s:%d)",
55 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000056 return -1;
57 }
58
59 return 0;
60}
61
vlm3717fb32004-06-14 08:17:27 +000062
vlmfa67ddc2004-06-03 03:38:44 +000063int
vlm6678cb12004-09-26 13:10:40 +000064OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
vlm72425de2004-09-13 08:31:01 +000065 unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
vlm2e3dd3b2004-06-14 07:24:36 +000066 uint8_t *arcend = arcbuf + arclen; /* End of arc */
vlm2e3dd3b2004-06-14 07:24:36 +000067 unsigned int cache = 0; /* No more than 14 significant bits */
vlm6678cb12004-09-26 13:10:40 +000068 unsigned char *rvbuf = (unsigned char *)rvbufp;
69 unsigned char *rvstart = rvbuf; /* Original start of the value buffer */
vlm2e3dd3b2004-06-14 07:24:36 +000070 int inc; /* Return value growth direction */
vlmfa67ddc2004-06-03 03:38:44 +000071
vlm2e3dd3b2004-06-14 07:24:36 +000072 rvsize *= CHAR_BIT; /* bytes to bits */
73 arclen *= 7; /* bytes to bits */
74
75 /*
76 * The arc has the number of bits
77 * cannot be represented using supplied return value type.
78 */
79 if(arclen > rvsize) {
80 if(arclen > (rvsize + CHAR_BIT)) {
81 errno = ERANGE; /* Overflow */
82 return -1;
83 } else {
84 /*
85 * Even if the number of bits in the arc representation
86 * is higher than the width of supplied * return value
87 * type, there is still possible to fit it when there
88 * are few unused high bits in the arc value
89 * representaion.
vlm12557712004-06-17 23:43:39 +000090 *
91 * Moreover, there is a possibility that the
92 * number could actually fit the arc space, given
93 * that add is negative, but we don't handle
94 * such "temporary lack of precision" situation here.
95 * May be considered as a bug.
vlm2e3dd3b2004-06-14 07:24:36 +000096 */
97 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
98 if((*arcbuf & mask)) {
vlmfa67ddc2004-06-03 03:38:44 +000099 errno = ERANGE; /* Overflow */
100 return -1;
101 }
vlm2e3dd3b2004-06-14 07:24:36 +0000102 /* Fool the routine computing unused bits */
103 arclen -= 7;
104 cache = *arcbuf & 0x7f;
105 arcbuf++;
106 }
107 }
108
vlm3717fb32004-06-14 08:17:27 +0000109 /* Faster path for common size */
110 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
111 unsigned long accum;
112 /* Gather all bits into the accumulator */
113 for(accum = cache; arcbuf < arcend; arcbuf++)
114 accum = (accum << 7) | (*arcbuf & ~0x80);
115 if(accum < (unsigned)-add) {
116 errno = ERANGE; /* Overflow */
117 return -1;
118 }
vlm6678cb12004-09-26 13:10:40 +0000119 *(unsigned long *)rvbuf = accum + add; /* alignment OK! */
vlm3717fb32004-06-14 08:17:27 +0000120 return 0;
121 }
122
vlm2e3dd3b2004-06-14 07:24:36 +0000123#ifndef WORDS_BIGENDIAN
124 if(*(unsigned char *)&LE) { /* Little endian (x86) */
125 /* "Convert" to big endian */
vlm6678cb12004-09-26 13:10:40 +0000126 rvbuf += rvsize / CHAR_BIT - 1;
127 rvstart--;
vlm2e3dd3b2004-06-14 07:24:36 +0000128 inc = -1; /* Descending */
vlm3717fb32004-06-14 08:17:27 +0000129 } else
vlm2e3dd3b2004-06-14 07:24:36 +0000130#endif /* !WORDS_BIGENDIAN */
vlm3717fb32004-06-14 08:17:27 +0000131 inc = +1; /* Big endian is known [at compile time] */
vlm2e3dd3b2004-06-14 07:24:36 +0000132
vlm3717fb32004-06-14 08:17:27 +0000133 {
vlm12557712004-06-17 23:43:39 +0000134 int bits; /* typically no more than 3-4 bits */
vlm3717fb32004-06-14 08:17:27 +0000135
vlm2e3dd3b2004-06-14 07:24:36 +0000136 /* Clear the high unused bits */
137 for(bits = rvsize - arclen;
138 bits > CHAR_BIT;
vlm6678cb12004-09-26 13:10:40 +0000139 rvbuf += inc, bits -= CHAR_BIT)
140 *rvbuf = 0;
vlm3717fb32004-06-14 08:17:27 +0000141
vlm2e3dd3b2004-06-14 07:24:36 +0000142 /* Fill the body of a value */
143 for(; arcbuf < arcend; arcbuf++) {
144 cache = (cache << 7) | (*arcbuf & 0x7f);
145 bits += 7;
146 if(bits >= CHAR_BIT) {
147 bits -= CHAR_BIT;
vlm6678cb12004-09-26 13:10:40 +0000148 *rvbuf = (cache >> bits);
149 rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000150 }
151 }
152 if(bits) {
vlm6678cb12004-09-26 13:10:40 +0000153 *rvbuf = cache;
154 rvbuf += inc;
vlm2e3dd3b2004-06-14 07:24:36 +0000155 }
156 }
157
158 if(add) {
vlm6678cb12004-09-26 13:10:40 +0000159 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
160 int v = add + *rvbuf;
vlm2e3dd3b2004-06-14 07:24:36 +0000161 if(v & (-1 << CHAR_BIT)) {
vlm6678cb12004-09-26 13:10:40 +0000162 *rvbuf = (unsigned char)(v + (1 << CHAR_BIT));
vlm2e3dd3b2004-06-14 07:24:36 +0000163 add = -1;
164 } else {
vlm6678cb12004-09-26 13:10:40 +0000165 *rvbuf = v;
vlm2e3dd3b2004-06-14 07:24:36 +0000166 break;
167 }
168 }
169 if(rvbuf == rvstart) {
170 /* No space to carry over */
vlmfa67ddc2004-06-03 03:38:44 +0000171 errno = ERANGE; /* Overflow */
172 return -1;
173 }
174 }
175
vlmfa67ddc2004-06-03 03:38:44 +0000176 return 0;
177}
178
vlm39ba4c42004-09-22 16:06:28 +0000179ssize_t
180OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add,
vlmfa67ddc2004-06-03 03:38:44 +0000181 asn_app_consume_bytes_f *cb, void *app_key) {
182 char scratch[64]; /* Conservative estimate */
183 unsigned long accum; /* Bits accumulator */
184 char *p; /* Position in the scratch buffer */
185
vlm2e3dd3b2004-06-14 07:24:36 +0000186 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
187 &accum, sizeof(accum)))
vlmfa67ddc2004-06-03 03:38:44 +0000188 return -1;
189
vlm3fff06b2004-08-23 09:23:02 +0000190 if(accum) {
vlm39ba4c42004-09-22 16:06:28 +0000191 ssize_t len;
192
vlm3fff06b2004-08-23 09:23:02 +0000193 /* Fill the scratch buffer in reverse. */
194 p = scratch + sizeof(scratch);
195 for(; accum; accum /= 10)
vlm39ba4c42004-09-22 16:06:28 +0000196 *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */
vlmfa67ddc2004-06-03 03:38:44 +0000197
vlm39ba4c42004-09-22 16:06:28 +0000198 len = sizeof(scratch) - (p - scratch);
199 if(cb(p, len, app_key) < 0)
200 return -1;
201 return len;
vlm3fff06b2004-08-23 09:23:02 +0000202 } else {
203 *scratch = 0x30;
vlm39ba4c42004-09-22 16:06:28 +0000204 if(cb(scratch, 1, app_key) < 0)
205 return -1;
206 return 1;
vlm3fff06b2004-08-23 09:23:02 +0000207 }
vlmfa67ddc2004-06-03 03:38:44 +0000208}
209
210int
vlm39ba4c42004-09-22 16:06:28 +0000211OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
212 asn_app_consume_bytes_f *cb, void *app_key) {
213
214 if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0)
215 return -1;
216
217 return 0;
218}
219
220static ssize_t
221OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
222 ssize_t wrote_len = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000223 int startn;
224 int add = 0;
225 int i;
226
vlmfa67ddc2004-06-03 03:38:44 +0000227 for(i = 0, startn = 0; i < st->size; i++) {
228 uint8_t b = st->buf[i];
229 if((b & 0x80)) /* Continuation expected */
230 continue;
231
232 if(startn == 0) {
233 /*
234 * First two arcs are encoded through the backdoor.
235 */
236 if(i) {
237 add = -80;
vlm39ba4c42004-09-22 16:06:28 +0000238 if(cb("2", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000239 } else if(b <= 39) {
240 add = 0;
vlm39ba4c42004-09-22 16:06:28 +0000241 if(cb("0", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000242 } else if(b < 79) {
243 add = -40;
vlm39ba4c42004-09-22 16:06:28 +0000244 if(cb("1", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000245 } else {
246 add = -80;
vlm39ba4c42004-09-22 16:06:28 +0000247 if(cb("2", 1, app_key) < 0) return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000248 }
vlm39ba4c42004-09-22 16:06:28 +0000249 wrote_len += 1;
vlmfa67ddc2004-06-03 03:38:44 +0000250 }
251
vlm39ba4c42004-09-22 16:06:28 +0000252 if(cb(".", 1, app_key) < 0) /* Separate arcs */
vlmfa67ddc2004-06-03 03:38:44 +0000253 return -1;
254
vlm39ba4c42004-09-22 16:06:28 +0000255 add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
256 i - startn + 1, add, cb, app_key);
257 if(add < 0) return -1;
258 wrote_len += 1 + add;
vlmfa67ddc2004-06-03 03:38:44 +0000259 startn = i + 1;
260 add = 0;
261 }
262
vlm39ba4c42004-09-22 16:06:28 +0000263 return wrote_len;
264}
265
266asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000267OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000268 int ilevel, enum xer_encoder_flags_e flags,
269 asn_app_consume_bytes_f *cb, void *app_key) {
270 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
271 asn_enc_rval_t er;
272
273 (void)ilevel;
274 (void)flags;
275
276 if(!st || !st->buf)
277 _ASN_ENCODE_FAILED;
278
279 er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
280 if(er.encoded < 0) _ASN_ENCODE_FAILED;
281
282 return er;
283}
284
285int
vlmef6355b2004-09-29 13:26:15 +0000286OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000287 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
288 const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
289
290 (void)td; /* Unused argument */
291 (void)ilevel; /* Unused argument */
292
293 if(!st || !st->buf)
vlm6678cb12004-09-26 13:10:40 +0000294 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlm39ba4c42004-09-22 16:06:28 +0000295
296 /* Dump preamble */
vlm6678cb12004-09-26 13:10:40 +0000297 if(cb("{ ", 2, app_key) < 0)
vlm39ba4c42004-09-22 16:06:28 +0000298 return -1;
299
300 if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0)
301 return -1;
302
vlm6678cb12004-09-26 13:10:40 +0000303 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000304}
305
306int
vlm2e3dd3b2004-06-14 07:24:36 +0000307OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
308 unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000309 void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
vlm2e3dd3b2004-06-14 07:24:36 +0000310 int num_arcs = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000311 int startn = 0;
312 int add = 0;
313 int i;
314
vlm12557712004-06-17 23:43:39 +0000315 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000316 errno = EINVAL;
317 return -1;
318 }
319
320 for(i = 0; i < oid->size; i++) {
321 uint8_t b = oid->buf[i];
322 if((b & 0x80)) /* Continuation expected */
323 continue;
324
vlm2e3dd3b2004-06-14 07:24:36 +0000325 if(num_arcs == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000326 /*
327 * First two arcs are encoded through the backdoor.
328 */
vlm2e3dd3b2004-06-14 07:24:36 +0000329 unsigned LE = 1; /* Little endian */
330 int first_arc;
331 num_arcs++;
332 if(!arc_slots) { num_arcs++; continue; }
333
334 if(i) first_arc = 2;
335 else if(b <= 39) first_arc = 0;
336 else if(b < 79) first_arc = 1;
337 else first_arc = 2;
338
339 add = -40 * first_arc;
340 memset(arcs, 0, arc_type_size);
vlm1ff928d2004-08-11 08:10:13 +0000341 *(unsigned char *)((char *)arcs
vlm2e3dd3b2004-06-14 07:24:36 +0000342 + ((*(char *)&LE)?0:(arc_type_size - 1)))
343 = first_arc;
vlmd86c9252004-08-25 01:34:11 +0000344 arcs = ((char *)arcs) + arc_type_size;
vlmfa67ddc2004-06-03 03:38:44 +0000345 }
346
vlm2e3dd3b2004-06-14 07:24:36 +0000347 /* Decode, if has space */
348 if(arcs < arcs_end) {
349 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
350 i - startn + 1, add,
351 arcs, arc_type_size))
352 return -1;
vlmfa67ddc2004-06-03 03:38:44 +0000353 startn = i + 1;
vlmd86c9252004-08-25 01:34:11 +0000354 arcs = ((char *)arcs) + arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000355 add = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000356 }
vlm2e3dd3b2004-06-14 07:24:36 +0000357 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000358 }
359
vlm2e3dd3b2004-06-14 07:24:36 +0000360 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000361}
362
vlm12557712004-06-17 23:43:39 +0000363
364/*
365 * Save the single value as an object identifier arc.
366 */
vlme55716a2004-08-11 09:10:59 +0000367int
vlm12557712004-06-17 23:43:39 +0000368OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
369 /*
370 * The following conditions must hold:
371 * assert(arcval);
372 * assert(arcval_size > 0);
373 * assert(arcbuf);
374 */
375#ifdef WORDS_BIGENDIAN
376 const unsigned isLittleEndian = 0;
377#else
378 unsigned LE = 1;
379 unsigned isLittleEndian = *(char *)&LE;
380#endif
vlm12557712004-06-17 23:43:39 +0000381 uint8_t *tp, *tend;
382 unsigned int cache;
383 uint8_t *bp = arcbuf;
384 int bits;
vlm6e73a042004-08-11 07:17:22 +0000385#ifdef __GNUC__
386 uint8_t buffer[arcval_size];
387#else
388 uint8_t *buffer = alloca(arcval_size);
vlm7a6a60e2004-08-11 07:41:45 +0000389 if(!buffer) { errno = ENOMEM; return -1; }
vlm6e73a042004-08-11 07:17:22 +0000390#endif
vlm12557712004-06-17 23:43:39 +0000391
392 if(isLittleEndian && !prepared_order) {
vlm1ff928d2004-08-11 08:10:13 +0000393 uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
vlmda674682004-08-11 09:07:36 +0000394 uint8_t *aend = (uint8_t *)arcval;
vlm12557712004-06-17 23:43:39 +0000395 uint8_t *msb = buffer + arcval_size - 1;
396 for(tp = buffer; a >= aend; tp++, a--)
397 if((*tp = *a) && (tp < msb))
398 msb = tp;
399 tend = &buffer[arcval_size];
400 tp = msb; /* Most significant non-zero byte */
401 } else {
402 /* Look for most significant non-zero byte */
vlm1ff928d2004-08-11 08:10:13 +0000403 tend = (unsigned char *)arcval + arcval_size;
vlmda674682004-08-11 09:07:36 +0000404 for(tp = (uint8_t *)arcval; tp < tend - 1; tp++)
vlm12557712004-06-17 23:43:39 +0000405 if(*tp) break;
406 }
407
408 /*
409 * Split the value in 7-bits chunks.
410 */
411 bits = ((tend - tp) * CHAR_BIT) % 7;
412 if(bits) {
413 cache = *tp >> (CHAR_BIT - bits);
414 if(cache) {
415 *bp++ = cache | 0x80;
416 cache = *tp++;
417 bits = CHAR_BIT - bits;
418 } else {
419 bits = -bits;
420 }
421 } else {
422 cache = 0;
423 }
424 for(; tp < tend; tp++) {
425 cache = (cache << CHAR_BIT) + *tp;
426 bits += CHAR_BIT;
427 while(bits >= 7) {
428 bits -= 7;
429 *bp++ = 0x80 | (cache >> bits);
430 }
431 }
432 if(bits) *bp++ = cache;
433 bp[-1] &= 0x7f; /* Clear the last bit */
434
435 return bp - arcbuf;
436}
437
vlmfa67ddc2004-06-03 03:38:44 +0000438int
vlm12557712004-06-17 23:43:39 +0000439OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000440 uint8_t *buf;
441 uint8_t *bp;
vlm12557712004-06-17 23:43:39 +0000442 unsigned LE = 1; /* Little endian (x86) */
443 unsigned isLittleEndian = *((char *)&LE);
444 unsigned int arc0;
445 unsigned int arc1;
446 unsigned size;
vlm3717fb32004-06-14 08:17:27 +0000447 unsigned i;
vlmfa67ddc2004-06-03 03:38:44 +0000448
vlm12557712004-06-17 23:43:39 +0000449 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000450 errno = EINVAL;
451 return -1;
452 }
453
vlm12557712004-06-17 23:43:39 +0000454 switch(arc_type_size) {
455 case sizeof(char):
456 arc0 = ((unsigned char *)arcs)[0];
457 arc1 = ((unsigned char *)arcs)[1];
458 break;
459 case sizeof(short):
460 arc0 = ((unsigned short *)arcs)[0];
461 arc1 = ((unsigned short *)arcs)[1];
462 break;
463 case sizeof(int):
464 arc0 = ((unsigned int *)arcs)[0];
465 arc1 = ((unsigned int *)arcs)[1];
466 break;
467 default:
468 arc1 = arc0 = 0;
469 if(isLittleEndian) { /* Little endian (x86) */
470 unsigned char *ps, *pe;
471 /* If more significant bytes are present,
472 * make them > 255 quick */
vlm1ff928d2004-08-11 08:10:13 +0000473 for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
474 ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000475 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
476 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
477 arc0 = *((unsigned char *)arcs + 0);
478 arc1 = *((unsigned char *)arcs + arc_type_size);
479 } else {
480 unsigned char *ps, *pe;
481 /* If more significant bytes are present,
482 * make them > 255 quick */
vlmda674682004-08-11 09:07:36 +0000483 for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
vlm12557712004-06-17 23:43:39 +0000484 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
485 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
486 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
487 }
488 }
489
490 /*
491 * The previous chapter left us with the first and the second arcs.
492 * The values are not precise (that is, they are valid only if
493 * they're less than 255), but OK for the purposes of making
494 * the sanity test below.
495 */
496 if(arc0 <= 1) {
497 if(arc1 >= 39) {
vlmfa67ddc2004-06-03 03:38:44 +0000498 /* 8.19.4: At most 39 subsequent values (including 0) */
499 errno = ERANGE;
500 return -1;
501 }
vlm12557712004-06-17 23:43:39 +0000502 } else if(arc0 > 2) {
vlmfa67ddc2004-06-03 03:38:44 +0000503 /* 8.19.4: Only three values are allocated from the root node */
504 errno = ERANGE;
505 return -1;
506 }
vlm12557712004-06-17 23:43:39 +0000507 /*
508 * After above tests it is known that the value of arc0 is completely
509 * trustworthy (0..2). However, the arc1's value is still meaningless.
510 */
vlmfa67ddc2004-06-03 03:38:44 +0000511
512 /*
513 * Roughly estimate the maximum size necessary to encode these arcs.
vlm12557712004-06-17 23:43:39 +0000514 * This estimation implicitly takes in account the following facts,
515 * that cancel each other:
516 * * the first two arcs are encoded in a single value.
517 * * the first value may require more space (+1 byte)
518 * * the value of the first arc which is in range (0..2)
vlmfa67ddc2004-06-03 03:38:44 +0000519 */
vlm12557712004-06-17 23:43:39 +0000520 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
vlmda674682004-08-11 09:07:36 +0000521 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000522 if(!buf) {
523 /* ENOMEM */
524 return -1;
525 }
526
527 /*
vlm12557712004-06-17 23:43:39 +0000528 * Encode the first two arcs.
529 * These require special treatment.
vlmfa67ddc2004-06-03 03:38:44 +0000530 */
vlmfa67ddc2004-06-03 03:38:44 +0000531 {
vlm12557712004-06-17 23:43:39 +0000532 uint8_t *tp;
vlma51f7ce2004-08-11 07:48:19 +0000533#ifdef __GNUC__
534 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
vlm1ff928d2004-08-11 08:10:13 +0000535 uint8_t *fv = first_value;
vlma51f7ce2004-08-11 07:48:19 +0000536#else
537 uint8_t *first_value = alloca(1 + arc_type_size);
vlma51f7ce2004-08-11 07:48:19 +0000538 uint8_t *fv = first_value;
vlm1ff928d2004-08-11 08:10:13 +0000539 if(!first_value) {
540 errno = ENOMEM;
541 return -1;
542 }
543#endif
vlmfa67ddc2004-06-03 03:38:44 +0000544
vlm12557712004-06-17 23:43:39 +0000545 /*
546 * Simulate first_value = arc0 * 40 + arc1;
547 */
548 /* Copy the second (1'st) arcs[1] into the first_value */
549 *fv++ = 0;
vlmd86c9252004-08-25 01:34:11 +0000550 arcs = ((char *)arcs) + arc_type_size;
vlm12557712004-06-17 23:43:39 +0000551 if(isLittleEndian) {
vlm1ff928d2004-08-11 08:10:13 +0000552 uint8_t *aend = (unsigned char *)arcs - 1;
553 uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
vlm12557712004-06-17 23:43:39 +0000554 for(; a1 > aend; fv++, a1--) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000555 } else {
vlmda674682004-08-11 09:07:36 +0000556 uint8_t *a1 = (uint8_t *)arcs;
vlm12557712004-06-17 23:43:39 +0000557 uint8_t *aend = a1 + arc_type_size;
558 for(; a1 < aend; fv++, a1++) *fv = *a1;
vlmfa67ddc2004-06-03 03:38:44 +0000559 }
vlm12557712004-06-17 23:43:39 +0000560 /* Increase the first_value by arc0 */
561 arc0 *= 40; /* (0..80) */
562 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
563 unsigned int v = *tp;
564 v += arc0;
565 *tp = v;
566 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
567 else break;
568 }
569
570 assert(tp >= first_value);
571
572 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
573 fv - first_value, 1);
574 }
575
576 /*
577 * Save the rest of arcs.
578 */
vlmd86c9252004-08-25 01:34:11 +0000579 for(arcs = ((char *)arcs) + arc_type_size, i = 2;
580 i < arc_slots;
581 i++, arcs = ((char *)arcs) + arc_type_size) {
vlm12557712004-06-17 23:43:39 +0000582 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
583 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000584 }
585
vlma63e0292004-06-17 23:46:45 +0000586 assert((unsigned)(bp - buf) <= size);
vlmfa67ddc2004-06-03 03:38:44 +0000587
588 /*
589 * Replace buffer.
590 */
vlm12557712004-06-17 23:43:39 +0000591 oid->size = bp - buf;
vlmfa67ddc2004-06-03 03:38:44 +0000592 bp = oid->buf;
593 oid->buf = buf;
594 if(bp) FREEMEM(bp);
595
596 return 0;
597}
vlm3717fb32004-06-14 08:17:27 +0000598