blob: ae71c044c8bab95ba77e5c03531d572322df8af1 [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>
14#include <INTEGER.h>
15#include <assert.h>
16
17/*
18 * NativeInteger basic type description.
19 */
vlmef6355b2004-09-29 13:26:15 +000020static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
vlmfa67ddc2004-06-03 03:38:44 +000021 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
22};
vlmef6355b2004-09-29 13:26:15 +000023asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
vlmfa67ddc2004-06-03 03:38:44 +000024 "INTEGER", /* The ASN.1 type is still 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,
vlm39ba4c42004-09-22 16:06:28 +000030 0, /* Not implemented yet */
31 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 */
44ber_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,
vlmfa67ddc2004-06-03 03:38:44 +000047 void **int_ptr, void *buf_ptr, size_t size, int tag_mode) {
vlmda674682004-08-11 09:07:36 +000048 int *Int = (int *)*int_ptr;
vlmfa67ddc2004-06-03 03:38:44 +000049 ber_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 */
55 if(Int == NULL) {
vlm6678cb12004-09-26 13:10:40 +000056 Int = (int *)(*int_ptr = CALLOC(1, sizeof(*Int)));
vlmfa67ddc2004-06-03 03:38:44 +000057 if(Int == NULL) {
58 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
90 * Fill the Int, at the same time checking for overflow.
91 * 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
105 *Int = l;
106
107 /*
108 * Note that int might be shorter than long.
109 * This expression hopefully will be optimized away
110 * by compiler.
111 */
112 if(sizeof(int) != sizeof(long) && (*Int != l)) {
113 *Int = 0; /* Safe value */
114 rval.code = RC_FAIL;
115 rval.consumed = 0;
116 return rval;
117 }
118 }
119
120 rval.code = RC_OK;
121 rval.consumed += length;
122
123 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
124 (long)rval.consumed, (long)length, td->name, *Int);
125
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) {
136 unsigned int Int = *(unsigned int *)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
vlm39ba4c42004-09-22 16:06:28 +0000142 tmp.buf = (uint8_t *)&Int;
vlmfa67ddc2004-06-03 03:38:44 +0000143 tmp.size = sizeof(Int);
144
145#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
146 uint8_t buf[sizeof(int)];
147 uint8_t *p;
148
149 /* Prepare fake INTEGER */
150 for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8)
151 *p = Int & 0xff;
152
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
vlm39ba4c42004-09-22 16:06:28 +0000166asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000167NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000168 int ilevel, enum xer_encoder_flags_e flags,
169 asn_app_consume_bytes_f *cb, void *app_key) {
170 char scratch[32]; /* Enough for 64-bit int */
171 asn_enc_rval_t er;
172 const int *Int = (const int *)sptr;
173
174 (void)ilevel;
175 (void)flags;
176
177 if(!Int) _ASN_ENCODE_FAILED;
178
179 er.encoded = snprintf(scratch, sizeof(scratch), "%d", *Int);
180 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
181 || cb(scratch, er.encoded, app_key) < 0)
182 _ASN_ENCODE_FAILED;
183
184 return er;
185}
186
vlmfa67ddc2004-06-03 03:38:44 +0000187/*
188 * INTEGER specific human-readable output.
189 */
190int
vlmef6355b2004-09-29 13:26:15 +0000191NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000192 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000193 const int *Int = (const int *)sptr;
vlm7d278c42004-07-19 17:30:25 +0000194 char scratch[32]; /* Enough for 64-bit int */
vlmfa67ddc2004-06-03 03:38:44 +0000195 int ret;
196
vlmb42843a2004-06-05 08:17:50 +0000197 (void)td; /* Unused argument */
198 (void)ilevel; /* Unused argument */
199
vlmfa67ddc2004-06-03 03:38:44 +0000200 if(Int) {
201 ret = snprintf(scratch, sizeof(scratch), "%d", *Int);
vlmb42843a2004-06-05 08:17:50 +0000202 assert(ret > 0 && ret < (int)sizeof(scratch));
vlm6678cb12004-09-26 13:10:40 +0000203 return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000204 } else {
vlm6678cb12004-09-26 13:10:40 +0000205 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000206 }
207}
208
209void
vlmef6355b2004-09-29 13:26:15 +0000210NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlmfa67ddc2004-06-03 03:38:44 +0000211
212 if(!td || !ptr)
213 return;
214
215 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
216 td->name, contents_only, ptr);
217
218 if(!contents_only) {
219 FREEMEM(ptr);
220 }
221}
222