| [1466] | 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This program is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| [1494] | 16 | * load-stream.c: loaded stream functions |
|---|
| [1466] | 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| [1478] | 20 | |
|---|
| [1497] | 21 | #define _GNU_SOURCE /* for getline() and getdelim() */ |
|---|
| [1466] | 22 | |
|---|
| 23 | #if defined HAVE_STDINT_H |
|---|
| 24 | # include <stdint.h> |
|---|
| 25 | #elif defined HAVE_INTTYPES_H |
|---|
| 26 | # include <inttypes.h> |
|---|
| 27 | #endif |
|---|
| 28 | #include <stdlib.h> |
|---|
| 29 | |
|---|
| [1494] | 30 | #include <stdio.h> |
|---|
| [1594] | 31 | #include <sys/types.h> |
|---|
| [1728] | 32 | #if defined HAVE___SREFILL && defined HAVE_UNISTD_H |
|---|
| [1611] | 33 | # include <unistd.h> /* Needed for __srefill’s lseek() call */ |
|---|
| 34 | #endif |
|---|
| [1494] | 35 | |
|---|
| [1470] | 36 | #include "libzzuf.h" |
|---|
| [1683] | 37 | #include "lib-load.h" |
|---|
| [1470] | 38 | #include "debug.h" |
|---|
| 39 | #include "fuzz.h" |
|---|
| [1613] | 40 | #include "fd.h" |
|---|
| [1470] | 41 | |
|---|
| [1695] | 42 | #if defined HAVE___SREFILL |
|---|
| [1699] | 43 | int NEW(__srefill)(FILE *fp); |
|---|
| [1602] | 44 | #endif |
|---|
| 45 | |
|---|
| [1470] | 46 | /* Library functions that we divert */ |
|---|
| [1699] | 47 | static FILE * (*ORIG(fopen)) (const char *path, const char *mode); |
|---|
| [1695] | 48 | #if defined HAVE_FOPEN64 |
|---|
| [1699] | 49 | static FILE * (*ORIG(fopen64)) (const char *path, const char *mode); |
|---|
| [1543] | 50 | #endif |
|---|
| [1699] | 51 | static FILE * (*ORIG(freopen)) (const char *path, const char *mode, |
|---|
| 52 | FILE *stream); |
|---|
| 53 | static int (*ORIG(fseek)) (FILE *stream, long offset, int whence); |
|---|
| [1695] | 54 | #if defined HAVE_FSEEKO |
|---|
| [1699] | 55 | static int (*ORIG(fseeko)) (FILE *stream, off_t offset, int whence); |
|---|
| [1594] | 56 | #endif |
|---|
| [1699] | 57 | static void (*ORIG(rewind)) (FILE *stream); |
|---|
| 58 | static size_t (*ORIG(fread)) (void *ptr, size_t size, size_t nmemb, |
|---|
| 59 | FILE *stream); |
|---|
| [2331] | 60 | #if defined HAVE_FREAD_UNLOCKED |
|---|
| 61 | static size_t (*ORIG(fread_unlocked)) (void *ptr, size_t size, size_t nmemb, |
|---|
| 62 | FILE *stream); |
|---|
| 63 | #endif |
|---|
| [1699] | 64 | static int (*ORIG(getc)) (FILE *stream); |
|---|
| 65 | static int (*ORIG(fgetc)) (FILE *stream); |
|---|
| [1695] | 66 | #if defined HAVE__IO_GETC |
|---|
| [1699] | 67 | static int (*ORIG(_IO_getc)) (FILE *stream); |
|---|
| [1600] | 68 | #endif |
|---|
| [2330] | 69 | #if defined HAVE_GETC_UNLOCKED |
|---|
| 70 | static int (*ORIG(getc_unlocked)) (FILE *stream); |
|---|
| 71 | #endif |
|---|
| 72 | #if defined HAVE_FGETC_UNLOCKED |
|---|
| 73 | static int (*ORIG(fgetc_unlocked)) (FILE *stream); |
|---|
| 74 | #endif |
|---|
| [1699] | 75 | static char * (*ORIG(fgets)) (char *s, int size, FILE *stream); |
|---|
| [2331] | 76 | #if defined HAVE_FGETS_UNLOCKED |
|---|
| 77 | static char * (*ORIG(fgets_unlocked)) (char *s, int size, FILE *stream); |
|---|
| 78 | #endif |
|---|
| [1699] | 79 | static int (*ORIG(ungetc)) (int c, FILE *stream); |
|---|
| 80 | static int (*ORIG(fclose)) (FILE *fp); |
|---|
| [1478] | 81 | |
|---|
| [1497] | 82 | /* Additional GNUisms */ |
|---|
| [1695] | 83 | #if defined HAVE_GETLINE |
|---|
| [1699] | 84 | static ssize_t (*ORIG(getline)) (char **lineptr, size_t *n, FILE *stream); |
|---|
| [1543] | 85 | #endif |
|---|
| [1695] | 86 | #if defined HAVE_GETDELIM |
|---|
| [1699] | 87 | static ssize_t (*ORIG(getdelim)) (char **lineptr, size_t *n, int delim, |
|---|
| 88 | FILE *stream); |
|---|
| [1543] | 89 | #endif |
|---|
| [1695] | 90 | #if defined HAVE___GETDELIM |
|---|
| [1699] | 91 | static ssize_t (*ORIG(__getdelim)) (char **lineptr, size_t *n, int delim, |
|---|
| 92 | FILE *stream); |
|---|
| [1543] | 93 | #endif |
|---|
| [1497] | 94 | |
|---|
| [1566] | 95 | /* Additional BSDisms */ |
|---|
| [1695] | 96 | #if defined HAVE_FGETLN |
|---|
| [1699] | 97 | static char * (*ORIG(fgetln)) (FILE *stream, size_t *len); |
|---|
| [1566] | 98 | #endif |
|---|
| [1695] | 99 | #if defined HAVE___SREFILL |
|---|
| [1699] | 100 | int (*ORIG(__srefill)) (FILE *fp); |
|---|
| [1598] | 101 | #endif |
|---|
| [1566] | 102 | |
|---|
| [1466] | 103 | /* Our function wrappers */ |
|---|
| [1482] | 104 | #define FOPEN(fn) \ |
|---|
| [1471] | 105 | do \ |
|---|
| 106 | { \ |
|---|
| [1648] | 107 | LOADSYM(fn); \ |
|---|
| [1523] | 108 | if(!_zz_ready) \ |
|---|
| [1497] | 109 | return ORIG(fn)(path, mode); \ |
|---|
| [1694] | 110 | _zz_lock(-1); \ |
|---|
| [1471] | 111 | ret = ORIG(fn)(path, mode); \ |
|---|
| [1694] | 112 | _zz_unlock(-1); \ |
|---|
| [1527] | 113 | if(ret && _zz_mustwatch(path)) \ |
|---|
| [1471] | 114 | { \ |
|---|
| [1527] | 115 | int fd = fileno(ret); \ |
|---|
| 116 | _zz_register(fd); \ |
|---|
| [1687] | 117 | debug("%s(\"%s\", \"%s\") = [%i]", __func__, path, mode, fd); \ |
|---|
| [1471] | 118 | } \ |
|---|
| 119 | } while(0) |
|---|
| 120 | |
|---|
| [1699] | 121 | FILE *NEW(fopen)(const char *path, const char *mode) |
|---|
| [1466] | 122 | { |
|---|
| [1482] | 123 | FILE *ret; FOPEN(fopen); return ret; |
|---|
| [1471] | 124 | } |
|---|
| [1470] | 125 | |
|---|
| [1695] | 126 | #if defined HAVE_FOPEN64 |
|---|
| [1699] | 127 | FILE *NEW(fopen64)(const char *path, const char *mode) |
|---|
| [1471] | 128 | { |
|---|
| [1482] | 129 | FILE *ret; FOPEN(fopen64); return ret; |
|---|
| [1466] | 130 | } |
|---|
| [1543] | 131 | #endif |
|---|
| [1466] | 132 | |
|---|
| [1699] | 133 | FILE *NEW(freopen)(const char *path, const char *mode, FILE *stream) |
|---|
| [1619] | 134 | { |
|---|
| 135 | FILE *ret; |
|---|
| 136 | int fd0 = -1, fd1 = -1, disp = 0; |
|---|
| 137 | |
|---|
| [1648] | 138 | LOADSYM(freopen); |
|---|
| [1619] | 139 | if(_zz_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0)) |
|---|
| 140 | { |
|---|
| 141 | _zz_unregister(fd0); |
|---|
| 142 | disp = 1; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| [1694] | 145 | _zz_lock(-1); |
|---|
| [1699] | 146 | ret = ORIG(freopen)(path, mode, stream); |
|---|
| [1694] | 147 | _zz_unlock(-1); |
|---|
| [1619] | 148 | |
|---|
| 149 | if(ret && _zz_mustwatch(path)) |
|---|
| 150 | { |
|---|
| 151 | fd1 = fileno(ret); |
|---|
| 152 | _zz_register(fd1); |
|---|
| 153 | disp = 1; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if(disp) |
|---|
| [1687] | 157 | debug("%s(\"%s\", \"%s\", [%i]) = [%i]", __func__, |
|---|
| 158 | path, mode, fd0, fd1); |
|---|
| [1619] | 159 | |
|---|
| 160 | return ret; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| [1606] | 163 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 164 | # define FSEEK_FUZZ(fn2) |
|---|
| 165 | #else |
|---|
| 166 | # define FSEEK_FUZZ(fn2) \ |
|---|
| [1594] | 167 | if(ret == 0) \ |
|---|
| 168 | { \ |
|---|
| 169 | /* FIXME: check what happens when fseek()ing a pipe */ \ |
|---|
| 170 | switch(whence) \ |
|---|
| 171 | { \ |
|---|
| 172 | case SEEK_END: \ |
|---|
| 173 | offset = fn2(stream); \ |
|---|
| 174 | /* fall through */ \ |
|---|
| 175 | case SEEK_SET: \ |
|---|
| 176 | _zz_setpos(fd, offset); \ |
|---|
| 177 | break; \ |
|---|
| 178 | case SEEK_CUR: \ |
|---|
| 179 | _zz_addpos(fd, offset); \ |
|---|
| 180 | break; \ |
|---|
| 181 | } \ |
|---|
| [1606] | 182 | } |
|---|
| 183 | #endif |
|---|
| 184 | |
|---|
| 185 | #define FSEEK(fn, fn2) \ |
|---|
| 186 | do \ |
|---|
| 187 | { \ |
|---|
| 188 | int fd; \ |
|---|
| [1648] | 189 | LOADSYM(fn); \ |
|---|
| [1606] | 190 | fd = fileno(stream); \ |
|---|
| [1791] | 191 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| [1606] | 192 | return ORIG(fn)(stream, offset, whence); \ |
|---|
| [1694] | 193 | _zz_lock(fd); \ |
|---|
| [1606] | 194 | ret = ORIG(fn)(stream, offset, whence); \ |
|---|
| [1694] | 195 | _zz_unlock(fd); \ |
|---|
| [1687] | 196 | debug("%s([%i], %lli, %i) = %i", __func__, \ |
|---|
| [1606] | 197 | fd, (long long int)offset, whence, ret); \ |
|---|
| 198 | FSEEK_FUZZ(fn2) \ |
|---|
| [1594] | 199 | } while(0) |
|---|
| 200 | |
|---|
| [1699] | 201 | int NEW(fseek)(FILE *stream, long offset, int whence) |
|---|
| [1476] | 202 | { |
|---|
| [1594] | 203 | int ret; FSEEK(fseek, ftell); return ret; |
|---|
| 204 | } |
|---|
| [1476] | 205 | |
|---|
| [1695] | 206 | #if defined HAVE_FSEEKO |
|---|
| [1699] | 207 | int NEW(fseeko)(FILE *stream, off_t offset, int whence) |
|---|
| [1594] | 208 | { |
|---|
| 209 | int ret; FSEEK(fseeko, ftello); return ret; |
|---|
| [1476] | 210 | } |
|---|
| [1594] | 211 | #endif |
|---|
| [1476] | 212 | |
|---|
| [1699] | 213 | void NEW(rewind)(FILE *stream) |
|---|
| [1581] | 214 | { |
|---|
| 215 | int fd; |
|---|
| 216 | |
|---|
| [1648] | 217 | LOADSYM(rewind); |
|---|
| [1581] | 218 | fd = fileno(stream); |
|---|
| [1791] | 219 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| [1581] | 220 | { |
|---|
| [1699] | 221 | ORIG(rewind)(stream); |
|---|
| [1581] | 222 | return; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| [1694] | 225 | _zz_lock(fd); |
|---|
| [1699] | 226 | ORIG(rewind)(stream); |
|---|
| [1694] | 227 | _zz_unlock(fd); |
|---|
| [1687] | 228 | debug("%s([%i])", __func__, fd); |
|---|
| [1581] | 229 | |
|---|
| [1606] | 230 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 231 | #else |
|---|
| [1581] | 232 | /* FIXME: check what happens when rewind()ing a pipe */ |
|---|
| 233 | _zz_setpos(fd, 0); |
|---|
| [1606] | 234 | #endif |
|---|
| [1581] | 235 | } |
|---|
| 236 | |
|---|
| [1606] | 237 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| [2331] | 238 | # define FREAD_FUZZ() \ |
|---|
| 239 | do \ |
|---|
| 240 | { \ |
|---|
| 241 | debug("%s(%p, %li, %li, [%i]) = %li", __func__, ptr, \ |
|---|
| 242 | (long int)size, (long int)nmemb, fd, (long int)ret); \ |
|---|
| 243 | } while(0) |
|---|
| [1606] | 244 | #else |
|---|
| [2331] | 245 | # define FREAD_FUZZ() \ |
|---|
| 246 | do \ |
|---|
| 247 | { \ |
|---|
| 248 | int64_t newpos = ftell(stream); \ |
|---|
| 249 | /* XXX: the number of bytes read is not ret * size, because \ |
|---|
| 250 | * a partial read may have advanced the stream pointer. However, \ |
|---|
| 251 | * when reading from a pipe ftell() will return 0, and ret * size \ |
|---|
| 252 | * is then better than nothing. */ \ |
|---|
| 253 | if(newpos <= 0) \ |
|---|
| 254 | { \ |
|---|
| 255 | pos = _zz_getpos(fd); \ |
|---|
| 256 | newpos = pos + ret * size; \ |
|---|
| 257 | } \ |
|---|
| 258 | if(newpos != pos) \ |
|---|
| 259 | { \ |
|---|
| 260 | char *b = ptr; \ |
|---|
| 261 | _zz_fuzz(fd, ptr, newpos - pos); \ |
|---|
| 262 | _zz_setpos(fd, newpos); \ |
|---|
| 263 | if(newpos >= pos + 4) \ |
|---|
| 264 | debug("%s(%p, %li, %li, [%i]) = %li \"%c%c%c%c...", __func__, \ |
|---|
| 265 | ptr, (long int)size, (long int)nmemb, fd, \ |
|---|
| 266 | (long int)ret, b[0], b[1], b[2], b[3]); \ |
|---|
| 267 | else \ |
|---|
| 268 | debug("%s(%p, %li, %li, [%i]) = %li \"%c...", __func__, ptr, \ |
|---|
| 269 | (long int)size, (long int)nmemb, fd, \ |
|---|
| 270 | (long int)ret, b[0]); \ |
|---|
| 271 | } \ |
|---|
| 272 | else \ |
|---|
| 273 | debug("%s(%p, %li, %li, [%i]) = %li", __func__, ptr, \ |
|---|
| 274 | (long int)size, (long int)nmemb, fd, (long int)ret); \ |
|---|
| 275 | } while(0) |
|---|
| [1606] | 276 | #endif |
|---|
| [1473] | 277 | |
|---|
| [2331] | 278 | #define FREAD(fn) \ |
|---|
| 279 | do \ |
|---|
| 280 | { \ |
|---|
| 281 | int64_t pos; \ |
|---|
| 282 | int fd; \ |
|---|
| 283 | LOADSYM(fn); \ |
|---|
| 284 | fd = fileno(stream); \ |
|---|
| 285 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 286 | return ORIG(fn)(ptr, size, nmemb, stream); \ |
|---|
| 287 | pos = ftell(stream); \ |
|---|
| 288 | _zz_lock(fd); \ |
|---|
| 289 | ret = ORIG(fn)(ptr, size, nmemb, stream); \ |
|---|
| 290 | _zz_unlock(fd); \ |
|---|
| 291 | FREAD_FUZZ(); \ |
|---|
| 292 | } while(0) |
|---|
| [1474] | 293 | |
|---|
| [2331] | 294 | size_t NEW(fread)(void *ptr, size_t size, size_t nmemb, FILE *stream) |
|---|
| 295 | { |
|---|
| 296 | size_t ret; FREAD(fread); return ret; |
|---|
| 297 | } |
|---|
| [1591] | 298 | |
|---|
| [2331] | 299 | #if defined HAVE_FREAD_UNLOCKED |
|---|
| 300 | #undef fread_unlocked /* can be a macro; we don’t want that */ |
|---|
| 301 | size_t NEW(fread_unlocked)(void *ptr, size_t size, size_t nmemb, FILE *stream) |
|---|
| 302 | { |
|---|
| 303 | size_t ret; FREAD(fread_unlocked); return ret; |
|---|
| 304 | } |
|---|
| [1602] | 305 | #endif |
|---|
| [1591] | 306 | |
|---|
| [1606] | 307 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| [1602] | 308 | # define FGETC_FUZZ |
|---|
| 309 | #else |
|---|
| 310 | # define FGETC_FUZZ \ |
|---|
| 311 | if(ret != EOF) \ |
|---|
| 312 | { \ |
|---|
| 313 | uint8_t ch = ret; \ |
|---|
| 314 | _zz_fuzz(fd, &ch, 1); \ |
|---|
| 315 | _zz_addpos(fd, 1); \ |
|---|
| 316 | ret = ch; \ |
|---|
| 317 | } |
|---|
| 318 | #endif |
|---|
| 319 | |
|---|
| [1497] | 320 | #define FGETC(fn) \ |
|---|
| 321 | do { \ |
|---|
| 322 | int fd; \ |
|---|
| [1648] | 323 | LOADSYM(fn); \ |
|---|
| [1497] | 324 | fd = fileno(stream); \ |
|---|
| [1791] | 325 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| [1497] | 326 | return ORIG(fn)(stream); \ |
|---|
| [1694] | 327 | _zz_lock(fd); \ |
|---|
| [1497] | 328 | ret = ORIG(fn)(stream); \ |
|---|
| [1694] | 329 | _zz_unlock(fd); \ |
|---|
| [1602] | 330 | FGETC_FUZZ \ |
|---|
| [1724] | 331 | if(ret == EOF) \ |
|---|
| 332 | debug("%s([%i]) = EOF", __func__, fd); \ |
|---|
| [1723] | 333 | else \ |
|---|
| [1722] | 334 | debug("%s([%i]) = '%c'", __func__, fd, ret); \ |
|---|
| [1497] | 335 | } while(0) |
|---|
| 336 | |
|---|
| [1595] | 337 | #undef getc /* can be a macro; we don’t want that */ |
|---|
| [1699] | 338 | int NEW(getc)(FILE *stream) |
|---|
| [1497] | 339 | { |
|---|
| 340 | int ret; FGETC(getc); return ret; |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| [1699] | 343 | int NEW(fgetc)(FILE *stream) |
|---|
| [1497] | 344 | { |
|---|
| 345 | int ret; FGETC(fgetc); return ret; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| [1695] | 348 | #if defined HAVE__IO_GETC |
|---|
| [1699] | 349 | int NEW(_IO_getc)(FILE *stream) |
|---|
| [1595] | 350 | { |
|---|
| 351 | int ret; FGETC(_IO_getc); return ret; |
|---|
| 352 | } |
|---|
| 353 | #endif |
|---|
| 354 | |
|---|
| [2330] | 355 | #if defined HAVE_GETC_UNLOCKED |
|---|
| [2331] | 356 | #undef getc_unlocked /* can be a macro; we don’t want that */ |
|---|
| [2330] | 357 | int NEW(getc_unlocked)(FILE *stream) |
|---|
| 358 | { |
|---|
| 359 | int ret; FGETC(getc_unlocked); return ret; |
|---|
| 360 | } |
|---|
| 361 | #endif |
|---|
| 362 | |
|---|
| 363 | #if defined HAVE_FGETC_UNLOCKED |
|---|
| [2331] | 364 | #undef fgetc_unlocked /* can be a macro; we don’t want that */ |
|---|
| [2330] | 365 | int NEW(fgetc_unlocked)(FILE *stream) |
|---|
| 366 | { |
|---|
| 367 | int ret; FGETC(fgetc_unlocked); return ret; |
|---|
| 368 | } |
|---|
| 369 | #endif |
|---|
| 370 | |
|---|
| [1606] | 371 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| [2331] | 372 | # define FGETS_FUZZ \ |
|---|
| 373 | _zz_lock(fd); \ |
|---|
| 374 | ret = ORIG(fgets)(s, size, stream); \ |
|---|
| 375 | _zz_unlock(fd); |
|---|
| [1602] | 376 | #else |
|---|
| [2331] | 377 | # define FGETS_FUZZ \ |
|---|
| 378 | if(size <= 0) \ |
|---|
| 379 | ret = NULL; \ |
|---|
| 380 | else if(size == 1) \ |
|---|
| 381 | s[0] = '\0'; \ |
|---|
| 382 | else \ |
|---|
| 383 | { \ |
|---|
| 384 | int i; \ |
|---|
| 385 | for(i = 0; i < size - 1; i++) \ |
|---|
| 386 | { \ |
|---|
| 387 | int ch; \ |
|---|
| 388 | _zz_lock(fd); \ |
|---|
| 389 | ch = ORIG(fgetc)(stream); \ |
|---|
| 390 | _zz_unlock(fd); \ |
|---|
| 391 | if(ch == EOF) \ |
|---|
| 392 | { \ |
|---|
| 393 | s[i] = '\0'; \ |
|---|
| 394 | if(!i) \ |
|---|
| 395 | ret = NULL; \ |
|---|
| 396 | break; \ |
|---|
| 397 | } \ |
|---|
| 398 | s[i] = (char)(unsigned char)ch; \ |
|---|
| 399 | _zz_fuzz(fd, (uint8_t *)s + i, 1); /* rather inefficient */ \ |
|---|
| 400 | _zz_addpos(fd, 1); \ |
|---|
| 401 | if(s[i] == '\n') \ |
|---|
| 402 | { \ |
|---|
| 403 | s[i + 1] = '\0'; \ |
|---|
| 404 | break; \ |
|---|
| 405 | } \ |
|---|
| 406 | } \ |
|---|
| 407 | } |
|---|
| 408 | #endif |
|---|
| [1603] | 409 | |
|---|
| [2331] | 410 | #define FGETS(fn) \ |
|---|
| 411 | do \ |
|---|
| 412 | { \ |
|---|
| 413 | int fd; \ |
|---|
| 414 | ret = s; \ |
|---|
| 415 | LOADSYM(fn); \ |
|---|
| 416 | LOADSYM(fgetc); \ |
|---|
| 417 | fd = fileno(stream); \ |
|---|
| 418 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| 419 | return ORIG(fn)(s, size, stream); \ |
|---|
| 420 | FGETS_FUZZ \ |
|---|
| 421 | debug("%s(%p, %i, [%i]) = %p", __func__, s, size, fd, ret); \ |
|---|
| 422 | } while(0) |
|---|
| [1497] | 423 | |
|---|
| [2331] | 424 | char *NEW(fgets)(char *s, int size, FILE *stream) |
|---|
| 425 | { |
|---|
| 426 | char *ret; FGETS(fgets); return ret; |
|---|
| 427 | } |
|---|
| [1553] | 428 | |
|---|
| [2331] | 429 | #if defined HAVE_FGETS_UNLOCKED |
|---|
| 430 | char *NEW(fgets_unlocked)(char *s, int size, FILE *stream) |
|---|
| 431 | { |
|---|
| 432 | char *ret; FGETS(fgets_unlocked); return ret; |
|---|
| 433 | } |
|---|
| [1602] | 434 | #endif |
|---|
| [1497] | 435 | |
|---|
| [1699] | 436 | int NEW(ungetc)(int c, FILE *stream) |
|---|
| [1497] | 437 | { |
|---|
| 438 | int ret, fd; |
|---|
| 439 | |
|---|
| [1648] | 440 | LOADSYM(ungetc); |
|---|
| [1497] | 441 | fd = fileno(stream); |
|---|
| [1791] | 442 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| [1699] | 443 | return ORIG(ungetc)(c, stream); |
|---|
| [1497] | 444 | |
|---|
| [1694] | 445 | _zz_lock(fd); |
|---|
| [1719] | 446 | ret = ORIG(ungetc)(c, stream); |
|---|
| [1694] | 447 | _zz_unlock(fd); |
|---|
| [1606] | 448 | |
|---|
| [1719] | 449 | if(ret != EOF) |
|---|
| 450 | { |
|---|
| 451 | struct fuzz *fuzz = _zz_getfuzz(fd); |
|---|
| 452 | fuzz->uflag = 1; |
|---|
| 453 | fuzz->upos = _zz_getpos(fd) - 1; |
|---|
| 454 | fuzz->uchar = c; |
|---|
| [1606] | 455 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 456 | #else |
|---|
| [1719] | 457 | _zz_addpos(fd, -1); |
|---|
| [1606] | 458 | #endif |
|---|
| [1719] | 459 | } |
|---|
| [1606] | 460 | |
|---|
| [1724] | 461 | if(ret == EOF) |
|---|
| 462 | debug("%s(0x%02x, [%i]) = EOF", __func__, c, fd); |
|---|
| [1722] | 463 | else |
|---|
| 464 | debug("%s(0x%02x, [%i]) = '%c'", __func__, c, fd, ret); |
|---|
| 465 | |
|---|
| [1497] | 466 | return ret; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| [1699] | 469 | int NEW(fclose)(FILE *fp) |
|---|
| [1478] | 470 | { |
|---|
| 471 | int ret, fd; |
|---|
| 472 | |
|---|
| [1648] | 473 | LOADSYM(fclose); |
|---|
| [1478] | 474 | fd = fileno(fp); |
|---|
| [1527] | 475 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| [1699] | 476 | return ORIG(fclose)(fp); |
|---|
| [1497] | 477 | |
|---|
| [1694] | 478 | _zz_lock(fd); |
|---|
| [1699] | 479 | ret = ORIG(fclose)(fp); |
|---|
| [1694] | 480 | _zz_unlock(fd); |
|---|
| [1687] | 481 | debug("%s([%i]) = %i", __func__, fd, ret); |
|---|
| [1524] | 482 | _zz_unregister(fd); |
|---|
| [1478] | 483 | |
|---|
| 484 | return ret; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| [1497] | 487 | #define GETDELIM(fn, delim, need_delim) \ |
|---|
| 488 | do { \ |
|---|
| 489 | char *line; \ |
|---|
| 490 | ssize_t done, size; \ |
|---|
| 491 | int fd, finished = 0; \ |
|---|
| [1648] | 492 | LOADSYM(fn); \ |
|---|
| [1679] | 493 | LOADSYM(getdelim); \ |
|---|
| 494 | LOADSYM(fgetc); \ |
|---|
| [1497] | 495 | fd = fileno(stream); \ |
|---|
| [1791] | 496 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) \ |
|---|
| [1699] | 497 | return ORIG(getdelim)(lineptr, n, delim, stream); \ |
|---|
| [1497] | 498 | line = *lineptr; \ |
|---|
| 499 | size = line ? *n : 0; \ |
|---|
| 500 | ret = done = finished = 0; \ |
|---|
| 501 | for(;;) \ |
|---|
| 502 | { \ |
|---|
| 503 | int ch; \ |
|---|
| 504 | if(done >= size) /* highly inefficient but I don't care */ \ |
|---|
| 505 | line = realloc(line, size = done + 1); \ |
|---|
| 506 | if(finished) \ |
|---|
| 507 | { \ |
|---|
| 508 | line[done] = '\0'; \ |
|---|
| 509 | *n = size; \ |
|---|
| 510 | *lineptr = line; \ |
|---|
| 511 | break; \ |
|---|
| 512 | } \ |
|---|
| [1694] | 513 | _zz_lock(fd); \ |
|---|
| [1699] | 514 | ch = ORIG(fgetc)(stream); \ |
|---|
| [1694] | 515 | _zz_unlock(fd); \ |
|---|
| [1497] | 516 | if(ch == EOF) \ |
|---|
| 517 | { \ |
|---|
| 518 | finished = 1; \ |
|---|
| 519 | ret = done; \ |
|---|
| 520 | } \ |
|---|
| 521 | else \ |
|---|
| 522 | { \ |
|---|
| 523 | unsigned char c = ch; \ |
|---|
| [1523] | 524 | _zz_fuzz(fd, &c, 1); /* even more inefficient */ \ |
|---|
| [1497] | 525 | line[done++] = c; \ |
|---|
| [1524] | 526 | _zz_addpos(fd, 1); \ |
|---|
| [1497] | 527 | if(c == delim) \ |
|---|
| 528 | { \ |
|---|
| 529 | finished = 1; \ |
|---|
| 530 | ret = done; \ |
|---|
| 531 | } \ |
|---|
| 532 | } \ |
|---|
| 533 | } \ |
|---|
| 534 | if(need_delim) \ |
|---|
| [1688] | 535 | debug("%s(%p, %p, '%c', [%i]) = %li", __func__, \ |
|---|
| [1551] | 536 | lineptr, n, delim, fd, (long int)ret); \ |
|---|
| [1497] | 537 | else \ |
|---|
| [1687] | 538 | debug("%s(%p, %p, [%i]) = %li", __func__, \ |
|---|
| [1551] | 539 | lineptr, n, fd, (long int)ret); \ |
|---|
| [1497] | 540 | return ret; \ |
|---|
| 541 | } while(0) |
|---|
| 542 | |
|---|
| [1695] | 543 | #if defined HAVE_GETLINE |
|---|
| [1699] | 544 | ssize_t NEW(getline)(char **lineptr, size_t *n, FILE *stream) |
|---|
| [1497] | 545 | { |
|---|
| 546 | ssize_t ret; GETDELIM(getline, '\n', 0); return ret; |
|---|
| 547 | } |
|---|
| [1543] | 548 | #endif |
|---|
| [1497] | 549 | |
|---|
| [1695] | 550 | #if defined HAVE_GETDELIM |
|---|
| [1699] | 551 | ssize_t NEW(getdelim)(char **lineptr, size_t *n, int delim, FILE *stream) |
|---|
| [1497] | 552 | { |
|---|
| 553 | ssize_t ret; GETDELIM(getdelim, delim, 1); return ret; |
|---|
| 554 | } |
|---|
| [1543] | 555 | #endif |
|---|
| [1497] | 556 | |
|---|
| [1695] | 557 | #if defined HAVE___GETDELIM |
|---|
| [1699] | 558 | ssize_t NEW(__getdelim)(char **lineptr, size_t *n, int delim, FILE *stream) |
|---|
| [1497] | 559 | { |
|---|
| 560 | ssize_t ret; GETDELIM(__getdelim, delim, 1); return ret; |
|---|
| 561 | } |
|---|
| [1543] | 562 | #endif |
|---|
| [1497] | 563 | |
|---|
| [1695] | 564 | #if defined HAVE_FGETLN |
|---|
| [1699] | 565 | char *NEW(fgetln)(FILE *stream, size_t *len) |
|---|
| [1566] | 566 | { |
|---|
| [1602] | 567 | char *ret; |
|---|
| [1606] | 568 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| [1604] | 569 | #else |
|---|
| [1566] | 570 | struct fuzz *fuzz; |
|---|
| 571 | size_t i, size; |
|---|
| [1603] | 572 | #endif |
|---|
| [1566] | 573 | int fd; |
|---|
| 574 | |
|---|
| [1648] | 575 | LOADSYM(fgetln); |
|---|
| [1679] | 576 | LOADSYM(fgetc); |
|---|
| [1566] | 577 | fd = fileno(stream); |
|---|
| [1791] | 578 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| [1699] | 579 | return ORIG(fgetln)(stream, len); |
|---|
| [1566] | 580 | |
|---|
| [1606] | 581 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| [1694] | 582 | _zz_lock(fd); |
|---|
| [1699] | 583 | ret = ORIG(fgetln)(stream, len); |
|---|
| [1694] | 584 | _zz_unlock(fd); |
|---|
| [1602] | 585 | #else |
|---|
| [1566] | 586 | fuzz = _zz_getfuzz(fd); |
|---|
| 587 | |
|---|
| [1593] | 588 | for(i = size = 0; ; /* i is incremented below */) |
|---|
| [1566] | 589 | { |
|---|
| 590 | int ch; |
|---|
| 591 | |
|---|
| [1694] | 592 | _zz_lock(fd); |
|---|
| [1699] | 593 | ch = ORIG(fgetc)(stream); |
|---|
| [1694] | 594 | _zz_unlock(fd); |
|---|
| [1566] | 595 | |
|---|
| 596 | if(ch == EOF) |
|---|
| 597 | break; |
|---|
| 598 | |
|---|
| [1567] | 599 | if(i >= size) |
|---|
| 600 | fuzz->tmp = realloc(fuzz->tmp, (size += 80)); |
|---|
| 601 | |
|---|
| 602 | fuzz->tmp[i] = (char)(unsigned char)ch; |
|---|
| 603 | _zz_fuzz(fd, (uint8_t *)fuzz->tmp + i, 1); /* rather inefficient */ |
|---|
| [1566] | 604 | _zz_addpos(fd, 1); |
|---|
| [1567] | 605 | |
|---|
| [1593] | 606 | if(fuzz->tmp[i++] == '\n') |
|---|
| [1567] | 607 | break; |
|---|
| [1566] | 608 | } |
|---|
| 609 | |
|---|
| [1567] | 610 | *len = i; |
|---|
| [1602] | 611 | ret = fuzz->tmp; |
|---|
| 612 | #endif |
|---|
| [1566] | 613 | |
|---|
| [1687] | 614 | debug("%s([%i], &%li) = %p", __func__, fd, (long int)*len, ret); |
|---|
| [1602] | 615 | return ret; |
|---|
| [1566] | 616 | } |
|---|
| 617 | #endif |
|---|
| 618 | |
|---|
| [1695] | 619 | #if defined HAVE___SREFILL |
|---|
| [1699] | 620 | int NEW(__srefill)(FILE *fp) |
|---|
| [1598] | 621 | { |
|---|
| [1609] | 622 | off_t newpos; |
|---|
| 623 | int ret, fd, tmp; |
|---|
| [1598] | 624 | |
|---|
| [1648] | 625 | LOADSYM(__srefill); |
|---|
| [1599] | 626 | fd = fileno(fp); |
|---|
| [1791] | 627 | if(!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) |
|---|
| [1699] | 628 | return ORIG(__srefill)(fp); |
|---|
| [1598] | 629 | |
|---|
| [1694] | 630 | _zz_lock(fd); |
|---|
| [1699] | 631 | ret = ORIG(__srefill)(fp); |
|---|
| [1606] | 632 | newpos = lseek(fd, 0, SEEK_CUR); |
|---|
| [1697] | 633 | _zz_unlock(fd); |
|---|
| [1602] | 634 | if(ret != EOF) |
|---|
| [1606] | 635 | { |
|---|
| 636 | if(newpos != -1) |
|---|
| 637 | _zz_setpos(fd, newpos - fp->_r); |
|---|
| [1602] | 638 | _zz_fuzz(fd, fp->_p, fp->_r); |
|---|
| [1606] | 639 | _zz_addpos(fd, fp->_r); |
|---|
| 640 | } |
|---|
| [1602] | 641 | |
|---|
| [1694] | 642 | if(!_zz_islocked(fd)) |
|---|
| [1687] | 643 | debug("%s([%i]) = %i", __func__, fd, ret); |
|---|
| [1605] | 644 | |
|---|
| [1598] | 645 | return ret; |
|---|
| 646 | } |
|---|
| 647 | #endif |
|---|
| 648 | |
|---|