2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <NativeInteger.h>
10 * Decode the chunk of XML text encoding INTEGER.
13 NativeInteger_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
14 const asn_TYPE_descriptor_t *td, void **sptr,
15 const char *opt_mname, const void *buf_ptr,
17 const asn_INTEGER_specifics_t *specs =
18 (const asn_INTEGER_specifics_t *)td->specifics;
21 void *st_ptr = (void *)&st;
22 long *native = (long *)*sptr;
25 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
26 if(!native) ASN__DECODE_FAILED;
29 memset(&st, 0, sizeof(st));
30 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
31 opt_mname, buf_ptr, size);
32 if(rval.code == RC_OK) {
34 if((specs&&specs->field_unsigned)
35 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
36 : asn_INTEGER2long(&st, &l)) {
44 * Cannot restart from the middle;
45 * there is no place to save state in the native type.
46 * Request a continuation from the very beginning.
50 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
56 NativeInteger_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
57 int ilevel, enum xer_encoder_flags_e flags,
58 asn_app_consume_bytes_f *cb, void *app_key) {
59 const asn_INTEGER_specifics_t *specs =
60 (const asn_INTEGER_specifics_t *)td->specifics;
61 char scratch[32]; /* Enough for 64-bit int */
62 asn_enc_rval_t er = {0,0,0};
63 const long *native = (const long *)sptr;
68 if(!native) ASN__ENCODE_FAILED;
70 er.encoded = snprintf(scratch, sizeof(scratch),
71 (specs && specs->field_unsigned)
72 ? "%lu" : "%ld", *native);
73 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
74 || cb(scratch, er.encoded, app_key) < 0)