4 #include "huff_codes.h"
8 #define OUT_BUFFER_SIZE 64*1024
9 int get_filesize(FILE * f
)
13 curr
= ftell(f
); /* Save current position */
14 fseek(f
, 0L, SEEK_END
);
16 fseek(f
, curr
, SEEK_SET
); /* Restore position */
20 int main(int argc
, char *argv
[])
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
;
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";
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");
37 in
= fopen(argv
[1], "rb");
39 fprintf(stderr
, "Can't open %s for reading\n", argv
[1]);
43 /* Allocate space for entire input file and output
44 * (assuming some possible expansion on output size)
46 in_file_size
= get_filesize(in
);
48 out_buf_size
= OUT_BUFFER_SIZE
;
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
);
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");
60 fread(in_buf
, 1, in_file_size
, in
);
62 /* Inflate data with isal_inflate */
63 memset(state
, 0xff, sizeof(struct inflate_state
));
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
;
71 iret
= isal_inflate_stateless(state
);
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);
83 zret
= inflate(&zstate
, Z_FINISH
);
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);
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
);