Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <INTEGER.h> |
| 6 | #include <assert.h> |
| 7 | #include <errno.h> |
| 8 | |
| 9 | /* |
| 10 | * INTEGER basic type description. |
| 11 | */ |
| 12 | static ber_tlv_tag_t asn1_DEF_INTEGER_tags[] = { |
| 13 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 14 | }; |
| 15 | asn1_TYPE_descriptor_t asn1_DEF_INTEGER = { |
| 16 | "INTEGER", |
| 17 | asn_generic_no_constraint, |
| 18 | INTEGER_decode_ber, |
| 19 | INTEGER_encode_der, |
| 20 | INTEGER_print, |
| 21 | INTEGER_free, |
| 22 | 0, /* Use generic outmost tag fetcher */ |
| 23 | asn1_DEF_INTEGER_tags, |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 24 | sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]), |
| 25 | asn1_DEF_INTEGER_tags, /* Same as above */ |
| 26 | sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_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 | * Decode INTEGER type. |
| 34 | */ |
| 35 | ber_dec_rval_t |
| 36 | INTEGER_decode_ber(asn1_TYPE_descriptor_t *td, |
| 37 | void **int_structure, void *buf_ptr, size_t size, int tag_mode) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 38 | INTEGER_t *st = (INTEGER_t *)*int_structure; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | ber_dec_rval_t rval; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 40 | ber_dec_ctx_t ctx = { 0, 0, 0, 0 }; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | ber_tlv_len_t length; |
| 42 | |
| 43 | /* |
| 44 | * If the structure is not there, allocate it. |
| 45 | */ |
| 46 | if(st == NULL) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 47 | (void *)st = *int_structure = CALLOC(1, sizeof(*st)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | if(st == NULL) { |
| 49 | rval.code = RC_FAIL; |
| 50 | rval.consumed = 0; |
| 51 | return rval; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | ASN_DEBUG("Decoding %s as INTEGER (tm=%d)", |
| 56 | td->name, tag_mode); |
| 57 | |
| 58 | /* |
| 59 | * Check tags. |
| 60 | */ |
| 61 | rval = ber_check_tags(td, &ctx, |
| 62 | buf_ptr, size, tag_mode, &length, 0); |
| 63 | if(rval.code != RC_OK) |
| 64 | return rval; |
| 65 | |
| 66 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 67 | |
| 68 | /* |
| 69 | * Make sure we have this length. |
| 70 | */ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 71 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | size -= rval.consumed; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 73 | if(length > (ber_tlv_len_t)size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | rval.code = RC_WMORE; |
| 75 | rval.consumed = 0; |
| 76 | return rval; |
| 77 | } |
| 78 | |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 79 | st->buf = (uint8_t *)MALLOC(length); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | if(st->buf) { |
| 81 | st->size = length; |
| 82 | } else { |
| 83 | rval.code = RC_FAIL; |
| 84 | rval.consumed = 0; |
| 85 | return rval; |
| 86 | } |
| 87 | |
| 88 | memcpy(st->buf, buf_ptr, st->size); |
| 89 | |
| 90 | rval.code = RC_OK; |
| 91 | rval.consumed += length; |
| 92 | |
| 93 | ASN_DEBUG("Took %ld/%ld bytes to encode %s", |
| 94 | (long)rval.consumed, |
| 95 | (long)length, td->name); |
| 96 | |
| 97 | return rval; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Encode INTEGER type using DER. |
| 102 | */ |
| 103 | der_enc_rval_t |
| 104 | INTEGER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr, |
| 105 | int tag_mode, ber_tlv_tag_t tag, |
| 106 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 107 | der_enc_rval_t erval; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 108 | INTEGER_t *st = (INTEGER_t *)ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 109 | |
| 110 | ASN_DEBUG("%s %s as INTEGER (tm=%d)", |
| 111 | cb?"Encoding":"Estimating", sd->name, tag_mode); |
| 112 | |
| 113 | /* |
| 114 | * Canonicalize integer in the buffer. |
| 115 | * (Remove too long sign extension, remove some first 0x00 bytes) |
| 116 | */ |
| 117 | if(st->buf) { |
| 118 | uint8_t *buf = st->buf; |
| 119 | uint8_t *end1 = buf + st->size - 1; |
| 120 | int shift; |
| 121 | |
| 122 | /* Compute the number of superfluous leading bytes */ |
| 123 | for(; buf < end1; buf++) { |
| 124 | /* |
| 125 | * If the contents octets of an integer value encoding |
| 126 | * consist of more than one octet, then the bits of the |
| 127 | * first octet and bit 8 of the second octet: |
| 128 | * a) shall not all be ones; and |
| 129 | * b) shall not all be zero. |
| 130 | */ |
| 131 | switch(*buf) { |
| 132 | case 0x00: if((buf[1] & 0x80) == 0) |
| 133 | continue; |
| 134 | break; |
| 135 | case 0xff: if((buf[1] & 0x80)) |
| 136 | continue; |
| 137 | break; |
| 138 | } |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | /* Remove leading superfluous bytes from the integer */ |
| 143 | shift = buf - st->buf; |
| 144 | if(shift) { |
| 145 | uint8_t *nb = st->buf; |
| 146 | uint8_t *end; |
| 147 | |
| 148 | st->size -= shift; /* New size, minus bad bytes */ |
| 149 | end = nb + st->size; |
| 150 | |
| 151 | for(; nb < end; nb++, buf++) |
| 152 | *nb = *buf; |
| 153 | } |
| 154 | |
| 155 | } /* if(1) */ |
| 156 | |
| 157 | erval.encoded = der_write_tags(sd, st->size, tag_mode, tag, |
| 158 | cb, app_key); |
| 159 | ASN_DEBUG("INTEGER %s wrote tags %d", sd->name, (int)erval.encoded); |
| 160 | if(erval.encoded == -1) { |
| 161 | erval.failed_type = sd; |
| 162 | erval.structure_ptr = ptr; |
| 163 | return erval; |
| 164 | } |
| 165 | |
| 166 | if(cb && st->buf) { |
| 167 | ssize_t ret; |
| 168 | |
| 169 | ret = cb(st->buf, st->size, app_key); |
| 170 | if(ret == -1) { |
| 171 | erval.encoded = -1; |
| 172 | erval.failed_type = sd; |
| 173 | erval.structure_ptr = ptr; |
| 174 | return erval; |
| 175 | } |
| 176 | } else { |
| 177 | assert(st->buf || st->size == 0); |
| 178 | } |
| 179 | |
| 180 | erval.encoded += st->size; |
| 181 | |
| 182 | return erval; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * INTEGER specific human-readable output. |
| 187 | */ |
| 188 | int |
| 189 | INTEGER_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 190 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 191 | char scratch[32]; /* Enough for 64-bit integer */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 192 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | uint8_t *buf = st->buf; |
| 194 | uint8_t *buf_end = st->buf + st->size; |
| 195 | signed long accum; |
| 196 | char *p; |
| 197 | int ret; |
| 198 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 199 | (void)td; /* Unused argument */ |
| 200 | (void)ilevel; /* Unused argument */ |
| 201 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 202 | if(!st && !st->buf) return cb("<absent>", 8, app_key); |
| 203 | |
| 204 | if(st->size == 0) |
| 205 | return cb("0", 1, app_key); |
| 206 | |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 207 | /* |
| 208 | * Advance buf pointer until the start of the value's body. |
| 209 | * This will make us able to process large integers using simple case, |
| 210 | * when the actual value is small |
| 211 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 212 | */ |
| 213 | /* Skip the insignificant leading bytes */ |
| 214 | for(; buf < buf_end-1; buf++) { |
| 215 | switch(*buf) { |
| 216 | case 0x00: if((buf[1] & 0x80) == 0) continue; break; |
| 217 | case 0xff: if((buf[1] & 0x80) != 0) continue; break; |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 222 | /* Simple case: the integer size is small */ |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 223 | if((size_t)(buf_end - buf) <= sizeof(accum)) { |
| 224 | accum = (*buf & 0x80) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 225 | for(; buf < buf_end; buf++) |
| 226 | accum = (accum << 8) | *buf; |
| 227 | ret = snprintf(scratch, sizeof(scratch), "%ld", accum); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 228 | assert(ret > 0 && ret < (int)sizeof(scratch)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 229 | return cb(scratch, ret, app_key); |
| 230 | } |
| 231 | |
| 232 | /* Output in the long xx:yy:zz... format */ |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 233 | /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | for(p = scratch; buf < buf_end; buf++) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 235 | static const char *h2c = "0123456789ABCDEF"; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 236 | if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 237 | /* Flush buffer */ |
| 238 | if(cb(scratch, p - scratch, app_key)) |
| 239 | return -1; |
| 240 | p = scratch; |
| 241 | } |
| 242 | *p++ = h2c[*buf >> 4]; |
| 243 | *p++ = h2c[*buf & 0x0F]; |
| 244 | *p++ = ':'; |
| 245 | } |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 246 | if(p != scratch) |
| 247 | p--; /* Remove the last ':' */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 248 | |
| 249 | return cb(scratch, p - scratch, app_key); |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | INTEGER_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 254 | INTEGER_t *st = (INTEGER_t *)ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 255 | |
| 256 | if(!td || !st) |
| 257 | return; |
| 258 | |
| 259 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, %p)", |
| 260 | td->name, contents_only, st, st->buf); |
| 261 | |
| 262 | if(st->buf) { |
| 263 | FREEMEM(st->buf); |
| 264 | } |
| 265 | |
| 266 | if(!contents_only) { |
| 267 | FREEMEM(st); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | int |
| 272 | asn1_INTEGER2long(const INTEGER_t *iptr, long *lptr) { |
| 273 | uint8_t *b, *end; |
| 274 | size_t size; |
| 275 | long l; |
| 276 | |
| 277 | /* Sanity checking */ |
| 278 | if(!iptr || !iptr->buf || !lptr) { |
| 279 | errno = EINVAL; |
| 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | /* Cache the begin/end of the buffer */ |
| 284 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 285 | size = iptr->size; |
| 286 | end = b + size; /* Where to stop */ |
| 287 | |
| 288 | if(size > sizeof(long)) { |
| 289 | uint8_t *end1 = end - 1; |
| 290 | /* |
| 291 | * Slightly more advanced processing, |
| 292 | * able to >sizeof(long) bytes, |
| 293 | * when the actual value is small |
| 294 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 295 | */ |
| 296 | /* Skip out the insignificant leading bytes */ |
| 297 | for(; b < end1; b++) { |
| 298 | switch(*b) { |
| 299 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 300 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 301 | } |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | size = end - b; |
| 306 | if(size > sizeof(long)) { |
| 307 | /* Still cannot fit the long */ |
| 308 | errno = ERANGE; |
| 309 | return -1; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* Shortcut processing of a corner case */ |
| 314 | if(end == b) { |
| 315 | *lptr = 0; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /* Perform the sign initialization */ |
| 320 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 321 | if((*b >> 7)) l = -1; else l = 0; |
| 322 | |
| 323 | /* Conversion engine */ |
| 324 | for(; b < end; b++) |
| 325 | l = (l << 8) | *b; |
| 326 | |
| 327 | *lptr = l; |
| 328 | return 0; |
| 329 | } |