File: corrupttest.c

package info (click to toggle)
leptonlib 1.84.1-4
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 29,476 kB
  • sloc: ansic: 196,041; sh: 4,543; cpp: 2,026; makefile: 284
file content (270 lines) | stat: -rw-r--r-- 11,491 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*====================================================================*
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
 -
 -  Redistribution and use in source and binary forms, with or without
 -  modification, are permitted provided that the following conditions
 -  are met:
 -  1. Redistributions of source code must retain the above copyright
 -     notice, this list of conditions and the following disclaimer.
 -  2. Redistributions in binary form must reproduce the above
 -     copyright notice, this list of conditions and the following
 -     disclaimer in the documentation and/or other materials
 -     provided with the distribution.
 -
 -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
 -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *====================================================================*/

/*
 * corrupttest.c
 *
 *     Excises or permutes a given fraction of bytes, starting from
 *     a specified location.  The parameters @loc and @size are fractions
 *     of the entire file (between 0.0 and 1.0).
 *
 *     Syntax:  corrupttest <file> <deletion> [loc size]
 *
 *        where <deletion> == 1 means that bytes are deleted
 *              <deletion> == 0 means that random bytes are substituted
 *
 *     Use: "fuzz testing" jpeg, png, tiff, bmp, webp and pnm reading,
 *          under corruption by either random byte substitution or
 *          deletion of part of the compressed file.
 *
 *     For example,
 *          corrupttest rabi.png 0 0.0001 0.0001
 *     which tests read functions on rabi.png after 23 bytes (0.01%)
 *     starting at byte 23 have been randomly permuted, emits the following:
 *      > Info in fileCorruptByMutation: Randomizing 23 bytes at location 23
 *      > libpng error: IHDR: CRC error
 *      > Error in pixReadMemPng: internal png error
 *      > Error in pixReadStream: png: no pix returned
 *      > Error in pixRead: pix not read
 *      > libpng error: IHDR: CRC error
 *      > Error in fgetPngResolution: internal png error
 */

#ifdef HAVE_CONFIG_H
#include <config_auto.h>
#endif  /* HAVE_CONFIG_H */

#include "string.h"
#include "allheaders.h"

static const char *corruptfile = "/tmp/lept/corrupt/badfile";

