asn1: Fix extra.c printer functions
[heimdal.git] / lib / asn1 / extra.c
blob9e04723a01d9ff3f8fd8caebcf2bb38c002af91e
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"
40 RCSID("$Id$");
42 int
43 encode_heim_any(unsigned char *p, size_t len,
44 const heim_any *data, size_t *size)
46 return der_put_octet_string (p, len, data, size);
49 int
50 decode_heim_any(const unsigned char *p, size_t len,
51 heim_any *data, size_t *size)
53 size_t len_len, length, l;
54 Der_class thisclass;
55 Der_type thistype;
56 unsigned int thistag;
57 int e;
59 memset(data, 0, sizeof(*data));
61 e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
62 if (e) return e;
63 if (l > len)
64 return ASN1_OVERFLOW;
65 e = der_get_length(p + l, len - l, &length, &len_len);
66 if (e) return e;
67 if (length == ASN1_INDEFINITE) {
68 if (len < len_len + l)
69 return ASN1_OVERFLOW;
70 length = len - (len_len + l);
71 } else {
72 if (len < length + len_len + l)
73 return ASN1_OVERFLOW;
76 data->data = malloc(length + len_len + l);
77 if (data->data == NULL)
78 return ENOMEM;
79 data->length = length + len_len + l;
80 memcpy(data->data, p, length + len_len + l);
82 if (size)
83 *size = length + len_len + l;
85 return 0;
88 void
89 free_heim_any(heim_any *data)
91 der_free_octet_string(data);
94 char *
95 print_heim_any(const heim_any *data, int flags)
97 char *s2 = NULL;
98 char *s = der_print_octet_string(data, 0);
99 int r = -1;
101 (void)flags;
102 if (s)
103 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
104 free(s);
105 s = NULL;
106 if (r > -1)
107 r = asprintf(&s, "\"%s\"", s2);
108 free(s2);
109 return s;
112 size_t
113 length_heim_any(const heim_any *data)
115 return data->length;
119 copy_heim_any(const heim_any *from, heim_any *to)
121 return der_copy_octet_string(from, to);
125 encode_HEIM_ANY(unsigned char *p, size_t len,
126 const heim_any *data, size_t *size)
128 return encode_heim_any(p, len, data, size);
132 decode_HEIM_ANY(const unsigned char *p, size_t len,
133 heim_any *data, size_t *size)
135 return decode_heim_any(p, len, data, size);
138 void
139 free_HEIM_ANY(heim_any *data)
141 der_free_octet_string(data);
144 char *
145 print_HEIM_ANY(const heim_any *data, int flags)
147 char *s2 = NULL;
148 char *s = der_print_octet_string(data, 0);
149 int r = -1;
151 (void)flags;
152 if (s)
153 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
154 free(s);
155 s = NULL;
156 if (r > -1)
157 r = asprintf(&s, "\"%s\"", s2);
158 free(s2);
159 return s;
162 size_t
163 length_HEIM_ANY(const heim_any *data)
165 return data->length;
169 copy_HEIM_ANY(const heim_any *from, heim_any *to)
171 return der_copy_octet_string(from, to);
175 encode_heim_any_set(unsigned char *p, size_t len,
176 const heim_any_set *data, size_t *size)
178 return der_put_octet_string (p, len, data, size);
182 decode_heim_any_set(const unsigned char *p, size_t len,
183 heim_any_set *data, size_t *size)
185 return der_get_octet_string(p, len, data, size);
188 void
189 free_heim_any_set(heim_any_set *data)
191 der_free_octet_string(data);
194 char *
195 print_heim_any_set(const heim_any_set *data, int flags)
197 char *s2 = NULL;
198 char *s = der_print_octet_string(data, 0);
199 int r = -1;
201 (void)flags;
202 if (s)
203 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
204 free(s);
205 s = NULL;
206 if (r > -1)
207 r = asprintf(&s, "\"%s\"", s2);
208 free(s2);
209 return s;
212 size_t
213 length_heim_any_set(const heim_any *data)
215 return data->length;
219 copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
221 return der_copy_octet_string(from, to);
225 heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
227 return der_heim_octet_string_cmp(p, q);
231 encode_HEIM_ANY_SET(unsigned char *p, size_t len,
232 const heim_any_set *data, size_t *size)
234 return encode_heim_any_set(p, len, data, size);
238 decode_HEIM_ANY_SET(const unsigned char *p, size_t len,
239 heim_any_set *data, size_t *size)
241 return decode_heim_any_set(p, len, data, size);
244 void
245 free_HEIM_ANY_SET(heim_any_set *data)
247 der_free_octet_string(data);
250 char *
251 print_HEIM_ANY_SET(const heim_any_set *data, int flags)
253 char *s2 = NULL;
254 char *s = der_print_octet_string(data, 0);
255 int r = -1;
257 (void)flags;
258 if (s)
259 r = rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
260 free(s);
261 s = NULL;
262 if (r > -1)
263 r = asprintf(&s, "\"%s\"", s2);
264 free(s2);
265 return s;
268 size_t
269 length_HEIM_ANY_SET(const heim_any *data)
271 return data->length;
275 copy_HEIM_ANY_SET(const heim_any_set *from, heim_any_set *to)
277 return der_copy_octet_string(from, to);
281 HEIM_ANY_cmp(const heim_any_set *p, const heim_any_set *q)
283 return der_heim_octet_string_cmp(p, q);