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 p.s. 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
20 #define M_BLOCK 1000000
22 typedef unsigned char uchar;
24 #define M_BLOCK_OUT (M_BLOCK + 1000000)
26 uchar outbuf[M_BLOCK_OUT];
27 uchar zbuf[M_BLOCK + 600 + (M_BLOCK / 100)];
31 static char *bzerrorstrings[] = {
41 ,"???" /* for future */
42 ,"???" /* for future */
43 ,"???" /* for future */
44 ,"???" /* for future */
45 ,"???" /* for future */
46 ,"???" /* for future */
49 void flip_bit ( int bit )
53 uchar mask = 1 << bitno;
54 //fprintf ( stderr, "(byte %d bit %d mask %d)",
55 // byteno, bitno, (int)mask );
59 int main ( int argc, char** argv )
67 fprintf ( stderr, "usage: unzcrash filename\n" );
71 f = fopen ( argv[1], "r" );
73 fprintf ( stderr, "unzcrash: can't open %s\n", argv[1] );
77 nIn = fread ( inbuf, 1, M_BLOCK, f );
78 fprintf ( stderr, "%d bytes read\n", nIn );
81 r = BZ2_bzBuffToBuffCompress (
82 zbuf, &nZ, inbuf, nIn, 9, 0, 30 );
85 fprintf ( stderr, "%d after compression\n", nZ );
87 for (bit = 0; bit < nZ*8; bit++) {
88 fprintf ( stderr, "bit %d ", bit );
91 r = BZ2_bzBuffToBuffDecompress (
92 outbuf, &nOut, zbuf, nZ, 0, 0 );
93 fprintf ( stderr, " %d %s ", r, bzerrorstrings[-r] );
96 fprintf ( stderr, "\n" );
99 fprintf(stderr, "nIn/nOut mismatch %d %d\n", nIn, nOut );
102 for (i = 0; i < nOut; i++)
103 if (inbuf[i] != outbuf[i]) {
104 fprintf(stderr, "mismatch at %d\n", i );
107 if (i == nOut) fprintf(stderr, "really ok!\n" );
115 assert (nOut == nIn);
116 for (i = 0; i < nOut; i++) {
117 if (inbuf[i] != outbuf[i]) {
118 fprintf ( stderr, "difference at %d !\n", i );
124 fprintf ( stderr, "all ok\n" );