int main(int     argc,
         char  **argv)
{
size_t     filesize;
l_float32  loc, size;
l_float32  coeff1[15], coeff2[25];
l_int32    i, j, w, xres, yres, format, ret, nwarn, hint, deletion, show;
l_uint8   *comment, *filedata;
char      *filein;
size_t     nbytes;
FILE      *fp;
PIX       *pix;

    if (argc != 3 && argc != 5)
        return ERROR_INT("syntax: corrupttest filein deletion [loc size]",
        __func__, 1);
    filein = argv[1];
    deletion = atoi(argv[2]);
    findFileFormat(filein, &format);
    nbytes = nbytesInFile(filein);
    lept_stderr("file size: %lu bytes\n", (unsigned long)nbytes);

    setLeptDebugOK(1);
    lept_mkdir("lept/corrupt");

    hint = 0;
    if (argc == 5) {  /* Single test */
        loc = atof(argv[3]);
        size = atof(argv[4]);
        if (deletion == TRUE) {
            fileCorruptByDeletion(filein, loc, size, corruptfile);
        } else {  /* mutation */
            fileCorruptByMutation(filein, loc, size, corruptfile);
        }
        if ((fp = fopenReadStream(corruptfile)) == NULL)
            return ERROR_INT_1("stream not opened", corruptfile, __func__, 1);
        if (format == IFF_JFIF_JPEG) {
            if ((pix = pixReadJpeg(corruptfile, 0, 1, &nwarn, hint)) != NULL) {
                pixDisplay(pix, 100, 100);
                pixDestroy(&pix);
            }
            freadHeaderJpeg(fp, &w, NULL, NULL, NULL, NULL);
            fgetJpegResolution(fp, &xres, &yres);
            ret = fgetJpegComment(fp, &comment);
            if (!ret && comment) {
                lept_stderr("comment: %s\n", comment);
                lept_free(comment);
            }
        } else if (format == IFF_PNG) {
            if ((pix = pixRead(corruptfile)) != NULL) {
                pixDisplay(pix, 100, 100);
                pixDestroy(&pix);
            }
            freadHeaderPng(fp, &w, NULL, NULL, NULL, NULL);
            fgetPngResolution(fp, &xres, &yres);
        } else if (format == IFF_WEBP) {
            if ((pix = pixRead(corruptfile)) != NULL) {
                pixDisplay(pix, 100, 100);
                pixDestroy(&pix);
            }
            readHeaderWebP(corruptfile, &w, NULL, NULL);
        } else if (format == IFF_PNM) {
            if ((pix = pixRead(corruptfile)) != NULL) {
                pixDisplay(pix, 100, 100);
                pixDestroy(&pix);
            }
            freadHeaderPnm(fp, &w, NULL, NULL, NULL, NULL, NULL);
        }
        fclose(fp);
        return 0;
    }

        /* Generate coefficients so that the size of the mangled
         * or deleted data can range from 0.001% to 1% of the file,
         * and the location of deleted data ranges from 0.001%
         * to 90% of the file. */
    for (i = 0; i < 15; i++) {
        if (i < 5) coeff1[i] = 0.00001;
        else if (i < 10) coeff1[i] = 0.0001;
        else coeff1[i] = 0.001;
    }
    for (i = 0; i < 25; i++) {
        if (i < 5) coeff2[i] = 0.00001;
        else if (i < 10) coeff2[i] = 0.0001;
        else if (i < 15) coeff2[i] = 0.001;
        else if (i < 20) coeff2[i] = 0.01;
        else coeff2[i] = 0.1;
    }

        /* Multiple test (argc == 3) */
    show = TRUE;
    for (i = 0; i < 25; i++) {
        loc = coeff2[i] * (2 * (i % 5) + 1);
        for (j = 0; j < 15; j++) {
            size = coeff1[j] * (2 * (j % 5) + 1);

                /* Write corrupt file */
            if (deletion == TRUE) {
                fileCorruptByDeletion(filein, loc, size, corruptfile);
            } else {
                fileCorruptByMutation(filein, loc, size, corruptfile);
            }

                /* Attempt to read the file */
            pix = NULL;
            if (format == IFF_JFIF_JPEG) {
                /* The pix is usually returned as long as the header
                 * information is not damaged.
                 * We expect nwarn > 0 (typically 1) for nearly every
                 * corrupted image.  In the situation where only a few
                 * bytes are removed, a corrupted image will occasionally
                 * have nwarn == 0 even though it's visually defective.  */
                pix = pixReadJpeg(corruptfile, 0, 1, &nwarn, 0);
                if (pix && nwarn != 1 && deletion == TRUE) {
                    lept_stderr("nwarn[%d,%d] = %d\n", j, i, nwarn);
                    if (show) pixDisplay(pix, 20 * i, 30 * j);
                    show = FALSE;
                }
            } else if (format == IFF_PNG) {
                pix = pixRead(corruptfile);
                if (pix) {
                    lept_stderr("pix[%d,%d] is read\n", j, i);
                    if (show) pixDisplay(pix, 20 * i, 30 * j);
                    show = FALSE;
                }
                pixDestroy(&pix);
                filedata = l_binaryRead(corruptfile, &filesize);
                pix = pixReadMemPng(filedata, filesize);
                lept_free(filedata);
            } else if (L_FORMAT_IS_TIFF(format)) {
                /* A corrupted pix is often returned, as long as the
                 * header is not damaged, so we do not display them.  */
                pix = pixRead(corruptfile);
                if (pix) lept_stderr("pix[%d,%d] is read\n", j, i);
                pixDestroy(&pix);
                filedata = l_binaryRead(corruptfile, &filesize);
                pix = pixReadMemTiff(filedata, filesize, 0);
                if (!pix) lept_stderr("no pix!\n");
                lept_free(filedata);
            } else if (format == IFF_BMP) {
                /* A corrupted pix is always returned if the header is
                 * not damaged, so we do not display them.  */
                pix = pixRead(corruptfile);
                if (pix) lept_stderr("pix[%d,%d] is read\n", j, i);
                pixDestroy(&pix);
                filedata = l_binaryRead(corruptfile, &filesize);
                pix = pixReadMemBmp(filedata, filesize);
                lept_free(filedata);
            } else if (format == IFF_WEBP) {
                /* A corrupted pix is always returned if the header is
                 * not damaged, so we do not display them.  */
                pix = pixRead(corruptfile);
                if (pix) lept_stderr("pix[%d,%d] is read\n", j, i);
                pixDestroy(&pix);
                filedata = l_binaryRead(corruptfile, &filesize);
                pix = pixReadMemWebP(filedata, filesize);
                lept_free(filedata);
            } else if (format == IFF_PNM) {
                /* A corrupted pix is always returned if the header is
                 * not damaged, so we do not display them.  */
                pix = pixRead(corruptfile);
                if (pix) lept_stderr("pix[%d,%d] is read\n", j, i);
                pixDestroy(&pix);
                filedata = l_binaryRead(corruptfile, &filesize);
                pix = pixReadMemPnm(filedata, filesize);
                lept_free(filedata);
            } else {
                lept_stderr("Format %d unknown\n", format);
                continue;
            }

                /* Effect of 1% byte mangling from interior of data stream */
            if (pix && j == 14 && i == 10 && deletion == FALSE)
                pixDisplay(pix, 0, 0);
            pixDestroy(&pix);

                /* Attempt to read the header and the resolution */
            fp = fopenReadStream(corruptfile);
            if (format == IFF_JFIF_JPEG) {
                freadHeaderJpeg(fp, &w, NULL, NULL, NULL, NULL);
                if (fgetJpegResolution(fp, &xres, &yres) == 0)
                    lept_stderr("w = %d, xres = %d, yres = %d\n",
                    w, xres, yres);
            } else if (format == IFF_PNG)  {
                freadHeaderPng(fp, &w, NULL, NULL, NULL, NULL);
                if (fgetPngResolution(fp, &xres, &yres) == 0)
                    lept_stderr("w = %d, xres = %d, yres = %d\n",
                    w, xres, yres);
            } else if (L_FORMAT_IS_TIFF(format)) {
                freadHeaderTiff(fp, 0, &w, NULL, NULL, NULL, NULL, NULL, NULL);
                getTiffResolution(fp, &xres, &yres);
                lept_stderr("w = %d, xres = %d, yres = %d\n",
                        w, xres, yres);
            } else if (format == IFF_WEBP)  {
                if (readHeaderWebP(corruptfile, &w, NULL, NULL) == 0)
                    lept_stderr("w = %d\n", w);
            } else if (format == IFF_PNM)  {
                if (freadHeaderPnm(fp, &w, NULL, NULL, NULL, NULL, NULL) == 0)
                    lept_stderr("w = %d\n", w);
            }
            fclose(fp);
        }
    }
    return 0;
}