blob: ef17bee31ce9c5eb5e93c6af137570ffa307d55c [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 */
Harald Welte498c9712015-08-30 16:33:07 +020034 NativeInteger_decode_aper, /* Aligned PER decoder */
35 NativeInteger_encode_aper, /* Aligned PER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000036 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000037 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]),
Lev Walkin59b176e2005-11-26 11:25:14 +000041 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000042 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000043 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000044};
45
46/*
47 * Decode INTEGER type.
48 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000049asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000050NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
51 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000052 void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin8bb57a22007-12-03 13:41:36 +000053 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkine0b56e02005-02-25 12:10:27 +000054 long *native = (long *)*nint_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000055 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000056 ber_tlv_len_t length;
57
58 /*
59 * If the structure is not there, allocate it.
60 */
Lev Walkine0b56e02005-02-25 12:10:27 +000061 if(native == NULL) {
62 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
63 if(native == NULL) {
Lev Walkinf15320b2004-06-03 03:38:44 +000064 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 */
Lev Walkin5e033762004-09-29 13:26:15 +000076 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
77 tag_mode, 0, &length, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000078 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 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000086 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000087 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +000088 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000089 rval.code = RC_WMORE;
90 rval.consumed = 0;
91 return rval;
92 }
93
94 /*
95 * ASN.1 encoded INTEGER: buf_ptr, length
Lev Walkine0b56e02005-02-25 12:10:27 +000096 * Fill the native, at the same time checking for overflow.
Lev Walkinf15320b2004-06-03 03:38:44 +000097 * If overflow occured, return with RC_FAIL.
98 */
99 {
100 INTEGER_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +0000101 union {
102 const void *constbuf;
103 void *nonconstbuf;
104 } unconst_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 long l;
Lev Walkin7e033b52005-07-02 08:19:17 +0000106
107 unconst_buf.constbuf = buf_ptr;
108 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 tmp.size = length;
110
Lev Walkin8bb57a22007-12-03 13:41:36 +0000111 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700112 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000113 : asn_INTEGER2long(&tmp, &l)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 rval.code = RC_FAIL;
115 rval.consumed = 0;
116 return rval;
117 }
118
Lev Walkine0b56e02005-02-25 12:10:27 +0000119 *native = l;
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 }
121
122 rval.code = RC_OK;
123 rval.consumed += length;
124
Lev Walkine0b56e02005-02-25 12:10:27 +0000125 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
126 (long)rval.consumed, (long)length, td->name, (long)*native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000127
128 return rval;
129}
130
131/*
132 * Encode the NativeInteger using the standard INTEGER type DER encoder.
133 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000134asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000135NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 int tag_mode, ber_tlv_tag_t tag,
137 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000138 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000139 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 INTEGER_t tmp;
141
142#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
143
Lev Walkine0b56e02005-02-25 12:10:27 +0000144 tmp.buf = (uint8_t *)&native;
145 tmp.size = sizeof(native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000146
147#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
Lev Walkine0b56e02005-02-25 12:10:27 +0000148 uint8_t buf[sizeof(native)];
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 uint8_t *p;
150
Lev Walkind54f2552004-10-27 13:24:51 +0000151 /* Prepare a fake INTEGER */
Lev Walkine0b56e02005-02-25 12:10:27 +0000152 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
Lev Walkinf3c089e2007-11-13 23:53:13 +0000153 *p = (uint8_t)native;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154
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
Lev Walkin435cb282004-10-21 14:13:48 +0000168/*
169 * Decode the chunk of XML text encoding INTEGER.
170 */
171asn_dec_rval_t
172NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
173 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000174 const void *buf_ptr, size_t size) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000175 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin435cb282004-10-21 14:13:48 +0000176 asn_dec_rval_t rval;
Lev Walkin59b176e2005-11-26 11:25:14 +0000177 INTEGER_t st;
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000178 void *st_ptr = (void *)&st;
Lev Walkine0b56e02005-02-25 12:10:27 +0000179 long *native = (long *)*sptr;
Lev Walkin435cb282004-10-21 14:13:48 +0000180
Lev Walkine0b56e02005-02-25 12:10:27 +0000181 if(!native) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000182 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
183 if(!native) _ASN_DECODE_FAILED;
Lev Walkin435cb282004-10-21 14:13:48 +0000184 }
185
Lev Walkin59b176e2005-11-26 11:25:14 +0000186 memset(&st, 0, sizeof(st));
187 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000188 opt_mname, buf_ptr, size);
Lev Walkin435cb282004-10-21 14:13:48 +0000189 if(rval.code == RC_OK) {
190 long l;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000191 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700192 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000193 : asn_INTEGER2long(&st, &l)) {
Lev Walkin435cb282004-10-21 14:13:48 +0000194 rval.code = RC_FAIL;
195 rval.consumed = 0;
196 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000197 *native = l;
Lev Walkin435cb282004-10-21 14:13:48 +0000198 }
199 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000200 /*
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 */
Lev Walkin435cb282004-10-21 14:13:48 +0000205 rval.consumed = 0;
206 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000207 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
Lev Walkin435cb282004-10-21 14:13:48 +0000208 return rval;
209}
210
211
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000213NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214 int ilevel, enum xer_encoder_flags_e flags,
215 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000216 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000217 char scratch[32]; /* Enough for 64-bit int */
218 asn_enc_rval_t er;
Lev Walkine0b56e02005-02-25 12:10:27 +0000219 const long *native = (const long *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000220
221 (void)ilevel;
222 (void)flags;
223
Lev Walkine0b56e02005-02-25 12:10:27 +0000224 if(!native) _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000225
Lev Walkin8bb57a22007-12-03 13:41:36 +0000226 er.encoded = snprintf(scratch, sizeof(scratch),
227 (specs && specs->field_unsigned)
228 ? "%lu" : "%ld", *native);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000229 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
230 || cb(scratch, er.encoded, app_key) < 0)
231 _ASN_ENCODE_FAILED;
232
Lev Walkin59b176e2005-11-26 11:25:14 +0000233 _ASN_ENCODED_OK(er);
234}
235
236asn_dec_rval_t
237NativeInteger_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
Lev Walkin8bb57a22007-12-03 13:41:36 +0000241 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000242 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);
Lev Walkinb2bed822005-11-27 12:40:43 +0000258 if(rval.code == RC_OK) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000259 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700260 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
Lev Walkin8bb57a22007-12-03 13:41:36 +0000261 : asn_INTEGER2long(&tmpint, native))
Lev Walkin59b176e2005-11-26 11:25:14 +0000262 rval.code = RC_FAIL;
263 else
264 ASN_DEBUG("NativeInteger %s got value %ld",
265 td->name, *native);
Lev Walkinb2bed822005-11-27 12:40:43 +0000266 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000267 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
Lev Walkin59b176e2005-11-26 11:25:14 +0000268
269 return rval;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000270}
271
Harald Welte498c9712015-08-30 16:33:07 +0200272asn_dec_rval_t
273NativeInteger_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
Lev Walkin523de9e2006-08-18 01:34:18 +0000308asn_enc_rval_t
309NativeInteger_encode_uper(asn_TYPE_descriptor_t *td,
310 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000311 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000312 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));
Lev Walkin8bb57a22007-12-03 13:41:36 +0000323 if((specs&&specs->field_unsigned)
324 ? asn_ulong2INTEGER(&tmpint, native)
325 : asn_long2INTEGER(&tmpint, native))
Lev Walkin523de9e2006-08-18 01:34:18 +0000326 _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
Harald Welte498c9712015-08-30 16:33:07 +0200332asn_enc_rval_t
333NativeInteger_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
Lev Walkinf15320b2004-06-03 03:38:44 +0000358/*
359 * INTEGER specific human-readable output.
360 */
361int
Lev Walkin5e033762004-09-29 13:26:15 +0000362NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000363 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000364 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkine0b56e02005-02-25 12:10:27 +0000365 const long *native = (const long *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000366 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 int ret;
368
Lev Walkind9bd7752004-06-05 08:17:50 +0000369 (void)td; /* Unused argument */
370 (void)ilevel; /* Unused argument */
371
Lev Walkine0b56e02005-02-25 12:10:27 +0000372 if(native) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000373 ret = snprintf(scratch, sizeof(scratch),
374 (specs && specs->field_unsigned)
375 ? "%lu" : "%ld", *native);
Lev Walkine0b56e02005-02-25 12:10:27 +0000376 assert(ret > 0 && (size_t)ret < sizeof(scratch));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000377 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000378 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000379 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000380 }
381}
382
383void
Lev Walkin5e033762004-09-29 13:26:15 +0000384NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000385
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