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]), |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 27 | asn1_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */ |
| 28 | sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags) |
| 29 | / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]), |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 30 | 0, /* Always in primitive form */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 31 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 32 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * Encode OBJECT IDENTIFIER type using DER. |
| 38 | */ |
| 39 | der_enc_rval_t |
| 40 | OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr, |
| 41 | int tag_mode, ber_tlv_tag_t tag, |
| 42 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 43 | der_enc_rval_t erval; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 44 | OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | |
| 46 | ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)", |
| 47 | cb?"Encoding":"Estimating", sd->name, tag_mode); |
| 48 | |
| 49 | erval.encoded = der_write_tags(sd, st->size, tag_mode, tag, |
| 50 | cb, app_key); |
| 51 | ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d", |
| 52 | sd->name, (int)erval.encoded); |
| 53 | if(erval.encoded == -1) { |
| 54 | erval.failed_type = sd; |
| 55 | erval.structure_ptr = ptr; |
| 56 | return erval; |
| 57 | } |
| 58 | |
| 59 | if(cb && st->buf) { |
| 60 | ssize_t ret; |
| 61 | |
| 62 | ret = cb(st->buf, st->size, app_key); |
| 63 | if(ret == -1) { |
| 64 | erval.encoded = -1; |
| 65 | erval.failed_type = sd; |
| 66 | erval.structure_ptr = ptr; |
| 67 | return erval; |
| 68 | } |
| 69 | } else { |
| 70 | assert(st->buf || st->size == 0); |
| 71 | } |
| 72 | |
| 73 | erval.encoded += st->size; |
| 74 | |
| 75 | return erval; |
| 76 | } |
| 77 | |
| 78 | int |
| 79 | OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 80 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 81 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | |
| 83 | if(st && st->buf) { |
| 84 | if(st->size < 1) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 85 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 86 | "%s: at least one numerical value " |
| 87 | "expected (%s:%d)", |
| 88 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 92 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 93 | "%s: value not given (%s:%d)", |
| 94 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 101 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 103 | OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) { |
vlm | 72425de | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 104 | unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 105 | uint8_t *arcend = arcbuf + arclen; /* End of arc */ |
| 106 | void *rvstart = rvbuf; /* Original start of the value buffer */ |
| 107 | unsigned int cache = 0; /* No more than 14 significant bits */ |
| 108 | int inc; /* Return value growth direction */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 109 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 110 | rvsize *= CHAR_BIT; /* bytes to bits */ |
| 111 | arclen *= 7; /* bytes to bits */ |
| 112 | |
| 113 | /* |
| 114 | * The arc has the number of bits |
| 115 | * cannot be represented using supplied return value type. |
| 116 | */ |
| 117 | if(arclen > rvsize) { |
| 118 | if(arclen > (rvsize + CHAR_BIT)) { |
| 119 | errno = ERANGE; /* Overflow */ |
| 120 | return -1; |
| 121 | } else { |
| 122 | /* |
| 123 | * Even if the number of bits in the arc representation |
| 124 | * is higher than the width of supplied * return value |
| 125 | * type, there is still possible to fit it when there |
| 126 | * are few unused high bits in the arc value |
| 127 | * representaion. |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 128 | * |
| 129 | * Moreover, there is a possibility that the |
| 130 | * number could actually fit the arc space, given |
| 131 | * that add is negative, but we don't handle |
| 132 | * such "temporary lack of precision" situation here. |
| 133 | * May be considered as a bug. |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 134 | */ |
| 135 | uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f; |
| 136 | if((*arcbuf & mask)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | errno = ERANGE; /* Overflow */ |
| 138 | return -1; |
| 139 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 140 | /* Fool the routine computing unused bits */ |
| 141 | arclen -= 7; |
| 142 | cache = *arcbuf & 0x7f; |
| 143 | arcbuf++; |
| 144 | } |
| 145 | } |
| 146 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 147 | /* Faster path for common size */ |
| 148 | if(rvsize == (CHAR_BIT * sizeof(unsigned long))) { |
| 149 | unsigned long accum; |
| 150 | /* Gather all bits into the accumulator */ |
| 151 | for(accum = cache; arcbuf < arcend; arcbuf++) |
| 152 | accum = (accum << 7) | (*arcbuf & ~0x80); |
| 153 | if(accum < (unsigned)-add) { |
| 154 | errno = ERANGE; /* Overflow */ |
| 155 | return -1; |
| 156 | } |
| 157 | *(unsigned long *)rvbuf = accum + add; |
| 158 | return 0; |
| 159 | } |
| 160 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 161 | #ifndef WORDS_BIGENDIAN |
| 162 | if(*(unsigned char *)&LE) { /* Little endian (x86) */ |
| 163 | /* "Convert" to big endian */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 164 | (unsigned char *)rvbuf += rvsize / CHAR_BIT - 1; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 165 | ((unsigned char *)rvstart)--; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 166 | inc = -1; /* Descending */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 167 | } else |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 168 | #endif /* !WORDS_BIGENDIAN */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 169 | inc = +1; /* Big endian is known [at compile time] */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 170 | |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 171 | { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 172 | int bits; /* typically no more than 3-4 bits */ |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 173 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 174 | /* Clear the high unused bits */ |
| 175 | for(bits = rvsize - arclen; |
| 176 | bits > CHAR_BIT; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 177 | (unsigned char *)rvbuf += inc, bits -= CHAR_BIT) |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 178 | *(unsigned char *)rvbuf = 0; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 179 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 180 | /* Fill the body of a value */ |
| 181 | for(; arcbuf < arcend; arcbuf++) { |
| 182 | cache = (cache << 7) | (*arcbuf & 0x7f); |
| 183 | bits += 7; |
| 184 | if(bits >= CHAR_BIT) { |
| 185 | bits -= CHAR_BIT; |
| 186 | *(unsigned char *)rvbuf = (cache >> bits); |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 187 | (unsigned char *)rvbuf += inc; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | if(bits) { |
| 191 | *(unsigned char *)rvbuf = cache; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 192 | (unsigned char *)rvbuf += inc; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | if(add) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 197 | for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) { |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 198 | int v = add + *(unsigned char *)rvbuf; |
| 199 | if(v & (-1 << CHAR_BIT)) { |
| 200 | *(unsigned char *)rvbuf |
vlm | 754284a | 2004-08-11 09:17:15 +0000 | [diff] [blame] | 201 | = (unsigned char)(v + (1 << CHAR_BIT)); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 202 | add = -1; |
| 203 | } else { |
| 204 | *(unsigned char *)rvbuf = v; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | if(rvbuf == rvstart) { |
| 209 | /* No space to carry over */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | errno = ERANGE; /* Overflow */ |
| 211 | return -1; |
| 212 | } |
| 213 | } |
| 214 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 218 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 219 | int |
| 220 | OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add, |
| 221 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 222 | char scratch[64]; /* Conservative estimate */ |
| 223 | unsigned long accum; /* Bits accumulator */ |
| 224 | char *p; /* Position in the scratch buffer */ |
| 225 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 226 | if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add, |
| 227 | &accum, sizeof(accum))) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 228 | return -1; |
| 229 | |
vlm | 3fff06b | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 230 | if(accum) { |
| 231 | /* Fill the scratch buffer in reverse. */ |
| 232 | p = scratch + sizeof(scratch); |
| 233 | for(; accum; accum /= 10) |
| 234 | *(--p) = (char)(accum % 10) + 0x30; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 235 | |
vlm | 3fff06b | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 236 | return cb(p, sizeof(scratch) - (p - scratch), app_key); |
| 237 | } else { |
| 238 | *scratch = 0x30; |
| 239 | return cb(scratch, 1, app_key); |
| 240 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | int |
| 244 | OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 245 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 246 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | int startn; |
| 248 | int add = 0; |
| 249 | int i; |
| 250 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 251 | (void)td; /* Unused argument */ |
| 252 | (void)ilevel; /* Unused argument */ |
| 253 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 254 | if(!st || !st->buf) |
| 255 | return cb("<absent>", 8, app_key); |
| 256 | |
| 257 | /* Dump preamble */ |
| 258 | if(cb("{ ", 2, app_key)) |
| 259 | return -1; |
| 260 | |
| 261 | for(i = 0, startn = 0; i < st->size; i++) { |
| 262 | uint8_t b = st->buf[i]; |
| 263 | if((b & 0x80)) /* Continuation expected */ |
| 264 | continue; |
| 265 | |
| 266 | if(startn == 0) { |
| 267 | /* |
| 268 | * First two arcs are encoded through the backdoor. |
| 269 | */ |
| 270 | if(i) { |
| 271 | add = -80; |
| 272 | if(cb("2", 1, app_key)) return -1; |
| 273 | } else if(b <= 39) { |
| 274 | add = 0; |
| 275 | if(cb("0", 1, app_key)) return -1; |
| 276 | } else if(b < 79) { |
| 277 | add = -40; |
| 278 | if(cb("1", 1, app_key)) return -1; |
| 279 | } else { |
| 280 | add = -80; |
| 281 | if(cb("2", 1, app_key)) return -1; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if(cb(" ", 1, app_key)) /* Separate arcs */ |
| 286 | return -1; |
| 287 | |
| 288 | if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn], |
| 289 | i - startn + 1, add, |
| 290 | cb, app_key)) |
| 291 | return -1; |
| 292 | startn = i + 1; |
| 293 | add = 0; |
| 294 | } |
| 295 | |
| 296 | return cb(" }", 2, app_key); |
| 297 | } |
| 298 | |
| 299 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 300 | OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, |
| 301 | unsigned int arc_type_size, unsigned int arc_slots) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 302 | void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 303 | int num_arcs = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 304 | int startn = 0; |
| 305 | int add = 0; |
| 306 | int i; |
| 307 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 308 | if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | errno = EINVAL; |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | for(i = 0; i < oid->size; i++) { |
| 314 | uint8_t b = oid->buf[i]; |
| 315 | if((b & 0x80)) /* Continuation expected */ |
| 316 | continue; |
| 317 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 318 | if(num_arcs == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 319 | /* |
| 320 | * First two arcs are encoded through the backdoor. |
| 321 | */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 322 | unsigned LE = 1; /* Little endian */ |
| 323 | int first_arc; |
| 324 | num_arcs++; |
| 325 | if(!arc_slots) { num_arcs++; continue; } |
| 326 | |
| 327 | if(i) first_arc = 2; |
| 328 | else if(b <= 39) first_arc = 0; |
| 329 | else if(b < 79) first_arc = 1; |
| 330 | else first_arc = 2; |
| 331 | |
| 332 | add = -40 * first_arc; |
| 333 | memset(arcs, 0, arc_type_size); |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 334 | *(unsigned char *)((char *)arcs |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 335 | + ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 336 | = first_arc; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 337 | arcs = ((char *)arcs) + arc_type_size; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 338 | } |
| 339 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 340 | /* Decode, if has space */ |
| 341 | if(arcs < arcs_end) { |
| 342 | if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn], |
| 343 | i - startn + 1, add, |
| 344 | arcs, arc_type_size)) |
| 345 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 346 | startn = i + 1; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 347 | arcs = ((char *)arcs) + arc_type_size; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 348 | add = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 349 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 350 | num_arcs++; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 351 | } |
| 352 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 353 | return num_arcs; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 354 | } |
| 355 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Save the single value as an object identifier arc. |
| 359 | */ |
vlm | e55716a | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 360 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 361 | OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) { |
| 362 | /* |
| 363 | * The following conditions must hold: |
| 364 | * assert(arcval); |
| 365 | * assert(arcval_size > 0); |
| 366 | * assert(arcbuf); |
| 367 | */ |
| 368 | #ifdef WORDS_BIGENDIAN |
| 369 | const unsigned isLittleEndian = 0; |
| 370 | #else |
| 371 | unsigned LE = 1; |
| 372 | unsigned isLittleEndian = *(char *)&LE; |
| 373 | #endif |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 374 | uint8_t *tp, *tend; |
| 375 | unsigned int cache; |
| 376 | uint8_t *bp = arcbuf; |
| 377 | int bits; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 378 | #ifdef __GNUC__ |
| 379 | uint8_t buffer[arcval_size]; |
| 380 | #else |
| 381 | uint8_t *buffer = alloca(arcval_size); |
vlm | 7a6a60e | 2004-08-11 07:41:45 +0000 | [diff] [blame] | 382 | if(!buffer) { errno = ENOMEM; return -1; } |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 383 | #endif |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 384 | |
| 385 | if(isLittleEndian && !prepared_order) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 386 | uint8_t *a = (unsigned char *)arcval + arcval_size - 1; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 387 | uint8_t *aend = (uint8_t *)arcval; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 388 | uint8_t *msb = buffer + arcval_size - 1; |
| 389 | for(tp = buffer; a >= aend; tp++, a--) |
| 390 | if((*tp = *a) && (tp < msb)) |
| 391 | msb = tp; |
| 392 | tend = &buffer[arcval_size]; |
| 393 | tp = msb; /* Most significant non-zero byte */ |
| 394 | } else { |
| 395 | /* Look for most significant non-zero byte */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 396 | tend = (unsigned char *)arcval + arcval_size; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 397 | for(tp = (uint8_t *)arcval; tp < tend - 1; tp++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 398 | if(*tp) break; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Split the value in 7-bits chunks. |
| 403 | */ |
| 404 | bits = ((tend - tp) * CHAR_BIT) % 7; |
| 405 | if(bits) { |
| 406 | cache = *tp >> (CHAR_BIT - bits); |
| 407 | if(cache) { |
| 408 | *bp++ = cache | 0x80; |
| 409 | cache = *tp++; |
| 410 | bits = CHAR_BIT - bits; |
| 411 | } else { |
| 412 | bits = -bits; |
| 413 | } |
| 414 | } else { |
| 415 | cache = 0; |
| 416 | } |
| 417 | for(; tp < tend; tp++) { |
| 418 | cache = (cache << CHAR_BIT) + *tp; |
| 419 | bits += CHAR_BIT; |
| 420 | while(bits >= 7) { |
| 421 | bits -= 7; |
| 422 | *bp++ = 0x80 | (cache >> bits); |
| 423 | } |
| 424 | } |
| 425 | if(bits) *bp++ = cache; |
| 426 | bp[-1] &= 0x7f; /* Clear the last bit */ |
| 427 | |
| 428 | return bp - arcbuf; |
| 429 | } |
| 430 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 431 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 432 | 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] | 433 | uint8_t *buf; |
| 434 | uint8_t *bp; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 435 | unsigned LE = 1; /* Little endian (x86) */ |
| 436 | unsigned isLittleEndian = *((char *)&LE); |
| 437 | unsigned int arc0; |
| 438 | unsigned int arc1; |
| 439 | unsigned size; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 440 | unsigned i; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 441 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 442 | if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 443 | errno = EINVAL; |
| 444 | return -1; |
| 445 | } |
| 446 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 447 | switch(arc_type_size) { |
| 448 | case sizeof(char): |
| 449 | arc0 = ((unsigned char *)arcs)[0]; |
| 450 | arc1 = ((unsigned char *)arcs)[1]; |
| 451 | break; |
| 452 | case sizeof(short): |
| 453 | arc0 = ((unsigned short *)arcs)[0]; |
| 454 | arc1 = ((unsigned short *)arcs)[1]; |
| 455 | break; |
| 456 | case sizeof(int): |
| 457 | arc0 = ((unsigned int *)arcs)[0]; |
| 458 | arc1 = ((unsigned int *)arcs)[1]; |
| 459 | break; |
| 460 | default: |
| 461 | arc1 = arc0 = 0; |
| 462 | if(isLittleEndian) { /* Little endian (x86) */ |
| 463 | unsigned char *ps, *pe; |
| 464 | /* If more significant bytes are present, |
| 465 | * make them > 255 quick */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 466 | for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size; |
| 467 | ps < pe; ps++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 468 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 469 | arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT; |
| 470 | arc0 = *((unsigned char *)arcs + 0); |
| 471 | arc1 = *((unsigned char *)arcs + arc_type_size); |
| 472 | } else { |
| 473 | unsigned char *ps, *pe; |
| 474 | /* If more significant bytes are present, |
| 475 | * make them > 255 quick */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 476 | 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] | 477 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 478 | arc0 = *((unsigned char *)arcs + arc_type_size - 1); |
| 479 | arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * The previous chapter left us with the first and the second arcs. |
| 485 | * The values are not precise (that is, they are valid only if |
| 486 | * they're less than 255), but OK for the purposes of making |
| 487 | * the sanity test below. |
| 488 | */ |
| 489 | if(arc0 <= 1) { |
| 490 | if(arc1 >= 39) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 491 | /* 8.19.4: At most 39 subsequent values (including 0) */ |
| 492 | errno = ERANGE; |
| 493 | return -1; |
| 494 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 495 | } else if(arc0 > 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 496 | /* 8.19.4: Only three values are allocated from the root node */ |
| 497 | errno = ERANGE; |
| 498 | return -1; |
| 499 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 500 | /* |
| 501 | * After above tests it is known that the value of arc0 is completely |
| 502 | * trustworthy (0..2). However, the arc1's value is still meaningless. |
| 503 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 504 | |
| 505 | /* |
| 506 | * Roughly estimate the maximum size necessary to encode these arcs. |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 507 | * This estimation implicitly takes in account the following facts, |
| 508 | * that cancel each other: |
| 509 | * * the first two arcs are encoded in a single value. |
| 510 | * * the first value may require more space (+1 byte) |
| 511 | * * the value of the first arc which is in range (0..2) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 512 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 513 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 514 | bp = buf = (uint8_t *)MALLOC(size + 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 515 | if(!buf) { |
| 516 | /* ENOMEM */ |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | /* |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 521 | * Encode the first two arcs. |
| 522 | * These require special treatment. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 523 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 524 | { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 525 | uint8_t *tp; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 526 | #ifdef __GNUC__ |
| 527 | uint8_t first_value[1 + arc_type_size]; /* of two arcs */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 528 | uint8_t *fv = first_value; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 529 | #else |
| 530 | uint8_t *first_value = alloca(1 + arc_type_size); |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 531 | uint8_t *fv = first_value; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 532 | if(!first_value) { |
| 533 | errno = ENOMEM; |
| 534 | return -1; |
| 535 | } |
| 536 | #endif |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 537 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 538 | /* |
| 539 | * Simulate first_value = arc0 * 40 + arc1; |
| 540 | */ |
| 541 | /* Copy the second (1'st) arcs[1] into the first_value */ |
| 542 | *fv++ = 0; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 543 | arcs = ((char *)arcs) + arc_type_size; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 544 | if(isLittleEndian) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 545 | uint8_t *aend = (unsigned char *)arcs - 1; |
| 546 | uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 547 | for(; a1 > aend; fv++, a1--) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 548 | } else { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 549 | uint8_t *a1 = (uint8_t *)arcs; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 550 | uint8_t *aend = a1 + arc_type_size; |
| 551 | for(; a1 < aend; fv++, a1++) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 552 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 553 | /* Increase the first_value by arc0 */ |
| 554 | arc0 *= 40; /* (0..80) */ |
| 555 | for(tp = first_value + arc_type_size; tp >= first_value; tp--) { |
| 556 | unsigned int v = *tp; |
| 557 | v += arc0; |
| 558 | *tp = v; |
| 559 | if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT; |
| 560 | else break; |
| 561 | } |
| 562 | |
| 563 | assert(tp >= first_value); |
| 564 | |
| 565 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value, |
| 566 | fv - first_value, 1); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Save the rest of arcs. |
| 571 | */ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 572 | for(arcs = ((char *)arcs) + arc_type_size, i = 2; |
| 573 | i < arc_slots; |
| 574 | i++, arcs = ((char *)arcs) + arc_type_size) { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 575 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 576 | arcs, arc_type_size, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 577 | } |
| 578 | |
vlm | a63e029 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 579 | assert((unsigned)(bp - buf) <= size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * Replace buffer. |
| 583 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 584 | oid->size = bp - buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 585 | bp = oid->buf; |
| 586 | oid->buf = buf; |
| 587 | if(bp) FREEMEM(bp); |
| 588 | |
| 589 | return 0; |
| 590 | } |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 591 | |