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