r14198: Update Samba4 to current lorikeet-heimdal.
[jelmer/samba4-debian.git] / source / heimdal / lib / asn1 / der_length.c
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: der_length.c,v 1.18 2006/01/20 10:04:46 lha Exp $");
37
38 size_t
39 _heim_len_unsigned (unsigned val)
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)
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)
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 length_len (size_t len)
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 length_integer (const int *data)
117 {
118     return _heim_len_int (*data);
119 }
120
121 size_t
122 length_unsigned (const unsigned *data)
123 {
124     return _heim_len_unsigned(*data);
125 }
126
127 size_t
128 length_enumerated (const unsigned *data)
129 {
130   return _heim_len_int (*data);
131 }
132
133 size_t
134 length_general_string (const heim_general_string *data)
135 {
136     return strlen(*data);
137 }
138
139 size_t
140 length_utf8string (const heim_utf8_string *data)
141 {
142     return strlen(*data);
143 }
144
145 size_t
146 length_printable_string (const heim_printable_string *data)
147 {
148     return strlen(*data);
149 }
150
151 size_t
152 length_ia5_string (const heim_ia5_string *data)
153 {
154     return strlen(*data);
155 }
156
157 size_t
158 length_bmp_string (const heim_bmp_string *data)
159 {
160     return data->length * 2;
161 }
162
163 size_t
164 length_universal_string (const heim_universal_string *data)
165 {
166     return data->length * 4;
167 }
168
169 size_t
170 length_octet_string (const heim_octet_string *k)
171 {
172     return k->length;
173 }
174
175 size_t
176 length_heim_integer (const heim_integer *k)
177 {
178     if (k->length == 0)
179         return 1;
180     if (k->negative)
181         return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
182     else
183         return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
184 }
185
186 size_t
187 length_oid (const heim_oid *k)
188 {
189     return len_oid (k);
190 }
191
192 size_t
193 length_generalized_time (const time_t *t)
194 {
195     heim_octet_string k;
196     size_t ret;
197
198     _heim_time2generalizedtime (*t, &k, 1);
199     ret = k.length;
200     free(k.data);
201     return ret;
202 }
203
204 size_t
205 length_utctime (const time_t *t)
206 {
207     heim_octet_string k;
208     size_t ret;
209
210     _heim_time2generalizedtime (*t, &k, 0);
211     ret = k.length;
212     free(k.data);
213     return ret;
214 }
215
216 size_t
217 length_boolean (const int *k)
218 {
219     return 1;
220 }
221
222 size_t
223 length_bit_string (const heim_bit_string *k)
224 {
225     return (k->length + 7) / 8 + 1;
226 }