blob: 96c787569a9864f82555fea8d13f581a864ff764 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
vlm5ec2fe12005-03-29 17:21:14 +00002 * Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
vlmfa67ddc2004-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 */
vlm39ba4c42004-09-22 16:06:28 +000012#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +000013#include <NativeInteger.h>
vlmfa67ddc2004-06-03 03:38:44 +000014
15/*
16 * NativeInteger basic type description.
17 */
vlmef6355b2004-09-29 13:26:15 +000018static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
vlmfa67ddc2004-06-03 03:38:44 +000019 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
20};
vlmef6355b2004-09-29 13:26:15 +000021asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
vlmfa67ddc2004-06-03 03:38:44 +000022 "INTEGER", /* The ASN.1 type is still INTEGER */
vlm9de248e2004-10-20 15:50:55 +000023 "INTEGER",
vlm39ba4c42004-09-22 16:06:28 +000024 NativeInteger_free,
25 NativeInteger_print,
vlmfa67ddc2004-06-03 03:38:44 +000026 asn_generic_no_constraint,
27 NativeInteger_decode_ber,
28 NativeInteger_encode_der,
vlmd52f9b52004-10-21 14:13:48 +000029 NativeInteger_decode_xer,
vlm39ba4c42004-09-22 16:06:28 +000030 NativeInteger_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +000031 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-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]),
vlme413c122004-08-20 13:23:42 +000036 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000037 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000038};
39
40/*
41 * Decode INTEGER type.
42 */
vlm9de248e2004-10-20 15:50:55 +000043asn_dec_rval_t
vlmef6355b2004-09-29 13:26:15 +000044NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
45 asn_TYPE_descriptor_t *td,
vlmb02dcc62005-03-10 18:52:02 +000046 void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
vlm80a48592005-02-25 12:10:27 +000047 long *native = (long *)*nint_ptr;
vlm9de248e2004-10-20 15:50:55 +000048 asn_dec_rval_t rval;
vlmfa67ddc2004-06-03 03:38:44 +000049 ber_tlv_len_t length;
50
51 /*
52 * If the structure is not there, allocate it.
53 */
vlm80a48592005-02-25 12:10:27 +000054 if(native == NULL) {
55 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
56 if(native == NULL) {
vlmfa67ddc2004-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 */
vlmef6355b2004-09-29 13:26:15 +000069 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
70 tag_mode, 0, &length, 0);
vlmfa67ddc2004-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 */
vlmb02dcc62005-03-10 18:52:02 +000079 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
vlmfa67ddc2004-06-03 03:38:44 +000080 size -= rval.consumed;
vlmb42843a2004-06-05 08:17:50 +000081 if(length > (ber_tlv_len_t)size) {
vlmfa67ddc2004-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
vlm80a48592005-02-25 12:10:27 +000089 * Fill the native, at the same time checking for overflow.
vlmfa67ddc2004-06-03 03:38:44 +000090 * If overflow occured, return with RC_FAIL.
91 */
92 {
93 INTEGER_t tmp;
vlm41dee122005-07-02 08:19:17 +000094 union {
95 const void *constbuf;
96 void *nonconstbuf;
97 } unconst_buf;
vlmfa67ddc2004-06-03 03:38:44 +000098 long l;
vlm41dee122005-07-02 08:19:17 +000099
100 unconst_buf.constbuf = buf_ptr;
101 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
vlmfa67ddc2004-06-03 03:38:44 +0000102 tmp.size = length;
103
vlmef6355b2004-09-29 13:26:15 +0000104 if(asn_INTEGER2long(&tmp, &l)) {
vlmfa67ddc2004-06-03 03:38:44 +0000105 rval.code = RC_FAIL;
106 rval.consumed = 0;
107 return rval;
108 }
109
vlm80a48592005-02-25 12:10:27 +0000110 *native = l;
vlmfa67ddc2004-06-03 03:38:44 +0000111
112 /*
vlm80a48592005-02-25 12:10:27 +0000113 * Note that native integer size might be other than long.
vlmfa67ddc2004-06-03 03:38:44 +0000114 * This expression hopefully will be optimized away
115 * by compiler.
116 */
vlm80a48592005-02-25 12:10:27 +0000117 if(sizeof(*native) != sizeof(long) && ((long)*native != l)) {
118 *native = 0; /* Safe value */
vlmfa67ddc2004-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
vlm80a48592005-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);
vlmfa67ddc2004-06-03 03:38:44 +0000130
131 return rval;
132}
133
134/*
135 * Encode the NativeInteger using the standard INTEGER type DER encoder.
136 */
vlm39ba4c42004-09-22 16:06:28 +0000137asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000138NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
vlmfa67ddc2004-06-03 03:38:44 +0000139 int tag_mode, ber_tlv_tag_t tag,
140 asn_app_consume_bytes_f *cb, void *app_key) {
vlm80a48592005-02-25 12:10:27 +0000141 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
vlm39ba4c42004-09-22 16:06:28 +0000142 asn_enc_rval_t erval;
vlmfa67ddc2004-06-03 03:38:44 +0000143 INTEGER_t tmp;
144
145#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
146
vlm80a48592005-02-25 12:10:27 +0000147 tmp.buf = (uint8_t *)&native;
148 tmp.size = sizeof(native);
vlmfa67ddc2004-06-03 03:38:44 +0000149
150#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
vlm80a48592005-02-25 12:10:27 +0000151 uint8_t buf[sizeof(native)];
vlmfa67ddc2004-06-03 03:38:44 +0000152 uint8_t *p;
153
vlm978078f2004-10-27 13:24:51 +0000154 /* Prepare a fake INTEGER */
vlm80a48592005-02-25 12:10:27 +0000155 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
156 *p = native;
vlmfa67ddc2004-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
vlmd52f9b52004-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,
vlmb02dcc62005-03-10 18:52:02 +0000177 const void *buf_ptr, size_t size) {
vlmd52f9b52004-10-21 14:13:48 +0000178 asn_dec_rval_t rval;
179 INTEGER_t *st = 0;
vlm8c0d2312005-01-27 17:54:57 +0000180 void *st_ptr = (void *)&st;
vlm80a48592005-02-25 12:10:27 +0000181 long *native = (long *)*sptr;
vlmd52f9b52004-10-21 14:13:48 +0000182
vlm80a48592005-02-25 12:10:27 +0000183 if(!native) {
vlma8f4a4c2005-01-17 14:32:45 +0000184 *sptr = CALLOC(1, sizeof(int));
vlm80a48592005-02-25 12:10:27 +0000185 native = (long *)*sptr;
186 if(!native) {
vlmd52f9b52004-10-21 14:13:48 +0000187 rval.code = RC_FAIL;
188 rval.consumed = 0;
189 return rval;
190 }
191 }
192
vlm8c0d2312005-01-27 17:54:57 +0000193 rval = INTEGER_decode_xer(opt_codec_ctx, td, (void **)st_ptr,
194 opt_mname, buf_ptr, size);
vlmd52f9b52004-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 {
vlm80a48592005-02-25 12:10:27 +0000201 *native = l;
vlmd52f9b52004-10-21 14:13:48 +0000202
vlm80a48592005-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 */
vlmd52f9b52004-10-21 14:13:48 +0000207 rval.code = RC_FAIL;
208 rval.consumed = 0;
209 return rval;
210 }
211 }
212 } else {
vlm80a48592005-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 */
vlmd52f9b52004-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
vlm39ba4c42004-09-22 16:06:28 +0000225asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000226NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-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;
vlm80a48592005-02-25 12:10:27 +0000231 const long *native = (const long *)sptr;
vlm39ba4c42004-09-22 16:06:28 +0000232
233 (void)ilevel;
234 (void)flags;
235
vlm80a48592005-02-25 12:10:27 +0000236 if(!native) _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000237
vlm80a48592005-02-25 12:10:27 +0000238 er.encoded = snprintf(scratch, sizeof(scratch), "%ld", *native);
vlm39ba4c42004-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
vlmfa67ddc2004-06-03 03:38:44 +0000246/*
247 * INTEGER specific human-readable output.
248 */
249int
vlmef6355b2004-09-29 13:26:15 +0000250NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000251 asn_app_consume_bytes_f *cb, void *app_key) {
vlm80a48592005-02-25 12:10:27 +0000252 const long *native = (const long *)sptr;
vlm7d278c42004-07-19 17:30:25 +0000253 char scratch[32]; /* Enough for 64-bit int */
vlmfa67ddc2004-06-03 03:38:44 +0000254 int ret;
255
vlmb42843a2004-06-05 08:17:50 +0000256 (void)td; /* Unused argument */
257 (void)ilevel; /* Unused argument */
258
vlm80a48592005-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));
vlm6678cb12004-09-26 13:10:40 +0000262 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000263 } else {
vlm6678cb12004-09-26 13:10:40 +0000264 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000265 }
266}
267
268void
vlmef6355b2004-09-29 13:26:15 +0000269NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlmfa67ddc2004-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