int alloc_lzo(struct vtun_host *host) { int zlevel = host->zlevel ? host->zlevel : 1; int mem; switch( zlevel ){ case 9: lzo1x_compress = lzo1x_999_compress; mem = LZO1X_999_MEM_COMPRESS; break; default: lzo1x_compress = lzo1x_1_15_compress; mem = LZO1X_1_15_MEM_COMPRESS; break; } if( lzo_init() != LZO_E_OK ){ vtun_syslog(LOG_ERR,"Can't initialize compressor"); return 1; } if( !(zbuf = lfd_alloc(zbuf_size)) ){ vtun_syslog(LOG_ERR,"Can't allocate buffer for the compressor"); return 1; } if( !(wmem = lzo_malloc(mem)) ){ vtun_syslog(LOG_ERR,"Can't allocate buffer for the compressor"); return 1; } vtun_syslog(LOG_INFO, "LZO compression[level %d] initialized", zlevel); return 0; }
compr *lzo_compress(unsigned char *buf, int buflen) { int r; lzo_bytep in; lzo_bytep out; lzo_bytep wrkmem; lzo_uint out_len; compr *retdata; pthread_mutex_lock(&lzo_mutex); retdata = s_malloc(sizeof(compr)); in = (lzo_bytep) buf; out = (lzo_bytep) lzo_malloc(OUT_LEN); wrkmem = (lzo_bytep) lzo_malloc(LZO1A_MEM_COMPRESS); if (in == NULL || out == NULL || wrkmem == NULL) { LFATAL("out of memory\n"); exit(3); } r = lzo1a_compress(in, buflen, out, &out_len, wrkmem); if (r != LZO_E_OK) { /* this should NEVER happen */ LFATAL("internal error - compression failed: %d\n", r); exit(2); } // LDEBUG("Compressed %i bytes to %lu bytes",buflen,(unsigned long)out_len); if ( out_len > buflen ) { retdata->data = s_malloc(buflen+1); memcpy(&retdata->data[1], buf, buflen); retdata->size = buflen+1; retdata->data[0]=0; } else { retdata->data = s_malloc(out_len+1); retdata->size = out_len+1; memcpy(&retdata->data[1], out, out_len); retdata->data[0]='L'; } lzo_free(wrkmem); lzo_free(out); pthread_mutex_unlock(&lzo_mutex); return retdata; }
int main(int argc, char *argv[]) { lzo_bytep buf; lzo_uint step; if (argc >= 2 && strcmp(argv[1],"-v") == 0) opt_verbose = 1; if (lzo_init() != LZO_E_OK) { printf("lzo_init() failed !!!\n"); return 3; } buf = (lzo_bytep) lzo_malloc(2*BLOCK_SIZE + 256); if (buf == NULL) { printf("out of memory\n"); return 2; } #if defined(lzo_uintptr_t) printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (lzo_uintptr_t) buf); #elif defined(__LZO_MMODEL_HUGE) printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) buf); #else printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (size_t) buf); #endif for (step = 1; step <= 65536L; step *= 2) { lzo_bytep block = buf; unsigned long n; unsigned gap; gap = __lzo_align_gap(block, step); block = LZO_PTR_ALIGN_UP(block, step); if (opt_verbose >= 1) printf("STEP %5lu: GAP: %5lu %p %p %5lu\n", (unsigned long) step, (unsigned long) gap, buf, block, (unsigned long) (block - buf)); n = align_test(block, BLOCK_SIZE, step); if (n == 0) return 1; if ((n + 1) * step != BLOCK_SIZE) { printf("error 4: %ld %lu\n", (long)step, n); return 1; } } lzo_free(buf); printf("Alignment test passed.\n"); return 0; }
byte* LZEncoder::getPage() { buffer=encoder->getPage(); if(buffer != NULL){ //startPos = encoder->getStartPos(); //numValsPerPage=encoder->getNumValsPerPage(); //valsize=encoder->getValSize(); bufferSize = encoder->getBufferSize(); }else{ return NULL; } memset(page, 0, PAGE_SIZE); if(lzo_init() != LZO_E_OK) throw UnexpectedException("LZEncoder: Error, lzo initialization error!"); lzo_byte *compressedData = (lzo_bytep) lzo_malloc(bufferSize + bufferSize / 64 + 16 + 3); //new byte[numValsPerPage*valsize + numValsPerPage*valsize / 64 + 16 + 3]; lzo_byte *wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS); int r = 0; r = lzo1x_1_compress((const unsigned char*)buffer, bufferSize, compressedData, &sizeCompressedData, wrkmem); if (r != LZO_E_OK) { throw new UnexpectedException("LZEncoder: compress error!"); } // Writing header for decoder to use *((int*) page)=sizeCompressedData; *((int*) (page+sizeof(int)))=bufferSize; //*((int*) (page+2*sizeof(int)))=startPos; //*((int*) (page+3*sizeof(int)))=valsize; if (sizeCompressedData>PAGE_SIZE-2*sizeof(int)) throw CodingException("LZEncoder: Error, compressed data larger than what we can fit on page"); memcpy(page+2*sizeof(int), compressedData, sizeCompressedData); delete[] compressedData; lzo_free(wrkmem); return page; }
compr *lzo_decompress(unsigned char *buf, int buflen) { int r; lzo_bytep in; lzo_bytep out; lzo_bytep wrkmem; lzo_uint out_len = 0; lzo_uint in_len = 0; compr *retdata; FUNC; pthread_mutex_lock(&lzo_mutex); retdata = s_malloc(sizeof(compr)); in_len = buflen-1; in = (lzo_bytep) &buf[1]; out = (lzo_bytep) lzo_malloc(BLKSIZE); wrkmem = (lzo_bytep) lzo_malloc(LZO1A_MEM_COMPRESS); //FASTER if (in == NULL || out == NULL || wrkmem == NULL) { LFATAL("out of memory\n"); exit(3); } r = lzo1a_decompress(in, in_len, out, &out_len, NULL); if (r != LZO_E_OK) { /* this should NEVER happen */ LFATAL("internal error - decompression failed: %d\n", r); exit(22); } retdata->data = s_malloc(out_len); retdata->size = out_len; memcpy(retdata->data, out, out_len); lzo_free(wrkmem); lzo_free(out); pthread_mutex_unlock(&lzo_mutex); return retdata; }
static void lzo_compress_init(struct compress_context *compctx) { msg(D_INIT_MEDIUM, "LZO compression initializing"); ASSERT(!(compctx->flags & COMP_F_SWAP)); compctx->wu.lzo.wmem_size = LZO_WORKSPACE; int lzo_status = lzo_init(); if (lzo_status != LZO_E_OK) { msg(M_FATAL, "Cannot initialize LZO compression library (lzo_init() returns %d)", lzo_status); } compctx->wu.lzo.wmem = (lzo_voidp) lzo_malloc(compctx->wu.lzo.wmem_size); check_malloc_return(compctx->wu.lzo.wmem); }
int main(int argc, char *argv[]) { lzo_bytep block; lzo_uint block_len; lzo_uint32 adler, crc; if (argc < 0 && argv == NULL) /* avoid warning about unused args */ return 0; if (lzo_init() != LZO_E_OK) { printf("lzo_init() failed !!!\n"); return 4; } /* prepare the block */ block_len = 128 * 1024L; block = (lzo_bytep) lzo_malloc(block_len); if (block == NULL) { printf("out of memory\n"); return 3; } lzo_memset(block, 0, block_len); /* adler32 checksum */ adler = lzo_adler32(0, NULL, 0); adler = lzo_adler32(adler, block, block_len); if (adler != 0x001e0001UL) { printf("adler32 checksum error !!! (0x%08lx)\n", (long) adler); return 2; } /* crc32 checksum */ crc = lzo_crc32(0, NULL, 0); crc = lzo_crc32(crc, block, block_len); if (crc != 0x7ee8cdcdUL) { printf("crc32 checksum error !!! (0x%08lx)\n", (long) crc); return 1; } lzo_free(block); printf("Checksum test passed.\n"); return 0; }
int do_file ( const char *in_name, int level ) { int r; lzo_bytep in; lzo_bytep out; lzo_bytep newb; lzo_bytep wrkmem; lzo_uint in_len; lzo_uint out_len; lzo_uint new_len; long l; FILE *f; /* * Step 1: open the input file */ f = fopen(in_name,"rb"); if (f == 0) { printf("%s: cannot open file %s\n", progname, in_name); return 0; /* no error */ } fseek(f,0,SEEK_END); l = ftell(f); fseek(f,0,SEEK_SET); if (l <= 0) { printf("%s: %s: empty file -- skipping\n", progname, in_name); fclose(f); return 0; } in_len = (lzo_uint) l; /* * Step 2: allocate compression buffers and read the file */ in = (lzo_bytep) lzo_malloc(in_len); out = (lzo_bytep) lzo_malloc(in_len + in_len / 16 + 64 + 3); newb = (lzo_bytep) lzo_malloc(in_len); wrkmem = (lzo_bytep) lzo_malloc(LZO1X_999_MEM_COMPRESS); if (in == NULL || out == NULL || newb == NULL || wrkmem == NULL) { printf("%s: out of memory\n", progname); exit(1); } in_len = (lzo_uint) lzo_fread(f,in,in_len); fclose(f); /* * Step 3: compress from `in' to `out' with LZO1X-999 */ r = lzo1x_999_compress_level(in,in_len,out,&out_len,wrkmem, dict, dict_len, 0, level); if (r != LZO_E_OK) { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); return 1; } print_file(in_name,in_len,out_len); /* * Step 4: decompress again, now going from `out' to `newb' */ new_len = in_len; r = lzo1x_decompress_dict_safe(out,out_len,newb,&new_len,NULL,dict,dict_len); if (r != LZO_E_OK) { /* this should NEVER happen */ printf("internal error - decompression failed: %d\n", r); return 1; } /* * Step 5: verify decompression */ if (new_len != in_len || lzo_memcmp(in,newb,in_len) != 0) { /* this should NEVER happen */ printf("internal error - decompression data error\n"); return 1; } /* free buffers in reverse order to help malloc() */ lzo_free(wrkmem); lzo_free(newb); lzo_free(out); lzo_free(in); return 0; }
int __lzo_cdecl_main main(int argc, char *argv[]) { int i = 1; int r; const char *dict_name; FILE *f; time_t t_total; int level = 7; lzo_wildargv(&argc, &argv); printf("\nLZO real-time data compression library (v%s, %s).\n", lzo_version_string(), lzo_version_date()); printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); progname = argv[0]; if (i < argc && argv[i][0] == '-' && isdigit(argv[i][1])) level = atoi(&argv[i++][1]); if (i + 1 >= argc || level < 1 || level > 9) { printf("usage: %s [-level] [ dictionary-file | -n ] file...\n", progname); exit(1); } printf("Compression level is LZO1X-999/%d\n", level); /* * Step 1: initialize the LZO library */ if (lzo_init() != LZO_E_OK) { printf("internal error - lzo_init() failed !!!\n"); printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); exit(1); } /* * Step 2: prepare the dictionary */ dict = (lzo_bytep) lzo_malloc(DICT_LEN); if (dict == NULL) { printf("%s: out of memory\n", progname); exit(1); } dict_name = argv[i++]; if (strcmp(dict_name,"-n") == 0) { dict_name = "empty"; dict_len = 0; } else { f = fopen(dict_name,"rb"); if (!f) { printf("%s: cannot open dictionary file %s\n", progname, dict_name); exit(1); } dict_len = (lzo_uint) lzo_fread(f,dict,DICT_LEN); fclose(f); } dict_adler32 = lzo_adler32(0,NULL,0); dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len); printf("Using dictionary '%s', %ld bytes, ID 0x%08lx.\n", dict_name, (long) dict_len, (long) dict_adler32); /* * Step 3: process files */ t_total = time(NULL); for (r = 0; r == 0 && i < argc; i++) r = do_file(argv[i], level); t_total = time(NULL) - t_total; lzo_free(dict); if (total_n > 1) print_file("***TOTALS***",total_d_len,total_c_len); printf("Dictionary compression test %s, execution time %lu seconds.\n", r == 0 ? "passed" : "FAILED", (unsigned long) t_total); return r; }
void decode_lzo(decode_t *decode) { long bytes; ssize_t ss; tc_lzo_header_t h; /* * Step 1: initialize the LZO library */ if (lzo_init() != LZO_E_OK) { tc_log_error(__FILE__, "lzo_init() failed"); goto decoder_error; } wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS); out = (lzo_bytep) lzo_malloc(BUFFER_SIZE); inbuf = (lzo_bytep) lzo_malloc(BUFFER_SIZE); if (wrkmem == NULL || out == NULL) { tc_log_error(__FILE__, "out of memory"); goto decoder_error; } r = 0; out_len = 0; verbose = decode->verbose; for (;;) { if ( (ss=tc_pread (decode->fd_in, (uint8_t *)&h, sizeof(h))) != sizeof(h)) { //tc_log_msg(__FILE__, "failed to read frame size: EOF. expected (%ld) got (%d)", 4L, ss); goto decoder_out; } // check magic if (h.magic != TC_CODEC_LZO2) { tc_log_error(__FILE__, "Wrong stream magic: expected (0x%x) got (0x%x)", TC_CODEC_LZO2, h.magic); goto decoder_error; } //str2long(bb, &bytes); bytes = h.size; if (verbose & TC_DEBUG) tc_log_msg(__FILE__, "got bytes (%ld)", bytes); if ( (ss=tc_pread (decode->fd_in, inbuf, bytes))!=bytes) { tc_log_error(__FILE__, "failed to read frame: expected (%ld) got (%lu)", bytes, (unsigned long)ss); goto decoder_error; } if (h.flags & TC_LZO_NOT_COMPRESSIBLE) { ac_memcpy(out, inbuf, bytes); out_len = bytes; r = LZO_E_OK; } else { r = lzo1x_decompress(inbuf, bytes, out, &out_len, wrkmem); } if (r == LZO_E_OK) { if(verbose & TC_DEBUG) tc_log_msg(__FILE__, "decompressed %lu bytes into %lu bytes", (long) bytes, (long) out_len); } else { /* this should NEVER happen */ tc_log_error(__FILE__, "internal error - decompression failed: %d", r); goto decoder_error; } if ( (ss = tc_pwrite (decode->fd_out, out, out_len)) != out_len) { tc_log_error(__FILE__, "failed to write frame: expected (%ld) wrote (%lu)", bytes, (unsigned long)ss); goto decoder_error; } } decoder_out: import_exit(0); decoder_error: import_exit(1); }
int __lzo_cdecl_main main(int argc, char *argv[]) { int r; int lazy; const int max_try_lazy = 5; const lzo_uint big = 65536L; /* can result in very slow compression */ const lzo_uint32 flags = 0x1; lzo_bytep in; lzo_uint in_len; lzo_bytep out; lzo_uint out_bufsize; lzo_uint out_len = 0; lzo_bytep wrkmem; lzo_uint wrk_len; lzo_uint best_len; int best_compress = -1; int best_lazy = -1; lzo_uint orig_len; lzo_uint32 uncompressed_checksum; lzo_uint32 compressed_checksum; FILE *f; const char *progname = NULL; const char *in_name = NULL; const char *out_name = NULL; long l; lzo_wildargv(&argc, &argv); printf("\nLZO real-time data compression library (v%s, %s).\n", lzo_version_string(), lzo_version_date()); printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); progname = argv[0]; if (argc < 2 || argc > 3) { printf("usage: %s file [output-file]\n", progname); exit(1); } in_name = argv[1]; if (argc > 2) out_name = argv[2]; /* * Step 1: initialize the LZO library */ if (lzo_init() != LZO_E_OK) { printf("internal error - lzo_init() failed !!!\n"); printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); exit(1); } /* * Step 2: allocate the work-memory */ wrk_len = 1; #ifdef USE_LZO1X if (wrk_len < LZO1X_999_MEM_COMPRESS) wrk_len = LZO1X_999_MEM_COMPRESS; #endif #ifdef USE_LZO1Y if (wrk_len < LZO1Y_999_MEM_COMPRESS) wrk_len = LZO1Y_999_MEM_COMPRESS; #endif wrkmem = (lzo_bytep) lzo_malloc(wrk_len); if (wrkmem == NULL) { printf("%s: out of memory\n", progname); exit(1); } /* * Step 3: open the input file */ f = fopen(in_name,"rb"); if (f == NULL) { printf("%s: cannot open file %s\n", progname, in_name); exit(1); } fseek(f,0,SEEK_END); l = ftell(f); fseek(f,0,SEEK_SET); if (l <= 0) { printf("%s: %s: empty file\n", progname, in_name); fclose(f); exit(1); } in_len = (lzo_uint) l; out_bufsize = in_len + in_len / 16 + 64 + 3; best_len = in_len; /* * Step 4: allocate compression buffers and read the file */ in = (lzo_bytep) lzo_malloc(in_len); out = (lzo_bytep) lzo_malloc(out_bufsize); if (in == NULL || out == NULL) { printf("%s: out of memory\n", progname); exit(1); } in_len = (lzo_uint) lzo_fread(f,in,in_len); printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len); fclose(f); /* * Step 5: compute a checksum of the uncompressed data */ uncompressed_checksum = lzo_adler32(0,NULL,0); uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len); /* * Step 6a: compress from `in' to `out' with LZO1X-999 */ #ifdef USE_LZO1X for (lazy = 0; lazy <= max_try_lazy; lazy++) { out_len = out_bufsize; r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem, NULL, 0, 0, lazy, big, big, big, big, flags); if (r != LZO_E_OK) { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); exit(1); } printf("LZO1X-999: lazy =%2d: %8lu -> %8lu\n", lazy, (unsigned long) in_len, (unsigned long) out_len); if (out_len < best_len) { best_len = out_len; best_lazy = lazy; best_compress = 1; /* LZO1X-999 */ } } #endif /* USE_LZO1X */ /* * Step 6b: compress from `in' to `out' with LZO1Y-999 */ #ifdef USE_LZO1Y for (lazy = 0; lazy <= max_try_lazy; lazy++) { out_len = out_bufsize; r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem, NULL, 0, 0, lazy, big, big, big, big, flags); if (r != LZO_E_OK) { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); exit(1); } printf("LZO1Y-999: lazy =%2d: %8lu -> %8lu\n", lazy, (unsigned long) in_len, (unsigned long) out_len); if (out_len < best_len) { best_len = out_len; best_lazy = lazy; best_compress = 2; /* LZO1Y-999 */ } } #endif /* USE_LZO1Y */ /* * Step 7: check if compressible */ if (best_len >= in_len) { printf("This file contains incompressible data.\n"); return 0; } /* * Step 8: compress data again using the best compressor found */ out_len = out_bufsize; if (best_compress == 1) r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem, NULL, 0, 0, best_lazy, big, big, big, big, flags); else if (best_compress == 2) r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem, NULL, 0, 0, best_lazy, big, big, big, big, flags); else r = -100; assert(r == LZO_E_OK); assert(out_len == best_len); /* * Step 9: optimize compressed data (compressed data is in `out' buffer) */ #if 1 /* Optimization does not require any data in the buffer that will * hold the uncompressed data. To prove this, we clear the buffer. */ lzo_memset(in,0,in_len); #endif orig_len = in_len; r = -100; #ifdef USE_LZO1X if (best_compress == 1) r = lzo1x_optimize(out,out_len,in,&orig_len,NULL); #endif #ifdef USE_LZO1Y if (best_compress == 2) r = lzo1y_optimize(out,out_len,in,&orig_len,NULL); #endif if (r != LZO_E_OK || orig_len != in_len) { /* this should NEVER happen */ printf("internal error - optimization failed: %d\n", r); exit(1); } /* * Step 10: compute a checksum of the compressed data */ compressed_checksum = lzo_adler32(0,NULL,0); compressed_checksum = lzo_adler32(compressed_checksum,out,out_len); /* * Step 11: write compressed data to a file */ printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n", progname, in_name, (long) in_len, (long) out_len, (long) uncompressed_checksum, (long) compressed_checksum); if (out_name && out_name[0]) { printf("%s: writing to file %s\n", progname, out_name); f = fopen(out_name,"wb"); if (f == NULL) { printf("%s: cannot open output file %s\n", progname, out_name); exit(1); } if (lzo_fwrite(f,out,out_len) != out_len || fclose(f) != 0) { printf("%s: write error !!\n", progname); exit(1); } } /* * Step 12: verify decompression */ #ifdef PARANOID lzo_memset(in,0,in_len); /* paranoia - clear output buffer */ orig_len = in_len; r = -100; #ifdef USE_LZO1X if (best_compress == 1) r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL); #endif #ifdef USE_LZO1Y if (best_compress == 2) r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL); #endif if (r != LZO_E_OK || orig_len != in_len) { /* this should NEVER happen */ printf("internal error - decompression failed: %d\n", r); exit(1); } if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len)) { /* this should NEVER happen */ printf("internal error - decompression data error\n"); exit(1); } /* Now you could also verify decompression under similar conditions as in * your application, e.g. overlapping assembler decompression etc. */ #endif lzo_free(in); lzo_free(out); lzo_free(wrkmem); return 0; }
int __lzo_cdecl_main main(int argc, char *argv[]) { int r; lzo_bytep in; lzo_bytep out; lzo_bytep wrkmem; lzo_uint in_len; lzo_uint out_len; lzo_uint new_len; if (argc < 0 && argv == NULL) /* avoid warning about unused args */ return 0; printf("\nLZO real-time data compression library (v%s, %s).\n", lzo_version_string(), lzo_version_date()); printf("Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); /* * Step 1: initialize the LZO library */ if (lzo_init() != LZO_E_OK) { printf("internal error - lzo_init() failed !!!\n"); printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); return 4; } /* * Step 2: allocate blocks and the work-memory */ in = (lzo_bytep) lzo_malloc(IN_LEN); out = (lzo_bytep) lzo_malloc(OUT_LEN); wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS); if (in == NULL || out == NULL || wrkmem == NULL) { printf("out of memory\n"); return 3; } /* * Step 3: prepare the input block that will get compressed. * We just fill it with zeros in this example program, * but you would use your real-world data here. */ in_len = IN_LEN; lzo_memset(in,0,in_len); /* * Step 4: compress from `in' to `out' with LZO1X-1 */ r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem); if (r == LZO_E_OK) printf("compressed %lu bytes into %lu bytes\n", (unsigned long) in_len, (unsigned long) out_len); else { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); return 2; } /* check for an incompressible block */ if (out_len >= in_len) { printf("This block contains incompressible data.\n"); return 0; } /* * Step 5: decompress again, now going from `out' to `in' */ new_len = in_len; r = lzo1x_decompress(out,out_len,in,&new_len,NULL); if (r == LZO_E_OK && new_len == in_len) printf("decompressed %lu bytes back into %lu bytes\n", (unsigned long) out_len, (unsigned long) in_len); else { /* this should NEVER happen */ printf("internal error - decompression failed: %d\n", r); return 1; } lzo_free(wrkmem); lzo_free(out); lzo_free(in); printf("Simple compression test passed.\n"); return 0; }
int main(int argc, char *argv[]) { int r; lzo_byte *in; lzo_uint in_len; lzo_byte *out; lzo_uint out_len = 0; lzo_byte *wrkmem; lzo_uint wrk_len; lzo_uint best_len; int best_compress = -1; lzo_uint orig_len; lzo_uint32 uncompressed_checksum; lzo_uint32 compressed_checksum; FILE *f; const char *progname = NULL; const char *in_name = NULL; const char *out_name = NULL; long l; #if defined(__EMX__) _response(&argc,&argv); _wildcard(&argc,&argv); #endif printf("\nLZO real-time data compression library (v%s, %s).\n", lzo_version_string(), lzo_version_date()); printf("Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer\n\n"); progname = argv[0]; if (argc < 2 || argc > 3) { printf("usage: %s file [output-file]\n", progname); exit(1); } in_name = argv[1]; out_name = (argc > 2) ? argv[2] : NULL; /* * Step 1: initialize the LZO library */ if (lzo_init() != LZO_E_OK) { printf("lzo_init() failed !!!\n"); exit(1); } /* * Step 2: allocate the work-memory */ wrk_len = 1; #ifdef USE_LZO1X if (wrk_len < LZO1X_999_MEM_COMPRESS) wrk_len = LZO1X_999_MEM_COMPRESS; #endif #ifdef USE_LZO1Y if (wrk_len < LZO1Y_999_MEM_COMPRESS) wrk_len = LZO1Y_999_MEM_COMPRESS; #endif wrkmem = (lzo_bytep) lzo_malloc(wrk_len); if (wrkmem == NULL) { printf("%s: out of memory\n", progname); exit(1); } /* * Step 3: open the input file */ f = fopen(in_name,"rb"); if (f == NULL) { printf("%s: cannot open file %s\n", progname, in_name); exit(1); } fseek(f,0,SEEK_END); l = ftell(f); fseek(f,0,SEEK_SET); if (l <= 0) { printf("%s: %s: empty file\n", progname, in_name); fclose(f); exit(1); } in_len = (lzo_uint) l; best_len = in_len; /* * Step 4: allocate compression buffers and read the file */ in = (lzo_bytep) lzo_malloc(in_len); out = (lzo_bytep) lzo_malloc(in_len + in_len / 64 + 16 + 3); if (in == NULL || out == NULL) { printf("%s: out of memory\n", progname); exit(1); } in_len = lzo_fread(f,in,in_len); printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len); fclose(f); /* * Step 5: compute a checksum of the uncompressed data */ uncompressed_checksum = lzo_adler32(0,NULL,0); uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len); /* * Step 6a: compress from `in' to `out' with LZO1X-999 */ #ifdef USE_LZO1X r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem); if (r != LZO_E_OK) { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); exit(1); } printf("LZO1X-999: %8lu -> %8lu\n", (long) in_len, (long) out_len); if (out_len < best_len) { best_len = out_len; best_compress = 1; /* LZO1X-999 */ } #endif /* USE_LZO1X */ /* * Step 6b: compress from `in' to `out' with LZO1Y-999 */ #ifdef USE_LZO1Y r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem); if (r != LZO_E_OK) { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); exit(1); } printf("LZO1Y-999: %8lu -> %8lu\n", (long) in_len, (long) out_len); if (out_len < best_len) { best_len = out_len; best_compress = 2; /* LZO1Y-999 */ } #endif /* USE_LZO1Y */ /* * Step 7: check if compressible */ if (best_len >= in_len) { printf("This file contains incompressible data.\n"); return 0; } /* * Step 8: compress data again using the best compressor found */ if (best_compress == 1) r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem); else if (best_compress == 2) r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem); else r = -100; assert(r == LZO_E_OK); assert(out_len == best_len); /* * Step 9: optimize compressed data (compressed data is in `out' buffer) */ #if 1 /* Optimization does not require any data in the buffer that will * hold the uncompressed data. To prove this, we clear the buffer. */ lzo_memset(in,0,in_len); #endif orig_len = in_len; if (best_compress == 1) r = lzo1x_optimize(out,out_len,in,&orig_len,NULL); else if (best_compress == 2) r = lzo1y_optimize(out,out_len,in,&orig_len,NULL); else r = -100; if (r != LZO_E_OK || orig_len != in_len) { /* this should NEVER happen */ printf("internal error - optimization failed: %d\n", r); exit(1); } /* * Step 10: compute a checksum of the compressed data */ compressed_checksum = lzo_adler32(0,NULL,0); compressed_checksum = lzo_adler32(compressed_checksum,out,out_len); /* * Step 11: write compressed data to a file */ printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n", progname, in_name, (long) in_len, (long) out_len, (long) uncompressed_checksum, (long) compressed_checksum); if (out_name && out_name[0]) { printf("%s: writing to file %s\n", progname, out_name); f = fopen(out_name,"wb"); if (f == NULL) { printf("%s: cannot open output file %s\n", progname, out_name); exit(1); } if (lzo_fwrite(f,out,out_len) != out_len || fclose(f) != 0) { printf("%s: write error !!\n", progname); exit(1); } } /* * Step 12: verify decompression */ #ifdef PARANOID orig_len = in_len; if (best_compress == 1) r = lzo1x_decompress(out,out_len,in,&orig_len,NULL); else if (best_compress == 2) r = lzo1y_decompress(out,out_len,in,&orig_len,NULL); else r = -100; if (r != LZO_E_OK || orig_len != in_len) { /* this should NEVER happen */ printf("internal error - decompression failed: %d\n", r); exit(1); } if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len)) { /* this should NEVER happen */ printf("internal error - decompression data error\n"); exit(1); } /* Now you could also verify decompression under similar conditions as in * your application, e.g. overlapping assembler decompression etc. */ #endif lzo_free(in); lzo_free(out); lzo_free(wrkmem); return 0; }