etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / krb5 / data.c
blobbeffe6fc6380f942542d2eeaaab4deea0320aaec
1 /* $NetBSD: data.c,v 1.1.1.1 2011/04/13 18:15:33 elric Exp $ */
3 /*
4 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "krb5_locl.h"
38 /**
39 * Reset the (potentially uninitalized) krb5_data structure.
41 * @param p krb5_data to reset.
43 * @ingroup krb5
46 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
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 (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.
61 * @ingroup krb5
64 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
65 krb5_data_free(krb5_data *p)
67 if(p->data != NULL)
68 free(p->data);
69 krb5_data_zero(p);
72 /**
73 * Free krb5_data (and its content).
75 * @param context Kerberos 5 context.
76 * @param p krb5_data to free.
78 * @ingroup krb5
81 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
82 krb5_free_data(krb5_context context,
83 krb5_data *p)
85 krb5_data_free(p);
86 free(p);
89 /**
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.
98 * @ingroup krb5
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)
106 return ENOMEM;
107 p->length = len;
108 return 0;
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.
120 * @ingroup krb5
123 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
124 krb5_data_realloc(krb5_data *p, int len)
126 void *tmp;
127 tmp = realloc(p->data, len);
128 if(len && !tmp)
129 return ENOMEM;
130 p->data = tmp;
131 p->length = len;
132 return 0;
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.
145 * @ingroup krb5
148 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
149 krb5_data_copy(krb5_data *p, const void *data, size_t len)
151 if (len) {
152 if(krb5_data_alloc(p, len))
153 return ENOMEM;
154 memmove(p->data, data, len);
155 } else
156 p->data = NULL;
157 p->length = len;
158 return 0;
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.
171 * @ingroup krb5
174 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
175 krb5_copy_data(krb5_context context,
176 const krb5_data *indata,
177 krb5_data **outdata)
179 krb5_error_code ret;
180 ALLOC(*outdata, 1);
181 if(*outdata == NULL) {
182 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
183 return ENOMEM;
185 ret = der_copy_octet_string(indata, *outdata);
186 if(ret) {
187 krb5_clear_error_message (context);
188 free(*outdata);
189 *outdata = NULL;
191 return ret;
195 * Compare to data.
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.
202 * @ingroup krb5
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.
221 * @ingroup krb5
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);