vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
vlm | 5ec2fe1 | 2005-03-29 17:21:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2005 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 | */ |
| 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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 12 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | #include <NativeInteger.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * NativeInteger basic type description. |
| 17 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 18 | static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 20 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 21 | asn_TYPE_descriptor_t asn_DEF_NativeInteger = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | "INTEGER", /* The ASN.1 type is still INTEGER */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 23 | "INTEGER", |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | NativeInteger_free, |
| 25 | NativeInteger_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | asn_generic_no_constraint, |
| 27 | NativeInteger_decode_ber, |
| 28 | NativeInteger_encode_der, |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 29 | NativeInteger_decode_xer, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 30 | NativeInteger_encode_xer, |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 31 | NativeInteger_decode_uper, /* Unaligned PER decoder */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 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]), |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 37 | 0, /* No PER visible constraints */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 38 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 39 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * Decode INTEGER type. |
| 44 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 45 | asn_dec_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 46 | NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 47 | asn_TYPE_descriptor_t *td, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 48 | void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 49 | long *native = (long *)*nint_ptr; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 50 | asn_dec_rval_t rval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | ber_tlv_len_t length; |
| 52 | |
| 53 | /* |
| 54 | * If the structure is not there, allocate it. |
| 55 | */ |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 56 | if(native == NULL) { |
| 57 | native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native))); |
| 58 | if(native == NULL) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | rval.code = RC_FAIL; |
| 60 | rval.consumed = 0; |
| 61 | return rval; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | ASN_DEBUG("Decoding %s as INTEGER (tm=%d)", |
| 66 | td->name, tag_mode); |
| 67 | |
| 68 | /* |
| 69 | * Check tags. |
| 70 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 71 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 72 | tag_mode, 0, &length, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | if(rval.code != RC_OK) |
| 74 | return rval; |
| 75 | |
| 76 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 77 | |
| 78 | /* |
| 79 | * Make sure we have this length. |
| 80 | */ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 81 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | size -= rval.consumed; |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 83 | if(length > (ber_tlv_len_t)size) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | rval.code = RC_WMORE; |
| 85 | rval.consumed = 0; |
| 86 | return rval; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * ASN.1 encoded INTEGER: buf_ptr, length |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 91 | * Fill the native, at the same time checking for overflow. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | * If overflow occured, return with RC_FAIL. |
| 93 | */ |
| 94 | { |
| 95 | INTEGER_t tmp; |
vlm | 41dee12 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 96 | union { |
| 97 | const void *constbuf; |
| 98 | void *nonconstbuf; |
| 99 | } unconst_buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | long l; |
vlm | 41dee12 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 101 | |
| 102 | unconst_buf.constbuf = buf_ptr; |
| 103 | tmp.buf = (uint8_t *)unconst_buf.nonconstbuf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | tmp.size = length; |
| 105 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 106 | if(asn_INTEGER2long(&tmp, &l)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | rval.code = RC_FAIL; |
| 108 | rval.consumed = 0; |
| 109 | return rval; |
| 110 | } |
| 111 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 112 | *native = l; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | rval.code = RC_OK; |
| 116 | rval.consumed += length; |
| 117 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 118 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)", |
| 119 | (long)rval.consumed, (long)length, td->name, (long)*native); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | |
| 121 | return rval; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Encode the NativeInteger using the standard INTEGER type DER encoder. |
| 126 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 127 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 128 | NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | int tag_mode, ber_tlv_tag_t tag, |
| 130 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 131 | unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 132 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | INTEGER_t tmp; |
| 134 | |
| 135 | #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */ |
| 136 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 137 | tmp.buf = (uint8_t *)&native; |
| 138 | tmp.size = sizeof(native); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | |
| 140 | #else /* Works even if WORDS_BIGENDIAN is not set where should've been */ |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 141 | uint8_t buf[sizeof(native)]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 142 | uint8_t *p; |
| 143 | |
vlm | 978078f | 2004-10-27 13:24:51 +0000 | [diff] [blame] | 144 | /* Prepare a fake INTEGER */ |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 145 | for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8) |
| 146 | *p = native; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | |
| 148 | tmp.buf = buf; |
| 149 | tmp.size = sizeof(buf); |
| 150 | #endif /* WORDS_BIGENDIAN */ |
| 151 | |
| 152 | /* Encode fake INTEGER */ |
| 153 | erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key); |
| 154 | if(erval.encoded == -1) { |
| 155 | assert(erval.structure_ptr == &tmp); |
| 156 | erval.structure_ptr = ptr; |
| 157 | } |
| 158 | return erval; |
| 159 | } |
| 160 | |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 161 | /* |
| 162 | * Decode the chunk of XML text encoding INTEGER. |
| 163 | */ |
| 164 | asn_dec_rval_t |
| 165 | NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 166 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 167 | const void *buf_ptr, size_t size) { |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 168 | asn_dec_rval_t rval; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 169 | INTEGER_t st; |
vlm | 8c0d231 | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 170 | void *st_ptr = (void *)&st; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 171 | long *native = (long *)*sptr; |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 172 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 173 | if(!native) { |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 174 | native = (long *)(*sptr = CALLOC(1, sizeof(*native))); |
| 175 | if(!native) _ASN_DECODE_FAILED; |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 176 | } |
| 177 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 178 | memset(&st, 0, sizeof(st)); |
| 179 | rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr, |
vlm | 8c0d231 | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 180 | opt_mname, buf_ptr, size); |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 181 | if(rval.code == RC_OK) { |
| 182 | long l; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 183 | if(asn_INTEGER2long(&st, &l)) { |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 184 | rval.code = RC_FAIL; |
| 185 | rval.consumed = 0; |
| 186 | } else { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 187 | *native = l; |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 188 | } |
| 189 | } else { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 190 | /* |
| 191 | * Cannot restart from the middle; |
| 192 | * there is no place to save state in the native type. |
| 193 | * Request a continuation from the very beginning. |
| 194 | */ |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 195 | rval.consumed = 0; |
| 196 | } |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 197 | asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, &st, 1); |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 198 | return rval; |
| 199 | } |
| 200 | |
| 201 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 202 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 203 | NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 204 | int ilevel, enum xer_encoder_flags_e flags, |
| 205 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 206 | char scratch[32]; /* Enough for 64-bit int */ |
| 207 | asn_enc_rval_t er; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 208 | const long *native = (const long *)sptr; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 209 | |
| 210 | (void)ilevel; |
| 211 | (void)flags; |
| 212 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 213 | if(!native) _ASN_ENCODE_FAILED; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 214 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 215 | er.encoded = snprintf(scratch, sizeof(scratch), "%ld", *native); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 216 | if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch) |
| 217 | || cb(scratch, er.encoded, app_key) < 0) |
| 218 | _ASN_ENCODE_FAILED; |
| 219 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 220 | _ASN_ENCODED_OK(er); |
| 221 | } |
| 222 | |
| 223 | asn_dec_rval_t |
| 224 | NativeInteger_decode_uper(asn_codec_ctx_t *opt_codec_ctx, |
| 225 | asn_TYPE_descriptor_t *td, |
| 226 | asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { |
| 227 | |
| 228 | asn_dec_rval_t rval; |
| 229 | long *native = (long *)*sptr; |
| 230 | INTEGER_t tmpint; |
| 231 | void *tmpintptr = &tmpint; |
| 232 | |
| 233 | (void)opt_codec_ctx; |
| 234 | ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name); |
| 235 | |
| 236 | if(!native) { |
| 237 | native = (long *)(*sptr = CALLOC(1, sizeof(*native))); |
| 238 | if(!native) _ASN_DECODE_FAILED; |
| 239 | } |
| 240 | |
| 241 | memset(&tmpint, 0, sizeof tmpint); |
| 242 | rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints, |
| 243 | &tmpintptr, pd); |
| 244 | if(rval.code == RC_OK) |
| 245 | if(asn_INTEGER2long(&tmpint, native)) |
| 246 | rval.code = RC_FAIL; |
| 247 | else |
| 248 | ASN_DEBUG("NativeInteger %s got value %ld", |
| 249 | td->name, *native); |
| 250 | asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, &tmpint, 1); |
| 251 | |
| 252 | return rval; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 253 | } |
| 254 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 255 | /* |
| 256 | * INTEGER specific human-readable output. |
| 257 | */ |
| 258 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 259 | NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 260 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 261 | const long *native = (const long *)sptr; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 262 | char scratch[32]; /* Enough for 64-bit int */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 263 | int ret; |
| 264 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 265 | (void)td; /* Unused argument */ |
| 266 | (void)ilevel; /* Unused argument */ |
| 267 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 268 | if(native) { |
| 269 | ret = snprintf(scratch, sizeof(scratch), "%ld", *native); |
| 270 | assert(ret > 0 && (size_t)ret < sizeof(scratch)); |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 271 | return (cb(scratch, ret, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 272 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 273 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 278 | NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 279 | |
| 280 | if(!td || !ptr) |
| 281 | return; |
| 282 | |
| 283 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
| 284 | td->name, contents_only, ptr); |
| 285 | |
| 286 | if(!contents_only) { |
| 287 | FREEMEM(ptr); |
| 288 | } |
| 289 | } |
| 290 | |