]>
Commit | Line | Data |
---|---|---|
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 | ||
30 | #include <stdint.h> | |
31 | #include <stdio.h> | |
32 | #include <zlib.h> | |
33 | #include "igzip_inflate_ref.h" | |
34 | #include "huff_codes.h" | |
35 | ||
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 | |
39 | ||
40 | int test(uint8_t * compressed_stream, uint64_t * compressed_length, | |
41 | uint8_t * uncompressed_stream, int uncompressed_length, | |
42 | uint8_t * uncompressed_test_stream) | |
43 | { | |
44 | struct inflate_state state; | |
45 | int ret; | |
46 | ret = | |
47 | compress2(compressed_stream, compressed_length, uncompressed_stream, | |
48 | uncompressed_length, 9); | |
49 | if (ret) { | |
50 | printf("Failed compressing input with exit code %d", ret); | |
51 | return ret; | |
52 | } | |
53 | ||
54 | igzip_inflate_init(&state, compressed_stream + 2, *compressed_length - 2, | |
55 | uncompressed_test_stream, uncompressed_length); | |
56 | ret = igzip_inflate(&state); | |
57 | ||
58 | switch (ret) { | |
59 | case 0: | |
60 | break; | |
61 | case END_OF_INPUT: | |
62 | printf(" did not decompress all input\n"); | |
63 | return END_OF_INPUT; | |
64 | break; | |
65 | case INVALID_BLOCK_HEADER: | |
66 | printf(" invalid header\n"); | |
67 | return INVALID_BLOCK_HEADER; | |
68 | break; | |
69 | case INVALID_SYMBOL: | |
70 | printf(" invalid symbol\n"); | |
71 | return INVALID_SYMBOL; | |
72 | break; | |
73 | case OUT_BUFFER_OVERFLOW: | |
74 | printf(" out buffer overflow\n"); | |
75 | return OUT_BUFFER_OVERFLOW; | |
76 | break; | |
77 | case INVALID_NON_COMPRESSED_BLOCK_LENGTH: | |
78 | printf("Invalid length bits in non-compressed block\n"); | |
79 | return INVALID_NON_COMPRESSED_BLOCK_LENGTH; | |
80 | break; | |
81 | case INVALID_LOOK_BACK_DISTANCE: | |
82 | printf("Invalid lookback distance"); | |
83 | return INVALID_LOOK_BACK_DISTANCE; | |
84 | break; | |
85 | default: | |
86 | printf(" error\n"); | |
87 | return -1; | |
88 | break; | |
89 | } | |
90 | ||
91 | if (state.out_buffer.total_out != uncompressed_length) { | |
92 | printf("incorrect amount of data was decompressed from compressed data\n"); | |
93 | printf("%d decompressed of %d compressed", state.out_buffer.total_out, | |
94 | uncompressed_length); | |
95 | return -1; | |
96 | } | |
97 | if (memcmp(uncompressed_stream, uncompressed_test_stream, uncompressed_length)) { | |
98 | printf(" decompressed data is not the same as the compressed data\n"); | |
99 | return -1; | |
100 | } | |
101 | return 0; | |
102 | } | |
103 | ||
104 | int main(int argc, char **argv) | |
105 | { | |
106 | int i, j, ret = 0, fin_ret = 0; | |
107 | FILE *file; | |
108 | uint64_t compressed_length, file_length, uncompressed_length; | |
109 | uint8_t *uncompressed_stream, *compressed_stream, *uncompressed_test_stream; | |
110 | ||
111 | if (argc == 1) | |
112 | printf("Error, no input file\n"); | |
113 | ||
114 | for (i = 1; i < argc; i++) { | |
115 | file = fopen(argv[i], "r"); | |
116 | if (file == NULL) { | |
117 | printf("Error opening file %s\n", argv[i]); | |
118 | return 1; | |
119 | } else | |
120 | printf("Starting file %s", argv[i]); | |
121 | ||
122 | fseek(file, 0, SEEK_END); | |
123 | file_length = ftell(file); | |
124 | fseek(file, 0, SEEK_SET); | |
125 | file_length -= ftell(file); | |
126 | if (file_length > MAX_INPUT_FILE_SIZE) { | |
127 | printf("File too large to run on this test\n"); | |
128 | fclose(file); | |
129 | continue; | |
130 | } | |
131 | compressed_length = compressBound(file_length); | |
132 | uncompressed_stream = malloc(file_length); | |
133 | compressed_stream = malloc(compressed_length); | |
134 | uncompressed_test_stream = malloc(file_length); | |
135 | ||
136 | if (uncompressed_stream == NULL) { | |
137 | printf("Failed to allocate memory\n"); | |
138 | exit(0); | |
139 | } | |
140 | ||
141 | if (compressed_stream == NULL) { | |
142 | printf("Failed to allocate memory\n"); | |
143 | exit(0); | |
144 | } | |
145 | ||
146 | if (uncompressed_test_stream == NULL) { | |
147 | printf("Failed to allocate memory\n"); | |
148 | exit(0); | |
149 | } | |
150 | ||
151 | uncompressed_length = fread(uncompressed_stream, 1, file_length, file); | |
152 | ret = | |
153 | test(compressed_stream, &compressed_length, uncompressed_stream, | |
154 | uncompressed_length, uncompressed_test_stream); | |
155 | if (ret) { | |
156 | for (j = 0; j < compressed_length; j++) { | |
157 | if ((j & 31) == 0) | |
158 | printf("\n"); | |
159 | else | |
160 | printf(" "); | |
161 | printf("0x%02x,", compressed_stream[j]); | |
162 | ||
163 | } | |
164 | printf("\n"); | |
165 | ||
166 | } | |
167 | ||
168 | fclose(file); | |
169 | free(compressed_stream); | |
170 | free(uncompressed_stream); | |
171 | free(uncompressed_test_stream); | |
172 | ||
173 | if (ret) { | |
174 | printf(" ... Fail with exit code %d\n", ret); | |
175 | return ret; | |
176 | } else | |
177 | printf(" ... Pass\n"); | |
178 | ||
179 | fin_ret |= ret; | |
180 | } | |
181 | return fin_ret; | |
182 | } |