xref: /isa-l/igzip/igzip_inflate_test.c (revision 09a5a243bfec3ba0945a2bc7bfdb07e1d105e6af)
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 "inflate.h"
34 #include "huff_codes.h"
35 
36 #define OUT_BUFFER_SLOP 16
37 
38 /*Don't use file larger memory can support because compression and decompression
39  * are done in a stateless manner. */
40 #define MAX_INPUT_FILE_SIZE 2L*1024L*1024L*1024L
41 
42 int test(uint8_t * compressed_stream, uint64_t * compressed_length,
43 	 uint8_t * uncompressed_stream, int uncompressed_length,
44 	 uint8_t * uncompressed_test_stream, int uncompressed_test_stream_length)
45 {
46 	struct inflate_state state;
47 	int ret;
48 	ret =
49 	    compress2(compressed_stream, compressed_length, uncompressed_stream,
50 		      uncompressed_length, 9);
51 	if (ret) {
52 		printf("Failed compressing input with exit code %d", ret);
53 		return ret;
54 	}
55 
56 	isal_inflate_init(&state, compressed_stream + 2, *compressed_length - 2,
57 			  uncompressed_test_stream, uncompressed_test_stream_length);
58 	ret = isal_inflate_stateless(&state);
59 
60 	switch (ret) {
61 	case 0:
62 		break;
63 	case END_OF_INPUT:
64 		printf(" did not decompress all input\n");
65 		return END_OF_INPUT;
66 		break;
67 	case INVALID_BLOCK_HEADER:
68 		printf("  invalid header\n");
69 		return INVALID_BLOCK_HEADER;
70 		break;
71 	case INVALID_SYMBOL:
72 		printf(" invalid symbol\n");
73 		return INVALID_SYMBOL;
74 		break;
75 	case OUT_BUFFER_OVERFLOW:
76 		printf(" out buffer overflow\n");
77 		return OUT_BUFFER_OVERFLOW;
78 		break;
79 	case INVALID_NON_COMPRESSED_BLOCK_LENGTH:
80 		printf("Invalid length bits in non-compressed block\n");
81 		return INVALID_NON_COMPRESSED_BLOCK_LENGTH;
82 		break;
83 	case INVALID_LOOK_BACK_DISTANCE:
84 		printf("Invalid lookback distance");
85 		return INVALID_LOOK_BACK_DISTANCE;
86 		break;
87 	default:
88 		printf(" error\n");
89 		return -1;
90 		break;
91 	}
92 
93 	if (state.out_buffer.total_out != uncompressed_length) {
94 		printf("incorrect amount of data was decompressed from compressed data\n");
95 		printf("%d decompressed of %d compressed", state.out_buffer.total_out,
96 		       uncompressed_length);
97 		return -1;
98 	}
99 	if (memcmp(uncompressed_stream, uncompressed_test_stream, uncompressed_length)) {
100 		int i;
101 		for (i = 0; i < uncompressed_length; i++) {
102 			if (uncompressed_stream[i] != uncompressed_test_stream[i]) {
103 				printf("first error at %d, 0x%x != 0x%x\n", i,
104 				       uncompressed_stream[i], uncompressed_test_stream[i]);
105 			}
106 		}
107 		printf(" decompressed data is not the same as the compressed data\n");
108 		return -1;
109 	}
110 	return 0;
111 }
112 
113 int main(int argc, char **argv)
114 {
115 	int i, j, ret = 0, fin_ret = 0;
116 	FILE *file = NULL;
117 	uint64_t compressed_length, file_length;
118 	uint64_t uncompressed_length, uncompressed_test_stream_length;
119 	uint8_t *uncompressed_stream = NULL;
120 	uint8_t *compressed_stream = NULL;
121 	uint8_t *uncompressed_test_stream = NULL;
122 
123 	if (argc == 1)
124 		printf("Error, no input file\n");
125 
126 	for (i = 1; i < argc; i++) {
127 		file = fopen(argv[i], "r");
128 		if (file == NULL) {
129 			printf("Error opening file %s\n", argv[i]);
130 			return 1;
131 		} else
132 			printf("Starting file %s", argv[i]);
133 
134 		fseek(file, 0, SEEK_END);
135 		file_length = ftell(file);
136 		fseek(file, 0, SEEK_SET);
137 		file_length -= ftell(file);
138 		if (file_length > MAX_INPUT_FILE_SIZE) {
139 			printf("File too large to run on this test\n");
140 			fclose(file);
141 			continue;
142 		}
143 
144 		compressed_length = compressBound(file_length);
145 		if (file_length != 0) {
146 			uncompressed_stream = malloc(file_length);
147 			uncompressed_test_stream = malloc(file_length);
148 		}
149 		compressed_stream = malloc(compressed_length);
150 
151 		if (uncompressed_stream == NULL && file_length != 0) {
152 			printf("Failed to allocate memory\n");
153 			exit(0);
154 		}
155 
156 		if (compressed_stream == NULL) {
157 			printf("Failed to allocate memory\n");
158 			exit(0);
159 		}
160 
161 		if (uncompressed_test_stream == NULL) {
162 			printf("Failed to allocate memory\n");
163 			exit(0);
164 		}
165 
166 		uncompressed_length = fread(uncompressed_stream, 1, file_length, file);
167 		uncompressed_test_stream_length = uncompressed_length + OUT_BUFFER_SLOP;
168 
169 		ret =
170 		    test(compressed_stream, &compressed_length, uncompressed_stream,
171 			 uncompressed_length, uncompressed_test_stream,
172 			 uncompressed_test_stream_length);
173 		if (ret) {
174 			for (j = 0; j < compressed_length; j++) {
175 				if ((j & 31) == 0)
176 					printf("\n");
177 				else
178 					printf(" ");
179 				printf("0x%02x,", compressed_stream[j]);
180 
181 			}
182 			printf("\n");
183 
184 		}
185 
186 		fflush(0);
187 		fclose(file);
188 		free(compressed_stream);
189 		if (uncompressed_stream != NULL)
190 			free(uncompressed_stream);
191 		if (uncompressed_test_stream != NULL)
192 			free(uncompressed_test_stream);
193 
194 		if (ret) {
195 			printf(" ... Fail with exit code %d\n", ret);
196 			return ret;
197 		} else
198 			printf(" ... Pass\n");
199 
200 		fin_ret |= ret;
201 	}
202 	return fin_ret;
203 }
204