2 /* A test program written to test robustness to decompression of
3 corrupted data. Usage is
5 and the program will read the specified file, compress it (in memory),
6 and then repeatedly decompress it, each time with a different bit of
7 the compressed data inverted, so as to test all possible one-bit errors.
8 This should not cause any invalid memory accesses. If it does,
9 I want to know about it!
11 PS. As you can see from the above description, the process is
12 incredibly slow. A file of size eg 5KB will cause it to run for
16 /* ------------------------------------------------------------------
17 This file is part of bzip2/libbzip2, a program and library for
18 lossless, block-sorting data compression.
20 bzip2/libbzip2 version 1.0.8 of 13 July 2019
21 Copyright (C) 1996-2019 Julian Seward <jseward@acm.org>
23 Please read the WARNING, DISCLAIMER and PATENTS sections in the
26 This program is released under the terms of the license contained
28 ------------------------------------------------------------------ */
35 #define M_BLOCK 1000000
37 typedef unsigned char uchar;
39 #define M_BLOCK_OUT (M_BLOCK + 1000000)
41 uchar outbuf[M_BLOCK_OUT];
42 uchar zbuf[M_BLOCK + 600 + (M_BLOCK / 100)];
46 static char *bzerrorstrings[] = {
56 ,"???" /* for future */
57 ,"???" /* for future */
58 ,"???" /* for future */
59 ,"???" /* for future */
60 ,"???" /* for future */
61 ,"???" /* for future */
64 void flip_bit ( int bit )
68 uchar mask = 1 << bitno;
69 //fprintf ( stderr, "(byte %d bit %d mask %d)",
70 // byteno, bitno, (int)mask );
74 int main ( int argc, char** argv )
82 fprintf ( stderr, "usage: unzcrash filename\n" );
86 f = fopen ( argv[1], "r" );
88 fprintf ( stderr, "unzcrash: can't open %s\n", argv[1] );
92 nIn = fread ( inbuf, 1, M_BLOCK, f );
93 fprintf ( stderr, "%d bytes read\n", nIn );
96 r = BZ2_bzBuffToBuffCompress (
97 zbuf, &nZ, inbuf, nIn, 9, 0, 30 );
100 fprintf ( stderr, "%d after compression\n", nZ );
102 for (bit = 0; bit < nZ*8; bit++) {
103 fprintf ( stderr, "bit %d ", bit );
106 r = BZ2_bzBuffToBuffDecompress (
107 outbuf, &nOut, zbuf, nZ, 0, 0 );
108 fprintf ( stderr, " %d %s ", r, bzerrorstrings[-r] );
111 fprintf ( stderr, "\n" );
114 fprintf(stderr, "nIn/nOut mismatch %d %d\n", nIn, nOut );
117 for (i = 0; i < nOut; i++)
118 if (inbuf[i] != outbuf[i]) {
119 fprintf(stderr, "mismatch at %d\n", i );
122 if (i == nOut) fprintf(stderr, "really ok!\n" );
130 assert (nOut == nIn);
131 for (i = 0; i < nOut; i++) {
132 if (inbuf[i] != outbuf[i]) {
133 fprintf ( stderr, "difference at %d !\n", i );
139 fprintf ( stderr, "all ok\n" );