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