blob: 102372c482768c029ec1dd803eea6417c3af672f [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * 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#include <assert.h>
15
16/*
17 * NativeInteger basic type description.
18 */
vlmef6355b2004-09-29 13:26:15 +000019static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
vlmfa67ddc2004-06-03 03:38:44 +000020 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
21};
vlmef6355b2004-09-29 13:26:15 +000022asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
vlmfa67ddc2004-06-03 03:38:44 +000023 "INTEGER", /* The ASN.1 type is still INTEGER */
vlm9de248e2004-10-20 15:50:55 +000024 "INTEGER",
vlm39ba4c42004-09-22 16:06:28 +000025 NativeInteger_free,
26 NativeInteger_print,
vlmfa67ddc2004-06-03 03:38:44 +000027 asn_generic_no_constraint,
28 NativeInteger_decode_ber,
29 NativeInteger_encode_der,
vlmd52f9b52004-10-21 14:13:48 +000030 NativeInteger_decode_xer,
vlm39ba4c42004-09-22 16:06:28 +000031 NativeInteger_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +000032 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +000033 asn_DEF_NativeInteger_tags,
34 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
35 asn_DEF_NativeInteger_tags, /* Same as above */
36 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
vlme413c122004-08-20 13:23:42 +000037 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000038 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000039};
40
41/*
42 * Decode INTEGER type.
43 */
vlm9de248e2004-10-20 15:50:55 +000044asn_dec_rval_t
vlmef6355b2004-09-29 13:26:15 +000045NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
46 asn_TYPE_descriptor_t *td,
vlm80a48592005-02-25 12:10:27 +000047 void **nint_ptr, void *buf_ptr, size_t size, int tag_mode) {
48 long *native = (long *)*nint_ptr;
vlm9de248e2004-10-20 15:50:55 +000049 asn_dec_rval_t rval;
vlmfa67ddc2004-06-03 03:38:44 +000050 ber_tlv_len_t length;
51
52 /*
53 * If the structure is not there, allocate it.
54 */
vlm80a48592005-02-25 12:10:27 +000055 if(native == NULL) {
56 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
57 if(native == NULL) {
vlmfa67ddc2004-06-03 03:38:44 +000058 rval.code = RC_FAIL;
59 rval.consumed = 0;
60 return rval;
61 }
62 }
63
64 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
65 td->name, tag_mode);
66
67 /*
68 * Check tags.
69 */
vlmef6355b2004-09-29 13:26:15 +000070 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
71 tag_mode, 0, &length, 0);
vlmfa67ddc2004-06-03 03:38:44 +000072 if(rval.code != RC_OK)
73 return rval;
74
75 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
76
77 /*
78 * Make sure we have this length.
79 */
vlmd86c9252004-08-25 01:34:11 +000080 buf_ptr = ((char *)buf_ptr) + rval.consumed;
vlmfa67ddc2004-06-03 03:38:44 +000081 size -= rval.consumed;
vlmb42843a2004-06-05 08:17:50 +000082 if(length > (ber_tlv_len_t)size) {
vlmfa67ddc2004-06-03 03:38:44 +000083 rval.code = RC_WMORE;
84 rval.consumed = 0;
85 return rval;
86 }
87
88 /*
89 * ASN.1 encoded INTEGER: buf_ptr, length
vlm80a48592005-02-25 12:10:27 +000090 * Fill the native, at the same time checking for overflow.
vlmfa67ddc2004-06-03 03:38:44 +000091 * If overflow occured, return with RC_FAIL.
92 */
93 {
94 INTEGER_t tmp;
95 long l;
vlmda674682004-08-11 09:07:36 +000096 tmp.buf = (uint8_t *)buf_ptr;
vlmfa67ddc2004-06-03 03:38:44 +000097 tmp.size = length;
98
vlmef6355b2004-09-29 13:26:15 +000099 if(asn_INTEGER2long(&tmp, &l)) {
vlmfa67ddc2004-06-03 03:38:44 +0000100 rval.code = RC_FAIL;
101 rval.consumed = 0;
102 return rval;
103 }
104
vlm80a48592005-02-25 12:10:27 +0000105 *native = l;
vlmfa67ddc2004-06-03 03:38:44 +0000106
107 /*
vlm80a48592005-02-25 12:10:27 +0000108 * Note that native integer size might be other than long.
vlmfa67ddc2004-06-03 03:38:44 +0000109 * This expression hopefully will be optimized away
110 * by compiler.
111 */
vlm80a48592005-02-25 12:10:27 +0000112 if(sizeof(*native) != sizeof(long) && ((long)*native != l)) {
113 *native = 0; /* Safe value */
vlmfa67ddc2004-06-03 03:38:44 +0000114 rval.code = RC_FAIL;
115 rval.consumed = 0;
116 return rval;
117 }
118 }
119
120 rval.code = RC_OK;
121 rval.consumed += length;
122
vlm80a48592005-02-25 12:10:27 +0000123 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
124 (long)rval.consumed, (long)length, td->name, (long)*native);
vlmfa67ddc2004-06-03 03:38:44 +0000125
126 return rval;
127}
128
129/*
130 * Encode the NativeInteger using the standard INTEGER type DER encoder.
131 */
vlm39ba4c42004-09-22 16:06:28 +0000132asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000133NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
vlmfa67ddc2004-06-03 03:38:44 +0000134 int tag_mode, ber_tlv_tag_t tag,
135 asn_app_consume_bytes_f *cb, void *app_key) {
vlm80a48592005-02-25 12:10:27 +0000136 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
vlm39ba4c42004-09-22 16:06:28 +0000137 asn_enc_rval_t erval;
vlmfa67ddc2004-06-03 03:38:44 +0000138 INTEGER_t tmp;
139
140#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
141
vlm80a48592005-02-25 12:10:27 +0000142 tmp.buf = (uint8_t *)&native;
143 tmp.size = sizeof(native);
vlmfa67ddc2004-06-03 03:38:44 +0000144
145#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
vlm80a48592005-02-25 12:10:27 +0000146 uint8_t buf[sizeof(native)];
vlmfa67ddc2004-06-03 03:38:44 +0000147 uint8_t *p;
148
vlm978078f2004-10-27 13:24:51 +0000149 /* Prepare a fake INTEGER */
vlm80a48592005-02-25 12:10:27 +0000150 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
151 *p = native;
vlmfa67ddc2004-06-03 03:38:44 +0000152
153 tmp.buf = buf;
154 tmp.size = sizeof(buf);
155#endif /* WORDS_BIGENDIAN */
156
157 /* Encode fake INTEGER */
158 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
159 if(erval.encoded == -1) {
160 assert(erval.structure_ptr == &tmp);
161 erval.structure_ptr = ptr;
162 }
163 return erval;
164}
165
vlmd52f9b52004-10-21 14:13:48 +0000166/*
167 * Decode the chunk of XML text encoding INTEGER.
168 */
169asn_dec_rval_t
170NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
171 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
172 void *buf_ptr, size_t size) {
173 asn_dec_rval_t rval;
174 INTEGER_t *st = 0;
vlm8c0d2312005-01-27 17:54:57 +0000175 void *st_ptr = (void *)&st;
vlm80a48592005-02-25 12:10:27 +0000176 long *native = (long *)*sptr;
vlmd52f9b52004-10-21 14:13:48 +0000177
vlm80a48592005-02-25 12:10:27 +0000178 if(!native) {
vlma8f4a4c2005-01-17 14:32:45 +0000179 *sptr = CALLOC(1, sizeof(int));
vlm80a48592005-02-25 12:10:27 +0000180 native = (long *)*sptr;
181 if(!native) {
vlmd52f9b52004-10-21 14:13:48 +0000182 rval.code = RC_FAIL;
183 rval.consumed = 0;
184 return rval;
185 }
186 }
187
vlm8c0d2312005-01-27 17:54:57 +0000188 rval = INTEGER_decode_xer(opt_codec_ctx, td, (void **)st_ptr,
189 opt_mname, buf_ptr, size);
vlmd52f9b52004-10-21 14:13:48 +0000190 if(rval.code == RC_OK) {
191 long l;
192 if(asn_INTEGER2long(st, &l)) {
193 rval.code = RC_FAIL;
194 rval.consumed = 0;
195 } else {
vlm80a48592005-02-25 12:10:27 +0000196 *native = l;
vlmd52f9b52004-10-21 14:13:48 +0000197
vlm80a48592005-02-25 12:10:27 +0000198 /* Native type might be shorter than long */
199 if(sizeof(*native) != sizeof(long)
200 && ((long)*native != l)) {
201 *native = 0; /* Safe value */
vlmd52f9b52004-10-21 14:13:48 +0000202 rval.code = RC_FAIL;
203 rval.consumed = 0;
204 return rval;
205 }
206 }
207 } else {
vlm80a48592005-02-25 12:10:27 +0000208 /*
209 * Cannot restart from the middle;
210 * there is no place to save state in the native type.
211 * Request a continuation from the very beginning.
212 */
vlmd52f9b52004-10-21 14:13:48 +0000213 rval.consumed = 0;
214 }
215 asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0);
216 return rval;
217}
218
219
vlm39ba4c42004-09-22 16:06:28 +0000220asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000221NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000222 int ilevel, enum xer_encoder_flags_e flags,
223 asn_app_consume_bytes_f *cb, void *app_key) {
224 char scratch[32]; /* Enough for 64-bit int */
225 asn_enc_rval_t er;
vlm80a48592005-02-25 12:10:27 +0000226 const long *native = (const long *)sptr;
vlm39ba4c42004-09-22 16:06:28 +0000227
228 (void)ilevel;
229 (void)flags;
230
vlm80a48592005-02-25 12:10:27 +0000231 if(!native) _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000232
vlm80a48592005-02-25 12:10:27 +0000233 er.encoded = snprintf(scratch, sizeof(scratch), "%ld", *native);
vlm39ba4c42004-09-22 16:06:28 +0000234 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
235 || cb(scratch, er.encoded, app_key) < 0)
236 _ASN_ENCODE_FAILED;
237
238 return er;
239}
240
vlmfa67ddc2004-06-03 03:38:44 +0000241/*
242 * INTEGER specific human-readable output.
243 */
244int
vlmef6355b2004-09-29 13:26:15 +0000245NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000246 asn_app_consume_bytes_f *cb, void *app_key) {
vlm80a48592005-02-25 12:10:27 +0000247 const long *native = (const long *)sptr;
vlm7d278c42004-07-19 17:30:25 +0000248 char scratch[32]; /* Enough for 64-bit int */
vlmfa67ddc2004-06-03 03:38:44 +0000249 int ret;
250
vlmb42843a2004-06-05 08:17:50 +0000251 (void)td; /* Unused argument */
252 (void)ilevel; /* Unused argument */
253
vlm80a48592005-02-25 12:10:27 +0000254 if(native) {
255 ret = snprintf(scratch, sizeof(scratch), "%ld", *native);
256 assert(ret > 0 && (size_t)ret < sizeof(scratch));
vlm6678cb12004-09-26 13:10:40 +0000257 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000258 } else {
vlm6678cb12004-09-26 13:10:40 +0000259 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000260 }
261}
262
263void
vlmef6355b2004-09-29 13:26:15 +0000264NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000265
266 if(!td || !ptr)
267 return;
268
269 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
270 td->name, contents_only, ptr);
271
272 if(!contents_only) {
273 FREEMEM(ptr);
274 }
275}
276