vlm | fa67ddc | 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 | */ |
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> |
| 14 | #include <INTEGER.h> |
| 15 | #include <assert.h> |
| 16 | |
| 17 | /* |
| 18 | * NativeInteger basic type description. |
| 19 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 20 | static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 22 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 23 | asn_TYPE_descriptor_t asn_DEF_NativeInteger = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | "INTEGER", /* The ASN.1 type is still INTEGER */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 25 | "INTEGER", |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 26 | NativeInteger_free, |
| 27 | NativeInteger_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | asn_generic_no_constraint, |
| 29 | NativeInteger_decode_ber, |
| 30 | NativeInteger_encode_der, |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 31 | NativeInteger_decode_xer, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 32 | NativeInteger_encode_xer, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 33 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 34 | asn_DEF_NativeInteger_tags, |
| 35 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
| 36 | asn_DEF_NativeInteger_tags, /* Same as above */ |
| 37 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
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 | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | void **int_ptr, void *buf_ptr, size_t size, int tag_mode) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 49 | int *Int = (int *)*int_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 | */ |
| 56 | if(Int == NULL) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 57 | Int = (int *)(*int_ptr = CALLOC(1, sizeof(*Int))); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | if(Int == NULL) { |
| 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 | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 81 | buf_ptr = ((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 |
| 91 | * Fill the Int, at the same time checking for overflow. |
| 92 | * If overflow occured, return with RC_FAIL. |
| 93 | */ |
| 94 | { |
| 95 | INTEGER_t tmp; |
| 96 | long l; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 97 | tmp.buf = (uint8_t *)buf_ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | tmp.size = length; |
| 99 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 100 | if(asn_INTEGER2long(&tmp, &l)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | rval.code = RC_FAIL; |
| 102 | rval.consumed = 0; |
| 103 | return rval; |
| 104 | } |
| 105 | |
| 106 | *Int = l; |
| 107 | |
| 108 | /* |
| 109 | * Note that int might be shorter than long. |
| 110 | * This expression hopefully will be optimized away |
| 111 | * by compiler. |
| 112 | */ |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 113 | if(sizeof(int) != sizeof(long) && ((long)*Int != l)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | *Int = 0; /* Safe value */ |
| 115 | rval.code = RC_FAIL; |
| 116 | rval.consumed = 0; |
| 117 | return rval; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | rval.code = RC_OK; |
| 122 | rval.consumed += length; |
| 123 | |
| 124 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)", |
| 125 | (long)rval.consumed, (long)length, td->name, *Int); |
| 126 | |
| 127 | return rval; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Encode the NativeInteger using the standard INTEGER type DER encoder. |
| 132 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 133 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 134 | NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | int tag_mode, ber_tlv_tag_t tag, |
| 136 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 137 | unsigned int Int = *(unsigned int *)ptr; /* Disable sign ext. */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 138 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | INTEGER_t tmp; |
| 140 | |
| 141 | #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */ |
| 142 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 143 | tmp.buf = (uint8_t *)∬ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | tmp.size = sizeof(Int); |
| 145 | |
| 146 | #else /* Works even if WORDS_BIGENDIAN is not set where should've been */ |
| 147 | uint8_t buf[sizeof(int)]; |
| 148 | uint8_t *p; |
| 149 | |
| 150 | /* Prepare fake INTEGER */ |
| 151 | for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8) |
| 152 | *p = Int & 0xff; |
| 153 | |
| 154 | tmp.buf = buf; |
| 155 | tmp.size = sizeof(buf); |
| 156 | #endif /* WORDS_BIGENDIAN */ |
| 157 | |
| 158 | /* Encode fake INTEGER */ |
| 159 | erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key); |
| 160 | if(erval.encoded == -1) { |
| 161 | assert(erval.structure_ptr == &tmp); |
| 162 | erval.structure_ptr = ptr; |
| 163 | } |
| 164 | return erval; |
| 165 | } |
| 166 | |
vlm | d52f9b5 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 167 | /* |
| 168 | * Decode the chunk of XML text encoding INTEGER. |
| 169 | */ |
| 170 | asn_dec_rval_t |
| 171 | NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 172 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 173 | void *buf_ptr, size_t size) { |
| 174 | asn_dec_rval_t rval; |
| 175 | INTEGER_t *st = 0; |
| 176 | int *Int = (int *)*sptr; |
| 177 | |
| 178 | if(!Int) { |
| 179 | (void *)Int = *sptr = CALLOC(1, sizeof(int)); |
| 180 | if(!Int) { |
| 181 | rval.code = RC_FAIL; |
| 182 | rval.consumed = 0; |
| 183 | return rval; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | rval = INTEGER_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname, |
| 188 | buf_ptr, size); |
| 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 { |
| 195 | *Int = l; |
| 196 | |
| 197 | /* int might be shorter than long */ |
| 198 | if(sizeof(int) != sizeof(long) && ((long)*Int != l)) { |
| 199 | *Int = 0; /* Safe value */ |
| 200 | rval.code = RC_FAIL; |
| 201 | rval.consumed = 0; |
| 202 | return rval; |
| 203 | } |
| 204 | } |
| 205 | } else { |
| 206 | rval.consumed = 0; |
| 207 | } |
| 208 | asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0); |
| 209 | return rval; |
| 210 | } |
| 211 | |
| 212 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 213 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 214 | NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 215 | int ilevel, enum xer_encoder_flags_e flags, |
| 216 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 217 | char scratch[32]; /* Enough for 64-bit int */ |
| 218 | asn_enc_rval_t er; |
| 219 | const int *Int = (const int *)sptr; |
| 220 | |
| 221 | (void)ilevel; |
| 222 | (void)flags; |
| 223 | |
| 224 | if(!Int) _ASN_ENCODE_FAILED; |
| 225 | |
| 226 | er.encoded = snprintf(scratch, sizeof(scratch), "%d", *Int); |
| 227 | if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch) |
| 228 | || cb(scratch, er.encoded, app_key) < 0) |
| 229 | _ASN_ENCODE_FAILED; |
| 230 | |
| 231 | return er; |
| 232 | } |
| 233 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | /* |
| 235 | * INTEGER specific human-readable output. |
| 236 | */ |
| 237 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 238 | NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 239 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 240 | const int *Int = (const int *)sptr; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 241 | char scratch[32]; /* Enough for 64-bit int */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 242 | int ret; |
| 243 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 244 | (void)td; /* Unused argument */ |
| 245 | (void)ilevel; /* Unused argument */ |
| 246 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | if(Int) { |
| 248 | ret = snprintf(scratch, sizeof(scratch), "%d", *Int); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 249 | assert(ret > 0 && ret < (int)sizeof(scratch)); |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 250 | return (cb(scratch, ret, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 251 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 252 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 257 | NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 258 | |
| 259 | if(!td || !ptr) |
| 260 | return; |
| 261 | |
| 262 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
| 263 | td->name, contents_only, ptr); |
| 264 | |
| 265 | if(!contents_only) { |
| 266 | FREEMEM(ptr); |
| 267 | } |
| 268 | } |
| 269 | |