2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "krb5_locl.h"
36 __RCSID("$Heimdal: data.c 22064 2007-11-11 16:28:14Z lha $"
40 * Reset the (potentially uninitalized) krb5_data structure.
42 * @param p krb5_data to reset.
47 void KRB5_LIB_FUNCTION
48 krb5_data_zero(krb5_data
*p
)
55 * Free the content of krb5_data structure, its ok to free a zeroed
56 * structure. When done, the structure will be zeroed.
58 * @param p krb5_data to free.
63 void KRB5_LIB_FUNCTION
64 krb5_data_free(krb5_data
*p
)
72 * Same as krb5_data_free().
74 * @param context Kerberos 5 context.
75 * @param data krb5_data to free.
80 void KRB5_LIB_FUNCTION
81 krb5_free_data_contents(krb5_context context
, krb5_data
*data
)
87 * Free krb5_data (and its content).
89 * @param context Kerberos 5 context.
90 * @param p krb5_data to free.
95 void KRB5_LIB_FUNCTION
96 krb5_free_data(krb5_context context
,
104 * Allocate data of and krb5_data.
106 * @param p krb5_data to free.
107 * @param len size to allocate.
109 * @return Returns 0 to indicate success. Otherwise an kerberos et
110 * error code is returned.
115 krb5_error_code KRB5_LIB_FUNCTION
116 krb5_data_alloc(krb5_data
*p
, int len
)
118 p
->data
= malloc(len
);
119 if(len
&& p
->data
== NULL
)
126 * Grow (or shrink) the content of krb5_data to a new size.
128 * @param p krb5_data to free.
129 * @param len new size.
131 * @return Returns 0 to indicate success. Otherwise an kerberos et
132 * error code is returned.
137 krb5_error_code KRB5_LIB_FUNCTION
138 krb5_data_realloc(krb5_data
*p
, int len
)
141 tmp
= realloc(p
->data
, len
);
150 * Copy the data of len into the krb5_data.
152 * @param p krb5_data to copy into.
153 * @param data data to copy..
154 * @param len new size.
156 * @return Returns 0 to indicate success. Otherwise an kerberos et
157 * error code is returned.
162 krb5_error_code KRB5_LIB_FUNCTION
163 krb5_data_copy(krb5_data
*p
, const void *data
, size_t len
)
166 if(krb5_data_alloc(p
, len
))
168 memmove(p
->data
, data
, len
);
176 * Copy the data into a newly allocated krb5_data.
178 * @param context Kerberos 5 context.
179 * @param indata the krb5_data data to copy
180 * @param outdata new krb5_date to copy too. Free with krb5_free_data().
182 * @return Returns 0 to indicate success. Otherwise an kerberos et
183 * error code is returned.
188 krb5_error_code KRB5_LIB_FUNCTION
189 krb5_copy_data(krb5_context context
,
190 const krb5_data
*indata
,
195 if(*outdata
== NULL
) {
196 krb5_set_error_string(context
, "malloc: out of memory");
199 ret
= der_copy_octet_string(indata
, *outdata
);
201 krb5_clear_error_string (context
);
211 * @param data1 krb5_data to compare
212 * @param data2 krb5_data to compare
214 * @return return the same way as memcmp(), useful when sorting.
219 int KRB5_LIB_FUNCTION
220 krb5_data_cmp(const krb5_data
*data1
, const krb5_data
*data2
)
222 if (data1
->length
!= data2
->length
)
223 return data1
->length
- data2
->length
;
224 return memcmp(data1
->data
, data2
->data
, data1
->length
);