robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 2 | * |
robert.swiecki@gmail.com | 97c7733 | 2015-02-14 23:06:58 +0000 | [diff] [blame] | 3 | * honggfuzz - utilities |
| 4 | * ----------------------------------------- |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 5 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 6 | * Author: Robert Swiecki <swiecki@google.com> |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 7 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 8 | * Copyright 2010-2015 by Google Inc. All Rights Reserved. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 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 |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 12 | * a copy of the License at |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 13 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 15 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 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. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 21 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 22 | */ |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 23 | |
Jagger | c8b6eda | 2016-09-01 03:52:35 +0200 | [diff] [blame] | 24 | #include "common.h" |
| 25 | #include "util.h" |
| 26 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 28 | #include <inttypes.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 29 | #include <math.h> |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 30 | #include <stdarg.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 31 | #include <stdint.h> |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 34 | #include <string.h> |
Robert Swiecki | ac56a50 | 2016-03-17 18:59:46 +0100 | [diff] [blame] | 35 | #include <sys/mman.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 36 | #include <sys/stat.h> |
| 37 | #include <sys/time.h> |
| 38 | #include <sys/types.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 39 | #include <time.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 40 | #include <unistd.h> |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 41 | #include <ctype.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 42 | |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 43 | #include "files.h" |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 44 | #include "log.h" |
| 45 | |
Robert Swiecki | 3bfc33c | 2016-03-14 18:12:41 +0100 | [diff] [blame] | 46 | void *util_Malloc(size_t sz) |
| 47 | { |
| 48 | void *p = malloc(sz); |
| 49 | if (p == NULL) { |
| 50 | LOG_F("malloc(size='%zu')", sz); |
| 51 | } |
| 52 | return p; |
| 53 | } |
| 54 | |
Robert Swiecki | ac56a50 | 2016-03-17 18:59:46 +0100 | [diff] [blame] | 55 | void *util_Calloc(size_t sz) |
| 56 | { |
| 57 | void *p = util_Malloc(sz); |
| 58 | memset(p, '\0', sz); |
| 59 | return p; |
| 60 | } |
| 61 | |
Jagger | f49962d | 2016-07-21 22:49:54 +0200 | [diff] [blame] | 62 | extern void *util_Realloc(void *ptr, size_t sz) |
| 63 | { |
| 64 | void *ret = realloc(ptr, sz); |
| 65 | if (ret == NULL) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 66 | PLOG_W("realloc(%p, %zu)", ptr, sz); |
Jagger | f49962d | 2016-07-21 22:49:54 +0200 | [diff] [blame] | 67 | free(ptr); |
| 68 | return NULL; |
| 69 | } |
| 70 | return ret; |
| 71 | } |
| 72 | |
Robert Swiecki | ac56a50 | 2016-03-17 18:59:46 +0100 | [diff] [blame] | 73 | void *util_MMap(size_t sz) |
| 74 | { |
| 75 | void *p = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_NORESERVE, -1, 0); |
| 76 | if (p == MAP_FAILED) { |
| 77 | LOG_F("mmap(size='%zu')", sz); |
| 78 | } |
| 79 | return p; |
| 80 | } |
| 81 | |
Jagger | 6539b3a | 2016-04-06 02:32:50 +0200 | [diff] [blame] | 82 | char *util_StrDup(const char *s) |
| 83 | { |
| 84 | char *ret = strdup(s); |
| 85 | if (ret == NULL) { |
| 86 | LOG_F("strdup(size=%zu)", strlen(s)); |
| 87 | } |
| 88 | return ret; |
| 89 | } |
| 90 | |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 91 | static int util_urandomFd = -1; |
Jagger | 2e518b5 | 2015-09-24 04:01:16 +0200 | [diff] [blame] | 92 | static __thread uint64_t rndX; |
| 93 | static __thread uint64_t rndIni = false; |
robert.swiecki@gmail.com | 180263f | 2015-02-13 13:57:15 +0000 | [diff] [blame] | 94 | |
Jagger | c44a89a | 2016-09-25 16:42:44 +0200 | [diff] [blame] | 95 | uint64_t util_rnd64(void) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 96 | { |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 97 | if (util_urandomFd == -1) { |
Jagger | 5a3c4c3 | 2016-04-16 19:27:47 +0200 | [diff] [blame] | 98 | if ((util_urandomFd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 99 | PLOG_F("Couldn't open /dev/urandom for writing"); |
robert.swiecki@gmail.com | 26a9ab7 | 2015-02-22 13:41:18 +0000 | [diff] [blame] | 100 | } |
robert.swiecki@gmail.com | 180263f | 2015-02-13 13:57:15 +0000 | [diff] [blame] | 101 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 102 | |
Jagger | 2e518b5 | 2015-09-24 04:01:16 +0200 | [diff] [blame] | 103 | if (rndIni == false) { |
Jagger | 4d10985 | 2016-02-04 02:28:44 +0100 | [diff] [blame] | 104 | if (files_readFromFd(util_urandomFd, (uint8_t *) & rndX, sizeof(rndX)) != sizeof(rndX)) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 105 | PLOG_F("Couldn't read '%zu' bytes from /dev/urandom", sizeof(rndX)); |
Jagger | 2e518b5 | 2015-09-24 04:01:16 +0200 | [diff] [blame] | 106 | } |
| 107 | rndIni = true; |
robert.swiecki@gmail.com | 4be12dc | 2015-02-13 14:03:53 +0000 | [diff] [blame] | 108 | } |
robert.swiecki@gmail.com | 180263f | 2015-02-13 13:57:15 +0000 | [diff] [blame] | 109 | |
Jagger | 2e518b5 | 2015-09-24 04:01:16 +0200 | [diff] [blame] | 110 | /* MMIX LCG PRNG */ |
| 111 | static const uint64_t a = 6364136223846793005ULL; |
| 112 | static const uint64_t c = 1442695040888963407ULL; |
robert.swiecki@gmail.com | bce825a | 2015-02-13 23:26:07 +0000 | [diff] [blame] | 113 | |
Jagger | 2e518b5 | 2015-09-24 04:01:16 +0200 | [diff] [blame] | 114 | rndX = (a * rndX + c); |
Jagger | c44a89a | 2016-09-25 16:42:44 +0200 | [diff] [blame] | 115 | return rndX; |
| 116 | } |
Jagger | 2e518b5 | 2015-09-24 04:01:16 +0200 | [diff] [blame] | 117 | |
Jagger | c44a89a | 2016-09-25 16:42:44 +0200 | [diff] [blame] | 118 | uint64_t util_rndGet(uint64_t min, uint64_t max) |
| 119 | { |
| 120 | if (min > max) { |
| 121 | LOG_F("min:%" PRIu64 " > max:%" PRIu64, min, max); |
| 122 | } |
| 123 | |
| 124 | return ((util_rnd64() % (max - min + 1)) + min); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 125 | } |
| 126 | |
robert.swiecki@gmail.com | 89cc38c | 2015-02-23 02:52:08 +0000 | [diff] [blame] | 127 | void util_rndBuf(uint8_t * buf, size_t sz) |
| 128 | { |
robert.swiecki@gmail.com | 08174c7 | 2015-02-28 14:38:06 +0000 | [diff] [blame] | 129 | for (size_t i = 0; i < sz; i++) { |
Choongwoo Han | c0acb0d | 2016-10-15 17:34:11 +0900 | [diff] [blame] | 130 | buf[i] = (uint8_t) ((util_rnd64() & 0xFF0000) >> 16); |
robert.swiecki@gmail.com | 89cc38c | 2015-02-23 02:52:08 +0000 | [diff] [blame] | 131 | } |
robert.swiecki@gmail.com | 89cc38c | 2015-02-23 02:52:08 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Robert Swiecki | 23ec02a | 2016-01-19 18:47:45 +0100 | [diff] [blame] | 134 | /* |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 135 | * Function has variable length stack size, although already we know it's invoked |
| 136 | * with relatively small sizes (max is _HF_REPORT_SIZE), thus safe to silent warning. |
| 137 | */ |
| 138 | #pragma GCC diagnostic push |
Anestis Bechtsoudis | 539dbc5 | 2015-12-26 20:11:46 +0200 | [diff] [blame] | 139 | #pragma GCC diagnostic ignored "-Wframe-larger-than=" |
robert.swiecki@gmail.com | 9e2d43d | 2015-02-16 12:17:39 +0000 | [diff] [blame] | 140 | int util_vssnprintf(char *str, size_t size, const char *format, va_list ap) |
| 141 | { |
| 142 | char buf1[size]; |
| 143 | char buf2[size]; |
| 144 | |
| 145 | strncpy(buf1, str, size); |
| 146 | |
| 147 | vsnprintf(buf2, size, format, ap); |
| 148 | |
| 149 | return snprintf(str, size, "%s%s", buf1, buf2); |
| 150 | } |
| 151 | |
| 152 | int util_ssnprintf(char *str, size_t size, const char *format, ...) |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 153 | { |
| 154 | char buf1[size]; |
| 155 | char buf2[size]; |
| 156 | |
| 157 | strncpy(buf1, str, size); |
| 158 | |
| 159 | va_list args; |
| 160 | va_start(args, format); |
| 161 | vsnprintf(buf2, size, format, args); |
| 162 | va_end(args); |
| 163 | |
robert.swiecki@gmail.com | 9e2d43d | 2015-02-16 12:17:39 +0000 | [diff] [blame] | 164 | return snprintf(str, size, "%s%s", buf1, buf2); |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 167 | #pragma GCC diagnostic pop /* EOF diagnostic ignored "-Wstack-usage=" */ |
| 168 | |
Robert Swiecki | 81c6a0d | 2015-09-08 15:43:20 +0200 | [diff] [blame] | 169 | void util_getLocalTime(const char *fmt, char *buf, size_t len, time_t tm) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 170 | { |
| 171 | struct tm ltime; |
Robert Swiecki | 81c6a0d | 2015-09-08 15:43:20 +0200 | [diff] [blame] | 172 | localtime_r(&tm, <ime); |
| 173 | if (strftime(buf, len, fmt, <ime) < 1) { |
| 174 | snprintf(buf, len, "[date fetch error]"); |
| 175 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
robert.swiecki | 40499ff | 2010-12-13 19:47:08 +0000 | [diff] [blame] | 178 | void util_nullifyStdio(void) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 179 | { |
| 180 | int fd = open("/dev/null", O_RDWR); |
| 181 | |
| 182 | if (fd == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 183 | PLOG_E("Couldn't open '/dev/null'"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | dup2(fd, 0); |
| 188 | dup2(fd, 1); |
| 189 | dup2(fd, 2); |
| 190 | |
| 191 | if (fd > 2) { |
| 192 | close(fd); |
| 193 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Robert Swiecki | 1e1b353 | 2016-03-10 18:43:28 +0100 | [diff] [blame] | 196 | bool util_redirectStdin(const char *inputFile) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 197 | { |
| 198 | int fd = open(inputFile, O_RDONLY); |
| 199 | |
| 200 | if (fd == -1) { |
Robert Swiecki | bc7532e | 2016-08-20 00:34:17 +0200 | [diff] [blame] | 201 | PLOG_W("Couldn't open '%s'", inputFile); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | dup2(fd, 0); |
| 206 | if (fd != 0) { |
| 207 | close(fd); |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 213 | /* |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 214 | * This is not a cryptographically secure hash |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 215 | */ |
Anestis Bechtsoudis | 3857255 | 2016-02-07 12:55:55 +0200 | [diff] [blame] | 216 | uint64_t util_hash(const char *buf, size_t len) |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 217 | { |
| 218 | uint64_t ret = 0; |
| 219 | |
| 220 | for (size_t i = 0; i < len; i++) { |
| 221 | ret += buf[i]; |
| 222 | ret += (ret << 10); |
| 223 | ret ^= (ret >> 6); |
| 224 | } |
| 225 | |
| 226 | return ret; |
| 227 | } |
robert.swiecki@gmail.com | 549ff18 | 2015-02-28 22:38:00 +0000 | [diff] [blame] | 228 | |
Anestis Bechtsoudis | 3857255 | 2016-02-07 12:55:55 +0200 | [diff] [blame] | 229 | int64_t util_timeNowMillis(void) |
robert.swiecki@gmail.com | 3213a11 | 2015-03-12 01:42:02 +0000 | [diff] [blame] | 230 | { |
| 231 | struct timeval tv; |
| 232 | if (gettimeofday(&tv, NULL) == -1) { |
Robert Swiecki | c8c32db | 2015-10-09 18:06:22 +0200 | [diff] [blame] | 233 | PLOG_F("gettimeofday()"); |
robert.swiecki@gmail.com | 3213a11 | 2015-03-12 01:42:02 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return (((int64_t) tv.tv_sec * 1000LL) + ((int64_t) tv.tv_usec / 1000LL)); |
| 237 | } |
| 238 | |
Anestis Bechtsoudis | 3857255 | 2016-02-07 12:55:55 +0200 | [diff] [blame] | 239 | uint64_t util_getUINT32(const uint8_t * buf) |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 240 | { |
Jagger | 26569d9 | 2016-03-17 02:22:15 +0100 | [diff] [blame] | 241 | uint32_t r; |
Jagger | c54b893 | 2016-03-17 03:12:06 +0100 | [diff] [blame] | 242 | memcpy(&r, buf, sizeof(r)); |
Jagger | 26569d9 | 2016-03-17 02:22:15 +0100 | [diff] [blame] | 243 | return (uint64_t) r; |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Anestis Bechtsoudis | 3857255 | 2016-02-07 12:55:55 +0200 | [diff] [blame] | 246 | uint64_t util_getUINT64(const uint8_t * buf) |
Anestis Bechtsoudis | be0ac7b | 2015-12-26 15:38:47 +0200 | [diff] [blame] | 247 | { |
Jagger | 26569d9 | 2016-03-17 02:22:15 +0100 | [diff] [blame] | 248 | uint64_t r; |
Jagger | c54b893 | 2016-03-17 03:12:06 +0100 | [diff] [blame] | 249 | memcpy(&r, buf, sizeof(r)); |
Jagger | 26569d9 | 2016-03-17 02:22:15 +0100 | [diff] [blame] | 250 | return r; |
robert.swiecki@gmail.com | 549ff18 | 2015-02-28 22:38:00 +0000 | [diff] [blame] | 251 | } |
Jagger | 421aca8 | 2015-09-05 14:09:47 +0200 | [diff] [blame] | 252 | |
Jagger | 6caced3 | 2016-03-10 21:42:23 +0100 | [diff] [blame] | 253 | void util_mutexLock(pthread_mutex_t * mutex, const char *func, int line) |
Jagger | 421aca8 | 2015-09-05 14:09:47 +0200 | [diff] [blame] | 254 | { |
| 255 | if (pthread_mutex_lock(mutex)) { |
Anestis Bechtsoudis | 5a401c4 | 2016-04-22 07:50:19 +0300 | [diff] [blame] | 256 | PLOG_F("%s():%d pthread_mutex_lock(%p)", func, line, (void *)mutex); |
Jagger | 421aca8 | 2015-09-05 14:09:47 +0200 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Jagger | 6caced3 | 2016-03-10 21:42:23 +0100 | [diff] [blame] | 260 | void util_mutexUnlock(pthread_mutex_t * mutex, const char *func, int line) |
Jagger | 421aca8 | 2015-09-05 14:09:47 +0200 | [diff] [blame] | 261 | { |
| 262 | if (pthread_mutex_unlock(mutex)) { |
Anestis Bechtsoudis | 5a401c4 | 2016-04-22 07:50:19 +0300 | [diff] [blame] | 263 | PLOG_F("%s():%d pthread_mutex_unlock(%p)", func, line, (void *)mutex); |
Jagger | 421aca8 | 2015-09-05 14:09:47 +0200 | [diff] [blame] | 264 | } |
| 265 | } |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 266 | |
Anestis Bechtsoudis | 3857255 | 2016-02-07 12:55:55 +0200 | [diff] [blame] | 267 | int64_t fastArray64Search(uint64_t * array, size_t arraySz, uint64_t key) |
Anestis Bechtsoudis | d59af69 | 2015-09-21 15:15:05 +0300 | [diff] [blame] | 268 | { |
| 269 | size_t low = 0; |
| 270 | size_t high = arraySz - 1; |
| 271 | size_t mid; |
| 272 | |
| 273 | while (array[high] != array[low] && key >= array[low] && key <= array[high]) { |
| 274 | mid = low + (key - array[low]) * ((high - low) / (array[high] - array[low])); |
| 275 | |
| 276 | if (array[mid] < key) { |
| 277 | low = mid + 1; |
| 278 | } else if (key < array[mid]) { |
| 279 | high = mid - 1; |
| 280 | } else { |
| 281 | return mid; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (key == array[low]) { |
| 286 | return low; |
| 287 | } else { |
| 288 | return -1; |
| 289 | } |
| 290 | } |
Anestis Bechtsoudis | 413cb13 | 2016-02-07 12:59:00 +0200 | [diff] [blame] | 291 | |
| 292 | bool util_isANumber(const char *s) |
| 293 | { |
| 294 | if (!isdigit(s[0])) { |
| 295 | return false; |
| 296 | } |
| 297 | for (int i = 0; s[i]; s++) { |
| 298 | if (!isdigit(s[i]) && s[i] != 'x') { |
| 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | return true; |
| 303 | } |
Robert Swiecki | fe8d289 | 2016-09-13 17:57:27 +0200 | [diff] [blame] | 304 | |
Jagger | d619ed1 | 2016-09-22 04:08:49 +0200 | [diff] [blame] | 305 | size_t util_decodeCString(char *s) |
Robert Swiecki | fe8d289 | 2016-09-13 17:57:27 +0200 | [diff] [blame] | 306 | { |
| 307 | size_t len = strlen(s); |
| 308 | size_t o = 0; |
| 309 | for (size_t i = 0; s[i] != '\0' && i < len; i++, o++) { |
| 310 | switch (s[i]) { |
| 311 | case '\\':{ |
| 312 | i++; |
| 313 | switch (s[i]) { |
| 314 | case 'a': |
| 315 | s[o] = '\a'; |
| 316 | break; |
| 317 | case 'r': |
| 318 | s[o] = '\r'; |
| 319 | break; |
| 320 | case 'n': |
| 321 | s[o] = '\n'; |
| 322 | break; |
| 323 | case 't': |
| 324 | s[o] = '\t'; |
| 325 | break; |
| 326 | case '0': |
| 327 | s[o] = '\0'; |
| 328 | break; |
| 329 | case 'x':{ |
| 330 | /* Yup, this can overflow */ |
| 331 | char hex[] = { s[i + 1], s[i + 2], 0 }; |
| 332 | s[o] = strtoul(hex, NULL, 16); |
| 333 | i += 2; |
| 334 | break; |
| 335 | } |
| 336 | default: |
| 337 | s[o] = ' '; |
| 338 | break; |
| 339 | } |
| 340 | break; |
| 341 | } |
| 342 | default:{ |
| 343 | s[o] = s[i]; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | s[o] = '\0'; |
Jagger | d619ed1 | 2016-09-22 04:08:49 +0200 | [diff] [blame] | 349 | return o; |
Robert Swiecki | fe8d289 | 2016-09-13 17:57:27 +0200 | [diff] [blame] | 350 | } |