ReactOS  0.4.14-dev-41-g31d7680
unzcrash.c
Go to the documentation of this file.
1 
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!
10 
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.
14 */
15 
16 /* ------------------------------------------------------------------
17  This file is part of bzip2/libbzip2, a program and library for
18  lossless, block-sorting data compression.
19 
20  bzip2/libbzip2 version 1.0.6 of 6 September 2010
21  Copyright (C) 1996-2010 Julian Seward <jseward@bzip.org>
22 
23  Please read the WARNING, DISCLAIMER and PATENTS sections in the
24  README file.
25 
26  This program is released under the terms of the license contained
27  in the file LICENSE.
28  ------------------------------------------------------------------ */
29 
30 
31 #include <stdio.h>
32 #include <assert.h>
33 #include "bzlib.h"
34 
35 #define M_BLOCK 1000000
36 
37 typedef unsigned char uchar;
38 
39 #define M_BLOCK_OUT (M_BLOCK + 1000000)
42 uchar zbuf[M_BLOCK + 600 + (M_BLOCK / 100)];
43 
44 int nIn, nOut, nZ;
45 
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 */
62 };
63 
64 void flip_bit ( int bit )
65 {
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;
72 }
73 
74 int main ( int argc, char** argv )
75 {
76  FILE* f;
77  int r;
78  int bit;
79  int i;
80 
81  if (argc != 2) {
82  fprintf ( stderr, "usage: unzcrash filename\n" );
83  return 1;
84  }
85 
86  f = fopen ( argv[1], "r" );
87  if (!f) {
88  fprintf ( stderr, "unzcrash: can't open %s\n", argv[1] );
89  return 1;
90  }
91 
92  nIn = fread ( inbuf, 1, M_BLOCK, f );
93  fprintf ( stderr, "%d bytes read\n", nIn );
94 
95  nZ = M_BLOCK;
97  zbuf, &nZ, inbuf, nIn, 9, 0, 30 );
98 
99  assert (r == BZ_OK);
100  fprintf ( stderr, "%d after compression\n", nZ );
101 
102  for (bit = 0; bit < nZ*8; bit++) {
103  fprintf ( stderr, "bit %d ", bit );
104  flip_bit ( bit );
105  nOut = M_BLOCK_OUT;
107  outbuf, &nOut, zbuf, nZ, 0, 0 );
108  fprintf ( stderr, " %d %s ", r, bzerrorstrings[-r] );
109 
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;
121  }
122  if (i == nOut) fprintf(stderr, "really ok!\n" );
123  }
124  }
125 
126  flip_bit ( bit );
127  }
128 
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;
135  }
136  }
137 #endif
138 
139  fprintf ( stderr, "all ok\n" );
140  return 0;
141 }
int BZ_API() BZ2_bzBuffToBuffDecompress(char *dest, unsigned int *destLen, char *source, unsigned int sourceLen, int small, int verbosity)
Definition: bzlib.c:1317
static int argc
Definition: ServiceArgs.c:12
#define M_BLOCK_OUT
Definition: unzcrash.c:39
unsigned char uchar
Definition: Unfrag.h:59
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define assert(x)
Definition: debug.h:53
#define M_BLOCK
Definition: unzcrash.c:35
#define argv
Definition: mplay32.c:18
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLenum GLint GLuint mask
Definition: glext.h:6028
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
unsigned char uchar
Definition: unzcrash.c:37
int main(int argc, char **argv)
Definition: unzcrash.c:74
#define BZ_OK
Definition: bzlib.h:33
GLfloat f
Definition: glext.h:7540
void flip_bit(int bit)
Definition: unzcrash.c:64
int nOut
Definition: unzcrash.c:44
static char * bzerrorstrings[]
Definition: unzcrash.c:46
uchar inbuf[M_BLOCK]
Definition: unzcrash.c:40
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
int nZ
Definition: unzcrash.c:44
uchar zbuf[M_BLOCK+600+(M_BLOCK/100)]
Definition: unzcrash.c:42
#define f
Definition: ke_i.h:83
int BZ_API() BZ2_bzBuffToBuffCompress(char *dest, unsigned int *destLen, char *source, unsigned int sourceLen, int blockSize100k, int verbosity, int workFactor)
Definition: bzlib.c:1264
FILE * stderr
int nIn
Definition: unzcrash.c:44
uchar outbuf[M_BLOCK_OUT]
Definition: unzcrash.c:41