blob: 38d6f5c640f4edcbe9632254da6671a9346739cb [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 Walkincd2f48e2017-08-10 02:14:59 -070027 NativeInteger_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000028 asn_generic_no_constraint,
29 NativeInteger_decode_ber,
30 NativeInteger_encode_der,
Lev Walkin435cb282004-10-21 14:13:48 +000031 NativeInteger_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000032 NativeInteger_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070033#ifdef ASN_DISABLE_OER_SUPPORT
34 0,
35 0,
36#else
Lev Walkin9ae018e2017-07-24 01:45:57 +040037 NativeInteger_decode_oer, /* OER decoder */
38 NativeInteger_encode_oer, /* Canonical OER encoder */
Lev Walkincc159472017-07-06 08:26:36 -070039#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040040#ifdef ASN_DISABLE_PER_SUPPORT
41 0,
42 0,
43#else
44 NativeInteger_decode_uper, /* Unaligned PER decoder */
45 NativeInteger_encode_uper, /* Unaligned PER encoder */
46#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000047 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000048 asn_DEF_NativeInteger_tags,
49 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
50 asn_DEF_NativeInteger_tags, /* Same as above */
51 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070052 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000053 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000054 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000055 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000056};
57
58/*
59 * Decode INTEGER type.
60 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000061asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000062NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
63 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000064 void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin8bb57a22007-12-03 13:41:36 +000065 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkine0b56e02005-02-25 12:10:27 +000066 long *native = (long *)*nint_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000067 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000068 ber_tlv_len_t length;
69
70 /*
71 * If the structure is not there, allocate it.
72 */
Lev Walkine0b56e02005-02-25 12:10:27 +000073 if(native == NULL) {
74 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
75 if(native == NULL) {
Lev Walkinf15320b2004-06-03 03:38:44 +000076 rval.code = RC_FAIL;
77 rval.consumed = 0;
78 return rval;
79 }
80 }
81
82 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
83 td->name, tag_mode);
84
85 /*
86 * Check tags.
87 */
Lev Walkin5e033762004-09-29 13:26:15 +000088 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
89 tag_mode, 0, &length, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000090 if(rval.code != RC_OK)
91 return rval;
92
93 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
94
95 /*
96 * Make sure we have this length.
97 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000098 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000099 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +0000100 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 rval.code = RC_WMORE;
102 rval.consumed = 0;
103 return rval;
104 }
105
106 /*
107 * ASN.1 encoded INTEGER: buf_ptr, length
Lev Walkine0b56e02005-02-25 12:10:27 +0000108 * Fill the native, at the same time checking for overflow.
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 * If overflow occured, return with RC_FAIL.
110 */
111 {
112 INTEGER_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +0000113 union {
114 const void *constbuf;
115 void *nonconstbuf;
116 } unconst_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 long l;
Lev Walkin7e033b52005-07-02 08:19:17 +0000118
119 unconst_buf.constbuf = buf_ptr;
120 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 tmp.size = length;
122
Lev Walkin8bb57a22007-12-03 13:41:36 +0000123 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700124 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000125 : asn_INTEGER2long(&tmp, &l)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 rval.code = RC_FAIL;
127 rval.consumed = 0;
128 return rval;
129 }
130
Lev Walkine0b56e02005-02-25 12:10:27 +0000131 *native = l;
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 }
133
134 rval.code = RC_OK;
135 rval.consumed += length;
136
Lev Walkine0b56e02005-02-25 12:10:27 +0000137 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
138 (long)rval.consumed, (long)length, td->name, (long)*native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000139
140 return rval;
141}
142
143/*
144 * Encode the NativeInteger using the standard INTEGER type DER encoder.
145 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000146asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000147NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 int tag_mode, ber_tlv_tag_t tag,
149 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000150 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000151 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 INTEGER_t tmp;
153
154#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
155
Lev Walkine0b56e02005-02-25 12:10:27 +0000156 tmp.buf = (uint8_t *)&native;
157 tmp.size = sizeof(native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000158
159#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
Lev Walkine0b56e02005-02-25 12:10:27 +0000160 uint8_t buf[sizeof(native)];
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 uint8_t *p;
162
Lev Walkind54f2552004-10-27 13:24:51 +0000163 /* Prepare a fake INTEGER */
Lev Walkine0b56e02005-02-25 12:10:27 +0000164 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
Lev Walkinf3c089e2007-11-13 23:53:13 +0000165 *p = (uint8_t)native;
Lev Walkinf15320b2004-06-03 03:38:44 +0000166
167 tmp.buf = buf;
168 tmp.size = sizeof(buf);
169#endif /* WORDS_BIGENDIAN */
170
171 /* Encode fake INTEGER */
172 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
173 if(erval.encoded == -1) {
174 assert(erval.structure_ptr == &tmp);
175 erval.structure_ptr = ptr;
176 }
177 return erval;
178}
179
Lev Walkin435cb282004-10-21 14:13:48 +0000180/*
181 * Decode the chunk of XML text encoding INTEGER.
182 */
183asn_dec_rval_t
184NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
185 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000186 const void *buf_ptr, size_t size) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000187 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin435cb282004-10-21 14:13:48 +0000188 asn_dec_rval_t rval;
Lev Walkin59b176e2005-11-26 11:25:14 +0000189 INTEGER_t st;
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000190 void *st_ptr = (void *)&st;
Lev Walkine0b56e02005-02-25 12:10:27 +0000191 long *native = (long *)*sptr;
Lev Walkin435cb282004-10-21 14:13:48 +0000192
Lev Walkine0b56e02005-02-25 12:10:27 +0000193 if(!native) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000194 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700195 if(!native) ASN__DECODE_FAILED;
Lev Walkin435cb282004-10-21 14:13:48 +0000196 }
197
Lev Walkin59b176e2005-11-26 11:25:14 +0000198 memset(&st, 0, sizeof(st));
199 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000200 opt_mname, buf_ptr, size);
Lev Walkin435cb282004-10-21 14:13:48 +0000201 if(rval.code == RC_OK) {
202 long l;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000203 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700204 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
Lev Walkin8bb57a22007-12-03 13:41:36 +0000205 : asn_INTEGER2long(&st, &l)) {
Lev Walkin435cb282004-10-21 14:13:48 +0000206 rval.code = RC_FAIL;
207 rval.consumed = 0;
208 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000209 *native = l;
Lev Walkin435cb282004-10-21 14:13:48 +0000210 }
211 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000212 /*
213 * Cannot restart from the middle;
214 * there is no place to save state in the native type.
215 * Request a continuation from the very beginning.
216 */
Lev Walkin435cb282004-10-21 14:13:48 +0000217 rval.consumed = 0;
218 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000219 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
Lev Walkin435cb282004-10-21 14:13:48 +0000220 return rval;
221}
222
223
Lev Walkina9cc46e2004-09-22 16:06:28 +0000224asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000225NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000226 int ilevel, enum xer_encoder_flags_e flags,
227 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000228 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000229 char scratch[32]; /* Enough for 64-bit int */
230 asn_enc_rval_t er;
Lev Walkine0b56e02005-02-25 12:10:27 +0000231 const long *native = (const long *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000232
233 (void)ilevel;
234 (void)flags;
235
Lev Walkin7c1dc052016-03-14 03:08:15 -0700236 if(!native) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000237
Lev Walkin8bb57a22007-12-03 13:41:36 +0000238 er.encoded = snprintf(scratch, sizeof(scratch),
239 (specs && specs->field_unsigned)
240 ? "%lu" : "%ld", *native);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000241 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
242 || cb(scratch, er.encoded, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700243 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000244
Lev Walkin7c1dc052016-03-14 03:08:15 -0700245 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000246}
247
Lev Walkin62d76872017-08-05 22:49:42 -0700248#ifndef ASN_DISABLE_PER_SUPPORT
249
Lev Walkin59b176e2005-11-26 11:25:14 +0000250asn_dec_rval_t
251NativeInteger_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -0700252 asn_TYPE_descriptor_t *td,
253 const asn_per_constraints_t *constraints, void **sptr,
254 asn_per_data_t *pd) {
255 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000256 asn_dec_rval_t rval;
257 long *native = (long *)*sptr;
258 INTEGER_t tmpint;
259 void *tmpintptr = &tmpint;
260
261 (void)opt_codec_ctx;
262 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
263
264 if(!native) {
265 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700266 if(!native) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000267 }
268
269 memset(&tmpint, 0, sizeof tmpint);
270 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
271 &tmpintptr, pd);
Lev Walkinb2bed822005-11-27 12:40:43 +0000272 if(rval.code == RC_OK) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000273 if((specs&&specs->field_unsigned)
Lev Walkinaaae9bf2010-10-24 16:56:30 -0700274 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
Lev Walkin8bb57a22007-12-03 13:41:36 +0000275 : asn_INTEGER2long(&tmpint, native))
Lev Walkin59b176e2005-11-26 11:25:14 +0000276 rval.code = RC_FAIL;
277 else
278 ASN_DEBUG("NativeInteger %s got value %ld",
279 td->name, *native);
Lev Walkinb2bed822005-11-27 12:40:43 +0000280 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000281 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
Lev Walkin59b176e2005-11-26 11:25:14 +0000282
283 return rval;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000284}
285
Lev Walkin523de9e2006-08-18 01:34:18 +0000286asn_enc_rval_t
287NativeInteger_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700288 const asn_per_constraints_t *constraints, void *sptr,
289 asn_per_outp_t *po) {
290 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000291 asn_enc_rval_t er;
292 long native;
293 INTEGER_t tmpint;
294
Lev Walkin7c1dc052016-03-14 03:08:15 -0700295 if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000296
297 native = *(long *)sptr;
298
299 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
300
301 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin8bb57a22007-12-03 13:41:36 +0000302 if((specs&&specs->field_unsigned)
303 ? asn_ulong2INTEGER(&tmpint, native)
304 : asn_long2INTEGER(&tmpint, native))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700305 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000306 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
307 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
308 return er;
309}
310
Lev Walkin62d76872017-08-05 22:49:42 -0700311#endif /* ASN_DISABLE_PER_SUPPORT */
312
Lev Walkinf15320b2004-06-03 03:38:44 +0000313/*
314 * INTEGER specific human-readable output.
315 */
316int
Lev Walkin5e033762004-09-29 13:26:15 +0000317NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000318 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000319 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkine0b56e02005-02-25 12:10:27 +0000320 const long *native = (const long *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000321 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 int ret;
323
Lev Walkind9bd7752004-06-05 08:17:50 +0000324 (void)td; /* Unused argument */
325 (void)ilevel; /* Unused argument */
326
Lev Walkine0b56e02005-02-25 12:10:27 +0000327 if(native) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000328 ret = snprintf(scratch, sizeof(scratch),
329 (specs && specs->field_unsigned)
330 ? "%lu" : "%ld", *native);
Lev Walkine0b56e02005-02-25 12:10:27 +0000331 assert(ret > 0 && (size_t)ret < sizeof(scratch));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000332 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000333 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000334 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 }
336}
337
338void
Lev Walkin5e033762004-09-29 13:26:15 +0000339NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000340
341 if(!td || !ptr)
342 return;
343
344 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
345 td->name, contents_only, ptr);
346
347 if(!contents_only) {
348 FREEMEM(ptr);
349 }
350}
351
Lev Walkincd2f48e2017-08-10 02:14:59 -0700352int
353NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
354 (void)td;
355
356 if(aptr && bptr) {
357 const asn_INTEGER_specifics_t *specs =
358 (const asn_INTEGER_specifics_t *)td->specifics;
359 if(specs->field_unsigned) {
360 const unsigned long *a = aptr;
361 const unsigned long *b = bptr;
362 if(*a < *b) {
363 return -1;
364 } else if(*a > *b) {
365 return 1;
366 } else {
367 return 1;
368 }
369 } else {
370 const long *a = aptr;
371 const long *b = bptr;
372 if(*a < *b) {
373 return -1;
374 } else if(*a > *b) {
375 return 1;
376 } else {
377 return 1;
378 }
379 }
380 } else if(!aptr) {
381 return -1;
382 } else {
383 return 1;
384 }
385}