/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- krb5_data_zero
- krb5_data_free
- krb5_free_data_contents
- krb5_free_data
- krb5_data_alloc
- krb5_data_realloc
- krb5_data_copy
- krb5_copy_data
- krb5_data_cmp
1 /*
2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include "krb5_locl.h"
35
36 RCSID("$Id$");
37
38 /**
39 * Reset the (potentially uninitalized) krb5_data structure.
40 *
41 * @param p krb5_data to reset.
42 *
43 * @ingroup krb5
44 */
45
46 void KRB5_LIB_FUNCTION
47 krb5_data_zero(krb5_data *p)
/* [<][>][^][v][top][bottom][index][help] */
48 {
49 p->length = 0;
50 p->data = NULL;
51 }
52
53 /**
54 * Free the content of krb5_data structure, its ok to free a zeroed
55 * structure. When done, the structure will be zeroed.
56 *
57 * @param p krb5_data to free.
58 *
59 * @ingroup krb5
60 */
61
62 void KRB5_LIB_FUNCTION
63 krb5_data_free(krb5_data *p)
/* [<][>][^][v][top][bottom][index][help] */
64 {
65 if(p->data != NULL)
66 free(p->data);
67 krb5_data_zero(p);
68 }
69
70 /**
71 * Same as krb5_data_free().
72 *
73 * @param context Kerberos 5 context.
74 * @param data krb5_data to free.
75 *
76 * @ingroup krb5
77 */
78
79 void KRB5_LIB_FUNCTION
80 krb5_free_data_contents(krb5_context context, krb5_data *data)
/* [<][>][^][v][top][bottom][index][help] */
81 {
82 krb5_data_free(data);
83 }
84
85 /**
86 * Free krb5_data (and its content).
87 *
88 * @param context Kerberos 5 context.
89 * @param p krb5_data to free.
90 *
91 * @ingroup krb5
92 */
93
94 void KRB5_LIB_FUNCTION
95 krb5_free_data(krb5_context context,
/* [<][>][^][v][top][bottom][index][help] */
96 krb5_data *p)
97 {
98 krb5_data_free(p);
99 free(p);
100 }
101
102 /**
103 * Allocate data of and krb5_data.
104 *
105 * @param p krb5_data to free.
106 * @param len size to allocate.
107 *
108 * @return Returns 0 to indicate success. Otherwise an kerberos et
109 * error code is returned.
110 *
111 * @ingroup krb5
112 */
113
114 krb5_error_code KRB5_LIB_FUNCTION
115 krb5_data_alloc(krb5_data *p, int len)
/* [<][>][^][v][top][bottom][index][help] */
116 {
117 p->data = malloc(len);
118 if(len && p->data == NULL)
119 return ENOMEM;
120 p->length = len;
121 return 0;
122 }
123
124 /**
125 * Grow (or shrink) the content of krb5_data to a new size.
126 *
127 * @param p krb5_data to free.
128 * @param len new size.
129 *
130 * @return Returns 0 to indicate success. Otherwise an kerberos et
131 * error code is returned.
132 *
133 * @ingroup krb5
134 */
135
136 krb5_error_code KRB5_LIB_FUNCTION
137 krb5_data_realloc(krb5_data *p, int len)
/* [<][>][^][v][top][bottom][index][help] */
138 {
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;
146 }
147
148 /**
149 * Copy the data of len into the krb5_data.
150 *
151 * @param p krb5_data to copy into.
152 * @param data data to copy..
153 * @param len new size.
154 *
155 * @return Returns 0 to indicate success. Otherwise an kerberos et
156 * error code is returned.
157 *
158 * @ingroup krb5
159 */
160
161 krb5_error_code KRB5_LIB_FUNCTION
162 krb5_data_copy(krb5_data *p, const void *data, size_t len)
/* [<][>][^][v][top][bottom][index][help] */
163 {
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;
172 }
173
174 /**
175 * Copy the data into a newly allocated krb5_data.
176 *
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().
180 *
181 * @return Returns 0 to indicate success. Otherwise an kerberos et
182 * error code is returned.
183 *
184 * @ingroup krb5
185 */
186
187 krb5_error_code KRB5_LIB_FUNCTION
188 krb5_copy_data(krb5_context context,
/* [<][>][^][v][top][bottom][index][help] */
189 const krb5_data *indata,
190 krb5_data **outdata)
191 {
192 krb5_error_code ret;
193 ALLOC(*outdata, 1);
194 if(*outdata == NULL) {
195 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
196 return ENOMEM;
197 }
198 ret = der_copy_octet_string(indata, *outdata);
199 if(ret) {
200 krb5_clear_error_message (context);
201 free(*outdata);
202 *outdata = NULL;
203 }
204 return ret;
205 }
206
207 /**
208 * Compare to data.
209 *
210 * @param data1 krb5_data to compare
211 * @param data2 krb5_data to compare
212 *
213 * @return return the same way as memcmp(), useful when sorting.
214 *
215 * @ingroup krb5
216 */
217
218 int KRB5_LIB_FUNCTION
219 krb5_data_cmp(const krb5_data *data1, const krb5_data *data2)
/* [<][>][^][v][top][bottom][index][help] */
220 {
221 if (data1->length != data2->length)
222 return data1->length - data2->length;
223 return memcmp(data1->data, data2->data, data1->length);
224 }