1/*	$NetBSD$	*/
2
3/*
4 * Copyright (c) 1997 - 2007 Kungliga Tekniska H��gskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
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.
18 *
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.
22 *
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.
34 */
35
36#include "krb5_locl.h"
37
38/**
39 * Reset the (potentially uninitalized) krb5_data structure.
40 *
41 * @param p krb5_data to reset.
42 *
43 * @ingroup krb5
44 */
45
46KRB5_LIB_FUNCTION void KRB5_LIB_CALL
47krb5_data_zero(krb5_data *p)
48{
49    p->length = 0;
50    p->data   = NULL;
51}
52
53/**
54 * Free the content of krb5_data structure, its ok to free a zeroed
55 * structure (with memset() or krb5_data_zero()). When done, the
56 * structure will be zeroed. The same function is called
57 * krb5_free_data_contents() in MIT Kerberos.
58 *
59 * @param p krb5_data to free.
60 *
61 * @ingroup krb5
62 */
63
64KRB5_LIB_FUNCTION void KRB5_LIB_CALL
65krb5_data_free(krb5_data *p)
66{
67    if(p->data != NULL)
68	free(p->data);
69    krb5_data_zero(p);
70}
71
72/**
73 * Free krb5_data (and its content).
74 *
75 * @param context Kerberos 5 context.
76 * @param p krb5_data to free.
77 *
78 * @ingroup krb5
79 */
80
81KRB5_LIB_FUNCTION void KRB5_LIB_CALL
82krb5_free_data(krb5_context context,
83	       krb5_data *p)
84{
85    krb5_data_free(p);
86    free(p);
87}
88
89/**
90 * Allocate data of and krb5_data.
91 *
92 * @param p krb5_data to allocate.
93 * @param len size to allocate.
94 *
95 * @return Returns 0 to indicate success. Otherwise an kerberos et
96 * error code is returned.
97 *
98 * @ingroup krb5
99 */
100
101KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
102krb5_data_alloc(krb5_data *p, int len)
103{
104    p->data = malloc(len);
105    if(len && p->data == NULL)
106	return ENOMEM;
107    p->length = len;
108    return 0;
109}
110
111/**
112 * Grow (or shrink) the content of krb5_data to a new size.
113 *
114 * @param p krb5_data to free.
115 * @param len new size.
116 *
117 * @return Returns 0 to indicate success. Otherwise an kerberos et
118 * error code is returned.
119 *
120 * @ingroup krb5
121 */
122
123KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
124krb5_data_realloc(krb5_data *p, int len)
125{
126    void *tmp;
127    tmp = realloc(p->data, len);
128    if(len && !tmp)
129	return ENOMEM;
130    p->data = tmp;
131    p->length = len;
132    return 0;
133}
134
135/**
136 * Copy the data of len into the krb5_data.
137 *
138 * @param p krb5_data to copy into.
139 * @param data data to copy..
140 * @param len new size.
141 *
142 * @return Returns 0 to indicate success. Otherwise an kerberos et
143 * error code is returned.
144 *
145 * @ingroup krb5
146 */
147
148KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
149krb5_data_copy(krb5_data *p, const void *data, size_t len)
150{
151    if (len) {
152	if(krb5_data_alloc(p, len))
153	    return ENOMEM;
154	memmove(p->data, data, len);
155    } else
156	p->data = NULL;
157    p->length = len;
158    return 0;
159}
160
161/**
162 * Copy the data into a newly allocated krb5_data.
163 *
164 * @param context Kerberos 5 context.
165 * @param indata the krb5_data data to copy
166 * @param outdata new krb5_date to copy too. Free with krb5_free_data().
167 *
168 * @return Returns 0 to indicate success. Otherwise an kerberos et
169 * error code is returned.
170 *
171 * @ingroup krb5
172 */
173
174KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
175krb5_copy_data(krb5_context context,
176	       const krb5_data *indata,
177	       krb5_data **outdata)
178{
179    krb5_error_code ret;
180    ALLOC(*outdata, 1);
181    if(*outdata == NULL) {
182	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
183	return ENOMEM;
184    }
185    ret = der_copy_octet_string(indata, *outdata);
186    if(ret) {
187	krb5_clear_error_message (context);
188	free(*outdata);
189	*outdata = NULL;
190    }
191    return ret;
192}
193
194/**
195 * Compare to data.
196 *
197 * @param data1 krb5_data to compare
198 * @param data2 krb5_data to compare
199 *
200 * @return return the same way as memcmp(), useful when sorting.
201 *
202 * @ingroup krb5
203 */
204
205KRB5_LIB_FUNCTION int KRB5_LIB_CALL
206krb5_data_cmp(const krb5_data *data1, const krb5_data *data2)
207{
208    if (data1->length != data2->length)
209	return data1->length - data2->length;
210    return memcmp(data1->data, data2->data, data1->length);
211}
212
213/**
214 * Compare to data not exposing timing information from the checksum data
215 *
216 * @param data1 krb5_data to compare
217 * @param data2 krb5_data to compare
218 *
219 * @return returns zero for same data, otherwise non zero.
220 *
221 * @ingroup krb5
222 */
223
224KRB5_LIB_FUNCTION int KRB5_LIB_CALL
225krb5_data_ct_cmp(const krb5_data *data1, const krb5_data *data2)
226{
227    if (data1->length != data2->length)
228	return data1->length - data2->length;
229    return ct_memcmp(data1->data, data2->data, data1->length);
230}
231