No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / asn1 / der_length.c
blobb21ef353d824e408d5892cf33362ba859b6dc494
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "der_locl.h"
36 __RCSID("$Heimdal: der_length.c 19539 2006-12-28 17:15:05Z lha $"
37 "$NetBSD$");
39 size_t
40 _heim_len_unsigned (unsigned val)
42 size_t ret = 0;
43 int last_val_gt_128;
45 do {
46 ++ret;
47 last_val_gt_128 = (val >= 128);
48 val /= 256;
49 } while (val);
51 if(last_val_gt_128)
52 ret++;
54 return ret;
57 size_t
58 _heim_len_int (int val)
60 unsigned char q;
61 size_t ret = 0;
63 if (val >= 0) {
64 do {
65 q = val % 256;
66 ret++;
67 val /= 256;
68 } while(val);
69 if(q >= 128)
70 ret++;
71 } else {
72 val = ~val;
73 do {
74 q = ~(val % 256);
75 ret++;
76 val /= 256;
77 } while(val);
78 if(q < 128)
79 ret++;
81 return ret;
84 static size_t
85 len_oid (const heim_oid *oid)
87 size_t ret = 1;
88 int n;
90 for (n = 2; n < oid->length; ++n) {
91 unsigned u = oid->components[n];
93 do {
94 ++ret;
95 u /= 128;
96 } while(u > 0);
98 return ret;
101 size_t
102 der_length_len (size_t len)
104 if (len < 128)
105 return 1;
106 else {
107 int ret = 0;
108 do {
109 ++ret;
110 len /= 256;
111 } while (len);
112 return ret + 1;
116 size_t
117 der_length_integer (const int *data)
119 return _heim_len_int (*data);
122 size_t
123 der_length_unsigned (const unsigned *data)
125 return _heim_len_unsigned(*data);
128 size_t
129 der_length_enumerated (const unsigned *data)
131 return _heim_len_int (*data);
134 size_t
135 der_length_general_string (const heim_general_string *data)
137 return strlen(*data);
140 size_t
141 der_length_utf8string (const heim_utf8_string *data)
143 return strlen(*data);
146 size_t
147 der_length_printable_string (const heim_printable_string *data)
149 return strlen(*data);
152 size_t
153 der_length_ia5_string (const heim_ia5_string *data)
155 return strlen(*data);
158 size_t
159 der_length_bmp_string (const heim_bmp_string *data)
161 return data->length * 2;
164 size_t
165 der_length_universal_string (const heim_universal_string *data)
167 return data->length * 4;
170 size_t
171 der_length_visible_string (const heim_visible_string *data)
173 return strlen(*data);
176 size_t
177 der_length_octet_string (const heim_octet_string *k)
179 return k->length;
182 size_t
183 der_length_heim_integer (const heim_integer *k)
185 if (k->length == 0)
186 return 1;
187 if (k->negative)
188 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
189 else
190 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
193 size_t
194 der_length_oid (const heim_oid *k)
196 return len_oid (k);
199 size_t
200 der_length_generalized_time (const time_t *t)
202 heim_octet_string k;
203 size_t ret;
205 _heim_time2generalizedtime (*t, &k, 1);
206 ret = k.length;
207 free(k.data);
208 return ret;
211 size_t
212 der_length_utctime (const time_t *t)
214 heim_octet_string k;
215 size_t ret;
217 _heim_time2generalizedtime (*t, &k, 0);
218 ret = k.length;
219 free(k.data);
220 return ret;
223 size_t
224 der_length_boolean (const int *k)
226 return 1;
229 size_t
230 der_length_bit_string (const heim_bit_string *k)
232 return (k->length + 7) / 8 + 1;