]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/igzip/igzip_rand_test.c
update sources to v12.1.1
[ceph.git] / ceph / src / isa-l / igzip / igzip_rand_test.c
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
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34 #include "igzip_lib.h"
35 #include "crc_inflate.h"
36 #include "inflate_std_vects.h"
37 #include <math.h>
38
39 #ifndef RANDOMS
40 # define RANDOMS 0x40
41 #endif
42 #ifndef TEST_SEED
43 # define TEST_SEED 0x1234
44 #endif
45
46 #define IBUF_SIZE (1024*1024)
47
48 #define PAGE_SIZE 4*1024
49
50 #define str1 "Short test string"
51 #define str2 "one two three four five six seven eight nine ten eleven twelve " \
52 "thirteen fourteen fifteen sixteen"
53
54 #define TYPE0_HDR_SIZE 5 /* Size of a type 0 blocks header in bytes */
55 #define TYPE0_MAX_SIZE 65535 /* Max length of a type 0 block in bytes (excludes the header) */
56
57 #define MAX_LOOPS 20
58 /* Defines for the possible error conditions */
59 enum IGZIP_TEST_ERROR_CODES {
60 IGZIP_COMP_OK,
61
62 MALLOC_FAILED,
63 FILE_READ_FAILED,
64
65 COMPRESS_INCORRECT_STATE,
66 COMPRESS_INPUT_STREAM_INTEGRITY_ERROR,
67 COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR,
68 COMPRESS_END_OF_STREAM_NOT_SET,
69 COMPRESS_ALL_INPUT_FAIL,
70 COMPRESS_OUT_BUFFER_OVERFLOW,
71 COMPRESS_LOOP_COUNT_OVERFLOW,
72 COMPRESS_GENERAL_ERROR,
73
74 INFLATE_END_OF_INPUT,
75 INFLATE_INVALID_BLOCK_HEADER,
76 INFLATE_INVALID_SYMBOL,
77 INFLATE_OUT_BUFFER_OVERFLOW,
78 INFLATE_LEFTOVER_INPUT,
79 INFLATE_INCORRECT_OUTPUT_SIZE,
80 INFLATE_INVALID_LOOK_BACK_DISTANCE,
81 INVALID_GZIP_HEADER,
82 INCORRECT_GZIP_TRAILER,
83 INFLATE_GENERAL_ERROR,
84
85 INVALID_FLUSH_ERROR,
86
87 OVERFLOW_TEST_ERROR,
88 RESULT_ERROR
89 };
90
91 static const int hdr_bytes = 300;
92
93 static const uint8_t gzip_hdr[10] = {
94 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0xff
96 };
97
98 static const uint32_t gzip_hdr_bytes = 10;
99 /* static const uint32_t gzip_trl_bytes = 8; */
100
101 static const int gzip_extra_bytes = 18; /* gzip_hdr_bytes + gzip_trl_bytes */
102
103 int inflate_type = 0;
104
105 struct isal_hufftables *hufftables = NULL;
106
107 #define HISTORY_SIZE 32*1024
108 #define MIN_LENGTH 3
109 #define MIN_DIST 1
110
111 /* Create random compressible data. This is achieved by randomly choosing a
112 * random character, or to repeat previous data in the stream for a random
113 * length and look back distance. The probability of a random character or a
114 * repeat being chosen is semi-randomly chosen by setting max_repeat_data to be
115 * differing values */
116 void create_rand_repeat_data(uint8_t * data, int size)
117 {
118 uint32_t next_data;
119 uint8_t *data_start = data;
120 uint32_t length, distance;
121 uint32_t max_repeat_data = 256;
122 uint32_t power = rand() % 32;
123 /* An array of the powers of 2 (except the final element which is 0) */
124 const uint32_t power_of_2_array[] = {
125 0x00000001, 0x00000002, 0x00000004, 0x00000008,
126 0x00000010, 0x00000020, 0x00000040, 0x00000080,
127 0x00000100, 0x00000200, 0x00000400, 0x00000800,
128 0x00001000, 0x00002000, 0x00004000, 0x00008000,
129 0x00010000, 0x00020000, 0x00040000, 0x00080000,
130 0x00100000, 0x00200000, 0x00400000, 0x00800000,
131 0x01000000, 0x02000000, 0x04000000, 0x08000000,
132 0x10000000, 0x20000000, 0x40000000, 0x00000000
133 };
134
135 max_repeat_data += power_of_2_array[power];
136
137 if (size-- > 0)
138 *data++ = rand();
139
140 while (size > 0) {
141 next_data = rand() % max_repeat_data;
142 if (next_data < 256) {
143 *data++ = next_data;
144 size--;
145 } else if (size < 3) {
146 *data++ = rand() % 256;
147 size--;
148 } else {
149 length = (rand() % 256) + MIN_LENGTH;
150 if (length > size)
151 length = (rand() % (size - 2)) + MIN_LENGTH;
152
153 distance = (rand() % HISTORY_SIZE) + MIN_DIST;
154 if (distance > data - data_start)
155 distance = (rand() % (data - data_start)) + MIN_DIST;
156
157 size -= length;
158 if (distance <= length) {
159 while (length-- > 0) {
160 *data = *(data - distance);
161 data++;
162 }
163 } else
164 memcpy(data, data - distance, length);
165 }
166 }
167 }
168
169 void print_error(int error_code)
170 {
171 switch (error_code) {
172 case IGZIP_COMP_OK:
173 break;
174 case MALLOC_FAILED:
175 printf("error: failed to allocate memory\n");
176 break;
177 case FILE_READ_FAILED:
178 printf("error: failed to read in file\n");
179 break;
180 case COMPRESS_INCORRECT_STATE:
181 printf("error: incorrect stream internal state\n");
182 break;
183 case COMPRESS_INPUT_STREAM_INTEGRITY_ERROR:
184 printf("error: inconsistent stream input buffer\n");
185 break;
186 case COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR:
187 printf("error: inconsistent stream output buffer\n");
188 break;
189 case COMPRESS_END_OF_STREAM_NOT_SET:
190 printf("error: end of stream not set\n");
191 break;
192 case COMPRESS_ALL_INPUT_FAIL:
193 printf("error: not all input data compressed\n");
194 break;
195 case COMPRESS_OUT_BUFFER_OVERFLOW:
196 printf("error: output buffer overflow while compressing data\n");
197 break;
198 case COMPRESS_GENERAL_ERROR:
199 printf("error: compression failed\n");
200 break;
201 case INFLATE_END_OF_INPUT:
202 printf("error: did not decompress all input\n");
203 break;
204 case INFLATE_INVALID_BLOCK_HEADER:
205 printf("error: invalid header\n");
206 break;
207 case INFLATE_INVALID_SYMBOL:
208 printf("error: invalid symbol found when decompressing input\n");
209 break;
210 case INFLATE_OUT_BUFFER_OVERFLOW:
211 printf("error: output buffer overflow while decompressing data\n");
212 break;
213 case INFLATE_GENERAL_ERROR:
214 printf("error: decompression failed\n");
215 break;
216 case INFLATE_LEFTOVER_INPUT:
217 printf("error: the trailer of igzip output contains junk\n");
218 break;
219 case INFLATE_INCORRECT_OUTPUT_SIZE:
220 printf("error: incorrect amount of data was decompressed\n");
221 break;
222 case INFLATE_INVALID_LOOK_BACK_DISTANCE:
223 printf("error: invalid look back distance found while decompressing\n");
224 break;
225 case INVALID_GZIP_HEADER:
226 printf("error: incorrect gzip header found when inflating data\n");
227 break;
228 case INCORRECT_GZIP_TRAILER:
229 printf("error: incorrect gzip trailer found when inflating data\n");
230 break;
231 case INVALID_FLUSH_ERROR:
232 printf("error: invalid flush did not cause compression to error\n");
233 break;
234 case RESULT_ERROR:
235 printf("error: decompressed data is not the same as the compressed data\n");
236 break;
237 case OVERFLOW_TEST_ERROR:
238 printf("error: overflow undetected\n");
239 break;
240 default:
241 printf("error: unknown error code\n");
242 }
243 }
244
245 void print_uint8_t(uint8_t * array, uint64_t length)
246 {
247 const int line_size = 16;
248 int i;
249
250 printf("Length = %lu", length);
251 for (i = 0; i < length; i++) {
252 if ((i % line_size) == 0)
253 printf("\n0x%08x\t", i);
254 else
255 printf(" ");
256 printf("0x%02x,", array[i]);
257 }
258 printf("\n");
259 }
260
261 uint32_t check_gzip_header(uint8_t * z_buf)
262 {
263 /* These values are defined in RFC 1952 page 4 */
264 const uint8_t ID1 = 0x1f, ID2 = 0x8b, CM = 0x08, FLG = 0;
265 uint32_t ret = 0;
266 int i;
267 /* Verify that the gzip header is the one used in hufftables_c.c */
268 for (i = 0; i < gzip_hdr_bytes; i++)
269 if (z_buf[i] != gzip_hdr[i])
270 ret = INVALID_GZIP_HEADER;
271
272 /* Verify that the gzip header is a valid gzip header */
273 if (*z_buf++ != ID1)
274 ret = INVALID_GZIP_HEADER;
275
276 if (*z_buf++ != ID2)
277 ret = INVALID_GZIP_HEADER;
278
279 /* Verfiy compression method is Deflate */
280 if (*z_buf++ != CM)
281 ret = INVALID_GZIP_HEADER;
282
283 /* The following comparison is specific to how gzip headers are written in igzip */
284 /* Verify no extra flags are set */
285 if (*z_buf != FLG)
286 ret = INVALID_GZIP_HEADER;
287
288 /* The last 6 bytes in the gzip header do not contain any information
289 * important to decomrpessing the data */
290
291 return ret;
292 }
293
294 uint32_t check_gzip_trl(uint64_t gzip_trl, uint32_t inflate_crc, uint8_t * uncompress_buf,
295 uint32_t uncompress_len)
296 {
297 uint64_t trl, ret = 0;
298 uint32_t crc;
299
300 crc = find_crc(uncompress_buf, uncompress_len);
301 trl = ((uint64_t) uncompress_len << 32) | crc;
302
303 if (crc != inflate_crc || trl != gzip_trl)
304 ret = INCORRECT_GZIP_TRAILER;
305
306 return ret;
307 }
308
309 int inflate_stateless_pass(uint8_t * compress_buf, uint64_t compress_len,
310 uint8_t * uncompress_buf, uint32_t * uncompress_len,
311 uint32_t gzip_flag)
312 {
313 struct inflate_state state;
314 int ret = 0;
315
316 state.next_in = compress_buf;
317 state.avail_in = compress_len;
318 state.next_out = uncompress_buf;
319 state.avail_out = *uncompress_len;
320 state.crc_flag = gzip_flag;
321
322 ret = isal_inflate_stateless(&state);
323
324 *uncompress_len = state.total_out;
325
326 if (gzip_flag) {
327 if (!ret)
328 ret =
329 check_gzip_trl(*(uint64_t *) state.next_in, state.crc,
330 uncompress_buf, *uncompress_len);
331 state.avail_in -= 8;
332 }
333
334 if (ret == 0 && state.avail_in != 0)
335 ret = INFLATE_LEFTOVER_INPUT;
336
337 return ret;
338 }
339
340 int inflate_multi_pass(uint8_t * compress_buf, uint64_t compress_len,
341 uint8_t * uncompress_buf, uint32_t * uncompress_len, uint32_t gzip_flag)
342 {
343 struct inflate_state *state = NULL;
344 int ret = 0;
345 uint8_t *comp_tmp = NULL, *uncomp_tmp = NULL;
346 uint32_t comp_tmp_size = 0, uncomp_tmp_size = 0;
347 uint32_t comp_processed = 0, uncomp_processed = 0;
348 int32_t read_in_old = 0;
349
350 state = malloc(sizeof(struct inflate_state));
351 if (state == NULL) {
352 printf("Failed to allocate memory\n");
353 exit(0);
354 }
355
356 isal_inflate_init(state);
357
358 state->next_in = NULL;
359 state->next_out = NULL;
360 state->avail_in = 0;
361 state->avail_out = 0;
362 state->crc_flag = gzip_flag;
363
364 if (gzip_flag)
365 compress_len -= 8;
366
367 while (1) {
368 if (state->avail_in == 0) {
369 comp_tmp_size = rand() % (compress_len + 1);
370
371 if (comp_tmp_size >= compress_len - comp_processed)
372 comp_tmp_size = compress_len - comp_processed;
373
374 if (comp_tmp_size != 0) {
375 if (comp_tmp != NULL) {
376 free(comp_tmp);
377 comp_tmp = NULL;
378 }
379
380 comp_tmp = malloc(comp_tmp_size);
381
382 if (comp_tmp == NULL) {
383 printf("Failed to allocate memory\n");
384 return MALLOC_FAILED;
385 }
386
387 memcpy(comp_tmp, compress_buf + comp_processed, comp_tmp_size);
388 comp_processed += comp_tmp_size;
389
390 state->next_in = comp_tmp;
391 state->avail_in = comp_tmp_size;
392 }
393 }
394
395 if (state->avail_out == 0) {
396 /* Save uncompressed data into uncompress_buf */
397 if (uncomp_tmp != NULL) {
398 memcpy(uncompress_buf + uncomp_processed, uncomp_tmp,
399 uncomp_tmp_size);
400 uncomp_processed += uncomp_tmp_size;
401 }
402
403 uncomp_tmp_size = rand() % (*uncompress_len + 1);
404
405 /* Limit size of buffer to be smaller than maximum */
406 if (uncomp_tmp_size > *uncompress_len - uncomp_processed)
407 uncomp_tmp_size = *uncompress_len - uncomp_processed;
408
409 if (uncomp_tmp_size != 0) {
410
411 if (uncomp_tmp != NULL) {
412 fflush(0);
413 free(uncomp_tmp);
414 uncomp_tmp = NULL;
415 }
416
417 uncomp_tmp = malloc(uncomp_tmp_size);
418 if (uncomp_tmp == NULL) {
419 printf("Failed to allocate memory\n");
420 return MALLOC_FAILED;
421 }
422
423 state->avail_out = uncomp_tmp_size;
424 state->next_out = uncomp_tmp;
425 }
426 }
427
428 ret = isal_inflate(state);
429
430 if (state->block_state == ISAL_BLOCK_FINISH || ret != 0) {
431 memcpy(uncompress_buf + uncomp_processed, uncomp_tmp, uncomp_tmp_size);
432 *uncompress_len = state->total_out;
433 break;
434 }
435
436 if (*uncompress_len - uncomp_processed == 0 && state->avail_out == 0
437 && state->tmp_out_valid - state->tmp_out_processed > 0) {
438 ret = ISAL_OUT_OVERFLOW;
439 break;
440 }
441
442 if (compress_len - comp_processed == 0 && state->avail_in == 0
443 && (state->block_state != ISAL_BLOCK_INPUT_DONE)
444 && state->tmp_out_valid - state->tmp_out_processed == 0) {
445 if (state->read_in_length == read_in_old) {
446 ret = ISAL_END_INPUT;
447 break;
448 }
449 read_in_old = state->read_in_length;
450 }
451 }
452
453 if (gzip_flag) {
454 if (!ret)
455 ret =
456 check_gzip_trl(*(uint64_t *) & compress_buf[compress_len],
457 state->crc, uncompress_buf, *uncompress_len);
458 }
459 if (ret == 0 && state->avail_in != 0)
460 ret = INFLATE_LEFTOVER_INPUT;
461
462 if (comp_tmp != NULL) {
463 free(comp_tmp);
464 comp_tmp = NULL;
465 }
466
467 if (uncomp_tmp != NULL) {
468 free(uncomp_tmp);
469 uncomp_tmp = NULL;
470 }
471
472 free(state);
473 return ret;
474 }
475
476 /* Inflate the compressed data and check that the decompressed data agrees with the input data */
477 int inflate_check(uint8_t * z_buf, int z_size, uint8_t * in_buf, int in_size,
478 uint32_t gzip_flag)
479 {
480 /* Test inflate with reference inflate */
481
482 int ret = 0;
483 uint32_t test_size = in_size;
484 uint8_t *test_buf = NULL;
485 int mem_result = 0;
486 int gzip_hdr_result = 0, gzip_trl_result = 0;
487
488 if (in_size > 0) {
489 assert(in_buf != NULL);
490 test_buf = malloc(test_size);
491 if (test_buf == NULL)
492 return MALLOC_FAILED;
493 }
494
495 if (test_buf != NULL)
496 memset(test_buf, 0xff, test_size);
497
498 if (gzip_flag == IGZIP_GZIP) {
499 gzip_hdr_result = check_gzip_header(z_buf);
500 z_buf += gzip_hdr_bytes;
501 z_size -= gzip_hdr_bytes;
502 }
503
504 if (inflate_type == 0) {
505 ret = inflate_stateless_pass(z_buf, z_size, test_buf, &test_size, gzip_flag);
506 inflate_type = 1;
507 } else {
508 ret = inflate_multi_pass(z_buf, z_size, test_buf, &test_size, gzip_flag);
509 inflate_type = 0;
510 }
511
512 if (test_buf != NULL)
513 mem_result = memcmp(in_buf, test_buf, in_size);
514
515 #ifdef VERBOSE
516 int i;
517 if (mem_result)
518 for (i = 0; i < in_size; i++) {
519 if (in_buf[i] != test_buf[i]) {
520 printf
521 ("First incorrect data at 0x%x of 0x%x, 0x%x != 0x%x\n",
522 i, in_size, in_buf[i], test_buf[i]);
523 break;
524 }
525 }
526 #endif
527
528 if (test_buf != NULL)
529 free(test_buf);
530 switch (ret) {
531 case 0:
532 break;
533 case ISAL_END_INPUT:
534 return INFLATE_END_OF_INPUT;
535 break;
536 case ISAL_INVALID_BLOCK:
537 return INFLATE_INVALID_BLOCK_HEADER;
538 break;
539 case ISAL_INVALID_SYMBOL:
540 return INFLATE_INVALID_SYMBOL;
541 break;
542 case ISAL_OUT_OVERFLOW:
543 return INFLATE_OUT_BUFFER_OVERFLOW;
544 break;
545 case ISAL_INVALID_LOOKBACK:
546 return INFLATE_INVALID_LOOK_BACK_DISTANCE;
547 break;
548 case INFLATE_LEFTOVER_INPUT:
549 return INFLATE_LEFTOVER_INPUT;
550 break;
551 case INCORRECT_GZIP_TRAILER:
552 gzip_trl_result = INCORRECT_GZIP_TRAILER;
553 break;
554
555 default:
556 return INFLATE_GENERAL_ERROR;
557 break;
558 }
559
560 if (test_size != in_size)
561 return INFLATE_INCORRECT_OUTPUT_SIZE;
562
563 if (mem_result)
564 return RESULT_ERROR;
565
566 if (gzip_flag) {
567 if (gzip_hdr_result)
568 return INVALID_GZIP_HEADER;
569
570 if (gzip_trl_result)
571 return INCORRECT_GZIP_TRAILER;
572 }
573
574 return 0;
575 }
576
577 /* Check if that the state of the data stream is consistent */
578 int stream_valid_check(struct isal_zstream *stream, 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 total_in, in_buffer_size, total_out, out_buffer_size;
583
584 total_in =
585 (in_size ==
586 0) ? in_processed : (in_processed - in_size) + (stream->next_in - in_buf);
587 in_buffer_size = (in_size == 0) ? 0 : stream->next_in - in_buf + stream->avail_in;
588
589 /* Check for a consistent amount of data processed */
590 if (total_in != stream->total_in || in_buffer_size != in_size)
591 return COMPRESS_INPUT_STREAM_INTEGRITY_ERROR;
592
593 total_out =
594 (out_size == 0) ? out_processed : out_processed + (stream->next_out - out_buf);
595 out_buffer_size = (out_size == 0) ? 0 : stream->next_out - out_buf + stream->avail_out;
596
597 /* Check for a consistent amount of data compressed */
598 if (total_out != stream->total_out || out_buffer_size != out_size) {
599 return COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR;
600 }
601
602 return 0;
603 }
604
605 /* Performs compression with checks to discover and verify the state of the
606 * stream
607 * stream: compress data structure which has been initialized to use
608 * in_buf and out_buf as the buffers
609 * data_size: size of all input data
610 * compressed_size: size of all available output buffers
611 * in_buf: next buffer of data to be compressed
612 * in_size: size of in_buf
613 * out_buf: next out put buffer where data is stored
614 * out_size: size of out_buf
615 * in_processed: the amount of input data which has been loaded into buffers
616 * to be compressed, this includes the data in in_buf
617 * out_processed: the amount of output data which has been compressed and stored,
618 * this does not include the data in the current out_buf
619 */
620 int isal_deflate_with_checks(struct isal_zstream *stream, uint32_t data_size,
621 uint32_t compressed_size, uint8_t * in_buf, uint32_t in_size,
622 uint32_t in_processed, uint8_t * out_buf, uint32_t out_size,
623 uint32_t out_processed)
624 {
625 int ret, stream_check;
626 struct isal_zstate *state = &stream->internal_state;
627
628 #ifdef VERBOSE
629 printf("Pre compression\n");
630 printf
631 ("data_size = 0x%05x, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x, total_in = 0x%05x\n",
632 data_size, in_processed, in_size, stream->avail_in, stream->total_in);
633 printf
634 ("compressed_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n",
635 compressed_size, out_processed, out_size, stream->avail_out, stream->total_out);
636 #endif
637
638 ret = isal_deflate(stream);
639
640 #ifdef VERBOSE
641 printf("Post compression\n");
642 printf
643 ("data_size = 0x%05x, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x, total_in = 0x%05x\n",
644 data_size, in_processed, in_size, stream->avail_in, stream->total_in);
645 printf
646 ("compressed_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n",
647 compressed_size, out_processed, out_size, stream->avail_out, stream->total_out);
648 printf("\n\n");
649 #endif
650
651 /* Verify the stream is in a valid state */
652 stream_check = stream_valid_check(stream, in_buf, in_size, out_buf, out_size,
653 in_processed, out_processed, data_size);
654
655 if (stream_check != 0)
656 return stream_check;
657
658 if (ret != IGZIP_COMP_OK)
659 return COMPRESS_GENERAL_ERROR;
660
661 /* Check if the compression is completed */
662 if (state->state != ZSTATE_END)
663 if (compressed_size - out_processed - (out_size - stream->avail_out) <= 0)
664 return COMPRESS_OUT_BUFFER_OVERFLOW;
665
666 return ret;
667
668 }
669
670 void set_random_hufftable(struct isal_zstream *stream)
671 {
672 isal_deflate_set_hufftables(stream, hufftables, rand() % 4);
673 }
674
675 /* Compress the input data into the output buffer where the input buffer and
676 * output buffer are randomly segmented to test state information for the
677 * compression*/
678 int compress_multi_pass(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
679 uint32_t * compressed_size, uint32_t flush_type, uint32_t gzip_flag,
680 uint32_t level)
681 {
682 int ret = IGZIP_COMP_OK;
683 uint8_t *in_buf = NULL, *out_buf = NULL;
684 uint32_t in_size = 0, out_size = 0;
685 uint32_t in_processed = 0, out_processed = 0;
686 struct isal_zstream stream;
687 struct isal_zstate *state = &stream.internal_state;
688 uint32_t loop_count = 0;
689 uint32_t level_buf_size;
690 uint8_t *level_buf = NULL;
691
692 #ifdef VERBOSE
693 printf("Starting Compress Multi Pass\n");
694 #endif
695
696 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
697
698 isal_deflate_init(&stream);
699
700 if (state->state != ZSTATE_NEW_HDR)
701 return COMPRESS_INCORRECT_STATE;
702
703 stream.flush = flush_type;
704 stream.end_of_stream = 0;
705
706 /* These are set here to allow the loop to run correctly */
707 stream.avail_in = 0;
708 stream.avail_out = 0;
709 stream.gzip_flag = gzip_flag;
710 stream.level = level;
711
712 if (level >= 1) {
713 level_buf_size = rand() % IBUF_SIZE + ISAL_DEF_LVL1_MIN;
714 level_buf = malloc(level_buf_size);
715 create_rand_repeat_data(level_buf, level_buf_size);
716 stream.level_buf = level_buf;
717 stream.level_buf_size = level_buf_size;
718 }
719
720 while (1) {
721 loop_count++;
722
723 /* Setup in buffer for next round of compression */
724 if (stream.avail_in == 0) {
725 if (flush_type == NO_FLUSH || state->state == ZSTATE_NEW_HDR) {
726 /* Randomly choose size of the next out buffer */
727 in_size = rand() % (data_size + 1);
728
729 /* Limit size of buffer to be smaller than maximum */
730 if (in_size >= data_size - in_processed) {
731 in_size = data_size - in_processed;
732 stream.end_of_stream = 1;
733 }
734
735 if (in_size != 0) {
736 if (in_buf != NULL) {
737 free(in_buf);
738 in_buf = NULL;
739 }
740
741 in_buf = malloc(in_size);
742 if (in_buf == NULL) {
743 ret = MALLOC_FAILED;
744 break;
745 }
746 memcpy(in_buf, data + in_processed, in_size);
747 in_processed += in_size;
748
749 stream.avail_in = in_size;
750 stream.next_in = in_buf;
751 }
752 }
753 }
754
755 /* Setup out buffer for next round of compression */
756 if (stream.avail_out == 0) {
757 /* Save compressed data inot compressed_buf */
758 if (out_buf != NULL) {
759 memcpy(compressed_buf + out_processed, out_buf,
760 out_size - stream.avail_out);
761 out_processed += out_size - stream.avail_out;
762 }
763
764 /* Randomly choose size of the next out buffer */
765 out_size = rand() % (*compressed_size + 1);
766
767 /* Limit size of buffer to be smaller than maximum */
768 if (out_size > *compressed_size - out_processed)
769 out_size = *compressed_size - out_processed;
770
771 if (out_size != 0) {
772 if (out_buf != NULL) {
773 free(out_buf);
774 out_buf = NULL;
775 }
776
777 out_buf = malloc(out_size);
778 if (out_buf == NULL) {
779 ret = MALLOC_FAILED;
780 break;
781 }
782
783 stream.avail_out = out_size;
784 stream.next_out = out_buf;
785 }
786 }
787
788 if (state->state == ZSTATE_NEW_HDR)
789 set_random_hufftable(&stream);
790
791 ret =
792 isal_deflate_with_checks(&stream, data_size, *compressed_size, in_buf,
793 in_size, in_processed, out_buf, out_size,
794 out_processed);
795
796 if (ret) {
797 if (ret == COMPRESS_OUT_BUFFER_OVERFLOW
798 || ret == COMPRESS_INCORRECT_STATE)
799 memcpy(compressed_buf + out_processed, out_buf, out_size);
800 break;
801 }
802
803 /* Check if the compression is completed */
804 if (state->state == ZSTATE_END) {
805 memcpy(compressed_buf + out_processed, out_buf, out_size);
806 *compressed_size = stream.total_out;
807 break;
808 }
809
810 }
811
812 if (level_buf != NULL)
813 free(level_buf);
814 if (in_buf != NULL)
815 free(in_buf);
816 if (out_buf != NULL)
817 free(out_buf);
818
819 if (ret == COMPRESS_OUT_BUFFER_OVERFLOW && flush_type == SYNC_FLUSH
820 && loop_count >= MAX_LOOPS)
821 ret = COMPRESS_LOOP_COUNT_OVERFLOW;
822
823 return ret;
824
825 }
826
827 /* Compress the input data into the outbuffer in one call to isal_deflate */
828 int compress_single_pass(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
829 uint32_t * compressed_size, uint32_t flush_type, uint32_t gzip_flag,
830 uint32_t level)
831 {
832 int ret = IGZIP_COMP_OK;
833 struct isal_zstream stream;
834 struct isal_zstate *state = &stream.internal_state;
835 uint32_t level_buf_size;
836 uint8_t *level_buf = NULL;
837
838 #ifdef VERBOSE
839 printf("Starting Compress Single Pass\n");
840 #endif
841
842 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
843
844 isal_deflate_init(&stream);
845
846 set_random_hufftable(&stream);
847
848 if (state->state != ZSTATE_NEW_HDR)
849 return COMPRESS_INCORRECT_STATE;
850
851 stream.flush = flush_type;
852 stream.avail_in = data_size;
853 stream.next_in = data;
854 stream.avail_out = *compressed_size;
855 stream.next_out = compressed_buf;
856 stream.end_of_stream = 1;
857 stream.gzip_flag = gzip_flag;
858 stream.level = level;
859
860 if (level >= 1) {
861 level_buf_size = rand() % IBUF_SIZE + ISAL_DEF_LVL1_MIN;
862 level_buf = malloc(level_buf_size);
863 create_rand_repeat_data(level_buf, level_buf_size);
864 stream.level_buf = level_buf;
865 stream.level_buf_size = level_buf_size;
866 }
867
868 ret =
869 isal_deflate_with_checks(&stream, data_size, *compressed_size, data, data_size,
870 data_size, compressed_buf, *compressed_size, 0);
871
872 if (level_buf != NULL)
873 free(level_buf);
874
875 /* Check if the compression is completed */
876 if (state->state == ZSTATE_END)
877 *compressed_size = stream.total_out;
878 else if (flush_type == SYNC_FLUSH && stream.avail_out < 16)
879 ret = COMPRESS_OUT_BUFFER_OVERFLOW;
880
881 return ret;
882
883 }
884
885 /* Statelessly compress the input buffer into the output buffer */
886 int compress_stateless(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
887 uint32_t * compressed_size, uint32_t flush_type, uint32_t gzip_flag,
888 uint32_t level)
889 {
890 int ret = IGZIP_COMP_OK;
891 struct isal_zstream stream;
892 uint32_t level_buf_size;
893 uint8_t *level_buf = NULL;
894
895 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
896
897 isal_deflate_stateless_init(&stream);
898
899 set_random_hufftable(&stream);
900
901 stream.avail_in = data_size;
902 stream.next_in = data;
903 stream.flush = flush_type;
904 if (flush_type != NO_FLUSH)
905 stream.end_of_stream = 1;
906 stream.avail_out = *compressed_size;
907 stream.next_out = compressed_buf;
908 stream.gzip_flag = gzip_flag;
909 stream.level = level;
910
911 if (level >= 1) {
912 level_buf_size = rand() % IBUF_SIZE;
913 if (level_buf_size >= ISAL_DEF_LVL1_MIN) {
914 level_buf = malloc(level_buf_size);
915 create_rand_repeat_data(level_buf, level_buf_size);
916 stream.level_buf = level_buf;
917 stream.level_buf_size = level_buf_size;
918 }
919 }
920
921 ret = isal_deflate_stateless(&stream);
922
923 if (level_buf != NULL)
924 free(level_buf);
925
926 /* verify the stream */
927 if (stream.next_in - data != stream.total_in ||
928 stream.total_in + stream.avail_in != data_size)
929 return COMPRESS_INPUT_STREAM_INTEGRITY_ERROR;
930
931 if (stream.next_out - compressed_buf != stream.total_out ||
932 stream.total_out + stream.avail_out != *compressed_size) {
933 return COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR;
934 }
935
936 if (ret != IGZIP_COMP_OK) {
937 if (ret == STATELESS_OVERFLOW)
938 return COMPRESS_OUT_BUFFER_OVERFLOW;
939 else if (ret == INVALID_FLUSH)
940 return INVALID_FLUSH_ERROR;
941 else
942 return COMPRESS_GENERAL_ERROR;
943 }
944
945 if (!stream.end_of_stream) {
946 return COMPRESS_END_OF_STREAM_NOT_SET;
947 }
948
949 if (stream.avail_in != 0)
950 return COMPRESS_ALL_INPUT_FAIL;
951
952 *compressed_size = stream.total_out;
953
954 return ret;
955
956 }
957
958 /* Statelessly compress the input buffer into the output buffer */
959 int compress_stateless_full_flush(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
960 uint32_t * compressed_size, uint32_t level)
961 {
962 int ret = IGZIP_COMP_OK;
963 uint8_t *in_buf = NULL, *level_buf = NULL, *out_buf = compressed_buf;
964 uint32_t in_size = 0, level_buf_size;
965 uint32_t in_processed = 00;
966 struct isal_zstream stream;
967 uint32_t loop_count = 0;
968
969 #ifdef VERBOSE
970 printf("Starting Stateless Compress Full Flush\n");
971 #endif
972
973 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
974
975 isal_deflate_stateless_init(&stream);
976
977 stream.flush = FULL_FLUSH;
978 stream.end_of_stream = 0;
979 stream.avail_out = *compressed_size;
980 stream.next_out = compressed_buf;
981 stream.level = level;
982
983 if (level >= 1) {
984 level_buf_size = rand() % IBUF_SIZE;
985 if (level_buf_size >= ISAL_DEF_LVL1_MIN) {
986 level_buf = malloc(level_buf_size);
987 create_rand_repeat_data(level_buf, level_buf_size);
988 stream.level_buf = level_buf;
989 stream.level_buf_size = level_buf_size;
990 }
991 }
992
993 while (1) {
994 loop_count++;
995
996 /* Randomly choose size of the next out buffer */
997 in_size = rand() % (data_size + 1);
998
999 /* Limit size of buffer to be smaller than maximum */
1000 if (in_size >= data_size - in_processed) {
1001 in_size = data_size - in_processed;
1002 stream.end_of_stream = 1;
1003 }
1004
1005 stream.avail_in = in_size;
1006
1007 if (in_size != 0) {
1008 if (in_buf != NULL) {
1009 free(in_buf);
1010 in_buf = NULL;
1011 }
1012
1013 in_buf = malloc(in_size);
1014 if (in_buf == NULL) {
1015 ret = MALLOC_FAILED;
1016 break;
1017 }
1018 memcpy(in_buf, data + in_processed, in_size);
1019 in_processed += in_size;
1020
1021 stream.next_in = in_buf;
1022 }
1023
1024 out_buf = stream.next_out;
1025
1026 if (stream.internal_state.state == ZSTATE_NEW_HDR)
1027 set_random_hufftable(&stream);
1028
1029 ret = isal_deflate_stateless(&stream);
1030 assert(stream.internal_state.bitbuf.m_bit_count == 0);
1031
1032 assert(compressed_buf == stream.next_out - stream.total_out);
1033 if (ret)
1034 break;
1035
1036 /* Verify that blocks are independent */
1037 ret = inflate_check(out_buf, stream.next_out - out_buf, in_buf, in_size, 0);
1038
1039 if (ret == INFLATE_INVALID_LOOK_BACK_DISTANCE) {
1040 break;
1041 } else
1042 ret = 0;
1043
1044 /* Check if the compression is completed */
1045 if (in_processed == data_size) {
1046 *compressed_size = stream.total_out;
1047 break;
1048 }
1049
1050 }
1051
1052 if (level_buf != NULL)
1053 free(level_buf);
1054
1055 if (in_buf != NULL)
1056 free(in_buf);
1057
1058 if (ret == STATELESS_OVERFLOW && loop_count >= MAX_LOOPS)
1059 ret = COMPRESS_LOOP_COUNT_OVERFLOW;
1060
1061 return ret;
1062
1063 }
1064
1065 /* Compress the input data into the output buffer where the input buffer and
1066 * is randomly segmented to test for independence of blocks in full flush
1067 * compression*/
1068 int compress_full_flush(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
1069 uint32_t * compressed_size, uint32_t gzip_flag, uint32_t level)
1070 {
1071 int ret = IGZIP_COMP_OK;
1072 uint8_t *in_buf = NULL, *out_buf = compressed_buf, *level_buf = NULL;
1073 uint32_t in_size = 0, level_buf_size;
1074 uint32_t in_processed = 00;
1075 struct isal_zstream stream;
1076 struct isal_zstate *state = &stream.internal_state;
1077 uint32_t loop_count = 0;
1078
1079 #ifdef VERBOSE
1080 printf("Starting Compress Full Flush\n");
1081 #endif
1082
1083 create_rand_repeat_data((uint8_t *) & stream, sizeof(stream));
1084
1085 isal_deflate_init(&stream);
1086
1087 if (state->state != ZSTATE_NEW_HDR)
1088 return COMPRESS_INCORRECT_STATE;
1089
1090 stream.flush = FULL_FLUSH;
1091 stream.end_of_stream = 0;
1092 stream.avail_out = *compressed_size;
1093 stream.next_out = compressed_buf;
1094 stream.total_out = 0;
1095 stream.gzip_flag = gzip_flag;
1096 stream.level = level;
1097
1098 if (level >= 1) {
1099 level_buf_size = rand() % IBUF_SIZE + ISAL_DEF_LVL1_MIN;
1100 if (level_buf_size >= ISAL_DEF_LVL1_MIN) {
1101 level_buf = malloc(level_buf_size);
1102 create_rand_repeat_data(level_buf, level_buf_size);
1103 stream.level_buf = level_buf;
1104 stream.level_buf_size = level_buf_size;
1105 }
1106 }
1107
1108 while (1) {
1109 loop_count++;
1110
1111 /* Setup in buffer for next round of compression */
1112 if (state->state == ZSTATE_NEW_HDR) {
1113 /* Randomly choose size of the next out buffer */
1114 in_size = rand() % (data_size + 1);
1115
1116 /* Limit size of buffer to be smaller than maximum */
1117 if (in_size >= data_size - in_processed) {
1118 in_size = data_size - in_processed;
1119 stream.end_of_stream = 1;
1120 }
1121
1122 stream.avail_in = in_size;
1123
1124 if (in_size != 0) {
1125 if (in_buf != NULL) {
1126 free(in_buf);
1127 in_buf = NULL;
1128 }
1129
1130 in_buf = malloc(in_size);
1131 if (in_buf == NULL) {
1132 ret = MALLOC_FAILED;
1133 break;
1134 }
1135 memcpy(in_buf, data + in_processed, in_size);
1136 in_processed += in_size;
1137
1138 stream.next_in = in_buf;
1139 }
1140
1141 out_buf = stream.next_out;
1142 }
1143
1144 if (state->state == ZSTATE_NEW_HDR)
1145 set_random_hufftable(&stream);
1146
1147 ret = isal_deflate(&stream);
1148
1149 if (ret)
1150 break;
1151
1152 /* Verify that blocks are independent */
1153 if (state->state == ZSTATE_NEW_HDR || state->state == ZSTATE_END) {
1154 ret =
1155 inflate_check(out_buf, stream.next_out - out_buf, in_buf, in_size,
1156 0);
1157
1158 if (ret == INFLATE_INVALID_LOOK_BACK_DISTANCE)
1159 break;
1160 else
1161 ret = 0;
1162 }
1163
1164 /* Check if the compression is completed */
1165 if (state->state == ZSTATE_END) {
1166 *compressed_size = stream.total_out;
1167 break;
1168 }
1169
1170 }
1171
1172 if (level_buf != NULL)
1173 free(level_buf);
1174
1175 if (in_buf != NULL)
1176 free(in_buf);
1177
1178 if (ret == COMPRESS_OUT_BUFFER_OVERFLOW && loop_count >= MAX_LOOPS)
1179 ret = COMPRESS_LOOP_COUNT_OVERFLOW;
1180
1181 return ret;
1182
1183 }
1184
1185 /*Compress the input buffer into the output buffer, but switch the flush type in
1186 * the middle of the compression to test what happens*/
1187 int compress_swap_flush(uint8_t * data, uint32_t data_size, uint8_t * compressed_buf,
1188 uint32_t * compressed_size, uint32_t flush_type, uint32_t gzip_flag)
1189 {
1190 int ret = IGZIP_COMP_OK;
1191 struct isal_zstream stream;
1192 struct isal_zstate *state = &stream.internal_state;
1193 uint32_t partial_size;
1194
1195 #ifdef VERBOSE
1196 printf("Starting Compress Swap Flush\n");
1197 #endif
1198
1199 isal_deflate_init(&stream);
1200
1201 set_random_hufftable(&stream);
1202
1203 if (state->state != ZSTATE_NEW_HDR)
1204 return COMPRESS_INCORRECT_STATE;
1205
1206 partial_size = rand() % (data_size + 1);
1207
1208 stream.flush = flush_type;
1209 stream.avail_in = partial_size;
1210 stream.next_in = data;
1211 stream.avail_out = *compressed_size;
1212 stream.next_out = compressed_buf;
1213 stream.end_of_stream = 0;
1214 stream.gzip_flag = gzip_flag;
1215
1216 ret =
1217 isal_deflate_with_checks(&stream, data_size, *compressed_size, data, partial_size,
1218 partial_size, compressed_buf, *compressed_size, 0);
1219
1220 if (ret)
1221 return ret;
1222
1223 if (state->state == ZSTATE_NEW_HDR)
1224 set_random_hufftable(&stream);
1225
1226 flush_type = rand() % 3;
1227
1228 stream.flush = flush_type;
1229 stream.avail_in = data_size - partial_size;
1230 stream.next_in = data + partial_size;
1231 stream.end_of_stream = 1;
1232
1233 ret =
1234 isal_deflate_with_checks(&stream, data_size, *compressed_size, data + partial_size,
1235 data_size - partial_size, data_size, compressed_buf,
1236 *compressed_size, 0);
1237
1238 if (ret == COMPRESS_GENERAL_ERROR)
1239 return INVALID_FLUSH_ERROR;
1240
1241 *compressed_size = stream.total_out;
1242
1243 return ret;
1244 }
1245
1246 /* Test deflate_stateless */
1247 int test_compress_stateless(uint8_t * in_data, uint32_t in_size, uint32_t flush_type)
1248 {
1249 int ret = IGZIP_COMP_OK;
1250 uint32_t z_size, overflow, gzip_flag, level;
1251 uint8_t *z_buf = NULL;
1252 uint8_t *in_buf = NULL;
1253
1254 gzip_flag = rand() % 3;
1255 level = rand() % 2;
1256
1257 if (in_size != 0) {
1258 in_buf = malloc(in_size);
1259
1260 if (in_buf == NULL)
1261 return MALLOC_FAILED;
1262
1263 memcpy(in_buf, in_data, in_size);
1264 }
1265
1266 /* Test non-overflow case where a type 0 block is not written */
1267 z_size = 2 * in_size + hdr_bytes;
1268 if (gzip_flag)
1269 z_size += gzip_extra_bytes;
1270
1271 z_buf = malloc(z_size);
1272
1273 if (z_buf == NULL)
1274 return MALLOC_FAILED;
1275
1276 create_rand_repeat_data(z_buf, z_size);
1277
1278 /* If flush type is invalid */
1279 if (flush_type != NO_FLUSH && flush_type != FULL_FLUSH) {
1280 ret =
1281 compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag,
1282 level);
1283
1284 if (ret != INVALID_FLUSH_ERROR)
1285 print_error(ret);
1286 else
1287 ret = 0;
1288
1289 if (z_buf != NULL)
1290 free(z_buf);
1291
1292 if (in_buf != NULL)
1293 free(in_buf);
1294
1295 return ret;
1296 }
1297
1298 /* Else test valid flush type */
1299 ret =
1300 compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag, level);
1301
1302 if (!ret)
1303 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1304
1305 #ifdef VERBOSE
1306 if (ret) {
1307 printf("Compressed array at level %d with gzip flag %d: ", level, gzip_flag);
1308 print_uint8_t(z_buf, z_size);
1309 printf("\n");
1310 printf("Data: ");
1311 print_uint8_t(in_buf, in_size);
1312 }
1313 #endif
1314 if (z_buf != NULL) {
1315 free(z_buf);
1316 z_buf = NULL;
1317 }
1318
1319 print_error(ret);
1320 if (ret)
1321 return ret;
1322
1323 /*Test non-overflow case where a type 0 block is possible to be written */
1324 z_size = TYPE0_HDR_SIZE * ((in_size + TYPE0_MAX_SIZE - 1) / TYPE0_MAX_SIZE) + in_size;
1325 if (gzip_flag)
1326 z_size += gzip_extra_bytes;
1327
1328 if (z_size == gzip_extra_bytes)
1329 z_size += TYPE0_HDR_SIZE;
1330
1331 if (z_size < 8)
1332 z_size = 8;
1333
1334 z_buf = malloc(z_size);
1335
1336 if (z_buf == NULL)
1337 return MALLOC_FAILED;
1338
1339 create_rand_repeat_data(z_buf, z_size);
1340
1341 ret =
1342 compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag, level);
1343 if (!ret)
1344 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1345 #ifdef VERBOSE
1346 if (ret) {
1347 printf("Compressed array at level %d with gzip flag %d: ", level, gzip_flag);
1348 print_uint8_t(z_buf, z_size);
1349 printf("\n");
1350 printf("Data: ");
1351 print_uint8_t(in_buf, in_size);
1352 }
1353 #endif
1354
1355 if (!ret) {
1356 free(z_buf);
1357 z_buf = NULL;
1358
1359 /* Test random overflow case */
1360 z_size = rand() % z_size;
1361
1362 if (z_size > in_size)
1363 z_size = rand() & in_size;
1364
1365 if (z_size > 0) {
1366 z_buf = malloc(z_size);
1367
1368 if (z_buf == NULL)
1369 return MALLOC_FAILED;
1370 }
1371
1372 overflow =
1373 compress_stateless(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag,
1374 level);
1375
1376 if (overflow != COMPRESS_OUT_BUFFER_OVERFLOW) {
1377 #ifdef VERBOSE
1378 printf("overflow error = %d\n", overflow);
1379 print_error(overflow);
1380 if (overflow == 0) {
1381 overflow =
1382 inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1383 printf("inflate ret = %d\n", overflow);
1384 print_error(overflow);
1385 }
1386 printf("Compressed array at level %d with gzip flag %d: ", level,
1387 gzip_flag);
1388 print_uint8_t(z_buf, z_size);
1389 printf("\n");
1390 printf("Data: ");
1391 print_uint8_t(in_buf, in_size);
1392 #endif
1393 }
1394 }
1395
1396 print_error(ret);
1397 if (ret) {
1398 if (z_buf != NULL) {
1399 free(z_buf);
1400 z_buf = NULL;
1401 }
1402 if (in_buf != NULL)
1403 free(in_buf);
1404 return ret;
1405 }
1406
1407 if (flush_type == FULL_FLUSH) {
1408 if (z_buf != NULL)
1409 free(z_buf);
1410
1411 z_size = 2 * in_size + MAX_LOOPS * (hdr_bytes + 5);
1412
1413 z_buf = malloc(z_size);
1414
1415 if (z_buf == NULL)
1416 return MALLOC_FAILED;
1417
1418 create_rand_repeat_data(z_buf, z_size);
1419
1420 /* Else test valid flush type */
1421 ret = compress_stateless_full_flush(in_buf, in_size, z_buf, &z_size, level);
1422
1423 if (!ret)
1424 ret = inflate_check(z_buf, z_size, in_buf, in_size, 0);
1425 else if (ret == COMPRESS_LOOP_COUNT_OVERFLOW)
1426 ret = 0;
1427
1428 print_error(ret);
1429 #ifdef VERBOSE
1430 if (ret) {
1431 printf("Compressed array at level %d with gzip flag %d: ", level,
1432 gzip_flag);
1433 print_uint8_t(z_buf, z_size);
1434 printf("\n");
1435 printf("Data: ");
1436 print_uint8_t(in_buf, in_size);
1437 }
1438 #endif
1439 }
1440 if (z_buf != NULL)
1441 free(z_buf);
1442
1443 if (in_buf != NULL)
1444 free(in_buf);
1445
1446 return ret;
1447 }
1448
1449 /* Test deflate */
1450 int test_compress(uint8_t * in_buf, uint32_t in_size, uint32_t flush_type)
1451 {
1452 int ret = IGZIP_COMP_OK, fin_ret = IGZIP_COMP_OK;
1453 uint32_t overflow = 0, gzip_flag, level;
1454 uint32_t z_size, z_size_max, z_compressed_size;
1455 uint8_t *z_buf = NULL;
1456
1457 /* Test a non overflow case */
1458 if (flush_type == NO_FLUSH)
1459 z_size_max = 2 * in_size + hdr_bytes + 2;
1460 else if (flush_type == SYNC_FLUSH || flush_type == FULL_FLUSH)
1461 z_size_max = 2 * in_size + MAX_LOOPS * (hdr_bytes + 5);
1462 else {
1463 printf("Invalid Flush Parameter\n");
1464 return COMPRESS_GENERAL_ERROR;
1465 }
1466
1467 gzip_flag = rand() % 3;
1468 level = rand() % 2;
1469 if (gzip_flag)
1470 z_size_max += gzip_extra_bytes;
1471
1472 z_size = z_size_max;
1473
1474 z_buf = malloc(z_size);
1475 if (z_buf == NULL) {
1476 print_error(MALLOC_FAILED);
1477 return MALLOC_FAILED;
1478 }
1479 create_rand_repeat_data(z_buf, z_size);
1480
1481 ret = compress_single_pass(in_buf, in_size, z_buf, &z_size, flush_type,
1482 gzip_flag, level);
1483
1484 if (!ret)
1485 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1486
1487 if (ret) {
1488 #ifdef VERBOSE
1489 printf("Compressed array at level %d with gzip flag %d: ", level, gzip_flag);
1490 print_uint8_t(z_buf, z_size);
1491 printf("\n");
1492 printf("Data: ");
1493 print_uint8_t(in_buf, in_size);
1494 #endif
1495 printf("Failed on compress single pass\n");
1496 print_error(ret);
1497 }
1498
1499 fin_ret |= ret;
1500
1501 z_compressed_size = z_size;
1502 z_size = z_size_max;
1503 create_rand_repeat_data(z_buf, z_size_max);
1504
1505 ret =
1506 compress_multi_pass(in_buf, in_size, z_buf, &z_size, flush_type, gzip_flag, level);
1507
1508 if (!ret)
1509 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1510
1511 if (ret) {
1512 #ifdef VERBOSE
1513 printf("Compressed array at level %d with gzip flag %d: ", level, gzip_flag);
1514 print_uint8_t(z_buf, z_size);
1515 printf("\n");
1516 printf("Data: ");
1517 print_uint8_t(in_buf, in_size);
1518 #endif
1519 printf("Failed on compress multi pass\n");
1520 print_error(ret);
1521 }
1522
1523 fin_ret |= ret;
1524
1525 ret = 0;
1526
1527 /* Test random overflow case */
1528 if (flush_type == SYNC_FLUSH && z_compressed_size > in_size)
1529 z_compressed_size = in_size + 1;
1530
1531 z_size = rand() % z_compressed_size;
1532 create_rand_repeat_data(z_buf, z_size_max);
1533
1534 overflow = compress_single_pass(in_buf, in_size, z_buf, &z_size, flush_type,
1535 gzip_flag, level);
1536
1537 if (overflow != COMPRESS_OUT_BUFFER_OVERFLOW) {
1538 if (overflow == 0)
1539 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1540
1541 /* Rarely single pass overflow will compresses data
1542 * better than the initial run. This is to stop that
1543 * case from erroring. */
1544 if (overflow != 0 || ret != 0) {
1545 #ifdef VERBOSE
1546 printf("overflow error = %d\n", overflow);
1547 print_error(overflow);
1548 printf("inflate ret = %d\n", ret);
1549 print_error(overflow);
1550
1551 printf("Compressed array at level %d with gzip flag %d: ", level,
1552 gzip_flag);
1553 print_uint8_t(z_buf, z_size);
1554 printf("\n");
1555 printf("Data: ");
1556 print_uint8_t(in_buf, in_size);
1557 #endif
1558 printf("Failed on compress multi pass overflow\n");
1559 print_error(ret);
1560 ret = OVERFLOW_TEST_ERROR;
1561 }
1562 }
1563
1564 fin_ret |= ret;
1565
1566 if (flush_type == NO_FLUSH) {
1567 create_rand_repeat_data(z_buf, z_size_max);
1568
1569 overflow =
1570 compress_multi_pass(in_buf, in_size, z_buf, &z_size, flush_type,
1571 gzip_flag, level);
1572
1573 if (overflow != COMPRESS_OUT_BUFFER_OVERFLOW) {
1574 if (overflow == 0)
1575 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1576
1577 /* Rarely multi pass overflow will compresses data
1578 * better than the initial run. This is to stop that
1579 * case from erroring */
1580 if (overflow != 0 || ret != 0) {
1581 #ifdef VERBOSE
1582 printf("overflow error = %d\n", overflow);
1583 print_error(overflow);
1584 printf("inflate ret = %d\n", ret);
1585 print_error(overflow);
1586
1587 printf("Compressed array at level %d with gzip flag %d: ",
1588 level, gzip_flag);
1589 print_uint8_t(z_buf, z_size);
1590 printf("\n");
1591 printf("Data: ");
1592 print_uint8_t(in_buf, in_size);
1593 #endif
1594 printf("Failed on compress multi pass overflow\n");
1595 print_error(ret);
1596 ret = OVERFLOW_TEST_ERROR;
1597 }
1598 }
1599 fin_ret |= ret;
1600 }
1601
1602 free(z_buf);
1603
1604 return fin_ret;
1605 }
1606
1607 /* Test swapping flush types in the middle of compression */
1608 int test_flush(uint8_t * in_buf, uint32_t in_size)
1609 {
1610 int fin_ret = IGZIP_COMP_OK, ret;
1611 uint32_t z_size, flush_type = 0, gzip_flag, level;
1612 uint8_t *z_buf = NULL;
1613
1614 gzip_flag = rand() % 3;
1615 level = rand() % 2;
1616
1617 z_size = 2 * in_size + 2 * hdr_bytes + 8;
1618 if (gzip_flag)
1619 z_size += gzip_extra_bytes;
1620 z_buf = malloc(z_size);
1621
1622 if (z_buf == NULL)
1623 return MALLOC_FAILED;
1624
1625 create_rand_repeat_data(z_buf, z_size);
1626
1627 while (flush_type < 3)
1628 flush_type = rand();
1629
1630 /* Test invalid flush */
1631 ret = compress_single_pass(in_buf, in_size, z_buf, &z_size, flush_type,
1632 gzip_flag, level);
1633
1634 if (ret == COMPRESS_GENERAL_ERROR)
1635 ret = 0;
1636 else {
1637 printf("Failed when passing invalid flush parameter\n");
1638 ret = INVALID_FLUSH_ERROR;
1639 }
1640
1641 fin_ret |= ret;
1642 print_error(ret);
1643
1644 create_rand_repeat_data(z_buf, z_size);
1645
1646 /* Test swapping flush type */
1647 ret = compress_swap_flush(in_buf, in_size, z_buf, &z_size, rand() % 3, gzip_flag);
1648
1649 if (!ret)
1650 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1651
1652 if (ret) {
1653 #ifdef VERBOSE
1654 printf("Compressed array at level %d with gzip flag %d: ", level, gzip_flag);
1655 print_uint8_t(z_buf, z_size);
1656 printf("\n");
1657 printf("Data: ");
1658 print_uint8_t(in_buf, in_size);
1659 #endif
1660 printf("Failed on swapping flush type\n");
1661 print_error(ret);
1662 }
1663
1664 fin_ret |= ret;
1665 print_error(ret);
1666
1667 return fin_ret;
1668 }
1669
1670 /* Test there are no length distance pairs across full flushes */
1671 int test_full_flush(uint8_t * in_buf, uint32_t in_size)
1672 {
1673 int ret = IGZIP_COMP_OK;
1674 uint32_t z_size, gzip_flag, level;
1675 uint8_t *z_buf = NULL;
1676
1677 gzip_flag = rand() % 3;
1678 level = rand() % 2;
1679 z_size = 2 * in_size + MAX_LOOPS * (hdr_bytes + 5);
1680
1681 if (gzip_flag)
1682 z_size += gzip_extra_bytes;
1683
1684 z_buf = malloc(z_size);
1685 if (z_buf == NULL) {
1686 print_error(MALLOC_FAILED);
1687 return MALLOC_FAILED;
1688 }
1689
1690 create_rand_repeat_data(z_buf, z_size);
1691
1692 ret = compress_full_flush(in_buf, in_size, z_buf, &z_size, gzip_flag, level);
1693
1694 if (!ret)
1695 ret = inflate_check(z_buf, z_size, in_buf, in_size, gzip_flag);
1696
1697 if (ret) {
1698 #ifdef VERBOSE
1699 printf("Compressed array: ");
1700 print_uint8_t(z_buf, z_size);
1701 printf("\n");
1702 printf("Data: ");
1703 print_uint8_t(in_buf, in_size);
1704 #endif
1705 printf("Failed on compress multi pass\n");
1706 print_error(ret);
1707 }
1708
1709 free(z_buf);
1710
1711 return ret;
1712 }
1713
1714 int test_inflate(struct vect_result *in_vector)
1715 {
1716 int ret = IGZIP_COMP_OK;
1717 uint8_t *compress_buf = in_vector->vector, *out_buf = NULL;
1718 uint64_t compress_len = in_vector->vector_length;
1719 uint32_t out_size = 0;
1720
1721 out_size = 10 * in_vector->vector_length;
1722 out_buf = malloc(out_size);
1723 if (out_buf == NULL)
1724 return MALLOC_FAILED;
1725
1726 ret = inflate_stateless_pass(compress_buf, compress_len, out_buf, &out_size, 0);
1727
1728 if (ret == INFLATE_LEFTOVER_INPUT)
1729 ret = ISAL_DECOMP_OK;
1730
1731 if (ret != in_vector->expected_error)
1732 printf("Inflate return value incorrect, %d != %d\n", ret,
1733 in_vector->expected_error);
1734 else
1735 ret = IGZIP_COMP_OK;
1736
1737 if (!ret) {
1738 ret = inflate_multi_pass(compress_buf, compress_len, out_buf, &out_size, 0);
1739
1740 if (ret == INFLATE_LEFTOVER_INPUT)
1741 ret = ISAL_DECOMP_OK;
1742
1743 if (ret != in_vector->expected_error)
1744 printf("Inflate return value incorrect, %d != %d\n", ret,
1745 in_vector->expected_error);
1746 else
1747 ret = IGZIP_COMP_OK;
1748 }
1749
1750 return ret;
1751
1752 }
1753
1754 int get_filesize(FILE * f)
1755 {
1756 int curr, end;
1757
1758 curr = ftell(f); /* Save current position */
1759 fseek(f, 0L, SEEK_END);
1760 end = ftell(f);
1761 fseek(f, curr, SEEK_SET); /* Restore position */
1762 return end;
1763 }
1764
1765 /* Run multiple compression tests on data stored in a file */
1766 int test_compress_file(char *file_name)
1767 {
1768 int ret = IGZIP_COMP_OK;
1769 uint32_t in_size;
1770 uint8_t *in_buf = NULL;
1771 FILE *in_file = NULL;
1772
1773 in_file = fopen(file_name, "rb");
1774 if (!in_file)
1775 return FILE_READ_FAILED;
1776
1777 in_size = get_filesize(in_file);
1778 if (in_size != 0) {
1779 in_buf = malloc(in_size);
1780 if (in_buf == NULL)
1781 return MALLOC_FAILED;
1782 fread(in_buf, 1, in_size, in_file);
1783 }
1784
1785 ret |= test_compress_stateless(in_buf, in_size, NO_FLUSH);
1786 ret |= test_compress_stateless(in_buf, in_size, SYNC_FLUSH);
1787 ret |= test_compress_stateless(in_buf, in_size, FULL_FLUSH);
1788 ret |= test_compress(in_buf, in_size, NO_FLUSH);
1789 ret |= test_compress(in_buf, in_size, SYNC_FLUSH);
1790 ret |= test_compress(in_buf, in_size, FULL_FLUSH);
1791 ret |= test_flush(in_buf, in_size);
1792
1793 if (ret)
1794 printf("Failed on file %s\n", file_name);
1795
1796 if (in_buf != NULL)
1797 free(in_buf);
1798
1799 return ret;
1800 }
1801
1802 int create_custom_hufftables(struct isal_hufftables *hufftables_custom, int argc, char *argv[])
1803 {
1804 long int file_length;
1805 uint8_t *stream = NULL;
1806 struct isal_huff_histogram histogram;
1807 FILE *file;
1808
1809 memset(&histogram, 0, sizeof(histogram));
1810
1811 while (argc > 1) {
1812 printf("Processing %s\n", argv[argc - 1]);
1813 file = fopen(argv[argc - 1], "r");
1814 if (file == NULL) {
1815 printf("Error opening file\n");
1816 return 1;
1817 }
1818 fseek(file, 0, SEEK_END);
1819 file_length = ftell(file);
1820 fseek(file, 0, SEEK_SET);
1821 file_length -= ftell(file);
1822
1823 if (file_length > 0) {
1824 stream = malloc(file_length);
1825 if (stream == NULL) {
1826 printf("Failed to allocate memory to read in file\n");
1827 fclose(file);
1828 return 1;
1829 }
1830 }
1831
1832 fread(stream, 1, file_length, file);
1833
1834 if (ferror(file)) {
1835 printf("Error occurred when reading file\n");
1836 fclose(file);
1837 free(stream);
1838 stream = NULL;
1839 return 1;
1840 }
1841
1842 /* Create a histogram of frequency of symbols found in stream to
1843 * generate the huffman tree.*/
1844 isal_update_histogram(stream, file_length, &histogram);
1845
1846 fclose(file);
1847 if (stream != NULL) {
1848 free(stream);
1849 stream = NULL;
1850 }
1851 argc--;
1852 }
1853
1854 return isal_create_hufftables(hufftables_custom, &histogram);
1855
1856 }
1857
1858 int main(int argc, char *argv[])
1859 {
1860 int i = 0, ret = 0, fin_ret = 0;
1861 uint32_t in_size = 0, offset = 0;
1862 uint8_t *in_buf;
1863 struct isal_hufftables hufftables_custom;
1864
1865 #ifndef VERBOSE
1866 setbuf(stdout, NULL);
1867 #endif
1868
1869 printf("Window Size: %d K\n", IGZIP_HIST_SIZE / 1024);
1870 printf("Test Seed : %d\n", TEST_SEED);
1871 printf("Randoms : %d\n", RANDOMS);
1872 srand(TEST_SEED);
1873
1874 if (argc > 1) {
1875 ret = create_custom_hufftables(&hufftables_custom, argc, argv);
1876 if (ret == 0)
1877 hufftables = &hufftables_custom;
1878 else {
1879 printf("Failed to generate custom hufftable");
1880 return -1;
1881 }
1882 }
1883
1884 in_buf = malloc(IBUF_SIZE);
1885 memset(in_buf, 0, IBUF_SIZE);
1886
1887 if (in_buf == NULL) {
1888 fprintf(stderr, "Can't allocate in_buf memory\n");
1889 return -1;
1890 }
1891
1892 if (argc > 1) {
1893 printf("igzip_rand_test files: ");
1894
1895 for (i = 1; i < argc; i++) {
1896 ret |= test_compress_file(argv[i]);
1897 if (ret)
1898 return ret;
1899 }
1900
1901 printf("................");
1902 printf("%s\n", ret ? "Fail" : "Pass");
1903 fin_ret |= ret;
1904 }
1905
1906 printf("igzip_rand_test stateless: ");
1907
1908 ret = test_compress_stateless((uint8_t *) str1, sizeof(str1), NO_FLUSH);
1909 if (ret)
1910 return ret;
1911
1912 ret |= test_compress_stateless((uint8_t *) str2, sizeof(str2), NO_FLUSH);
1913 if (ret)
1914 return ret;
1915
1916 for (i = 0; i < RANDOMS; i++) {
1917 in_size = rand() % (IBUF_SIZE + 1);
1918 offset = rand() % (IBUF_SIZE + 1 - in_size);
1919 in_buf += offset;
1920
1921 create_rand_repeat_data(in_buf, in_size);
1922
1923 ret |= test_compress_stateless(in_buf, in_size, NO_FLUSH);
1924
1925 in_buf -= offset;
1926
1927 if (i % (RANDOMS / 16) == 0)
1928 printf(".");
1929
1930 if (ret)
1931 return ret;
1932 }
1933
1934 for (i = 0; i < RANDOMS / 16; i++) {
1935 create_rand_repeat_data(in_buf, PAGE_SIZE);
1936 ret |= test_compress_stateless(in_buf, PAGE_SIZE, NO_FLUSH); // good for efence
1937 if (ret)
1938 return ret;
1939 }
1940
1941 fin_ret |= ret;
1942
1943 ret = test_compress_stateless((uint8_t *) str1, sizeof(str1), SYNC_FLUSH);
1944 if (ret)
1945 return ret;
1946
1947 ret |= test_compress_stateless((uint8_t *) str2, sizeof(str2), SYNC_FLUSH);
1948 if (ret)
1949 return ret;
1950
1951 for (i = 0; i < 16; i++) {
1952 in_size = rand() % (IBUF_SIZE + 1);
1953 offset = rand() % (IBUF_SIZE + 1 - in_size);
1954 in_buf += offset;
1955
1956 create_rand_repeat_data(in_buf, in_size);
1957
1958 ret |= test_compress_stateless(in_buf, in_size, SYNC_FLUSH);
1959
1960 in_buf -= offset;
1961
1962 if (ret)
1963 return ret;
1964 }
1965
1966 fin_ret |= ret;
1967
1968 printf("%s\n", ret ? "Fail" : "Pass");
1969
1970 printf("igzip_rand_test stateless FULL_FLUSH: ");
1971
1972 ret = test_compress_stateless((uint8_t *) str1, sizeof(str1), FULL_FLUSH);
1973 if (ret)
1974 return ret;
1975
1976 ret |= test_compress_stateless((uint8_t *) str2, sizeof(str2), FULL_FLUSH);
1977 if (ret)
1978 return ret;
1979
1980 for (i = 0; i < RANDOMS; i++) {
1981 in_size = rand() % (IBUF_SIZE + 1);
1982 offset = rand() % (IBUF_SIZE + 1 - in_size);
1983 in_buf += offset;
1984
1985 create_rand_repeat_data(in_buf, in_size);
1986
1987 ret |= test_compress_stateless(in_buf, in_size, FULL_FLUSH);
1988
1989 in_buf -= offset;
1990
1991 if (i % (RANDOMS / 16) == 0)
1992 printf(".");
1993
1994 if (ret)
1995 return ret;
1996 }
1997
1998 for (i = 0; i < RANDOMS / 16; i++) {
1999 create_rand_repeat_data(in_buf, PAGE_SIZE);
2000 ret |= test_compress_stateless(in_buf, PAGE_SIZE, FULL_FLUSH); // good for efence
2001 if (ret)
2002 return ret;
2003 }
2004 fin_ret |= ret;
2005
2006 printf("%s\n", ret ? "Fail" : "Pass");
2007
2008 printf("igzip_rand_test stateful NO_FLUSH: ");
2009
2010 ret = test_compress((uint8_t *) str1, sizeof(str1), NO_FLUSH);
2011 if (ret)
2012 return ret;
2013
2014 ret |= test_compress((uint8_t *) str2, sizeof(str2), NO_FLUSH);
2015 if (ret)
2016 return ret;
2017
2018 for (i = 0; i < RANDOMS; i++) {
2019 in_size = rand() % (IBUF_SIZE + 1);
2020 offset = rand() % (IBUF_SIZE + 1 - in_size);
2021 in_buf += offset;
2022
2023 create_rand_repeat_data(in_buf, in_size);
2024
2025 ret |= test_compress(in_buf, in_size, NO_FLUSH);
2026
2027 in_buf -= offset;
2028
2029 if (i % (RANDOMS / 16) == 0)
2030 printf(".");
2031 if (ret)
2032 return ret;
2033 }
2034
2035 fin_ret |= ret;
2036
2037 printf("%s\n", ret ? "Fail" : "Pass");
2038
2039 printf("igzip_rand_test stateful SYNC_FLUSH: ");
2040
2041 ret = test_compress((uint8_t *) str1, sizeof(str1), SYNC_FLUSH);
2042 if (ret)
2043 return ret;
2044
2045 ret |= test_compress((uint8_t *) str2, sizeof(str2), SYNC_FLUSH);
2046 if (ret)
2047 return ret;
2048
2049 for (i = 0; i < RANDOMS; i++) {
2050 in_size = rand() % (IBUF_SIZE + 1);
2051 offset = rand() % (IBUF_SIZE + 1 - in_size);
2052 in_buf += offset;
2053
2054 create_rand_repeat_data(in_buf, in_size);
2055
2056 ret |= test_compress(in_buf, in_size, SYNC_FLUSH);
2057
2058 in_buf -= offset;
2059
2060 if (i % (RANDOMS / 16) == 0)
2061 printf(".");
2062 if (ret)
2063 return ret;
2064 }
2065
2066 fin_ret |= ret;
2067
2068 printf("%s\n", ret ? "Fail" : "Pass");
2069
2070 printf("igzip_rand_test stateful FULL_FLUSH: ");
2071
2072 ret = test_compress((uint8_t *) str1, sizeof(str1), FULL_FLUSH);
2073 if (ret)
2074 return ret;
2075
2076 ret |= test_compress((uint8_t *) str2, sizeof(str2), FULL_FLUSH);
2077 if (ret)
2078 return ret;
2079
2080 for (i = 0; i < RANDOMS; i++) {
2081 in_size = rand() % (IBUF_SIZE + 1);
2082 offset = rand() % (IBUF_SIZE + 1 - in_size);
2083 in_buf += offset;
2084
2085 create_rand_repeat_data(in_buf, in_size);
2086
2087 ret |= test_compress(in_buf, in_size, FULL_FLUSH);
2088
2089 in_buf -= offset;
2090
2091 if (i % (RANDOMS / 16) == 0)
2092 printf(".");
2093 if (ret)
2094 return ret;
2095 }
2096
2097 for (i = 0; i < RANDOMS / 8; i++) {
2098 in_size = rand() % (IBUF_SIZE + 1);
2099 offset = rand() % (IBUF_SIZE + 1 - in_size);
2100 in_buf += offset;
2101
2102 create_rand_repeat_data(in_buf, in_size);
2103
2104 ret |= test_full_flush(in_buf, in_size);
2105
2106 in_buf -= offset;
2107
2108 if (ret)
2109 return ret;
2110 }
2111
2112 fin_ret |= ret;
2113
2114 printf("%s\n", ret ? "Fail" : "Pass");
2115
2116 printf("igzip_rand_test stateful Change Flush: ");
2117
2118 ret = test_flush((uint8_t *) str1, sizeof(str1));
2119 if (ret)
2120 return ret;
2121
2122 ret |= test_flush((uint8_t *) str2, sizeof(str2));
2123 if (ret)
2124 return ret;
2125
2126 for (i = 0; i < RANDOMS / 4; i++) {
2127 in_size = rand() % (IBUF_SIZE + 1);
2128 offset = rand() % (IBUF_SIZE + 1 - in_size);
2129 in_buf += offset;
2130
2131 create_rand_repeat_data(in_buf, in_size);
2132
2133 ret |= test_flush(in_buf, in_size);
2134
2135 in_buf -= offset;
2136
2137 if (i % ((RANDOMS / 4) / 16) == 0)
2138 printf(".");
2139 if (ret)
2140 return ret;
2141 }
2142
2143 fin_ret |= ret;
2144
2145 printf("%s\n", ret ? "Fail" : "Pass");
2146
2147 printf("igzip_rand_test inflate Std Vectors: ");
2148
2149 for (i = 0; i < sizeof(std_vect_array) / sizeof(struct vect_result); i++) {
2150 ret = test_inflate(&std_vect_array[i]);
2151 if (ret)
2152 return ret;
2153 }
2154 printf("................");
2155 printf("%s\n", ret ? "Fail" : "Pass");
2156
2157 printf("igzip rand test finished: %s\n",
2158 fin_ret ? "Some tests failed" : "All tests passed");
2159
2160 return fin_ret != IGZIP_COMP_OK;
2161 }