SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / NativeInteger_xer.c
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <NativeInteger.h>
8
9 /*
10  * Decode the chunk of XML text encoding INTEGER.
11  */
12 asn_dec_rval_t
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,
16                          size_t size) {
17     const asn_INTEGER_specifics_t *specs =
18         (const asn_INTEGER_specifics_t *)td->specifics;
19     asn_dec_rval_t rval;
20     INTEGER_t st;
21     void *st_ptr = (void *)&st;
22     long *native = (long *)*sptr;
23
24     if(!native) {
25         native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
26         if(!native) ASN__DECODE_FAILED;
27     }
28
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) {
33         long l;
34         if((specs&&specs->field_unsigned)
35             ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
36             : asn_INTEGER2long(&st, &l)) {
37             rval.code = RC_FAIL;
38             rval.consumed = 0;
39         } else {
40             *native = l;
41         }
42     } else {
43         /*
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.
47          */
48         rval.consumed = 0;
49     }
50     ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
51     return rval;
52 }
53
54
55 asn_enc_rval_t
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;
64
65     (void)ilevel;
66     (void)flags;
67
68     if(!native) ASN__ENCODE_FAILED;
69
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)
75         ASN__ENCODE_FAILED;
76
77     ASN__ENCODED_OK(er);
78 }