]> git.proxmox.com Git - ceph.git/blame - ceph/src/isa-l/igzip/igzip_rand_test.c
Import ceph 15.2.8
[ceph.git] / ceph / src / isa-l / igzip / igzip_rand_test.c
CommitLineData
7c673cae
FG
1/**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28**********************************************************************/
29
f91f0fd5 30#define _FILE_OFFSET_BITS 64
7c673cae
FG
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
224ce89b 34#include <assert.h>
f91f0fd5 35#include <stdarg.h>
7c673cae 36#include "igzip_lib.h"
f91f0fd5 37#include "checksum_test_ref.h"
224ce89b 38#include "inflate_std_vects.h"
7c673cae 39#include <math.h>
f91f0fd5
TL
40#include "test.h"
41#include "unaligned.h"
42
43#ifdef HAVE_GETOPT
44#include <getopt.h>
45#endif
7c673cae
FG
46
47#ifndef RANDOMS
224ce89b 48# define RANDOMS 0x40
7c673cae
FG
49#endif
50#ifndef TEST_SEED
51# define TEST_SEED 0x1234
52#endif
53
f91f0fd5
TL
54#define MAX_BITS_COUNT 20
55#define MIN_BITS_COUNT 8
56
7c673cae
FG
57#define IBUF_SIZE (1024*1024)
58
f91f0fd5
TL
59#define MAX_LARGE_COMP_BUF_SIZE (1024*1024)
60
7c673cae
FG
61#define PAGE_SIZE 4*1024
62
f91f0fd5
TL
63#define MAX_FILE_SIZE 0x7fff8fff
64
7c673cae
FG
65#define str1 "Short test string"
66#define str2 "one two three four five six seven eight nine ten eleven twelve " \
67 "thirteen fourteen fifteen sixteen"
68
69#define TYPE0_HDR_SIZE 5 /* Size of a type 0 blocks header in bytes */
70#define TYPE0_MAX_SIZE 65535 /* Max length of a type 0 block in bytes (excludes the header) */
71
72#define MAX_LOOPS 20
73/* Defines for the possible error conditions */
74enum IGZIP_TEST_ERROR_CODES {
f91f0fd5 75 IGZIP_COMP_OK = 0,
7c673cae
FG
76
77 MALLOC_FAILED,
78 FILE_READ_FAILED,
79
80 COMPRESS_INCORRECT_STATE,
81 COMPRESS_INPUT_STREAM_INTEGRITY_ERROR,
82 COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR,
83 COMPRESS_END_OF_STREAM_NOT_SET,
84 COMPRESS_ALL_INPUT_FAIL,
85 COMPRESS_OUT_BUFFER_OVERFLOW,
86 COMPRESS_LOOP_COUNT_OVERFLOW,
87 COMPRESS_GENERAL_ERROR,
88
89 INFLATE_END_OF_INPUT,
90 INFLATE_INVALID_BLOCK_HEADER,
91 INFLATE_INVALID_SYMBOL,
92 INFLATE_OUT_BUFFER_OVERFLOW,
7c673cae
FG
93 INFLATE_LEFTOVER_INPUT,
94 INFLATE_INCORRECT_OUTPUT_SIZE,
95 INFLATE_INVALID_LOOK_BACK_DISTANCE,
f91f0fd5
TL
96 INFLATE_INPUT_STREAM_INTEGRITY_ERROR,
97 INFLATE_OUTPUT_STREAM_INTEGRITY_ERROR,
7c673cae
FG
98 INVALID_GZIP_HEADER,
99 INCORRECT_GZIP_TRAILER,
f91f0fd5
TL
100 INVALID_ZLIB_HEADER,
101 INCORRECT_ZLIB_TRAILER,
102
103 UNSUPPORTED_METHOD,
104
7c673cae
FG
105 INFLATE_GENERAL_ERROR,
106
107 INVALID_FLUSH_ERROR,
108
109 OVERFLOW_TEST_ERROR,
110 RESULT_ERROR
111};
112
224ce89b 113static const int hdr_bytes = 300;
7c673cae 114
f91f0fd5
TL
115static const uint32_t gzip_trl_bytes = 8;
116static const uint32_t zlib_trl_bytes = 4;
224ce89b 117static const int gzip_extra_bytes = 18; /* gzip_hdr_bytes + gzip_trl_bytes */
f91f0fd5 118static const int zlib_extra_bytes = 6; /* zlib_hdr_bytes + zlib_trl_bytes */
7c673cae 119
224ce89b 120int inflate_type = 0;
7c673cae
FG
121
122struct isal_hufftables *hufftables = NULL;
f91f0fd5 123struct isal_hufftables *hufftables_subset = NULL;
7c673cae
FG
124
125#define HISTORY_SIZE 32*1024
126#define MIN_LENGTH 3
127#define MIN_DIST 1
128
f91f0fd5
TL
129struct test_options {
130 int test_seed;
131 int randoms;
132 int do_large_test;
133 int verbose;
134
135};
136
137struct test_options options;
138
139void init_options(void)
140{
141 options.test_seed = TEST_SEED;
142 options.randoms = RANDOMS;
143 options.do_large_test = 1;
144#ifdef VERBOSE
145 options.verbose = 1;
146#else
147 options.verbose = 0;
148#endif
149}
150
151void usage(void)
152{
153 fprintf(stderr,
154 "Usage: igzip_rand_test [options] [FILES]\n"
155 " -h help, print this message\n"
156 " -l turn off large input test\n"
157 " -r <iter> number of randoms for each test\n"
158 " -s <seed> set rand() test seed\n"
159 " -v enable verbose test log\n");
160 exit(0);
161}
162
163size_t parse_options(int argc, char *argv[])
164{
165 init_options();
166#ifdef HAVE_GETOPT
167 int c;
168 char optstring[] = "hlr:s:v";
169 while ((c = getopt(argc, argv, optstring)) != -1) {
170 switch (c) {
171 case 'l':
172 options.do_large_test = 0;
173 break;
174 case 'r':
175 options.randoms = atoi(optarg);
176 break;
177 case 's':
178 options.test_seed = atoi(optarg);
179 break;
180 case 'v':
181 options.verbose = 1;
182 break;
183 case 'h':
184 default:
185 usage();
186 break;
187 }
188 }
189 return optind;
190#else
191 return 1;
192#endif
193}
194
7c673cae
FG
195/* Create random compressible data. This is achieved by randomly choosing a
196 * random character, or to repeat previous data in the stream for a random
197 * length and look back distance. The probability of a random character or a
198 * repeat being chosen is semi-randomly chosen by setting max_repeat_data to be
199 * differing values */
200void create_rand_repeat_data(uint8_t * data, int size)
201{
202 uint32_t next_data;
203 uint8_t *data_start = data;
204 uint32_t length, distance;
f91f0fd5
TL
205 uint32_t symbol_count = rand() % 255 + 1, swaps_left, tmp;
206 uint32_t max_repeat_data = symbol_count;
207 uint8_t symbols[256], *symbols_next, swap_val;
208
7c673cae
FG
209 /* An array of the powers of 2 (except the final element which is 0) */
210 const uint32_t power_of_2_array[] = {
211 0x00000001, 0x00000002, 0x00000004, 0x00000008,
212 0x00000010, 0x00000020, 0x00000040, 0x00000080,
213 0x00000100, 0x00000200, 0x00000400, 0x00000800,
214 0x00001000, 0x00002000, 0x00004000, 0x00008000,
215 0x00010000, 0x00020000, 0x00040000, 0x00080000,
216 0x00100000, 0x00200000, 0x00400000, 0x00800000,
217 0x01000000, 0x02000000, 0x04000000, 0x08000000,
218 0x10000000, 0x20000000, 0x40000000, 0x00000000
219 };
220
f91f0fd5
TL
221 uint32_t power = rand() % sizeof(power_of_2_array) / sizeof(uint32_t);
222
223 if (symbol_count > 128) {
224 memset(symbols, 1, sizeof(symbols));
225 swap_val = 0;
226 swaps_left = 256 - symbol_count;
227 } else {
228 memset(symbols, 0, sizeof(symbols));
229 swap_val = 1;
230 swaps_left = symbol_count;
231 }
232
233 while (swaps_left > 0) {
234 tmp = rand() % 256;
235 if (symbols[tmp] != swap_val) {
236 symbols[tmp] = swap_val;
237 swaps_left--;
238 }
239 }
240
241 symbols_next = symbols;
242 for (tmp = 0; tmp < 256; tmp++) {
243 if (symbols[tmp]) {
244 *symbols_next = tmp;
245 symbols_next++;
246 }
247 }
248
7c673cae
FG
249 max_repeat_data += power_of_2_array[power];
250
f91f0fd5
TL
251 if (size > 0) {
252 size--;
7c673cae 253 *data++ = rand();
f91f0fd5 254 }
7c673cae
FG
255
256 while (size > 0) {
257 next_data = rand() % max_repeat_data;
f91f0fd5
TL
258 if (next_data < symbol_count) {
259 *data++ = symbols[next_data];
7c673cae
FG
260 size--;
261 } else if (size < 3) {
f91f0fd5 262 *data++ = symbols[rand() % symbol_count];
7c673cae
FG
263 size--;
264 } else {
265 length = (rand() % 256) + MIN_LENGTH;
266 if (length > size)
267 length = (rand() % (size - 2)) + MIN_LENGTH;
268
269 distance = (rand() % HISTORY_SIZE) + MIN_DIST;
270 if (distance > data - data_start)
271 distance = (rand() % (data - data_start)) + MIN_DIST;
272
273 size -= length;
274 if (distance <= length) {
275 while (length-- > 0) {
276 *data = *(data - distance);
277 data++;
278 }
f91f0fd5 279 } else {
7c673cae 280 memcpy(data, data - distance, length);
f91f0fd5
TL
281 data += length;
282 }
7c673cae
FG
283 }
284 }
285}
286
f91f0fd5
TL
287void create_rand_dict(uint8_t * dict, uint32_t dict_len, uint8_t * buf, uint32_t buf_len)
288{
289 uint32_t dict_chunk_size, buf_chunk_size;
290 while (dict_len > 0) {
291 dict_chunk_size = rand() % IGZIP_K;
292 dict_chunk_size = (dict_len >= dict_chunk_size) ? dict_chunk_size : dict_len;
293
294 buf_chunk_size = rand() % IGZIP_K;
295 buf_chunk_size = (buf_len >= buf_chunk_size) ? buf_chunk_size : buf_len;
296
297 if (rand() % 3 == 0 && buf_len >= dict_len)
298 memcpy(dict, buf, dict_chunk_size);
299 else
300 create_rand_repeat_data(dict, dict_chunk_size);
301
302 dict_len -= dict_chunk_size;
303 dict += dict_chunk_size;
304 buf_len -= buf_chunk_size;
305 buf += buf_chunk_size;
306 }
307
308}
309
310int get_rand_data_length(void)
311{
312 int max_mask =
313 (1 << ((rand() % (MAX_BITS_COUNT - MIN_BITS_COUNT)) + MIN_BITS_COUNT)) - 1;
314 return rand() & max_mask;
315}
316
317int get_rand_level(void)
318{
319 return ISAL_DEF_MIN_LEVEL + rand() % (ISAL_DEF_MAX_LEVEL - ISAL_DEF_MIN_LEVEL + 1);
320
321}
322
323int get_rand_level_buf_size(int level)
324{
325 int size;
326 switch (level) {
327 case 3:
328 size = rand() % IBUF_SIZE + ISAL_DEF_LVL3_MIN;
329 break;
330 case 2:
331 size = rand() % IBUF_SIZE + ISAL_DEF_LVL2_MIN;
332 break;
333 case 1:
334 default:
335 size = rand() % IBUF_SIZE + ISAL_DEF_LVL1_MIN;
336 }
337 return size;
338}
339
7c673cae
FG
340void print_error(int error_code)
341{
342 switch (error_code) {
343 case IGZIP_COMP_OK:
344 break;
345 case MALLOC_FAILED:
346 printf("error: failed to allocate memory\n");
347 break;
348 case FILE_READ_FAILED:
349 printf("error: failed to read in file\n");
350 break;
351 case COMPRESS_INCORRECT_STATE:
352 printf("error: incorrect stream internal state\n");
353 break;
354 case COMPRESS_INPUT_STREAM_INTEGRITY_ERROR:
355 printf("error: inconsistent stream input buffer\n");
356 break;
357 case COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR:
358 printf("error: inconsistent stream output buffer\n");
359 break;
360 case COMPRESS_END_OF_STREAM_NOT_SET:
361 printf("error: end of stream not set\n");
362 break;
363 case COMPRESS_ALL_INPUT_FAIL:
364 printf("error: not all input data compressed\n");
365 break;
366 case COMPRESS_OUT_BUFFER_OVERFLOW:
367 printf("error: output buffer overflow while compressing data\n");
368 break;
369 case COMPRESS_GENERAL_ERROR:
370 printf("error: compression failed\n");
371 break;
372 case INFLATE_END_OF_INPUT:
373 printf("error: did not decompress all input\n");
374 break;
375 case INFLATE_INVALID_BLOCK_HEADER:
376 printf("error: invalid header\n");
377 break;
378 case INFLATE_INVALID_SYMBOL:
379 printf("error: invalid symbol found when decompressing input\n");
380 break;
381 case INFLATE_OUT_BUFFER_OVERFLOW:
382 printf("error: output buffer overflow while decompressing data\n");
383 break;
7c673cae
FG
384 case INFLATE_GENERAL_ERROR:
385 printf("error: decompression failed\n");
386 break;
387 case INFLATE_LEFTOVER_INPUT:
388 printf("error: the trailer of igzip output contains junk\n");
389 break;
390 case INFLATE_INCORRECT_OUTPUT_SIZE:
391 printf("error: incorrect amount of data was decompressed\n");
392 break;
393 case INFLATE_INVALID_LOOK_BACK_DISTANCE:
394 printf("error: invalid look back distance found while decompressing\n");
395 break;
f91f0fd5
TL
396 case INFLATE_INPUT_STREAM_INTEGRITY_ERROR:
397 printf("error: inconsistent input buffer\n");
398 break;
399 case INFLATE_OUTPUT_STREAM_INTEGRITY_ERROR:
400 printf("error: inconsistent output buffer\n");
401 break;
7c673cae
FG
402 case INVALID_GZIP_HEADER:
403 printf("error: incorrect gzip header found when inflating data\n");
404 break;
405 case INCORRECT_GZIP_TRAILER:
406 printf("error: incorrect gzip trailer found when inflating data\n");
407 break;
f91f0fd5
TL
408 case INVALID_ZLIB_HEADER:
409 printf("error: incorrect zlib header found when inflating data\n");
410 break;
411 case INCORRECT_ZLIB_TRAILER:
412 printf("error: incorrect zlib trailer found when inflating data\n");
413 break;
414 case UNSUPPORTED_METHOD:
415 printf("error: invalid compression method in wrapper header\n");
416 break;
7c673cae
FG
417 case INVALID_FLUSH_ERROR:
418 printf("error: invalid flush did not cause compression to error\n");
419 break;
420 case RESULT_ERROR:
421 printf("error: decompressed data is not the same as the compressed data\n");
422 break;
423 case OVERFLOW_TEST_ERROR:
424 printf("error: overflow undetected\n");
425 break;
426 default:
427 printf("error: unknown error code\n");
428 }
429}
430
431void print_uint8_t(uint8_t * array, uint64_t length)
432{
433 const int line_size = 16;
434 int i;
435
436 printf("Length = %lu", length);
437 for (i = 0; i < length; i++) {
438 if ((i % line_size) == 0)
439 printf("\n0x%08x\t", i);
440 else
441 printf(" ");
442 printf("0x%02x,", array[i]);
443 }
444 printf("\n");
445}
446
f91f0fd5 447void log_print(char *format, ...)
7c673cae 448{
f91f0fd5
TL
449 va_list args;
450 va_start(args, format);
7c673cae 451
f91f0fd5
TL
452 if (options.verbose)
453 vfprintf(stdout, format, args);
7c673cae 454
f91f0fd5
TL
455 va_end(args);
456}
7c673cae 457
f91f0fd5
TL
458void log_uint8_t(uint8_t * array, uint64_t length)
459{
460 if (options.verbose)
461 print_uint8_t(array, length);
462}
7c673cae 463
f91f0fd5
TL
464void log_error(int error_code)
465{
466 if (options.verbose)
467 print_error(error_code);
7c673cae
FG
468}
469
224ce89b
WB
470uint32_t check_gzip_trl(uint64_t gzip_trl, uint32_t inflate_crc, uint8_t * uncompress_buf,
471 uint32_t uncompress_len)
7c673cae 472{
224ce89b
WB
473 uint64_t trl, ret = 0;
474 uint32_t crc;
7c673cae 475
f91f0fd5 476 crc = crc32_gzip_refl_ref(0, uncompress_buf, uncompress_len);
224ce89b 477 trl = ((uint64_t) uncompress_len << 32) | crc;
7c673cae 478
224ce89b 479 if (crc != inflate_crc || trl != gzip_trl)
7c673cae
FG
480 ret = INCORRECT_GZIP_TRAILER;
481
482 return ret;
483}
224ce89b 484
f91f0fd5
TL
485uint32_t check_zlib_trl(uint32_t zlib_trl, uint32_t inflate_adler, uint8_t * uncompress_buf,
486 uint32_t uncompress_len)
487{
488 uint32_t trl, ret = 0;
489 uint32_t adler;
490
491 adler = adler_ref(1, uncompress_buf, uncompress_len);
492
493 trl =
494 (adler >> 24) | ((adler >> 8) & 0xFF00) | (adler << 24) | ((adler & 0xFF00) << 8);
495
496 if (adler != inflate_adler || trl != zlib_trl) {
497 ret = INCORRECT_ZLIB_TRAILER;
498 }
499
500 return ret;
501}
502
224ce89b
WB
503int inflate_stateless_pass(uint8_t * compress_buf, uint64_t compress_len,
504 uint8_t * uncompress_buf, uint32_t * uncompress_len,
505 uint32_t gzip_flag)
506{
507 struct inflate_state state;
f91f0fd5
TL
508 int ret = 0, offset = 0;
509 struct isal_gzip_header gz_hdr;
510 struct isal_zlib_header z_hdr;
224ce89b
WB
511
512 state.next_in = compress_buf;
513 state.avail_in = compress_len;
514 state.next_out = uncompress_buf;
515 state.avail_out = *uncompress_len;
f91f0fd5
TL
516
517 if (gzip_flag == IGZIP_GZIP) {
518 if (rand() % 2 == 0) {
519 memset(&gz_hdr, 0, sizeof(gz_hdr));
520 isal_inflate_reset(&state);
521 state.tmp_in_size = 0;
522 gzip_flag = ISAL_GZIP_NO_HDR_VER;
523
524 isal_read_gzip_header(&state, &gz_hdr);
525 }
526 } else if (gzip_flag == IGZIP_ZLIB) {
527 if (rand() % 2 == 0) {
528 memset(&z_hdr, 0, sizeof(z_hdr));
529 isal_inflate_reset(&state);
530 gzip_flag = ISAL_ZLIB_NO_HDR_VER;
531 isal_read_zlib_header(&state, &z_hdr);
532 }
533 }
534
224ce89b
WB
535 state.crc_flag = gzip_flag;
536
537 ret = isal_inflate_stateless(&state);
538
539 *uncompress_len = state.total_out;
540
541 if (gzip_flag) {
f91f0fd5
TL
542 if (gzip_flag == IGZIP_GZIP || gzip_flag == IGZIP_GZIP_NO_HDR
543 || gzip_flag == ISAL_GZIP_NO_HDR_VER) {
544 if (gzip_flag == IGZIP_GZIP || gzip_flag == ISAL_GZIP_NO_HDR_VER)
545 offset = gzip_trl_bytes;
546
547 if (!ret)
548 ret =
549 check_gzip_trl(load_u64(state.next_in - offset),
550 state.crc, uncompress_buf, *uncompress_len);
551 else if (ret == ISAL_INCORRECT_CHECKSUM)
552 ret = INCORRECT_GZIP_TRAILER;
553 state.avail_in -= (gzip_trl_bytes - offset);
554 } else if (gzip_flag == IGZIP_ZLIB || gzip_flag == IGZIP_ZLIB_NO_HDR
555 || gzip_flag == ISAL_ZLIB_NO_HDR_VER) {
556 if (gzip_flag == IGZIP_ZLIB || gzip_flag == ISAL_ZLIB_NO_HDR_VER)
557 offset = zlib_trl_bytes;
558
559 if (!ret)
560 ret =
561 check_zlib_trl(load_u32(state.next_in - offset),
562 state.crc, uncompress_buf, *uncompress_len);
563 else if (ret == ISAL_INCORRECT_CHECKSUM)
564 ret = INCORRECT_ZLIB_TRAILER;
565 state.avail_in -= (zlib_trl_bytes - offset);
566
567 }
568
224ce89b
WB
569 }
570
571 if (ret == 0 && state.avail_in != 0)
572 ret = INFLATE_LEFTOVER_INPUT;
573
574 return ret;
575}
576
f91f0fd5
TL
577/* Check if that the state of the data stream is consistent */
578int inflate_state_valid_check(struct inflate_state *state, uint8_t * in_buf, uint32_t in_size,
579 uint8_t * out_buf, uint32_t out_size, uint32_t in_processed,
580 uint32_t out_processed, uint32_t data_size)
581{
582 uint32_t in_buffer_size, total_out, out_buffer_size;
583
584 in_buffer_size = (in_size == 0) ? 0 : state->next_in - in_buf + state->avail_in;
585
586 /* Check for a consistent amount of data processed */
587 if (in_buffer_size != in_size)
588 return INFLATE_INPUT_STREAM_INTEGRITY_ERROR;
589
590 total_out =
591 (out_size == 0) ? out_processed : out_processed + (state->next_out - out_buf);
592 out_buffer_size = (out_size == 0) ? 0 : state->next_out - out_buf + state->avail_out;
593
594 /* Check for a consistent amount of data compressed */
595 if (total_out != state->total_out || out_buffer_size != out_size)
596 return INFLATE_OUTPUT_STREAM_INTEGRITY_ERROR;
597
598 return 0;
599}
600
601/* Performs compression with checks to discover and verify the state of the
602 * stream
603 * state: inflate data structure which has been initialized to use
604 * in_buf and out_buf as the buffers
605 * compress_len: size of all input compressed data
606 * data_size: size of all available output buffers
607 * in_buf: next buffer of data to be inflated
608 * in_size: size of in_buf
609 * out_buf: next out put buffer where data is stored
610 * out_size: size of out_buf
611 * in_processed: the amount of input data which has been loaded into buffers
612 * to be inflated, this includes the data in in_buf
613 * out_processed: the amount of output data which has been decompressed and stored,
614 * this does not include the data in the current out_buf
615*/
616int isal_inflate_with_checks(struct inflate_state *state, uint32_t compress_len,
617 uint32_t data_size, uint8_t * in_buf, uint32_t in_size,
618 uint32_t in_processed, uint8_t * out_buf, uint32_t out_size,
619 uint32_t out_processed)
620{
621 int ret, stream_check = 0;
622
623 ret = isal_inflate(state);
624
625 /* Verify the stream is in a valid state when no errors occured */
626 if (ret >= 0) {
627 stream_check =
628 inflate_state_valid_check(state, in_buf, in_size, out_buf, out_size,
629 in_processed, out_processed, data_size);
630 }
631
632 if (stream_check != 0)
633 return stream_check;
634
635 return ret;
636
637}
638
224ce89b 639int inflate_multi_pass(uint8_t * compress_buf, uint64_t compress_len,
f91f0fd5
TL
640 uint8_t * uncompress_buf, uint32_t * uncompress_len, uint32_t gzip_flag,
641 uint8_t * dict, uint32_t dict_len, uint32_t hist_bits)
224ce89b
WB
642{
643 struct inflate_state *state = NULL;
644 int ret = 0;
645 uint8_t *comp_tmp = NULL, *uncomp_tmp = NULL;
646 uint32_t comp_tmp_size = 0, uncomp_tmp_size = 0;
647 uint32_t comp_processed = 0, uncomp_processed = 0;
648 int32_t read_in_old = 0;
f91f0fd5 649 uint32_t reset_test_flag = 0;
224ce89b
WB
650
651 state = malloc(sizeof(struct inflate_state));
652 if (state == NULL) {
653 printf("Failed to allocate memory\n");
654 exit(0);
655 }
656
f91f0fd5 657 create_rand_repeat_data((uint8_t *) state, sizeof(state));
224ce89b
WB
658 isal_inflate_init(state);
659
f91f0fd5
TL
660 if (rand() % 4 == 0) {
661 /* Test reset */
662 reset_test_flag = 1;
663 create_rand_repeat_data((uint8_t *) state, sizeof(state));
664 }
665
666 if (gzip_flag == IGZIP_GZIP_NO_HDR) {
667 if (rand() % 2 == 0)
668 compress_len -= gzip_trl_bytes;
669 else
670 gzip_flag = ISAL_GZIP_NO_HDR_VER;
671 } else if (gzip_flag == IGZIP_ZLIB_NO_HDR) {
672 if (rand() % 2 == 0)
673 compress_len -= zlib_trl_bytes;
674 else
675 gzip_flag = ISAL_ZLIB_NO_HDR_VER;
676 }
677
224ce89b
WB
678 state->next_in = NULL;
679 state->next_out = NULL;
680 state->avail_in = 0;
681 state->avail_out = 0;
682 state->crc_flag = gzip_flag;
f91f0fd5
TL
683 state->hist_bits = hist_bits;
684
685 if (reset_test_flag)
686 isal_inflate_reset(state);
224ce89b 687
f91f0fd5
TL
688 if (dict != NULL)
689 isal_inflate_set_dict(state, dict, dict_len);
224ce89b
WB
690
691 while (1) {
692 if (state->avail_in == 0) {
693 comp_tmp_size = rand() % (compress_len + 1);
694
695 if (comp_tmp_size >= compress_len - comp_processed)
696 comp_tmp_size = compress_len - comp_processed;
697
698 if (comp_tmp_size != 0) {
699 if (comp_tmp != NULL) {
700 free(comp_tmp);
701 comp_tmp = NULL;
702 }
703
704 comp_tmp = malloc(comp_tmp_size);
705
706 if (comp_tmp == NULL) {
707 printf("Failed to allocate memory\n");
708 return MALLOC_FAILED;
709 }
710
711 memcpy(comp_tmp, compress_buf + comp_processed, comp_tmp_size);
712 comp_processed += comp_tmp_size;
713
714 state->next_in = comp_tmp;
715 state->avail_in = comp_tmp_size;
716 }
717 }
718
719 if (state->avail_out == 0) {
720 /* Save uncompressed data into uncompress_buf */
721 if (uncomp_tmp != NULL) {
722 memcpy(uncompress_buf + uncomp_processed, uncomp_tmp,
723 uncomp_tmp_size);
724 uncomp_processed += uncomp_tmp_size;
725 }
726
727 uncomp_tmp_size = rand() % (*uncompress_len + 1);
728
729 /* Limit size of buffer to be smaller than maximum */
730 if (uncomp_tmp_size > *uncompress_len - uncomp_processed)
731 uncomp_tmp_size = *uncompress_len - uncomp_processed;
732
733 if (uncomp_tmp_size != 0) {
734
735 if (uncomp_tmp != NULL) {
736 fflush(0);
737 free(uncomp_tmp);
738 uncomp_tmp = NULL;
739 }
740
741 uncomp_tmp = malloc(uncomp_tmp_size);
742 if (uncomp_tmp == NULL) {
743 printf("Failed to allocate memory\n");
744 return MALLOC_FAILED;
745 }
746
747 state->avail_out = uncomp_tmp_size;
748 state->next_out = uncomp_tmp;
749 }
750 }
751
f91f0fd5
TL
752 log_print("Pre inflate\n");
753 log_print
754 ("compressed_size = 0x%05lx, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x\n",
755 compress_len, comp_processed, comp_tmp_size, state->avail_in);
756 log_print
757 ("data_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n",
758 *uncompress_len, uncomp_processed, uncomp_tmp_size, state->avail_out,
759 state->total_out);
760
761 ret = isal_inflate_with_checks(state, compress_len, *uncompress_len, comp_tmp,
762 comp_tmp_size, comp_processed, uncomp_tmp,
763 uncomp_tmp_size, uncomp_processed);
764
765 log_print("Post inflate\n");
766 log_print
767 ("compressed_size = 0x%05lx, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x\n",
768 compress_len, comp_processed, comp_tmp_size, state->avail_in);
769 log_print
770 ("data_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n",
771 *uncompress_len, uncomp_processed, uncomp_tmp_size, state->avail_out,
772 state->total_out);
224ce89b
WB
773
774 if (state->block_state == ISAL_BLOCK_FINISH || ret != 0) {
775 memcpy(uncompress_buf + uncomp_processed, uncomp_tmp, uncomp_tmp_size);
776 *uncompress_len = state->total_out;
777 break;
778 }
779
780 if (*uncompress_len - uncomp_processed == 0 && state->avail_out == 0
781 && state->tmp_out_valid - state->tmp_out_processed > 0) {
782 ret = ISAL_OUT_OVERFLOW;
783 break;
784 }
785
786 if (compress_len - comp_processed == 0 && state->avail_in == 0
787 && (state->block_state != ISAL_BLOCK_INPUT_DONE)
788 && state->tmp_out_valid - state->tmp_out_processed == 0) {
789 if (state->read_in_length == read_in_old) {
790 ret = ISAL_END_INPUT;
791 break;
792 }
793 read_in_old = state->read_in_length;
794 }
795 }
796
797 if (gzip_flag) {
f91f0fd5
TL
798 if (!ret) {
799 if (gzip_flag == IGZIP_GZIP || gzip_flag == IGZIP_GZIP_NO_HDR
800 || gzip_flag == ISAL_GZIP_NO_HDR_VER) {
801 if (gzip_flag == ISAL_GZIP_NO_HDR_VER
802 || gzip_flag == IGZIP_GZIP)
803 compress_len -= gzip_trl_bytes;
804 ret =
805 check_gzip_trl(load_u64(compress_buf + compress_len),
806 state->crc, uncompress_buf,
807 *uncompress_len);
808 } else if (gzip_flag == IGZIP_ZLIB_NO_HDR) {
809 if (gzip_flag == IGZIP_ZLIB
810 || gzip_flag == ISAL_ZLIB_NO_HDR_VER)
811 compress_len -= zlib_trl_bytes;
812 ret =
813 check_zlib_trl(load_u32(compress_buf + compress_len),
814 state->crc, uncompress_buf,
815 *uncompress_len);
816 }
817 }
224ce89b
WB
818 }
819 if (ret == 0 && state->avail_in != 0)
820 ret = INFLATE_LEFTOVER_INPUT;
821
822 if (comp_tmp != NULL) {
823 free(comp_tmp);
824 comp_tmp = NULL;
825 }
826
827 if (uncomp_tmp != NULL) {
828 free(uncomp_tmp);
829 uncomp_tmp = NULL;
830 }
831
832 free(state);
833 return ret;
834}
7c673cae 835
f91f0fd5
TL
836int inflate_ret_to_code(int ret)
837{
838 switch (ret) {
839 case ISAL_DECOMP_OK:
840 return 0;
841 case ISAL_END_INPUT:
842 return INFLATE_END_OF_INPUT;
843 case ISAL_OUT_OVERFLOW:
844 return INFLATE_OUT_BUFFER_OVERFLOW;
845 case ISAL_INVALID_BLOCK:
846 return INFLATE_INVALID_BLOCK_HEADER;
847 case ISAL_INVALID_SYMBOL:
848 return INFLATE_INVALID_SYMBOL;
849 case ISAL_INVALID_LOOKBACK:
850 return INFLATE_INVALID_LOOK_BACK_DISTANCE;
851 default:
852 return INFLATE_GENERAL_ERROR;
853 }
854}
855
7c673cae 856/* Inflate the compressed data and check that the decompressed data agrees with the input data */
f91f0fd5
TL
857int inflate_check(uint8_t * z_buf, uint32_t z_size, uint8_t * in_buf, uint32_t in_size,
858 uint32_t gzip_flag, uint8_t * dict, uint32_t dict_len, uint32_t hist_bits)
7c673cae
FG
859{
860 /* Test inflate with reference inflate */
861
862 int ret = 0;
7c673cae
FG
863 uint32_t test_size = in_size;
864 uint8_t *test_buf = NULL;
865 int mem_result = 0;
224ce89b 866 int gzip_hdr_result = 0, gzip_trl_result = 0;
7c673cae
FG
867
868 if (in_size > 0) {
869 assert(in_buf != NULL);
870 test_buf = malloc(test_size);
7c673cae
FG
871 if (test_buf == NULL)
872 return MALLOC_FAILED;
873 }
224ce89b 874
7c673cae
FG
875 if (test_buf != NULL)
876 memset(test_buf, 0xff, test_size);
877
f91f0fd5 878 if (inflate_type == 0 && dict == NULL) {
224ce89b
WB
879 ret = inflate_stateless_pass(z_buf, z_size, test_buf, &test_size, gzip_flag);
880 inflate_type = 1;
881 } else {
f91f0fd5
TL
882 ret =
883 inflate_multi_pass(z_buf, z_size, test_buf, &test_size, gzip_flag, dict,
884 dict_len, hist_bits);
224ce89b
WB
885 inflate_type = 0;
886 }
7c673cae
FG
887
888 if (test_buf != NULL)
889 mem_result = memcmp(in_buf, test_buf, in_size);
890
f91f0fd5
TL
891 if (options.verbose && mem_result) {
892 int i;
7c673cae
FG
893 for (i = 0; i < in_size; i++) {
894 if (in_buf[i] != test_buf[i]) {
f91f0fd5
TL
895 log_print
896 ("First incorrect data at 0x%x of 0x%x, 0x%x != 0x%x\n", i,
897 in_size, in_buf[i], test_buf[i]);
7c673cae
FG
898 break;
899 }
900 }
f91f0fd5 901 }
7c673cae 902
7c673cae
FG
903 if (test_buf != NULL)
904 free(test_buf);
7c673cae
FG
905 switch (ret) {
906 case 0:
907 break;
224ce89b 908 case ISAL_END_INPUT:
7c673cae
FG
909 return INFLATE_END_OF_INPUT;
910 break;
224ce89b 911 case ISAL_INVALID_BLOCK:
7c673cae
FG
912 return INFLATE_INVALID_BLOCK_HEADER;
913 break;
224ce89b 914 case ISAL_INVALID_SYMBOL:
7c673cae
FG
915 return INFLATE_INVALID_SYMBOL;
916 break;
224ce89b 917 case ISAL_OUT_OVERFLOW:
7c673cae
FG
918 return INFLATE_OUT_BUFFER_OVERFLOW;
919 break;
224ce89b 920 case ISAL_INVALID_LOOKBACK:
7c673cae
FG
921 return INFLATE_INVALID_LOOK_BACK_DISTANCE;
922 break;
224ce89b
WB
923 case INFLATE_LEFTOVER_INPUT:
924 return INFLATE_LEFTOVER_INPUT;
925 break;
926 case INCORRECT_GZIP_TRAILER:
927 gzip_trl_result = INCORRECT_GZIP_TRAILER;
928 break;
f91f0fd5
TL
929 case INCORRECT_ZLIB_TRAILER:
930 gzip_trl_result = INCORRECT_ZLIB_TRAILER;
931 break;
932 case ISAL_INCORRECT_CHECKSUM:
933 if (gzip_flag == IGZIP_GZIP || gzip_flag == IGZIP_GZIP_NO_HDR
934 || gzip_flag == ISAL_GZIP_NO_HDR_VER)
935 gzip_trl_result = INCORRECT_GZIP_TRAILER;
936 else if (gzip_flag == IGZIP_ZLIB || gzip_flag == IGZIP_ZLIB_NO_HDR
937 || gzip_flag == ISAL_ZLIB_NO_HDR_VER)
938 gzip_trl_result = INCORRECT_GZIP_TRAILER;
939 break;
940 case ISAL_UNSUPPORTED_METHOD:
941 return UNSUPPORTED_METHOD;
942 case INFLATE_INPUT_STREAM_INTEGRITY_ERROR:
943 return INFLATE_INPUT_STREAM_INTEGRITY_ERROR;
944 break;
945 case INFLATE_OUTPUT_STREAM_INTEGRITY_ERROR:
946 return INFLATE_OUTPUT_STREAM_INTEGRITY_ERROR;
947 break;
7c673cae
FG
948 default:
949 return INFLATE_GENERAL_ERROR;
950 break;
951 }
952
224ce89b 953 if (test_size != in_size)
7c673cae
FG
954 return INFLATE_INCORRECT_OUTPUT_SIZE;
955
956 if (mem_result)
957 return RESULT_ERROR;
958
f91f0fd5
TL
959 if (gzip_hdr_result == INVALID_GZIP_HEADER)
960 return INVALID_GZIP_HEADER;
7c673cae 961
f91f0fd5
TL
962 else if (gzip_hdr_result == INVALID_ZLIB_HEADER)
963 return INVALID_ZLIB_HEADER;
964
965 if (gzip_trl_result == INCORRECT_GZIP_TRAILER)
966 return INCORRECT_GZIP_TRAILER;
967
968 else if (gzip_trl_result == INCORRECT_ZLIB_TRAILER)
969 return INCORRECT_ZLIB_TRAILER;
7c673cae
FG
970
971 return 0;
972}
973
974/* Check if that the state of the data stream is consistent */
975int stream_valid_check(struct isal_zstream *stream, uint8_t * in_buf, uint32_t in_size,
976 uint8_t * out_buf, uint32_t out_size, uint32_t in_processed,
977 uint32_t out_processed, uint32_t data_size)
978{
979 uint32_t total_in, in_buffer_size, total_out, out_buffer_size;
980
981 total_in =
982 (in_size ==
983 0) ? in_processed : (in_processed - in_size) + (stream->next_in - in_buf);
984 in_buffer_size = (in_size == 0) ? 0 : stream->next_in - in_buf + stream->avail_in;
985
986 /* Check for a consistent amount of data processed */
987 if (total_in != stream->total_in || in_buffer_size != in_size)
988 return COMPRESS_INPUT_STREAM_INTEGRITY_ERROR;
989
990 total_out =
991 (out_size == 0) ? out_processed : out_processed + (stream->next_out - out_buf);
992 out_buffer_size = (out_size == 0) ? 0 : stream->next_out - out_buf + stream->avail_out;
993
994 /* Check for a consistent amount of data compressed */
995 if (total_out != stream->total_out || out_buffer_size != out_size) {
996 return COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR;
997 }
998
999 return 0;
1000}
1001
1002/* Performs compression with checks to discover and verify the state of the
1003 * stream
1004 * stream: compress data structure which has been initialized to use
1005 * in_buf and out_buf as the buffers
1006 * data_size: size of all input data
1007 * compressed_size: size of all available output buffers
1008 * in_buf: next buffer of data to be compressed
1009 * in_size: size of in_buf
1010 * out_buf: next out put buffer where data is stored
1011 * out_size: size of out_buf
1012 * in_processed: the amount of input data which has been loaded into buffers
1013 * to be compressed, this includes the data in in_buf
1014 * out_processed: the amount of output data which has been compressed and stored,
1015 * this does not include the data in the current out_buf
1016*/
1017int isal_deflate_with_checks(struct isal_zstream *stream, uint32_t data_size,
1018 uint32_t compressed_size, uint8_t * in_buf, uint32_t in_size,
1019 uint32_t in_processed, uint8_t * out_buf, uint32_t out_size,
1020 uint32_t out_processed)
1021{
1022 int ret, stream_check;
1023 struct isal_zstate *state = &stream->internal_state;
1024
f91f0fd5
TL
1025 log_print("Pre compression\n");
1026 log_print
7c673cae
FG
1027 ("data_size = 0x%05x, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x, total_in = 0x%05x\n",
1028 data_size, in_processed, in_size, stream->avail_in, stream->total_in);
f91f0fd5 1029 log_print
7c673cae
FG
1030 ("compressed_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n",
1031 compressed_size, out_processed, out_size, stream->avail_out, stream->total_out);
7c673cae
FG
1032
1033 ret = isal_deflate(stream);
1034
f91f0fd5
TL
1035 log_print("Post compression\n");
1036 log_print
7c673cae
FG
1037 ("data_size = 0x%05x, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x, total_in = 0x%05x\n",
1038 data_size, in_processed, in_size, stream->avail_in, stream->total_in);
f91f0fd5 1039 log_print
7c673cae
FG
1040 ("compressed_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n",
1041 compressed_size, out_processed, out_size, stream->avail_out, stream->total_out);
f91f0fd5 1042 log_print("\n\n");
7c673cae
FG
1043
1044 /* Verify the stream is in a valid state */
1045 stream_check = stream_valid_check(stream, in_buf, in_size, out_buf, out_size,
1046 in_processed, out_processed, data_size);
1047
1048 if (stream_check != 0)
1049 return stream_check;
1050
1051 if (ret != IGZIP_COMP_OK)
1052 return COMPRESS_GENERAL_ERROR;
1053
1054 /* Check if the compression is completed */
1055 if (state->state != ZSTATE_END)
1056 if (compressed_size - out_processed - (out_size - stream->avail_out) <= 0)
1057 return COMPRESS_OUT_BUFFER_OVERFLOW;
1058
1059 return ret;
1060
1061}
1062
f91f0fd5
TL
1063void set_random_hufftable(struct isal_zstream *stream, int level, uint8_t * data,
1064 uint32_t data_size)
224ce89b 1065{
f91f0fd5
TL
1066 struct isal_hufftables *huff = hufftables;
1067 struct isal_huff_histogram hist;
1068 if (level == 0 || rand() % 16 == 0) {
1069 if (rand() % 8 == 0) {
1070 huff = hufftables_subset;
1071 memset(&hist, 0, sizeof(hist));
1072 isal_update_histogram(data, data_size, &hist);
1073 isal_create_hufftables_subset(huff, &hist);
1074 }
1075
1076 isal_deflate_set_hufftables(stream, huff, rand() % 4);
1077 }
224ce89b
WB
1078}
1079
7c673cae
FG
1080/* Compress the input data into the output buffer where the input buffer and
1081 * output buffer are randomly segmented to test state information for the
1082 * compression*/
1083int compress_multi_pass(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
224ce89b 1084 uint32_t * compressed_size, uint32_t flush_type, uint32_t gzip_flag,
f91f0fd5 1085 uint32_t level, uint8_t * dict, uint32_t dict_len, uint32_t hist_bits)
7c673cae
FG
1086{
1087 int ret = IGZIP_COMP_OK;
1088 uint8_t *in_buf = NULL, *out_buf = NULL;
1089 uint32_t in_size = 0, out_size = 0;
1090 uint32_t in_processed = 0, out_processed = 0;
f91f0fd5
TL
1091 struct isal_zstream *stream;
1092 struct isal_zstate *state;
7c673cae 1093 uint32_t loop_count = 0;
224ce89b
WB
1094 uint32_t level_buf_size;
1095 uint8_t *level_buf = NULL;
f91f0fd5
TL
1096 struct isal_hufftables *huff_tmp;
1097 uint32_t reset_test_flag = 0;
1098 uint8_t tmp_symbol;
1099 int no_mod = 0;
7c673cae 1100
f91f0fd5 1101 log_print("Starting Compress Multi Pass\n");
7c673cae 1102
f91f0fd5
TL
1103 stream = malloc(sizeof(*stream));
1104 if (stream == NULL)
1105 return MALLOC_FAILED;
1106 state = &stream->internal_state;
7c673cae 1107
f91f0fd5
TL
1108 create_rand_repeat_data((uint8_t *) stream, sizeof(*stream));
1109
1110 isal_deflate_init(stream);
7c673cae 1111
7c673cae
FG
1112 if (state->state != ZSTATE_NEW_HDR)
1113 return COMPRESS_INCORRECT_STATE;
1114
f91f0fd5
TL
1115 if (rand() % 4 == 0) {
1116 /* Test reset */
1117 reset_test_flag = 1;
1118 huff_tmp = stream->hufftables;
1119 create_rand_repeat_data((uint8_t *) stream, sizeof(*stream));
1120
1121 /* Restore variables not necessarily set by user */
1122 stream->hufftables = huff_tmp;
1123 stream->end_of_stream = 0;
1124 stream->level = 0;
1125 stream->level_buf = NULL;
1126 stream->level_buf_size = 0;
1127 }
1128
1129 stream->flush = flush_type;
1130 stream->end_of_stream = 0;
7c673cae
FG
1131
1132 /* These are set here to allow the loop to run correctly */
f91f0fd5
TL
1133 stream->avail_in = 0;
1134 stream->avail_out = 0;
1135 stream->gzip_flag = gzip_flag;
1136 stream->level = level;
1137 stream->hist_bits = hist_bits;
224ce89b
WB
1138
1139 if (level >= 1) {
f91f0fd5 1140 level_buf_size = get_rand_level_buf_size(stream->level);
224ce89b
WB
1141 level_buf = malloc(level_buf_size);
1142 create_rand_repeat_data(level_buf, level_buf_size);
f91f0fd5
TL
1143 stream->level_buf = level_buf;
1144 stream->level_buf_size = level_buf_size;
224ce89b 1145 }
7c673cae 1146
f91f0fd5
TL
1147 if (reset_test_flag)
1148 isal_deflate_reset(stream);
1149
1150 if (dict != NULL)
1151 isal_deflate_set_dict(stream, dict, dict_len);
1152
7c673cae
FG
1153 while (1) {
1154 loop_count++;
1155
1156 /* Setup in buffer for next round of compression */
f91f0fd5 1157 if (stream->avail_in == 0) {
7c673cae
FG
1158 if (flush_type == NO_FLUSH || state->state == ZSTATE_NEW_HDR) {
1159 /* Randomly choose size of the next out buffer */
1160 in_size = rand() % (data_size + 1);
1161
1162 /* Limit size of buffer to be smaller than maximum */
1163 if (in_size >= data_size - in_processed) {
1164 in_size = data_size - in_processed;
f91f0fd5 1165 stream->end_of_stream = 1;
7c673cae
FG
1166 }
1167
1168 if (in_size != 0) {
1169 if (in_buf != NULL) {
1170 free(in_buf);
1171 in_buf = NULL;
1172 }
1173
1174 in_buf = malloc(in_size);
1175 if (in_buf == NULL) {
1176 ret = MALLOC_FAILED;
1177 break;
1178 }
1179 memcpy(in_buf, data + in_processed, in_size);
1180 in_processed += in_size;
1181
f91f0fd5
TL
1182 stream->avail_in = in_size;
1183 stream->next_in = in_buf;
7c673cae
FG
1184 }
1185 }
f91f0fd5
TL
1186 } else {
1187 /* Randomly modify data after next in */
1188 if (rand() % 4 == 0 && !no_mod) {
1189
1190 tmp_symbol = rand();
1191 log_print
1192 ("Modifying data at index 0x%x from 0x%x to 0x%x before recalling isal_deflate\n",
1193 in_processed - stream->avail_in,
1194 data[in_processed - stream->avail_in], tmp_symbol);
1195 *stream->next_in = tmp_symbol;
1196 data[in_processed - stream->avail_in] = tmp_symbol;
1197 }
7c673cae
FG
1198 }
1199
1200 /* Setup out buffer for next round of compression */
f91f0fd5 1201 if (stream->avail_out == 0) {
7c673cae
FG
1202 /* Save compressed data inot compressed_buf */
1203 if (out_buf != NULL) {
1204 memcpy(compressed_buf + out_processed, out_buf,
f91f0fd5
TL
1205 out_size - stream->avail_out);
1206 out_processed += out_size - stream->avail_out;
7c673cae
FG
1207 }
1208
1209 /* Randomly choose size of the next out buffer */
1210 out_size = rand() % (*compressed_size + 1);
1211
1212 /* Limit size of buffer to be smaller than maximum */
1213 if (out_size > *compressed_size - out_processed)
1214 out_size = *compressed_size - out_processed;
1215
1216 if (out_size != 0) {
1217 if (out_buf != NULL) {
1218 free(out_buf);
1219 out_buf = NULL;
1220 }
1221
1222 out_buf = malloc(out_size);
1223 if (out_buf == NULL) {
1224 ret = MALLOC_FAILED;
1225 break;
1226 }
1227
f91f0fd5
TL
1228 stream->avail_out = out_size;
1229 stream->next_out = out_buf;
7c673cae
FG
1230 }
1231 }
1232
f91f0fd5
TL
1233 if (state->state == ZSTATE_NEW_HDR) {
1234 set_random_hufftable(stream, level, data, data_size);
1235 if (stream->hufftables == hufftables_subset)
1236 no_mod = 1;
1237 else
1238 no_mod = 0;
1239 }
224ce89b 1240
7c673cae 1241 ret =
f91f0fd5 1242 isal_deflate_with_checks(stream, data_size, *compressed_size, in_buf,
7c673cae
FG
1243 in_size, in_processed, out_buf, out_size,
1244 out_processed);
1245
1246 if (ret) {
1247 if (ret == COMPRESS_OUT_BUFFER_OVERFLOW
1248 || ret == COMPRESS_INCORRECT_STATE)
1249 memcpy(compressed_buf + out_processed, out_buf, out_size);
1250 break;
1251 }
1252
1253 /* Check if the compression is completed */
1254 if (state->state == ZSTATE_END) {
1255 memcpy(compressed_buf + out_processed, out_buf, out_size);
f91f0fd5 1256 *compressed_size = stream->total_out;
7c673cae
FG
1257 break;
1258 }
1259
1260 }
1261
f91f0fd5
TL
1262 if (stream != NULL)
1263 free(stream);
224ce89b
WB
1264 if (level_buf != NULL)
1265 free(level_buf);
7c673cae
FG
1266 if (in_buf != NULL)
1267 free(in_buf);
1268 if (out_buf != NULL)
1269 free(out_buf);
1270
1271 if (ret == COMPRESS_OUT_BUFFER_OVERFLOW && flush_type == SYNC_FLUSH
1272 && loop_count >= MAX_LOOPS)
1273 ret = COMPRESS_LOOP_COUNT_OVERFLOW;
1274
1275 return ret;
1276
1277}
1278
1279/* Compress the input data into the outbuffer in one call to isal_deflate */
1280int compress_single_pass(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
224ce89b 1281 uint32_t * compressed_size, uint32_t flush_type, uint32_t gzip_flag,
f91f0fd5 1282 uint32_t level, uint8_t * dict, uint32_t dict_len, uint32_t hist_bits)
7c673cae
FG
1283{
1284 int ret = IGZIP_COMP_OK;
1285 struct isal_zstream stream;
1286 struct isal_zstate *state = &stream.internal_state;
224ce89b
WB
1287 uint32_t level_buf_size;
1288 uint8_t *level_buf = NULL;
f91f0fd5
TL
1289 struct isal_hufftables *huff_tmp;
1290 uint32_t reset_test_flag = 0;
7c673cae 1291
f91f0fd5 1292 log_print("Starting Compress Single Pass\n");
7c673cae
FG
1293
1294 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1295
1296 isal_deflate_init(&stream);
1297
f91f0fd5 1298 set_random_hufftable(&stream, level, data, data_size);
7c673cae
FG
1299
1300 if (state->state != ZSTATE_NEW_HDR)
1301 return COMPRESS_INCORRECT_STATE;
1302
f91f0fd5
TL
1303 if (rand() % 4 == 0) {
1304 /* Test reset */
1305 reset_test_flag = 1;
1306 huff_tmp = stream.hufftables;
1307 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1308
1309 /* Restore variables not necessarily set by user */
1310 stream.hufftables = huff_tmp;
1311 stream.end_of_stream = 0;
1312 stream.level = 0;
1313 stream.level_buf = NULL;
1314 stream.level_buf_size = 0;
1315 }
1316
7c673cae
FG
1317 stream.flush = flush_type;
1318 stream.avail_in = data_size;
1319 stream.next_in = data;
1320 stream.avail_out = *compressed_size;
1321 stream.next_out = compressed_buf;
1322 stream.end_of_stream = 1;
224ce89b
WB
1323 stream.gzip_flag = gzip_flag;
1324 stream.level = level;
f91f0fd5 1325 stream.hist_bits = hist_bits;
224ce89b
WB
1326
1327 if (level >= 1) {
f91f0fd5 1328 level_buf_size = get_rand_level_buf_size(stream.level);
224ce89b
WB
1329 level_buf = malloc(level_buf_size);
1330 create_rand_repeat_data(level_buf, level_buf_size);
1331 stream.level_buf = level_buf;
1332 stream.level_buf_size = level_buf_size;
1333 }
7c673cae 1334
f91f0fd5
TL
1335 if (reset_test_flag)
1336 isal_deflate_reset(&stream);
1337
1338 if (dict != NULL)
1339 isal_deflate_set_dict(&stream, dict, dict_len);
1340
7c673cae
FG
1341 ret =
1342 isal_deflate_with_checks(&stream, data_size, *compressed_size, data, data_size,
1343 data_size, compressed_buf, *compressed_size, 0);
1344
224ce89b
WB
1345 if (level_buf != NULL)
1346 free(level_buf);
1347
7c673cae
FG
1348 /* Check if the compression is completed */
1349 if (state->state == ZSTATE_END)
1350 *compressed_size = stream.total_out;
1351 else if (flush_type == SYNC_FLUSH && stream.avail_out < 16)
1352 ret = COMPRESS_OUT_BUFFER_OVERFLOW;
1353
1354 return ret;
1355
1356}
1357
f91f0fd5
TL
1358/* Compress the input data repeatedly into the outbuffer
1359 * Compresses and verifies in place to decrease memory usage
1360 */
1361int compress_ver_rep_buf(uint8_t * data, uint32_t data_size, uint64_t data_rep_size,
1362 uint8_t * compressed_buf, uint32_t compressed_size,
1363 uint8_t * decomp_buf, uint32_t decomp_buf_size, uint32_t flush_type,
1364 uint32_t gzip_flag, uint32_t level)
1365{
1366 int ret = IGZIP_COMP_OK;
1367 struct isal_zstream stream;
1368 struct inflate_state state;
1369 uint32_t level_buf_size;
1370 uint8_t *level_buf = NULL;
1371 uint64_t data_remaining = data_rep_size;
1372 uint64_t data_verified = 0;
1373 uint32_t index;
1374 uint32_t out_size, cmp_size;
1375 uint32_t avail_out_start;
1376
1377 log_print("Starting Compress and Verify Repeated Buffer\n");
1378
1379 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1380
1381 /* Setup compression stream */
1382 isal_deflate_init(&stream);
1383 stream.avail_in = 0;
1384 stream.next_in = NULL;
1385 stream.avail_out = 0;
1386 stream.next_out = NULL;
1387
1388 set_random_hufftable(&stream, level, data, data_size);
1389 stream.flush = flush_type;
1390 stream.end_of_stream = 0;
1391 stream.gzip_flag = gzip_flag;
1392 stream.level = level;
1393
1394 if (level >= 1) {
1395 level_buf_size = get_rand_level_buf_size(stream.level);
1396 level_buf = malloc(level_buf_size);
1397 create_rand_repeat_data(level_buf, level_buf_size);
1398 stream.level_buf = level_buf;
1399 stream.level_buf_size = level_buf_size;
1400 }
1401
1402 /* Setup decompression stream */
1403 create_rand_repeat_data((uint8_t *) & state, sizeof(state));
1404 isal_inflate_init(&state);
1405 state.crc_flag = gzip_flag;
1406
1407 while (data_remaining || stream.avail_in) {
1408 /* Compress the input buffer */
1409 if (stream.next_out == NULL) {
1410 stream.avail_out = compressed_size;
1411 stream.next_out = compressed_buf;
1412 }
1413
1414 while (stream.avail_out > 0 && (data_remaining || stream.avail_in)) {
1415 if (stream.avail_in == 0) {
1416 stream.avail_in = data_size;
1417 if (data_size >= data_remaining) {
1418 stream.avail_in = data_remaining;
1419 stream.end_of_stream = 1;
1420 }
1421
1422 stream.next_in = data;
1423 data_remaining -= stream.avail_in;
1424 }
1425
1426 ret = isal_deflate(&stream);
1427
1428 if (ret)
1429 return COMPRESS_GENERAL_ERROR;
1430 }
1431
1432 /* Verfiy the compressed buffer */
1433 state.next_in = compressed_buf;
1434 state.avail_in = compressed_size;
1435 state.next_out = NULL;
1436 state.avail_out = 0;
1437 create_rand_repeat_data(decomp_buf, decomp_buf_size);
1438
1439 while (state.avail_out == 0) {
1440 state.next_out = decomp_buf;
1441 state.avail_out = decomp_buf_size;
1442
1443 /* Force decoding to stop when avail_out rolls over */
1444 if ((1ULL << 32) - state.total_out < decomp_buf_size)
1445 state.avail_out = (1ULL << 32) - state.total_out;
1446
1447 avail_out_start = state.avail_out;
1448
1449 ret = isal_inflate(&state);
1450 if (ret)
1451 return inflate_ret_to_code(ret);
1452
1453 /* Check data accuracy */
1454 index = data_verified % data_size;
1455 out_size = avail_out_start - state.avail_out;
1456 cmp_size =
1457 (out_size > data_size - index) ? data_size - index : out_size;
1458 ret |= memcmp(decomp_buf, data + index, cmp_size);
1459 out_size -= cmp_size;
1460 cmp_size = (out_size > index) ? index : out_size;
1461 ret |= memcmp(decomp_buf + data_size - index, data, cmp_size);
1462 out_size -= cmp_size;
1463 cmp_size = out_size;
1464 ret |= memcmp(decomp_buf, decomp_buf + data_size, out_size);
1465 if (ret)
1466 return RESULT_ERROR;
1467
1468 data_verified += avail_out_start - state.avail_out;
1469 }
1470 stream.next_out = NULL;
1471 }
1472
1473 if (level_buf != NULL)
1474 free(level_buf);
1475
1476 return ret;
1477
1478}
1479
7c673cae
FG
1480/* Statelessly compress the input buffer into the output buffer */
1481int compress_stateless(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
224ce89b 1482 uint32_t * compressed_size, uint32_t flush_type, uint32_t gzip_flag,
f91f0fd5 1483 uint32_t level, uint32_t hist_bits)
7c673cae
FG
1484{
1485 int ret = IGZIP_COMP_OK;
1486 struct isal_zstream stream;
224ce89b
WB
1487 uint32_t level_buf_size;
1488 uint8_t *level_buf = NULL;
f91f0fd5
TL
1489 struct isal_hufftables *huff_tmp;
1490 uint32_t reset_test_flag = 0;
7c673cae
FG
1491
1492 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1493
224ce89b 1494 isal_deflate_stateless_init(&stream);
7c673cae 1495
f91f0fd5
TL
1496 set_random_hufftable(&stream, level, data, data_size);
1497
1498 if (rand() % 4 == 0) {
1499 /* Test reset */
1500 reset_test_flag = 1;
1501 huff_tmp = stream.hufftables;
1502 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1503
1504 /* Restore variables not necessarily set by user */
1505 stream.hufftables = huff_tmp;
1506 stream.end_of_stream = 0;
1507 stream.level = 0;
1508 stream.level_buf = NULL;
1509 stream.level_buf_size = 0;
1510 }
7c673cae
FG
1511
1512 stream.avail_in = data_size;
7c673cae 1513 stream.next_in = data;
224ce89b
WB
1514 stream.flush = flush_type;
1515 if (flush_type != NO_FLUSH)
1516 stream.end_of_stream = 1;
7c673cae
FG
1517 stream.avail_out = *compressed_size;
1518 stream.next_out = compressed_buf;
224ce89b
WB
1519 stream.gzip_flag = gzip_flag;
1520 stream.level = level;
f91f0fd5 1521 stream.hist_bits = hist_bits;
224ce89b 1522
f91f0fd5
TL
1523 if (level == 1) {
1524 /* This is to test case where level buf uses already existing
1525 * internal buffers */
224ce89b 1526 level_buf_size = rand() % IBUF_SIZE;
f91f0fd5 1527
224ce89b
WB
1528 if (level_buf_size >= ISAL_DEF_LVL1_MIN) {
1529 level_buf = malloc(level_buf_size);
1530 create_rand_repeat_data(level_buf, level_buf_size);
1531 stream.level_buf = level_buf;
1532 stream.level_buf_size = level_buf_size;
1533 }
f91f0fd5
TL
1534 } else if (level > 1) {
1535 level_buf_size = get_rand_level_buf_size(level);
1536 level_buf = malloc(level_buf_size);
1537 create_rand_repeat_data(level_buf, level_buf_size);
1538 stream.level_buf = level_buf;
1539 stream.level_buf_size = level_buf_size;
224ce89b 1540 }
7c673cae 1541
f91f0fd5
TL
1542 if (reset_test_flag)
1543 isal_deflate_reset(&stream);
1544
7c673cae
FG
1545 ret = isal_deflate_stateless(&stream);
1546
224ce89b
WB
1547 if (level_buf != NULL)
1548 free(level_buf);
1549
7c673cae
FG
1550 /* verify the stream */
1551 if (stream.next_in - data != stream.total_in ||
1552 stream.total_in + stream.avail_in != data_size)
1553 return COMPRESS_INPUT_STREAM_INTEGRITY_ERROR;
1554
1555 if (stream.next_out - compressed_buf != stream.total_out ||
224ce89b 1556 stream.total_out + stream.avail_out != *compressed_size) {
7c673cae 1557 return COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR;
224ce89b 1558 }
7c673cae
FG
1559
1560 if (ret != IGZIP_COMP_OK) {
1561 if (ret == STATELESS_OVERFLOW)
1562 return COMPRESS_OUT_BUFFER_OVERFLOW;
224ce89b
WB
1563 else if (ret == INVALID_FLUSH)
1564 return INVALID_FLUSH_ERROR;
f91f0fd5
TL
1565 else {
1566 printf("Return due to ret = %d with level = %d or %d\n", ret, level,
1567 stream.level);
7c673cae 1568 return COMPRESS_GENERAL_ERROR;
f91f0fd5 1569 }
7c673cae
FG
1570 }
1571
1572 if (!stream.end_of_stream) {
1573 return COMPRESS_END_OF_STREAM_NOT_SET;
1574 }
1575
1576 if (stream.avail_in != 0)
1577 return COMPRESS_ALL_INPUT_FAIL;
1578
1579 *compressed_size = stream.total_out;
1580
1581 return ret;
1582
1583}
1584
224ce89b
WB
1585/* Statelessly compress the input buffer into the output buffer */
1586int compress_stateless_full_flush(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
f91f0fd5
TL
1587 uint32_t * compressed_size, uint32_t level,
1588 uint32_t hist_bits)
224ce89b
WB
1589{
1590 int ret = IGZIP_COMP_OK;
1591 uint8_t *in_buf = NULL, *level_buf = NULL, *out_buf = compressed_buf;
1592 uint32_t in_size = 0, level_buf_size;
1593 uint32_t in_processed = 00;
1594 struct isal_zstream stream;
1595 uint32_t loop_count = 0;
f91f0fd5
TL
1596 struct isal_hufftables *huff_tmp;
1597 uint32_t reset_test_flag = 0;
224ce89b 1598
f91f0fd5 1599 log_print("Starting Stateless Compress Full Flush\n");
224ce89b
WB
1600
1601 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1602
1603 isal_deflate_stateless_init(&stream);
1604
f91f0fd5
TL
1605 if (rand() % 4 == 0) {
1606 /* Test reset */
1607 reset_test_flag = 1;
1608 huff_tmp = stream.hufftables;
1609 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1610
1611 /* Restore variables not necessarily set by user */
1612 stream.hufftables = huff_tmp;
1613 stream.end_of_stream = 0;
1614 stream.level = 0;
1615 stream.level_buf = NULL;
1616 stream.level_buf_size = 0;
1617 stream.gzip_flag = 0;
1618 }
1619
224ce89b
WB
1620 stream.flush = FULL_FLUSH;
1621 stream.end_of_stream = 0;
1622 stream.avail_out = *compressed_size;
1623 stream.next_out = compressed_buf;
1624 stream.level = level;
f91f0fd5 1625 stream.hist_bits = hist_bits;
224ce89b 1626
f91f0fd5
TL
1627 if (level == 1) {
1628 /* This is to test case where level_buf uses already existing
1629 * internal buffers */
224ce89b 1630 level_buf_size = rand() % IBUF_SIZE;
f91f0fd5 1631
224ce89b
WB
1632 if (level_buf_size >= ISAL_DEF_LVL1_MIN) {
1633 level_buf = malloc(level_buf_size);
1634 create_rand_repeat_data(level_buf, level_buf_size);
1635 stream.level_buf = level_buf;
1636 stream.level_buf_size = level_buf_size;
1637 }
f91f0fd5
TL
1638 } else if (level > 1) {
1639 level_buf_size = get_rand_level_buf_size(level);
1640 level_buf = malloc(level_buf_size);
1641 create_rand_repeat_data(level_buf, level_buf_size);
1642 stream.level_buf = level_buf;
1643 stream.level_buf_size = level_buf_size;
224ce89b
WB
1644 }
1645
f91f0fd5
TL
1646 if (reset_test_flag)
1647 isal_deflate_reset(&stream);
1648
224ce89b
WB
1649 while (1) {
1650 loop_count++;
1651
1652 /* Randomly choose size of the next out buffer */
1653 in_size = rand() % (data_size + 1);
1654
1655 /* Limit size of buffer to be smaller than maximum */
1656 if (in_size >= data_size - in_processed) {
1657 in_size = data_size - in_processed;
1658 stream.end_of_stream = 1;
1659 }
1660
1661 stream.avail_in = in_size;
1662
1663 if (in_size != 0) {
1664 if (in_buf != NULL) {
1665 free(in_buf);
1666 in_buf = NULL;
1667 }
1668
1669 in_buf = malloc(in_size);
1670 if (in_buf == NULL) {
1671 ret = MALLOC_FAILED;
1672 break;
1673 }
1674 memcpy(in_buf, data + in_processed, in_size);
1675 in_processed += in_size;
1676
1677 stream.next_in = in_buf;
1678 }
1679
1680 out_buf = stream.next_out;
1681
1682 if (stream.internal_state.state == ZSTATE_NEW_HDR)
f91f0fd5 1683 set_random_hufftable(&stream, level, data, data_size);
224ce89b
WB
1684
1685 ret = isal_deflate_stateless(&stream);
f91f0fd5 1686
224ce89b
WB
1687 assert(stream.internal_state.bitbuf.m_bit_count == 0);
1688
1689 assert(compressed_buf == stream.next_out - stream.total_out);
1690 if (ret)
1691 break;
1692
1693 /* Verify that blocks are independent */
f91f0fd5
TL
1694 ret =
1695 inflate_check(out_buf, stream.next_out - out_buf, in_buf, in_size, 0, NULL,
1696 0, hist_bits);
224ce89b
WB
1697
1698 if (ret == INFLATE_INVALID_LOOK_BACK_DISTANCE) {
1699 break;
1700 } else
1701 ret = 0;
1702
1703 /* Check if the compression is completed */
1704 if (in_processed == data_size) {
1705 *compressed_size = stream.total_out;
1706 break;
1707 }
1708
1709 }
1710
1711 if (level_buf != NULL)
1712 free(level_buf);
1713
1714 if (in_buf != NULL)
1715 free(in_buf);
1716
1717 if (ret == STATELESS_OVERFLOW && loop_count >= MAX_LOOPS)
1718 ret = COMPRESS_LOOP_COUNT_OVERFLOW;
1719
1720 return ret;
1721
1722}
1723
7c673cae
FG
1724/* Compress the input data into the output buffer where the input buffer and
1725 * is randomly segmented to test for independence of blocks in full flush
1726 * compression*/
1727int compress_full_flush(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
224ce89b 1728 uint32_t * compressed_size, uint32_t gzip_flag, uint32_t level)
7c673cae
FG
1729{
1730 int ret = IGZIP_COMP_OK;
224ce89b
WB
1731 uint8_t *in_buf = NULL, *out_buf = compressed_buf, *level_buf = NULL;
1732 uint32_t in_size = 0, level_buf_size;
7c673cae
FG
1733 uint32_t in_processed = 00;
1734 struct isal_zstream stream;
1735 struct isal_zstate *state = &stream.internal_state;
1736 uint32_t loop_count = 0;
f91f0fd5
TL
1737 struct isal_hufftables *huff_tmp;
1738 uint32_t reset_test_flag = 0;
7c673cae 1739
f91f0fd5 1740 log_print("Starting Compress Full Flush\n");
7c673cae
FG
1741
1742 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1743
1744 isal_deflate_init(&stream);
1745
7c673cae
FG
1746 if (state->state != ZSTATE_NEW_HDR)
1747 return COMPRESS_INCORRECT_STATE;
1748
f91f0fd5
TL
1749 if (rand() % 4 == 0) {
1750 /* Test reset */
1751 reset_test_flag = 1;
1752 huff_tmp = stream.hufftables;
1753 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1754
1755 /* Restore variables not necessarily set by user */
1756 stream.hufftables = huff_tmp;
1757 stream.end_of_stream = 0;
1758 stream.level = 0;
1759 stream.level_buf = NULL;
1760 stream.level_buf_size = 0;
1761 stream.hist_bits = 0;
1762 }
1763
7c673cae
FG
1764 stream.flush = FULL_FLUSH;
1765 stream.end_of_stream = 0;
1766 stream.avail_out = *compressed_size;
1767 stream.next_out = compressed_buf;
1768 stream.total_out = 0;
224ce89b
WB
1769 stream.gzip_flag = gzip_flag;
1770 stream.level = level;
1771
1772 if (level >= 1) {
f91f0fd5 1773 level_buf_size = get_rand_level_buf_size(stream.level);
224ce89b
WB
1774 if (level_buf_size >= ISAL_DEF_LVL1_MIN) {
1775 level_buf = malloc(level_buf_size);
1776 create_rand_repeat_data(level_buf, level_buf_size);
1777 stream.level_buf = level_buf;
1778 stream.level_buf_size = level_buf_size;
1779 }
1780 }
7c673cae 1781
f91f0fd5
TL
1782 if (reset_test_flag)
1783 isal_deflate_reset(&stream);
1784
7c673cae
FG
1785 while (1) {
1786 loop_count++;
1787
1788 /* Setup in buffer for next round of compression */
1789 if (state->state == ZSTATE_NEW_HDR) {
1790 /* Randomly choose size of the next out buffer */
1791 in_size = rand() % (data_size + 1);
1792
1793 /* Limit size of buffer to be smaller than maximum */
1794 if (in_size >= data_size - in_processed) {
1795 in_size = data_size - in_processed;
1796 stream.end_of_stream = 1;
1797 }
1798
1799 stream.avail_in = in_size;
1800
1801 if (in_size != 0) {
1802 if (in_buf != NULL) {
1803 free(in_buf);
1804 in_buf = NULL;
1805 }
1806
1807 in_buf = malloc(in_size);
1808 if (in_buf == NULL) {
1809 ret = MALLOC_FAILED;
1810 break;
1811 }
1812 memcpy(in_buf, data + in_processed, in_size);
1813 in_processed += in_size;
1814
1815 stream.next_in = in_buf;
1816 }
1817
1818 out_buf = stream.next_out;
1819 }
1820
224ce89b 1821 if (state->state == ZSTATE_NEW_HDR)
f91f0fd5 1822 set_random_hufftable(&stream, level, data, data_size);
224ce89b 1823
7c673cae
FG
1824 ret = isal_deflate(&stream);
1825
1826 if (ret)
1827 break;
1828
1829 /* Verify that blocks are independent */
1830 if (state->state == ZSTATE_NEW_HDR || state->state == ZSTATE_END) {
1831 ret =
224ce89b 1832 inflate_check(out_buf, stream.next_out - out_buf, in_buf, in_size,
f91f0fd5 1833 0, NULL, 0, 0);
7c673cae
FG
1834
1835 if (ret == INFLATE_INVALID_LOOK_BACK_DISTANCE)
1836 break;
1837 else
1838 ret = 0;
1839 }
1840
1841 /* Check if the compression is completed */
1842 if (state->state == ZSTATE_END) {
1843 *compressed_size = stream.total_out;
1844 break;
1845 }
1846
1847 }
1848
224ce89b
WB
1849 if (level_buf != NULL)
1850 free(level_buf);
1851
7c673cae
FG
1852 if (in_buf != NULL)
1853 free(in_buf);
1854
1855 if (ret == COMPRESS_OUT_BUFFER_OVERFLOW && loop_count >= MAX_LOOPS)
1856 ret = COMPRESS_LOOP_COUNT_OVERFLOW;
1857
1858 return ret;
1859
1860}
1861
1862/*Compress the input buffer into the output buffer, but switch the flush type in
1863 * the middle of the compression to test what happens*/
1864int compress_swap_flush(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
f91f0fd5
TL
1865 uint32_t * compressed_size, uint32_t flush_type, int level,
1866 uint32_t gzip_flag)
7c673cae
FG
1867{
1868 int ret = IGZIP_COMP_OK;
1869 struct isal_zstream stream;
1870 struct isal_zstate *state = &stream.internal_state;
1871 uint32_t partial_size;
f91f0fd5
TL
1872 struct isal_hufftables *huff_tmp;
1873 uint32_t reset_test_flag = 0;
1874 uint32_t level_buf_size;
1875 uint8_t *level_buf = NULL;
7c673cae 1876
f91f0fd5 1877 log_print("Starting Compress Swap Flush\n");
7c673cae
FG
1878
1879 isal_deflate_init(&stream);
1880
f91f0fd5 1881 set_random_hufftable(&stream, 0, data, data_size);
7c673cae
FG
1882
1883 if (state->state != ZSTATE_NEW_HDR)
1884 return COMPRESS_INCORRECT_STATE;
1885
f91f0fd5
TL
1886 if (rand() % 4 == 0) {
1887 /* Test reset */
1888 reset_test_flag = 1;
1889 huff_tmp = stream.hufftables;
1890 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1891
1892 /* Restore variables not necessarily set by user */
1893 stream.hufftables = huff_tmp;
1894 stream.end_of_stream = 0;
1895 stream.level = 0;
1896 stream.level_buf = NULL;
1897 stream.level_buf_size = 0;
1898 }
1899
7c673cae
FG
1900 partial_size = rand() % (data_size + 1);
1901
1902 stream.flush = flush_type;
1903 stream.avail_in = partial_size;
1904 stream.next_in = data;
1905 stream.avail_out = *compressed_size;
1906 stream.next_out = compressed_buf;
1907 stream.end_of_stream = 0;
224ce89b 1908 stream.gzip_flag = gzip_flag;
f91f0fd5
TL
1909 if (level) {
1910 stream.level = level;
1911 level_buf_size = get_rand_level_buf_size(stream.level);
1912 level_buf = malloc(level_buf_size);
1913 create_rand_repeat_data(level_buf, level_buf_size);
1914 stream.level_buf = level_buf;
1915 stream.level_buf_size = level_buf_size;
1916 }
1917
1918 if (reset_test_flag)
1919 isal_deflate_reset(&stream);
7c673cae
FG
1920
1921 ret =
1922 isal_deflate_with_checks(&stream, data_size, *compressed_size, data, partial_size,
1923 partial_size, compressed_buf, *compressed_size, 0);
1924
1925 if (ret)
1926 return ret;
1927
224ce89b 1928 if (state->state == ZSTATE_NEW_HDR)
f91f0fd5 1929 set_random_hufftable(&stream, 0, data, data_size);
224ce89b 1930
7c673cae
FG
1931 flush_type = rand() % 3;
1932
1933 stream.flush = flush_type;
1934 stream.avail_in = data_size - partial_size;
1935 stream.next_in = data + partial_size;
1936 stream.end_of_stream = 1;
1937
1938 ret =
1939 isal_deflate_with_checks(&stream, data_size, *compressed_size, data + partial_size,
1940 data_size - partial_size, data_size, compressed_buf,
1941 *compressed_size, 0);
1942
1943 if (ret == COMPRESS_GENERAL_ERROR)
1944 return INVALID_FLUSH_ERROR;
1945
1946 *compressed_size = stream.total_out;
1947
f91f0fd5
TL
1948 if (stream.level_buf != NULL)
1949 free(stream.level_buf);
1950
7c673cae
FG
1951 return ret;
1952}
1953
1954/* Test deflate_stateless */
224ce89b 1955int test_compress_stateless(uint8_t * in_data, uint32_t in_size, uint32_t flush_type)
7c673cae
FG
1956{
1957 int ret = IGZIP_COMP_OK;
f91f0fd5 1958 uint32_t z_size, overflow, gzip_flag, level, hist_bits;
7c673cae
FG
1959 uint8_t *z_buf = NULL;
1960 uint8_t *in_buf = NULL;
1961
f91f0fd5
TL
1962 gzip_flag = rand() % 5;
1963 hist_bits = rand() % 16;
1964 level = get_rand_level();
224ce89b 1965
7c673cae
FG
1966 if (in_size != 0) {
1967 in_buf = malloc(in_size);
1968
1969 if (in_buf == NULL)
1970 return MALLOC_FAILED;
1971
1972 memcpy(in_buf, in_data, in_size);
1973 }
1974
1975 /* Test non-overflow case where a type 0 block is not written */
224ce89b 1976 z_size = 2 * in_size + hdr_bytes;
f91f0fd5 1977 if (gzip_flag == IGZIP_GZIP)
224ce89b 1978 z_size += gzip_extra_bytes;
f91f0fd5
TL
1979 else if (gzip_flag == IGZIP_GZIP_NO_HDR)
1980 z_size += gzip_trl_bytes;
1981 else if (gzip_flag == IGZIP_ZLIB)
1982 z_size += zlib_extra_bytes;
1983 else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
1984 z_size += zlib_trl_bytes;
7c673cae
FG
1985
1986 z_buf = malloc(z_size);
1987
1988 if (z_buf == NULL)
1989 return MALLOC_FAILED;
1990
1991 create_rand_repeat_data(z_buf, z_size);
1992
224ce89b
WB
1993 /* If flush type is invalid */
1994 if (flush_type != NO_FLUSH && flush_type != FULL_FLUSH) {
1995 ret =
1996 compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag,
f91f0fd5 1997 level, hist_bits);
224ce89b
WB
1998
1999 if (ret != INVALID_FLUSH_ERROR)
2000 print_error(ret);
2001 else
2002 ret = 0;
2003
2004 if (z_buf != NULL)
2005 free(z_buf);
2006
2007 if (in_buf != NULL)
2008 free(in_buf);
2009
2010 return ret;
2011 }
2012
2013 /* Else test valid flush type */
f91f0fd5
TL
2014 ret = compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag, level,
2015 hist_bits);
7c673cae
FG
2016
2017 if (!ret)
f91f0fd5
TL
2018 ret =
2019 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag, NULL, 0,
2020 hist_bits);
2021
2022 if (options.verbose && ret) {
2023 log_print
2024 ("Compressed array at level %d with gzip flag %d, flush type %d, and window bits %d: ",
2025 level, gzip_flag, flush_type, hist_bits);
2026 log_uint8_t(z_buf, z_size);
2027 log_print("\n");
2028 log_print("Data: ");
2029 log_uint8_t(in_buf, in_size);
7c673cae 2030 }
f91f0fd5 2031
7c673cae
FG
2032 if (z_buf != NULL) {
2033 free(z_buf);
2034 z_buf = NULL;
2035 }
224ce89b 2036
7c673cae
FG
2037 print_error(ret);
2038 if (ret)
2039 return ret;
2040
2041 /*Test non-overflow case where a type 0 block is possible to be written */
224ce89b 2042 z_size = TYPE0_HDR_SIZE * ((in_size + TYPE0_MAX_SIZE - 1) / TYPE0_MAX_SIZE) + in_size;
7c673cae 2043
f91f0fd5
TL
2044 if (gzip_flag == IGZIP_GZIP)
2045 z_size += gzip_extra_bytes;
2046 else if (gzip_flag == IGZIP_GZIP_NO_HDR)
2047 z_size += gzip_trl_bytes;
2048 else if (gzip_flag == IGZIP_ZLIB)
2049 z_size += zlib_extra_bytes;
2050 else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
2051 z_size += zlib_trl_bytes;
2052
2053 if (z_size <= gzip_extra_bytes)
7c673cae
FG
2054 z_size += TYPE0_HDR_SIZE;
2055
2056 if (z_size < 8)
2057 z_size = 8;
2058
2059 z_buf = malloc(z_size);
2060
2061 if (z_buf == NULL)
2062 return MALLOC_FAILED;
2063
2064 create_rand_repeat_data(z_buf, z_size);
2065
f91f0fd5
TL
2066 ret = compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag, level,
2067 hist_bits);
7c673cae 2068 if (!ret)
f91f0fd5
TL
2069 ret =
2070 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag, NULL, 0,
2071 hist_bits);
7c673cae 2072 if (ret) {
f91f0fd5
TL
2073 log_print
2074 ("Compressed array at level %d with gzip flag %d, flush type %d, and hist_bits %d: ",
2075 level, gzip_flag, flush_type, hist_bits);
2076 log_uint8_t(z_buf, z_size);
2077 log_print("\n");
2078 log_print("Data: ");
2079 log_uint8_t(in_buf, in_size);
7c673cae 2080 }
7c673cae
FG
2081
2082 if (!ret) {
2083 free(z_buf);
2084 z_buf = NULL;
2085
2086 /* Test random overflow case */
2087 z_size = rand() % z_size;
2088
2089 if (z_size > in_size)
2090 z_size = rand() & in_size;
2091
2092 if (z_size > 0) {
2093 z_buf = malloc(z_size);
2094
2095 if (z_buf == NULL)
2096 return MALLOC_FAILED;
2097 }
2098
f91f0fd5
TL
2099 overflow = compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type,
2100 gzip_flag, level, hist_bits);
7c673cae
FG
2101
2102 if (overflow != COMPRESS_OUT_BUFFER_OVERFLOW) {
f91f0fd5
TL
2103 if (overflow == 0)
2104 ret =
2105 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag,
2106 NULL, 0, hist_bits);
2107
2108 if (overflow != 0 || ret != 0) {
2109 log_print("overflow error = %d\n", overflow);
2110 log_error(overflow);
2111 log_print("inflate ret = %d\n", ret);
2112 log_error(ret);
2113
2114 log_print
2115 ("Compressed array at level %d with gzip flag %d, flush type %d, and hist_bits %d: ",
2116 level, gzip_flag, flush_type, hist_bits);
2117
2118 log_uint8_t(z_buf, z_size);
2119 log_print("\n");
2120 log_print("Data: ");
2121 log_uint8_t(in_buf, in_size);
2122
2123 printf("Failed on compress single pass overflow\n");
2124 print_error(ret);
2125 ret = OVERFLOW_TEST_ERROR;
7c673cae 2126 }
7c673cae
FG
2127 }
2128 }
2129
2130 print_error(ret);
224ce89b
WB
2131 if (ret) {
2132 if (z_buf != NULL) {
2133 free(z_buf);
2134 z_buf = NULL;
2135 }
2136 if (in_buf != NULL)
2137 free(in_buf);
2138 return ret;
2139 }
2140
2141 if (flush_type == FULL_FLUSH) {
2142 if (z_buf != NULL)
2143 free(z_buf);
2144
2145 z_size = 2 * in_size + MAX_LOOPS * (hdr_bytes + 5);
2146
2147 z_buf = malloc(z_size);
2148
2149 if (z_buf == NULL)
2150 return MALLOC_FAILED;
7c673cae 2151
224ce89b
WB
2152 create_rand_repeat_data(z_buf, z_size);
2153
2154 /* Else test valid flush type */
f91f0fd5
TL
2155 ret = compress_stateless_full_flush(in_buf, in_size, z_buf, &z_size,
2156 level, hist_bits);
224ce89b
WB
2157
2158 if (!ret)
f91f0fd5
TL
2159 ret =
2160 inflate_check(z_buf, z_size, in_buf, in_size, 0, NULL, 0,
2161 hist_bits);
224ce89b
WB
2162 else if (ret == COMPRESS_LOOP_COUNT_OVERFLOW)
2163 ret = 0;
2164
2165 print_error(ret);
f91f0fd5 2166
224ce89b 2167 if (ret) {
f91f0fd5
TL
2168 log_print
2169 ("Compressed array at level %d with gzip flag %d, flush type %d, and hist_bits %d: ",
2170 level, gzip_flag, FULL_FLUSH, hist_bits);
2171 log_uint8_t(z_buf, z_size);
2172 log_print("\n");
2173 log_print("Data: ");
2174 log_uint8_t(in_buf, in_size);
224ce89b 2175 }
224ce89b 2176 }
7c673cae
FG
2177 if (z_buf != NULL)
2178 free(z_buf);
2179
2180 if (in_buf != NULL)
2181 free(in_buf);
2182
2183 return ret;
2184}
2185
2186/* Test deflate */
2187int test_compress(uint8_t * in_buf, uint32_t in_size, uint32_t flush_type)
2188{
2189 int ret = IGZIP_COMP_OK, fin_ret = IGZIP_COMP_OK;
f91f0fd5
TL
2190 uint32_t overflow = 0, gzip_flag, level, hist_bits;
2191 uint32_t z_size = 0, z_size_max = 0, z_compressed_size, dict_len = 0;
2192 uint8_t *z_buf = NULL, *dict = NULL;
7c673cae
FG
2193
2194 /* Test a non overflow case */
2195 if (flush_type == NO_FLUSH)
224ce89b 2196 z_size_max = 2 * in_size + hdr_bytes + 2;
7c673cae 2197 else if (flush_type == SYNC_FLUSH || flush_type == FULL_FLUSH)
224ce89b 2198 z_size_max = 2 * in_size + MAX_LOOPS * (hdr_bytes + 5);
7c673cae
FG
2199 else {
2200 printf("Invalid Flush Parameter\n");
2201 return COMPRESS_GENERAL_ERROR;
2202 }
2203
f91f0fd5
TL
2204 gzip_flag = rand() % 5;
2205 hist_bits = rand() % 16;
2206 level = get_rand_level();
224ce89b 2207
7c673cae
FG
2208 z_size = z_size_max;
2209
f91f0fd5
TL
2210 if (gzip_flag == IGZIP_GZIP)
2211 z_size += gzip_extra_bytes;
2212 else if (gzip_flag == IGZIP_GZIP_NO_HDR)
2213 z_size += gzip_trl_bytes;
2214 else if (gzip_flag == IGZIP_ZLIB)
2215 z_size += zlib_extra_bytes;
2216 else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
2217 z_size += zlib_trl_bytes;
2218
7c673cae
FG
2219 z_buf = malloc(z_size);
2220 if (z_buf == NULL) {
2221 print_error(MALLOC_FAILED);
2222 return MALLOC_FAILED;
2223 }
224ce89b 2224 create_rand_repeat_data(z_buf, z_size);
7c673cae 2225
f91f0fd5
TL
2226 if (rand() % 8 == 0) {
2227 dict_len = (rand() % IGZIP_HIST_SIZE) + 1;
2228 dict = malloc(dict_len);
2229 if (dict == NULL) {
2230 print_error(MALLOC_FAILED);
2231 return MALLOC_FAILED;
2232 }
2233 create_rand_dict(dict, dict_len, z_buf, z_size);
2234 }
2235
224ce89b 2236 ret = compress_single_pass(in_buf, in_size, z_buf, &z_size, flush_type,
f91f0fd5 2237 gzip_flag, level, dict, dict_len, hist_bits);
7c673cae
FG
2238
2239 if (!ret)
f91f0fd5
TL
2240 ret =
2241 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag, dict, dict_len,
2242 hist_bits);
7c673cae
FG
2243
2244 if (ret) {
f91f0fd5
TL
2245 log_print
2246 ("Compressed array at level %d with gzip flag %d, flush type %d, and hist_bits %d: ",
2247 level, gzip_flag, flush_type, hist_bits);
2248 log_uint8_t(z_buf, z_size);
2249 log_print("\n");
2250 if (dict != NULL) {
2251 log_print("Using Dictionary: ");
2252 log_uint8_t(dict, dict_len);
2253 log_print("\n");
2254 }
2255 log_print("Data: ");
2256 log_uint8_t(in_buf, in_size);
2257
7c673cae
FG
2258 printf("Failed on compress single pass\n");
2259 print_error(ret);
2260 }
2261
f91f0fd5
TL
2262 if (dict != NULL) {
2263 free(dict);
2264 dict = NULL;
2265 dict_len = 0;
2266 }
2267
7c673cae 2268 fin_ret |= ret;
f91f0fd5
TL
2269 if (ret)
2270 goto test_compress_cleanup;
7c673cae
FG
2271
2272 z_compressed_size = z_size;
2273 z_size = z_size_max;
2274 create_rand_repeat_data(z_buf, z_size_max);
2275
f91f0fd5
TL
2276 if (rand() % 8 == 0) {
2277 dict_len = (rand() % IGZIP_HIST_SIZE) + 1;
2278 dict = malloc(dict_len);
2279 if (dict == NULL) {
2280 print_error(MALLOC_FAILED);
2281 return MALLOC_FAILED;
2282 }
2283 create_rand_dict(dict, dict_len, z_buf, z_size);
2284 }
2285
224ce89b 2286 ret =
f91f0fd5
TL
2287 compress_multi_pass(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag, level,
2288 dict, dict_len, hist_bits);
7c673cae
FG
2289
2290 if (!ret)
f91f0fd5
TL
2291 ret =
2292 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag, dict, dict_len,
2293 hist_bits);
7c673cae
FG
2294
2295 if (ret) {
f91f0fd5
TL
2296 log_print
2297 ("Compressed array at level %d with gzip flag %d, flush type %d and hist_bits %d: ",
2298 level, gzip_flag, flush_type, hist_bits);
2299 log_uint8_t(z_buf, z_size);
2300 log_print("\n");
2301 if (dict != NULL) {
2302 log_print("Using Dictionary: ");
2303 log_uint8_t(dict, dict_len);
2304 log_print("\n");
2305 }
2306 log_print("Data: ");
2307 log_uint8_t(in_buf, in_size);
2308
7c673cae
FG
2309 printf("Failed on compress multi pass\n");
2310 print_error(ret);
2311 }
2312
f91f0fd5
TL
2313 if (dict != NULL) {
2314 free(dict);
2315 dict = NULL;
2316 dict_len = 0;
2317 }
2318
7c673cae 2319 fin_ret |= ret;
f91f0fd5
TL
2320 if (ret)
2321 goto test_compress_cleanup;
7c673cae
FG
2322
2323 ret = 0;
2324
2325 /* Test random overflow case */
2326 if (flush_type == SYNC_FLUSH && z_compressed_size > in_size)
2327 z_compressed_size = in_size + 1;
2328
2329 z_size = rand() % z_compressed_size;
f91f0fd5 2330 create_rand_repeat_data(z_buf, z_size);
7c673cae 2331
224ce89b 2332 overflow = compress_single_pass(in_buf, in_size, z_buf, &z_size, flush_type,
f91f0fd5 2333 gzip_flag, level, dict, dict_len, hist_bits);
7c673cae
FG
2334
2335 if (overflow != COMPRESS_OUT_BUFFER_OVERFLOW) {
2336 if (overflow == 0)
f91f0fd5
TL
2337 ret =
2338 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag, dict,
2339 dict_len, hist_bits);
7c673cae
FG
2340
2341 /* Rarely single pass overflow will compresses data
2342 * better than the initial run. This is to stop that
2343 * case from erroring. */
2344 if (overflow != 0 || ret != 0) {
f91f0fd5
TL
2345 log_print("overflow error = %d\n", overflow);
2346 log_error(overflow);
2347 log_print("inflate ret = %d\n", ret);
2348 log_error(ret);
2349
2350 log_print
2351 ("Compressed array at level %d with gzip flag %d, flush type %d, and hist_bits %d: ",
2352 level, gzip_flag, flush_type, hist_bits);
2353 log_uint8_t(z_buf, z_size);
2354 log_print("\n");
2355 log_print("Data: ");
2356 log_uint8_t(in_buf, in_size);
2357
2358 printf("Failed on compress single pass overflow\n");
7c673cae
FG
2359 print_error(ret);
2360 ret = OVERFLOW_TEST_ERROR;
2361 }
2362 }
2363
2364 fin_ret |= ret;
f91f0fd5
TL
2365 if (ret)
2366 goto test_compress_cleanup;
7c673cae
FG
2367
2368 if (flush_type == NO_FLUSH) {
f91f0fd5 2369 create_rand_repeat_data(z_buf, z_size);
7c673cae 2370
224ce89b
WB
2371 overflow =
2372 compress_multi_pass(in_buf, in_size, z_buf, &z_size, flush_type,
f91f0fd5 2373 gzip_flag, level, dict, dict_len, hist_bits);
7c673cae
FG
2374
2375 if (overflow != COMPRESS_OUT_BUFFER_OVERFLOW) {
2376 if (overflow == 0)
f91f0fd5
TL
2377 ret =
2378 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag,
2379 dict, dict_len, hist_bits);
7c673cae
FG
2380
2381 /* Rarely multi pass overflow will compresses data
2382 * better than the initial run. This is to stop that
2383 * case from erroring */
2384 if (overflow != 0 || ret != 0) {
f91f0fd5
TL
2385 log_print("overflow error = %d\n", overflow);
2386 log_error(overflow);
2387 log_print("inflate ret = %d\n", ret);
2388 log_error(ret);
2389 log_print
2390 ("Compressed array at level %d with gzip flag %d, flush type %d, and hist_bits %d: ",
2391 level, gzip_flag, flush_type, hist_bits);
2392 log_uint8_t(z_buf, z_size);
2393 log_print("\n");
2394 log_print("Data: ");
2395 log_uint8_t(in_buf, in_size);
2396
7c673cae
FG
2397 printf("Failed on compress multi pass overflow\n");
2398 print_error(ret);
2399 ret = OVERFLOW_TEST_ERROR;
2400 }
2401 }
2402 fin_ret |= ret;
2403 }
2404
f91f0fd5 2405 test_compress_cleanup:
7c673cae
FG
2406 free(z_buf);
2407
2408 return fin_ret;
2409}
2410
2411/* Test swapping flush types in the middle of compression */
2412int test_flush(uint8_t * in_buf, uint32_t in_size)
2413{
2414 int fin_ret = IGZIP_COMP_OK, ret;
224ce89b 2415 uint32_t z_size, flush_type = 0, gzip_flag, level;
7c673cae
FG
2416 uint8_t *z_buf = NULL;
2417
f91f0fd5
TL
2418 gzip_flag = rand() % 5;
2419 level = get_rand_level();
7c673cae 2420
224ce89b 2421 z_size = 2 * in_size + 2 * hdr_bytes + 8;
f91f0fd5 2422 if (gzip_flag == IGZIP_GZIP)
224ce89b 2423 z_size += gzip_extra_bytes;
f91f0fd5
TL
2424 else if (gzip_flag == IGZIP_GZIP_NO_HDR)
2425 z_size += gzip_trl_bytes;
2426 else if (gzip_flag == IGZIP_ZLIB)
2427 z_size += zlib_extra_bytes;
2428 else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
2429 z_size += zlib_trl_bytes;
2430
7c673cae
FG
2431 z_buf = malloc(z_size);
2432
2433 if (z_buf == NULL)
2434 return MALLOC_FAILED;
2435
2436 create_rand_repeat_data(z_buf, z_size);
2437
2438 while (flush_type < 3)
f91f0fd5 2439 flush_type = rand() & 0xFFFF;
7c673cae
FG
2440
2441 /* Test invalid flush */
224ce89b 2442 ret = compress_single_pass(in_buf, in_size, z_buf, &z_size, flush_type,
f91f0fd5 2443 gzip_flag, level, NULL, 0, 0);
7c673cae
FG
2444
2445 if (ret == COMPRESS_GENERAL_ERROR)
2446 ret = 0;
2447 else {
2448 printf("Failed when passing invalid flush parameter\n");
2449 ret = INVALID_FLUSH_ERROR;
2450 }
2451
2452 fin_ret |= ret;
2453 print_error(ret);
2454
2455 create_rand_repeat_data(z_buf, z_size);
2456
2457 /* Test swapping flush type */
f91f0fd5
TL
2458 ret =
2459 compress_swap_flush(in_buf, in_size, z_buf, &z_size, rand() % 3, level, gzip_flag);
7c673cae
FG
2460
2461 if (!ret)
f91f0fd5 2462 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag, NULL, 0, 0);
7c673cae
FG
2463
2464 if (ret) {
f91f0fd5
TL
2465 log_print("Compressed array at level %d with gzip flag %d: ", level,
2466 gzip_flag);
2467 log_uint8_t(z_buf, z_size);
2468 log_print("\n");
2469 log_print("Data: ");
2470 log_uint8_t(in_buf, in_size);
2471
7c673cae
FG
2472 printf("Failed on swapping flush type\n");
2473 print_error(ret);
2474 }
2475
2476 fin_ret |= ret;
2477 print_error(ret);
2478
2479 return fin_ret;
2480}
2481
2482/* Test there are no length distance pairs across full flushes */
2483int test_full_flush(uint8_t * in_buf, uint32_t in_size)
2484{
2485 int ret = IGZIP_COMP_OK;
224ce89b 2486 uint32_t z_size, gzip_flag, level;
7c673cae
FG
2487 uint8_t *z_buf = NULL;
2488
f91f0fd5
TL
2489 gzip_flag = rand() % 5;
2490 level = get_rand_level();
224ce89b
WB
2491 z_size = 2 * in_size + MAX_LOOPS * (hdr_bytes + 5);
2492
f91f0fd5 2493 if (gzip_flag == IGZIP_GZIP)
224ce89b 2494 z_size += gzip_extra_bytes;
f91f0fd5
TL
2495 else if (gzip_flag == IGZIP_GZIP_NO_HDR)
2496 z_size += gzip_trl_bytes;
2497 else if (gzip_flag == IGZIP_ZLIB)
2498 z_size += zlib_extra_bytes;
2499 else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
2500 z_size += zlib_trl_bytes;
7c673cae
FG
2501
2502 z_buf = malloc(z_size);
2503 if (z_buf == NULL) {
2504 print_error(MALLOC_FAILED);
2505 return MALLOC_FAILED;
2506 }
2507
2508 create_rand_repeat_data(z_buf, z_size);
2509
224ce89b 2510 ret = compress_full_flush(in_buf, in_size, z_buf, &z_size, gzip_flag, level);
7c673cae
FG
2511
2512 if (!ret)
f91f0fd5 2513 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag, NULL, 0, 0);
7c673cae
FG
2514
2515 if (ret) {
f91f0fd5
TL
2516 log_print("Compressed array at level %d with gzip flag %d and flush type %d: ",
2517 level, gzip_flag, FULL_FLUSH);
2518 log_uint8_t(z_buf, z_size);
2519 log_print("\n");
2520 log_print("Data: ");
2521 log_uint8_t(in_buf, in_size);
2522
7c673cae
FG
2523 printf("Failed on compress multi pass\n");
2524 print_error(ret);
2525 }
2526
2527 free(z_buf);
2528
2529 return ret;
2530}
2531
224ce89b
WB
2532int test_inflate(struct vect_result *in_vector)
2533{
2534 int ret = IGZIP_COMP_OK;
2535 uint8_t *compress_buf = in_vector->vector, *out_buf = NULL;
2536 uint64_t compress_len = in_vector->vector_length;
2537 uint32_t out_size = 0;
2538
2539 out_size = 10 * in_vector->vector_length;
2540 out_buf = malloc(out_size);
2541 if (out_buf == NULL)
2542 return MALLOC_FAILED;
2543
2544 ret = inflate_stateless_pass(compress_buf, compress_len, out_buf, &out_size, 0);
2545
2546 if (ret == INFLATE_LEFTOVER_INPUT)
2547 ret = ISAL_DECOMP_OK;
2548
2549 if (ret != in_vector->expected_error)
2550 printf("Inflate return value incorrect, %d != %d\n", ret,
2551 in_vector->expected_error);
2552 else
2553 ret = IGZIP_COMP_OK;
2554
2555 if (!ret) {
f91f0fd5
TL
2556 ret = inflate_multi_pass(compress_buf, compress_len, out_buf, &out_size,
2557 0, NULL, 0, 0);
224ce89b
WB
2558
2559 if (ret == INFLATE_LEFTOVER_INPUT)
2560 ret = ISAL_DECOMP_OK;
2561
2562 if (ret != in_vector->expected_error)
2563 printf("Inflate return value incorrect, %d != %d\n", ret,
2564 in_vector->expected_error);
2565 else
2566 ret = IGZIP_COMP_OK;
2567 }
2568
2569 return ret;
2570
2571}
2572
f91f0fd5 2573int test_large(uint8_t * in_buf, uint32_t in_size, uint64_t large_size)
7c673cae 2574{
7c673cae 2575
f91f0fd5
TL
2576 int ret = IGZIP_COMP_OK;
2577 uint32_t gzip_flag, level;
2578 uint32_t z_size = 0, z_size_max = 0, tmp_buf_size;
2579 uint8_t *z_buf = NULL, *tmp_buf = NULL;
2580 int flush_type = NO_FLUSH;
2581
2582 /* Test a non overflow case */
2583 z_size_max = MAX_LARGE_COMP_BUF_SIZE;
2584
2585 gzip_flag = rand() % 5;
2586 level = get_rand_level();
2587
2588 z_size = z_size_max;
2589 z_buf = malloc(z_size);
2590 if (z_buf == NULL) {
2591 print_error(MALLOC_FAILED);
2592 return MALLOC_FAILED;
2593 }
2594 create_rand_repeat_data(z_buf, z_size);
2595
2596 tmp_buf_size = IBUF_SIZE;
2597 tmp_buf = malloc(tmp_buf_size);
2598 if (tmp_buf == NULL) {
2599 print_error(MALLOC_FAILED);
2600 return MALLOC_FAILED;
2601 }
2602
2603 ret =
2604 compress_ver_rep_buf(in_buf, in_size, large_size, z_buf, z_size, tmp_buf,
2605 tmp_buf_size, flush_type, gzip_flag, level);
2606
2607 if (ret)
2608 print_error(ret);
2609
2610 if (z_buf != NULL) {
2611 free(z_buf);
2612 z_buf = NULL;
2613 }
2614
2615 if (tmp_buf != NULL) {
2616 free(tmp_buf);
2617 tmp_buf = NULL;
2618 }
2619
2620 return ret;
7c673cae
FG
2621}
2622
2623/* Run multiple compression tests on data stored in a file */
2624int test_compress_file(char *file_name)
2625{
2626 int ret = IGZIP_COMP_OK;
f91f0fd5 2627 uint64_t in_size;
7c673cae
FG
2628 uint8_t *in_buf = NULL;
2629 FILE *in_file = NULL;
2630
2631 in_file = fopen(file_name, "rb");
f91f0fd5
TL
2632 if (!in_file) {
2633 printf("Failed to open file %s\n", file_name);
7c673cae 2634 return FILE_READ_FAILED;
f91f0fd5 2635 }
7c673cae
FG
2636
2637 in_size = get_filesize(in_file);
f91f0fd5
TL
2638 if (in_size > MAX_FILE_SIZE)
2639 in_size = MAX_FILE_SIZE;
2640
7c673cae
FG
2641 if (in_size != 0) {
2642 in_buf = malloc(in_size);
f91f0fd5
TL
2643 if (in_buf == NULL) {
2644 printf("Failed to allocate in_buf for test_compress_file\n");
7c673cae 2645 return MALLOC_FAILED;
f91f0fd5
TL
2646 }
2647 if (fread(in_buf, 1, in_size, in_file) != in_size) {
2648 printf("Failed to read in_buf from test_compress_file\n");
2649 free(in_buf);
2650 return FILE_READ_FAILED;
2651 }
7c673cae
FG
2652 }
2653
224ce89b 2654 ret |= test_compress_stateless(in_buf, in_size, NO_FLUSH);
f91f0fd5
TL
2655 if (!ret)
2656 ret |= test_compress_stateless(in_buf, in_size, SYNC_FLUSH);
2657 if (!ret)
2658 ret |= test_compress_stateless(in_buf, in_size, FULL_FLUSH);
2659 if (!ret)
2660 ret |= test_compress(in_buf, in_size, NO_FLUSH);
2661 if (!ret)
2662 ret |= test_compress(in_buf, in_size, SYNC_FLUSH);
2663 if (!ret)
2664 ret |= test_compress(in_buf, in_size, FULL_FLUSH);
2665 if (!ret)
2666 ret |= test_flush(in_buf, in_size);
7c673cae
FG
2667
2668 if (ret)
2669 printf("Failed on file %s\n", file_name);
2670
2671 if (in_buf != NULL)
2672 free(in_buf);
2673
2674 return ret;
2675}
2676
f91f0fd5
TL
2677int create_custom_hufftables(struct isal_hufftables *hufftables_custom, int file_count,
2678 char *files[])
7c673cae
FG
2679{
2680 long int file_length;
2681 uint8_t *stream = NULL;
2682 struct isal_huff_histogram histogram;
2683 FILE *file;
f91f0fd5 2684 int i;
7c673cae
FG
2685
2686 memset(&histogram, 0, sizeof(histogram));
2687
f91f0fd5
TL
2688 for (i = 0; i < file_count; i++) {
2689 printf("Processing %s\n", files[i]);
2690 file = fopen(files[i], "r");
7c673cae
FG
2691 if (file == NULL) {
2692 printf("Error opening file\n");
2693 return 1;
2694 }
2695 fseek(file, 0, SEEK_END);
2696 file_length = ftell(file);
2697 fseek(file, 0, SEEK_SET);
2698 file_length -= ftell(file);
2699
2700 if (file_length > 0) {
2701 stream = malloc(file_length);
2702 if (stream == NULL) {
2703 printf("Failed to allocate memory to read in file\n");
2704 fclose(file);
2705 return 1;
2706 }
2707 }
2708
f91f0fd5 2709 if (fread(stream, 1, file_length, file) != file_length) {
224ce89b 2710 printf("Error occurred when reading file\n");
7c673cae
FG
2711 fclose(file);
2712 free(stream);
224ce89b 2713 stream = NULL;
7c673cae
FG
2714 return 1;
2715 }
2716
2717 /* Create a histogram of frequency of symbols found in stream to
2718 * generate the huffman tree.*/
2719 isal_update_histogram(stream, file_length, &histogram);
2720
2721 fclose(file);
224ce89b
WB
2722 if (stream != NULL) {
2723 free(stream);
2724 stream = NULL;
2725 }
7c673cae
FG
2726 }
2727
2728 return isal_create_hufftables(hufftables_custom, &histogram);
2729
2730}
2731
2732int main(int argc, char *argv[])
2733{
f91f0fd5 2734 int i = 0, j = 0, ret = 0, fin_ret = 0;
7c673cae
FG
2735 uint32_t in_size = 0, offset = 0;
2736 uint8_t *in_buf;
f91f0fd5
TL
2737 struct isal_hufftables hufftables_custom, hufftables_sub;
2738 uint64_t iterations, large_buf_size;
2739 size_t argv_index;
2740 char **input_files;
2741 size_t file_count;
7c673cae 2742
f91f0fd5
TL
2743 argv_index = parse_options(argc, argv);
2744
2745 input_files = &argv[argv_index];
2746 file_count = argc - argv_index;
2747
2748 if (options.verbose)
2749 setbuf(stdout, NULL);
7c673cae 2750
224ce89b 2751 printf("Window Size: %d K\n", IGZIP_HIST_SIZE / 1024);
f91f0fd5
TL
2752 printf("Test Seed : %d\n", options.test_seed);
2753 printf("Randoms : %d\n", options.randoms);
2754 srand(options.test_seed);
7c673cae 2755
f91f0fd5
TL
2756 hufftables_subset = &hufftables_sub;
2757 if (file_count > 0) {
2758 ret = create_custom_hufftables(&hufftables_custom, file_count, input_files);
7c673cae
FG
2759 if (ret == 0)
2760 hufftables = &hufftables_custom;
2761 else {
2762 printf("Failed to generate custom hufftable");
2763 return -1;
2764 }
2765 }
2766
2767 in_buf = malloc(IBUF_SIZE);
2768 memset(in_buf, 0, IBUF_SIZE);
2769
2770 if (in_buf == NULL) {
2771 fprintf(stderr, "Can't allocate in_buf memory\n");
2772 return -1;
2773 }
2774
f91f0fd5 2775 if (file_count > 0) {
224ce89b 2776 printf("igzip_rand_test files: ");
7c673cae 2777
f91f0fd5
TL
2778 for (i = 0; i < file_count; i++) {
2779 ret |= test_compress_file(input_files[i]);
7c673cae
FG
2780 if (ret)
2781 return ret;
2782 }
2783
2784 printf("................");
2785 printf("%s\n", ret ? "Fail" : "Pass");
2786 fin_ret |= ret;
2787 }
2788
224ce89b 2789 printf("igzip_rand_test stateless: ");
7c673cae 2790
224ce89b 2791 ret = test_compress_stateless((uint8_t *) str1, sizeof(str1), NO_FLUSH);
7c673cae
FG
2792 if (ret)
2793 return ret;
2794
224ce89b 2795 ret |= test_compress_stateless((uint8_t *) str2, sizeof(str2), NO_FLUSH);
7c673cae
FG
2796 if (ret)
2797 return ret;
2798
f91f0fd5
TL
2799 for (i = 0; i < options.randoms; i++) {
2800 in_size = get_rand_data_length();
7c673cae
FG
2801 offset = rand() % (IBUF_SIZE + 1 - in_size);
2802 in_buf += offset;
2803
2804 create_rand_repeat_data(in_buf, in_size);
2805
224ce89b 2806 ret |= test_compress_stateless(in_buf, in_size, NO_FLUSH);
7c673cae
FG
2807
2808 in_buf -= offset;
2809
f91f0fd5 2810 if (i % (options.randoms / 16) == 0)
7c673cae
FG
2811 printf(".");
2812
2813 if (ret)
2814 return ret;
2815 }
2816
f91f0fd5 2817 for (i = 0; i < options.randoms / 16; i++) {
7c673cae 2818 create_rand_repeat_data(in_buf, PAGE_SIZE);
224ce89b
WB
2819 ret |= test_compress_stateless(in_buf, PAGE_SIZE, NO_FLUSH); // good for efence
2820 if (ret)
2821 return ret;
2822 }
2823
2824 fin_ret |= ret;
2825
2826 ret = test_compress_stateless((uint8_t *) str1, sizeof(str1), SYNC_FLUSH);
2827 if (ret)
2828 return ret;
2829
2830 ret |= test_compress_stateless((uint8_t *) str2, sizeof(str2), SYNC_FLUSH);
2831 if (ret)
2832 return ret;
2833
2834 for (i = 0; i < 16; i++) {
f91f0fd5 2835 in_size = get_rand_data_length();
224ce89b
WB
2836 offset = rand() % (IBUF_SIZE + 1 - in_size);
2837 in_buf += offset;
2838
2839 create_rand_repeat_data(in_buf, in_size);
2840
2841 ret |= test_compress_stateless(in_buf, in_size, SYNC_FLUSH);
2842
2843 in_buf -= offset;
2844
2845 if (ret)
2846 return ret;
2847 }
2848
2849 fin_ret |= ret;
2850
2851 printf("%s\n", ret ? "Fail" : "Pass");
2852
2853 printf("igzip_rand_test stateless FULL_FLUSH: ");
2854
2855 ret = test_compress_stateless((uint8_t *) str1, sizeof(str1), FULL_FLUSH);
2856 if (ret)
2857 return ret;
2858
2859 ret |= test_compress_stateless((uint8_t *) str2, sizeof(str2), FULL_FLUSH);
2860 if (ret)
2861 return ret;
2862
f91f0fd5
TL
2863 for (i = 0; i < options.randoms; i++) {
2864 in_size = get_rand_data_length();
224ce89b
WB
2865 offset = rand() % (IBUF_SIZE + 1 - in_size);
2866 in_buf += offset;
2867
2868 create_rand_repeat_data(in_buf, in_size);
2869
2870 ret |= test_compress_stateless(in_buf, in_size, FULL_FLUSH);
2871
2872 in_buf -= offset;
2873
f91f0fd5 2874 if (i % (options.randoms / 16) == 0)
224ce89b
WB
2875 printf(".");
2876
2877 if (ret)
2878 return ret;
7c673cae
FG
2879 }
2880
f91f0fd5 2881 for (i = 0; i < options.randoms / 16; i++) {
224ce89b
WB
2882 create_rand_repeat_data(in_buf, PAGE_SIZE);
2883 ret |= test_compress_stateless(in_buf, PAGE_SIZE, FULL_FLUSH); // good for efence
2884 if (ret)
2885 return ret;
2886 }
7c673cae
FG
2887 fin_ret |= ret;
2888
2889 printf("%s\n", ret ? "Fail" : "Pass");
2890
224ce89b 2891 printf("igzip_rand_test stateful NO_FLUSH: ");
7c673cae 2892
f91f0fd5
TL
2893 memcpy(in_buf, str1, sizeof(str1));
2894 ret = test_compress(in_buf, sizeof(str1), NO_FLUSH);
7c673cae
FG
2895 if (ret)
2896 return ret;
2897
f91f0fd5
TL
2898 memcpy(in_buf, str2, sizeof(str2));
2899 ret |= test_compress(in_buf, sizeof(str2), NO_FLUSH);
7c673cae
FG
2900 if (ret)
2901 return ret;
2902
f91f0fd5
TL
2903 for (i = 0; i < options.randoms; i++) {
2904 in_size = get_rand_data_length();
7c673cae
FG
2905 offset = rand() % (IBUF_SIZE + 1 - in_size);
2906 in_buf += offset;
2907
2908 create_rand_repeat_data(in_buf, in_size);
2909
2910 ret |= test_compress(in_buf, in_size, NO_FLUSH);
2911
2912 in_buf -= offset;
2913
f91f0fd5 2914 if (i % (options.randoms / 16) == 0)
7c673cae
FG
2915 printf(".");
2916 if (ret)
2917 return ret;
2918 }
2919
2920 fin_ret |= ret;
2921
2922 printf("%s\n", ret ? "Fail" : "Pass");
2923
224ce89b 2924 printf("igzip_rand_test stateful SYNC_FLUSH: ");
7c673cae 2925
f91f0fd5
TL
2926 memcpy(in_buf, str1, sizeof(str1));
2927 ret = test_compress(in_buf, sizeof(str1), SYNC_FLUSH);
7c673cae
FG
2928 if (ret)
2929 return ret;
2930
f91f0fd5
TL
2931 memcpy(in_buf, str2, sizeof(str2));
2932 ret |= test_compress(in_buf, sizeof(str2), SYNC_FLUSH);
7c673cae
FG
2933 if (ret)
2934 return ret;
2935
f91f0fd5
TL
2936 for (i = 0; i < options.randoms; i++) {
2937 in_size = get_rand_data_length();
7c673cae
FG
2938 offset = rand() % (IBUF_SIZE + 1 - in_size);
2939 in_buf += offset;
2940
2941 create_rand_repeat_data(in_buf, in_size);
2942
2943 ret |= test_compress(in_buf, in_size, SYNC_FLUSH);
2944
2945 in_buf -= offset;
2946
f91f0fd5 2947 if (i % (options.randoms / 16) == 0)
7c673cae
FG
2948 printf(".");
2949 if (ret)
2950 return ret;
2951 }
2952
2953 fin_ret |= ret;
2954
2955 printf("%s\n", ret ? "Fail" : "Pass");
2956
224ce89b 2957 printf("igzip_rand_test stateful FULL_FLUSH: ");
7c673cae 2958
f91f0fd5
TL
2959 memcpy(in_buf, str1, sizeof(str1));
2960 ret = test_compress(in_buf, sizeof(str1), FULL_FLUSH);
7c673cae
FG
2961 if (ret)
2962 return ret;
2963
f91f0fd5
TL
2964 memcpy(in_buf, str2, sizeof(str2));
2965 ret |= test_compress(in_buf, sizeof(str2), FULL_FLUSH);
7c673cae
FG
2966 if (ret)
2967 return ret;
2968
f91f0fd5
TL
2969 for (i = 0; i < options.randoms; i++) {
2970 in_size = get_rand_data_length();
7c673cae
FG
2971 offset = rand() % (IBUF_SIZE + 1 - in_size);
2972 in_buf += offset;
2973
2974 create_rand_repeat_data(in_buf, in_size);
2975
2976 ret |= test_compress(in_buf, in_size, FULL_FLUSH);
2977
2978 in_buf -= offset;
2979
f91f0fd5 2980 if (i % (options.randoms / 16) == 0)
7c673cae
FG
2981 printf(".");
2982 if (ret)
2983 return ret;
2984 }
2985
f91f0fd5
TL
2986 for (i = 0; i < options.randoms / 8; i++) {
2987 in_size = get_rand_data_length();
7c673cae
FG
2988 offset = rand() % (IBUF_SIZE + 1 - in_size);
2989 in_buf += offset;
2990
2991 create_rand_repeat_data(in_buf, in_size);
2992
2993 ret |= test_full_flush(in_buf, in_size);
2994
2995 in_buf -= offset;
2996
2997 if (ret)
2998 return ret;
2999 }
7c673cae
FG
3000
3001 fin_ret |= ret;
3002
3003 printf("%s\n", ret ? "Fail" : "Pass");
3004
224ce89b 3005 printf("igzip_rand_test stateful Change Flush: ");
7c673cae
FG
3006
3007 ret = test_flush((uint8_t *) str1, sizeof(str1));
3008 if (ret)
3009 return ret;
3010
3011 ret |= test_flush((uint8_t *) str2, sizeof(str2));
3012 if (ret)
3013 return ret;
3014
f91f0fd5
TL
3015 for (i = 0; i < options.randoms / 4; i++) {
3016 in_size = get_rand_data_length();
7c673cae
FG
3017 offset = rand() % (IBUF_SIZE + 1 - in_size);
3018 in_buf += offset;
3019
3020 create_rand_repeat_data(in_buf, in_size);
3021
3022 ret |= test_flush(in_buf, in_size);
3023
3024 in_buf -= offset;
3025
f91f0fd5 3026 if (i % ((options.randoms / 4) / 16) == 0)
7c673cae
FG
3027 printf(".");
3028 if (ret)
3029 return ret;
3030 }
3031
3032 fin_ret |= ret;
3033
3034 printf("%s\n", ret ? "Fail" : "Pass");
3035
f91f0fd5
TL
3036 if (options.do_large_test) {
3037 printf("igzip_rand_test large input ");
3038
3039 iterations = options.randoms / 256 + 1;
3040 for (i = 0; i < iterations; i++) {
3041 in_size = rand() % (32 * 1024) + 16 * 1024;
3042 offset = rand() % (IBUF_SIZE + 1 - in_size);
3043 in_buf += offset;
3044
3045 large_buf_size = 1;
3046 large_buf_size <<= 32;
3047 large_buf_size += rand() % (1024 * 1024) + 1;
3048 create_rand_repeat_data(in_buf, in_size);
3049
3050 ret |= test_large(in_buf, in_size, large_buf_size);
3051
3052 if (ret)
3053 return ret;
3054
3055 in_buf -= offset;
3056
3057 if (iterations < 16) {
3058 for (j = 0; j < 16 / iterations; j++)
3059 printf(".");
3060 } else if (i % (iterations / 16) == 0)
3061 printf(".");
3062
3063 }
3064
3065 if (iterations < 16) {
3066 for (j = (16 / iterations) * iterations; j < 16; j++)
3067 printf(".");
3068 }
3069
3070 printf("%s\n", ret ? "Fail" : "Pass");
3071 }
3072
224ce89b
WB
3073 printf("igzip_rand_test inflate Std Vectors: ");
3074
3075 for (i = 0; i < sizeof(std_vect_array) / sizeof(struct vect_result); i++) {
3076 ret = test_inflate(&std_vect_array[i]);
3077 if (ret)
3078 return ret;
3079 }
3080 printf("................");
3081 printf("%s\n", ret ? "Fail" : "Pass");
3082
3083 printf("igzip rand test finished: %s\n",
7c673cae
FG
3084 fin_ret ? "Some tests failed" : "All tests passed");
3085
3086 return fin_ret != IGZIP_COMP_OK;
3087}