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