blob: d76fc6ab88f7790866ea90ed39b2771ce9b92ca9 [file] [log] [blame]
robert.swiecki3bb518c2010-10-14 00:48:24 +00001/*
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00002 *
robert.swiecki@gmail.com97c77332015-02-14 23:06:58 +00003 * honggfuzz - utilities
4 * -----------------------------------------
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +00005 *
6 * Author: Robert Swiecki <swiecki@google.com>
7 *
8 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License. You may obtain
12 * a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
19 * implied. See the License for the specific language governing
20 * permissions and limitations under the License.
21 *
22 */
robert.swiecki3bb518c2010-10-14 00:48:24 +000023
robert.swiecki3bb518c2010-10-14 00:48:24 +000024#include <fcntl.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000025#include <math.h>
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000026#include <stdarg.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000027#include <stdint.h>
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000028#include <stdio.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000029#include <stdlib.h>
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000030#include <string.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000031#include <sys/stat.h>
32#include <sys/time.h>
33#include <sys/types.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000034#include <time.h>
robert.swiecki@gmail.comba85c3e2015-02-02 14:55:16 +000035#include <unistd.h>
robert.swiecki3bb518c2010-10-14 00:48:24 +000036
37#include "common.h"
38#include "log.h"
39
robert.swiecki@gmail.com180263f2015-02-13 13:57:15 +000040static int util_urandomFD = -1;
41
robert.swiecki@gmail.com4be12dc2015-02-13 14:03:53 +000042uint32_t util_rndGet(uint32_t min, uint32_t max)
robert.swiecki3bb518c2010-10-14 00:48:24 +000043{
robert.swiecki@gmail.com180263f2015-02-13 13:57:15 +000044 if (util_urandomFD == -1) {
45 if ((util_urandomFD = open("/dev/urandom", O_RDONLY)) == -1) {
46 LOGMSG_P(l_FATAL, "Couldn't open /dev/urandom");
47 }
48 }
robert.swiecki3bb518c2010-10-14 00:48:24 +000049
robert.swiecki@gmail.comb0383372015-02-13 14:17:27 +000050 unsigned short seed16v[3];
51
52 if (read(util_urandomFD, seed16v, sizeof(seed16v)) != sizeof(seed16v)) {
robert.swiecki@gmail.com4be12dc2015-02-13 14:03:53 +000053 struct timeval tv;
54 gettimeofday(&tv, NULL);
robert.swiecki@gmail.comb0383372015-02-13 14:17:27 +000055 seed16v[0] = ((unsigned short)tv.tv_usec);
56 gettimeofday(&tv, NULL);
57 seed16v[1] = ((unsigned short)tv.tv_usec);
58 gettimeofday(&tv, NULL);
59 seed16v[2] = ((unsigned short)tv.tv_usec);
robert.swiecki@gmail.com4be12dc2015-02-13 14:03:53 +000060 }
robert.swiecki@gmail.com180263f2015-02-13 13:57:15 +000061
robert.swiecki@gmail.comb0383372015-02-13 14:17:27 +000062 seed48(seed16v);
63 uint32_t rnd1 = (uint32_t) lrand48();
64 uint32_t rnd2 = (uint32_t) lrand48();
65 uint32_t rnd = (rnd1 << 16) ^ rnd2;
robert.swiecki3bb518c2010-10-14 00:48:24 +000066
robert.swiecki@gmail.combce825a2015-02-13 23:26:07 +000067 if (min > max) {
68 LOGMSG(l_FATAL, "min:%d > max:%d", min, max);
69 }
70
robert.swiecki@gmail.comb0383372015-02-13 14:17:27 +000071 return ((rnd % (max - min + 1)) + min);
robert.swiecki3bb518c2010-10-14 00:48:24 +000072}
73
robert.swiecki@gmail.come7190b92015-02-14 23:05:42 +000074void util_ssnprintf(char *str, size_t size, const char *format, ...)
75{
76 char buf1[size];
77 char buf2[size];
78
79 strncpy(buf1, str, size);
80
81 va_list args;
82 va_start(args, format);
83 vsnprintf(buf2, size, format, args);
84 va_end(args);
85
86 snprintf(str, size, "%s%s", buf1, buf2);
87}
88
robert.swiecki3bb518c2010-10-14 00:48:24 +000089void util_getLocalTime(const char *fmt, char *buf, size_t len)
90{
91 struct tm ltime;
92
93 time_t t = time(NULL);
94
95 localtime_r(&t, &ltime);
96 strftime(buf, len, fmt, &ltime);
97}
98
robert.swiecki40499ff2010-12-13 19:47:08 +000099void util_nullifyStdio(void)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000100{
101 int fd = open("/dev/null", O_RDWR);
102
103 if (fd == -1) {
104 LOGMSG_P(l_ERROR, "Couldn't open '/dev/null'");
105 return;
106 }
107
108 dup2(fd, 0);
109 dup2(fd, 1);
110 dup2(fd, 2);
111
112 if (fd > 2) {
113 close(fd);
114 }
115
116 return;
117}
118
119bool util_redirectStdin(char *inputFile)
120{
121 int fd = open(inputFile, O_RDONLY);
122
123 if (fd == -1) {
124 LOGMSG_P(l_ERROR, "Couldn't open '%s'", inputFile);
125 return false;
126 }
127
128 dup2(fd, 0);
129 if (fd != 0) {
130 close(fd);
131 }
132
133 return true;
134}
135
robert.swiecki40499ff2010-12-13 19:47:08 +0000136void util_recoverStdio(void)
robert.swiecki3bb518c2010-10-14 00:48:24 +0000137{
138 int fd = open("/dev/tty", O_RDWR);
139
140 if (fd == -1) {
141 LOGMSG_P(l_ERROR, "Couldn't open '/dev/tty'");
142 return;
143 }
144
145 dup2(fd, 0);
146 dup2(fd, 1);
147 dup2(fd, 2);
148
149 if (fd > 2) {
150 close(fd);
151 }
152
153 return;
154}
groebert@google.com1bd4c212013-06-19 11:13:56 +0000155
robert.swiecki@gmail.com772b33d2015-02-14 20:35:00 +0000156/*
157 * This is not a cryptographically secure hash
158 */
groebert@google.com1bd4c212013-06-19 11:13:56 +0000159extern uint64_t util_hash(const char *buf, size_t len)
160{
161 uint64_t ret = 0;
162
163 for (size_t i = 0; i < len; i++) {
164 ret += buf[i];
165 ret += (ret << 10);
166 ret ^= (ret >> 6);
167 }
168
169 return ret;
170}