1 /* $NetBSD: unzcrash.c,v 1.1.1.2 2012/05/07 00:41:46 wiz Exp $ */
4 /* A test program written to test robustness to decompression of
5 corrupted data. Usage is
7 and the program will read the specified file, compress it (in memory),
8 and then repeatedly decompress it, each time with a different bit of
9 the compressed data inverted, so as to test all possible one-bit errors.
10 This should not cause any invalid memory accesses. If it does,
11 I want to know about it!
13 PS. As you can see from the above description, the process is
14 incredibly slow. A file of size eg 5KB will cause it to run for
18 /* ------------------------------------------------------------------
19 This file is part of bzip2/libbzip2, a program and library for
20 lossless, block-sorting data compression.
22 bzip2/libbzip2 version 1.0.6 of 6 September 2010
23 Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
25 Please read the WARNING, DISCLAIMER and PATENTS sections in the
28 This program is released under the terms of the license contained
30 ------------------------------------------------------------------ */
37 #define M_BLOCK 1000000
39 typedef unsigned char uchar
;
41 #define M_BLOCK_OUT (M_BLOCK + 1000000)
43 uchar outbuf
[M_BLOCK_OUT
];
44 uchar zbuf
[M_BLOCK
+ 600 + (M_BLOCK
/ 100)];
48 static char *bzerrorstrings
[] = {
58 ,"???" /* for future */
59 ,"???" /* for future */
60 ,"???" /* for future */
61 ,"???" /* for future */
62 ,"???" /* for future */
63 ,"???" /* for future */
66 void flip_bit ( int bit
)
70 uchar mask
= 1 << bitno
;
71 //fprintf ( stderr, "(byte %d bit %d mask %d)",
72 // byteno, bitno, (int)mask );
76 int main ( int argc
, char** argv
)
84 fprintf ( stderr
, "usage: unzcrash filename\n" );
88 f
= fopen ( argv
[1], "r" );
90 fprintf ( stderr
, "unzcrash: can't open %s\n", argv
[1] );
94 nIn
= fread ( inbuf
, 1, M_BLOCK
, f
);
95 fprintf ( stderr
, "%d bytes read\n", nIn
);
98 r
= BZ2_bzBuffToBuffCompress (
99 zbuf
, &nZ
, inbuf
, nIn
, 9, 0, 30 );
102 fprintf ( stderr
, "%d after compression\n", nZ
);
104 for (bit
= 0; bit
< nZ
*8; bit
++) {
105 fprintf ( stderr
, "bit %d ", bit
);
108 r
= BZ2_bzBuffToBuffDecompress (
109 outbuf
, &nOut
, zbuf
, nZ
, 0, 0 );
110 fprintf ( stderr
, " %d %s ", r
, bzerrorstrings
[-r
] );
113 fprintf ( stderr
, "\n" );
116 fprintf(stderr
, "nIn/nOut mismatch %d %d\n", nIn
, nOut
);
119 for (i
= 0; i
< nOut
; i
++)
120 if (inbuf
[i
] != outbuf
[i
]) {
121 fprintf(stderr
, "mismatch at %d\n", i
);
124 if (i
== nOut
) fprintf(stderr
, "really ok!\n" );
132 assert (nOut
== nIn
);
133 for (i
= 0; i
< nOut
; i
++) {
134 if (inbuf
[i
] != outbuf
[i
]) {
135 fprintf ( stderr
, "difference at %d !\n", i
);
141 fprintf ( stderr
, "all ok\n" );