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 | }; |
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 | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 27 | NativeInteger_compare, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | asn_generic_no_constraint, |
| 29 | NativeInteger_decode_ber, |
| 30 | NativeInteger_encode_der, |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 31 | NativeInteger_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 32 | NativeInteger_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 33 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 34 | 0, |
| 35 | 0, |
| 36 | #else |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 37 | NativeInteger_decode_oer, /* OER decoder */ |
| 38 | NativeInteger_encode_oer, /* Canonical OER encoder */ |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 39 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 40 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 41 | 0, |
| 42 | 0, |
| 43 | #else |
| 44 | NativeInteger_decode_uper, /* Unaligned PER decoder */ |
| 45 | NativeInteger_encode_uper, /* Unaligned PER encoder */ |
| 46 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 48 | asn_DEF_NativeInteger_tags, |
| 49 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
| 50 | asn_DEF_NativeInteger_tags, /* Same as above */ |
| 51 | sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]), |
Lev Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 52 | 0, /* No OER visible constraints */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 53 | 0, /* No PER visible constraints */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 54 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 55 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * Decode INTEGER type. |
| 60 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 61 | asn_dec_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 62 | NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 63 | asn_TYPE_descriptor_t *td, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 64 | 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] | 65 | const asn_INTEGER_specifics_t *specs = |
| 66 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 67 | long *native = (long *)*nint_ptr; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 68 | asn_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 69 | ber_tlv_len_t length; |
| 70 | |
| 71 | /* |
| 72 | * If the structure is not there, allocate it. |
| 73 | */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 74 | if(native == NULL) { |
| 75 | native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native))); |
| 76 | if(native == NULL) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 77 | rval.code = RC_FAIL; |
| 78 | rval.consumed = 0; |
| 79 | return rval; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ASN_DEBUG("Decoding %s as INTEGER (tm=%d)", |
| 84 | td->name, tag_mode); |
| 85 | |
| 86 | /* |
| 87 | * Check tags. |
| 88 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 89 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 90 | tag_mode, 0, &length, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 91 | if(rval.code != RC_OK) |
| 92 | return rval; |
| 93 | |
| 94 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 95 | |
| 96 | /* |
| 97 | * Make sure we have this length. |
| 98 | */ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 99 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | size -= rval.consumed; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 101 | if(length > (ber_tlv_len_t)size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | rval.code = RC_WMORE; |
| 103 | rval.consumed = 0; |
| 104 | return rval; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * ASN.1 encoded INTEGER: buf_ptr, length |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 109 | * Fill the native, at the same time checking for overflow. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | * If overflow occured, return with RC_FAIL. |
| 111 | */ |
| 112 | { |
| 113 | INTEGER_t tmp; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 114 | union { |
| 115 | const void *constbuf; |
| 116 | void *nonconstbuf; |
| 117 | } unconst_buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | long l; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 119 | |
| 120 | unconst_buf.constbuf = buf_ptr; |
| 121 | tmp.buf = (uint8_t *)unconst_buf.nonconstbuf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | tmp.size = length; |
| 123 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 124 | if((specs&&specs->field_unsigned) |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 125 | ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */ |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 126 | : asn_INTEGER2long(&tmp, &l)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | rval.code = RC_FAIL; |
| 128 | rval.consumed = 0; |
| 129 | return rval; |
| 130 | } |
| 131 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 132 | *native = l; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | rval.code = RC_OK; |
| 136 | rval.consumed += length; |
| 137 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 138 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)", |
| 139 | (long)rval.consumed, (long)length, td->name, (long)*native); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 140 | |
| 141 | return rval; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Encode the NativeInteger using the standard INTEGER type DER encoder. |
| 146 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 147 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 148 | NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | int tag_mode, ber_tlv_tag_t tag, |
| 150 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 151 | unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 152 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 153 | INTEGER_t tmp; |
| 154 | |
| 155 | #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */ |
| 156 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 157 | tmp.buf = (uint8_t *)&native; |
| 158 | tmp.size = sizeof(native); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 159 | |
| 160 | #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] | 161 | uint8_t buf[sizeof(native)]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 162 | uint8_t *p; |
| 163 | |
Lev Walkin | d54f255 | 2004-10-27 13:24:51 +0000 | [diff] [blame] | 164 | /* Prepare a fake INTEGER */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 165 | for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8) |
Lev Walkin | f3c089e | 2007-11-13 23:53:13 +0000 | [diff] [blame] | 166 | *p = (uint8_t)native; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 167 | |
| 168 | tmp.buf = buf; |
| 169 | tmp.size = sizeof(buf); |
| 170 | #endif /* WORDS_BIGENDIAN */ |
| 171 | |
| 172 | /* Encode fake INTEGER */ |
| 173 | erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key); |
| 174 | if(erval.encoded == -1) { |
| 175 | assert(erval.structure_ptr == &tmp); |
| 176 | erval.structure_ptr = ptr; |
| 177 | } |
| 178 | return erval; |
| 179 | } |
| 180 | |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 181 | /* |
| 182 | * Decode the chunk of XML text encoding INTEGER. |
| 183 | */ |
| 184 | asn_dec_rval_t |
| 185 | NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 186 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 187 | const void *buf_ptr, size_t size) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 188 | const asn_INTEGER_specifics_t *specs = |
| 189 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 190 | asn_dec_rval_t rval; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 191 | INTEGER_t st; |
Lev Walkin | f0b7c9a | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 192 | void *st_ptr = (void *)&st; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 193 | long *native = (long *)*sptr; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 194 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 195 | if(!native) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 196 | native = (long *)(*sptr = CALLOC(1, sizeof(*native))); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 197 | if(!native) ASN__DECODE_FAILED; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 200 | memset(&st, 0, sizeof(st)); |
| 201 | rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr, |
Lev Walkin | f0b7c9a | 2005-01-27 17:54:57 +0000 | [diff] [blame] | 202 | opt_mname, buf_ptr, size); |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 203 | if(rval.code == RC_OK) { |
| 204 | long l; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 205 | if((specs&&specs->field_unsigned) |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 206 | ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */ |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 207 | : asn_INTEGER2long(&st, &l)) { |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 208 | rval.code = RC_FAIL; |
| 209 | rval.consumed = 0; |
| 210 | } else { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 211 | *native = l; |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 212 | } |
| 213 | } else { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 214 | /* |
| 215 | * Cannot restart from the middle; |
| 216 | * there is no place to save state in the native type. |
| 217 | * Request a continuation from the very beginning. |
| 218 | */ |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 219 | rval.consumed = 0; |
| 220 | } |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 221 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st); |
Lev Walkin | 435cb28 | 2004-10-21 14:13:48 +0000 | [diff] [blame] | 222 | return rval; |
| 223 | } |
| 224 | |
| 225 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 226 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 227 | NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 228 | int ilevel, enum xer_encoder_flags_e flags, |
| 229 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 230 | const asn_INTEGER_specifics_t *specs = |
| 231 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 232 | char scratch[32]; /* Enough for 64-bit int */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 233 | asn_enc_rval_t er; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 234 | const long *native = (const long *)sptr; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 235 | |
| 236 | (void)ilevel; |
| 237 | (void)flags; |
| 238 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 239 | if(!native) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 240 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 241 | er.encoded = snprintf(scratch, sizeof(scratch), |
| 242 | (specs && specs->field_unsigned) |
| 243 | ? "%lu" : "%ld", *native); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 244 | if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch) |
| 245 | || cb(scratch, er.encoded, app_key) < 0) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 246 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 247 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 248 | ASN__ENCODED_OK(er); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Lev Walkin | 62d7687 | 2017-08-05 22:49:42 -0700 | [diff] [blame] | 251 | #ifndef ASN_DISABLE_PER_SUPPORT |
| 252 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 253 | asn_dec_rval_t |
| 254 | NativeInteger_decode_uper(asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 255 | asn_TYPE_descriptor_t *td, |
| 256 | const asn_per_constraints_t *constraints, void **sptr, |
| 257 | asn_per_data_t *pd) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 258 | const asn_INTEGER_specifics_t *specs = |
| 259 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 260 | asn_dec_rval_t rval; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 261 | long *native = (long *)*sptr; |
| 262 | INTEGER_t tmpint; |
| 263 | void *tmpintptr = &tmpint; |
| 264 | |
| 265 | (void)opt_codec_ctx; |
| 266 | ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name); |
| 267 | |
| 268 | if(!native) { |
| 269 | native = (long *)(*sptr = CALLOC(1, sizeof(*native))); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 270 | if(!native) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | memset(&tmpint, 0, sizeof tmpint); |
| 274 | rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints, |
| 275 | &tmpintptr, pd); |
Lev Walkin | b2bed82 | 2005-11-27 12:40:43 +0000 | [diff] [blame] | 276 | if(rval.code == RC_OK) { |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 277 | if((specs&&specs->field_unsigned) |
Lev Walkin | aaae9bf | 2010-10-24 16:56:30 -0700 | [diff] [blame] | 278 | ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 279 | : asn_INTEGER2long(&tmpint, native)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 280 | rval.code = RC_FAIL; |
| 281 | else |
| 282 | ASN_DEBUG("NativeInteger %s got value %ld", |
| 283 | td->name, *native); |
Lev Walkin | b2bed82 | 2005-11-27 12:40:43 +0000 | [diff] [blame] | 284 | } |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 285 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 286 | |
| 287 | return rval; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 290 | asn_enc_rval_t |
| 291 | NativeInteger_encode_uper(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 292 | const asn_per_constraints_t *constraints, void *sptr, |
| 293 | asn_per_outp_t *po) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 294 | const asn_INTEGER_specifics_t *specs = |
| 295 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 296 | asn_enc_rval_t er; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 297 | long native; |
| 298 | INTEGER_t tmpint; |
| 299 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 300 | if(!sptr) ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 301 | |
| 302 | native = *(long *)sptr; |
| 303 | |
| 304 | ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native); |
| 305 | |
| 306 | memset(&tmpint, 0, sizeof(tmpint)); |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 307 | if((specs&&specs->field_unsigned) |
| 308 | ? asn_ulong2INTEGER(&tmpint, native) |
| 309 | : asn_long2INTEGER(&tmpint, native)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 310 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 311 | er = INTEGER_encode_uper(td, constraints, &tmpint, po); |
| 312 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint); |
| 313 | return er; |
| 314 | } |
| 315 | |
Lev Walkin | 62d7687 | 2017-08-05 22:49:42 -0700 | [diff] [blame] | 316 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 317 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 318 | /* |
| 319 | * INTEGER specific human-readable output. |
| 320 | */ |
| 321 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 322 | NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 323 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 324 | const asn_INTEGER_specifics_t *specs = |
| 325 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 326 | const long *native = (const long *)sptr; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 327 | char scratch[32]; /* Enough for 64-bit int */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 328 | int ret; |
| 329 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 330 | (void)td; /* Unused argument */ |
| 331 | (void)ilevel; /* Unused argument */ |
| 332 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 333 | if(native) { |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 334 | ret = snprintf(scratch, sizeof(scratch), |
| 335 | (specs && specs->field_unsigned) |
| 336 | ? "%lu" : "%ld", *native); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 337 | assert(ret > 0 && (size_t)ret < sizeof(scratch)); |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 338 | return (cb(scratch, ret, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 339 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 340 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | void |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 345 | NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr, |
| 346 | int contents_only) { |
| 347 | if(!td || !ptr) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 348 | return; |
| 349 | |
| 350 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
| 351 | td->name, contents_only, ptr); |
| 352 | |
| 353 | if(!contents_only) { |
| 354 | FREEMEM(ptr); |
| 355 | } |
| 356 | } |
| 357 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 358 | int |
| 359 | NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) { |
| 360 | (void)td; |
| 361 | |
| 362 | if(aptr && bptr) { |
| 363 | const asn_INTEGER_specifics_t *specs = |
| 364 | (const asn_INTEGER_specifics_t *)td->specifics; |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 365 | if(specs && specs->field_unsigned) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 366 | const unsigned long *a = aptr; |
| 367 | const unsigned long *b = bptr; |
| 368 | if(*a < *b) { |
| 369 | return -1; |
| 370 | } else if(*a > *b) { |
| 371 | return 1; |
| 372 | } else { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 373 | return 0; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 374 | } |
| 375 | } else { |
| 376 | const long *a = aptr; |
| 377 | const long *b = bptr; |
| 378 | if(*a < *b) { |
| 379 | return -1; |
| 380 | } else if(*a > *b) { |
| 381 | return 1; |
| 382 | } else { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 383 | return 0; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } else if(!aptr) { |
| 387 | return -1; |
| 388 | } else { |
| 389 | return 1; |
| 390 | } |
| 391 | } |