No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / data.c
blob7922800d357f0b0d6aec91d147988aa550fc0028
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("$Heimdal: data.c 22064 2007-11-11 16:28:14Z lha $"
37 "$NetBSD$");
39 /**
40 * Reset the (potentially uninitalized) krb5_data structure.
42 * @param p krb5_data to reset.
44 * @ingroup krb5
47 void KRB5_LIB_FUNCTION
48 krb5_data_zero(krb5_data *p)
50 p->length = 0;
51 p->data = NULL;
54 /**
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.
60 * @ingroup krb5
63 void KRB5_LIB_FUNCTION
64 krb5_data_free(krb5_data *p)
66 if(p->data != NULL)
67 free(p->data);
68 krb5_data_zero(p);
71 /**
72 * Same as krb5_data_free().
74 * @param context Kerberos 5 context.
75 * @param data krb5_data to free.
77 * @ingroup krb5
80 void KRB5_LIB_FUNCTION
81 krb5_free_data_contents(krb5_context context, krb5_data *data)
83 krb5_data_free(data);
86 /**
87 * Free krb5_data (and its content).
89 * @param context Kerberos 5 context.
90 * @param p krb5_data to free.
92 * @ingroup krb5
95 void KRB5_LIB_FUNCTION
96 krb5_free_data(krb5_context context,
97 krb5_data *p)
99 krb5_data_free(p);
100 free(p);
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.
112 * @ingroup krb5
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)
120 return ENOMEM;
121 p->length = len;
122 return 0;
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.
134 * @ingroup krb5
137 krb5_error_code KRB5_LIB_FUNCTION
138 krb5_data_realloc(krb5_data *p, int len)
140 void *tmp;
141 tmp = realloc(p->data, len);
142 if(len && !tmp)
143 return ENOMEM;
144 p->data = tmp;
145 p->length = len;
146 return 0;
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.
159 * @ingroup krb5
162 krb5_error_code KRB5_LIB_FUNCTION
163 krb5_data_copy(krb5_data *p, const void *data, size_t len)
165 if (len) {
166 if(krb5_data_alloc(p, len))
167 return ENOMEM;
168 memmove(p->data, data, len);
169 } else
170 p->data = NULL;
171 p->length = len;
172 return 0;
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.
185 * @ingroup krb5
188 krb5_error_code KRB5_LIB_FUNCTION
189 krb5_copy_data(krb5_context context,
190 const krb5_data *indata,
191 krb5_data **outdata)
193 krb5_error_code ret;
194 ALLOC(*outdata, 1);
195 if(*outdata == NULL) {
196 krb5_set_error_string(context, "malloc: out of memory");
197 return ENOMEM;
199 ret = der_copy_octet_string(indata, *outdata);
200 if(ret) {
201 krb5_clear_error_string (context);
202 free(*outdata);
203 *outdata = NULL;
205 return ret;
209 * Compare to data.
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.
216 * @ingroup krb5
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);