test debug
[heimdal.git] / lib / krb5 / data.c
blob0414fe241be2f48b1d50a7773a0fb5d8946743c5
1 /*
2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 RCSID("$Id$");
38 /**
39 * Reset the (potentially uninitalized) krb5_data structure.
41 * @param p krb5_data to reset.
43 * @ingroup krb5
46 void KRB5_LIB_FUNCTION
47 krb5_data_zero(krb5_data *p)
49 p->length = 0;
50 p->data = NULL;
53 /**
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.
59 * @ingroup krb5
62 void KRB5_LIB_FUNCTION
63 krb5_data_free(krb5_data *p)
65 if(p->data != NULL)
66 free(p->data);
67 krb5_data_zero(p);
70 /**
71 * Same as krb5_data_free().
73 * @param context Kerberos 5 context.
74 * @param data krb5_data to free.
76 * @ingroup krb5
79 void KRB5_LIB_FUNCTION
80 krb5_free_data_contents(krb5_context context, krb5_data *data)
82 krb5_data_free(data);
85 /**
86 * Free krb5_data (and its content).
88 * @param context Kerberos 5 context.
89 * @param p krb5_data to free.
91 * @ingroup krb5
94 void KRB5_LIB_FUNCTION
95 krb5_free_data(krb5_context context,
96 krb5_data *p)
98 krb5_data_free(p);
99 free(p);
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.
111 * @ingroup krb5
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)
119 return ENOMEM;
120 p->length = len;
121 return 0;
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.
133 * @ingroup krb5
136 krb5_error_code KRB5_LIB_FUNCTION
137 krb5_data_realloc(krb5_data *p, int len)
139 void *tmp;
140 tmp = realloc(p->data, len);
141 if(len && !tmp)
142 return ENOMEM;
143 p->data = tmp;
144 p->length = len;
145 return 0;
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.
158 * @ingroup krb5
161 krb5_error_code KRB5_LIB_FUNCTION
162 krb5_data_copy(krb5_data *p, const void *data, size_t len)
164 if (len) {
165 if(krb5_data_alloc(p, len))
166 return ENOMEM;
167 memmove(p->data, data, len);
168 } else
169 p->data = NULL;
170 p->length = len;
171 return 0;
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.
184 * @ingroup krb5
187 krb5_error_code KRB5_LIB_FUNCTION
188 krb5_copy_data(krb5_context context,
189 const krb5_data *indata,
190 krb5_data **outdata)
192 krb5_error_code ret;
193 ALLOC(*outdata, 1);
194 if(*outdata == NULL) {
195 krb5_set_error_string(context, "malloc: out of memory");
196 return ENOMEM;
198 ret = der_copy_octet_string(indata, *outdata);
199 if(ret) {
200 krb5_clear_error_string (context);
201 free(*outdata);
202 *outdata = NULL;
204 return ret;
208 * Compare to data.
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.
215 * @ingroup krb5
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);