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