Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
| 6 | /* |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 7 | * Read the NativeInteger.h for the explanation wrt. differences between |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | * INTEGER and NativeInteger. |
| 9 | * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this |
| 10 | * implementation deals with the standard (machine-specific) representation |
| 11 | * of them instead of using the platform-independent buffer. |
| 12 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 13 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | #include <NativeInteger.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * NativeInteger basic type description. |
| 18 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 19 | static const 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 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 22 | asn_TYPE_operation_t asn_OP_NativeInteger = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 23 | NativeInteger_free, |
| 24 | NativeInteger_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 25 | NativeInteger_compare, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | NativeInteger_decode_ber, |
| 27 | NativeInteger_encode_der, |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 28 | NativeInteger_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 29 | NativeInteger_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 30 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 31 | 0, |
| 32 | 0, |
| 33 | #else |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 34 | NativeInteger_decode_oer, /* OER decoder */ |
| 35 | NativeInteger_encode_oer, /* Canonical OER encoder */ |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 36 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 37 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 38 | 0, |
| 39 | 0, |
| 40 | #else |
| 41 | NativeInteger_decode_uper, /* Unaligned PER decoder */ |
| 42 | NativeInteger_encode_uper, /* Unaligned PER encoder */ |
| 43 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 44 | NativeInteger_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 45 | 0 /* Use generic outmost tag fetcher */ |
| 46 | }; |
| 47 | asn_TYPE_descriptor_t asn_DEF_NativeInteger = { |
| 48 | "INTEGER", /* The ASN.1 type is still INTEGER */ |
| 49 | "INTEGER", |
| 50 | &asn_OP_NativeInteger, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 51 | asn_DEF_NativeInteger_tags, |
| 52 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
| 53 | asn_DEF_NativeInteger_tags, /* Same as above */ |
| 54 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 55 | { 0, 0, asn_generic_no_constraint }, |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 56 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 57 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * Decode INTEGER type. |
| 62 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 63 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 64 | NativeInteger_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 65 | asn_TYPE_descriptor_t *td, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 66 | void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 67 | const asn_INTEGER_specifics_t *specs = |
| 68 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 69 | long *native = (long *)*nint_ptr; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 70 | asn_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | ber_tlv_len_t length; |
| 72 | |
| 73 | /* |
| 74 | * If the structure is not there, allocate it. |
| 75 | */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 76 | if(native == NULL) { |
| 77 | native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native))); |
| 78 | if(native == NULL) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | rval.code = RC_FAIL; |
| 80 | rval.consumed = 0; |
| 81 | return rval; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ASN_DEBUG("Decoding %s as INTEGER (tm=%d)", |
| 86 | td->name, tag_mode); |
| 87 | |
| 88 | /* |
| 89 | * Check tags. |
| 90 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 91 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 92 | tag_mode, 0, &length, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | if(rval.code != RC_OK) |
| 94 | return rval; |
| 95 | |
| 96 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 97 | |
| 98 | /* |
| 99 | * Make sure we have this length. |
| 100 | */ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 101 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | size -= rval.consumed; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 103 | if(length > (ber_tlv_len_t)size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | rval.code = RC_WMORE; |
| 105 | rval.consumed = 0; |
| 106 | return rval; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * ASN.1 encoded INTEGER: buf_ptr, length |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 111 | * Fill the native, at the same time checking for overflow. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | * If overflow occured, return with RC_FAIL. |
| 113 | */ |
| 114 | { |
| 115 | INTEGER_t tmp; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 116 | union { |
| 117 | const void *constbuf; |
| 118 | void *nonconstbuf; |
| 119 | } unconst_buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | long l; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 121 | |
| 122 | unconst_buf.constbuf = buf_ptr; |
| 123 | tmp.buf = (uint8_t *)unconst_buf.nonconstbuf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 124 | tmp.size = length; |
| 125 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 126 | if((specs&&specs->field_unsigned) |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 127 | ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */ |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 128 | : asn_INTEGER2long(&tmp, &l)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | rval.code = RC_FAIL; |
| 130 | rval.consumed = 0; |
| 131 | return rval; |
| 132 | } |
| 133 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 134 | *native = l; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | rval.code = RC_OK; |
| 138 | rval.consumed += length; |
| 139 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 140 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)", |
| 141 | (long)rval.consumed, (long)length, td->name, (long)*native); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 142 | |
| 143 | return rval; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Encode the NativeInteger using the standard INTEGER type DER encoder. |
| 148 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 149 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 150 | NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | int tag_mode, ber_tlv_tag_t tag, |
| 152 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 153 | unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 154 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | INTEGER_t tmp; |
| 156 | |
| 157 | #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */ |
| 158 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 159 | tmp.buf = (uint8_t *)&native; |
| 160 | tmp.size = sizeof(native); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | |
| 162 | #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] | 163 | uint8_t buf[sizeof(native)]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 164 | uint8_t *p; |
| 165 | |
Lev Walkin | d54f255 | 2004-10-27 13:24:51 +0000 | [diff] [blame] | 166 | /* Prepare a fake INTEGER */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 167 | for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8) |
Lev Walkin | f3c089e | 2007-11-13 23:53:13 +0000 | [diff] [blame] | 168 | *p = (uint8_t)native; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | |
| 170 | tmp.buf = buf; |
| 171 | tmp.size = sizeof(buf); |
| 172 | #endif /* WORDS_BIGENDIAN */ |
| 173 | |
| 174 | /* Encode fake INTEGER */ |
| 175 | erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key); |
| 176 | if(erval.encoded == -1) { |
| 177 | assert(erval.structure_ptr == &tmp); |
| 178 | erval.structure_ptr = ptr; |
| 179 | } |
| 180 | return erval; |
| 181 | } |
| 182 | |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 183 | /* |
| 184 | * Decode the chunk of XML text encoding INTEGER. |
| 185 | */ |
| 186 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 187 | NativeInteger_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 188 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 189 | const void *buf_ptr, size_t size) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 190 | const asn_INTEGER_specifics_t *specs = |
| 191 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 192 | asn_dec_rval_t rval; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 193 | INTEGER_t st; |
Lev Walkin | f0b7c9a | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 194 | void *st_ptr = (void *)&st; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 195 | long *native = (long *)*sptr; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 196 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 197 | if(!native) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 198 | native = (long *)(*sptr = CALLOC(1, sizeof(*native))); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 199 | if(!native) ASN__DECODE_FAILED; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 202 | memset(&st, 0, sizeof(st)); |
| 203 | rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr, |
Lev Walkin | f0b7c9a | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 204 | opt_mname, buf_ptr, size); |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 205 | if(rval.code == RC_OK) { |
| 206 | long l; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 207 | if((specs&&specs->field_unsigned) |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 208 | ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */ |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 209 | : asn_INTEGER2long(&st, &l)) { |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 210 | rval.code = RC_FAIL; |
| 211 | rval.consumed = 0; |
| 212 | } else { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 213 | *native = l; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 214 | } |
| 215 | } else { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 216 | /* |
| 217 | * Cannot restart from the middle; |
| 218 | * there is no place to save state in the native type. |
| 219 | * Request a continuation from the very beginning. |
| 220 | */ |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 221 | rval.consumed = 0; |
| 222 | } |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 223 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st); |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 224 | return rval; |
| 225 | } |
| 226 | |
| 227 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 228 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 229 | NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 230 | int ilevel, enum xer_encoder_flags_e flags, |
| 231 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 232 | const asn_INTEGER_specifics_t *specs = |
| 233 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 234 | char scratch[32]; /* Enough for 64-bit int */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 235 | asn_enc_rval_t er; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 236 | const long *native = (const long *)sptr; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 237 | |
| 238 | (void)ilevel; |
| 239 | (void)flags; |
| 240 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 241 | if(!native) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 242 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 243 | er.encoded = snprintf(scratch, sizeof(scratch), |
| 244 | (specs && specs->field_unsigned) |
| 245 | ? "%lu" : "%ld", *native); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 246 | if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch) |
| 247 | || cb(scratch, er.encoded, app_key) < 0) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 248 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 249 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 250 | ASN__ENCODED_OK(er); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Lev Walkin | 62d7687 | 2017-08-05 22:49:42 -0700 | [diff] [blame] | 253 | #ifndef ASN_DISABLE_PER_SUPPORT |
| 254 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 255 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 256 | NativeInteger_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 257 | asn_TYPE_descriptor_t *td, |
| 258 | const asn_per_constraints_t *constraints, void **sptr, |
| 259 | asn_per_data_t *pd) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 260 | const asn_INTEGER_specifics_t *specs = |
| 261 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 262 | asn_dec_rval_t rval; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 263 | long *native = (long *)*sptr; |
| 264 | INTEGER_t tmpint; |
| 265 | void *tmpintptr = &tmpint; |
| 266 | |
| 267 | (void)opt_codec_ctx; |
| 268 | ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name); |
| 269 | |
| 270 | if(!native) { |
| 271 | native = (long *)(*sptr = CALLOC(1, sizeof(*native))); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 272 | if(!native) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | memset(&tmpint, 0, sizeof tmpint); |
| 276 | rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints, |
| 277 | &tmpintptr, pd); |
Lev Walkin | b2bed82 | 2005-11-27 12:40:43 +0000 | [diff] [blame] | 278 | if(rval.code == RC_OK) { |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 279 | if((specs&&specs->field_unsigned) |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 280 | ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 281 | : asn_INTEGER2long(&tmpint, native)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 282 | rval.code = RC_FAIL; |
| 283 | else |
| 284 | ASN_DEBUG("NativeInteger %s got value %ld", |
| 285 | td->name, *native); |
Lev Walkin | b2bed82 | 2005-11-27 12:40:43 +0000 | [diff] [blame] | 286 | } |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 287 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 288 | |
| 289 | return rval; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 292 | asn_enc_rval_t |
| 293 | NativeInteger_encode_uper(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 294 | const asn_per_constraints_t *constraints, void *sptr, |
| 295 | asn_per_outp_t *po) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 296 | const asn_INTEGER_specifics_t *specs = |
| 297 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 298 | asn_enc_rval_t er; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 299 | long native; |
| 300 | INTEGER_t tmpint; |
| 301 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 302 | if(!sptr) ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 303 | |
| 304 | native = *(long *)sptr; |
| 305 | |
| 306 | ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native); |
| 307 | |
| 308 | memset(&tmpint, 0, sizeof(tmpint)); |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 309 | if((specs&&specs->field_unsigned) |
| 310 | ? asn_ulong2INTEGER(&tmpint, native) |
| 311 | : asn_long2INTEGER(&tmpint, native)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 312 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 313 | er = INTEGER_encode_uper(td, constraints, &tmpint, po); |
| 314 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint); |
| 315 | return er; |
| 316 | } |
| 317 | |
Lev Walkin | 62d7687 | 2017-08-05 22:49:42 -0700 | [diff] [blame] | 318 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 319 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 320 | /* |
| 321 | * INTEGER specific human-readable output. |
| 322 | */ |
| 323 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 324 | NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | ef8dd44 | 2017-09-18 22:38:03 -0700 | [diff] [blame] | 325 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 326 | const asn_INTEGER_specifics_t *specs = |
| 327 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 328 | const long *native = (const long *)sptr; |
Lev Walkin | ef8dd44 | 2017-09-18 22:38:03 -0700 | [diff] [blame] | 329 | char scratch[32]; /* Enough for 64-bit int */ |
| 330 | int ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 331 | |
Lev Walkin | ef8dd44 | 2017-09-18 22:38:03 -0700 | [diff] [blame] | 332 | (void)td; /* Unused argument */ |
| 333 | (void)ilevel; /* Unused argument */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 334 | |
Lev Walkin | ef8dd44 | 2017-09-18 22:38:03 -0700 | [diff] [blame] | 335 | if(native) { |
| 336 | long value = *native; |
| 337 | ret = snprintf(scratch, sizeof(scratch), |
| 338 | (specs && specs->field_unsigned) ? "%lu" : "%ld", value); |
| 339 | assert(ret > 0 && (size_t)ret < sizeof(scratch)); |
| 340 | if(cb(scratch, ret, app_key) < 0) return -1; |
| 341 | if(specs && (value >= 0 || !specs->field_unsigned)) { |
| 342 | const asn_INTEGER_enum_map_t *el = |
| 343 | INTEGER_map_value2enum(specs, value); |
| 344 | if(el) { |
| 345 | if(cb(" (", 2, app_key) < 0) return -1; |
| 346 | if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1; |
| 347 | if(cb(")", 1, app_key) < 0) return -1; |
| 348 | } |
| 349 | } |
| 350 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 351 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 352 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | void |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 357 | NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr, |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 358 | enum asn_struct_free_method method) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 359 | if(!td || !ptr) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 360 | return; |
| 361 | |
| 362 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 363 | td->name, method, ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 364 | |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 365 | switch(method) { |
| 366 | case ASFM_FREE_EVERYTHING: |
| 367 | FREEMEM(ptr); |
| 368 | break; |
| 369 | case ASFM_FREE_UNDERLYING: |
| 370 | break; |
| 371 | case ASFM_FREE_UNDERLYING_AND_RESET: |
| 372 | memset(ptr, 0, sizeof(long)); |
| 373 | break; |
| 374 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 377 | int |
| 378 | NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) { |
| 379 | (void)td; |
| 380 | |
| 381 | if(aptr && bptr) { |
| 382 | const asn_INTEGER_specifics_t *specs = |
| 383 | (const asn_INTEGER_specifics_t *)td->specifics; |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 384 | if(specs && specs->field_unsigned) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 385 | const unsigned long *a = aptr; |
| 386 | const unsigned long *b = bptr; |
| 387 | if(*a < *b) { |
| 388 | return -1; |
| 389 | } else if(*a > *b) { |
| 390 | return 1; |
| 391 | } else { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 392 | return 0; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 393 | } |
| 394 | } else { |
| 395 | const long *a = aptr; |
| 396 | const long *b = bptr; |
| 397 | if(*a < *b) { |
| 398 | return -1; |
| 399 | } else if(*a > *b) { |
| 400 | return 1; |
| 401 | } else { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 402 | return 0; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } else if(!aptr) { |
| 406 | return -1; |
| 407 | } else { |
| 408 | return 1; |
| 409 | } |
| 410 | } |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 411 | |
| 412 | asn_random_fill_result_t |
| 413 | NativeInteger_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 414 | const asn_encoding_constraints_t *constraints, |
| 415 | size_t max_length) { |
| 416 | const asn_INTEGER_specifics_t *specs = |
| 417 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 418 | asn_random_fill_result_t result_ok = {ARFILL_OK, 1}; |
| 419 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 420 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
| 421 | long *st = *sptr; |
| 422 | const asn_INTEGER_enum_map_t *emap; |
| 423 | size_t emap_len; |
| 424 | intmax_t value; |
| 425 | int find_inside_map; |
| 426 | |
| 427 | if(max_length == 0) return result_skipped; |
| 428 | |
| 429 | if(st == NULL) { |
| 430 | st = (long *)CALLOC(1, sizeof(*st)); |
| 431 | if(st == NULL) { |
| 432 | return result_failed; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if(specs) { |
| 437 | emap = specs->value2enum; |
| 438 | emap_len = specs->map_count; |
| 439 | if(specs->strict_enumeration) { |
| 440 | find_inside_map = emap_len > 0; |
| 441 | } else { |
| 442 | find_inside_map = emap_len ? asn_random_between(0, 1) : 0; |
| 443 | } |
| 444 | } else { |
| 445 | emap = 0; |
| 446 | emap_len = 0; |
| 447 | find_inside_map = 0; |
| 448 | } |
| 449 | |
| 450 | if(find_inside_map) { |
| 451 | assert(emap_len > 0); |
| 452 | value = emap[asn_random_between(0, emap_len - 1)].nat_value; |
| 453 | } else { |
Lev Walkin | 18741a9 | 2017-10-01 12:40:19 -0700 | [diff] [blame] | 454 | const asn_per_constraints_t *ct; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 455 | |
| 456 | static const long variants[] = { |
| 457 | -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384, |
| 458 | -16383, -257, -256, -255, -254, -129, -128, -127, |
| 459 | -126, -1, 0, 1, 126, 127, 128, 129, |
| 460 | 254, 255, 256, 257, 16383, 16384, 16385, 32767, |
| 461 | 32768, 32769, 65534, 65535, 65536, 65537}; |
| 462 | if(specs && specs->field_unsigned) { |
| 463 | assert(variants[18] == 0); |
| 464 | value = variants[asn_random_between( |
| 465 | 18, sizeof(variants) / sizeof(variants[0]) - 1)]; |
| 466 | } else { |
| 467 | value = variants[asn_random_between( |
| 468 | 0, sizeof(variants) / sizeof(variants[0]) - 1)]; |
| 469 | } |
| 470 | |
| 471 | if(!constraints) constraints = &td->encoding_constraints; |
Lev Walkin | 18741a9 | 2017-10-01 12:40:19 -0700 | [diff] [blame] | 472 | ct = constraints ? constraints->per_constraints : 0; |
| 473 | if(ct && (ct->value.flags & APC_CONSTRAINED)) { |
| 474 | if(value < ct->value.lower_bound || value > ct->value.upper_bound) { |
| 475 | value = asn_random_between(ct->value.lower_bound, |
| 476 | ct->value.upper_bound); |
| 477 | } |
| 478 | } |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Lev Walkin | b518668 | 2017-09-30 19:32:13 -0700 | [diff] [blame] | 481 | *sptr = st; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 482 | *st = value; |
| 483 | return result_ok; |
| 484 | } |