| 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 | /* |
|---|
| 16 | * load-stream.c: loaded stream functions |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | |
|---|
| 21 | #define _GNU_SOURCE /* for getline() and getdelim() */ |
|---|
| 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 | |
|---|
| 30 | #include <stdio.h> |
|---|
| 31 | #include <sys/types.h> |
|---|
| 32 | #if defined HAVE___SREFILL |
|---|
| 33 | # include <unistd.h> /* Needed for __srefill’s lseek() call */ |
|---|
| 34 | #endif |
|---|
| 35 | |
|---|
| 36 | #include "libzzuf.h" |
|---|
| 37 | #include "lib-load.h" |
|---|
| 38 | #include "debug.h" |
|---|
| 39 | #include "fuzz.h" |
|---|
| 40 | #include "fd.h" |
|---|
| 41 | |
|---|
| 42 | #if defined HAVE___SREFILL |
|---|
| 43 | int NEW(__srefill)(FILE *fp); |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 46 | /* Library functions that we divert */ |
|---|
| 47 | static FILE * (*ORIG(fopen)) (const char *path, const char *mode); |
|---|
| 48 | #if defined HAVE_FOPEN64 |
|---|
| 49 | static FILE * (*ORIG(fopen64)) (const char *path, const char *mode); |
|---|
| 50 | #endif |
|---|
| 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); |
|---|
| 54 | #if defined HAVE_FSEEKO |
|---|
| 55 | static int (*ORIG(fseeko)) (FILE *stream, off_t offset, int whence); |
|---|
| 56 | #endif |
|---|
| 57 | static void (*ORIG(rewind)) (FILE *stream); |
|---|
| 58 | static size_t (*ORIG(fread)) (void *ptr, size_t size, size_t nmemb, |
|---|
| 59 | FILE *stream); |
|---|
| 60 | static int (*ORIG(getc)) (FILE *stream); |
|---|
| 61 | static int (*ORIG(fgetc)) (FILE *stream); |
|---|
| 62 | #if defined HAVE__IO_GETC |
|---|
| 63 | static int (*ORIG(_IO_getc)) (FILE *stream); |
|---|
| 64 | #endif |
|---|
| 65 | static char * (*ORIG(fgets)) (char *s, int size, FILE *stream); |
|---|
| 66 | static int (*ORIG(ungetc)) (int c, FILE *stream); |
|---|
| 67 | static int (*ORIG(fclose)) (FILE *fp); |
|---|
| 68 | |
|---|
| 69 | /* Additional GNUisms */ |
|---|
| 70 | #if defined HAVE_GETLINE |
|---|
| 71 | static ssize_t (*ORIG(getline)) (char **lineptr, size_t *n, FILE *stream); |
|---|
| 72 | #endif |
|---|
| 73 | #if defined HAVE_GETDELIM |
|---|
| 74 | static ssize_t (*ORIG(getdelim)) (char **lineptr, size_t *n, int delim, |
|---|
| 75 | FILE *stream); |
|---|
| 76 | #endif |
|---|
| 77 | #if defined HAVE___GETDELIM |
|---|
| 78 | static ssize_t (*ORIG(__getdelim)) (char **lineptr, size_t *n, int delim, |
|---|
| 79 | FILE *stream); |
|---|
| 80 | #endif |
|---|
| 81 | |
|---|
| 82 | /* Additional BSDisms */ |
|---|
| 83 | #if defined HAVE_FGETLN |
|---|
| 84 | static char * (*ORIG(fgetln)) (FILE *stream, size_t *len); |
|---|
| 85 | #endif |
|---|
| 86 | #if defined HAVE___SREFILL |
|---|
| 87 | int (*ORIG(__srefill)) (FILE *fp); |
|---|
| 88 | #endif |
|---|
| 89 | |
|---|
| 90 | /* Our function wrappers */ |
|---|
| 91 | #define FOPEN(fn) \ |
|---|
| 92 | do \ |
|---|
| 93 | { \ |
|---|
| 94 | LOADSYM(fn); \ |
|---|
| 95 | if(!_zz_ready) \ |
|---|
| 96 | return ORIG(fn)(path, mode); \ |
|---|
| 97 | _zz_lock(-1); \ |
|---|
| 98 | ret = ORIG(fn)(path, mode); \ |
|---|
| 99 | _zz_unlock(-1); \ |
|---|
| 100 | if(ret && _zz_mustwatch(path)) \ |
|---|
| 101 | { \ |
|---|
| 102 | int fd = fileno(ret); \ |
|---|
| 103 | _zz_register(fd); \ |
|---|
| 104 | debug("%s(\"%s\", \"%s\") = [%i]", __func__, path, mode, fd); \ |
|---|
| 105 | } \ |
|---|
| 106 | } while(0) |
|---|
| 107 | |
|---|
| 108 | FILE *NEW(fopen)(const char *path, const char *mode) |
|---|
| 109 | { |
|---|
| 110 | FILE *ret; FOPEN(fopen); return ret; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | #if defined HAVE_FOPEN64 |
|---|
| 114 | FILE *NEW(fopen64)(const char *path, const char *mode) |
|---|
| 115 | { |
|---|
| 116 | FILE *ret; FOPEN(fopen64); return ret; |
|---|
| 117 | } |
|---|
| 118 | #endif |
|---|
| 119 | |
|---|
| 120 | FILE *NEW(freopen)(const char *path, const char *mode, FILE *stream) |
|---|
| 121 | { |
|---|
| 122 | FILE *ret; |
|---|
| 123 | int fd0 = -1, fd1 = -1, disp = 0; |
|---|
| 124 | |
|---|
| 125 | LOADSYM(freopen); |
|---|
| 126 | if(_zz_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0)) |
|---|
| 127 | { |
|---|
| 128 | _zz_unregister(fd0); |
|---|
| 129 | disp = 1; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | _zz_lock(-1); |
|---|
| 133 | ret = ORIG(freopen)(path, mode, stream); |
|---|
| 134 | _zz_unlock(-1); |
|---|
| 135 | |
|---|
| 136 | if(ret && _zz_mustwatch(path)) |
|---|
| 137 | { |
|---|
| 138 | fd1 = fileno(ret); |
|---|
| 139 | _zz_register(fd1); |
|---|
| 140 | disp = 1; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | if(disp) |
|---|
| 144 | debug("%s(\"%s\", \"%s\", [%i]) = [%i]", __func__, |
|---|
| 145 | path, mode, fd0, fd1); |
|---|
| 146 | |
|---|
| 147 | return ret; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 151 | # define FSEEK_FUZZ(fn2) |
|---|
| 152 | #else |
|---|
| 153 | # define FSEEK_FUZZ(fn2) \ |
|---|
| 154 | if(ret == 0) \ |
|---|
| 155 | { \ |
|---|
| 156 | /* FIXME: check what happens when fseek()ing a pipe */ \ |
|---|
| 157 | switch(whence) \ |
|---|
| 158 | { \ |
|---|
| 159 | case SEEK_END: \ |
|---|
| 160 | offset = fn2(stream); \ |
|---|
| 161 | /* fall through */ \ |
|---|
| 162 | case SEEK_SET: \ |
|---|
| 163 | _zz_setpos(fd, offset); \ |
|---|
| 164 | break; \ |
|---|
| 165 | case SEEK_CUR: \ |
|---|
| 166 | _zz_addpos(fd, offset); \ |
|---|
| 167 | break; \ |
|---|
| 168 | } \ |
|---|
| 169 | } |
|---|
| 170 | #endif |
|---|
| 171 | |
|---|
| 172 | #define FSEEK(fn, fn2) \ |
|---|
| 173 | do \ |
|---|
| 174 | { \ |
|---|
| 175 | int fd; \ |
|---|
| 176 | LOADSYM(fn); \ |
|---|
| 177 | fd = fileno(stream); \ |
|---|
| 178 | if(!_zz_ready || !_zz_iswatched(fd)) \ |
|---|
| 179 | return ORIG(fn)(stream, offset, whence); \ |
|---|
| 180 | _zz_lock(fd); \ |
|---|
| 181 | ret = ORIG(fn)(stream, offset, whence); \ |
|---|
| 182 | _zz_unlock(fd); \ |
|---|
| 183 | debug("%s([%i], %lli, %i) = %i", __func__, \ |
|---|
| 184 | fd, (long long int)offset, whence, ret); \ |
|---|
| 185 | FSEEK_FUZZ(fn2) \ |
|---|
| 186 | } while(0) |
|---|
| 187 | |
|---|
| 188 | int NEW(fseek)(FILE *stream, long offset, int whence) |
|---|
| 189 | { |
|---|
| 190 | int ret; FSEEK(fseek, ftell); return ret; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | #if defined HAVE_FSEEKO |
|---|
| 194 | int NEW(fseeko)(FILE *stream, off_t offset, int whence) |
|---|
| 195 | { |
|---|
| 196 | int ret; FSEEK(fseeko, ftello); return ret; |
|---|
| 197 | } |
|---|
| 198 | #endif |
|---|
| 199 | |
|---|
| 200 | void NEW(rewind)(FILE *stream) |
|---|
| 201 | { |
|---|
| 202 | int fd; |
|---|
| 203 | |
|---|
| 204 | LOADSYM(rewind); |
|---|
| 205 | fd = fileno(stream); |
|---|
| 206 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 207 | { |
|---|
| 208 | ORIG(rewind)(stream); |
|---|
| 209 | return; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | _zz_lock(fd); |
|---|
| 213 | ORIG(rewind)(stream); |
|---|
| 214 | _zz_unlock(fd); |
|---|
| 215 | debug("%s([%i])", __func__, fd); |
|---|
| 216 | |
|---|
| 217 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 218 | #else |
|---|
| 219 | /* FIXME: check what happens when rewind()ing a pipe */ |
|---|
| 220 | _zz_setpos(fd, 0); |
|---|
| 221 | #endif |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | size_t NEW(fread)(void *ptr, size_t size, size_t nmemb, FILE *stream) |
|---|
| 225 | { |
|---|
| 226 | long int pos; |
|---|
| 227 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 228 | #else |
|---|
| 229 | long int newpos; |
|---|
| 230 | #endif |
|---|
| 231 | size_t ret; |
|---|
| 232 | int fd; |
|---|
| 233 | |
|---|
| 234 | LOADSYM(fread); |
|---|
| 235 | fd = fileno(stream); |
|---|
| 236 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 237 | return ORIG(fread)(ptr, size, nmemb, stream); |
|---|
| 238 | |
|---|
| 239 | pos = ftell(stream); |
|---|
| 240 | _zz_lock(fd); |
|---|
| 241 | ret = ORIG(fread)(ptr, size, nmemb, stream); |
|---|
| 242 | _zz_unlock(fd); |
|---|
| 243 | |
|---|
| 244 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 245 | #else |
|---|
| 246 | newpos = ftell(stream); |
|---|
| 247 | /* XXX: the number of bytes read is not ret * size, because |
|---|
| 248 | * a partial read may have advanced the stream pointer. However, |
|---|
| 249 | * when reading from a pipe ftell() will return 0, and ret * size |
|---|
| 250 | * is then better than nothing. */ |
|---|
| 251 | if(newpos <= 0) |
|---|
| 252 | { |
|---|
| 253 | pos = _zz_getpos(fd); |
|---|
| 254 | newpos = pos + ret * size; |
|---|
| 255 | } |
|---|
| 256 | if(newpos != pos) |
|---|
| 257 | { |
|---|
| 258 | char *b = ptr; |
|---|
| 259 | |
|---|
| 260 | _zz_fuzz(fd, ptr, newpos - pos); |
|---|
| 261 | _zz_setpos(fd, newpos); |
|---|
| 262 | |
|---|
| 263 | if(newpos >= pos + 4) |
|---|
| 264 | debug("%s(%p, %li, %li, [%i]) = %li \"%c%c%c%c...", __func__, ptr, |
|---|
| 265 | (long int)size, (long int)nmemb, fd, (long int)ret, |
|---|
| 266 | 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, (long int)ret, b[0]); |
|---|
| 270 | } |
|---|
| 271 | else |
|---|
| 272 | #endif |
|---|
| 273 | debug("%s(%p, %li, %li, [%i]) = %li", __func__, ptr, |
|---|
| 274 | (long int)size, (long int)nmemb, fd, (long int)ret); |
|---|
| 275 | |
|---|
| 276 | return ret; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 280 | # define FGETC_FUZZ |
|---|
| 281 | #else |
|---|
| 282 | # define FGETC_FUZZ \ |
|---|
| 283 | if(ret != EOF) \ |
|---|
| 284 | { \ |
|---|
| 285 | uint8_t ch = ret; \ |
|---|
| 286 | _zz_fuzz(fd, &ch, 1); \ |
|---|
| 287 | _zz_addpos(fd, 1); \ |
|---|
| 288 | ret = ch; \ |
|---|
| 289 | } |
|---|
| 290 | #endif |
|---|
| 291 | |
|---|
| 292 | #define FGETC(fn) \ |
|---|
| 293 | do { \ |
|---|
| 294 | int fd; \ |
|---|
| 295 | LOADSYM(fn); \ |
|---|
| 296 | fd = fileno(stream); \ |
|---|
| 297 | if(!_zz_ready || !_zz_iswatched(fd)) \ |
|---|
| 298 | return ORIG(fn)(stream); \ |
|---|
| 299 | _zz_lock(fd); \ |
|---|
| 300 | ret = ORIG(fn)(stream); \ |
|---|
| 301 | _zz_unlock(fd); \ |
|---|
| 302 | FGETC_FUZZ \ |
|---|
| 303 | debug("%s([%i]) = '%c'", __func__, fd, ret); \ |
|---|
| 304 | } while(0) |
|---|
| 305 | |
|---|
| 306 | #undef getc /* can be a macro; we don’t want that */ |
|---|
| 307 | int NEW(getc)(FILE *stream) |
|---|
| 308 | { |
|---|
| 309 | int ret; FGETC(getc); return ret; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | int NEW(fgetc)(FILE *stream) |
|---|
| 313 | { |
|---|
| 314 | int ret; FGETC(fgetc); return ret; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | #if defined HAVE__IO_GETC |
|---|
| 318 | int NEW(_IO_getc)(FILE *stream) |
|---|
| 319 | { |
|---|
| 320 | int ret; FGETC(_IO_getc); return ret; |
|---|
| 321 | } |
|---|
| 322 | #endif |
|---|
| 323 | |
|---|
| 324 | char *NEW(fgets)(char *s, int size, FILE *stream) |
|---|
| 325 | { |
|---|
| 326 | char *ret = s; |
|---|
| 327 | int fd; |
|---|
| 328 | |
|---|
| 329 | LOADSYM(fgets); |
|---|
| 330 | LOADSYM(fgetc); |
|---|
| 331 | fd = fileno(stream); |
|---|
| 332 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 333 | return ORIG(fgets)(s, size, stream); |
|---|
| 334 | |
|---|
| 335 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 336 | _zz_lock(fd); |
|---|
| 337 | ret = ORIG(fgets)(s, size, stream); |
|---|
| 338 | _zz_unlock(fd); |
|---|
| 339 | #else |
|---|
| 340 | if(size <= 0) |
|---|
| 341 | ret = NULL; |
|---|
| 342 | else if(size == 1) |
|---|
| 343 | s[0] = '\0'; |
|---|
| 344 | else |
|---|
| 345 | { |
|---|
| 346 | int i; |
|---|
| 347 | |
|---|
| 348 | for(i = 0; i < size - 1; i++) |
|---|
| 349 | { |
|---|
| 350 | int ch; |
|---|
| 351 | |
|---|
| 352 | _zz_lock(fd); |
|---|
| 353 | ch = ORIG(fgetc)(stream); |
|---|
| 354 | _zz_unlock(fd); |
|---|
| 355 | |
|---|
| 356 | if(ch == EOF) |
|---|
| 357 | { |
|---|
| 358 | s[i] = '\0'; |
|---|
| 359 | if(!i) |
|---|
| 360 | ret = NULL; |
|---|
| 361 | break; |
|---|
| 362 | } |
|---|
| 363 | s[i] = (char)(unsigned char)ch; |
|---|
| 364 | _zz_fuzz(fd, (uint8_t *)s + i, 1); /* rather inefficient */ |
|---|
| 365 | _zz_addpos(fd, 1); |
|---|
| 366 | if(s[i] == '\n') |
|---|
| 367 | { |
|---|
| 368 | s[i + 1] = '\0'; |
|---|
| 369 | break; |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | #endif |
|---|
| 374 | |
|---|
| 375 | debug("%s(%p, %i, [%i]) = %p", __func__, s, size, fd, ret); |
|---|
| 376 | return ret; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | int NEW(ungetc)(int c, FILE *stream) |
|---|
| 380 | { |
|---|
| 381 | int ret, fd; |
|---|
| 382 | |
|---|
| 383 | LOADSYM(ungetc); |
|---|
| 384 | fd = fileno(stream); |
|---|
| 385 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 386 | return ORIG(ungetc)(c, stream); |
|---|
| 387 | |
|---|
| 388 | _zz_lock(fd); |
|---|
| 389 | ret = ORIG(ungetc)(c, stream); |
|---|
| 390 | _zz_unlock(fd); |
|---|
| 391 | |
|---|
| 392 | if(ret != EOF) |
|---|
| 393 | { |
|---|
| 394 | struct fuzz *fuzz = _zz_getfuzz(fd); |
|---|
| 395 | fuzz->uflag = 1; |
|---|
| 396 | fuzz->upos = _zz_getpos(fd) - 1; |
|---|
| 397 | fuzz->uchar = c; |
|---|
| 398 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 399 | #else |
|---|
| 400 | _zz_addpos(fd, -1); |
|---|
| 401 | #endif |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | debug("%s(0x%02x, [%i]) = '%c'", __func__, c, fd, ret); |
|---|
| 405 | return ret; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | int NEW(fclose)(FILE *fp) |
|---|
| 409 | { |
|---|
| 410 | int ret, fd; |
|---|
| 411 | |
|---|
| 412 | LOADSYM(fclose); |
|---|
| 413 | fd = fileno(fp); |
|---|
| 414 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 415 | return ORIG(fclose)(fp); |
|---|
| 416 | |
|---|
| 417 | _zz_lock(fd); |
|---|
| 418 | ret = ORIG(fclose)(fp); |
|---|
| 419 | _zz_unlock(fd); |
|---|
| 420 | debug("%s([%i]) = %i", __func__, fd, ret); |
|---|
| 421 | _zz_unregister(fd); |
|---|
| 422 | |
|---|
| 423 | return ret; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | #define GETDELIM(fn, delim, need_delim) \ |
|---|
| 427 | do { \ |
|---|
| 428 | char *line; \ |
|---|
| 429 | ssize_t done, size; \ |
|---|
| 430 | int fd, finished = 0; \ |
|---|
| 431 | LOADSYM(fn); \ |
|---|
| 432 | LOADSYM(getdelim); \ |
|---|
| 433 | LOADSYM(fgetc); \ |
|---|
| 434 | fd = fileno(stream); \ |
|---|
| 435 | if(!_zz_ready || !_zz_iswatched(fd)) \ |
|---|
| 436 | return ORIG(getdelim)(lineptr, n, delim, stream); \ |
|---|
| 437 | line = *lineptr; \ |
|---|
| 438 | size = line ? *n : 0; \ |
|---|
| 439 | ret = done = finished = 0; \ |
|---|
| 440 | for(;;) \ |
|---|
| 441 | { \ |
|---|
| 442 | int ch; \ |
|---|
| 443 | if(done >= size) /* highly inefficient but I don't care */ \ |
|---|
| 444 | line = realloc(line, size = done + 1); \ |
|---|
| 445 | if(finished) \ |
|---|
| 446 | { \ |
|---|
| 447 | line[done] = '\0'; \ |
|---|
| 448 | *n = size; \ |
|---|
| 449 | *lineptr = line; \ |
|---|
| 450 | break; \ |
|---|
| 451 | } \ |
|---|
| 452 | _zz_lock(fd); \ |
|---|
| 453 | ch = ORIG(fgetc)(stream); \ |
|---|
| 454 | _zz_unlock(fd); \ |
|---|
| 455 | if(ch == EOF) \ |
|---|
| 456 | { \ |
|---|
| 457 | finished = 1; \ |
|---|
| 458 | ret = done; \ |
|---|
| 459 | } \ |
|---|
| 460 | else \ |
|---|
| 461 | { \ |
|---|
| 462 | unsigned char c = ch; \ |
|---|
| 463 | _zz_fuzz(fd, &c, 1); /* even more inefficient */ \ |
|---|
| 464 | line[done++] = c; \ |
|---|
| 465 | _zz_addpos(fd, 1); \ |
|---|
| 466 | if(c == delim) \ |
|---|
| 467 | { \ |
|---|
| 468 | finished = 1; \ |
|---|
| 469 | ret = done; \ |
|---|
| 470 | } \ |
|---|
| 471 | } \ |
|---|
| 472 | } \ |
|---|
| 473 | if(need_delim) \ |
|---|
| 474 | debug("%s(%p, %p, '%c', [%i]) = %li", __func__, \ |
|---|
| 475 | lineptr, n, delim, fd, (long int)ret); \ |
|---|
| 476 | else \ |
|---|
| 477 | debug("%s(%p, %p, [%i]) = %li", __func__, \ |
|---|
| 478 | lineptr, n, fd, (long int)ret); \ |
|---|
| 479 | return ret; \ |
|---|
| 480 | } while(0) |
|---|
| 481 | |
|---|
| 482 | #if defined HAVE_GETLINE |
|---|
| 483 | ssize_t NEW(getline)(char **lineptr, size_t *n, FILE *stream) |
|---|
| 484 | { |
|---|
| 485 | ssize_t ret; GETDELIM(getline, '\n', 0); return ret; |
|---|
| 486 | } |
|---|
| 487 | #endif |
|---|
| 488 | |
|---|
| 489 | #if defined HAVE_GETDELIM |
|---|
| 490 | ssize_t NEW(getdelim)(char **lineptr, size_t *n, int delim, FILE *stream) |
|---|
| 491 | { |
|---|
| 492 | ssize_t ret; GETDELIM(getdelim, delim, 1); return ret; |
|---|
| 493 | } |
|---|
| 494 | #endif |
|---|
| 495 | |
|---|
| 496 | #if defined HAVE___GETDELIM |
|---|
| 497 | ssize_t NEW(__getdelim)(char **lineptr, size_t *n, int delim, FILE *stream) |
|---|
| 498 | { |
|---|
| 499 | ssize_t ret; GETDELIM(__getdelim, delim, 1); return ret; |
|---|
| 500 | } |
|---|
| 501 | #endif |
|---|
| 502 | |
|---|
| 503 | #if defined HAVE_FGETLN |
|---|
| 504 | char *NEW(fgetln)(FILE *stream, size_t *len) |
|---|
| 505 | { |
|---|
| 506 | char *ret; |
|---|
| 507 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 508 | #else |
|---|
| 509 | struct fuzz *fuzz; |
|---|
| 510 | size_t i, size; |
|---|
| 511 | #endif |
|---|
| 512 | int fd; |
|---|
| 513 | |
|---|
| 514 | LOADSYM(fgetln); |
|---|
| 515 | LOADSYM(fgetc); |
|---|
| 516 | fd = fileno(stream); |
|---|
| 517 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 518 | return ORIG(fgetln)(stream, len); |
|---|
| 519 | |
|---|
| 520 | #if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */ |
|---|
| 521 | _zz_lock(fd); |
|---|
| 522 | ret = ORIG(fgetln)(stream, len); |
|---|
| 523 | _zz_unlock(fd); |
|---|
| 524 | #else |
|---|
| 525 | fuzz = _zz_getfuzz(fd); |
|---|
| 526 | |
|---|
| 527 | for(i = size = 0; ; /* i is incremented below */) |
|---|
| 528 | { |
|---|
| 529 | int ch; |
|---|
| 530 | |
|---|
| 531 | _zz_lock(fd); |
|---|
| 532 | ch = ORIG(fgetc)(stream); |
|---|
| 533 | _zz_unlock(fd); |
|---|
| 534 | |
|---|
| 535 | if(ch == EOF) |
|---|
| 536 | break; |
|---|
| 537 | |
|---|
| 538 | if(i >= size) |
|---|
| 539 | fuzz->tmp = realloc(fuzz->tmp, (size += 80)); |
|---|
| 540 | |
|---|
| 541 | fuzz->tmp[i] = (char)(unsigned char)ch; |
|---|
| 542 | _zz_fuzz(fd, (uint8_t *)fuzz->tmp + i, 1); /* rather inefficient */ |
|---|
| 543 | _zz_addpos(fd, 1); |
|---|
| 544 | |
|---|
| 545 | if(fuzz->tmp[i++] == '\n') |
|---|
| 546 | break; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | *len = i; |
|---|
| 550 | ret = fuzz->tmp; |
|---|
| 551 | #endif |
|---|
| 552 | |
|---|
| 553 | debug("%s([%i], &%li) = %p", __func__, fd, (long int)*len, ret); |
|---|
| 554 | return ret; |
|---|
| 555 | } |
|---|
| 556 | #endif |
|---|
| 557 | |
|---|
| 558 | #if defined HAVE___SREFILL |
|---|
| 559 | int NEW(__srefill)(FILE *fp) |
|---|
| 560 | { |
|---|
| 561 | off_t newpos; |
|---|
| 562 | int ret, fd, tmp; |
|---|
| 563 | |
|---|
| 564 | LOADSYM(__srefill); |
|---|
| 565 | fd = fileno(fp); |
|---|
| 566 | if(!_zz_ready || !_zz_iswatched(fd)) |
|---|
| 567 | return ORIG(__srefill)(fp); |
|---|
| 568 | |
|---|
| 569 | _zz_lock(fd); |
|---|
| 570 | ret = ORIG(__srefill)(fp); |
|---|
| 571 | newpos = lseek(fd, 0, SEEK_CUR); |
|---|
| 572 | _zz_unlock(fd); |
|---|
| 573 | if(ret != EOF) |
|---|
| 574 | { |
|---|
| 575 | if(newpos != -1) |
|---|
| 576 | _zz_setpos(fd, newpos - fp->_r); |
|---|
| 577 | _zz_fuzz(fd, fp->_p, fp->_r); |
|---|
| 578 | _zz_addpos(fd, fp->_r); |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | if(!_zz_islocked(fd)) |
|---|
| 582 | debug("%s([%i]) = %i", __func__, fd, ret); |
|---|
| 583 | |
|---|
| 584 | return ret; |
|---|
| 585 | } |
|---|
| 586 | #endif |
|---|
| 587 | |
|---|