1 /* $NetBSD: data.c,v 1.1.1.1 2011/04/13 18:15:33 elric Exp $ */
4 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
36 #include "krb5_locl.h"
39 * Reset the (potentially uninitalized) krb5_data structure.
41 * @param p krb5_data to reset.
46 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
47 krb5_data_zero(krb5_data
*p
)
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.
59 * @param p krb5_data to free.
64 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
65 krb5_data_free(krb5_data
*p
)
73 * Free krb5_data (and its content).
75 * @param context Kerberos 5 context.
76 * @param p krb5_data to free.
81 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
82 krb5_free_data(krb5_context context
,
90 * Allocate data of and krb5_data.
92 * @param p krb5_data to allocate.
93 * @param len size to allocate.
95 * @return Returns 0 to indicate success. Otherwise an kerberos et
96 * error code is returned.
101 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
102 krb5_data_alloc(krb5_data
*p
, int len
)
104 p
->data
= malloc(len
);
105 if(len
&& p
->data
== NULL
)
112 * Grow (or shrink) the content of krb5_data to a new size.
114 * @param p krb5_data to free.
115 * @param len new size.
117 * @return Returns 0 to indicate success. Otherwise an kerberos et
118 * error code is returned.
123 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
124 krb5_data_realloc(krb5_data
*p
, int len
)
127 tmp
= realloc(p
->data
, len
);
136 * Copy the data of len into the krb5_data.
138 * @param p krb5_data to copy into.
139 * @param data data to copy..
140 * @param len new size.
142 * @return Returns 0 to indicate success. Otherwise an kerberos et
143 * error code is returned.
148 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
149 krb5_data_copy(krb5_data
*p
, const void *data
, size_t len
)
152 if(krb5_data_alloc(p
, len
))
154 memmove(p
->data
, data
, len
);
162 * Copy the data into a newly allocated krb5_data.
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().
168 * @return Returns 0 to indicate success. Otherwise an kerberos et
169 * error code is returned.
174 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
175 krb5_copy_data(krb5_context context
,
176 const krb5_data
*indata
,
181 if(*outdata
== NULL
) {
182 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
185 ret
= der_copy_octet_string(indata
, *outdata
);
187 krb5_clear_error_message (context
);
197 * @param data1 krb5_data to compare
198 * @param data2 krb5_data to compare
200 * @return return the same way as memcmp(), useful when sorting.
205 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
206 krb5_data_cmp(const krb5_data
*data1
, const krb5_data
*data2
)
208 if (data1
->length
!= data2
->length
)
209 return data1
->length
- data2
->length
;
210 return memcmp(data1
->data
, data2
->data
, data1
->length
);
214 * Compare to data not exposing timing information from the checksum data
216 * @param data1 krb5_data to compare
217 * @param data2 krb5_data to compare
219 * @return returns zero for same data, otherwise non zero.
224 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
225 krb5_data_ct_cmp(const krb5_data
*data1
, const krb5_data
*data2
)
227 if (data1
->length
!= data2
->length
)
228 return data1
->length
- data2
->length
;
229 return ct_memcmp(data1
->data
, data2
->data
, data1
->length
);