utils.c revision 1.12 1 1.12 christos /* $NetBSD: utils.c,v 1.12 2006/03/22 15:45:16 christos 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.12 christos __RCSID("$NetBSD: utils.c,v 1.12 2006/03/22 15:45:16 christos 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.1 elric
50 1.2 elric /* include the resolver gunk in order that we can use b64 routines */
51 1.2 elric #include <netinet/in.h>
52 1.2 elric #include <arpa/nameser.h>
53 1.2 elric #include <resolv.h>
54 1.2 elric
55 1.1 elric #include "utils.h"
56 1.1 elric
57 1.6 christos
58 1.6 christos void *
59 1.6 christos emalloc(size_t len)
60 1.6 christos {
61 1.6 christos void *ptr = malloc(len);
62 1.6 christos if (ptr == NULL)
63 1.6 christos err(1, NULL);
64 1.6 christos return ptr;
65 1.6 christos }
66 1.6 christos
67 1.6 christos void *
68 1.6 christos ecalloc(size_t nel, size_t len)
69 1.6 christos {
70 1.6 christos void *ptr = calloc(nel, len);
71 1.6 christos if (ptr == NULL)
72 1.6 christos err(1, NULL);
73 1.6 christos return ptr;
74 1.6 christos }
75 1.6 christos
76 1.6 christos char *
77 1.6 christos estrdup(const char *str)
78 1.6 christos {
79 1.6 christos char *ptr = strdup(str);
80 1.6 christos if (ptr == NULL)
81 1.6 christos err(1, NULL);
82 1.6 christos return ptr;
83 1.6 christos }
84 1.6 christos
85 1.1 elric /* just strsep(3), but skips empty fields. */
86 1.1 elric
87 1.1 elric static char *
88 1.1 elric strsep_getnext(char **stringp, const char *delim)
89 1.1 elric {
90 1.1 elric char *ret;
91 1.1 elric
92 1.1 elric ret = strsep(stringp, delim);
93 1.1 elric while (ret && index(delim, *ret))
94 1.1 elric ret = strsep(stringp, delim);
95 1.1 elric return ret;
96 1.1 elric }
97 1.1 elric
98 1.1 elric /*
99 1.1 elric * this function returns a dynamically sized char ** of the words
100 1.1 elric * in the line. the caller is responsible for both free(3)ing
101 1.1 elric * each word and the superstructure by calling words_free().
102 1.1 elric */
103 1.1 elric char **
104 1.1 elric words(const char *line, int *num)
105 1.1 elric {
106 1.1 elric int i = 0;
107 1.1 elric int nwords = 0;
108 1.1 elric char *cur;
109 1.1 elric char **ret;
110 1.9 christos const char *tmp;
111 1.9 christos char *tmp1, *tmpf;
112 1.1 elric
113 1.1 elric *num = 0;
114 1.9 christos tmp = line;
115 1.1 elric if (tmp[0] == '\0')
116 1.1 elric return NULL;
117 1.1 elric while (tmp[0]) {
118 1.1 elric if ((tmp[1] == ' ' || tmp[1] == '\t' || tmp[1] == '\0') &&
119 1.1 elric (tmp[0] != ' ' && tmp[0] != '\t'))
120 1.1 elric nwords++;
121 1.1 elric tmp++;
122 1.1 elric }
123 1.6 christos ret = emalloc((nwords+1) * sizeof(char *));
124 1.9 christos tmp1 = tmpf = estrdup(line);
125 1.9 christos while ((cur = strsep_getnext(&tmpf, " \t")) != NULL)
126 1.6 christos ret[i++] = estrdup(cur);
127 1.1 elric ret[i] = NULL;
128 1.1 elric free(tmp1);
129 1.1 elric *num = nwords;
130 1.1 elric return ret;
131 1.1 elric }
132 1.1 elric
133 1.1 elric void
134 1.1 elric words_free(char **w, int num)
135 1.1 elric {
136 1.1 elric int i;
137 1.1 elric
138 1.1 elric for (i=0; i < num; i++)
139 1.1 elric free(w[i]);
140 1.1 elric }
141 1.1 elric
142 1.1 elric /*
143 1.1 elric * this is a simple xor that has the same calling conventions as
144 1.1 elric * memcpy(3).
145 1.1 elric */
146 1.1 elric
147 1.1 elric void
148 1.1 elric memxor(void *res, const void *src, size_t len)
149 1.1 elric {
150 1.1 elric int i;
151 1.1 elric char *r;
152 1.1 elric const char *s;
153 1.1 elric
154 1.1 elric r = res;
155 1.1 elric s = src;
156 1.1 elric for (i=0; i < len; i++)
157 1.1 elric r[i] ^= s[i];
158 1.2 elric }
159 1.2 elric
160 1.2 elric /*
161 1.2 elric * well, a very simple set of string functions...
162 1.2 elric *
163 1.2 elric * The goal here is basically to manage length encoded strings,
164 1.2 elric * but just for safety we nul terminate them anyway.
165 1.2 elric */
166 1.2 elric
167 1.2 elric /* for now we use a very simple encoding */
168 1.2 elric
169 1.2 elric struct string {
170 1.2 elric int length;
171 1.2 elric char *text;
172 1.2 elric };
173 1.2 elric
174 1.2 elric string_t *
175 1.2 elric string_new(const char *intext, int inlength)
176 1.2 elric {
177 1.2 elric string_t *out;
178 1.2 elric
179 1.6 christos out = emalloc(sizeof(*out));
180 1.2 elric out->length = inlength;
181 1.6 christos out->text = emalloc(out->length + 1);
182 1.2 elric memcpy(out->text, intext, out->length);
183 1.2 elric out->text[out->length] = '\0';
184 1.2 elric return out;
185 1.2 elric }
186 1.2 elric
187 1.2 elric string_t *
188 1.2 elric string_dup(const string_t *in)
189 1.2 elric {
190 1.2 elric
191 1.2 elric return string_new(in->text, in->length);
192 1.2 elric }
193 1.2 elric
194 1.2 elric void
195 1.2 elric string_free(string_t *s)
196 1.2 elric {
197 1.2 elric
198 1.2 elric if (!s)
199 1.2 elric return;
200 1.6 christos free(s->text);
201 1.2 elric free(s);
202 1.2 elric }
203 1.2 elric
204 1.2 elric void
205 1.2 elric string_assign(string_t **lhs, string_t *rhs)
206 1.2 elric {
207 1.2 elric
208 1.2 elric string_free(*lhs);
209 1.2 elric *lhs = rhs;
210 1.2 elric }
211 1.2 elric
212 1.2 elric string_t *
213 1.2 elric string_add(const string_t *a1, const string_t *a2)
214 1.2 elric {
215 1.2 elric string_t *sum;
216 1.2 elric
217 1.6 christos sum = emalloc(sizeof(*sum));
218 1.2 elric sum->length = a1->length + a2->length;
219 1.6 christos sum->text = emalloc(sum->length + 1);
220 1.2 elric memcpy(sum->text, a1->text, a1->length);
221 1.2 elric memcpy(sum->text + a1->length, a2->text, a2->length);
222 1.2 elric sum->text[sum->length] = '\0';
223 1.2 elric return sum;
224 1.2 elric }
225 1.2 elric
226 1.2 elric string_t *
227 1.2 elric string_add_d(string_t *a1, string_t *a2)
228 1.2 elric {
229 1.2 elric string_t *sum;
230 1.2 elric
231 1.2 elric sum = string_add(a1, a2);
232 1.2 elric string_free(a1);
233 1.2 elric string_free(a2);
234 1.2 elric return sum;
235 1.2 elric }
236 1.2 elric
237 1.2 elric string_t *
238 1.2 elric string_fromcharstar(const char *in)
239 1.2 elric {
240 1.2 elric
241 1.2 elric return string_new(in, strlen(in));
242 1.2 elric }
243 1.2 elric
244 1.2 elric const char *
245 1.2 elric string_tocharstar(const string_t *in)
246 1.2 elric {
247 1.2 elric
248 1.2 elric return in->text;
249 1.2 elric }
250 1.2 elric
251 1.2 elric string_t *
252 1.2 elric string_fromint(int in)
253 1.2 elric {
254 1.2 elric string_t *ret;
255 1.2 elric
256 1.6 christos ret = emalloc(sizeof(*ret));
257 1.2 elric ret->length = asprintf(&ret->text, "%d", in);
258 1.6 christos if (ret->length == -1)
259 1.6 christos err(1, NULL);
260 1.2 elric return ret;
261 1.2 elric }
262 1.2 elric
263 1.2 elric void
264 1.2 elric string_fprint(FILE *f, const string_t *s)
265 1.2 elric {
266 1.2 elric
267 1.2 elric fwrite(s->text, s->length, 1, f);
268 1.2 elric }
269 1.2 elric
270 1.2 elric struct bits {
271 1.2 elric int length;
272 1.2 elric char *text;
273 1.2 elric };
274 1.2 elric
275 1.2 elric bits_t *
276 1.2 elric bits_new(const void *buf, int len)
277 1.2 elric {
278 1.2 elric bits_t *b;
279 1.2 elric
280 1.7 elric b = emalloc(sizeof(*b));
281 1.2 elric b->length = len;
282 1.6 christos b->text = emalloc(BITS2BYTES(b->length));
283 1.2 elric memcpy(b->text, buf, BITS2BYTES(b->length));
284 1.2 elric return b;
285 1.2 elric }
286 1.2 elric
287 1.2 elric bits_t *
288 1.2 elric bits_dup(const bits_t *in)
289 1.2 elric {
290 1.2 elric
291 1.2 elric return bits_new(in->text, in->length);
292 1.2 elric }
293 1.2 elric
294 1.2 elric void
295 1.2 elric bits_free(bits_t *b)
296 1.2 elric {
297 1.2 elric
298 1.2 elric if (!b)
299 1.2 elric return;
300 1.6 christos free(b->text);
301 1.2 elric free(b);
302 1.2 elric }
303 1.2 elric
304 1.2 elric void
305 1.2 elric bits_assign(bits_t **lhs, bits_t *rhs)
306 1.2 elric {
307 1.2 elric
308 1.2 elric bits_free(*lhs);
309 1.2 elric *lhs = rhs;
310 1.2 elric }
311 1.2 elric
312 1.2 elric const void *
313 1.2 elric bits_getbuf(bits_t *in)
314 1.2 elric {
315 1.2 elric
316 1.2 elric return in->text;
317 1.2 elric }
318 1.2 elric
319 1.2 elric int
320 1.2 elric bits_len(bits_t *in)
321 1.2 elric {
322 1.2 elric
323 1.2 elric return in->length;
324 1.3 cb }
325 1.3 cb
326 1.3 cb int
327 1.3 cb bits_match(const bits_t *b1, const bits_t *b2)
328 1.3 cb {
329 1.3 cb int i;
330 1.3 cb
331 1.3 cb if (b1->length != b2->length)
332 1.3 cb return 0;
333 1.3 cb
334 1.3 cb for (i = 0; i < BITS2BYTES(b1->length); i++)
335 1.3 cb if (b1->text[i] != b2->text[i])
336 1.3 cb return 0;
337 1.3 cb
338 1.3 cb return 1;
339 1.2 elric }
340 1.2 elric
341 1.2 elric bits_t *
342 1.2 elric bits_xor(const bits_t *x1, const bits_t *x2)
343 1.2 elric {
344 1.2 elric bits_t *b;
345 1.2 elric int i;
346 1.2 elric
347 1.6 christos b = emalloc(sizeof(*b));
348 1.2 elric b->length = MAX(x1->length, x2->length);
349 1.6 christos b->text = ecalloc(1, BITS2BYTES(b->length));
350 1.2 elric for (i=0; i < BITS2BYTES(MIN(x1->length, x2->length)); i++)
351 1.2 elric b->text[i] = x1->text[i] ^ x2->text[i];
352 1.2 elric return b;
353 1.2 elric }
354 1.2 elric
355 1.2 elric bits_t *
356 1.2 elric bits_xor_d(bits_t *x1, bits_t *x2)
357 1.2 elric {
358 1.2 elric bits_t *ret;
359 1.2 elric
360 1.2 elric ret = bits_xor(x1, x2);
361 1.2 elric bits_free(x1);
362 1.2 elric bits_free(x2);
363 1.2 elric return ret;
364 1.2 elric }
365 1.2 elric
366 1.2 elric /*
367 1.2 elric * bits_decode() reads an encoded base64 stream. We interpret
368 1.2 elric * the first 32 bits as an unsigned integer in network byte order
369 1.2 elric * specifying the number of bits in the stream to give a little
370 1.2 elric * resilience.
371 1.2 elric */
372 1.2 elric
373 1.2 elric bits_t *
374 1.2 elric bits_decode(const string_t *in)
375 1.2 elric {
376 1.2 elric bits_t *ret;
377 1.2 elric int len;
378 1.2 elric int nbits;
379 1.2 elric char *tmp;
380 1.2 elric
381 1.2 elric len = in->length;
382 1.6 christos tmp = emalloc(len);
383 1.2 elric
384 1.2 elric len = __b64_pton(in->text, tmp, len);
385 1.2 elric
386 1.2 elric if (len == -1) {
387 1.2 elric fprintf(stderr, "bits_decode: mangled base64 stream\n");
388 1.2 elric fprintf(stderr, " %s\n", in->text);
389 1.12 christos free(tmp);
390 1.2 elric return NULL;
391 1.2 elric }
392 1.2 elric
393 1.2 elric nbits = ntohl(*((u_int32_t *)tmp));
394 1.2 elric if (nbits > (len - 4) * 8) {
395 1.2 elric fprintf(stderr, "bits_decode: encoded bits claim to be "
396 1.2 elric "longer than they are (nbits=%u, stream len=%u bytes)\n",
397 1.2 elric (unsigned)nbits, (unsigned)len);
398 1.12 christos free(tmp);
399 1.2 elric return NULL;
400 1.2 elric }
401 1.2 elric
402 1.2 elric ret = bits_new(tmp+4, nbits);
403 1.2 elric free(tmp);
404 1.2 elric return ret;
405 1.2 elric }
406 1.2 elric
407 1.2 elric bits_t *
408 1.2 elric bits_decode_d(string_t *in)
409 1.2 elric {
410 1.2 elric bits_t *ret;
411 1.2 elric
412 1.2 elric ret = bits_decode(in);
413 1.2 elric string_free(in);
414 1.2 elric return ret;
415 1.2 elric }
416 1.2 elric
417 1.2 elric string_t *
418 1.2 elric bits_encode(const bits_t *in)
419 1.2 elric {
420 1.2 elric string_t *ret;
421 1.2 elric int len;
422 1.2 elric char *out;
423 1.2 elric char *tmp;
424 1.2 elric
425 1.2 elric if (!in)
426 1.2 elric return NULL;
427 1.2 elric
428 1.2 elric /* compute the total size of the input stream */
429 1.2 elric len = BITS2BYTES(in->length) + 4;
430 1.2 elric
431 1.6 christos tmp = emalloc(len);
432 1.6 christos out = emalloc(len * 2);
433 1.2 elric /* stuff the length up front */
434 1.2 elric *((u_int32_t *)tmp) = htonl(in->length);
435 1.2 elric memcpy(tmp + 4, in->text, len - 4);
436 1.2 elric
437 1.10 christos if ((len = __b64_ntop(tmp, len, out, len * 2)) == -1) {
438 1.10 christos free(tmp);
439 1.10 christos free(malloc);
440 1.10 christos return NULL;
441 1.10 christos }
442 1.2 elric ret = string_new(out, len);
443 1.2 elric free(tmp);
444 1.2 elric free(out);
445 1.2 elric return ret;
446 1.2 elric }
447 1.2 elric
448 1.2 elric string_t *
449 1.2 elric bits_encode_d(bits_t *in)
450 1.2 elric {
451 1.2 elric string_t *ret;
452 1.2 elric
453 1.2 elric ret = bits_encode(in);
454 1.2 elric bits_free(in);
455 1.2 elric return ret;
456 1.2 elric }
457 1.2 elric
458 1.2 elric bits_t *
459 1.2 elric bits_fget(FILE *f, int len)
460 1.2 elric {
461 1.2 elric bits_t *bits;
462 1.2 elric int ret;
463 1.2 elric
464 1.6 christos bits = emalloc(sizeof(*bits));
465 1.2 elric bits->length = len;
466 1.6 christos bits->text = emalloc(BITS2BYTES(bits->length));
467 1.2 elric ret = fread(bits->text, BITS2BYTES(bits->length), 1, f);
468 1.2 elric if (ret != 1) {
469 1.2 elric bits_free(bits);
470 1.2 elric return NULL;
471 1.2 elric }
472 1.2 elric return bits;
473 1.2 elric }
474 1.2 elric
475 1.2 elric bits_t *
476 1.2 elric bits_cget(const char *fn, int len)
477 1.2 elric {
478 1.2 elric bits_t *bits;
479 1.2 elric FILE *f;
480 1.2 elric
481 1.2 elric f = fopen(fn, "r");
482 1.8 lukem if (!f)
483 1.2 elric return NULL;
484 1.2 elric
485 1.2 elric bits = bits_fget(f, len);
486 1.2 elric fclose(f);
487 1.2 elric return bits;
488 1.2 elric }
489 1.2 elric
490 1.2 elric bits_t *
491 1.5 tv bits_getrandombits(int len, int hard)
492 1.2 elric {
493 1.2 elric
494 1.5 tv return bits_cget((hard ? "/dev/random" : "/dev/urandom"), len);
495 1.2 elric }
496 1.2 elric
497 1.2 elric void
498 1.2 elric bits_fprint(FILE *f, const bits_t *bits)
499 1.2 elric {
500 1.2 elric string_t *s;
501 1.2 elric
502 1.2 elric s = bits_encode(bits);
503 1.2 elric string_fprint(f, s);
504 1.2 elric free(s);
505 1.2 elric }
506