utils.c revision 1.16 1 1.16 cbiere /* $NetBSD: utils.c,v 1.16 2007/02/06 00:48:37 cbiere Exp $ */
2 1.1 elric
3 1.1 elric /*-
4 1.2 elric * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5 1.1 elric * All rights reserved.
6 1.1 elric *
7 1.1 elric * This code is derived from software contributed to The NetBSD Foundation
8 1.1 elric * by Roland C. Dowdeswell.
9 1.1 elric *
10 1.1 elric * Redistribution and use in source and binary forms, with or without
11 1.1 elric * modification, are permitted provided that the following conditions
12 1.1 elric * are met:
13 1.1 elric * 1. Redistributions of source code must retain the above copyright
14 1.1 elric * notice, this list of conditions and the following disclaimer.
15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elric * notice, this list of conditions and the following disclaimer in the
17 1.1 elric * documentation and/or other materials provided with the distribution.
18 1.1 elric * 3. All advertising materials mentioning features or use of this software
19 1.1 elric * must display the following acknowledgement:
20 1.1 elric * This product includes software developed by the NetBSD
21 1.1 elric * Foundation, Inc. and its contributors.
22 1.1 elric * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 elric * contributors may be used to endorse or promote products derived
24 1.1 elric * from this software without specific prior written permission.
25 1.1 elric *
26 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 elric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 elric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 elric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 elric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 elric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 elric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 elric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 elric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 elric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 elric * POSSIBILITY OF SUCH DAMAGE.
37 1.1 elric */
38 1.1 elric
39 1.1 elric #include <sys/cdefs.h>
40 1.1 elric #ifndef lint
41 1.16 cbiere __RCSID("$NetBSD: utils.c,v 1.16 2007/02/06 00:48:37 cbiere Exp $");
42 1.1 elric #endif
43 1.1 elric
44 1.2 elric #include <sys/param.h>
45 1.2 elric
46 1.2 elric #include <stdlib.h>
47 1.1 elric #include <string.h>
48 1.6 christos #include <err.h>
49 1.15 christos #include <util.h>
50 1.1 elric
51 1.2 elric /* include the resolver gunk in order that we can use b64 routines */
52 1.2 elric #include <netinet/in.h>
53 1.2 elric #include <arpa/nameser.h>
54 1.2 elric #include <resolv.h>
55 1.2 elric
56 1.1 elric #include "utils.h"
57 1.1 elric
58 1.6 christos
59 1.1 elric /* just strsep(3), but skips empty fields. */
60 1.1 elric
61 1.1 elric static char *
62 1.1 elric strsep_getnext(char **stringp, const char *delim)
63 1.1 elric {
64 1.1 elric char *ret;
65 1.1 elric
66 1.1 elric ret = strsep(stringp, delim);
67 1.1 elric while (ret && index(delim, *ret))
68 1.1 elric ret = strsep(stringp, delim);
69 1.1 elric return ret;
70 1.1 elric }
71 1.1 elric
72 1.1 elric /*
73 1.1 elric * this function returns a dynamically sized char ** of the words
74 1.1 elric * in the line. the caller is responsible for both free(3)ing
75 1.1 elric * each word and the superstructure by calling words_free().
76 1.1 elric */
77 1.1 elric char **
78 1.1 elric words(const char *line, int *num)
79 1.1 elric {
80 1.1 elric int i = 0;
81 1.1 elric int nwords = 0;
82 1.1 elric char *cur;
83 1.1 elric char **ret;
84 1.9 christos const char *tmp;
85 1.9 christos char *tmp1, *tmpf;
86 1.1 elric
87 1.1 elric *num = 0;
88 1.9 christos tmp = line;
89 1.1 elric if (tmp[0] == '\0')
90 1.1 elric return NULL;
91 1.1 elric while (tmp[0]) {
92 1.1 elric if ((tmp[1] == ' ' || tmp[1] == '\t' || tmp[1] == '\0') &&
93 1.1 elric (tmp[0] != ' ' && tmp[0] != '\t'))
94 1.1 elric nwords++;
95 1.1 elric tmp++;
96 1.1 elric }
97 1.6 christos ret = emalloc((nwords+1) * sizeof(char *));
98 1.9 christos tmp1 = tmpf = estrdup(line);
99 1.9 christos while ((cur = strsep_getnext(&tmpf, " \t")) != NULL)
100 1.6 christos ret[i++] = estrdup(cur);
101 1.1 elric ret[i] = NULL;
102 1.1 elric free(tmp1);
103 1.1 elric *num = nwords;
104 1.1 elric return ret;
105 1.1 elric }
106 1.1 elric
107 1.1 elric void
108 1.1 elric words_free(char **w, int num)
109 1.1 elric {
110 1.1 elric int i;
111 1.1 elric
112 1.1 elric for (i=0; i < num; i++)
113 1.1 elric free(w[i]);
114 1.1 elric }
115 1.1 elric
116 1.1 elric /*
117 1.1 elric * this is a simple xor that has the same calling conventions as
118 1.1 elric * memcpy(3).
119 1.1 elric */
120 1.1 elric
121 1.1 elric void
122 1.1 elric memxor(void *res, const void *src, size_t len)
123 1.1 elric {
124 1.1 elric char *r;
125 1.1 elric const char *s;
126 1.16 cbiere size_t i;
127 1.1 elric
128 1.1 elric r = res;
129 1.1 elric s = src;
130 1.16 cbiere for (i = 0; i < len; i++)
131 1.1 elric r[i] ^= s[i];
132 1.2 elric }
133 1.2 elric
134 1.2 elric /*
135 1.2 elric * well, a very simple set of string functions...
136 1.2 elric *
137 1.2 elric * The goal here is basically to manage length encoded strings,
138 1.2 elric * but just for safety we nul terminate them anyway.
139 1.2 elric */
140 1.2 elric
141 1.2 elric /* for now we use a very simple encoding */
142 1.2 elric
143 1.2 elric struct string {
144 1.2 elric int length;
145 1.2 elric char *text;
146 1.2 elric };
147 1.2 elric
148 1.2 elric string_t *
149 1.2 elric string_new(const char *intext, int inlength)
150 1.2 elric {
151 1.2 elric string_t *out;
152 1.2 elric
153 1.6 christos out = emalloc(sizeof(*out));
154 1.2 elric out->length = inlength;
155 1.6 christos out->text = emalloc(out->length + 1);
156 1.2 elric memcpy(out->text, intext, out->length);
157 1.2 elric out->text[out->length] = '\0';
158 1.2 elric return out;
159 1.2 elric }
160 1.2 elric
161 1.2 elric string_t *
162 1.2 elric string_dup(const string_t *in)
163 1.2 elric {
164 1.2 elric
165 1.2 elric return string_new(in->text, in->length);
166 1.2 elric }
167 1.2 elric
168 1.2 elric void
169 1.2 elric string_free(string_t *s)
170 1.2 elric {
171 1.2 elric
172 1.2 elric if (!s)
173 1.2 elric return;
174 1.6 christos free(s->text);
175 1.2 elric free(s);
176 1.2 elric }
177 1.2 elric
178 1.2 elric void
179 1.2 elric string_assign(string_t **lhs, string_t *rhs)
180 1.2 elric {
181 1.2 elric
182 1.2 elric string_free(*lhs);
183 1.2 elric *lhs = rhs;
184 1.2 elric }
185 1.2 elric
186 1.2 elric string_t *
187 1.2 elric string_add(const string_t *a1, const string_t *a2)
188 1.2 elric {
189 1.2 elric string_t *sum;
190 1.2 elric
191 1.6 christos sum = emalloc(sizeof(*sum));
192 1.2 elric sum->length = a1->length + a2->length;
193 1.6 christos sum->text = emalloc(sum->length + 1);
194 1.2 elric memcpy(sum->text, a1->text, a1->length);
195 1.2 elric memcpy(sum->text + a1->length, a2->text, a2->length);
196 1.2 elric sum->text[sum->length] = '\0';
197 1.2 elric return sum;
198 1.2 elric }
199 1.2 elric
200 1.2 elric string_t *
201 1.2 elric string_add_d(string_t *a1, string_t *a2)
202 1.2 elric {
203 1.2 elric string_t *sum;
204 1.2 elric
205 1.2 elric sum = string_add(a1, a2);
206 1.2 elric string_free(a1);
207 1.2 elric string_free(a2);
208 1.2 elric return sum;
209 1.2 elric }
210 1.2 elric
211 1.2 elric string_t *
212 1.2 elric string_fromcharstar(const char *in)
213 1.2 elric {
214 1.2 elric
215 1.2 elric return string_new(in, strlen(in));
216 1.2 elric }
217 1.2 elric
218 1.2 elric const char *
219 1.2 elric string_tocharstar(const string_t *in)
220 1.2 elric {
221 1.2 elric
222 1.2 elric return in->text;
223 1.2 elric }
224 1.2 elric
225 1.2 elric string_t *
226 1.2 elric string_fromint(int in)
227 1.2 elric {
228 1.2 elric string_t *ret;
229 1.2 elric
230 1.6 christos ret = emalloc(sizeof(*ret));
231 1.2 elric ret->length = asprintf(&ret->text, "%d", in);
232 1.6 christos if (ret->length == -1)
233 1.6 christos err(1, NULL);
234 1.2 elric return ret;
235 1.2 elric }
236 1.2 elric
237 1.2 elric void
238 1.2 elric string_fprint(FILE *f, const string_t *s)
239 1.2 elric {
240 1.2 elric
241 1.2 elric fwrite(s->text, s->length, 1, f);
242 1.2 elric }
243 1.2 elric
244 1.2 elric struct bits {
245 1.2 elric int length;
246 1.2 elric char *text;
247 1.2 elric };
248 1.2 elric
249 1.2 elric bits_t *
250 1.2 elric bits_new(const void *buf, int len)
251 1.2 elric {
252 1.2 elric bits_t *b;
253 1.2 elric
254 1.7 elric b = emalloc(sizeof(*b));
255 1.2 elric b->length = len;
256 1.6 christos b->text = emalloc(BITS2BYTES(b->length));
257 1.2 elric memcpy(b->text, buf, BITS2BYTES(b->length));
258 1.2 elric return b;
259 1.2 elric }
260 1.2 elric
261 1.2 elric bits_t *
262 1.2 elric bits_dup(const bits_t *in)
263 1.2 elric {
264 1.2 elric
265 1.2 elric return bits_new(in->text, in->length);
266 1.2 elric }
267 1.2 elric
268 1.2 elric void
269 1.2 elric bits_free(bits_t *b)
270 1.2 elric {
271 1.2 elric
272 1.2 elric if (!b)
273 1.2 elric return;
274 1.6 christos free(b->text);
275 1.2 elric free(b);
276 1.2 elric }
277 1.2 elric
278 1.2 elric void
279 1.2 elric bits_assign(bits_t **lhs, bits_t *rhs)
280 1.2 elric {
281 1.2 elric
282 1.2 elric bits_free(*lhs);
283 1.2 elric *lhs = rhs;
284 1.2 elric }
285 1.2 elric
286 1.2 elric const void *
287 1.2 elric bits_getbuf(bits_t *in)
288 1.2 elric {
289 1.2 elric
290 1.2 elric return in->text;
291 1.2 elric }
292 1.2 elric
293 1.2 elric int
294 1.2 elric bits_len(bits_t *in)
295 1.2 elric {
296 1.2 elric
297 1.2 elric return in->length;
298 1.3 cb }
299 1.3 cb
300 1.3 cb int
301 1.3 cb bits_match(const bits_t *b1, const bits_t *b2)
302 1.3 cb {
303 1.3 cb int i;
304 1.3 cb
305 1.3 cb if (b1->length != b2->length)
306 1.3 cb return 0;
307 1.3 cb
308 1.3 cb for (i = 0; i < BITS2BYTES(b1->length); i++)
309 1.3 cb if (b1->text[i] != b2->text[i])
310 1.3 cb return 0;
311 1.3 cb
312 1.3 cb return 1;
313 1.2 elric }
314 1.2 elric
315 1.2 elric bits_t *
316 1.2 elric bits_xor(const bits_t *x1, const bits_t *x2)
317 1.2 elric {
318 1.2 elric bits_t *b;
319 1.2 elric int i;
320 1.2 elric
321 1.6 christos b = emalloc(sizeof(*b));
322 1.2 elric b->length = MAX(x1->length, x2->length);
323 1.6 christos b->text = ecalloc(1, BITS2BYTES(b->length));
324 1.2 elric for (i=0; i < BITS2BYTES(MIN(x1->length, x2->length)); i++)
325 1.2 elric b->text[i] = x1->text[i] ^ x2->text[i];
326 1.2 elric return b;
327 1.2 elric }
328 1.2 elric
329 1.2 elric bits_t *
330 1.2 elric bits_xor_d(bits_t *x1, bits_t *x2)
331 1.2 elric {
332 1.2 elric bits_t *ret;
333 1.2 elric
334 1.2 elric ret = bits_xor(x1, x2);
335 1.2 elric bits_free(x1);
336 1.2 elric bits_free(x2);
337 1.2 elric return ret;
338 1.2 elric }
339 1.2 elric
340 1.2 elric /*
341 1.2 elric * bits_decode() reads an encoded base64 stream. We interpret
342 1.2 elric * the first 32 bits as an unsigned integer in network byte order
343 1.2 elric * specifying the number of bits in the stream to give a little
344 1.2 elric * resilience.
345 1.2 elric */
346 1.2 elric
347 1.2 elric bits_t *
348 1.2 elric bits_decode(const string_t *in)
349 1.2 elric {
350 1.2 elric bits_t *ret;
351 1.2 elric int len;
352 1.2 elric int nbits;
353 1.14 mrg u_char *tmp;
354 1.2 elric
355 1.2 elric len = in->length;
356 1.6 christos tmp = emalloc(len);
357 1.2 elric
358 1.2 elric len = __b64_pton(in->text, tmp, len);
359 1.2 elric
360 1.2 elric if (len == -1) {
361 1.2 elric fprintf(stderr, "bits_decode: mangled base64 stream\n");
362 1.2 elric fprintf(stderr, " %s\n", in->text);
363 1.12 christos free(tmp);
364 1.2 elric return NULL;
365 1.2 elric }
366 1.2 elric
367 1.2 elric nbits = ntohl(*((u_int32_t *)tmp));
368 1.2 elric if (nbits > (len - 4) * 8) {
369 1.2 elric fprintf(stderr, "bits_decode: encoded bits claim to be "
370 1.2 elric "longer than they are (nbits=%u, stream len=%u bytes)\n",
371 1.2 elric (unsigned)nbits, (unsigned)len);
372 1.12 christos free(tmp);
373 1.2 elric return NULL;
374 1.2 elric }
375 1.2 elric
376 1.2 elric ret = bits_new(tmp+4, nbits);
377 1.2 elric free(tmp);
378 1.2 elric return ret;
379 1.2 elric }
380 1.2 elric
381 1.2 elric bits_t *
382 1.2 elric bits_decode_d(string_t *in)
383 1.2 elric {
384 1.2 elric bits_t *ret;
385 1.2 elric
386 1.2 elric ret = bits_decode(in);
387 1.2 elric string_free(in);
388 1.2 elric return ret;
389 1.2 elric }
390 1.2 elric
391 1.2 elric string_t *
392 1.2 elric bits_encode(const bits_t *in)
393 1.2 elric {
394 1.2 elric string_t *ret;
395 1.2 elric int len;
396 1.2 elric char *out;
397 1.14 mrg u_char *tmp;
398 1.2 elric
399 1.2 elric if (!in)
400 1.2 elric return NULL;
401 1.2 elric
402 1.2 elric /* compute the total size of the input stream */
403 1.2 elric len = BITS2BYTES(in->length) + 4;
404 1.2 elric
405 1.6 christos tmp = emalloc(len);
406 1.6 christos out = emalloc(len * 2);
407 1.2 elric /* stuff the length up front */
408 1.2 elric *((u_int32_t *)tmp) = htonl(in->length);
409 1.2 elric memcpy(tmp + 4, in->text, len - 4);
410 1.2 elric
411 1.10 christos if ((len = __b64_ntop(tmp, len, out, len * 2)) == -1) {
412 1.13 christos free(out);
413 1.10 christos free(tmp);
414 1.10 christos return NULL;
415 1.10 christos }
416 1.2 elric ret = string_new(out, len);
417 1.2 elric free(tmp);
418 1.2 elric free(out);
419 1.2 elric return ret;
420 1.2 elric }
421 1.2 elric
422 1.2 elric string_t *
423 1.2 elric bits_encode_d(bits_t *in)
424 1.2 elric {
425 1.2 elric string_t *ret;
426 1.2 elric
427 1.2 elric ret = bits_encode(in);
428 1.2 elric bits_free(in);
429 1.2 elric return ret;
430 1.2 elric }
431 1.2 elric
432 1.2 elric bits_t *
433 1.2 elric bits_fget(FILE *f, int len)
434 1.2 elric {
435 1.2 elric bits_t *bits;
436 1.2 elric int ret;
437 1.2 elric
438 1.6 christos bits = emalloc(sizeof(*bits));
439 1.2 elric bits->length = len;
440 1.6 christos bits->text = emalloc(BITS2BYTES(bits->length));
441 1.2 elric ret = fread(bits->text, BITS2BYTES(bits->length), 1, f);
442 1.2 elric if (ret != 1) {
443 1.2 elric bits_free(bits);
444 1.2 elric return NULL;
445 1.2 elric }
446 1.2 elric return bits;
447 1.2 elric }
448 1.2 elric
449 1.2 elric bits_t *
450 1.2 elric bits_cget(const char *fn, int len)
451 1.2 elric {
452 1.2 elric bits_t *bits;
453 1.2 elric FILE *f;
454 1.2 elric
455 1.2 elric f = fopen(fn, "r");
456 1.8 lukem if (!f)
457 1.2 elric return NULL;
458 1.2 elric
459 1.2 elric bits = bits_fget(f, len);
460 1.2 elric fclose(f);
461 1.2 elric return bits;
462 1.2 elric }
463 1.2 elric
464 1.2 elric bits_t *
465 1.5 tv bits_getrandombits(int len, int hard)
466 1.2 elric {
467 1.2 elric
468 1.5 tv return bits_cget((hard ? "/dev/random" : "/dev/urandom"), len);
469 1.2 elric }
470 1.2 elric
471 1.2 elric void
472 1.2 elric bits_fprint(FILE *f, const bits_t *bits)
473 1.2 elric {
474 1.2 elric string_t *s;
475 1.2 elric
476 1.2 elric s = bits_encode(bits);
477 1.2 elric string_fprint(f, s);
478 1.2 elric free(s);
479 1.2 elric }
480