Spelling
[heimdal.git] / lib / asn1 / der_length.c
blob5ea3a848455f0ee5747886482f8eb2861f9da02f
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("$Id$");
38 size_t
39 _heim_len_unsigned (unsigned val)
41 size_t ret = 0;
42 int last_val_gt_128;
44 do {
45 ++ret;
46 last_val_gt_128 = (val >= 128);
47 val /= 256;
48 } while (val);
50 if(last_val_gt_128)
51 ret++;
53 return ret;
56 size_t
57 _heim_len_int (int val)
59 unsigned char q;
60 size_t ret = 0;
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++;
80 return ret;
83 static size_t
84 len_oid (const heim_oid *oid)
86 size_t ret = 1;
87 int n;
89 for (n = 2; n < oid->length; ++n) {
90 unsigned u = oid->components[n];
92 do {
93 ++ret;
94 u /= 128;
95 } while(u > 0);
97 return ret;
100 size_t
101 der_length_len (size_t len)
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;
115 size_t
116 der_length_integer (const int *data)
118 return _heim_len_int (*data);
121 size_t
122 der_length_unsigned (const unsigned *data)
124 return _heim_len_unsigned(*data);
127 size_t
128 der_length_enumerated (const unsigned *data)
130 return _heim_len_int (*data);
133 size_t
134 der_length_general_string (const heim_general_string *data)
136 return strlen(*data);
139 size_t
140 der_length_utf8string (const heim_utf8_string *data)
142 return strlen(*data);
145 size_t
146 der_length_printable_string (const heim_printable_string *data)
148 return strlen(*data);
151 size_t
152 der_length_ia5_string (const heim_ia5_string *data)
154 return strlen(*data);
157 size_t
158 der_length_bmp_string (const heim_bmp_string *data)
160 return data->length * 2;
163 size_t
164 der_length_universal_string (const heim_universal_string *data)
166 return data->length * 4;
169 size_t
170 der_length_visible_string (const heim_visible_string *data)
172 return strlen(*data);
175 size_t
176 der_length_octet_string (const heim_octet_string *k)
178 return k->length;
181 size_t
182 der_length_heim_integer (const heim_integer *k)
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);
192 size_t
193 der_length_oid (const heim_oid *k)
195 return len_oid (k);
198 size_t
199 der_length_generalized_time (const time_t *t)
201 heim_octet_string k;
202 size_t ret;
204 _heim_time2generalizedtime (*t, &k, 1);
205 ret = k.length;
206 free(k.data);
207 return ret;
210 size_t
211 der_length_utctime (const time_t *t)
213 heim_octet_string k;
214 size_t ret;
216 _heim_time2generalizedtime (*t, &k, 0);
217 ret = k.length;
218 free(k.data);
219 return ret;
222 size_t
223 der_length_boolean (const int *k)
225 return 1;
228 size_t
229 der_length_bit_string (const heim_bit_string *k)
231 return (k->length + 7) / 8 + 1;