util.c (5375B)
1 /** 2 util.c 3 4 5 Copyright (C) 2001-2003, Network Resonance, Inc. 6 Copyright (C) 2006, Network Resonance, Inc. 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 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 2. Redistributions in binary form must reproduce the above copyright 16 notice, this list of conditions and the following disclaimer in the 17 documentation and/or other materials provided with the distribution. 18 3. Neither the name of Network Resonance, Inc. nor the name of any 19 contributors to this software may be used to endorse or promote 20 products derived from this software without specific prior written 21 permission. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34 35 36 ekr@rtfm.com Wed Dec 26 17:19:36 2001 37 */ 38 39 #ifndef WIN32 40 #include <sys/uio.h> 41 #include <pwd.h> 42 #include <dirent.h> 43 #endif 44 #include <string.h> 45 #include <errno.h> 46 #include <ctype.h> 47 #include <sys/stat.h> 48 #ifdef OPENSSL 49 #include <openssl/evp.h> 50 #endif 51 #include <csi_platform.h> 52 #include "r_common.h" 53 #include "registry.h" 54 #include "util.h" 55 #include "r_log.h" 56 57 int nr_util_default_log_facility=LOG_COMMON; 58 59 int nr_bin2hex(UCHAR *in,int len,UCHAR *out) 60 { 61 while(len){ 62 sprintf((char*)out,"%.2x",in[0] & 0xff); 63 64 in+=1; 65 out+=2; 66 67 len--; 68 } 69 70 return(0); 71 } 72 73 74 #if defined(LINUX) || defined(WIN32) 75 /*- 76 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 77 * All rights reserved. 78 * 79 * Redistribution and use in source and binary forms, with or without 80 * modification, are permitted provided that the following conditions 81 * are met: 82 * 1. Redistributions of source code must retain the above copyright 83 * notice, this list of conditions and the following disclaimer. 84 * 2. Redistributions in binary form must reproduce the above copyright 85 * notice, this list of conditions and the following disclaimer in the 86 * documentation and/or other materials provided with the distribution. 87 * 3. The name of the author may not be used to endorse or promote products 88 * derived from this software without specific prior written permission. 89 * 90 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 91 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 92 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 93 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 94 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 95 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 96 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 97 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 98 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 99 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 100 */ 101 102 103 /* 104 * Appends src to string dst of size siz (unlike strncat, siz is the 105 * full size of dst, not space left). At most siz-1 characters 106 * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 107 * Returns strlen(src) + MIN(siz, strlen(initial dst)). 108 * If retval >= siz, truncation occurred. 109 */ 110 size_t strlcat(char *dst, const char *src, size_t siz) 111 { 112 char *d = dst; 113 const char *s = src; 114 size_t n = siz; 115 size_t dlen; 116 117 /* Find the end of dst and adjust bytes left but don't go past end */ 118 while (n-- != 0 && *d != '\0') 119 d++; 120 dlen = d - dst; 121 n = siz - dlen; 122 123 if (n == 0) 124 return(dlen + strlen(s)); 125 while (*s != '\0') { 126 if (n != 1) { 127 *d++ = *s; 128 n--; 129 } 130 s++; 131 } 132 *d = '\0'; 133 134 return(dlen + (s - src)); /* count does not include NUL */ 135 } 136 137 #endif /* LINUX or WIN32 */ 138 139 140 #ifdef WIN32 141 #include <time.h> 142 /* this is only millisecond-accurate, but that should be OK */ 143 144 int gettimeofday(struct timeval *tv, void *tz) 145 { 146 SYSTEMTIME st; 147 FILETIME ft; 148 ULARGE_INTEGER u; 149 150 GetLocalTime (&st); 151 152 /* strangely, the FILETIME is the number of 100 nanosecond (0.1 us) intervals 153 * since the Epoch */ 154 SystemTimeToFileTime(&st, &ft); 155 u.HighPart = ft.dwHighDateTime; 156 u.LowPart = ft.dwLowDateTime; 157 158 tv->tv_sec = (long) (u.QuadPart / 10000000L); 159 tv->tv_usec = (long) (st.wMilliseconds * 1000);; 160 161 return 0; 162 } 163 #endif