int main(int argc, char *argv[]) { int r; ucl_bytep in; ucl_bytep out; ucl_uint in_len; ucl_uint out_len; ucl_uint new_len; int level = 5; /* compression level (1-10) */ if (argc < 0 && argv == NULL) /* avoid warning about unused args */ return 0; printf("\nUCL data compression library (v%s, %s).\n", ucl_version_string(), ucl_version_date()); printf("Copyright (C) 1996-2003 Markus Franz Xaver Johannes Oberhumer\n"); printf("http://www.oberhumer.com/opensource/ucl/\n\n"); /* * Step 1: initialize the UCL library */ if (ucl_init() != UCL_E_OK) { printf("internal error - ucl_init() failed !!!\n"); printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DUCL_DEBUG' for diagnostics)\n"); return 1; } /* * Step 2: setup memory * * We want to compress the data block at `in' with length `in_len' to * the block at `out'. Because the input block may be incompressible, * we must provide a little more output space in case that compression * is not possible. */ in_len = 256 * 1024L; #if defined(ACC_MM_AHSHIFT) /* reduce memory requirements for ancient 640kB DOS real-mode */ if (ACC_MM_AHSHIFT != 3) in_len = 16 * 1024L; #endif out_len = in_len + in_len / 8 + 256; in = (ucl_bytep) ucl_malloc(in_len); out = (ucl_bytep) ucl_malloc(out_len); if (in == NULL || out == NULL) { printf("out of memory\n"); return 2; } /* * 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. */ ucl_memset(in, 0, in_len); /* * Step 4: compress from `in' to `out' with UCL NRV2B */ r = ucl_nrv2b_99_compress(in,in_len,out,&out_len,NULL,level,NULL,NULL); if (r == UCL_E_OUT_OF_MEMORY) { printf("out of memory in compress\n"); return 3; } if (r == UCL_E_OK) printf("compressed %lu bytes into %lu bytes\n", (long) in_len, (long) out_len); else { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); return 4; } /* 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 back from `out' to `in' */ new_len = in_len; #if defined(UCL_USE_ASM) r = ucl_nrv2b_decompress_asm_8(out,out_len,in,&new_len,NULL); #else r = ucl_nrv2b_decompress_8(out,out_len,in,&new_len,NULL); #endif if (r == UCL_E_OK && new_len == in_len) printf("decompressed %lu bytes back into %lu bytes\n", (long) out_len, (long) in_len); else { /* this should NEVER happen */ printf("internal error - decompression failed: %d\n", r); return 5; } ucl_free(out); ucl_free(in); printf("\n"); #if defined(UCL_USE_ASM) printf("i386 assembler version is enabled.\n"); #endif printf("Simple compression test passed.\n"); return 0; }
static int swd_init(ucl_swd_t * s, const ucl_byte * dict, ucl_uint dict_len) { ucl_uint i = 0; int c = 0; if (s->n == 0) s->n = SWD_N; if (s->f == 0) s->f = SWD_F; s->threshold = SWD_THRESHOLD; if (s->n > SWD_N || s->f > SWD_F) return UCL_E_INVALID_ARGUMENT; #if defined(SWD_USE_MALLOC) s->b = (unsigned char *) ucl_alloc(s->n + s->f + s->f, 1); s->head3 = (swd_uint *) ucl_alloc(SWD_HSIZE, sizeof(*s->head3)); s->succ3 = (swd_uint *) ucl_alloc(s->n + s->f, sizeof(*s->succ3)); s->best3 = (swd_uint *) ucl_alloc(s->n + s->f, sizeof(*s->best3)); s->llen3 = (swd_uint *) ucl_alloc(SWD_HSIZE, sizeof(*s->llen3)); if (!s->b || !s->head3 || !s->succ3 || !s->best3 || !s->llen3) return UCL_E_OUT_OF_MEMORY; #ifdef HEAD2 s->head2 = (swd_uint *) ucl_alloc(UCL_UINT32_C(65536), sizeof(*s->head2)); if (!s->head2) return UCL_E_OUT_OF_MEMORY; #endif #endif /* defaults */ s->max_chain = SWD_MAX_CHAIN; s->nice_length = s->f; s->use_best_off = 0; s->lazy_insert = 0; s->b_size = s->n + s->f; if (s->b_size + s->f >= SWD_UINT_MAX) return UCL_E_ERROR; s->b_wrap = s->b + s->b_size; s->node_count = s->n; ucl_memset(s->llen3, 0, sizeof(s->llen3[0]) * SWD_HSIZE); #ifdef HEAD2 #if 1 ucl_memset(s->head2, 0xff, sizeof(s->head2[0]) * UCL_UINT32_C(65536)); assert(s->head2[0] == NIL2); #else for (i = 0; i < UCL_UINT32_C(65536); i++) s->head2[i] = NIL2; #endif #endif s->ip = 0; swd_initdict(s, dict, dict_len); s->bp = s->ip; s->first_rp = s->ip; assert(s->ip + s->f <= s->b_size); #if 1 s->look = (ucl_uint) (s->c->in_end - s->c->ip); if (s->look > 0) { if (s->look > s->f) s->look = s->f; ucl_memcpy(&s->b[s->ip], s->c->ip, s->look); s->c->ip += s->look; s->ip += s->look; } #else s->look = 0; while (s->look < s->f) { if ((c = getbyte(*(s->c))) < 0) break; s->b[s->ip] = UCL_BYTE(c); s->ip++; s->look++; } #endif if (s->ip == s->b_size) s->ip = 0; if (s->look >= 2 && s->dict_len > 0) swd_insertdict(s, 0, s->dict_len); s->rp = s->first_rp; if (s->rp >= s->node_count) s->rp -= s->node_count; else s->rp += s->b_size - s->node_count; #if defined(__UCL_CHECKER) /* initialize memory for the first few HEAD3 (if s->ip is not far * enough ahead to do this job for us). The value doesn't matter. */ if (s->look < 3) ucl_memset(&s->b[s->bp + s->look], 0, 3); #endif UCL_UNUSED(i); UCL_UNUSED(c); return UCL_E_OK; }