1/*	$NetBSD: misc.c,v 1.4.6.1 2008/07/15 00:55:48 mgrooms Exp $	*/
2
3/*	$KAME: misc.c,v 1.23 2001/08/16 14:37:29 itojun Exp $	*/
4
5/*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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 "config.h"
35
36#include <sys/types.h>
37#include <sys/param.h>
38#include <sys/stat.h>
39#include <sys/time.h>
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
44#include <errno.h>
45#include <syslog.h>
46#include <ctype.h>
47
48#include "var.h"
49#include "misc.h"
50#include "debug.h"
51
52#if 0
53static int bindump __P((void *, size_t));
54
55static int
56bindump(buf0, len)
57        void *buf0;
58        size_t len;
59{
60	unsigned char *buf = (unsigned char *)buf0;
61	size_t i;
62
63	for (i = 0; i < len; i++) {
64		if ((buf[i] & 0x80) || !isprint(buf[i]))
65			printf("\\x%x", buf[i]);
66		else
67			printf("%c", buf[i]);
68	}
69	printf("\n");
70
71	return 0;
72}
73#endif
74
75int
76racoon_hexdump(buf0, len)
77	void *buf0;
78	size_t len;
79{
80	caddr_t buf = (caddr_t)buf0;
81	size_t i;
82
83	for (i = 0; i < len; i++) {
84		if (i != 0 && i % 32 == 0)
85			printf("\n");
86		if (i % 4 == 0)
87			printf(" ");
88		printf("%02x", (unsigned char)buf[i]);
89	}
90	printf("\n");
91
92	return 0;
93}
94
95char *
96bit2str(n, bl)
97	int n, bl;
98{
99#define MAXBITLEN 128
100	static char b[MAXBITLEN + 1];
101	int i;
102
103	if (bl > MAXBITLEN)
104		return "Failed to convert.";	/* NG */
105	memset(b, '0', bl);
106	b[bl] = '\0';
107
108	for (i = 0; i < bl; i++) {
109		if (n & (1 << i))
110			b[bl - 1 - i] = '1';
111	}
112
113	return b;
114}
115
116const char *
117debug_location(file, line, func)
118	const char *file;
119	int line;
120	const char *func;
121{
122	static char buf[1024];
123	const char *p;
124
125	/* truncate pathname */
126	p = strrchr(file, '/');
127	if (p)
128		p++;
129	else
130		p = file;
131
132	if (func)
133		snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func);
134	else
135		snprintf(buf, sizeof(buf), "%s:%d", p, line);
136
137	return buf;
138}
139
140/*
141 * get file size.
142 * -1: error occured.
143 */
144int
145getfsize(path)
146	char *path;
147{
148        struct stat st;
149
150        if (stat(path, &st) != 0)
151                return -1;
152        else
153                return st.st_size;
154}
155
156/*
157 * calculate the difference between two times.
158 * t1: start
159 * t2: end
160 */
161double
162timedelta(t1, t2)
163	struct timeval *t1, *t2;
164{
165	if (t2->tv_usec >= t1->tv_usec)
166		return t2->tv_sec - t1->tv_sec +
167			(double)(t2->tv_usec - t1->tv_usec) / 1000000;
168
169	return t2->tv_sec - t1->tv_sec - 1 +
170		(double)(1000000 + t2->tv_usec - t1->tv_usec) / 1000000;
171}
172