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("$Id: data.c 22064 2007-11-11 16:28:14Z lha $");
39 * Reset the (potentially uninitalized) krb5_data structure.
41 * @param p krb5_data to reset.
46 void KRB5_LIB_FUNCTION
47 krb5_data_zero(krb5_data *p)
54 * Free the content of krb5_data structure, its ok to free a zeroed
55 * structure. When done, the structure will be zeroed.
57 * @param p krb5_data to free.
62 void KRB5_LIB_FUNCTION
63 krb5_data_free(krb5_data *p)
71 * Same as krb5_data_free().
73 * @param context Kerberos 5 context.
74 * @param data krb5_data to free.
79 void KRB5_LIB_FUNCTION
80 krb5_free_data_contents(krb5_context context, krb5_data *data)
86 * Free krb5_data (and its content).
88 * @param context Kerberos 5 context.
89 * @param p krb5_data to free.
94 void KRB5_LIB_FUNCTION
95 krb5_free_data(krb5_context context,
103 * Allocate data of and krb5_data.
105 * @param p krb5_data to free.
106 * @param len size to allocate.
108 * @return Returns 0 to indicate success. Otherwise an kerberos et
109 * error code is returned.
114 krb5_error_code KRB5_LIB_FUNCTION
115 krb5_data_alloc(krb5_data *p, int len)
117 p->data = malloc(len);
118 if(len && p->data == NULL)
125 * Grow (or shrink) the content of krb5_data to a new size.
127 * @param p krb5_data to free.
128 * @param len new size.
130 * @return Returns 0 to indicate success. Otherwise an kerberos et
131 * error code is returned.
136 krb5_error_code KRB5_LIB_FUNCTION
137 krb5_data_realloc(krb5_data *p, int len)
140 tmp = realloc(p->data, len);
149 * Copy the data of len into the krb5_data.
151 * @param p krb5_data to copy into.
152 * @param data data to copy..
153 * @param len new size.
155 * @return Returns 0 to indicate success. Otherwise an kerberos et
156 * error code is returned.
161 krb5_error_code KRB5_LIB_FUNCTION
162 krb5_data_copy(krb5_data *p, const void *data, size_t len)
165 if(krb5_data_alloc(p, len))
167 memmove(p->data, data, len);
175 * Copy the data into a newly allocated krb5_data.
177 * @param context Kerberos 5 context.
178 * @param indata the krb5_data data to copy
179 * @param outdata new krb5_date to copy too. Free with krb5_free_data().
181 * @return Returns 0 to indicate success. Otherwise an kerberos et
182 * error code is returned.
187 krb5_error_code KRB5_LIB_FUNCTION
188 krb5_copy_data(krb5_context context,
189 const krb5_data *indata,
194 if(*outdata == NULL) {
195 krb5_set_error_string(context, "malloc: out of memory");
198 ret = der_copy_octet_string(indata, *outdata);
200 krb5_clear_error_string (context);
210 * @param data1 krb5_data to compare
211 * @param data2 krb5_data to compare
213 * @return return the same way as memcmp(), useful when sorting.
218 int KRB5_LIB_FUNCTION
219 krb5_data_cmp(const krb5_data *data1, const krb5_data *data2)
221 if (data1->length != data2->length)
222 return data1->length - data2->length;
223 return memcmp(data1->data, data2->data, data1->length);