-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
testpackbits.c
303 lines (246 loc) · 7.28 KB
/
testpackbits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// PackBits unit test program
//
// Usage:
//
// ./testpackbits [COUNT]
//
// Copyright © 2024 by Michael R Sweet
//
#include "lprint.h"
#include "test.h"
//
// Local functions...
//
static unsigned get_rand(void);
static size_t uncompress_packbits(unsigned char *dst, size_t dstsize, const unsigned char *src, size_t srclen);
//
// 'main()' - Main entry for test program.
//
int // O - Exit status
main(int argc, // I - Number of command-line arguments
char *argv[]) // I - Command-line arguments
{
int i, // Looping var
num_tests; // Number of tests to run
size_t offset, // Offset in source buffer
count, // Looping var
len; // Length of sequence
unsigned char src[1024], // Source buffer
check[2048]; // Check buffer
size_t checklen; // Check data length
unsigned char *dst; // Destination buffer
size_t dstlen, // Number of destination bytes
dstmax; // Maximum number of destination bytes
// See if the number of tests is on the command-line...
if (argc == 1)
{
num_tests = 1000000;
}
else if (argc > 2 || (num_tests = atoi(argv[1])) < 1)
{
fputs("Usage: ./testpackbits [COUNT]\n", stderr);
return (1);
}
// Maximum expansion as defined by the PackBits algorithm...
dstmax = sizeof(src) + sizeof(src) / 128;
// Allocate PackBits buffer
testBegin("lprintPackBitsAlloc(%u)", (unsigned)(2 * sizeof(src)));
if ((dst = lprintPackBitsAlloc(2 * sizeof(src))) != NULL)
testEnd(true);
else
testEndMessage(false, "%s", strerror(errno));
// Loop many times with random changes to the source buffer to test different
// source values...
memset(src, 0, sizeof(src));
testBegin("lprintPackBitsCompress(%d times)", num_tests);
for (i = 0; i < num_tests; i ++)
{
// Show progress...
testProgress();
// Make a random change to the source buffer...
len = (get_rand() % (sizeof(src) / 4)) + 1;
offset = get_rand() % (sizeof(src) - len);
if (get_rand() & 1)
{
// Add random bytes
for (count = 0; count < len; count ++)
src[offset + count] = get_rand();
}
else
{
// Add constant bytes
memset(src + offset, get_rand(), len);
}
// Compress the result
if ((dstlen = lprintPackBitsCompress(dst, src, sizeof(src))) == 0 || dstlen > dstmax)
{
testEndMessage(false, "%u > %u", (unsigned)dstlen, (unsigned)dstmax);
testError("\nSource Buffer:");
testHexDump(src, sizeof(src));
testError("\nDestination Buffer:");
testHexDump(dst, dstlen);
break;
}
// Verify compressed result...
if ((checklen = uncompress_packbits(check, sizeof(check), dst, dstlen)) != sizeof(src) || memcmp(check, src, sizeof(src)))
{
testEndMessage(false, "Decompression Failure");
testError("\nSource Buffer:");
testHexDump(src, sizeof(src));
testError("\nDestination Buffer (%u bytes):", (unsigned)dstlen);
testHexDump(dst, dstlen);
testError("\nCheck Buffer (%u bytes):", (unsigned)checklen);
testHexDump(check, checklen);
break;
}
}
if (i >= num_tests)
testEnd(true);
return (testsPassed ? 0 : 1);
}
//
// 'get_rand()' - Return a 32-bit pseudo-random number.
//
// This function returns a 32-bit pseudo-random number suitable for use as
// one-time identifiers or nonces. The random numbers are generated/seeded
// using system entropy.
//
static unsigned // O - Random number
get_rand(void)
{
#if _WIN32
// rand_s uses real entropy...
unsigned v; // Random number
rand_s(&v);
return (v);
#elif defined(__APPLE__)
// macOS/iOS arc4random() uses real entropy automatically...
return (arc4random());
#else
// Use a Mersenne twister random number generator seeded from /dev/urandom...
unsigned i, // Looping var
temp; // Temporary value
static bool first_time = true; // First time we ran?
static unsigned mt_state[624], // Mersenne twister state
mt_index; // Mersenne twister index
if (first_time)
{
int fd; // "/dev/urandom" file
struct timeval curtime; // Current time
// Seed the random number state...
if ((fd = open("/dev/urandom", O_RDONLY)) >= 0)
{
// Read random entropy from the system...
if (read(fd, mt_state, sizeof(mt_state[0])) < sizeof(mt_state[0]))
mt_state[0] = 0; // Force fallback...
close(fd);
}
else
mt_state[0] = 0;
if (!mt_state[0])
{
// Fallback to using the current time in microseconds...
gettimeofday(&curtime, NULL);
mt_state[0] = (unsigned)(curtime.tv_sec + curtime.tv_usec);
}
mt_index = 0;
for (i = 1; i < 624; i ++)
mt_state[i] = (unsigned)((1812433253 * (mt_state[i - 1] ^ (mt_state[i - 1] >> 30))) + i);
first_time = false;
}
if (mt_index == 0)
{
// Generate a sequence of random numbers...
unsigned i1 = 1, i397 = 397; // Looping vars
for (i = 0; i < 624; i ++)
{
temp = (mt_state[i] & 0x80000000) + (mt_state[i1] & 0x7fffffff);
mt_state[i] = mt_state[i397] ^ (temp >> 1);
if (temp & 1)
mt_state[i] ^= 2567483615u;
i1 ++;
i397 ++;
if (i1 == 624)
i1 = 0;
if (i397 == 624)
i397 = 0;
}
}
// Pull 32-bits of random data...
temp = mt_state[mt_index ++];
temp ^= temp >> 11;
temp ^= (temp << 7) & 2636928640u;
temp ^= (temp << 15) & 4022730752u;
temp ^= temp >> 18;
if (mt_index == 624)
mt_index = 0;
return (temp);
#endif // _WIN32
}
//
// 'uncompress_packbits()' - Uncompress PackBits data.
//
static size_t // O - Number of uncompressed bytes
uncompress_packbits(
unsigned char *dst, // I - Output buffer
size_t dstsize, // I - Size of output buffer
const unsigned char *src, // I - Input buffer
size_t srclen) // I - Length of input buffer
{
const unsigned char *srcptr, // Pointer into input buffer
*srcend; // End of input buffer
unsigned char *dstptr, // Pointer into output buffer
*dstend; // End of output buffer
unsigned count; // Literal/repeat count
// Loop through the input buffer and decompress...
dstptr = dst;
dstend = dst + dstsize;
srcptr = src;
srcend = src + srclen;
while (srcptr < srcend)
{
if (*srcptr < 0x80)
{
// N literal bytes
count = *srcptr + 1;
srcptr ++;
if ((srcptr + count) > srcend)
{
testError("Literal count %u but only %ld input bytes remaining.", count, srcend - srcptr);
return (0);
}
if (count > (unsigned)(dstend - dstptr))
{
testError("Literal count %u but only %ld output bytes remaining.", count, dstend - dstptr);
return (0);
}
memcpy(dstptr, srcptr, count);
dstptr += count;
srcptr += count;
}
else if (*srcptr == 0x80)
{
testError("Undefined pack value 0x80 seen.");
}
else
{
count = 257 - *srcptr;
srcptr ++;
if (srcptr >= srcend)
{
testError("Repeat count %u but no input bytes remaining.", count);
return (0);
}
if (count > (unsigned)(dstend - dstptr))
{
testError("Repeat count %u but only %ld output bytes remaining.", count, dstend - dstptr);
return (0);
}
memset(dstptr, *srcptr, count);
dstptr += count;
srcptr ++;
}
}
return ((size_t)(dstptr - dst));
}