Conf file is now in source tree
[mod_auth_kerb.cvs/.git] / spnegokrb5 / der_length.c
1 /*
2  * Copyright (c) 1997 - 2001 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 #if 0
37 RCSID("$Id$");
38 #endif
39
40 static size_t
41 len_unsigned (unsigned val)
42 {
43   size_t ret = 0;
44
45   do {
46     ++ret;
47     val /= 256;
48   } while (val);
49   return ret;
50 }
51
52 static size_t
53 len_int (int val)
54 {
55   size_t ret = 0;
56
57   if (val == 0)
58     return 1;
59   while (val > 255 || val < -255) {
60     ++ret;
61     val /= 256;
62   }
63   if (val != 0) {
64     ++ret;
65     if ((signed char)val != val)
66       ++ret;
67     val /= 256;
68   }
69   return ret;
70 }
71
72 static size_t
73 len_oid (const oid *oid)
74 {
75     size_t ret = 1;
76     int n;
77
78     for (n = 2; n < oid->length; ++n) {
79         unsigned u = oid->components[n];
80
81         ++ret;
82         u /= 128;
83         while (u > 0) {
84             ++ret;
85             u /= 128;
86         }
87     }
88     return ret;
89 }
90
91 size_t
92 length_len (size_t len)
93 {
94   if (len < 128)
95     return 1;
96   else
97     return len_unsigned (len) + 1;
98 }
99
100 size_t
101 length_integer (const int *data)
102 {
103   size_t len = len_int (*data);
104
105   return 1 + length_len(len) + len;
106 }
107
108 size_t
109 length_unsigned (const unsigned *data)
110 {
111   size_t len = len_unsigned (*data);
112
113   return 1 + length_len(len) + len;
114 }
115
116 size_t
117 length_enumerated (const unsigned *data)
118 {
119   size_t len = len_int (*data);
120
121   return 1 + length_len(len) + len;
122 }
123
124 size_t
125 length_general_string (const general_string *data)
126 {
127   char *str = *data;
128   size_t len = strlen(str);
129   return 1 + length_len(len) + len;
130 }
131
132 size_t
133 length_octet_string (const octet_string *k)
134 {
135   return 1 + length_len(k->length) + k->length;
136 }
137
138 size_t
139 length_oid (const oid *k)
140 {
141   size_t len = len_oid (k);
142
143   return 1 + length_len(len) + len;
144 }
145
146 size_t
147 length_generalized_time (const time_t *t)
148 {
149   octet_string k;
150   size_t ret;
151
152   time2generalizedtime (*t, &k);
153   ret = 1 + length_len(k.length) + k.length;
154   free (k.data);
155   return ret;
156 }