/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- _heim_len_unsigned
- _heim_len_int
- len_oid
- der_length_len
- der_length_integer
- der_length_unsigned
- der_length_enumerated
- der_length_general_string
- der_length_utf8string
- der_length_printable_string
- der_length_ia5_string
- der_length_bmp_string
- der_length_universal_string
- der_length_visible_string
- der_length_octet_string
- der_length_heim_integer
- der_length_oid
- der_length_generalized_time
- der_length_utctime
- der_length_boolean
- der_length_bit_string
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "der_locl.h"
35
36 RCSID("$Id$");
37
38 size_t
39 _heim_len_unsigned (unsigned val)
/* [<][>][^][v][top][bottom][index][help] */
40 {
41 size_t ret = 0;
42 int last_val_gt_128;
43
44 do {
45 ++ret;
46 last_val_gt_128 = (val >= 128);
47 val /= 256;
48 } while (val);
49
50 if(last_val_gt_128)
51 ret++;
52
53 return ret;
54 }
55
56 size_t
57 _heim_len_int (int val)
/* [<][>][^][v][top][bottom][index][help] */
58 {
59 unsigned char q;
60 size_t ret = 0;
61
62 if (val >= 0) {
63 do {
64 q = val % 256;
65 ret++;
66 val /= 256;
67 } while(val);
68 if(q >= 128)
69 ret++;
70 } else {
71 val = ~val;
72 do {
73 q = ~(val % 256);
74 ret++;
75 val /= 256;
76 } while(val);
77 if(q < 128)
78 ret++;
79 }
80 return ret;
81 }
82
83 static size_t
84 len_oid (const heim_oid *oid)
/* [<][>][^][v][top][bottom][index][help] */
85 {
86 size_t ret = 1;
87 int n;
88
89 for (n = 2; n < oid->length; ++n) {
90 unsigned u = oid->components[n];
91
92 do {
93 ++ret;
94 u /= 128;
95 } while(u > 0);
96 }
97 return ret;
98 }
99
100 size_t
101 der_length_len (size_t len)
/* [<][>][^][v][top][bottom][index][help] */
102 {
103 if (len < 128)
104 return 1;
105 else {
106 int ret = 0;
107 do {
108 ++ret;
109 len /= 256;
110 } while (len);
111 return ret + 1;
112 }
113 }
114
115 size_t
116 der_length_integer (const int *data)
/* [<][>][^][v][top][bottom][index][help] */
117 {
118 return _heim_len_int (*data);
119 }
120
121 size_t
122 der_length_unsigned (const unsigned *data)
/* [<][>][^][v][top][bottom][index][help] */
123 {
124 return _heim_len_unsigned(*data);
125 }
126
127 size_t
128 der_length_enumerated (const unsigned *data)
/* [<][>][^][v][top][bottom][index][help] */
129 {
130 return _heim_len_int (*data);
131 }
132
133 size_t
134 der_length_general_string (const heim_general_string *data)
/* [<][>][^][v][top][bottom][index][help] */
135 {
136 return strlen(*data);
137 }
138
139 size_t
140 der_length_utf8string (const heim_utf8_string *data)
/* [<][>][^][v][top][bottom][index][help] */
141 {
142 return strlen(*data);
143 }
144
145 size_t
146 der_length_printable_string (const heim_printable_string *data)
/* [<][>][^][v][top][bottom][index][help] */
147 {
148 return strlen(*data);
149 }
150
151 size_t
152 der_length_ia5_string (const heim_ia5_string *data)
/* [<][>][^][v][top][bottom][index][help] */
153 {
154 return strlen(*data);
155 }
156
157 size_t
158 der_length_bmp_string (const heim_bmp_string *data)
/* [<][>][^][v][top][bottom][index][help] */
159 {
160 return data->length * 2;
161 }
162
163 size_t
164 der_length_universal_string (const heim_universal_string *data)
/* [<][>][^][v][top][bottom][index][help] */
165 {
166 return data->length * 4;
167 }
168
169 size_t
170 der_length_visible_string (const heim_visible_string *data)
/* [<][>][^][v][top][bottom][index][help] */
171 {
172 return strlen(*data);
173 }
174
175 size_t
176 der_length_octet_string (const heim_octet_string *k)
/* [<][>][^][v][top][bottom][index][help] */
177 {
178 return k->length;
179 }
180
181 size_t
182 der_length_heim_integer (const heim_integer *k)
/* [<][>][^][v][top][bottom][index][help] */
183 {
184 if (k->length == 0)
185 return 1;
186 if (k->negative)
187 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
188 else
189 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
190 }
191
192 size_t
193 der_length_oid (const heim_oid *k)
/* [<][>][^][v][top][bottom][index][help] */
194 {
195 return len_oid (k);
196 }
197
198 size_t
199 der_length_generalized_time (const time_t *t)
/* [<][>][^][v][top][bottom][index][help] */
200 {
201 heim_octet_string k;
202 size_t ret;
203
204 _heim_time2generalizedtime (*t, &k, 1);
205 ret = k.length;
206 free(k.data);
207 return ret;
208 }
209
210 size_t
211 der_length_utctime (const time_t *t)
/* [<][>][^][v][top][bottom][index][help] */
212 {
213 heim_octet_string k;
214 size_t ret;
215
216 _heim_time2generalizedtime (*t, &k, 0);
217 ret = k.length;
218 free(k.data);
219 return ret;
220 }
221
222 size_t
223 der_length_boolean (const int *k)
/* [<][>][^][v][top][bottom][index][help] */
224 {
225 return 1;
226 }
227
228 size_t
229 der_length_bit_string (const heim_bit_string *k)
/* [<][>][^][v][top][bottom][index][help] */
230 {
231 return (k->length + 7) / 8 + 1;
232 }