blob: b0cc3b71322f74dcb21a8987e7867e8c693f48be [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 */
12#include <NativeInteger.h>
13#include <INTEGER.h>
14#include <assert.h>
15
16/*
17 * NativeInteger basic type description.
18 */
19static ber_tlv_tag_t asn1_DEF_NativeInteger_tags[] = {
20 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
21};
22asn1_TYPE_descriptor_t asn1_DEF_NativeInteger = {
23 "INTEGER", /* The ASN.1 type is still INTEGER */
24 asn_generic_no_constraint,
25 NativeInteger_decode_ber,
26 NativeInteger_encode_der,
27 NativeInteger_print,
28 NativeInteger_free,
29 0, /* Use generic outmost tag fetcher */
30 asn1_DEF_NativeInteger_tags,
31 sizeof(asn1_DEF_NativeInteger_tags)/sizeof(asn1_DEF_NativeInteger_tags[0]),
32 1, /* Single UNIVERSAL tag may be implicitly overriden */
Lev Walkind9bd7752004-06-05 08:17:50 +000033 0, /* Always in primitive form */
Lev Walkin449f8322004-08-20 13:23:42 +000034 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000035 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000036};
37
38/*
39 * Decode INTEGER type.
40 */
41ber_dec_rval_t
42NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td,
43 void **int_ptr, void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinc2346572004-08-11 09:07:36 +000044 int *Int = (int *)*int_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000045 ber_dec_rval_t rval;
Lev Walkind9bd7752004-06-05 08:17:50 +000046 ber_dec_ctx_t ctx = { 0, 0, 0, 0 };
Lev Walkinf15320b2004-06-03 03:38:44 +000047 ber_tlv_len_t length;
48
49 /*
50 * If the structure is not there, allocate it.
51 */
52 if(Int == NULL) {
Lev Walkinc2346572004-08-11 09:07:36 +000053 (void *)Int = *int_ptr = CALLOC(1, sizeof(*Int));
Lev Walkinf15320b2004-06-03 03:38:44 +000054 if(Int == NULL) {
55 rval.code = RC_FAIL;
56 rval.consumed = 0;
57 return rval;
58 }
59 }
60
61 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
62 td->name, tag_mode);
63
64 /*
65 * Check tags.
66 */
67 rval = ber_check_tags(td, &ctx,
68 buf_ptr, size, tag_mode, &length, 0);
69 if(rval.code != RC_OK)
70 return rval;
71
72 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
73
74 /*
75 * Make sure we have this length.
76 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +000077 buf_ptr = ((char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000078 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +000079 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000080 rval.code = RC_WMORE;
81 rval.consumed = 0;
82 return rval;
83 }
84
85 /*
86 * ASN.1 encoded INTEGER: buf_ptr, length
87 * Fill the Int, at the same time checking for overflow.
88 * If overflow occured, return with RC_FAIL.
89 */
90 {
91 INTEGER_t tmp;
92 long l;
Lev Walkinc2346572004-08-11 09:07:36 +000093 tmp.buf = (uint8_t *)buf_ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000094 tmp.size = length;
95
96 if(asn1_INTEGER2long(&tmp, &l)) {
97 rval.code = RC_FAIL;
98 rval.consumed = 0;
99 return rval;
100 }
101
102 *Int = l;
103
104 /*
105 * Note that int might be shorter than long.
106 * This expression hopefully will be optimized away
107 * by compiler.
108 */
109 if(sizeof(int) != sizeof(long) && (*Int != l)) {
110 *Int = 0; /* Safe value */
111 rval.code = RC_FAIL;
112 rval.consumed = 0;
113 return rval;
114 }
115 }
116
117 rval.code = RC_OK;
118 rval.consumed += length;
119
120 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
121 (long)rval.consumed, (long)length, td->name, *Int);
122
123 return rval;
124}
125
126/*
127 * Encode the NativeInteger using the standard INTEGER type DER encoder.
128 */
129der_enc_rval_t
130NativeInteger_encode_der(asn1_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 int Int = *(unsigned int *)ptr; /* Disable sign ext. */
134 der_enc_rval_t erval;
135 INTEGER_t tmp;
136
137#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
138
139 tmp.buf = &Int;
140 tmp.size = sizeof(Int);
141
142#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
143 uint8_t buf[sizeof(int)];
144 uint8_t *p;
145
146 /* Prepare fake INTEGER */
147 for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8)
148 *p = Int & 0xff;
149
150 tmp.buf = buf;
151 tmp.size = sizeof(buf);
152#endif /* WORDS_BIGENDIAN */
153
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;
159 }
160 return erval;
161}
162
163/*
164 * INTEGER specific human-readable output.
165 */
166int
167NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
168 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000169 const int *Int = (const int *)sptr;
Lev Walkindb13f512004-07-19 17:30:25 +0000170 char scratch[32]; /* Enough for 64-bit int */
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 int ret;
172
Lev Walkind9bd7752004-06-05 08:17:50 +0000173 (void)td; /* Unused argument */
174 (void)ilevel; /* Unused argument */
175
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 if(Int) {
177 ret = snprintf(scratch, sizeof(scratch), "%d", *Int);
Lev Walkind9bd7752004-06-05 08:17:50 +0000178 assert(ret > 0 && ret < (int)sizeof(scratch));
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 return cb(scratch, ret, app_key);
180 } else {
181 return cb("<absent>", 8, app_key);
182 }
183}
184
185void
186NativeInteger_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
187
188 if(!td || !ptr)
189 return;
190
191 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
192 td->name, contents_only, ptr);
193
194 if(!contents_only) {
195 FREEMEM(ptr);
196 }
197}
198