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