📄 ltest.c
字号:
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};/*************************************************************************// method info**************************************************************************/staticlzo_decompress_t get_decomp_info ( const compress_t *c, const char **nn ){ lzo_decompress_t d = 0; const char *n = NULL; /* safe has priority over asm/fast */ if (!d && opt_use_safe_decompressor && opt_use_asm_fast_decompressor) { d = c->decompress_asm_fast_safe; n = " [fs]"; } if (!d && opt_use_safe_decompressor && opt_use_asm_decompressor) { d = c->decompress_asm_safe; n = " [as]"; } if (!d && opt_use_safe_decompressor) { d = c->decompress_safe; n = " [s]"; } if (!d && opt_use_asm_fast_decompressor) { d = c->decompress_asm_fast; n = " [f]"; } if (!d && opt_use_asm_decompressor) { d = c->decompress_asm; n = " [a]"; } if (!d) { d = c->decompress; n = ""; } if (!d) n = "(null)"; if (opt_dict && c->decompress_dict_safe) n = ""; if (nn) *nn = n; return d;}staticconst compress_t *info ( int method, FILE *f ){ const compress_t *c = NULL; const compress_t *db = compress_database; size_t size = sizeof(compress_database) / sizeof(*(compress_database)); size_t i; if (method > 0) { for (i = 0; i < size && db->name != NULL; i++, db++) { if (method == db->id && WRK_LEN >= db->mem_compress && WRK_LEN >= db->mem_decompress) { c = db; break; } } } if (f != NULL) { if (c == NULL) { fprintf(f,"%s: invalid method %d\n",argv0,method); exit(EXIT_USAGE); } else { const char *n; get_decomp_info(c,&n); fprintf(f,"%s%s",c->name,n); } } return c;}staticconst compress_t *find_method ( const char *name ){ const compress_t *db = compress_database; size_t size = sizeof(compress_database) / sizeof(*(compress_database)); size_t i; for (i = 0; i < size && db->name != NULL; i++, db++) { size_t n = strlen(db->name);#if defined(HAVE_STRNCASECMP) if (strncasecmp(name,db->name,n) == 0 && (!name[n] || name[n] == ',')) return db;#else if (strncmp(name,db->name,n) == 0 && (!name[n] || name[n] == ',')) return db;#endif } return NULL;}staticlzo_bool is_compressor ( const compress_t *c ){ return (c->id <= M_LAST_COMPRESSOR || c->id >= 9721);}/*************************************************************************// check that memory gets accessed within bounds**************************************************************************/#if 0 || defined(LZO_DEBUG)static const lzo_bool opt_check_mem = 1;#elsestatic const lzo_bool opt_check_mem = 0;#endifvoid init_mem_checker ( lzo_byte *mem, lzo_byte *mem_base, lzo_uint32 l, unsigned char random_byte ){ lzo_uint i; lzo_uint len = (lzo_uint) l; if (opt_check_mem && mem && mem_base) { for (i = 0; i < 16; i++) mem[len+i] = random_byte++; for (i = 0; i < 16 && mem - i > mem_base; i++) mem[-1-i] = random_byte++;#if 0 || defined(LZO_DEBUG) /* fill in garbage */ random_byte |= 1; for (i = 0; i < len; i++, random_byte += 2) mem[i] = random_byte;#endif }}int check_mem ( lzo_byte *mem, lzo_byte *mem_base, lzo_uint32 l, unsigned char random_byte ){ lzo_uint i; lzo_uint len = (lzo_uint) l; if (opt_check_mem && mem && mem_base) { for (i = 0; i < 16; i++) if (mem[len+i] != random_byte++) return -1; for (i = 0; i < 16 && mem - i > mem_base; i++) if (mem[-1-i] != random_byte++) return -1; } return 0;}/*************************************************************************// compress a block**************************************************************************/staticint do_compress ( const compress_t *c, const lzo_byte *src, lzo_uint src_len, lzo_byte *dst, lzo_uintp dst_len ){ int r = -100; if (c && c->compress && WRK_LEN >= c->mem_compress) {#if defined(__LZO_CHECKER) /* malloc a block of the exact size to detect any overrun */ lzo_byte *w = wrkmem; if (c->mem_compress > 0) { wrkmem = malloc(c->mem_compress); /* must initialize memory - fill in garbage */ { lzo_uint32 i; unsigned char random_byte = (unsigned char) src_len; random_byte |= 1; for (i = 0; i < c->mem_compress; i++, random_byte += 2) wrkmem[i] = random_byte; } } else wrkmem = NULL;#else unsigned char random_byte = (unsigned char) src_len; init_mem_checker(wrkmem,_wrkmem,c->mem_compress,random_byte);#endif if (opt_dict && c->compress_dict) r = c->compress_dict(src,src_len,dst,dst_len,wrkmem,dict,dict_len); else r = c->compress(src,src_len,dst,dst_len,wrkmem);#if defined(__LZO_CHECKER) if (wrkmem) free(wrkmem); wrkmem = w;#else if (check_mem(wrkmem,_wrkmem,c->mem_compress,random_byte) != 0) printf("WARNING: wrkmem overwrite error (compress) !!!\n");#endif } if (r == 0 && opt_compute_adler32) { lzo_uint32 adler; adler = lzo_adler32(0, NULL, 0); adler = lzo_adler32(adler, src, src_len); adler_in = adler; } if (r == 0 && opt_compute_crc32) { lzo_uint32 crc; crc = lzo_crc32(0, NULL, 0); crc = lzo_crc32(crc, src, src_len); crc_in = crc; } return r;}/*************************************************************************// decompress a block**************************************************************************/staticint do_decompress ( const compress_t *c, lzo_decompress_t d, const lzo_byte *src, lzo_uint src_len, lzo_byte *dst, lzo_uintp dst_len ){ int r = -100; if (c && d && WRK_LEN >= c->mem_decompress) { unsigned char random_byte = (unsigned char) src_len; init_mem_checker(wrkmem,_wrkmem,c->mem_decompress,random_byte); if (opt_dict && c->decompress_dict_safe) r = c->decompress_dict_safe(src,src_len,dst,dst_len,wrkmem,dict,dict_len); else r = d(src,src_len,dst,dst_len,wrkmem); if (check_mem(wrkmem,_wrkmem,c->mem_decompress,random_byte) != 0) printf("WARNING: wrkmem overwrite error (decompress) !!!\n"); } if (r == 0 && opt_compute_adler32) { lzo_uint32 adler; adler = lzo_adler32(0, NULL, 0); adler = lzo_adler32(adler, dst, *dst_len); adler_out = adler; } if (r == 0 && opt_compute_crc32) { lzo_uint32 crc; crc = lzo_crc32(0, NULL, 0); crc = lzo_crc32(crc, dst, *dst_len); crc_out = crc; } return r;}/*************************************************************************// optimize a block**************************************************************************/staticint do_optimize ( const compress_t *c, lzo_byte *src, lzo_uint src_len, lzo_byte *dst, lzo_uintp dst_len ){ if (c && c->optimize && WRK_LEN >= c->mem_decompress) return c->optimize(src,src_len,dst,dst_len,wrkmem); else return 0;}/***********************************************************************// read a file************************************************************************/static const char *last_file = NULL;static lzo_uint last_len = 0;static void unload_file(void){ last_file = NULL; last_len = 0;#ifdef USE_MALLOC if (_data) { free(_data); data = _data = NULL; }#endif}int load_file ( const char *file_name, lzo_uintp len ){ FILE *f; long ll = -1; unsigned long ul; lzo_uint l; int r;#if 0 if (last_file && strcmp(file_name,last_file) == 0) { *len = last_len; return EXIT_OK; }#endif *len = 0; unload_file(); f = fopen(file_name,"rb"); if (f == NULL) { fprintf(stderr,"%s: ",file_name); perror("fopen"); fflush(stderr); return EXIT_FILE; } r = fseek(f,0,SEEK_END); if (r == 0) { ll = ftell(f); r = fseek(f,0,SEEK_SET); } if (r != 0 || ll < 0) { fprintf(stderr,"%s: ",file_name); perror("fseek"); fflush(stderr); return EXIT_FILE; } ul = ll;#ifdef USE_MALLOC l = (ul >= LZO_UINT_MAX - 512 ? LZO_UINT_MAX - 512 : (lzo_uint) ul); if (opt_max_data_len > 0 && l > opt_max_data_len) l = opt_max_data_len; _data = malloc (l + 256); if (_data == NULL) { perror("malloc"); fflush(stderr); return EXIT_MEM; }#else l = (ul >= DATA_LEN ? (lzo_uint) DATA_LEN : (lzo_uint) ul); if (opt_max_data_len > 0 && l > opt_max_data_len) l = opt_max_data_len;#endif data = LZO_PTR_ALIGN_UP(_data,256); l = lzo_fread(f,data,l); if (fclose(f) != 0) { unload_file(); fprintf(stderr,"%s: ",file_name); perror("fclose"); fflush(stderr); return EXIT_FILE; } last_file = file_name; last_len = l; *len = l; return EXIT_OK;}/***********************************************************************// print some compression statistics************************************************************************/staticvoid print_stats ( const char *method_name, const char *file_name, int t_loops, int c_loops, int d_loops, my_clock_t t_time, my_clock_t c_time, my_clock_t d_time, unsigned long c_len, unsigned long d_len, unsigned blocks ){ unsigned long x_len = d_len; unsigned long t_bytes, c_bytes, d_bytes; double t_secs, c_secs, d_secs; double t_kbs, c_kbs, d_kbs; double perc, bits; perc = (d_len > 0) ? c_len * 100.0 / d_len : 0; bits = perc * 0.08; c_bytes = x_len * c_loops * t_loops; d_bytes = x_len * d_loops * t_loops; t_bytes = c_bytes + d_bytes; c_secs = c_time / (double)(MY_CLOCKS_PER_SEC); c_kbs = (c_secs > 0.001) ? (c_bytes / c_secs) / 1000.0 : 0; d_secs = d_time / (double)(MY_CLOCKS_PER_SEC); d_kbs = (d_secs > 0.001) ? (d_bytes / d_secs) / 1000.0 : 0; t_secs = t_time / (double)(MY_CLOCKS_PER_SEC); t_kbs = (t_secs > 0.001) ? (t_bytes / t_secs) / 1000.0 : 0; total_n++; total_c_len += c_len; total_d_len += d_len; total_blocks += blocks; total_c_kbs += c_kbs; total_d_kbs += d_kbs; if (opt_verbose >= 2) { printf(" compressed into %lu bytes, %.2f%% %.3f\n", (long) c_len, perc, bits); printf("%-15s %5d: ","overall", t_loops); printf("%10lu bytes, %8.2f secs, %10.2f KB/sec\n", t_bytes, t_secs, t_kbs); printf("%-15s %5d: ","compress", c_loops); printf("%10lu bytes, %8.2f secs, %10.2f KB/sec\n", c_bytes, c_secs, c_kbs); printf("%-15s %5d: ","decompress", d_loops); printf("%10lu bytes, %8.2f secs, %10.2f KB/sec\n", d_bytes, d_secs, d_kbs); printf("\n"); } /* create a line for util/table.pl */ if (opt_verbose >= 1) { /* get basename */ const char *n, *nn, *b; for (nn = n = b = file_name; *nn; nn++) if (*nn == '/' || *nn == '\\' || *nn == ':') b = nn + 1; else n = b; printf("%-12s | %-14s %7ld %4ld %7ld %6.1f %9.2f %9.2f |\n", method_name, n, (long) d_len, (long) blocks, (long) c_len, perc, c_kbs, d_kbs); } if (opt_verbose >= 2) printf("\n");}staticvoid print_totals ( void ){ double perc; perc = (total_d_len > 0) ? total_c_len * 100.0 / total_d_len : 0; if (opt_verbose >= 1 && total_n > 1) { printf("%-12s %-14s %7ld %4ld %7ld %6.1f %9.2f %9.2f\n", "-------", "***TOTALS***", total_d_len, total_blocks, total_c_len, perc, total_c_kbs / total_n, total_d_kbs / total_n); }}/*************************************************************************// compress and decompress a file**************************************************************************/staticint process_file ( const compress_t *c, lzo_decompress_t decompress, const char *method_name, const char *file_name, lzo_uint l, int t_loops, int c_loops, int d_loops ){ int i; unsigned blocks = 0; unsigned long compressed_len = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -