Chris@4
|
1
|
Chris@4
|
2 /* A test program written to test robustness to decompression of
|
Chris@4
|
3 corrupted data. Usage is
|
Chris@4
|
4 unzcrash filename
|
Chris@4
|
5 and the program will read the specified file, compress it (in memory),
|
Chris@4
|
6 and then repeatedly decompress it, each time with a different bit of
|
Chris@4
|
7 the compressed data inverted, so as to test all possible one-bit errors.
|
Chris@4
|
8 This should not cause any invalid memory accesses. If it does,
|
Chris@4
|
9 I want to know about it!
|
Chris@4
|
10
|
Chris@4
|
11 PS. As you can see from the above description, the process is
|
Chris@4
|
12 incredibly slow. A file of size eg 5KB will cause it to run for
|
Chris@4
|
13 many hours.
|
Chris@4
|
14 */
|
Chris@4
|
15
|
Chris@4
|
16 /* ------------------------------------------------------------------
|
Chris@4
|
17 This file is part of bzip2/libbzip2, a program and library for
|
Chris@4
|
18 lossless, block-sorting data compression.
|
Chris@4
|
19
|
Chris@4
|
20 bzip2/libbzip2 version 1.0.6 of 6 September 2010
|
Chris@4
|
21 Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
|
Chris@4
|
22
|
Chris@4
|
23 Please read the WARNING, DISCLAIMER and PATENTS sections in the
|
Chris@4
|
24 README file.
|
Chris@4
|
25
|
Chris@4
|
26 This program is released under the terms of the license contained
|
Chris@4
|
27 in the file LICENSE.
|
Chris@4
|
28 ------------------------------------------------------------------ */
|
Chris@4
|
29
|
Chris@4
|
30
|
Chris@4
|
31 #include <stdio.h>
|
Chris@4
|
32 #include <assert.h>
|
Chris@4
|
33 #include "bzlib.h"
|
Chris@4
|
34
|
Chris@4
|
35 #define M_BLOCK 1000000
|
Chris@4
|
36
|
Chris@4
|
37 typedef unsigned char uchar;
|
Chris@4
|
38
|
Chris@4
|
39 #define M_BLOCK_OUT (M_BLOCK + 1000000)
|
Chris@4
|
40 uchar inbuf[M_BLOCK];
|
Chris@4
|
41 uchar outbuf[M_BLOCK_OUT];
|
Chris@4
|
42 uchar zbuf[M_BLOCK + 600 + (M_BLOCK / 100)];
|
Chris@4
|
43
|
Chris@4
|
44 int nIn, nOut, nZ;
|
Chris@4
|
45
|
Chris@4
|
46 static char *bzerrorstrings[] = {
|
Chris@4
|
47 "OK"
|
Chris@4
|
48 ,"SEQUENCE_ERROR"
|
Chris@4
|
49 ,"PARAM_ERROR"
|
Chris@4
|
50 ,"MEM_ERROR"
|
Chris@4
|
51 ,"DATA_ERROR"
|
Chris@4
|
52 ,"DATA_ERROR_MAGIC"
|
Chris@4
|
53 ,"IO_ERROR"
|
Chris@4
|
54 ,"UNEXPECTED_EOF"
|
Chris@4
|
55 ,"OUTBUFF_FULL"
|
Chris@4
|
56 ,"???" /* for future */
|
Chris@4
|
57 ,"???" /* for future */
|
Chris@4
|
58 ,"???" /* for future */
|
Chris@4
|
59 ,"???" /* for future */
|
Chris@4
|
60 ,"???" /* for future */
|
Chris@4
|
61 ,"???" /* for future */
|
Chris@4
|
62 };
|
Chris@4
|
63
|
Chris@4
|
64 void flip_bit ( int bit )
|
Chris@4
|
65 {
|
Chris@4
|
66 int byteno = bit / 8;
|
Chris@4
|
67 int bitno = bit % 8;
|
Chris@4
|
68 uchar mask = 1 << bitno;
|
Chris@4
|
69 //fprintf ( stderr, "(byte %d bit %d mask %d)",
|
Chris@4
|
70 // byteno, bitno, (int)mask );
|
Chris@4
|
71 zbuf[byteno] ^= mask;
|
Chris@4
|
72 }
|
Chris@4
|
73
|
Chris@4
|
74 int main ( int argc, char** argv )
|
Chris@4
|
75 {
|
Chris@4
|
76 FILE* f;
|
Chris@4
|
77 int r;
|
Chris@4
|
78 int bit;
|
Chris@4
|
79 int i;
|
Chris@4
|
80
|
Chris@4
|
81 if (argc != 2) {
|
Chris@4
|
82 fprintf ( stderr, "usage: unzcrash filename\n" );
|
Chris@4
|
83 return 1;
|
Chris@4
|
84 }
|
Chris@4
|
85
|
Chris@4
|
86 f = fopen ( argv[1], "r" );
|
Chris@4
|
87 if (!f) {
|
Chris@4
|
88 fprintf ( stderr, "unzcrash: can't open %s\n", argv[1] );
|
Chris@4
|
89 return 1;
|
Chris@4
|
90 }
|
Chris@4
|
91
|
Chris@4
|
92 nIn = fread ( inbuf, 1, M_BLOCK, f );
|
Chris@4
|
93 fprintf ( stderr, "%d bytes read\n", nIn );
|
Chris@4
|
94
|
Chris@4
|
95 nZ = M_BLOCK;
|
Chris@4
|
96 r = BZ2_bzBuffToBuffCompress (
|
Chris@4
|
97 zbuf, &nZ, inbuf, nIn, 9, 0, 30 );
|
Chris@4
|
98
|
Chris@4
|
99 assert (r == BZ_OK);
|
Chris@4
|
100 fprintf ( stderr, "%d after compression\n", nZ );
|
Chris@4
|
101
|
Chris@4
|
102 for (bit = 0; bit < nZ*8; bit++) {
|
Chris@4
|
103 fprintf ( stderr, "bit %d ", bit );
|
Chris@4
|
104 flip_bit ( bit );
|
Chris@4
|
105 nOut = M_BLOCK_OUT;
|
Chris@4
|
106 r = BZ2_bzBuffToBuffDecompress (
|
Chris@4
|
107 outbuf, &nOut, zbuf, nZ, 0, 0 );
|
Chris@4
|
108 fprintf ( stderr, " %d %s ", r, bzerrorstrings[-r] );
|
Chris@4
|
109
|
Chris@4
|
110 if (r != BZ_OK) {
|
Chris@4
|
111 fprintf ( stderr, "\n" );
|
Chris@4
|
112 } else {
|
Chris@4
|
113 if (nOut != nIn) {
|
Chris@4
|
114 fprintf(stderr, "nIn/nOut mismatch %d %d\n", nIn, nOut );
|
Chris@4
|
115 return 1;
|
Chris@4
|
116 } else {
|
Chris@4
|
117 for (i = 0; i < nOut; i++)
|
Chris@4
|
118 if (inbuf[i] != outbuf[i]) {
|
Chris@4
|
119 fprintf(stderr, "mismatch at %d\n", i );
|
Chris@4
|
120 return 1;
|
Chris@4
|
121 }
|
Chris@4
|
122 if (i == nOut) fprintf(stderr, "really ok!\n" );
|
Chris@4
|
123 }
|
Chris@4
|
124 }
|
Chris@4
|
125
|
Chris@4
|
126 flip_bit ( bit );
|
Chris@4
|
127 }
|
Chris@4
|
128
|
Chris@4
|
129 #if 0
|
Chris@4
|
130 assert (nOut == nIn);
|
Chris@4
|
131 for (i = 0; i < nOut; i++) {
|
Chris@4
|
132 if (inbuf[i] != outbuf[i]) {
|
Chris@4
|
133 fprintf ( stderr, "difference at %d !\n", i );
|
Chris@4
|
134 return 1;
|
Chris@4
|
135 }
|
Chris@4
|
136 }
|
Chris@4
|
137 #endif
|
Chris@4
|
138
|
Chris@4
|
139 fprintf ( stderr, "all ok\n" );
|
Chris@4
|
140 return 0;
|
Chris@4
|
141 }
|