roken: qsort provide ANSI C prototype for swapfunc()
[heimdal.git] / lib / asn1 / extra.c
blob253ac5aca6f38b86f41efdc93e87792e712d155e
1 /*
2 * Copyright (c) 2003 - 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"
37 #include "heim_asn1.h"
38 #include <vis.h>
39 #include <vis-extras.h>
41 RCSID("$Id$");
43 int ASN1CALL
44 encode_heim_any(unsigned char *p, size_t len,
45 const heim_any *data, size_t *size)
47 return der_put_octet_string (p, len, data, size);
50 int ASN1CALL
51 decode_heim_any(const unsigned char *p, size_t len,
52 heim_any *data, size_t *size)
54 size_t len_len, length, l;
55 Der_class thisclass;
56 Der_type thistype;
57 unsigned int thistag;
58 int e;
60 memset(data, 0, sizeof(*data));
62 e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
63 if (e) return e;
64 if (l > len)
65 return ASN1_OVERFLOW;
66 e = der_get_length(p + l, len - l, &length, &len_len);
67 if (e) return e;
68 if (length == ASN1_INDEFINITE) {
69 if (len < len_len + l)
70 return ASN1_OVERFLOW;
71 length = len - (len_len + l);
72 } else {
73 if (len < length + len_len + l)
74 return ASN1_OVERFLOW;
77 data->data = malloc(length + len_len + l);
78 if (data->data == NULL)
79 return ENOMEM;
80 data->length = length + len_len + l;
81 memcpy(data->data, p, length + len_len + l);
83 if (size)
84 *size = length + len_len + l;
86 return 0;
89 void ASN1CALL
90 free_heim_any(heim_any *data)
92 der_free_octet_string(data);
95 char * ASN1CALL
96 print_heim_any(const heim_any *data, int flags)
98 char *s2 = NULL;
99 char *s = der_print_octet_string(data, 0);
100 int r = -1;
102 (void)flags;
103 if (s)
104 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
105 free(s);
106 s = NULL;
107 if (r > -1)
108 (void) asprintf(&s, "\"%s\"", s2);
109 free(s2);
110 return s;
113 size_t ASN1CALL
114 length_heim_any(const heim_any *data)
116 return data->length;
119 int ASN1CALL
120 copy_heim_any(const heim_any *from, heim_any *to)
122 return der_copy_octet_string(from, to);
125 int ASN1CALL
126 encode_HEIM_ANY(unsigned char *p, size_t len,
127 const heim_any *data, size_t *size)
129 return encode_heim_any(p, len, data, size);
132 int ASN1CALL
133 decode_HEIM_ANY(const unsigned char *p, size_t len,
134 heim_any *data, size_t *size)
136 return decode_heim_any(p, len, data, size);
139 void ASN1CALL
140 free_HEIM_ANY(heim_any *data)
142 der_free_octet_string(data);
145 char * ASN1CALL
146 print_HEIM_ANY(const heim_any *data, int flags)
148 char *s2 = NULL;
149 char *s = der_print_octet_string(data, 0);
150 int r = -1;
152 (void)flags;
153 if (s)
154 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
155 free(s);
156 s = NULL;
157 if (r > -1)
158 (void) asprintf(&s, "\"%s\"", s2);
159 free(s2);
160 return s;
163 size_t ASN1CALL
164 length_HEIM_ANY(const heim_any *data)
166 return data->length;
169 int ASN1CALL
170 copy_HEIM_ANY(const heim_any *from, heim_any *to)
172 return der_copy_octet_string(from, to);
175 int ASN1CALL
176 encode_heim_any_set(unsigned char *p, size_t len,
177 const heim_any_set *data, size_t *size)
179 return der_put_octet_string (p, len, data, size);
182 int ASN1CALL
183 decode_heim_any_set(const unsigned char *p, size_t len,
184 heim_any_set *data, size_t *size)
186 return der_get_octet_string(p, len, data, size);
189 void ASN1CALL
190 free_heim_any_set(heim_any_set *data)
192 der_free_octet_string(data);
195 char * ASN1CALL
196 print_heim_any_set(const heim_any_set *data, int flags)
198 char *s2 = NULL;
199 char *s = der_print_octet_string(data, 0);
200 int r = -1;
202 (void)flags;
203 if (s)
204 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
205 free(s);
206 s = NULL;
207 if (r > -1)
208 (void) asprintf(&s, "\"%s\"", s2);
209 free(s2);
210 return s;
213 size_t ASN1CALL
214 length_heim_any_set(const heim_any *data)
216 return data->length;
219 int ASN1CALL
220 copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
222 return der_copy_octet_string(from, to);
225 int ASN1CALL
226 heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
228 return der_heim_octet_string_cmp(p, q);
231 int ASN1CALL
232 encode_HEIM_ANY_SET(unsigned char *p, size_t len,
233 const heim_any_set *data, size_t *size)
235 return encode_heim_any_set(p, len, data, size);
238 int ASN1CALL
239 decode_HEIM_ANY_SET(const unsigned char *p, size_t len,
240 heim_any_set *data, size_t *size)
242 return decode_heim_any_set(p, len, data, size);
245 void ASN1CALL
246 free_HEIM_ANY_SET(heim_any_set *data)
248 der_free_octet_string(data);
251 char * ASN1CALL
252 print_HEIM_ANY_SET(const heim_any_set *data, int flags)
254 char *s2 = NULL;
255 char *s = der_print_octet_string(data, 0);
256 int r = -1;
258 (void)flags;
259 if (s)
260 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
261 free(s);
262 s = NULL;
263 if (r > -1)
264 (void) asprintf(&s, "\"%s\"", s2);
265 free(s2);
266 return s;
269 size_t ASN1CALL
270 length_HEIM_ANY_SET(const heim_any *data)
272 return data->length;
275 int ASN1CALL
276 copy_HEIM_ANY_SET(const heim_any_set *from, heim_any_set *to)
278 return der_copy_octet_string(from, to);
281 int ASN1CALL
282 HEIM_ANY_cmp(const heim_any_set *p, const heim_any_set *q)
284 return der_heim_octet_string_cmp(p, q);