blob: e8ce6d2c33c1d8ad458fadf9499314a50d1979b6 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin523de9e2006-08-18 01:34:18 +00002 * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
6/*
Lev Walkin41635d32006-03-18 05:06:57 +00007 * Read the NativeInteger.h for the explanation wrt. differences between
Lev Walkinf15320b2004-06-03 03:38:44 +00008 * 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 Walkina9cc46e2004-09-22 16:06:28 +000013#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000014#include <NativeInteger.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * NativeInteger basic type description.
18 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070019static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000020 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
21};
Lev Walkin5e033762004-09-29 13:26:15 +000022asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
Lev Walkinf15320b2004-06-03 03:38:44 +000023 "INTEGER", /* The ASN.1 type is still INTEGER */
Lev Walkindc06f6b2004-10-20 15:50:55 +000024 "INTEGER",
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 NativeInteger_free,
26 NativeInteger_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 asn_generic_no_constraint,
28 NativeInteger_decode_ber,
29 NativeInteger_encode_der,
Lev Walkin435cb282004-10-21 14:13:48 +000030 NativeInteger_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000031 NativeInteger_encode_xer,
Lev Walkin59b176e2005-11-26 11:25:14 +000032 NativeInteger_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000033 NativeInteger_encode_uper, /* Unaligned PER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000034 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000035 asn_DEF_NativeInteger_tags,
36 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
37 asn_DEF_NativeInteger_tags, /* Same as above */
38 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000039 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000040 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000041 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000042};
43
44/*
45 * Decode INTEGER type.
46 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000047asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000048NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
49 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000050 void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin8bb57a22007-12-03 13:41:36 +000051 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkine0b56e02005-02-25 12:10:27 +000052 long *native = (long *)*nint_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000053 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000054 ber_tlv_len_t length;
55
56 /*
57 * If the structure is not there, allocate it.
58 */
Lev Walkine0b56e02005-02-25 12:10:27 +000059 if(native == NULL) {
60 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
61 if(native == NULL) {
Lev Walkinf15320b2004-06-03 03:38:44 +000062 rval.code = RC_FAIL;
63 rval.consumed = 0;
64 return rval;
65 }
66 }
67
68 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
69 td->name, tag_mode);
70
71 /*
72 * Check tags.
73 */
Lev Walkin5e033762004-09-29 13:26:15 +000074 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
75 tag_mode, 0, &length, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000076 if(rval.code != RC_OK)
77 return rval;
78
79 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
80
81 /*
82 * Make sure we have this length.
83 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000084 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000085 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +000086 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000087 rval.code = RC_WMORE;
88 rval.consumed = 0;
89 return rval;
90 }
91
92 /*
93 * ASN.1 encoded INTEGER: buf_ptr, length
Lev Walkine0b56e02005-02-25 12:10:27 +000094 * Fill the native, at the same time checking for overflow.
Lev Walkinf15320b2004-06-03 03:38:44 +000095 * If overflow occured, return with RC_FAIL.
96 */
97 {
98 INTEGER_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +000099 union {
100 const void *constbuf;
101 void *nonconstbuf;
102 } unconst_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 long l;
Lev Walkin7e033b52005-07-02 08:19:17 +0000104
105 unconst_buf.constbuf = buf_ptr;
106 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 tmp.size = length;
108
Lev Walkin8bb57a22007-12-03 13:41:36 +0000109 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700110 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000111 : asn_INTEGER2long(&tmp, &l)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 rval.code = RC_FAIL;
113 rval.consumed = 0;
114 return rval;
115 }
116
Lev Walkine0b56e02005-02-25 12:10:27 +0000117 *native = l;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 }
119
120 rval.code = RC_OK;
121 rval.consumed += length;
122
Lev Walkine0b56e02005-02-25 12:10:27 +0000123 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
124 (long)rval.consumed, (long)length, td->name, (long)*native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000125
126 return rval;
127}
128
129/*
130 * Encode the NativeInteger using the standard INTEGER type DER encoder.
131 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000132asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000133NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 int tag_mode, ber_tlv_tag_t tag,
135 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000136 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000137 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 INTEGER_t tmp;
139
140#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
141
Lev Walkine0b56e02005-02-25 12:10:27 +0000142 tmp.buf = (uint8_t *)&native;
143 tmp.size = sizeof(native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000144
145#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
Lev Walkine0b56e02005-02-25 12:10:27 +0000146 uint8_t buf[sizeof(native)];
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 uint8_t *p;
148
Lev Walkind54f2552004-10-27 13:24:51 +0000149 /* Prepare a fake INTEGER */
Lev Walkine0b56e02005-02-25 12:10:27 +0000150 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
Lev Walkinf3c089e2007-11-13 23:53:13 +0000151 *p = (uint8_t)native;
Lev Walkinf15320b2004-06-03 03:38:44 +0000152
153 tmp.buf = buf;
154 tmp.size = sizeof(buf);
155#endif /* WORDS_BIGENDIAN */
156
157 /* Encode fake INTEGER */
158 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
159 if(erval.encoded == -1) {
160 assert(erval.structure_ptr == &tmp);
161 erval.structure_ptr = ptr;
162 }
163 return erval;
164}
165
Lev Walkin435cb282004-10-21 14:13:48 +0000166/*
167 * Decode the chunk of XML text encoding INTEGER.
168 */
169asn_dec_rval_t
170NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
171 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000172 const void *buf_ptr, size_t size) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000173 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin435cb282004-10-21 14:13:48 +0000174 asn_dec_rval_t rval;
Lev Walkin59b176e2005-11-26 11:25:14 +0000175 INTEGER_t st;
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000176 void *st_ptr = (void *)&st;
Lev Walkine0b56e02005-02-25 12:10:27 +0000177 long *native = (long *)*sptr;
Lev Walkin435cb282004-10-21 14:13:48 +0000178
Lev Walkine0b56e02005-02-25 12:10:27 +0000179 if(!native) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000180 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700181 if(!native) ASN__DECODE_FAILED;
Lev Walkin435cb282004-10-21 14:13:48 +0000182 }
183
Lev Walkin59b176e2005-11-26 11:25:14 +0000184 memset(&st, 0, sizeof(st));
185 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000186 opt_mname, buf_ptr, size);
Lev Walkin435cb282004-10-21 14:13:48 +0000187 if(rval.code == RC_OK) {
188 long l;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000189 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700190 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000191 : asn_INTEGER2long(&st, &l)) {
Lev Walkin435cb282004-10-21 14:13:48 +0000192 rval.code = RC_FAIL;
193 rval.consumed = 0;
194 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000195 *native = l;
Lev Walkin435cb282004-10-21 14:13:48 +0000196 }
197 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000198 /*
199 * Cannot restart from the middle;
200 * there is no place to save state in the native type.
201 * Request a continuation from the very beginning.
202 */
Lev Walkin435cb282004-10-21 14:13:48 +0000203 rval.consumed = 0;
204 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000205 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
Lev Walkin435cb282004-10-21 14:13:48 +0000206 return rval;
207}
208
209
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000211NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212 int ilevel, enum xer_encoder_flags_e flags,
213 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000214 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000215 char scratch[32]; /* Enough for 64-bit int */
216 asn_enc_rval_t er;
Lev Walkine0b56e02005-02-25 12:10:27 +0000217 const long *native = (const long *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000218
219 (void)ilevel;
220 (void)flags;
221
Lev Walkin7c1dc052016-03-14 03:08:15 -0700222 if(!native) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000223
Lev Walkin8bb57a22007-12-03 13:41:36 +0000224 er.encoded = snprintf(scratch, sizeof(scratch),
225 (specs && specs->field_unsigned)
226 ? "%lu" : "%ld", *native);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000227 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
228 || cb(scratch, er.encoded, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700229 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000230
Lev Walkin7c1dc052016-03-14 03:08:15 -0700231 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000232}
233
234asn_dec_rval_t
235NativeInteger_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
236 asn_TYPE_descriptor_t *td,
237 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
238
Lev Walkin8bb57a22007-12-03 13:41:36 +0000239 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000240 asn_dec_rval_t rval;
241 long *native = (long *)*sptr;
242 INTEGER_t tmpint;
243 void *tmpintptr = &tmpint;
244
245 (void)opt_codec_ctx;
246 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
247
248 if(!native) {
249 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700250 if(!native) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000251 }
252
253 memset(&tmpint, 0, sizeof tmpint);
254 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
255 &tmpintptr, pd);
Lev Walkinb2bed822005-11-27 12:40:43 +0000256 if(rval.code == RC_OK) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000257 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700258 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
Lev Walkin8bb57a22007-12-03 13:41:36 +0000259 : asn_INTEGER2long(&tmpint, native))
Lev Walkin59b176e2005-11-26 11:25:14 +0000260 rval.code = RC_FAIL;
261 else
262 ASN_DEBUG("NativeInteger %s got value %ld",
263 td->name, *native);
Lev Walkinb2bed822005-11-27 12:40:43 +0000264 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000265 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
Lev Walkin59b176e2005-11-26 11:25:14 +0000266
267 return rval;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000268}
269
Lev Walkin523de9e2006-08-18 01:34:18 +0000270asn_enc_rval_t
271NativeInteger_encode_uper(asn_TYPE_descriptor_t *td,
272 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000273 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000274 asn_enc_rval_t er;
275 long native;
276 INTEGER_t tmpint;
277
Lev Walkin7c1dc052016-03-14 03:08:15 -0700278 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000279
280 native = *(long *)sptr;
281
282 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
283
284 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin8bb57a22007-12-03 13:41:36 +0000285 if((specs&&specs->field_unsigned)
286 ? asn_ulong2INTEGER(&tmpint, native)
287 : asn_long2INTEGER(&tmpint, native))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700288 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000289 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
290 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
291 return er;
292}
293
Lev Walkinf15320b2004-06-03 03:38:44 +0000294/*
295 * INTEGER specific human-readable output.
296 */
297int
Lev Walkin5e033762004-09-29 13:26:15 +0000298NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000299 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000300 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkine0b56e02005-02-25 12:10:27 +0000301 const long *native = (const long *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000302 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 int ret;
304
Lev Walkind9bd7752004-06-05 08:17:50 +0000305 (void)td; /* Unused argument */
306 (void)ilevel; /* Unused argument */
307
Lev Walkine0b56e02005-02-25 12:10:27 +0000308 if(native) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000309 ret = snprintf(scratch, sizeof(scratch),
310 (specs && specs->field_unsigned)
311 ? "%lu" : "%ld", *native);
Lev Walkine0b56e02005-02-25 12:10:27 +0000312 assert(ret > 0 && (size_t)ret < sizeof(scratch));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000313 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000315 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000316 }
317}
318
319void
Lev Walkin5e033762004-09-29 13:26:15 +0000320NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000321
322 if(!td || !ptr)
323 return;
324
325 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
326 td->name, contents_only, ptr);
327
328 if(!contents_only) {
329 FREEMEM(ptr);
330 }
331}
332