]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/igzip/igzip_fuzz_inflate.c
update sources to v12.1.1
[ceph.git] / ceph / src / isa-l / igzip / igzip_fuzz_inflate.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <zlib.h>
4 #include "huff_codes.h"
5 #include "igzip_lib.h"
6 #include "test.h"
7
8 #define OUT_BUFFER_SIZE 64*1024
9 int get_filesize(FILE * f)
10 {
11 int curr, end;
12
13 curr = ftell(f); /* Save current position */
14 fseek(f, 0L, SEEK_END);
15 end = ftell(f);
16 fseek(f, curr, SEEK_SET); /* Restore position */
17 return end;
18 }
19
20 int main(int argc, char *argv[])
21 {
22 FILE *in = NULL;
23 unsigned char *in_buf = NULL, *isal_out_buf = NULL, *zlib_out_buf = NULL;
24 int in_file_size, out_buf_size, zret, iret;
25 struct inflate_state *state = NULL;
26 z_stream zstate;
27 char z_msg_invalid_code_set[] = "invalid code lengths set";
28 char z_msg_invalid_dist_set[] = "invalid distances set";
29 char z_msg_invalid_lit_len_set[] = "invalid literal/lengths set";
30
31 if (argc != 2) {
32 fprintf(stderr, "Usage: isal_inflate_file_perf infile\n"
33 "\t - Runs multiple iterations of igzip on a file to "
34 "get more accurate time results.\n");
35 exit(1);
36 }
37 in = fopen(argv[1], "rb");
38 if (!in) {
39 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
40 exit(1);
41 }
42
43 /* Allocate space for entire input file and output
44 * (assuming some possible expansion on output size)
45 */
46 in_file_size = get_filesize(in);
47
48 out_buf_size = OUT_BUFFER_SIZE;
49
50 state = malloc(sizeof(struct inflate_state));
51 in_buf = malloc(in_file_size);
52 isal_out_buf = malloc(OUT_BUFFER_SIZE);
53 zlib_out_buf = malloc(OUT_BUFFER_SIZE);
54
55 if (state == NULL || in_buf == NULL || isal_out_buf == NULL || zlib_out_buf == NULL) {
56 fprintf(stderr, "Failed to malloc input and outputs buffers");
57 exit(1);
58 }
59
60 fread(in_buf, 1, in_file_size, in);
61
62 /* Inflate data with isal_inflate */
63 memset(state, 0xff, sizeof(struct inflate_state));
64
65 isal_inflate_init(state);
66 state->next_in = in_buf;
67 state->avail_in = in_file_size;
68 state->next_out = isal_out_buf;
69 state->avail_out = out_buf_size;
70
71 iret = isal_inflate_stateless(state);
72
73 /* Inflate data with zlib */
74 zstate.zalloc = Z_NULL;
75 zstate.zfree = Z_NULL;
76 zstate.opaque = Z_NULL;
77 zstate.avail_in = in_file_size;
78 zstate.next_in = in_buf;
79 zstate.avail_out = out_buf_size;
80 zstate.next_out = zlib_out_buf;
81 inflateInit2(&zstate, -15);
82
83 zret = inflate(&zstate, Z_FINISH);
84
85 if (zret == Z_STREAM_END) {
86 /* If zlib finished, assert isal finished with the same answer */
87 assert(state->block_state == ISAL_BLOCK_FINISH);
88 assert(zstate.total_out == state->total_out);
89 assert(memcmp(isal_out_buf, zlib_out_buf, state->total_out) == 0);
90 } else if (zret < 0) {
91 if (zret != Z_BUF_ERROR)
92 /* If zlib errors, assert isal errors, excluding a few
93 * cases where zlib is overzealous */
94 assert(iret < 0 || strcmp(zstate.msg, z_msg_invalid_code_set) == 0
95 || strcmp(zstate.msg, z_msg_invalid_dist_set) == 0
96 || strcmp(zstate.msg, z_msg_invalid_lit_len_set) == 0);
97 } else
98 /* If zlib did not finish or error, assert isal did not finish
99 * or that isal found an invalid header since isal notices the
100 * error faster than zlib */
101 assert(iret > 0 || iret == ISAL_INVALID_BLOCK);
102
103 return 0;
104 }