VM: simplify slab allocator
[minix.git] / dist / bzip2 / unzcrash.c
bloba1b75463adcaf53447a97ba1f27fcddbec623581
2 /* A test program written to test robustness to decompression of
3 corrupted data. Usage is
4 unzcrash filename
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 PS. 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
13 many hours.
16 /* ------------------------------------------------------------------
17 This file is part of bzip2/libbzip2, a program and library for
18 lossless, block-sorting data compression.
20 bzip2/libbzip2 version 1.0.5 of 10 December 2007
21 Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
23 Please read the WARNING, DISCLAIMER and PATENTS sections in the
24 README file.
26 This program is released under the terms of the license contained
27 in the file LICENSE.
28 ------------------------------------------------------------------ */
31 #include <stdio.h>
32 #include <assert.h>
33 #include "bzlib.h"
35 #define M_BLOCK 1000000
37 typedef unsigned char uchar;
39 #define M_BLOCK_OUT (M_BLOCK + 1000000)
40 uchar inbuf[M_BLOCK];
41 uchar outbuf[M_BLOCK_OUT];
42 uchar zbuf[M_BLOCK + 600 + (M_BLOCK / 100)];
44 int nIn, nOut, nZ;
46 static char *bzerrorstrings[] = {
47 "OK"
48 ,"SEQUENCE_ERROR"
49 ,"PARAM_ERROR"
50 ,"MEM_ERROR"
51 ,"DATA_ERROR"
52 ,"DATA_ERROR_MAGIC"
53 ,"IO_ERROR"
54 ,"UNEXPECTED_EOF"
55 ,"OUTBUFF_FULL"
56 ,"???" /* for future */
57 ,"???" /* for future */
58 ,"???" /* for future */
59 ,"???" /* for future */
60 ,"???" /* for future */
61 ,"???" /* for future */
64 void flip_bit ( int bit )
66 int byteno = bit / 8;
67 int bitno = bit % 8;
68 uchar mask = 1 << bitno;
69 //fprintf ( stderr, "(byte %d bit %d mask %d)",
70 // byteno, bitno, (int)mask );
71 zbuf[byteno] ^= mask;
74 int main ( int argc, char** argv )
76 FILE* f;
77 int r;
78 int bit;
79 int i;
81 if (argc != 2) {
82 fprintf ( stderr, "usage: unzcrash filename\n" );
83 return 1;
86 f = fopen ( argv[1], "r" );
87 if (!f) {
88 fprintf ( stderr, "unzcrash: can't open %s\n", argv[1] );
89 return 1;
92 nIn = fread ( inbuf, 1, M_BLOCK, f );
93 fprintf ( stderr, "%d bytes read\n", nIn );
95 nZ = M_BLOCK;
96 r = BZ2_bzBuffToBuffCompress (
97 zbuf, &nZ, inbuf, nIn, 9, 0, 30 );
99 assert (r == BZ_OK);
100 fprintf ( stderr, "%d after compression\n", nZ );
102 for (bit = 0; bit < nZ*8; bit++) {
103 fprintf ( stderr, "bit %d ", bit );
104 flip_bit ( bit );
105 nOut = M_BLOCK_OUT;
106 r = BZ2_bzBuffToBuffDecompress (
107 outbuf, &nOut, zbuf, nZ, 0, 0 );
108 fprintf ( stderr, " %d %s ", r, bzerrorstrings[-r] );
110 if (r != BZ_OK) {
111 fprintf ( stderr, "\n" );
112 } else {
113 if (nOut != nIn) {
114 fprintf(stderr, "nIn/nOut mismatch %d %d\n", nIn, nOut );
115 return 1;
116 } else {
117 for (i = 0; i < nOut; i++)
118 if (inbuf[i] != outbuf[i]) {
119 fprintf(stderr, "mismatch at %d\n", i );
120 return 1;
122 if (i == nOut) fprintf(stderr, "really ok!\n" );
126 flip_bit ( bit );
129 #if 0
130 assert (nOut == nIn);
131 for (i = 0; i < nOut; i++) {
132 if (inbuf[i] != outbuf[i]) {
133 fprintf ( stderr, "difference at %d !\n", i );
134 return 1;
137 #endif
139 fprintf ( stderr, "all ok\n" );
140 return 0;