blob: aac5f86b38fa20e7b10598838db6e3a5969515fe [file] [log] [blame]
Lev Walkinf15320b2004-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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000012#include <asn_internal.h>
Lev Walkinf15320b2004-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 */
20static ber_tlv_tag_t asn1_DEF_NativeInteger_tags[] = {
21 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
22};
23asn1_TYPE_descriptor_t asn1_DEF_NativeInteger = {
24 "INTEGER", /* The ASN.1 type is still INTEGER */
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 NativeInteger_free,
26 NativeInteger_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 asn_generic_no_constraint,
28 NativeInteger_decode_ber,
29 NativeInteger_encode_der,
Lev Walkina9cc46e2004-09-22 16:06:28 +000030 0, /* Not implemented yet */
31 NativeInteger_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000032 0, /* Use generic outmost tag fetcher */
33 asn1_DEF_NativeInteger_tags,
Lev Walkin188ed2c2004-09-13 08:31:01 +000034 sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]),
35 asn1_DEF_NativeInteger_tags, /* Same as above */
36 sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]),
Lev Walkind9bd7752004-06-05 08:17:50 +000037 0, /* Always in primitive form */
Lev Walkin449f8322004-08-20 13:23:42 +000038 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000039 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000040};
41
42/*
43 * Decode INTEGER type.
44 */
45ber_dec_rval_t
46NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td,
47 void **int_ptr, void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinc2346572004-08-11 09:07:36 +000048 int *Int = (int *)*int_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000049 ber_dec_rval_t rval;
Lev Walkinf15320b2004-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) {
Lev Walkinc2346572004-08-11 09:07:36 +000056 (void *)Int = *int_ptr = CALLOC(1, sizeof(*Int));
Lev Walkinf15320b2004-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 */
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000070 rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, &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 Walkin4ce78ca2004-08-25 01:34:11 +000079 buf_ptr = ((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
89 * Fill the Int, at the same time checking for overflow.
90 * If overflow occured, return with RC_FAIL.
91 */
92 {
93 INTEGER_t tmp;
94 long l;
Lev Walkinc2346572004-08-11 09:07:36 +000095 tmp.buf = (uint8_t *)buf_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000096 tmp.size = length;
97
98 if(asn1_INTEGER2long(&tmp, &l)) {
99 rval.code = RC_FAIL;
100 rval.consumed = 0;
101 return rval;
102 }
103
104 *Int = l;
105
106 /*
107 * Note that int might be shorter than long.
108 * This expression hopefully will be optimized away
109 * by compiler.
110 */
111 if(sizeof(int) != sizeof(long) && (*Int != l)) {
112 *Int = 0; /* Safe value */
113 rval.code = RC_FAIL;
114 rval.consumed = 0;
115 return rval;
116 }
117 }
118
119 rval.code = RC_OK;
120 rval.consumed += length;
121
122 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
123 (long)rval.consumed, (long)length, td->name, *Int);
124
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 Walkinf15320b2004-06-03 03:38:44 +0000132NativeInteger_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
133 int tag_mode, ber_tlv_tag_t tag,
134 asn_app_consume_bytes_f *cb, void *app_key) {
135 unsigned int Int = *(unsigned int *)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 Walkina9cc46e2004-09-22 16:06:28 +0000141 tmp.buf = (uint8_t *)&Int;
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 tmp.size = sizeof(Int);
143
144#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
145 uint8_t buf[sizeof(int)];
146 uint8_t *p;
147
148 /* Prepare fake INTEGER */
149 for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8)
150 *p = Int & 0xff;
151
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 Walkina9cc46e2004-09-22 16:06:28 +0000165asn_enc_rval_t
166NativeInteger_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
167 int ilevel, enum xer_encoder_flags_e flags,
168 asn_app_consume_bytes_f *cb, void *app_key) {
169 char scratch[32]; /* Enough for 64-bit int */
170 asn_enc_rval_t er;
171 const int *Int = (const int *)sptr;
172
173 (void)ilevel;
174 (void)flags;
175
176 if(!Int) _ASN_ENCODE_FAILED;
177
178 er.encoded = snprintf(scratch, sizeof(scratch), "%d", *Int);
179 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
180 || cb(scratch, er.encoded, app_key) < 0)
181 _ASN_ENCODE_FAILED;
182
183 return er;
184}
185
Lev Walkinf15320b2004-06-03 03:38:44 +0000186/*
187 * INTEGER specific human-readable output.
188 */
189int
190NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
191 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000192 const int *Int = (const int *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000193 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 int ret;
195
Lev Walkind9bd7752004-06-05 08:17:50 +0000196 (void)td; /* Unused argument */
197 (void)ilevel; /* Unused argument */
198
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 if(Int) {
200 ret = snprintf(scratch, sizeof(scratch), "%d", *Int);
Lev Walkind9bd7752004-06-05 08:17:50 +0000201 assert(ret > 0 && ret < (int)sizeof(scratch));
Lev Walkinf15320b2004-06-03 03:38:44 +0000202 return cb(scratch, ret, app_key);
203 } else {
204 return cb("<absent>", 8, app_key);
205 }
206}
207
208void
209NativeInteger_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
210
211 if(!td || !ptr)
212 return;
213
214 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
215 td->name, contents_only, ptr);
216
217 if(!contents_only) {
218 FREEMEM(ptr);
219 }
220}
221