/* Performs compression with checks to discover and verify the state of the * stream * stream: compress data structure which has been initialized to use * in_buf and out_buf as the buffers * data_size: size of all input data * compressed_size: size of all available output buffers * in_buf: next buffer of data to be compressed * in_size: size of in_buf * out_buf: next out put buffer where data is stored * out_size: size of out_buf * in_processed: the amount of input data which has been loaded into buffers * to be compressed, this includes the data in in_buf * out_processed: the amount of output data which has been compressed and stored, * this does not include the data in the current out_buf */ int isal_deflate_with_checks(struct isal_zstream *stream, uint32_t data_size, uint32_t compressed_size, uint8_t * in_buf, uint32_t in_size, uint32_t in_processed, uint8_t * out_buf, uint32_t out_size, uint32_t out_processed) { int ret, stream_check; struct isal_zstate *state = &stream->internal_state; #ifdef VERBOSE printf("Pre compression\n"); printf ("data_size = 0x%05x, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x, total_in = 0x%05x\n", data_size, in_processed, in_size, stream->avail_in, stream->total_in); printf ("compressed_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n", compressed_size, out_processed, out_size, stream->avail_out, stream->total_out); #endif ret = isal_deflate(stream); #ifdef VERBOSE printf("Post compression\n"); printf ("data_size = 0x%05x, in_processed = 0x%05x, in_size = 0x%05x, avail_in = 0x%05x, total_in = 0x%05x\n", data_size, in_processed, in_size, stream->avail_in, stream->total_in); printf ("compressed_size = 0x%05x, out_processed = 0x%05x, out_size = 0x%05x, avail_out = 0x%05x, total_out = 0x%05x\n", compressed_size, out_processed, out_size, stream->avail_out, stream->total_out); printf("\n\n"); #endif /* Verify the stream is in a valid state */ stream_check = stream_valid_check(stream, in_buf, in_size, out_buf, out_size, in_processed, out_processed, data_size); if (stream_check != 0) return stream_check; if (ret != IGZIP_COMP_OK) return COMPRESS_GENERAL_ERROR; /* Check if the compression is completed */ if (state->state != ZSTATE_END) if (compressed_size - out_processed - (out_size - stream->avail_out) <= 0) return COMPRESS_OUT_BUFFER_OVERFLOW; return ret; }
int main(int argc, char *argv[]) { uint8_t inbuf[BUF_SIZE], outbuf[BUF_SIZE]; FILE *in, *out; if (argc != 3) { fprintf(stderr, "Usage: igzip_sync_flush_example infile outfile\n"); exit(0); } in = fopen(argv[1], "rb"); if (!in) { fprintf(stderr, "Can't open %s for reading\n", argv[1]); exit(0); } out = fopen(argv[2], "wb"); if (!out) { fprintf(stderr, "Can't open %s for writing\n", argv[2]); exit(0); } printf("igzip_sync_flush_example\nWindow Size: %d K\n", IGZIP_HIST_SIZE / 1024); fflush(0); isal_deflate_init(&stream); stream.end_of_stream = 0; stream.flush = SYNC_FLUSH; do { if (stream.internal_state.state == ZSTATE_NEW_HDR) { stream.avail_in = (uint32_t) fread(inbuf, 1, BUF_SIZE, in); stream.end_of_stream = feof(in) ? 1 : 0; stream.next_in = inbuf; } do { stream.avail_out = BUF_SIZE; stream.next_out = outbuf; isal_deflate(&stream); fwrite(outbuf, 1, BUF_SIZE - stream.avail_out, out); } while (stream.avail_out == 0); } while (stream.internal_state.state != ZSTATE_END); fclose(out); fclose(in); printf("End of igzip_sync_flush_example\n\n"); return 0; }
int compress_file(void) { FILE *in = NULL, *out = NULL; unsigned char *inbuf = NULL, *outbuf = NULL, *level_buf = NULL; size_t inbuf_size, outbuf_size; int level_size = 0; struct isal_zstream stream; struct isal_gzip_header gz_hdr; int ret, success = 0; char *infile_name = global_options.infile_name, *outfile_name = global_options.outfile_name; char *suffix = global_options.suffix; size_t infile_name_len = global_options.infile_name_len; size_t outfile_name_len = global_options.outfile_name_len; size_t suffix_len = global_options.suffix_len; int level = global_options.level; if (suffix == NULL) { suffix = default_suffixes[0]; suffix_len = default_suffixes_lens[0]; } if (infile_name_len == stdin_file_name_len && memcmp(infile_name, stdin_file_name, infile_name_len) == 0) { infile_name = NULL; infile_name_len = 0; } if (outfile_name == NULL && infile_name != NULL && !global_options.use_stdout) { outfile_name_len = infile_name_len + suffix_len; outfile_name = malloc_safe(outfile_name_len + 1); strcpy(outfile_name, infile_name); strcat(outfile_name, suffix); } open_in_file(&in, infile_name); if (in == NULL) goto compress_file_cleanup; if (infile_name_len != 0 && infile_name_len == outfile_name_len && strncmp(infile_name, outfile_name, infile_name_len) == 0) { log_print(ERROR, "igzip: Error input and output file names must differ\n"); goto compress_file_cleanup; } open_out_file(&out, outfile_name); if (out == NULL) goto compress_file_cleanup; inbuf_size = BLOCK_SIZE; outbuf_size = BLOCK_SIZE; inbuf = malloc_safe(inbuf_size); outbuf = malloc_safe(outbuf_size); level_size = level_size_buf[level]; level_buf = malloc_safe(level_size); isal_gzip_header_init(&gz_hdr); if (global_options.name == NAME_DEFAULT || global_options.name == YES_NAME) { gz_hdr.time = get_posix_filetime(in); gz_hdr.name = infile_name; } gz_hdr.os = UNIX; gz_hdr.name_buf_len = infile_name_len + 1; isal_deflate_init(&stream); stream.avail_in = 0; stream.flush = NO_FLUSH; stream.level = level; stream.level_buf = level_buf; stream.level_buf_size = level_size; stream.gzip_flag = IGZIP_GZIP_NO_HDR; stream.next_out = outbuf; stream.avail_out = outbuf_size; isal_write_gzip_header(&stream, &gz_hdr); do { if (stream.avail_in == 0) { stream.next_in = inbuf; stream.avail_in = fread_safe(stream.next_in, 1, inbuf_size, in, infile_name); stream.end_of_stream = feof(in); } if (stream.next_out == NULL) { stream.next_out = outbuf; stream.avail_out = outbuf_size; } ret = isal_deflate(&stream); if (ret != ISAL_DECOMP_OK) { log_print(ERROR, "igzip: Error encountered while compressing file %s\n", infile_name); goto compress_file_cleanup; } fwrite_safe(outbuf, 1, stream.next_out - outbuf, out, outfile_name); stream.next_out = NULL; } while (!feof(in) || stream.avail_out == 0); success = 1; compress_file_cleanup: if (out != NULL && out != stdout) fclose(out); if (in != NULL && in != stdin) { fclose(in); if (success && global_options.remove) remove(infile_name); } if (global_options.outfile_name == NULL && outfile_name != NULL) free(outfile_name); if (inbuf != NULL) free(inbuf); if (outbuf != NULL) free(outbuf); if (level_buf != NULL) free(level_buf); return (success == 0); }
int main(int argc, char *argv[]) { FILE *in, *out = NULL; unsigned char *inbuf, *outbuf; int i, infile_size, iterations, outbuf_size; if (argc > 3 || argc < 2) { fprintf(stderr, "Usage: igzip_sync_flush_file_perf infile [outfile]\n" "\t - Runs multiple iterations of igzip on a file to get more accurate time results.\n"); exit(0); } in = fopen(argv[1], "rb"); if (!in) { fprintf(stderr, "Can't open %s for reading\n", argv[1]); exit(0); } if (argc > 2) { out = fopen(argv[2], "wb"); if (!out) { fprintf(stderr, "Can't open %s for writing\n", argv[2]); exit(0); } printf("outfile=%s\n", argv[2]); } printf("Window Size: %d K\n", HIST_SIZE); printf("igzip_sync_flush_file_perf: \n"); fflush(0); /* Allocate space for entire input file and * output (assuming 1:1 max output size) */ infile_size = get_filesize(in); if (infile_size != 0) { outbuf_size = infile_size; iterations = RUN_MEM_SIZE / infile_size; } else { outbuf_size = BUF_SIZE; iterations = MIN_TEST_LOOPS; } if (iterations < MIN_TEST_LOOPS) iterations = MIN_TEST_LOOPS; inbuf = malloc(infile_size); if (inbuf == NULL) { fprintf(stderr, "Can't allocate input buffer memory\n"); exit(0); } outbuf = malloc(outbuf_size); if (outbuf == NULL) { fprintf(stderr, "Can't allocate output buffer memory\n"); exit(0); } printf("igzip_sync_flush_file_perf: %s %d iterations\n", argv[1], iterations); /* Read complete input file into buffer */ stream.avail_in = (uint32_t) fread(inbuf, 1, infile_size, in); if (stream.avail_in != infile_size) { fprintf(stderr, "Couldn't fit all of input file into buffer\n"); exit(0); } struct perf start, stop; perf_start(&start); for (i = 0; i < iterations; i++) { isal_deflate_init(&stream); stream.end_of_stream = 0; stream.flush = SYNC_FLUSH; stream.next_in = inbuf; stream.avail_in = infile_size / 2; stream.next_out = outbuf; stream.avail_out = outbuf_size / 2; isal_deflate(&stream); if (infile_size == 0) continue; stream.avail_in = infile_size - infile_size / 2; stream.end_of_stream = 1; stream.next_in = inbuf + stream.total_in; stream.flush = SYNC_FLUSH; stream.avail_out = infile_size - outbuf_size / 2; stream.next_out = outbuf + stream.total_out; isal_deflate(&stream); if (stream.avail_in != 0) break; } perf_stop(&stop); if (stream.avail_in != 0) { fprintf(stderr, "Could not compress all of inbuf\n"); exit(0); } printf(" file %s - in_size=%d out_size=%d iter=%d ratio=%3.1f%%\n", argv[1], infile_size, stream.total_out, i, 100.0 * stream.total_out / infile_size); printf("igzip_file: "); perf_print(stop, start, (long long)infile_size * i); if (argc > 2 && out) { printf("writing %s\n", argv[2]); fwrite(outbuf, 1, stream.total_out, out); fclose(out); } fclose(in); printf("End of igzip_sync_flush_file_perf\n\n"); fflush(0); return 0; }