vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 7 | #include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */ |
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 | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 19 | "INTEGER", |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 20 | ASN__PRIMITIVE_TYPE_free, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 21 | INTEGER_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | asn_generic_no_constraint, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 23 | ber_decode_primitive, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | INTEGER_encode_der, |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 25 | INTEGER_decode_xer, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 26 | INTEGER_encode_xer, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 28 | asn_DEF_INTEGER_tags, |
| 29 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
| 30 | asn_DEF_INTEGER_tags, /* Same as above */ |
| 31 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 32 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 33 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | * Encode INTEGER type using DER. |
| 38 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 39 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 40 | INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | int tag_mode, ber_tlv_tag_t tag, |
| 42 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 43 | INTEGER_t *st = (INTEGER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 44 | |
| 45 | ASN_DEBUG("%s %s as INTEGER (tm=%d)", |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 46 | cb?"Encoding":"Estimating", td->name, tag_mode); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Canonicalize integer in the buffer. |
| 50 | * (Remove too long sign extension, remove some first 0x00 bytes) |
| 51 | */ |
| 52 | if(st->buf) { |
| 53 | uint8_t *buf = st->buf; |
| 54 | uint8_t *end1 = buf + st->size - 1; |
| 55 | int shift; |
| 56 | |
| 57 | /* Compute the number of superfluous leading bytes */ |
| 58 | for(; buf < end1; buf++) { |
| 59 | /* |
| 60 | * If the contents octets of an integer value encoding |
| 61 | * consist of more than one octet, then the bits of the |
| 62 | * first octet and bit 8 of the second octet: |
| 63 | * a) shall not all be ones; and |
| 64 | * b) shall not all be zero. |
| 65 | */ |
| 66 | switch(*buf) { |
| 67 | case 0x00: if((buf[1] & 0x80) == 0) |
| 68 | continue; |
| 69 | break; |
| 70 | case 0xff: if((buf[1] & 0x80)) |
| 71 | continue; |
| 72 | break; |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | /* Remove leading superfluous bytes from the integer */ |
| 78 | shift = buf - st->buf; |
| 79 | if(shift) { |
| 80 | uint8_t *nb = st->buf; |
| 81 | uint8_t *end; |
| 82 | |
| 83 | st->size -= shift; /* New size, minus bad bytes */ |
| 84 | end = nb + st->size; |
| 85 | |
| 86 | for(; nb < end; nb++, buf++) |
| 87 | *nb = *buf; |
| 88 | } |
| 89 | |
| 90 | } /* if(1) */ |
| 91 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 92 | return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * INTEGER specific human-readable output. |
| 97 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 98 | static ssize_t |
| 99 | 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] | 100 | char scratch[32]; /* Enough for 64-bit integer */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | uint8_t *buf = st->buf; |
| 102 | uint8_t *buf_end = st->buf + st->size; |
| 103 | signed long accum; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 104 | ssize_t wrote = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 105 | char *p; |
| 106 | int ret; |
| 107 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 108 | if(st->size == 0) { |
| 109 | return (cb("0", 1, app_key) < 0) ? -1 : 1; |
| 110 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 112 | /* |
| 113 | * Advance buf pointer until the start of the value's body. |
| 114 | * This will make us able to process large integers using simple case, |
| 115 | * when the actual value is small |
| 116 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 117 | */ |
| 118 | /* Skip the insignificant leading bytes */ |
| 119 | for(; buf < buf_end-1; buf++) { |
| 120 | switch(*buf) { |
| 121 | case 0x00: if((buf[1] & 0x80) == 0) continue; break; |
| 122 | case 0xff: if((buf[1] & 0x80) != 0) continue; break; |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | /* Simple case: the integer size is small */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 128 | if((size_t)(buf_end - buf) <= sizeof(accum)) { |
| 129 | accum = (*buf & 0x80) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 130 | for(; buf < buf_end; buf++) |
| 131 | accum = (accum << 8) | *buf; |
| 132 | ret = snprintf(scratch, sizeof(scratch), "%ld", accum); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 133 | assert(ret > 0 && ret < (int)sizeof(scratch)); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 134 | return (cb(scratch, ret, app_key) < 0) ? -1 : ret; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Output in the long xx:yy:zz... format */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 138 | /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | for(p = scratch; buf < buf_end; buf++) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 140 | static const char *h2c = "0123456789ABCDEF"; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 141 | if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 142 | /* Flush buffer */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 143 | if(cb(scratch, p - scratch, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | return -1; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 145 | wrote += p - scratch; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 146 | p = scratch; |
| 147 | } |
| 148 | *p++ = h2c[*buf >> 4]; |
| 149 | *p++ = h2c[*buf & 0x0F]; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 150 | *p++ = 0x3a; /* ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | } |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 152 | if(p != scratch) |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 153 | p--; /* Remove the last ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 154 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 155 | wrote += p - scratch; |
| 156 | return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * INTEGER specific human-readable output. |
| 161 | */ |
| 162 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 163 | INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 164 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 165 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 166 | ssize_t ret; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 167 | |
| 168 | (void)td; |
| 169 | (void)ilevel; |
| 170 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 171 | if(!st && !st->buf) |
| 172 | ret = cb("<absent>", 8, app_key); |
| 173 | else |
| 174 | ret = INTEGER__dump(st, cb, app_key); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 175 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 176 | return (ret < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 177 | } |
| 178 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 179 | /* |
| 180 | * Decode the chunk of XML text encoding INTEGER. |
| 181 | */ |
| 182 | static ssize_t |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 183 | INTEGER__xer_body_decode(void *sptr, void *chunk_buf, size_t chunk_size) { |
| 184 | INTEGER_t *st = (INTEGER_t *)sptr; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 185 | long sign = 1; |
| 186 | long value; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 187 | char *lp; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 188 | char *lstart = (char *)chunk_buf; |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 189 | char *lstop = lstart + chunk_size; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 190 | enum { |
| 191 | ST_SKIPSPACE, |
| 192 | ST_WAITDIGITS, |
| 193 | ST_DIGITS, |
| 194 | } state = ST_SKIPSPACE; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 195 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 196 | /* |
| 197 | * We may receive a tag here. But we aren't ready to deal with it yet. |
| 198 | * So, just use stroul()-like code and serialize the result. |
| 199 | */ |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 200 | for(value = 0, lp = lstart; lp < lstop; lp++) { |
| 201 | int lv = *lp; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 202 | switch(lv) { |
| 203 | case 0x09: case 0x0a: case 0x0d: case 0x20: |
| 204 | if(state == ST_SKIPSPACE) continue; |
| 205 | break; |
| 206 | case 0x2d: /* '-' */ |
| 207 | if(state == ST_SKIPSPACE) { |
| 208 | sign = -1; |
| 209 | state = ST_WAITDIGITS; |
| 210 | continue; |
| 211 | } |
| 212 | break; |
| 213 | case 0x2b: /* '+' */ |
| 214 | if(state == ST_SKIPSPACE) { |
| 215 | state = ST_WAITDIGITS; |
| 216 | continue; |
| 217 | } |
| 218 | break; |
| 219 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 220 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 221 | if(state != ST_DIGITS) state = ST_DIGITS; |
| 222 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 223 | { |
| 224 | long new_value = value * 10; |
| 225 | |
| 226 | if(new_value / 10 != value) |
| 227 | /* Overflow */ |
| 228 | return -1; |
| 229 | |
| 230 | value = new_value + (lv - 0x30); |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 231 | /* Check for two's complement overflow */ |
| 232 | if(value < 0) { |
| 233 | /* Check whether it is a LONG_MIN */ |
| 234 | if(sign == -1 |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 235 | && (unsigned long)value |
| 236 | == ~((unsigned long)-1 >> 1)) { |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 237 | sign = 1; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 238 | } else { |
| 239 | /* Overflow */ |
| 240 | return -1; |
| 241 | } |
| 242 | } |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 243 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 244 | continue; |
| 245 | } |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 246 | break; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | if(state != ST_DIGITS) |
| 250 | return -1; /* No digits */ |
| 251 | |
| 252 | value *= sign; /* Change sign, if needed */ |
| 253 | |
| 254 | if(asn_long2INTEGER(st, value)) |
| 255 | return -1; |
| 256 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 257 | return lp - lstart; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | asn_dec_rval_t |
| 261 | INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 262 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 263 | void *buf_ptr, size_t size) { |
| 264 | |
| 265 | return xer_decode_primitive(opt_codec_ctx, td, |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 266 | sptr, sizeof(INTEGER_t), opt_mname, |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 267 | buf_ptr, size, INTEGER__xer_body_decode); |
| 268 | } |
| 269 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 270 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 271 | INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 272 | int ilevel, enum xer_encoder_flags_e flags, |
| 273 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 274 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
| 275 | asn_enc_rval_t er; |
| 276 | |
| 277 | (void)ilevel; |
| 278 | (void)flags; |
| 279 | |
| 280 | if(!st && !st->buf) |
| 281 | _ASN_ENCODE_FAILED; |
| 282 | |
| 283 | er.encoded = INTEGER__dump(st, cb, app_key); |
| 284 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 285 | |
| 286 | return er; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 287 | } |
| 288 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 289 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 290 | asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 291 | uint8_t *b, *end; |
| 292 | size_t size; |
| 293 | long l; |
| 294 | |
| 295 | /* Sanity checking */ |
| 296 | if(!iptr || !iptr->buf || !lptr) { |
| 297 | errno = EINVAL; |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | /* Cache the begin/end of the buffer */ |
| 302 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 303 | size = iptr->size; |
| 304 | end = b + size; /* Where to stop */ |
| 305 | |
| 306 | if(size > sizeof(long)) { |
| 307 | uint8_t *end1 = end - 1; |
| 308 | /* |
| 309 | * Slightly more advanced processing, |
| 310 | * able to >sizeof(long) bytes, |
| 311 | * when the actual value is small |
| 312 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 313 | */ |
| 314 | /* Skip out the insignificant leading bytes */ |
| 315 | for(; b < end1; b++) { |
| 316 | switch(*b) { |
| 317 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 318 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 319 | } |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | size = end - b; |
| 324 | if(size > sizeof(long)) { |
| 325 | /* Still cannot fit the long */ |
| 326 | errno = ERANGE; |
| 327 | return -1; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* Shortcut processing of a corner case */ |
| 332 | if(end == b) { |
| 333 | *lptr = 0; |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* Perform the sign initialization */ |
| 338 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 339 | if((*b >> 7)) l = -1; else l = 0; |
| 340 | |
| 341 | /* Conversion engine */ |
| 342 | for(; b < end; b++) |
| 343 | l = (l << 8) | *b; |
| 344 | |
| 345 | *lptr = l; |
| 346 | return 0; |
| 347 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 348 | |
| 349 | int |
| 350 | asn_long2INTEGER(INTEGER_t *st, long value) { |
| 351 | uint8_t *buf, *bp; |
| 352 | uint8_t *p; |
| 353 | uint8_t *pstart; |
| 354 | uint8_t *pend1; |
vlm | 96a4e46 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 355 | int littleEndian = 1; /* Run-time detection */ |
| 356 | int add; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 357 | |
| 358 | if(!st) { |
| 359 | errno = EINVAL; |
| 360 | return -1; |
| 361 | } |
| 362 | |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 363 | buf = (uint8_t *)MALLOC(sizeof(value)); |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 364 | if(!buf) return -1; |
| 365 | |
vlm | 96a4e46 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 366 | if(*(char *)&littleEndian) { |
| 367 | pstart = (uint8_t *)&value + sizeof(value) - 1; |
| 368 | pend1 = (uint8_t *)&value; |
| 369 | add = -1; |
| 370 | } else { |
| 371 | pstart = (uint8_t *)&value; |
| 372 | pend1 = pstart + sizeof(value) - 1; |
| 373 | add = 1; |
| 374 | } |
| 375 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 376 | /* |
| 377 | * If the contents octet consists of more than one octet, |
| 378 | * then bits of the first octet and bit 8 of the second octet: |
| 379 | * a) shall not all be ones; and |
| 380 | * b) shall not all be zero. |
| 381 | */ |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 382 | for(p = pstart; p != pend1; p += add) { |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 383 | switch(*p) { |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 384 | case 0x00: if((*(p+add) & 0x80) == 0) |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 385 | continue; |
| 386 | break; |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 387 | case 0xff: if((*(p+add) & 0x80)) |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 388 | continue; |
| 389 | break; |
| 390 | } |
| 391 | break; |
| 392 | } |
| 393 | /* Copy the integer body */ |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 394 | for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add) |
| 395 | *bp++ = *p; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 396 | |
| 397 | if(st->buf) FREEMEM(st->buf); |
| 398 | st->buf = buf; |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 399 | st->size = bp - buf; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |