vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 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> |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 6 | #include <limits.h> /* for CHAR_BIT */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | |
| 10 | /* |
| 11 | * OBJECT IDENTIFIER basic type description. |
| 12 | */ |
| 13 | static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = { |
| 14 | (ASN_TAG_CLASS_UNIVERSAL | (6 << 2)) |
| 15 | }; |
| 16 | asn1_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 */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 28 | 0, /* Always in primitive form */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 29 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 30 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | * Encode OBJECT IDENTIFIER type using DER. |
| 36 | */ |
| 37 | der_enc_rval_t |
| 38 | OBJECT_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; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 42 | OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | |
| 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 | |
| 76 | int |
| 77 | OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 78 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 79 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | |
| 81 | if(st && st->buf) { |
| 82 | if(st->size < 1) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 83 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 84 | "%s: at least one numerical value " |
| 85 | "expected (%s:%d)", |
| 86 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 90 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 91 | "%s: value not given (%s:%d)", |
| 92 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 99 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 101 | OBJECT_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 */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 108 | 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. |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 126 | * |
| 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. |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 132 | */ |
| 133 | uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f; |
| 134 | if((*arcbuf & mask)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | errno = ERANGE; /* Overflow */ |
| 136 | return -1; |
| 137 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 138 | /* Fool the routine computing unused bits */ |
| 139 | arclen -= 7; |
| 140 | cache = *arcbuf & 0x7f; |
| 141 | arcbuf++; |
| 142 | } |
| 143 | } |
| 144 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 145 | /* 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 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 159 | #ifndef WORDS_BIGENDIAN |
| 160 | if(*(unsigned char *)&LE) { /* Little endian (x86) */ |
| 161 | /* "Convert" to big endian */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 162 | (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 163 | ((unsigned char *)rvstart)--; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 164 | inc = -1; /* Descending */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 165 | } else |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 166 | #endif /* !WORDS_BIGENDIAN */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 167 | inc = +1; /* Big endian is known [at compile time] */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 168 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 169 | { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 170 | int bits; /* typically no more than 3-4 bits */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 171 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 172 | /* Clear the high unused bits */ |
| 173 | for(bits = rvsize - arclen; |
| 174 | bits > CHAR_BIT; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 175 | (unsigned char *)rvbuf += inc, bits -= CHAR_BIT) |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 176 | *(unsigned char *)rvbuf = 0; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 177 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 178 | /* 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); |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 185 | (unsigned char *)rvbuf += inc; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | if(bits) { |
| 189 | *(unsigned char *)rvbuf = cache; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 190 | (unsigned char *)rvbuf += inc; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | if(add) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 195 | for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) { |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 196 | int v = add + *(unsigned char *)rvbuf; |
| 197 | if(v & (-1 << CHAR_BIT)) { |
| 198 | *(unsigned char *)rvbuf |
vlm | 754284a | 2004-08-11 09:17:15 +0000 | [diff] [blame] | 199 | = (unsigned char)(v + (1 << CHAR_BIT)); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 200 | add = -1; |
| 201 | } else { |
| 202 | *(unsigned char *)rvbuf = v; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | if(rvbuf == rvstart) { |
| 207 | /* No space to carry over */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 208 | errno = ERANGE; /* Overflow */ |
| 209 | return -1; |
| 210 | } |
| 211 | } |
| 212 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 216 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 217 | int |
| 218 | OBJECT_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 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 224 | if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add, |
| 225 | &accum, sizeof(accum))) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 226 | return -1; |
| 227 | |
vlm | 3fff06b | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 228 | 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; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 233 | |
vlm | 3fff06b | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 234 | return cb(p, sizeof(scratch) - (p - scratch), app_key); |
| 235 | } else { |
| 236 | *scratch = 0x30; |
| 237 | return cb(scratch, 1, app_key); |
| 238 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | int |
| 242 | OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 243 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 244 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 245 | int startn; |
| 246 | int add = 0; |
| 247 | int i; |
| 248 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 249 | (void)td; /* Unused argument */ |
| 250 | (void)ilevel; /* Unused argument */ |
| 251 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 252 | 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 | |
| 297 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 298 | OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, |
| 299 | unsigned int arc_type_size, unsigned int arc_slots) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 300 | void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 301 | int num_arcs = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 302 | int startn = 0; |
| 303 | int add = 0; |
| 304 | int i; |
| 305 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 306 | if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 307 | 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 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 316 | if(num_arcs == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 317 | /* |
| 318 | * First two arcs are encoded through the backdoor. |
| 319 | */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 320 | 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); |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 332 | *(unsigned char *)((char *)arcs |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 333 | + ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 334 | = first_arc; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame^] | 335 | arcs = ((char *)arcs) + arc_type_size; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 336 | } |
| 337 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 338 | /* 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; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 344 | startn = i + 1; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame^] | 345 | arcs = ((char *)arcs) + arc_type_size; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 346 | add = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 347 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 348 | num_arcs++; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 349 | } |
| 350 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 351 | return num_arcs; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | } |
| 353 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * Save the single value as an object identifier arc. |
| 357 | */ |
vlm | e55716a | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 358 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 359 | OBJECT_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 |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 372 | uint8_t *tp, *tend; |
| 373 | unsigned int cache; |
| 374 | uint8_t *bp = arcbuf; |
| 375 | int bits; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 376 | #ifdef __GNUC__ |
| 377 | uint8_t buffer[arcval_size]; |
| 378 | #else |
| 379 | uint8_t *buffer = alloca(arcval_size); |
vlm | 7a6a60e | 2004-08-11 07:41:45 +0000 | [diff] [blame] | 380 | if(!buffer) { errno = ENOMEM; return -1; } |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 381 | #endif |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 382 | |
| 383 | if(isLittleEndian && !prepared_order) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 384 | uint8_t *a = (unsigned char *)arcval + arcval_size - 1; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 385 | uint8_t *aend = (uint8_t *)arcval; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 386 | 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 */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 394 | tend = (unsigned char *)arcval + arcval_size; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 395 | for(tp = (uint8_t *)arcval; tp < tend - 1; tp++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 396 | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 429 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 430 | OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 431 | uint8_t *buf; |
| 432 | uint8_t *bp; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 433 | unsigned LE = 1; /* Little endian (x86) */ |
| 434 | unsigned isLittleEndian = *((char *)&LE); |
| 435 | unsigned int arc0; |
| 436 | unsigned int arc1; |
| 437 | unsigned size; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 438 | unsigned i; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 439 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 440 | if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 441 | errno = EINVAL; |
| 442 | return -1; |
| 443 | } |
| 444 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 445 | 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 */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 464 | for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size; |
| 465 | ps < pe; ps++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 466 | 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 */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 474 | for(ps = (unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 475 | 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) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 489 | /* 8.19.4: At most 39 subsequent values (including 0) */ |
| 490 | errno = ERANGE; |
| 491 | return -1; |
| 492 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 493 | } else if(arc0 > 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 494 | /* 8.19.4: Only three values are allocated from the root node */ |
| 495 | errno = ERANGE; |
| 496 | return -1; |
| 497 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 498 | /* |
| 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 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 502 | |
| 503 | /* |
| 504 | * Roughly estimate the maximum size necessary to encode these arcs. |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 505 | * 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) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 510 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 511 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 512 | bp = buf = (uint8_t *)MALLOC(size + 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 513 | if(!buf) { |
| 514 | /* ENOMEM */ |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | /* |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 519 | * Encode the first two arcs. |
| 520 | * These require special treatment. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 521 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 522 | { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 523 | uint8_t *tp; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 524 | #ifdef __GNUC__ |
| 525 | uint8_t first_value[1 + arc_type_size]; /* of two arcs */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 526 | uint8_t *fv = first_value; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 527 | #else |
| 528 | uint8_t *first_value = alloca(1 + arc_type_size); |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 529 | uint8_t *fv = first_value; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 530 | if(!first_value) { |
| 531 | errno = ENOMEM; |
| 532 | return -1; |
| 533 | } |
| 534 | #endif |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 535 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 536 | /* |
| 537 | * Simulate first_value = arc0 * 40 + arc1; |
| 538 | */ |
| 539 | /* Copy the second (1'st) arcs[1] into the first_value */ |
| 540 | *fv++ = 0; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame^] | 541 | arcs = ((char *)arcs) + arc_type_size; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 542 | if(isLittleEndian) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 543 | uint8_t *aend = (unsigned char *)arcs - 1; |
| 544 | uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 545 | for(; a1 > aend; fv++, a1--) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 546 | } else { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 547 | uint8_t *a1 = (uint8_t *)arcs; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 548 | uint8_t *aend = a1 + arc_type_size; |
| 549 | for(; a1 < aend; fv++, a1++) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 550 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 551 | /* 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 | */ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame^] | 570 | for(arcs = ((char *)arcs) + arc_type_size, i = 2; |
| 571 | i < arc_slots; |
| 572 | i++, arcs = ((char *)arcs) + arc_type_size) { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 573 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 574 | arcs, arc_type_size, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 575 | } |
| 576 | |
vlm | a63e029 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 577 | assert((unsigned)(bp - buf) <= size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * Replace buffer. |
| 581 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 582 | oid->size = bp - buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 583 | bp = oid->buf; |
| 584 | oid->buf = buf; |
| 585 | if(bp) FREEMEM(bp); |
| 586 | |
| 587 | return 0; |
| 588 | } |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 589 | |