2 * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
7 * Read the NativeInteger.h for the explanation wrt. differences between
8 * 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.
13 #include <asn_internal.h>
14 #include <NativeInteger.h>
17 * NativeInteger basic type description.
19 static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
20 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
22 asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
23 "INTEGER", /* The ASN.1 type is still INTEGER */
27 asn_generic_no_constraint,
28 NativeInteger_decode_ber,
29 NativeInteger_encode_der,
30 NativeInteger_decode_xer,
31 NativeInteger_encode_xer,
32 NativeInteger_decode_uper, /* Unaligned PER decoder */
33 NativeInteger_encode_uper, /* Unaligned PER encoder */
34 0, /* Use generic outmost tag fetcher */
35 asn_DEF_NativeInteger_tags,
36 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
37 asn_DEF_NativeInteger_tags, /* Same as above */
38 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
39 0, /* No PER visible constraints */
40 0, 0, /* No members */
45 * Decode INTEGER type.
48 NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
49 asn_TYPE_descriptor_t *td,
50 void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
51 long *native = (long *)*nint_ptr;
56 * If the structure is not there, allocate it.
59 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
67 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
73 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
74 tag_mode, 0, &length, 0);
75 if(rval.code != RC_OK)
78 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
81 * Make sure we have this length.
83 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
84 size -= rval.consumed;
85 if(length > (ber_tlv_len_t)size) {
92 * ASN.1 encoded INTEGER: buf_ptr, length
93 * Fill the native, at the same time checking for overflow.
94 * If overflow occured, return with RC_FAIL.
104 unconst_buf.constbuf = buf_ptr;
105 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
108 if(asn_INTEGER2long(&tmp, &l)) {
118 rval.consumed += length;
120 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
121 (long)rval.consumed, (long)length, td->name, (long)*native);
127 * Encode the NativeInteger using the standard INTEGER type DER encoder.
130 NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
131 int tag_mode, ber_tlv_tag_t tag,
132 asn_app_consume_bytes_f *cb, void *app_key) {
133 unsigned long native = *(unsigned long *)ptr; /* Disable sign ext. */
134 asn_enc_rval_t erval;
137 #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
139 tmp.buf = (uint8_t *)&native;
140 tmp.size = sizeof(native);
142 #else /* Works even if WORDS_BIGENDIAN is not set where should've been */
143 uint8_t buf[sizeof(native)];
146 /* Prepare a fake INTEGER */
147 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
151 tmp.size = sizeof(buf);
152 #endif /* WORDS_BIGENDIAN */
154 /* Encode fake INTEGER */
155 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
156 if(erval.encoded == -1) {
157 assert(erval.structure_ptr == &tmp);
158 erval.structure_ptr = ptr;
164 * Decode the chunk of XML text encoding INTEGER.
167 NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
168 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
169 const void *buf_ptr, size_t size) {
172 void *st_ptr = (void *)&st;
173 long *native = (long *)*sptr;
176 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
177 if(!native) _ASN_DECODE_FAILED;
180 memset(&st, 0, sizeof(st));
181 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
182 opt_mname, buf_ptr, size);
183 if(rval.code == RC_OK) {
185 if(asn_INTEGER2long(&st, &l)) {
193 * Cannot restart from the middle;
194 * there is no place to save state in the native type.
195 * Request a continuation from the very beginning.
199 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
205 NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
206 int ilevel, enum xer_encoder_flags_e flags,
207 asn_app_consume_bytes_f *cb, void *app_key) {
208 char scratch[32]; /* Enough for 64-bit int */
210 const long *native = (const long *)sptr;
215 if(!native) _ASN_ENCODE_FAILED;
217 er.encoded = snprintf(scratch, sizeof(scratch), "%ld", *native);
218 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
219 || cb(scratch, er.encoded, app_key) < 0)
226 NativeInteger_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
227 asn_TYPE_descriptor_t *td,
228 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
231 long *native = (long *)*sptr;
233 void *tmpintptr = &tmpint;
236 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
239 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
240 if(!native) _ASN_DECODE_FAILED;
243 memset(&tmpint, 0, sizeof tmpint);
244 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
246 if(rval.code == RC_OK) {
247 if(asn_INTEGER2long(&tmpint, native))
250 ASN_DEBUG("NativeInteger %s got value %ld",
253 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
259 NativeInteger_encode_uper(asn_TYPE_descriptor_t *td,
260 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
265 if(!sptr) _ASN_ENCODE_FAILED;
267 native = *(long *)sptr;
269 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
271 memset(&tmpint, 0, sizeof(tmpint));
272 if(asn_long2INTEGER(&tmpint, native))
274 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
275 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
280 * INTEGER specific human-readable output.
283 NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
284 asn_app_consume_bytes_f *cb, void *app_key) {
285 const long *native = (const long *)sptr;
286 char scratch[32]; /* Enough for 64-bit int */
289 (void)td; /* Unused argument */
290 (void)ilevel; /* Unused argument */
293 ret = snprintf(scratch, sizeof(scratch), "%ld", *native);
294 assert(ret > 0 && (size_t)ret < sizeof(scratch));
295 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
297 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
302 NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
307 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
308 td->name, contents_only, ptr);