s3: Add vfs_aio_posix
[Samba/gebeck_regimport.git] / source4 / heimdal / lib / asn1 / der_length.c
blobdb82025861ea5baa18cf6755093a357cec54eb3b
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "der_locl.h"
38 RCSID("$Id$");
40 size_t
41 _heim_len_unsigned (unsigned val)
43 size_t ret = 0;
44 int last_val_gt_128;
46 do {
47 ++ret;
48 last_val_gt_128 = (val >= 128);
49 val /= 256;
50 } while (val);
52 if(last_val_gt_128)
53 ret++;
55 return ret;
58 size_t
59 _heim_len_int (int val)
61 unsigned char q;
62 size_t ret = 0;
64 if (val >= 0) {
65 do {
66 q = val % 256;
67 ret++;
68 val /= 256;
69 } while(val);
70 if(q >= 128)
71 ret++;
72 } else {
73 val = ~val;
74 do {
75 q = ~(val % 256);
76 ret++;
77 val /= 256;
78 } while(val);
79 if(q < 128)
80 ret++;
82 return ret;
85 static size_t
86 len_oid (const heim_oid *oid)
88 size_t ret = 1;
89 size_t n;
91 for (n = 2; n < oid->length; ++n) {
92 unsigned u = oid->components[n];
94 do {
95 ++ret;
96 u /= 128;
97 } while(u > 0);
99 return ret;
102 size_t
103 der_length_len (size_t len)
105 if (len < 128)
106 return 1;
107 else {
108 int ret = 0;
109 do {
110 ++ret;
111 len /= 256;
112 } while (len);
113 return ret + 1;
117 size_t
118 der_length_tag(unsigned int tag)
120 size_t len = 0;
122 if(tag <= 30)
123 return 1;
124 while(tag) {
125 tag /= 128;
126 len++;
128 return len + 1;
131 size_t
132 der_length_integer (const int *data)
134 return _heim_len_int (*data);
137 size_t
138 der_length_unsigned (const unsigned *data)
140 return _heim_len_unsigned(*data);
143 size_t
144 der_length_enumerated (const unsigned *data)
146 return _heim_len_int (*data);
149 size_t
150 der_length_general_string (const heim_general_string *data)
152 return strlen(*data);
155 size_t
156 der_length_utf8string (const heim_utf8_string *data)
158 return strlen(*data);
161 size_t
162 der_length_printable_string (const heim_printable_string *data)
164 return data->length;
167 size_t
168 der_length_ia5_string (const heim_ia5_string *data)
170 return data->length;
173 size_t
174 der_length_bmp_string (const heim_bmp_string *data)
176 return data->length * 2;
179 size_t
180 der_length_universal_string (const heim_universal_string *data)
182 return data->length * 4;
185 size_t
186 der_length_visible_string (const heim_visible_string *data)
188 return strlen(*data);
191 size_t
192 der_length_octet_string (const heim_octet_string *k)
194 return k->length;
197 size_t
198 der_length_heim_integer (const heim_integer *k)
200 if (k->length == 0)
201 return 1;
202 if (k->negative)
203 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
204 else
205 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
208 size_t
209 der_length_oid (const heim_oid *k)
211 return len_oid (k);
214 size_t
215 der_length_generalized_time (const time_t *t)
217 heim_octet_string k;
218 size_t ret;
220 _heim_time2generalizedtime (*t, &k, 1);
221 ret = k.length;
222 free(k.data);
223 return ret;
226 size_t
227 der_length_utctime (const time_t *t)
229 heim_octet_string k;
230 size_t ret;
232 _heim_time2generalizedtime (*t, &k, 0);
233 ret = k.length;
234 free(k.data);
235 return ret;
238 size_t
239 der_length_boolean (const int *k)
241 return 1;
244 size_t
245 der_length_bit_string (const heim_bit_string *k)
247 return (k->length + 7) / 8 + 1;