blob: ca29049b736c1618830541a6e3c8bfbeee6f3692 [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 */
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 */
vlmb42843a2004-06-05 08:17:50 +000033 0, /* Always in primitive form */
34 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000035};
36
37/*
38 * Decode INTEGER type.
39 */
40ber_dec_rval_t
41NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td,
42 void **int_ptr, void *buf_ptr, size_t size, int tag_mode) {
vlmda674682004-08-11 09:07:36 +000043 int *Int = (int *)*int_ptr;
vlmfa67ddc2004-06-03 03:38:44 +000044 ber_dec_rval_t rval;
vlmb42843a2004-06-05 08:17:50 +000045 ber_dec_ctx_t ctx = { 0, 0, 0, 0 };
vlmfa67ddc2004-06-03 03:38:44 +000046 ber_tlv_len_t length;
47
48 /*
49 * If the structure is not there, allocate it.
50 */
51 if(Int == NULL) {
vlmda674682004-08-11 09:07:36 +000052 (void *)Int = *int_ptr = CALLOC(1, sizeof(*Int));
vlmfa67ddc2004-06-03 03:38:44 +000053 if(Int == NULL) {
54 rval.code = RC_FAIL;
55 rval.consumed = 0;
56 return rval;
57 }
58 }
59
60 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
61 td->name, tag_mode);
62
63 /*
64 * Check tags.
65 */
66 rval = ber_check_tags(td, &ctx,
67 buf_ptr, size, tag_mode, &length, 0);
68 if(rval.code != RC_OK)
69 return rval;
70
71 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
72
73 /*
74 * Make sure we have this length.
75 */
vlm1ff928d2004-08-11 08:10:13 +000076 (char *)buf_ptr += rval.consumed;
vlmfa67ddc2004-06-03 03:38:44 +000077 size -= rval.consumed;
vlmb42843a2004-06-05 08:17:50 +000078 if(length > (ber_tlv_len_t)size) {
vlmfa67ddc2004-06-03 03:38:44 +000079 rval.code = RC_WMORE;
80 rval.consumed = 0;
81 return rval;
82 }
83
84 /*
85 * ASN.1 encoded INTEGER: buf_ptr, length
86 * Fill the Int, at the same time checking for overflow.
87 * If overflow occured, return with RC_FAIL.
88 */
89 {
90 INTEGER_t tmp;
91 long l;
vlmda674682004-08-11 09:07:36 +000092 tmp.buf = (uint8_t *)buf_ptr;
vlmfa67ddc2004-06-03 03:38:44 +000093 tmp.size = length;
94
95 if(asn1_INTEGER2long(&tmp, &l)) {
96 rval.code = RC_FAIL;
97 rval.consumed = 0;
98 return rval;
99 }
100
101 *Int = l;
102
103 /*
104 * Note that int might be shorter than long.
105 * This expression hopefully will be optimized away
106 * by compiler.
107 */
108 if(sizeof(int) != sizeof(long) && (*Int != l)) {
109 *Int = 0; /* Safe value */
110 rval.code = RC_FAIL;
111 rval.consumed = 0;
112 return rval;
113 }
114 }
115
116 rval.code = RC_OK;
117 rval.consumed += length;
118
119 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
120 (long)rval.consumed, (long)length, td->name, *Int);
121
122 return rval;
123}
124
125/*
126 * Encode the NativeInteger using the standard INTEGER type DER encoder.
127 */
128der_enc_rval_t
129NativeInteger_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
130 int tag_mode, ber_tlv_tag_t tag,
131 asn_app_consume_bytes_f *cb, void *app_key) {
132 unsigned int Int = *(unsigned int *)ptr; /* Disable sign ext. */
133 der_enc_rval_t erval;
134 INTEGER_t tmp;
135
136#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
137
138 tmp.buf = &Int;
139 tmp.size = sizeof(Int);
140
141#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
142 uint8_t buf[sizeof(int)];
143 uint8_t *p;
144
145 /* Prepare fake INTEGER */
146 for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8)
147 *p = Int & 0xff;
148
149 tmp.buf = buf;
150 tmp.size = sizeof(buf);
151#endif /* WORDS_BIGENDIAN */
152
153 /* Encode fake INTEGER */
154 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
155 if(erval.encoded == -1) {
156 assert(erval.structure_ptr == &tmp);
157 erval.structure_ptr = ptr;
158 }
159 return erval;
160}
161
162/*
163 * INTEGER specific human-readable output.
164 */
165int
166NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
167 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000168 const int *Int = (const int *)sptr;
vlm7d278c42004-07-19 17:30:25 +0000169 char scratch[32]; /* Enough for 64-bit int */
vlmfa67ddc2004-06-03 03:38:44 +0000170 int ret;
171
vlmb42843a2004-06-05 08:17:50 +0000172 (void)td; /* Unused argument */
173 (void)ilevel; /* Unused argument */
174
vlmfa67ddc2004-06-03 03:38:44 +0000175 if(Int) {
176 ret = snprintf(scratch, sizeof(scratch), "%d", *Int);
vlmb42843a2004-06-05 08:17:50 +0000177 assert(ret > 0 && ret < (int)sizeof(scratch));
vlmfa67ddc2004-06-03 03:38:44 +0000178 return cb(scratch, ret, app_key);
179 } else {
180 return cb("<absent>", 8, app_key);
181 }
182}
183
184void
185NativeInteger_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
186
187 if(!td || !ptr)
188 return;
189
190 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
191 td->name, contents_only, ptr);
192
193 if(!contents_only) {
194 FREEMEM(ptr);
195 }
196}
197