blob: 2b136185205f68ce6cd32a9705ec998b187a1eff [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;
94 long l;
Lev Walkin8c3b8542005-03-10 18:52:02 +000095 (const uint8_t *)tmp.buf = (const uint8_t *)buf_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000096 tmp.size = length;
97
Lev Walkin5e033762004-09-29 13:26:15 +000098 if(asn_INTEGER2long(&tmp, &l)) {
Lev Walkinf15320b2004-06-03 03:38:44 +000099 rval.code = RC_FAIL;
100 rval.consumed = 0;
101 return rval;
102 }
103
Lev Walkine0b56e02005-02-25 12:10:27 +0000104 *native = l;
Lev Walkinf15320b2004-06-03 03:38:44 +0000105
106 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000107 * Note that native integer size might be other than long.
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 * This expression hopefully will be optimized away
109 * by compiler.
110 */
Lev Walkine0b56e02005-02-25 12:10:27 +0000111 if(sizeof(*native) != sizeof(long) && ((long)*native != l)) {
112 *native = 0; /* Safe value */
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 rval.code = RC_FAIL;
114 rval.consumed = 0;
115 return rval;
116 }
117 }
118
119 rval.code = RC_OK;
120 rval.consumed += length;
121
Lev Walkine0b56e02005-02-25 12:10:27 +0000122 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
123 (long)rval.consumed, (long)length, td->name, (long)*native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000124
125 return rval;
126}
127
128/*
129 * Encode the NativeInteger using the standard INTEGER type DER encoder.
130 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000131asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000132NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 int tag_mode, ber_tlv_tag_t tag,
134 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000135 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000136 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000137 INTEGER_t tmp;
138
139#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
140
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 tmp.buf = (uint8_t *)&native;
142 tmp.size = sizeof(native);
Lev Walkinf15320b2004-06-03 03:38:44 +0000143
144#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
Lev Walkine0b56e02005-02-25 12:10:27 +0000145 uint8_t buf[sizeof(native)];
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 uint8_t *p;
147
Lev Walkind54f2552004-10-27 13:24:51 +0000148 /* Prepare a fake INTEGER */
Lev Walkine0b56e02005-02-25 12:10:27 +0000149 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
150 *p = native;
Lev Walkinf15320b2004-06-03 03:38:44 +0000151
152 tmp.buf = buf;
153 tmp.size = sizeof(buf);
154#endif /* WORDS_BIGENDIAN */
155
156 /* Encode fake INTEGER */
157 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
158 if(erval.encoded == -1) {
159 assert(erval.structure_ptr == &tmp);
160 erval.structure_ptr = ptr;
161 }
162 return erval;
163}
164
Lev Walkin435cb282004-10-21 14:13:48 +0000165/*
166 * Decode the chunk of XML text encoding INTEGER.
167 */
168asn_dec_rval_t
169NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
170 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000171 const void *buf_ptr, size_t size) {
Lev Walkin435cb282004-10-21 14:13:48 +0000172 asn_dec_rval_t rval;
173 INTEGER_t *st = 0;
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000174 void *st_ptr = (void *)&st;
Lev Walkine0b56e02005-02-25 12:10:27 +0000175 long *native = (long *)*sptr;
Lev Walkin435cb282004-10-21 14:13:48 +0000176
Lev Walkine0b56e02005-02-25 12:10:27 +0000177 if(!native) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000178 *sptr = CALLOC(1, sizeof(int));
Lev Walkine0b56e02005-02-25 12:10:27 +0000179 native = (long *)*sptr;
180 if(!native) {
Lev Walkin435cb282004-10-21 14:13:48 +0000181 rval.code = RC_FAIL;
182 rval.consumed = 0;
183 return rval;
184 }
185 }
186
Lev Walkinf0b7c9a2005-01-27 17:54:57 +0000187 rval = INTEGER_decode_xer(opt_codec_ctx, td, (void **)st_ptr,
188 opt_mname, buf_ptr, size);
Lev Walkin435cb282004-10-21 14:13:48 +0000189 if(rval.code == RC_OK) {
190 long l;
191 if(asn_INTEGER2long(st, &l)) {
192 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
Lev Walkine0b56e02005-02-25 12:10:27 +0000197 /* Native type might be shorter than long */
198 if(sizeof(*native) != sizeof(long)
199 && ((long)*native != l)) {
200 *native = 0; /* Safe value */
Lev Walkin435cb282004-10-21 14:13:48 +0000201 rval.code = RC_FAIL;
202 rval.consumed = 0;
203 return rval;
204 }
205 }
206 } else {
Lev Walkine0b56e02005-02-25 12:10:27 +0000207 /*
208 * Cannot restart from the middle;
209 * there is no place to save state in the native type.
210 * Request a continuation from the very beginning.
211 */
Lev Walkin435cb282004-10-21 14:13:48 +0000212 rval.consumed = 0;
213 }
214 asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
215 return rval;
216}
217
218
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000220NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221 int ilevel, enum xer_encoder_flags_e flags,
222 asn_app_consume_bytes_f *cb, void *app_key) {
223 char scratch[32]; /* Enough for 64-bit int */
224 asn_enc_rval_t er;
Lev Walkine0b56e02005-02-25 12:10:27 +0000225 const long *native = (const long *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000226
227 (void)ilevel;
228 (void)flags;
229
Lev Walkine0b56e02005-02-25 12:10:27 +0000230 if(!native) _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000231
Lev Walkine0b56e02005-02-25 12:10:27 +0000232 er.encoded = snprintf(scratch, sizeof(scratch), "%ld", *native);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000233 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
234 || cb(scratch, er.encoded, app_key) < 0)
235 _ASN_ENCODE_FAILED;
236
237 return er;
238}
239
Lev Walkinf15320b2004-06-03 03:38:44 +0000240/*
241 * INTEGER specific human-readable output.
242 */
243int
Lev Walkin5e033762004-09-29 13:26:15 +0000244NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000245 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000246 const long *native = (const long *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000247 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 int ret;
249
Lev Walkind9bd7752004-06-05 08:17:50 +0000250 (void)td; /* Unused argument */
251 (void)ilevel; /* Unused argument */
252
Lev Walkine0b56e02005-02-25 12:10:27 +0000253 if(native) {
254 ret = snprintf(scratch, sizeof(scratch), "%ld", *native);
255 assert(ret > 0 && (size_t)ret < sizeof(scratch));
Lev Walkin8e8078a2004-09-26 13:10:40 +0000256 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000258 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000259 }
260}
261
262void
Lev Walkin5e033762004-09-29 13:26:15 +0000263NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000264
265 if(!td || !ptr)
266 return;
267
268 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
269 td->name, contents_only, ptr);
270
271 if(!contents_only) {
272 FREEMEM(ptr);
273 }
274}
275