Chris@4: Chris@4: /* A test program written to test robustness to decompression of Chris@4: corrupted data. Usage is Chris@4: unzcrash filename Chris@4: and the program will read the specified file, compress it (in memory), Chris@4: and then repeatedly decompress it, each time with a different bit of Chris@4: the compressed data inverted, so as to test all possible one-bit errors. Chris@4: This should not cause any invalid memory accesses. If it does, Chris@4: I want to know about it! Chris@4: Chris@4: PS. As you can see from the above description, the process is Chris@4: incredibly slow. A file of size eg 5KB will cause it to run for Chris@4: many hours. Chris@4: */ Chris@4: Chris@4: /* ------------------------------------------------------------------ Chris@4: This file is part of bzip2/libbzip2, a program and library for Chris@4: lossless, block-sorting data compression. Chris@4: Chris@4: bzip2/libbzip2 version 1.0.6 of 6 September 2010 Chris@4: Copyright (C) 1996-2010 Julian Seward Chris@4: Chris@4: Please read the WARNING, DISCLAIMER and PATENTS sections in the Chris@4: README file. Chris@4: Chris@4: This program is released under the terms of the license contained Chris@4: in the file LICENSE. Chris@4: ------------------------------------------------------------------ */ Chris@4: Chris@4: Chris@4: #include Chris@4: #include Chris@4: #include "bzlib.h" Chris@4: Chris@4: #define M_BLOCK 1000000 Chris@4: Chris@4: typedef unsigned char uchar; Chris@4: Chris@4: #define M_BLOCK_OUT (M_BLOCK + 1000000) Chris@4: uchar inbuf[M_BLOCK]; Chris@4: uchar outbuf[M_BLOCK_OUT]; Chris@4: uchar zbuf[M_BLOCK + 600 + (M_BLOCK / 100)]; Chris@4: Chris@4: int nIn, nOut, nZ; Chris@4: Chris@4: static char *bzerrorstrings[] = { Chris@4: "OK" Chris@4: ,"SEQUENCE_ERROR" Chris@4: ,"PARAM_ERROR" Chris@4: ,"MEM_ERROR" Chris@4: ,"DATA_ERROR" Chris@4: ,"DATA_ERROR_MAGIC" Chris@4: ,"IO_ERROR" Chris@4: ,"UNEXPECTED_EOF" Chris@4: ,"OUTBUFF_FULL" Chris@4: ,"???" /* for future */ Chris@4: ,"???" /* for future */ Chris@4: ,"???" /* for future */ Chris@4: ,"???" /* for future */ Chris@4: ,"???" /* for future */ Chris@4: ,"???" /* for future */ Chris@4: }; Chris@4: Chris@4: void flip_bit ( int bit ) Chris@4: { Chris@4: int byteno = bit / 8; Chris@4: int bitno = bit % 8; Chris@4: uchar mask = 1 << bitno; Chris@4: //fprintf ( stderr, "(byte %d bit %d mask %d)", Chris@4: // byteno, bitno, (int)mask ); Chris@4: zbuf[byteno] ^= mask; Chris@4: } Chris@4: Chris@4: int main ( int argc, char** argv ) Chris@4: { Chris@4: FILE* f; Chris@4: int r; Chris@4: int bit; Chris@4: int i; Chris@4: Chris@4: if (argc != 2) { Chris@4: fprintf ( stderr, "usage: unzcrash filename\n" ); Chris@4: return 1; Chris@4: } Chris@4: Chris@4: f = fopen ( argv[1], "r" ); Chris@4: if (!f) { Chris@4: fprintf ( stderr, "unzcrash: can't open %s\n", argv[1] ); Chris@4: return 1; Chris@4: } Chris@4: Chris@4: nIn = fread ( inbuf, 1, M_BLOCK, f ); Chris@4: fprintf ( stderr, "%d bytes read\n", nIn ); Chris@4: Chris@4: nZ = M_BLOCK; Chris@4: r = BZ2_bzBuffToBuffCompress ( Chris@4: zbuf, &nZ, inbuf, nIn, 9, 0, 30 ); Chris@4: Chris@4: assert (r == BZ_OK); Chris@4: fprintf ( stderr, "%d after compression\n", nZ ); Chris@4: Chris@4: for (bit = 0; bit < nZ*8; bit++) { Chris@4: fprintf ( stderr, "bit %d ", bit ); Chris@4: flip_bit ( bit ); Chris@4: nOut = M_BLOCK_OUT; Chris@4: r = BZ2_bzBuffToBuffDecompress ( Chris@4: outbuf, &nOut, zbuf, nZ, 0, 0 ); Chris@4: fprintf ( stderr, " %d %s ", r, bzerrorstrings[-r] ); Chris@4: Chris@4: if (r != BZ_OK) { Chris@4: fprintf ( stderr, "\n" ); Chris@4: } else { Chris@4: if (nOut != nIn) { Chris@4: fprintf(stderr, "nIn/nOut mismatch %d %d\n", nIn, nOut ); Chris@4: return 1; Chris@4: } else { Chris@4: for (i = 0; i < nOut; i++) Chris@4: if (inbuf[i] != outbuf[i]) { Chris@4: fprintf(stderr, "mismatch at %d\n", i ); Chris@4: return 1; Chris@4: } Chris@4: if (i == nOut) fprintf(stderr, "really ok!\n" ); Chris@4: } Chris@4: } Chris@4: Chris@4: flip_bit ( bit ); Chris@4: } Chris@4: Chris@4: #if 0 Chris@4: assert (nOut == nIn); Chris@4: for (i = 0; i < nOut; i++) { Chris@4: if (inbuf[i] != outbuf[i]) { Chris@4: fprintf ( stderr, "difference at %d !\n", i ); Chris@4: return 1; Chris@4: } Chris@4: } Chris@4: #endif Chris@4: Chris@4: fprintf ( stderr, "all ok\n" ); Chris@4: return 0; Chris@4: }