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