Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | /* |
| 6 | * Please read the NativeInteger.h for the explanation wrt. differences between |
| 7 | * INTEGER and NativeInteger. |
| 8 | * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this |
| 9 | * implementation deals with the standard (machine-specific) representation |
| 10 | * of them instead of using the platform-independent buffer. |
| 11 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 12 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | #include <NativeInteger.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | |
| 16 | /* |
| 17 | * NativeInteger basic type description. |
| 18 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 19 | static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 21 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 22 | asn_TYPE_descriptor_t asn_DEF_NativeInteger = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | "INTEGER", /* The ASN.1 type is still INTEGER */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 24 | "INTEGER", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 25 | NativeInteger_free, |
| 26 | NativeInteger_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | asn_generic_no_constraint, |
| 28 | NativeInteger_decode_ber, |
| 29 | NativeInteger_encode_der, |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 30 | NativeInteger_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 31 | NativeInteger_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 33 | asn_DEF_NativeInteger_tags, |
| 34 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
| 35 | asn_DEF_NativeInteger_tags, /* Same as above */ |
| 36 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 37 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 38 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * Decode INTEGER type. |
| 43 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 44 | asn_dec_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 45 | NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 46 | asn_TYPE_descriptor_t *td, |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 47 | void **nint_ptr, void *buf_ptr, size_t size, int tag_mode) { |
| 48 | long *native = (long *)*nint_ptr; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 49 | asn_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 50 | ber_tlv_len_t length; |
| 51 | |
| 52 | /* |
| 53 | * If the structure is not there, allocate it. |
| 54 | */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 55 | if(native == NULL) { |
| 56 | native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native))); |
| 57 | if(native == NULL) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | rval.code = RC_FAIL; |
| 59 | rval.consumed = 0; |
| 60 | return rval; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | ASN_DEBUG("Decoding %s as INTEGER (tm=%d)", |
| 65 | td->name, tag_mode); |
| 66 | |
| 67 | /* |
| 68 | * Check tags. |
| 69 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 70 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 71 | tag_mode, 0, &length, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | if(rval.code != RC_OK) |
| 73 | return rval; |
| 74 | |
| 75 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 76 | |
| 77 | /* |
| 78 | * Make sure we have this length. |
| 79 | */ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 80 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | size -= rval.consumed; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 82 | if(length > (ber_tlv_len_t)size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 83 | rval.code = RC_WMORE; |
| 84 | rval.consumed = 0; |
| 85 | return rval; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * ASN.1 encoded INTEGER: buf_ptr, length |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 90 | * Fill the native, at the same time checking for overflow. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 91 | * If overflow occured, return with RC_FAIL. |
| 92 | */ |
| 93 | { |
| 94 | INTEGER_t tmp; |
| 95 | long l; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 96 | tmp.buf = (uint8_t *)buf_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | tmp.size = length; |
| 98 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 99 | if(asn_INTEGER2long(&tmp, &l)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | rval.code = RC_FAIL; |
| 101 | rval.consumed = 0; |
| 102 | return rval; |
| 103 | } |
| 104 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 105 | *native = l; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 108 | * Note that native integer size might be other than long. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 109 | * This expression hopefully will be optimized away |
| 110 | * by compiler. |
| 111 | */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 112 | if(sizeof(*native) != sizeof(long) && ((long)*native != l)) { |
| 113 | *native = 0; /* Safe value */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | rval.code = RC_FAIL; |
| 115 | rval.consumed = 0; |
| 116 | return rval; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | rval.code = RC_OK; |
| 121 | rval.consumed += length; |
| 122 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 123 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)", |
| 124 | (long)rval.consumed, (long)length, td->name, (long)*native); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 125 | |
| 126 | return rval; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Encode the NativeInteger using the standard INTEGER type DER encoder. |
| 131 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 132 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 133 | NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | int tag_mode, ber_tlv_tag_t tag, |
| 135 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 136 | unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 137 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 138 | INTEGER_t tmp; |
| 139 | |
| 140 | #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */ |
| 141 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 142 | tmp.buf = (uint8_t *)&native; |
| 143 | tmp.size = sizeof(native); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | |
| 145 | #else /* Works even if WORDS_BIGENDIAN is not set where should've been */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 146 | uint8_t buf[sizeof(native)]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | uint8_t *p; |
| 148 | |
Lev Walkin | d54f255 | 2004-10-27 13:24:51 +0000 | [diff] [blame] | 149 | /* Prepare a fake INTEGER */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 150 | for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8) |
| 151 | *p = native; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | |
| 153 | tmp.buf = buf; |
| 154 | tmp.size = sizeof(buf); |
| 155 | #endif /* WORDS_BIGENDIAN */ |
| 156 | |
| 157 | /* Encode fake INTEGER */ |
| 158 | erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key); |
| 159 | if(erval.encoded == -1) { |
| 160 | assert(erval.structure_ptr == &tmp); |
| 161 | erval.structure_ptr = ptr; |
| 162 | } |
| 163 | return erval; |
| 164 | } |
| 165 | |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 166 | /* |
| 167 | * Decode the chunk of XML text encoding INTEGER. |
| 168 | */ |
| 169 | asn_dec_rval_t |
| 170 | NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 171 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 172 | void *buf_ptr, size_t size) { |
| 173 | asn_dec_rval_t rval; |
| 174 | INTEGER_t *st = 0; |
Lev Walkin | f0b7c9a | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 175 | void *st_ptr = (void *)&st; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 176 | long *native = (long *)*sptr; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 177 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 178 | if(!native) { |
Lev Walkin | c17d90f | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 179 | *sptr = CALLOC(1, sizeof(int)); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 180 | native = (long *)*sptr; |
| 181 | if(!native) { |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 182 | rval.code = RC_FAIL; |
| 183 | rval.consumed = 0; |
| 184 | return rval; |
| 185 | } |
| 186 | } |
| 187 | |
Lev Walkin | f0b7c9a | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 188 | rval = INTEGER_decode_xer(opt_codec_ctx, td, (void **)st_ptr, |
| 189 | opt_mname, buf_ptr, size); |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 190 | if(rval.code == RC_OK) { |
| 191 | long l; |
| 192 | if(asn_INTEGER2long(st, &l)) { |
| 193 | rval.code = RC_FAIL; |
| 194 | rval.consumed = 0; |
| 195 | } else { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 196 | *native = l; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 197 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 198 | /* Native type might be shorter than long */ |
| 199 | if(sizeof(*native) != sizeof(long) |
| 200 | && ((long)*native != l)) { |
| 201 | *native = 0; /* Safe value */ |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 202 | rval.code = RC_FAIL; |
| 203 | rval.consumed = 0; |
| 204 | return rval; |
| 205 | } |
| 206 | } |
| 207 | } else { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 208 | /* |
| 209 | * Cannot restart from the middle; |
| 210 | * there is no place to save state in the native type. |
| 211 | * Request a continuation from the very beginning. |
| 212 | */ |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 213 | rval.consumed = 0; |
| 214 | } |
| 215 | asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0); |
| 216 | return rval; |
| 217 | } |
| 218 | |
| 219 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 220 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 221 | NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 222 | int ilevel, enum xer_encoder_flags_e flags, |
| 223 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 224 | char scratch[32]; /* Enough for 64-bit int */ |
| 225 | asn_enc_rval_t er; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 226 | const long *native = (const long *)sptr; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 227 | |
| 228 | (void)ilevel; |
| 229 | (void)flags; |
| 230 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 231 | if(!native) _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 232 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 233 | er.encoded = snprintf(scratch, sizeof(scratch), "%ld", *native); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 234 | if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch) |
| 235 | || cb(scratch, er.encoded, app_key) < 0) |
| 236 | _ASN_ENCODE_FAILED; |
| 237 | |
| 238 | return er; |
| 239 | } |
| 240 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 241 | /* |
| 242 | * INTEGER specific human-readable output. |
| 243 | */ |
| 244 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 245 | NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 246 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 247 | const long *native = (const long *)sptr; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 248 | char scratch[32]; /* Enough for 64-bit int */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 249 | int ret; |
| 250 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 251 | (void)td; /* Unused argument */ |
| 252 | (void)ilevel; /* Unused argument */ |
| 253 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 254 | if(native) { |
| 255 | ret = snprintf(scratch, sizeof(scratch), "%ld", *native); |
| 256 | assert(ret > 0 && (size_t)ret < sizeof(scratch)); |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 257 | return (cb(scratch, ret, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 258 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 259 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 264 | NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 265 | |
| 266 | if(!td || !ptr) |
| 267 | return; |
| 268 | |
| 269 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
| 270 | td->name, contents_only, ptr); |
| 271 | |
| 272 | if(!contents_only) { |
| 273 | FREEMEM(ptr); |
| 274 | } |
| 275 | } |
| 276 | |