/* $NetBSD: misc.c,v 1.7 2018/05/19 20:40:40 maxv Exp $ */ /* $KAME: misc.c,v 1.23 2001/08/16 14:37:29 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * All rights reserved. * * 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. Neither the name of the project 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 PROJECT 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 PROJECT 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 "config.h" #include #include #include #include #include #include #include #include #include #include #include #include "var.h" #include "misc.h" #include "debug.h" int racoon_hexdump(void *buf0, size_t len) { caddr_t buf = (caddr_t)buf0; size_t i; for (i = 0; i < len; i++) { if (i != 0 && i % 32 == 0) printf("\n"); if (i % 4 == 0) printf(" "); printf("%02x", (unsigned char)buf[i]); } printf("\n"); return 0; } char * bit2str(int n, int bl) { #define MAXBITLEN 128 static char b[MAXBITLEN + 1]; int i; if (bl > MAXBITLEN) return "Failed to convert."; /* NG */ memset(b, '0', bl); b[bl] = '\0'; for (i = 0; i < bl; i++) { if (n & (1 << i)) b[bl - 1 - i] = '1'; } return b; } const char * debug_location(const char *file, int line, const char *func) { static char buf[1024]; const char *p; /* truncate pathname */ p = strrchr(file, '/'); if (p) p++; else p = file; if (func) snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func); else snprintf(buf, sizeof(buf), "%s:%d", p, line); return buf; } /* * get file size. * -1: error occured. */ int getfsize(char *path) { struct stat st; if (stat(path, &st) != 0) return -1; else return st.st_size; } /* * set the close-on-exec flag for file descriptor fd. */ void close_on_exec(int fd) { fcntl(fd, F_SETFD, FD_CLOEXEC); } /* * calculate the difference between two times. * t1: start * t2: end */ double timedelta(struct timeval *t1, struct timeval *t2) { if (t2->tv_usec >= t1->tv_usec) return t2->tv_sec - t1->tv_sec + (double)(t2->tv_usec - t1->tv_usec) / 1000000; return t2->tv_sec - t1->tv_sec - 1 + (double)(1000000 + t2->tv_usec - t1->tv_usec) / 1000000; }