2007-02-06 03:48:37 +03:00
|
|
|
/* $NetBSD: utils.c,v 1.16 2007/02/06 00:48:37 cbiere Exp $ */
|
2002-10-04 22:37:19 +04:00
|
|
|
|
|
|
|
/*-
|
2003-03-24 05:02:49 +03:00
|
|
|
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
|
2002-10-04 22:37:19 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Roland C. Dowdeswell.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2007-02-06 03:48:37 +03:00
|
|
|
__RCSID("$NetBSD: utils.c,v 1.16 2007/02/06 00:48:37 cbiere Exp $");
|
2002-10-04 22:37:19 +04:00
|
|
|
#endif
|
|
|
|
|
2003-03-24 05:02:49 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2002-10-04 22:37:19 +04:00
|
|
|
#include <string.h>
|
2005-03-30 21:10:18 +04:00
|
|
|
#include <err.h>
|
2006-08-26 22:14:28 +04:00
|
|
|
#include <util.h>
|
2002-10-04 22:37:19 +04:00
|
|
|
|
2003-03-24 05:02:49 +03:00
|
|
|
/* include the resolver gunk in order that we can use b64 routines */
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
|
2002-10-04 22:37:19 +04:00
|
|
|
#include "utils.h"
|
|
|
|
|
2005-03-30 21:10:18 +04:00
|
|
|
|
2002-10-04 22:37:19 +04:00
|
|
|
/* just strsep(3), but skips empty fields. */
|
|
|
|
|
|
|
|
static char *
|
|
|
|
strsep_getnext(char **stringp, const char *delim)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
ret = strsep(stringp, delim);
|
|
|
|
while (ret && index(delim, *ret))
|
|
|
|
ret = strsep(stringp, delim);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this function returns a dynamically sized char ** of the words
|
|
|
|
* in the line. the caller is responsible for both free(3)ing
|
|
|
|
* each word and the superstructure by calling words_free().
|
|
|
|
*/
|
|
|
|
char **
|
|
|
|
words(const char *line, int *num)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int nwords = 0;
|
|
|
|
char *cur;
|
|
|
|
char **ret;
|
2005-06-27 07:07:45 +04:00
|
|
|
const char *tmp;
|
|
|
|
char *tmp1, *tmpf;
|
2002-10-04 22:37:19 +04:00
|
|
|
|
|
|
|
*num = 0;
|
2005-06-27 07:07:45 +04:00
|
|
|
tmp = line;
|
2002-10-04 22:37:19 +04:00
|
|
|
if (tmp[0] == '\0')
|
|
|
|
return NULL;
|
|
|
|
while (tmp[0]) {
|
|
|
|
if ((tmp[1] == ' ' || tmp[1] == '\t' || tmp[1] == '\0') &&
|
|
|
|
(tmp[0] != ' ' && tmp[0] != '\t'))
|
|
|
|
nwords++;
|
|
|
|
tmp++;
|
|
|
|
}
|
2005-03-30 21:10:18 +04:00
|
|
|
ret = emalloc((nwords+1) * sizeof(char *));
|
2005-06-27 07:07:45 +04:00
|
|
|
tmp1 = tmpf = estrdup(line);
|
|
|
|
while ((cur = strsep_getnext(&tmpf, " \t")) != NULL)
|
2005-03-30 21:10:18 +04:00
|
|
|
ret[i++] = estrdup(cur);
|
2002-10-04 22:37:19 +04:00
|
|
|
ret[i] = NULL;
|
|
|
|
free(tmp1);
|
|
|
|
*num = nwords;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
words_free(char **w, int num)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i < num; i++)
|
|
|
|
free(w[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is a simple xor that has the same calling conventions as
|
|
|
|
* memcpy(3).
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
memxor(void *res, const void *src, size_t len)
|
|
|
|
{
|
|
|
|
char *r;
|
|
|
|
const char *s;
|
2007-02-06 03:48:37 +03:00
|
|
|
size_t i;
|
2002-10-04 22:37:19 +04:00
|
|
|
|
|
|
|
r = res;
|
|
|
|
s = src;
|
2007-02-06 03:48:37 +03:00
|
|
|
for (i = 0; i < len; i++)
|
2002-10-04 22:37:19 +04:00
|
|
|
r[i] ^= s[i];
|
|
|
|
}
|
2003-03-24 05:02:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* well, a very simple set of string functions...
|
|
|
|
*
|
|
|
|
* The goal here is basically to manage length encoded strings,
|
|
|
|
* but just for safety we nul terminate them anyway.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* for now we use a very simple encoding */
|
|
|
|
|
|
|
|
struct string {
|
|
|
|
int length;
|
|
|
|
char *text;
|
|
|
|
};
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
string_new(const char *intext, int inlength)
|
|
|
|
{
|
|
|
|
string_t *out;
|
|
|
|
|
2005-03-30 21:10:18 +04:00
|
|
|
out = emalloc(sizeof(*out));
|
2003-03-24 05:02:49 +03:00
|
|
|
out->length = inlength;
|
2005-03-30 21:10:18 +04:00
|
|
|
out->text = emalloc(out->length + 1);
|
2003-03-24 05:02:49 +03:00
|
|
|
memcpy(out->text, intext, out->length);
|
|
|
|
out->text[out->length] = '\0';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
string_dup(const string_t *in)
|
|
|
|
{
|
|
|
|
|
|
|
|
return string_new(in->text, in->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
string_free(string_t *s)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return;
|
2005-03-30 21:10:18 +04:00
|
|
|
free(s->text);
|
2003-03-24 05:02:49 +03:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
string_assign(string_t **lhs, string_t *rhs)
|
|
|
|
{
|
|
|
|
|
|
|
|
string_free(*lhs);
|
|
|
|
*lhs = rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
string_add(const string_t *a1, const string_t *a2)
|
|
|
|
{
|
|
|
|
string_t *sum;
|
|
|
|
|
2005-03-30 21:10:18 +04:00
|
|
|
sum = emalloc(sizeof(*sum));
|
2003-03-24 05:02:49 +03:00
|
|
|
sum->length = a1->length + a2->length;
|
2005-03-30 21:10:18 +04:00
|
|
|
sum->text = emalloc(sum->length + 1);
|
2003-03-24 05:02:49 +03:00
|
|
|
memcpy(sum->text, a1->text, a1->length);
|
|
|
|
memcpy(sum->text + a1->length, a2->text, a2->length);
|
|
|
|
sum->text[sum->length] = '\0';
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
string_add_d(string_t *a1, string_t *a2)
|
|
|
|
{
|
|
|
|
string_t *sum;
|
|
|
|
|
|
|
|
sum = string_add(a1, a2);
|
|
|
|
string_free(a1);
|
|
|
|
string_free(a2);
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
string_fromcharstar(const char *in)
|
|
|
|
{
|
|
|
|
|
|
|
|
return string_new(in, strlen(in));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
string_tocharstar(const string_t *in)
|
|
|
|
{
|
|
|
|
|
|
|
|
return in->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
string_fromint(int in)
|
|
|
|
{
|
|
|
|
string_t *ret;
|
|
|
|
|
2005-03-30 21:10:18 +04:00
|
|
|
ret = emalloc(sizeof(*ret));
|
2003-03-24 05:02:49 +03:00
|
|
|
ret->length = asprintf(&ret->text, "%d", in);
|
2005-03-30 21:10:18 +04:00
|
|
|
if (ret->length == -1)
|
|
|
|
err(1, NULL);
|
2003-03-24 05:02:49 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
string_fprint(FILE *f, const string_t *s)
|
|
|
|
{
|
|
|
|
|
|
|
|
fwrite(s->text, s->length, 1, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bits {
|
|
|
|
int length;
|
|
|
|
char *text;
|
|
|
|
};
|
|
|
|
|
|
|
|
bits_t *
|
|
|
|
bits_new(const void *buf, int len)
|
|
|
|
{
|
|
|
|
bits_t *b;
|
|
|
|
|
2005-03-30 21:17:51 +04:00
|
|
|
b = emalloc(sizeof(*b));
|
2003-03-24 05:02:49 +03:00
|
|
|
b->length = len;
|
2005-03-30 21:10:18 +04:00
|
|
|
b->text = emalloc(BITS2BYTES(b->length));
|
2003-03-24 05:02:49 +03:00
|
|
|
memcpy(b->text, buf, BITS2BYTES(b->length));
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits_t *
|
|
|
|
bits_dup(const bits_t *in)
|
|
|
|
{
|
|
|
|
|
|
|
|
return bits_new(in->text, in->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bits_free(bits_t *b)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!b)
|
|
|
|
return;
|
2005-03-30 21:10:18 +04:00
|
|
|
free(b->text);
|
2003-03-24 05:02:49 +03:00
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bits_assign(bits_t **lhs, bits_t *rhs)
|
|
|
|
{
|
|
|
|
|
|
|
|
bits_free(*lhs);
|
|
|
|
*lhs = rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *
|
|
|
|
bits_getbuf(bits_t *in)
|
|
|
|
{
|
|
|
|
|
|
|
|
return in->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
bits_len(bits_t *in)
|
|
|
|
{
|
|
|
|
|
|
|
|
return in->length;
|
|
|
|
}
|
|
|
|
|
2003-09-23 21:24:45 +04:00
|
|
|
int
|
|
|
|
bits_match(const bits_t *b1, const bits_t *b2)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (b1->length != b2->length)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < BITS2BYTES(b1->length); i++)
|
|
|
|
if (b1->text[i] != b2->text[i])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-03-24 05:02:49 +03:00
|
|
|
bits_t *
|
|
|
|
bits_xor(const bits_t *x1, const bits_t *x2)
|
|
|
|
{
|
|
|
|
bits_t *b;
|
|
|
|
int i;
|
|
|
|
|
2005-03-30 21:10:18 +04:00
|
|
|
b = emalloc(sizeof(*b));
|
2003-03-24 05:02:49 +03:00
|
|
|
b->length = MAX(x1->length, x2->length);
|
2005-03-30 21:10:18 +04:00
|
|
|
b->text = ecalloc(1, BITS2BYTES(b->length));
|
2003-03-24 05:02:49 +03:00
|
|
|
for (i=0; i < BITS2BYTES(MIN(x1->length, x2->length)); i++)
|
|
|
|
b->text[i] = x1->text[i] ^ x2->text[i];
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits_t *
|
|
|
|
bits_xor_d(bits_t *x1, bits_t *x2)
|
|
|
|
{
|
|
|
|
bits_t *ret;
|
|
|
|
|
|
|
|
ret = bits_xor(x1, x2);
|
|
|
|
bits_free(x1);
|
|
|
|
bits_free(x2);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bits_decode() reads an encoded base64 stream. We interpret
|
|
|
|
* the first 32 bits as an unsigned integer in network byte order
|
|
|
|
* specifying the number of bits in the stream to give a little
|
|
|
|
* resilience.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bits_t *
|
|
|
|
bits_decode(const string_t *in)
|
|
|
|
{
|
|
|
|
bits_t *ret;
|
|
|
|
int len;
|
|
|
|
int nbits;
|
2006-05-11 04:42:08 +04:00
|
|
|
u_char *tmp;
|
2003-03-24 05:02:49 +03:00
|
|
|
|
|
|
|
len = in->length;
|
2005-03-30 21:10:18 +04:00
|
|
|
tmp = emalloc(len);
|
2003-03-24 05:02:49 +03:00
|
|
|
|
|
|
|
len = __b64_pton(in->text, tmp, len);
|
|
|
|
|
|
|
|
if (len == -1) {
|
|
|
|
fprintf(stderr, "bits_decode: mangled base64 stream\n");
|
|
|
|
fprintf(stderr, " %s\n", in->text);
|
2006-03-22 18:45:16 +03:00
|
|
|
free(tmp);
|
2003-03-24 05:02:49 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nbits = ntohl(*((u_int32_t *)tmp));
|
|
|
|
if (nbits > (len - 4) * 8) {
|
|
|
|
fprintf(stderr, "bits_decode: encoded bits claim to be "
|
|
|
|
"longer than they are (nbits=%u, stream len=%u bytes)\n",
|
|
|
|
(unsigned)nbits, (unsigned)len);
|
2006-03-22 18:45:16 +03:00
|
|
|
free(tmp);
|
2003-03-24 05:02:49 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bits_new(tmp+4, nbits);
|
|
|
|
free(tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits_t *
|
|
|
|
bits_decode_d(string_t *in)
|
|
|
|
{
|
|
|
|
bits_t *ret;
|
|
|
|
|
|
|
|
ret = bits_decode(in);
|
|
|
|
string_free(in);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
bits_encode(const bits_t *in)
|
|
|
|
{
|
|
|
|
string_t *ret;
|
|
|
|
int len;
|
|
|
|
char *out;
|
2006-05-11 04:42:08 +04:00
|
|
|
u_char *tmp;
|
2003-03-24 05:02:49 +03:00
|
|
|
|
|
|
|
if (!in)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* compute the total size of the input stream */
|
|
|
|
len = BITS2BYTES(in->length) + 4;
|
|
|
|
|
2005-03-30 21:10:18 +04:00
|
|
|
tmp = emalloc(len);
|
|
|
|
out = emalloc(len * 2);
|
2003-03-24 05:02:49 +03:00
|
|
|
/* stuff the length up front */
|
|
|
|
*((u_int32_t *)tmp) = htonl(in->length);
|
|
|
|
memcpy(tmp + 4, in->text, len - 4);
|
|
|
|
|
2006-03-20 03:53:39 +03:00
|
|
|
if ((len = __b64_ntop(tmp, len, out, len * 2)) == -1) {
|
2006-03-22 19:35:44 +03:00
|
|
|
free(out);
|
2006-03-20 03:53:39 +03:00
|
|
|
free(tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-24 05:02:49 +03:00
|
|
|
ret = string_new(out, len);
|
|
|
|
free(tmp);
|
|
|
|
free(out);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_t *
|
|
|
|
bits_encode_d(bits_t *in)
|
|
|
|
{
|
|
|
|
string_t *ret;
|
|
|
|
|
|
|
|
ret = bits_encode(in);
|
|
|
|
bits_free(in);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits_t *
|
|
|
|
bits_fget(FILE *f, int len)
|
|
|
|
{
|
|
|
|
bits_t *bits;
|
|
|
|
int ret;
|
|
|
|
|
2005-03-30 21:10:18 +04:00
|
|
|
bits = emalloc(sizeof(*bits));
|
2003-03-24 05:02:49 +03:00
|
|
|
bits->length = len;
|
2005-03-30 21:10:18 +04:00
|
|
|
bits->text = emalloc(BITS2BYTES(bits->length));
|
2003-03-24 05:02:49 +03:00
|
|
|
ret = fread(bits->text, BITS2BYTES(bits->length), 1, f);
|
|
|
|
if (ret != 1) {
|
|
|
|
bits_free(bits);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits_t *
|
|
|
|
bits_cget(const char *fn, int len)
|
|
|
|
{
|
|
|
|
bits_t *bits;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fopen(fn, "r");
|
2005-06-02 05:31:30 +04:00
|
|
|
if (!f)
|
2003-03-24 05:02:49 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bits = bits_fget(f, len);
|
|
|
|
fclose(f);
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits_t *
|
2004-08-13 19:03:57 +04:00
|
|
|
bits_getrandombits(int len, int hard)
|
2003-03-24 05:02:49 +03:00
|
|
|
{
|
|
|
|
|
2004-08-13 19:03:57 +04:00
|
|
|
return bits_cget((hard ? "/dev/random" : "/dev/urandom"), len);
|
2003-03-24 05:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bits_fprint(FILE *f, const bits_t *bits)
|
|
|
|
{
|
|
|
|
string_t *s;
|
|
|
|
|
|
|
|
s = bits_encode(bits);
|
|
|
|
string_fprint(f, s);
|
|
|
|
free(s);
|
|
|
|
}
|