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