blob: 96c787569a9864f82555fea8d13f581a864ff764 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkinfc776432005-03-29 17:21:14 +00002 * Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Please read the NativeInteger.h for the explanation wrt. differences between
7 * INTEGER and NativeInteger.
8 * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
9 * implementation deals with the standard (machine-specific) representation
10 * of them instead of using the platform-independent buffer.
11 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000012#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000013#include <NativeInteger.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000014
15/*
16 * NativeInteger basic type description.
17 */
Lev Walkin5e033762004-09-29 13:26:15 +000018static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000019 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
20};
Lev Walkin5e033762004-09-29 13:26:15 +000021asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
Lev Walkinf15320b2004-06-03 03:38:44 +000022 "INTEGER", /* The ASN.1 type is still INTEGER */
Lev Walkindc06f6b2004-10-20 15:50:55 +000023 "INTEGER",
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 NativeInteger_free,
25 NativeInteger_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 asn_generic_no_constraint,
27 NativeInteger_decode_ber,
28 NativeInteger_encode_der,
Lev Walkin435cb282004-10-21 14:13:48 +000029 NativeInteger_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000030 NativeInteger_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000031 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000032 asn_DEF_NativeInteger_tags,
33 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
34 asn_DEF_NativeInteger_tags, /* Same as above */
35 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000036 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000037 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000038};
39
40/*
41 * Decode INTEGER type.
42 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000043asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000044NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
45 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000046 void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkine0b56e02005-02-25 12:10:27 +000047 long *native = (long *)*nint_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000048 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000049 ber_tlv_len_t length;
50
51 /*
52 * If the structure is not there, allocate it.
53 */
Lev Walkine0b56e02005-02-25 12:10:27 +000054 if(native == NULL) {
55 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
56 if(native == NULL) {
Lev Walkinf15320b2004-06-03 03:38:44 +000057 rval.code = RC_FAIL;
58 rval.consumed = 0;
59 return rval;
60 }
61 }
62
63 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
64 td->name, tag_mode);
65
66 /*
67 * Check tags.
68 */
Lev Walkin5e033762004-09-29 13:26:15 +000069 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
70 tag_mode, 0, &length, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000071 if(rval.code != RC_OK)
72 return rval;
73
74 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
75
76 /*
77 * Make sure we have this length.
78 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000079 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000080 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +000081 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000082 rval.code = RC_WMORE;
83 rval.consumed = 0;
84 return rval;
85 }
86
87 /*
88 * ASN.1 encoded INTEGER: buf_ptr, length
Lev Walkine0b56e02005-02-25 12:10:27 +000089 * Fill the native, at the same time checking for overflow.
Lev Walkinf15320b2004-06-03 03:38:44 +000090 * If overflow occured, return with RC_FAIL.
91 */
92 {
93 INTEGER_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +000094 union {
95 const void *constbuf;
96 void *nonconstbuf;
97 } unconst_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +000098 long l;
Lev Walkin7e033b52005-07-02 08:19:17 +000099
100 unconst_buf.constbuf = buf_ptr;
101 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 tmp.size = length;
103
Lev Walkin5e033762004-09-29 13:26:15 +0000104 if(asn_INTEGER2long(&tmp, &l)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 rval.code = RC_FAIL;
106 rval.consumed = 0;
107 return rval;
108 }
109
Lev Walkine0b56e02005-02-25 12:10:27 +0000110 *native = l;
Lev Walkinf15320b2004-06-03 03:38:44 +0000111
112 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000113 * Note that native integer size might be other than long.
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 * This expression hopefully will be optimized away
115 * by compiler.
116 */
Lev Walkine0b56e02005-02-25 12:10:27 +0000117 if(sizeof(*native) != sizeof(long) && ((long)*native != l)) {
118 *native = 0; /* Safe value */
Lev Walkinf15320b2004-06-03 03:38:44 +0000119 rval.code = RC_FAIL;
120 rval.consumed = 0;
121 return rval;
122 }
123 }
124
125 rval.code = RC_OK;
126 rval.consumed += length;
127
Lev Walkine0b56e02005-02-25 12:10:27 +0000128 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
129 (long)rval.consumed, (long)length, td->name, (long)*native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000130
131 return rval;
132}
133
134/*
135 * Encode the NativeInteger using the standard INTEGER type DER encoder.
136 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000137asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000138NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000139 int tag_mode, ber_tlv_tag_t tag,
140 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000142 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 INTEGER_t tmp;
144
145#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
146
Lev Walkine0b56e02005-02-25 12:10:27 +0000147 tmp.buf = (uint8_t *)&native;
148 tmp.size = sizeof(native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000149
150#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
Lev Walkine0b56e02005-02-25 12:10:27 +0000151 uint8_t buf[sizeof(native)];
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 uint8_t *p;
153
Lev Walkind54f2552004-10-27 13:24:51 +0000154 /* Prepare a fake INTEGER */
Lev Walkine0b56e02005-02-25 12:10:27 +0000155 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
156 *p = native;
Lev Walkinf15320b2004-06-03 03:38:44 +0000157
158 tmp.buf = buf;
159 tmp.size = sizeof(buf);
160#endif /* WORDS_BIGENDIAN */
161
162 /* Encode fake INTEGER */
163 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
164 if(erval.encoded == -1) {
165 assert(erval.structure_ptr == &tmp);
166 erval.structure_ptr = ptr;
167 }
168 return erval;
169}
170
Lev Walkin435cb282004-10-21 14:13:48 +0000171/*
172 * Decode the chunk of XML text encoding INTEGER.
173 */
174asn_dec_rval_t
175NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
176 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000177 const void *buf_ptr, size_t size) {
Lev Walkin435cb282004-10-21 14:13:48 +0000178 asn_dec_rval_t rval;
179 INTEGER_t *st = 0;
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000180 void *st_ptr = (void *)&st;
Lev Walkine0b56e02005-02-25 12:10:27 +0000181 long *native = (long *)*sptr;
Lev Walkin435cb282004-10-21 14:13:48 +0000182
Lev Walkine0b56e02005-02-25 12:10:27 +0000183 if(!native) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000184 *sptr = CALLOC(1, sizeof(int));
Lev Walkine0b56e02005-02-25 12:10:27 +0000185 native = (long *)*sptr;
186 if(!native) {
Lev Walkin435cb282004-10-21 14:13:48 +0000187 rval.code = RC_FAIL;
188 rval.consumed = 0;
189 return rval;
190 }
191 }
192
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000193 rval = INTEGER_decode_xer(opt_codec_ctx, td, (void **)st_ptr,
194 opt_mname, buf_ptr, size);
Lev Walkin435cb282004-10-21 14:13:48 +0000195 if(rval.code == RC_OK) {
196 long l;
197 if(asn_INTEGER2long(st, &l)) {
198 rval.code = RC_FAIL;
199 rval.consumed = 0;
200 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000201 *native = l;
Lev Walkin435cb282004-10-21 14:13:48 +0000202
Lev Walkine0b56e02005-02-25 12:10:27 +0000203 /* Native type might be shorter than long */
204 if(sizeof(*native) != sizeof(long)
205 && ((long)*native != l)) {
206 *native = 0; /* Safe value */
Lev Walkin435cb282004-10-21 14:13:48 +0000207 rval.code = RC_FAIL;
208 rval.consumed = 0;
209 return rval;
210 }
211 }
212 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000213 /*
214 * Cannot restart from the middle;
215 * there is no place to save state in the native type.
216 * Request a continuation from the very beginning.
217 */
Lev Walkin435cb282004-10-21 14:13:48 +0000218 rval.consumed = 0;
219 }
220 asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
221 return rval;
222}
223
224
Lev Walkina9cc46e2004-09-22 16:06:28 +0000225asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000226NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000227 int ilevel, enum xer_encoder_flags_e flags,
228 asn_app_consume_bytes_f *cb, void *app_key) {
229 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 Walkine0b56e02005-02-25 12:10:27 +0000236 if(!native) _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000237
Lev Walkine0b56e02005-02-25 12:10:27 +0000238 er.encoded = snprintf(scratch, sizeof(scratch), "%ld", *native);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000239 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
240 || cb(scratch, er.encoded, app_key) < 0)
241 _ASN_ENCODE_FAILED;
242
243 return er;
244}
245
Lev Walkinf15320b2004-06-03 03:38:44 +0000246/*
247 * INTEGER specific human-readable output.
248 */
249int
Lev Walkin5e033762004-09-29 13:26:15 +0000250NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000252 const long *native = (const long *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000253 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 int ret;
255
Lev Walkind9bd7752004-06-05 08:17:50 +0000256 (void)td; /* Unused argument */
257 (void)ilevel; /* Unused argument */
258
Lev Walkine0b56e02005-02-25 12:10:27 +0000259 if(native) {
260 ret = snprintf(scratch, sizeof(scratch), "%ld", *native);
261 assert(ret > 0 && (size_t)ret < sizeof(scratch));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000262 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000263 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000264 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 }
266}
267
268void
Lev Walkin5e033762004-09-29 13:26:15 +0000269NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000270
271 if(!td || !ptr)
272 return;
273
274 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
275 td->name, contents_only, ptr);
276
277 if(!contents_only) {
278 FREEMEM(ptr);
279 }
280}
281