source: zzuf/trunk/src/fd.c @ 1621

Last change on this file since 1621 was 1621, checked in by Sam Hocevar, 14 years ago
  • Factor regex stuff into fd.c, so that <regex.h> isn’t needed otherwhere.
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: fd.c 1621 2007-01-07 21:14:26Z sam $
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 *  libzzuf.c: preloaded wrapper library
17 */
18
19#include "config.h"
20#define _GNU_SOURCE
21
22#if defined HAVE_STDINT_H
23#   include <stdint.h>
24#elif defined HAVE_INTTYPES_H
25#   include <inttypes.h>
26#endif
27#include <stdlib.h>
28#include <regex.h>
29
30#include "libzzuf.h"
31#include "fd.h"
32
33/* Regex stuff */
34static regex_t * re_include = NULL;
35static regex_t * re_exclude = NULL;
36
37/* File descriptor stuff */
38static struct files
39{
40    int managed;
41    uint64_t seed;
42    uint64_t pos;
43    /* Public stuff */
44    struct fuzz fuzz;
45}
46*files;
47static int *fds;
48static int maxfd, nfiles;
49
50void _zz_include(char const *regex)
51{
52    re_include = malloc(sizeof(*re_include));
53
54    if(regcomp(re_include, regex, REG_EXTENDED) != 0)
55    {
56        free(re_include);
57        re_include = NULL;
58    }
59}
60
61void _zz_exclude(char const *regex)
62{
63    re_exclude = malloc(sizeof(*re_exclude));
64
65    if(regcomp(re_exclude, regex, REG_EXTENDED) != 0)
66    {
67        free(re_exclude);
68        re_exclude = NULL;
69    }
70}
71
72void _zz_fd_init(void)
73{
74    files = NULL;
75    nfiles = 0;
76
77    /* Start with one fd in the lookup table */
78    fds = malloc(1 * sizeof(int));
79    for(maxfd = 0; maxfd < 1; maxfd++)
80        fds[maxfd] = -1;
81}
82
83void _zz_fd_fini(void)
84{
85    int i;
86
87    for(i = 0; i < maxfd; i++)
88    {
89        if(!files[fds[i]].managed)
90            continue;
91
92        /* XXX: What are we supposed to do? If filedescriptors weren't
93         * closed properly, there's a leak, but it's not our problem. */
94    }
95
96    free(files);
97    free(fds);
98}
99
100int _zz_mustwatch(char const *file)
101{
102    if(re_include && regexec(re_include, file, 0, NULL, 0) == REG_NOMATCH)
103        return 0; /* not included: ignore */
104
105    if(re_exclude && regexec(re_exclude, file, 0, NULL, 0) != REG_NOMATCH)
106        return 0; /* excluded: ignore */
107
108    return 1; /* default */
109}
110
111int _zz_iswatched(int fd)
112{
113    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
114        return 0;
115
116    return 1;
117}
118
119void _zz_register(int fd)
120{
121    int i;
122
123    if(fd < 0 || fd > 65535 || (fd < maxfd && fds[fd] != -1))
124        return;
125
126    while(fd >= maxfd)
127    {
128        fds = realloc(fds, 2 * maxfd * sizeof(int));
129        for(i = maxfd; i < maxfd * 2; i++)
130            fds[i] = -1;
131        maxfd *= 2;
132    }
133           
134    /* Find an empty slot */
135    for(i = 0; i < nfiles; i++)
136        if(files[i].managed == 0)
137            break;
138
139    /* No slot found, allocate memory */
140    if(i == nfiles)
141    {
142        nfiles++;
143        files = realloc(files, nfiles * sizeof(struct files));
144    }
145
146    files[i].managed = 1;
147    files[i].pos = 0;
148    files[i].fuzz.cur = -1;
149    files[i].fuzz.data = malloc(CHUNKBYTES);
150#ifdef HAVE_FGETLN
151    files[i].fuzz.tmp = NULL;
152#endif
153
154    fds[fd] = i;
155}
156
157void _zz_unregister(int fd)
158{
159    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
160        return;
161
162    files[fds[fd]].managed = 0;
163    free(files[fds[fd]].fuzz.data);
164#ifdef HAVE_FGETLN
165    if(files[fds[fd]].fuzz.tmp)
166        free(files[fds[fd]].fuzz.tmp);
167#endif
168
169    fds[fd] = -1;
170}
171
172long int _zz_getpos(int fd)
173{
174    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
175        return 0;
176
177    return files[fds[fd]].pos;
178}
179
180void _zz_setpos(int fd, long int pos)
181{
182    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
183        return;
184
185    files[fds[fd]].pos = pos;
186}
187
188void _zz_addpos(int fd, long int off)
189{
190    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
191        return;
192
193    files[fds[fd]].pos += off;
194}
195
196struct fuzz *_zz_getfuzz(int fd)
197{
198    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
199        return NULL;
200
201    return &files[fds[fd]].fuzz;
202}
203
Note: See TracBrowser for help on using the repository browser.