source: zzuf/trunk/src/libzzuf.c @ 1520

Last change on this file since 1520 was 1520, checked in by Sam Hocevar, 14 years ago
  • Shorten function names.
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: libzzuf.c 1520 2006-12-28 15:51:43Z 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 <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <fcntl.h>
31#include <regex.h>
32
33#include <stdarg.h>
34#include <dlfcn.h>
35
36#include "libzzuf.h"
37#include "debug.h"
38#include "load.h"
39
40/* Global variables */
41int   _zzuf_ready   = 0;
42int   _zzuf_debug   = 0;
43int   _zzuf_seed    = 0;
44float _zzuf_ratio   = 0.004f;
45regex_t * _zzuf_include = NULL;
46regex_t * _zzuf_exclude = NULL;
47
48#define MAXFD 1024
49struct zzuf
50{
51    int managed;
52    uint64_t seed;
53    uint64_t pos;
54    /* Public stuff */
55    struct fuzz fuzz;
56}
57files[MAXFD];
58
59/* Library initialisation shit */
60void zzuf_init(void)
61{
62    char *tmp;
63    int i;
64
65    tmp = getenv("ZZUF_DEBUG");
66    if(tmp && *tmp)
67        _zzuf_debug = 1;
68
69    tmp = getenv("ZZUF_SEED");
70    if(tmp && *tmp)
71        _zzuf_seed = atol(tmp);
72
73    tmp = getenv("ZZUF_RATIO");
74    if(tmp && *tmp)
75        _zzuf_ratio = atof(tmp);
76    if(_zzuf_ratio < 0.0f)
77        _zzuf_ratio = 0.0f;
78    else if(_zzuf_ratio > 5.0f)
79        _zzuf_ratio = 5.0f;
80
81    tmp = getenv("ZZUF_INCLUDE");
82    if(tmp && *tmp)
83    {
84        _zzuf_include = malloc(sizeof(*_zzuf_include));
85        regcomp(_zzuf_include, tmp, 0);
86    }
87
88    tmp = getenv("ZZUF_EXCLUDE");
89    if(tmp && *tmp)
90    {
91        _zzuf_exclude = malloc(sizeof(*_zzuf_exclude));
92        regcomp(_zzuf_exclude, tmp, 0);
93    }
94
95    for(i = 0; i < MAXFD; i++)
96        files[i].managed = 0;
97
98    zzuf_load_fd();
99    zzuf_load_stream();
100
101    _zzuf_ready = 1;
102
103    debug("libzzuf initialised");
104}
105
106/* Deinitialisation */
107void zzuf_fini(void)
108{
109    int i;
110
111    for(i = 0; i < MAXFD; i++)
112    {
113        if(!files[i].managed)
114            continue;
115
116        /* XXX: What are we supposed to do? If filedescriptors weren't
117         * closed properly, there's a leak, but it's not our problem. */
118    }
119}
120
121/* fd stuff */
122int zfd_ismanaged(int fd)
123{
124    return files[fd].managed;
125}
126
127void zfd_manage(int fd)
128{
129    files[fd].managed = 1;
130    files[fd].pos = 0;
131    files[fd].fuzz.cur = -1;
132    files[fd].fuzz.data = malloc(CHUNKBYTES);
133}
134
135void zfd_unmanage(int fd)
136{
137    files[fd].managed = 0;
138    free(files[fd].fuzz.data);
139}
140
141long int zfd_getpos(int fd)
142{
143    return files[fd].pos;
144}
145
146void zfd_setpos(int fd, long int pos)
147{
148    files[fd].pos = pos;
149}
150
151void zfd_addpos(int fd, long int off)
152{
153    files[fd].pos += off;
154}
155
156struct fuzz *zfd_getfuzz(int fd)
157{
158    return &files[fd].fuzz;
159}
160
Note: See TracBrowser for help on using the repository browser.