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