1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
33 #include "igzip_lib.h"
34 #include "huff_codes.h"
36 /*Don't use file larger memory can support because compression and decompression
37 * are done in a stateless manner. */
38 #define MAX_INPUT_FILE_SIZE 2L*1024L*1024L*1024L
40 int inflate_multi_pass(uint8_t * compress_buf
, uint64_t compress_len
,
41 uint8_t * uncompress_buf
, uint32_t * uncompress_len
)
43 struct inflate_state
*state
= NULL
;
45 uint8_t *comp_tmp
= NULL
, *uncomp_tmp
= NULL
;
46 uint32_t comp_tmp_size
= 0, uncomp_tmp_size
= 0;
47 uint32_t comp_processed
= 0, uncomp_processed
= 0;
49 state
= malloc(sizeof(struct inflate_state
));
51 printf("Failed to allocate memory\n");
55 isal_inflate_init(state
);
57 state
->next_in
= NULL
;
58 state
->next_out
= NULL
;
63 if (state
->avail_in
== 0) {
64 comp_tmp_size
= rand() % (compress_len
+ 1);
66 if (comp_tmp_size
>= compress_len
- comp_processed
)
67 comp_tmp_size
= compress_len
- comp_processed
;
69 if (comp_tmp_size
!= 0) {
70 if (comp_tmp
!= NULL
) {
75 comp_tmp
= malloc(comp_tmp_size
);
77 if (comp_tmp
== NULL
) {
78 printf("Failed to allocate memory\n");
82 memcpy(comp_tmp
, compress_buf
+ comp_processed
, comp_tmp_size
);
83 comp_processed
+= comp_tmp_size
;
85 state
->next_in
= comp_tmp
;
86 state
->avail_in
= comp_tmp_size
;
90 if (state
->avail_out
== 0) {
91 /* Save uncompressed data into uncompress_buf */
92 if (uncomp_tmp
!= NULL
) {
93 memcpy(uncompress_buf
+ uncomp_processed
, uncomp_tmp
,
95 uncomp_processed
+= uncomp_tmp_size
;
98 uncomp_tmp_size
= rand() % (*uncompress_len
+ 1);
100 /* Limit size of buffer to be smaller than maximum */
101 if (uncomp_tmp_size
> *uncompress_len
- uncomp_processed
)
102 uncomp_tmp_size
= *uncompress_len
- uncomp_processed
;
104 if (uncomp_tmp_size
!= 0) {
106 if (uncomp_tmp
!= NULL
) {
112 uncomp_tmp
= malloc(uncomp_tmp_size
);
113 if (uncomp_tmp
== NULL
) {
114 printf("Failed to allocate memory\n");
118 state
->avail_out
= uncomp_tmp_size
;
119 state
->next_out
= uncomp_tmp
;
123 ret
= isal_inflate(state
);
125 if (state
->block_state
== ISAL_BLOCK_FINISH
|| ret
!= 0) {
126 memcpy(uncompress_buf
+ uncomp_processed
, uncomp_tmp
, uncomp_tmp_size
);
127 *uncompress_len
= state
->total_out
;
132 if (comp_tmp
!= NULL
) {
136 if (uncomp_tmp
!= NULL
) {
145 int test(uint8_t * compressed_stream
,
146 uint64_t * compressed_length
,
147 uint8_t * uncompressed_stream
, uint32_t uncompressed_length
,
148 uint8_t * uncompressed_test_stream
, uint32_t uncompressed_test_stream_length
)
152 compress2(compressed_stream
, compressed_length
,
153 uncompressed_stream
, uncompressed_length
, 6);
155 printf("Failed compressing input with exit code %d", ret
);
160 inflate_multi_pass(compressed_stream
+ 2,
161 *compressed_length
- 2 - 4,
162 uncompressed_test_stream
, &uncompressed_test_stream_length
);
167 printf(" did not decompress all input\n");
168 return ISAL_END_INPUT
;
170 case ISAL_INVALID_BLOCK
:
171 printf(" invalid header\n");
172 return ISAL_INVALID_BLOCK
;
174 case ISAL_INVALID_SYMBOL
:
175 printf(" invalid symbol\n");
176 return ISAL_INVALID_SYMBOL
;
178 case ISAL_OUT_OVERFLOW
:
179 printf(" out buffer overflow\n");
180 return ISAL_OUT_OVERFLOW
;
182 case ISAL_INVALID_LOOKBACK
:
183 printf("Invalid lookback distance");
184 return ISAL_INVALID_LOOKBACK
;
192 if (uncompressed_test_stream_length
!= uncompressed_length
) {
193 printf("incorrect amount of data was decompressed from compressed data\n");
194 printf("%d decompressed of %d compressed",
195 uncompressed_test_stream_length
, uncompressed_length
);
198 if (memcmp(uncompressed_stream
, uncompressed_test_stream
, uncompressed_length
)) {
200 for (i
= 0; i
< uncompressed_length
; i
++) {
201 if (uncompressed_stream
[i
] != uncompressed_test_stream
[i
]) {
202 printf("first error at %d, 0x%x != 0x%x\n", i
,
203 uncompressed_stream
[i
], uncompressed_test_stream
[i
]);
206 printf(" decompressed data is not the same as the compressed data\n");
213 int main(int argc
, char **argv
)
215 int i
, j
, ret
= 0, fin_ret
= 0;
217 uint64_t compressed_length
, file_length
;
218 uint64_t uncompressed_length
, uncompressed_test_stream_length
;
219 uint8_t *uncompressed_stream
= NULL
;
220 uint8_t *compressed_stream
= NULL
;
221 uint8_t *uncompressed_test_stream
= NULL
;
224 printf("Error, no input file\n");
225 for (i
= 1; i
< argc
; i
++) {
226 file
= fopen(argv
[i
], "r");
228 printf("Error opening file %s\n", argv
[i
]);
231 printf("Starting file %s", argv
[i
]);
233 fseek(file
, 0, SEEK_END
);
234 file_length
= ftell(file
);
235 fseek(file
, 0, SEEK_SET
);
236 file_length
-= ftell(file
);
237 if (file_length
> MAX_INPUT_FILE_SIZE
) {
238 printf("File too large to run on this test\n");
243 compressed_length
= compressBound(file_length
);
244 if (file_length
!= 0) {
245 uncompressed_stream
= malloc(file_length
);
246 uncompressed_test_stream
= malloc(file_length
);
248 compressed_stream
= malloc(compressed_length
);
249 if (uncompressed_stream
== NULL
&& file_length
!= 0) {
250 printf("Failed to allocate memory\n");
254 if (compressed_stream
== NULL
) {
255 printf("Failed to allocate memory\n");
259 if (uncompressed_test_stream
== NULL
) {
260 printf("Failed to allocate memory\n");
264 uncompressed_length
= fread(uncompressed_stream
, 1, file_length
, file
);
265 uncompressed_test_stream_length
= uncompressed_length
;
267 test(compressed_stream
, &compressed_length
, uncompressed_stream
,
268 uncompressed_length
, uncompressed_test_stream
,
269 uncompressed_test_stream_length
);
271 for (j
= 0; j
< compressed_length
; j
++) {
276 printf("0x%02x,", compressed_stream
[j
]);
283 free(compressed_stream
);
284 if (uncompressed_stream
!= NULL
)
285 free(uncompressed_stream
);
286 if (uncompressed_test_stream
!= NULL
)
287 free(uncompressed_test_stream
);
289 printf(" ... Fail with exit code %d\n", ret
);
292 printf(" ... Pass\n");