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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <INTEGER.h> |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 7 | #include <ber_codec_prim.h> /* Encoder and decoder of a primitive */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | #include <assert.h> |
| 9 | #include <errno.h> |
| 10 | |
| 11 | /* |
| 12 | * INTEGER basic type description. |
| 13 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 14 | static ber_tlv_tag_t asn_DEF_INTEGER_tags[] = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 16 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 17 | asn_TYPE_descriptor_t asn_DEF_INTEGER = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | "INTEGER", |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 19 | ASN__PRIMITIVE_TYPE_free, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 20 | INTEGER_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | asn_generic_no_constraint, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 22 | ber_decode_primitive, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | INTEGER_encode_der, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | 0, /* Not implemented yet */ |
| 25 | INTEGER_encode_xer, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 27 | asn_DEF_INTEGER_tags, |
| 28 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
| 29 | asn_DEF_INTEGER_tags, /* Same as above */ |
| 30 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 31 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 32 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | * Encode INTEGER type using DER. |
| 37 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 38 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 39 | INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | int tag_mode, ber_tlv_tag_t tag, |
| 41 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 42 | INTEGER_t *st = (INTEGER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | |
| 44 | ASN_DEBUG("%s %s as INTEGER (tm=%d)", |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 45 | cb?"Encoding":"Estimating", td->name, tag_mode); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Canonicalize integer in the buffer. |
| 49 | * (Remove too long sign extension, remove some first 0x00 bytes) |
| 50 | */ |
| 51 | if(st->buf) { |
| 52 | uint8_t *buf = st->buf; |
| 53 | uint8_t *end1 = buf + st->size - 1; |
| 54 | int shift; |
| 55 | |
| 56 | /* Compute the number of superfluous leading bytes */ |
| 57 | for(; buf < end1; buf++) { |
| 58 | /* |
| 59 | * If the contents octets of an integer value encoding |
| 60 | * consist of more than one octet, then the bits of the |
| 61 | * first octet and bit 8 of the second octet: |
| 62 | * a) shall not all be ones; and |
| 63 | * b) shall not all be zero. |
| 64 | */ |
| 65 | switch(*buf) { |
| 66 | case 0x00: if((buf[1] & 0x80) == 0) |
| 67 | continue; |
| 68 | break; |
| 69 | case 0xff: if((buf[1] & 0x80)) |
| 70 | continue; |
| 71 | break; |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | /* Remove leading superfluous bytes from the integer */ |
| 77 | shift = buf - st->buf; |
| 78 | if(shift) { |
| 79 | uint8_t *nb = st->buf; |
| 80 | uint8_t *end; |
| 81 | |
| 82 | st->size -= shift; /* New size, minus bad bytes */ |
| 83 | end = nb + st->size; |
| 84 | |
| 85 | for(; nb < end; nb++, buf++) |
| 86 | *nb = *buf; |
| 87 | } |
| 88 | |
| 89 | } /* if(1) */ |
| 90 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 91 | return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * INTEGER specific human-readable output. |
| 96 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 97 | static ssize_t |
| 98 | INTEGER__dump(const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 99 | char scratch[32]; /* Enough for 64-bit integer */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | uint8_t *buf = st->buf; |
| 101 | uint8_t *buf_end = st->buf + st->size; |
| 102 | signed long accum; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 103 | ssize_t wrote = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | char *p; |
| 105 | int ret; |
| 106 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 107 | if(st->size == 0) { |
| 108 | return (cb("0", 1, app_key) < 0) ? -1 : 1; |
| 109 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Advance buf pointer until the start of the value's body. |
| 113 | * This will make us able to process large integers using simple case, |
| 114 | * when the actual value is small |
| 115 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 116 | */ |
| 117 | /* Skip the insignificant leading bytes */ |
| 118 | for(; buf < buf_end-1; buf++) { |
| 119 | switch(*buf) { |
| 120 | case 0x00: if((buf[1] & 0x80) == 0) continue; break; |
| 121 | case 0xff: if((buf[1] & 0x80) != 0) continue; break; |
| 122 | } |
| 123 | break; |
| 124 | } |
| 125 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 126 | /* Simple case: the integer size is small */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 127 | if((size_t)(buf_end - buf) <= sizeof(accum)) { |
| 128 | accum = (*buf & 0x80) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | for(; buf < buf_end; buf++) |
| 130 | accum = (accum << 8) | *buf; |
| 131 | ret = snprintf(scratch, sizeof(scratch), "%ld", accum); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 132 | assert(ret > 0 && ret < (int)sizeof(scratch)); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 133 | return (cb(scratch, ret, app_key) < 0) ? -1 : ret; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* Output in the long xx:yy:zz... format */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 137 | /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 138 | for(p = scratch; buf < buf_end; buf++) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 139 | static const char *h2c = "0123456789ABCDEF"; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 140 | if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 141 | /* Flush buffer */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 142 | if(cb(scratch, p - scratch, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 143 | return -1; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 144 | wrote += p - scratch; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | p = scratch; |
| 146 | } |
| 147 | *p++ = h2c[*buf >> 4]; |
| 148 | *p++ = h2c[*buf & 0x0F]; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 149 | *p++ = 0x3a; /* ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | } |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 151 | if(p != scratch) |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 152 | p--; /* Remove the last ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 153 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 154 | wrote += p - scratch; |
| 155 | return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * INTEGER specific human-readable output. |
| 160 | */ |
| 161 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 162 | INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 163 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 164 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 165 | ssize_t ret; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 166 | |
| 167 | (void)td; |
| 168 | (void)ilevel; |
| 169 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 170 | if(!st && !st->buf) |
| 171 | ret = cb("<absent>", 8, app_key); |
| 172 | else |
| 173 | ret = INTEGER__dump(st, cb, app_key); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 174 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 175 | return (ret < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 179 | INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 180 | int ilevel, enum xer_encoder_flags_e flags, |
| 181 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 182 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
| 183 | asn_enc_rval_t er; |
| 184 | |
| 185 | (void)ilevel; |
| 186 | (void)flags; |
| 187 | |
| 188 | if(!st && !st->buf) |
| 189 | _ASN_ENCODE_FAILED; |
| 190 | |
| 191 | er.encoded = INTEGER__dump(st, cb, app_key); |
| 192 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 193 | |
| 194 | return er; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 195 | } |
| 196 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 198 | asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 199 | uint8_t *b, *end; |
| 200 | size_t size; |
| 201 | long l; |
| 202 | |
| 203 | /* Sanity checking */ |
| 204 | if(!iptr || !iptr->buf || !lptr) { |
| 205 | errno = EINVAL; |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | /* Cache the begin/end of the buffer */ |
| 210 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 211 | size = iptr->size; |
| 212 | end = b + size; /* Where to stop */ |
| 213 | |
| 214 | if(size > sizeof(long)) { |
| 215 | uint8_t *end1 = end - 1; |
| 216 | /* |
| 217 | * Slightly more advanced processing, |
| 218 | * able to >sizeof(long) bytes, |
| 219 | * when the actual value is small |
| 220 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 221 | */ |
| 222 | /* Skip out the insignificant leading bytes */ |
| 223 | for(; b < end1; b++) { |
| 224 | switch(*b) { |
| 225 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 226 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 227 | } |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | size = end - b; |
| 232 | if(size > sizeof(long)) { |
| 233 | /* Still cannot fit the long */ |
| 234 | errno = ERANGE; |
| 235 | return -1; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* Shortcut processing of a corner case */ |
| 240 | if(end == b) { |
| 241 | *lptr = 0; |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* Perform the sign initialization */ |
| 246 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 247 | if((*b >> 7)) l = -1; else l = 0; |
| 248 | |
| 249 | /* Conversion engine */ |
| 250 | for(; b < end; b++) |
| 251 | l = (l << 8) | *b; |
| 252 | |
| 253 | *lptr = l; |
| 254 | return 0; |
| 255 | } |