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