123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500 |
- /**************************************************************
- Form adapted from lzhuf.c
- written by Haruyasu Yoshizaki 11/20/1988
- some minor changes 4/6/1989
- comments translated by Haruhiko Okumura 4/7/1989
-
- minor beautifications and adjustments for compiling under Linux
- by Markus Gutschke <gutschk@math.uni-muenster.de>
- 1997-01-27
-
- Modifications to allow use as a filter by Ken Yap
- <ken_yap@users.sourceforge.net>.
-
- 1997-07-01
-
- Small mod to cope with running on big-endian machines
- by Jim Hague <jim.hague@acm.org)
- 1998-02-06
-
- Make compression statistics report shorter
- by Ken Yap <ken_yap@users.sourceforge.net>.
- 2001-04-25
-
- Replaced algorithm with nrv2b from ucl the compression
- library from upx. That code is:
- Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
- And is distributed under the terms of the GPL.
- The conversion was performed
- by Eric Biederman <ebiederman@lnxi.com>.
- 20 August 2002
-
- **************************************************************/
- #define UCLPACK_COMPAT 0
- #define NDEBUG 1
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <errno.h>
- #ifdef __FreeBSD__
- #include <inttypes.h>
- #else
- #include <stdint.h>
- #endif
- #include <limits.h>
- #include <assert.h>
- #if UCLPACK_COMPAT
- #include <netinet/in.h>
- #endif
-
- #ifndef VERBOSE
- #define Fprintf(x)
- #define wterr 0
- #else
- #define Fprintf(x) fprintf x
- #endif
-
- #ifndef MAIN
- extern
- #endif
- FILE *infile, *outfile;
-
- #if defined(ENCODE) || defined(DECODE)
-
- #ifndef ENDIAN
- #define ENDIAN 0
- #endif
- #ifndef BITSIZE
- #define BITSIZE 32
- #endif
-
- static __inline__ void Error(char *message)
- {
- Fprintf((stderr, "\n%s\n", message));
- exit(EXIT_FAILURE);
- }
-
- /* These will be a complete waste of time on a lo-endian */
- /* system, but it only gets done once so WTF. */
- static unsigned long __attribute__ (( unused )) i86ul_to_host(unsigned long ul)
- {
- unsigned long res = 0;
- int i;
- union
- {
- unsigned char c[4];
- unsigned long ul;
- } u;
-
- u.ul = ul;
- for (i = 3; i >= 0; i--)
- res = (res << 8) + u.c[i];
- return res;
- }
-
- static unsigned long host_to_i86ul(unsigned long ul)
- {
- int i;
- union
- {
- unsigned char c[4];
- unsigned long ul;
- } u;
-
- for (i = 0; i < 4; i++)
- {
- u.c[i] = ul & 0xff;
- ul >>= 8;
- }
- return u.ul;
- }
- #endif
-
-
-
- #if UCLPACK_COMPAT
- /* magic file header for compressed files */
- static const unsigned char magic[8] =
- { 0x00, 0xe9, 0x55, 0x43, 0x4c, 0xff, 0x01, 0x1a };
-
- #endif
-
- #ifdef ENCODE
- /********** NRV2B_99 compression **********/
-
- /* Note by limiting the ring buffer I have limited the maximum
- * offset to 64K. Since etherboot rarely gets that big it
- * is not a problem and it gives me a firm guarantee
- * that I will never get a 3 byte string match that is encodes
- * to more than 9/8 it's original size.
- * That guaranteee is important to for the inplace decompressor.
- * There are better ways to do this if a larger offset and buffer
- * would give better compression.
- */
- #define N (65536ul) /* size of ring buffer */
- #define THRESHOLD 1 /* lower limit for match length */
- #define F 2048 /* upper limit for match length */
- #define M2_MAX_OFFSET 0xd00
-
- /* note: to use default values pass -1, i.e. initialize
- * this struct by a memset(x,0xff,sizeof(x)) */
- struct ucl_compress_config
- {
- int bb_endian;
- int bb_size;
- unsigned int max_offset;
- unsigned int max_match;
- int s_level;
- int h_level;
- int p_level;
- int c_flags;
- unsigned int m_size;
- };
-
- struct ucl_compress
- {
- int init;
-
- unsigned int look; /* bytes in lookahead buffer */
-
- unsigned int m_len;
- unsigned int m_off;
-
- unsigned int last_m_len;
- unsigned int last_m_off;
-
- const unsigned char *bp;
- const unsigned char *ip;
- const unsigned char *in;
- const unsigned char *in_end;
- unsigned char *out;
-
- uint64_t bb_b;
- unsigned bb_k;
- unsigned bb_c_endian;
- unsigned bb_c_s;
- unsigned bb_c_s8;
- unsigned char *bb_p;
- unsigned char *bb_op;
-
- struct ucl_compress_config conf;
- unsigned int *result;
-
- unsigned int textsize; /* text size counter */
- unsigned int codesize; /* code size counter */
- unsigned int printcount; /* counter for reporting progress every 1K
- bytes */
-
-
- /* some stats */
- unsigned long lit_bytes;
- unsigned long match_bytes;
- unsigned long rep_bytes;
- unsigned long lazy;
- };
-
-
-
- #define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
-
- #define UCL_E_OK 0
- #define UCL_E_INVALID_ARGUMENT 1
- #define UCL_E_OUT_OF_MEMORY 2
- #define UCL_E_ERROR 3
-
- /***********************************************************************
- //
- ************************************************************************/
-
- #define SWD_HSIZE 16384
- #define SWD_MAX_CHAIN 2048
- #define SWD_BEST_OFF 1
-
- #define HEAD3(b,p) \
- (((0x9f5f*(((((uint32_t)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
-
- #define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
- #define NIL2 UINT_MAX
-
- struct ucl_swd
- {
- /* public - "built-in" */
- unsigned int n;
- unsigned int f;
- unsigned int threshold;
-
- /* public - configuration */
- unsigned int max_chain;
- unsigned int nice_length;
- int use_best_off;
- unsigned int lazy_insert;
-
- /* public - output */
- unsigned int m_len;
- unsigned int m_off;
- unsigned int look;
- int b_char;
- #if defined(SWD_BEST_OFF)
- unsigned int best_off[ SWD_BEST_OFF ];
- #endif
-
- /* semi public */
- struct ucl_compress *c;
- unsigned int m_pos;
- #if defined(SWD_BEST_OFF)
- unsigned int best_pos[ SWD_BEST_OFF ];
- #endif
-
- /* private */
- const uint8_t *dict;
- const uint8_t *dict_end;
- unsigned int dict_len;
-
- /* private */
- unsigned int ip; /* input pointer (lookahead) */
- unsigned int bp; /* buffer pointer */
- unsigned int rp; /* remove pointer */
- unsigned int b_size;
-
- unsigned char *b_wrap;
-
- unsigned int node_count;
- unsigned int first_rp;
-
- unsigned char b [ N + F + F ];
- unsigned int head3 [ SWD_HSIZE ];
- unsigned int succ3 [ N + F ];
- unsigned int best3 [ N + F ];
- unsigned int llen3 [ SWD_HSIZE ];
- unsigned int head2 [ 65536U ];
- };
-
- #define s_head3(s,key) s->head3[key]
-
-
- #if !defined( NDEBUG)
- static void assert_match(const struct ucl_swd * swd, unsigned int m_len,
- unsigned int m_off )
-
- {
- const struct ucl_compress *c = swd->c;
- unsigned int d_off;
-
- assert(m_len >= 2);
- if (m_off <= (unsigned int) (c->bp - c->in))
- {
- assert(c->bp - m_off + m_len < c->ip);
- assert(memcmp(c->bp, c->bp - m_off, m_len) == 0);
- }
- else
- {
- assert(swd->dict != NULL);
- d_off = m_off - (unsigned int) (c->bp - c->in);
- assert(d_off <= swd->dict_len);
- if (m_len > d_off)
- {
- assert(memcmp(c->bp, swd->dict_end - d_off, d_off) ==
- 0);
-
- assert(c->in + m_len - d_off < c->ip);
- assert(memcmp(c->bp + d_off, c->in, m_len - d_off) ==
- 0);
-
- }
- else
- {
- assert(memcmp(c->bp, swd->dict_end - d_off, m_len) ==
- 0);
-
- }
- }
- }
- #else
- # define assert_match(a,b,c) ((void)0)
- #endif
-
- /***********************************************************************
- //
- ************************************************************************/
-
-
- static
- void swd_initdict(struct ucl_swd *s, const uint8_t *dict, unsigned int dict_len)
-
- {
- s->dict = s->dict_end = NULL;
- s->dict_len = 0;
-
- if (!dict || dict_len <= 0)
- return;
- if (dict_len > s->n)
- {
- dict += dict_len - s->n;
- dict_len = s->n;
- }
-
- s->dict = dict;
- s->dict_len = dict_len;
- s->dict_end = dict + dict_len;
- memcpy(s->b,dict,dict_len);
- s->ip = dict_len;
- }
-
-
- static
- void swd_insertdict(struct ucl_swd *s, unsigned int node, unsigned int len)
- {
- unsigned int key;
-
- s->node_count = s->n - len;
- s->first_rp = node;
-
- while (len-- > 0)
- {
- key = HEAD3(s->b,node);
- s->succ3[node] = s_head3(s,key);
- s->head3[key] = (unsigned int)(node);
- s->best3[node] = (unsigned int)(s->f + 1);
- s->llen3[key]++;
- assert(s->llen3[key] <= s->n);
-
- key = HEAD2(s->b,node);
- s->head2[key] = (unsigned int)(node);
-
- node++;
- }
- }
-
- /***********************************************************************
- //
- ************************************************************************/
-
-
- static
- int swd_init(struct ucl_swd *s, const uint8_t *dict, unsigned int dict_len)
- {
- unsigned int i = 0;
-
- if (s->n == 0)
- s->n = N;
- if (s->f == 0)
- s->f = F;
- s->threshold = THRESHOLD;
- if (s->n > N || s->f > F)
- return UCL_E_INVALID_ARGUMENT;
-
- /* 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 >= UINT_MAX)
- return UCL_E_ERROR;
- s->b_wrap = s->b + s->b_size;
- s->node_count = s->n;
-
- memset(s->llen3, 0, sizeof(s->llen3[0]) * SWD_HSIZE);
- for (i = 0; i < 65536U; i++)
- s->head2[i] = NIL2;
-
- 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);
-
- s->look = (unsigned int) (s->c->in_end - s->c->ip);
- if (s->look > 0)
- {
- if (s->look > s->f)
- s->look = s->f;
- memcpy(&s->b[s->ip],s->c->ip,s->look);
- s->c->ip += s->look;
- s->ip += s->look;
- }
- 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;
-
- /* unused i */
- /* unused c */
- return UCL_E_OK;
- }
-
-
- static
- void swd_exit(struct ucl_swd *s)
- {
- /* unused s */
- ( void ) s;
- }
-
- #define swd_pos2off(s,pos) \
- (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
-
- /***********************************************************************
- //
- ************************************************************************/
-
- static __inline__
- void swd_getbyte(struct ucl_swd *s)
- {
- int c;
-
- if ((c = getbyte(*(s->c))) < 0)
- {
- if (s->look > 0)
- --s->look;
- }
- else
- {
- s->b[s->ip] = (uint8_t)(c);
- if (s->ip < s->f)
- s->b_wrap[s->ip] = (uint8_t)(c);
- }
- if (++s->ip == s->b_size)
- s->ip = 0;
- if (++s->bp == s->b_size)
- s->bp = 0;
- if (++s->rp == s->b_size)
- s->rp = 0;
- }
- /***********************************************************************
- // remove node from lists
- ************************************************************************/
-
- static __inline__
- void swd_remove_node(struct ucl_swd *s, unsigned int node)
- {
- if (s->node_count == 0)
- {
- unsigned int key;
-
- #ifdef UCL_DEBUG
- if (s->first_rp != UINT_MAX)
- {
- if (node != s->first_rp)
- printf("Remove %5d: %5d %5d %5d %5d %6d %6d\n",
-
- node, s->rp, s->ip, s->bp, s->first_rp,
- s->ip - node, s->ip - s->bp);
- assert(node == s->first_rp);
- s->first_rp = UINT_MAX;
- }
- #endif
-
- key = HEAD3(s->b,node);
- assert(s->llen3[key] > 0);
- --s->llen3[key];
-
- key = HEAD2(s->b,node);
- assert(s->head2[key] != NIL2);
- if ((unsigned int) s->head2[key] == node)
- s->head2[key] = NIL2;
- }
- else
- --s->node_count;
- }
-
-
- /***********************************************************************
- //
- ************************************************************************/
-
-
- static
- void swd_accept(struct ucl_swd *s, unsigned int n)
- {
- assert(n <= s->look);
-
- if (n > 0) do
- {
- unsigned int key;
-
- swd_remove_node(s,s->rp);
-
- /* add bp into HEAD3 */
- key = HEAD3(s->b,s->bp);
- s->succ3[s->bp] = s_head3(s,key);
- s->head3[key] = (unsigned int)(s->bp);
- s->best3[s->bp] = (unsigned int)(s->f + 1);
- s->llen3[key]++;
- assert(s->llen3[key] <= s->n);
-
- /* add bp into HEAD2 */
- key = HEAD2(s->b,s->bp);
- s->head2[key] = (unsigned int)(s->bp);
-
- swd_getbyte(s);
- } while (--n > 0);
- }
-
- /***********************************************************************
- //
- ************************************************************************/
-
- static
- void swd_search(struct ucl_swd *s, unsigned int node, unsigned int cnt)
- {
- const unsigned char *p1;
- const unsigned char *p2;
- const unsigned char *px;
-
- unsigned int m_len = s->m_len;
- const unsigned char * b = s->b;
- const unsigned char * bp = s->b + s->bp;
- const unsigned char * bx = s->b + s->bp + s->look;
- unsigned char scan_end1;
-
- assert(s->m_len > 0);
-
- scan_end1 = bp[m_len - 1];
- for ( ; cnt-- > 0; node = s->succ3[node])
- {
- p1 = bp;
- p2 = b + node;
- px = bx;
-
- assert(m_len < s->look);
-
- if (
- p2[m_len - 1] == scan_end1 &&
- p2[m_len] == p1[m_len] &&
- p2[0] == p1[0] &&
- p2[1] == p1[1])
- {
- unsigned int i;
- assert(memcmp(bp,&b[node],3) == 0);
-
- p1 += 2; p2 += 2;
- do {} while (++p1 < px && *p1 == *++p2);
- i = p1 - bp;
-
- #ifdef UCL_DEBUG
- if (memcmp(bp,&b[node],i) != 0)
- printf("%5ld %5ld %02x%02x %02x%02x\n",
- (long)s->bp, (long) node,
- bp[0], bp[1], b[node], b[node+1]);
- #endif
- assert(memcmp(bp,&b[node],i) == 0);
-
- #if defined(SWD_BEST_OFF)
- if (i < SWD_BEST_OFF)
- {
- if (s->best_pos[i] == 0)
- s->best_pos[i] = node + 1;
- }
- #endif
- if (i > m_len)
- {
- s->m_len = m_len = i;
- s->m_pos = node;
- if (m_len == s->look)
- return;
- if (m_len >= s->nice_length)
- return;
- if (m_len > (unsigned int) s->best3[node])
- return;
- scan_end1 = bp[m_len - 1];
- }
- }
- }
- }
-
- static int swd_search2(struct ucl_swd *s)
- {
- unsigned int key;
-
- assert(s->look >= 2);
- assert(s->m_len > 0);
-
- key = s->head2[ HEAD2(s->b,s->bp) ];
- if (key == NIL2)
- return 0;
- #ifdef UCL_DEBUG
- if (memcmp(&s->b[s->bp],&s->b[key],2) != 0)
- printf("%5ld %5ld %02x%02x %02x%02x\n", (long)s->bp, (long)key,
- s->b[s->bp], s->b[s->bp+1], s->b[key], s->b[key+1]);
- #endif
- assert(memcmp(&s->b[s->bp],&s->b[key],2) == 0);
- #if defined(SWD_BEST_OFF)
- if (s->best_pos[2] == 0)
- s->best_pos[2] = key + 1;
- #endif
-
- if (s->m_len < 2)
- {
- s->m_len = 2;
- s->m_pos = key;
- }
- return 1;
- }
-
- /***********************************************************************
- //
- ************************************************************************/
-
- static
- void swd_findbest(struct ucl_swd *s)
- {
- unsigned int key;
- unsigned int cnt, node;
- unsigned int len;
-
- assert(s->m_len > 0);
-
- /* get current head, add bp into HEAD3 */
- key = HEAD3(s->b,s->bp);
- node = s->succ3[s->bp] = s_head3(s,key);
- cnt = s->llen3[key]++;
- assert(s->llen3[key] <= s->n + s->f);
- if (cnt > s->max_chain && s->max_chain > 0)
- cnt = s->max_chain;
- s->head3[key] = (unsigned int)(s->bp);
-
- s->b_char = s->b[s->bp];
- len = s->m_len;
- if (s->m_len >= s->look)
- {
- if (s->look == 0)
- s->b_char = -1;
- s->m_off = 0;
- s->best3[s->bp] = (unsigned int)(s->f + 1);
- }
- else
- {
- if (swd_search2(s))
- if (s->look >= 3)
- swd_search(s,node,cnt);
- if (s->m_len > len)
- s->m_off = swd_pos2off(s,s->m_pos);
- s->best3[s->bp] = (unsigned int)(s->m_len);
-
- #if defined(SWD_BEST_OFF)
- if (s->use_best_off)
- {
- int i;
- for (i = 2; i < SWD_BEST_OFF; i++)
- if (s->best_pos[i] > 0)
- s->best_off[i] =
- swd_pos2off(s,s->best_pos[i]-1);
-
- else
- s->best_off[i] = 0;
- }
- #endif
- }
-
- swd_remove_node(s,s->rp);
-
- /* add bp into HEAD2 */
- key = HEAD2(s->b,s->bp);
- s->head2[key] = (unsigned int)(s->bp);
- }
-
-
- /***********************************************************************
- //
- ************************************************************************/
-
- static int
- init_match ( struct ucl_compress *c, struct ucl_swd *s,
- const uint8_t *dict, unsigned int dict_len,
- uint32_t flags )
- {
- int r;
-
- assert(!c->init);
- c->init = 1;
-
- s->c = c;
-
- c->last_m_len = c->last_m_off = 0;
-
- c->textsize = c->codesize = c->printcount = 0;
- c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
- c->lazy = 0;
-
- r = swd_init(s,dict,dict_len);
- if (r != UCL_E_OK)
- {
- swd_exit(s);
- return r;
- }
-
- s->use_best_off = (flags & 1) ? 1 : 0;
- return UCL_E_OK;
- }
-
- static int
- find_match ( struct ucl_compress *c, struct ucl_swd *s,
- unsigned int this_len, unsigned int skip )
- {
- assert(c->init);
-
- if (skip > 0)
- {
- assert(this_len >= skip);
- swd_accept(s, this_len - skip);
- c->textsize += this_len - skip + 1;
- }
- else
- {
- assert(this_len <= 1);
- c->textsize += this_len - skip;
- }
-
- s->m_len = THRESHOLD;
- #ifdef SWD_BEST_OFF
- if (s->use_best_off)
- memset(s->best_pos,0,sizeof(s->best_pos));
- #endif
- swd_findbest(s);
- c->m_len = s->m_len;
- c->m_off = s->m_off;
-
- swd_getbyte(s);
-
- if (s->b_char < 0)
- {
- c->look = 0;
- c->m_len = 0;
- swd_exit(s);
- }
- else
- {
- c->look = s->look + 1;
- }
- c->bp = c->ip - c->look;
-
- #if 0
- /* brute force match search */
- if (c->m_len > THRESHOLD && c->m_len + 1 <= c->look)
- {
- const uint8_t *ip = c->bp;
- const uint8_t *m = c->bp - c->m_off;
- const uint8_t *in = c->in;
-
- if (ip - in > N)
- in = ip - N;
- for (;;)
- {
- while (*in != *ip)
- in++;
- if (in == ip)
- break;
- if (in != m)
- if (memcmp(in,ip,c->m_len+1) == 0)
- printf("%p %p %p %5d\n",in,ip,m,c->m_len);
-
- in++;
- }
- }
- #endif
-
- return UCL_E_OK;
- }
-
-
- static int bbConfig(struct ucl_compress *c, int endian, int bitsize)
- {
- if (endian != -1)
- {
- if (endian != 0)
- return UCL_E_ERROR;
- c->bb_c_endian = endian;
- }
- if (bitsize != -1)
- {
- if (bitsize != 8 && bitsize != 16 && bitsize != 32 && bitsize != 64)
- return UCL_E_ERROR;
- c->bb_c_s = bitsize;
- c->bb_c_s8 = bitsize / 8;
- }
- c->bb_b = 0; c->bb_k = 0;
- c->bb_p = NULL;
- c->bb_op = NULL;
- return UCL_E_OK;
- }
-
- static void bbWriteBits(struct ucl_compress *c)
- {
- uint8_t *p = c->bb_p;
- uint64_t b = c->bb_b;
-
- p[0] = (uint8_t)(b >> 0);
- if (c->bb_c_s >= 16)
- {
- p[1] = (uint8_t)(b >> 8);
- if (c->bb_c_s >= 32)
- {
- p[2] = (uint8_t)(b >> 16);
- p[3] = (uint8_t)(b >> 24);
- if (c->bb_c_s == 64)
- {
- p[4] = (uint8_t)(b >> 32);
- p[5] = (uint8_t)(b >> 40);
- p[6] = (uint8_t)(b >> 48);
- p[7] = (uint8_t)(b >> 56);
- }
- }
- }
- }
-
-
- static void bbPutBit(struct ucl_compress *c, unsigned bit)
- {
- assert(bit == 0 || bit == 1);
- assert(c->bb_k <= c->bb_c_s);
-
- if (c->bb_k < c->bb_c_s)
- {
- if (c->bb_k == 0)
- {
- assert(c->bb_p == NULL);
- c->bb_p = c->bb_op;
- c->bb_op += c->bb_c_s8;
- }
- assert(c->bb_p != NULL);
- assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
-
- c->bb_b = (c->bb_b << 1) + bit;
- c->bb_k++;
- }
- else
- {
- assert(c->bb_p != NULL);
- assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
-
- bbWriteBits(c);
- c->bb_p = c->bb_op;
- c->bb_op += c->bb_c_s8;
- c->bb_b = bit;
- c->bb_k = 1;
- }
- }
-
-
- static void bbPutByte(struct ucl_compress *c, unsigned b)
- {
- /**printf("putbyte %p %p %x (%d)\n", op, bb_p, x, bb_k);*/
- assert(c->bb_p == NULL || c->bb_p + c->bb_c_s8 <= c->bb_op);
- *c->bb_op++ = (uint8_t)(b);
- }
-
- static void bbFlushBits(struct ucl_compress *c, unsigned filler_bit)
- {
- if (c->bb_k > 0)
- {
- assert(c->bb_k <= c->bb_c_s);
- while (c->bb_k != c->bb_c_s)
- bbPutBit(c, filler_bit);
- bbWriteBits(c);
- c->bb_k = 0;
- }
- c->bb_p = NULL;
- }
-
-
-
- /***********************************************************************
- //
- ************************************************************************/
-
-
- static void code_prefix_ss11(struct ucl_compress *c, uint32_t i)
- {
- if (i >= 2)
- {
- uint32_t t = 4;
- i += 2;
- do {
- t <<= 1;
- } while (i >= t);
- t >>= 1;
- do {
- t >>= 1;
- bbPutBit(c, (i & t) ? 1 : 0);
- bbPutBit(c, 0);
- } while (t > 2);
- }
- bbPutBit(c, (unsigned)i & 1);
- bbPutBit(c, 1);
- }
-
- static void
- code_match(struct ucl_compress *c, unsigned int m_len, const unsigned int m_off)
-
- {
- while (m_len > c->conf.max_match)
- {
- code_match(c, c->conf.max_match - 3, m_off);
- m_len -= c->conf.max_match - 3;
- }
-
- c->match_bytes += m_len;
- if (m_len > c->result[3])
- c->result[3] = m_len;
- if (m_off > c->result[1])
- c->result[1] = m_off;
-
- bbPutBit(c, 0);
-
- if (m_off == c->last_m_off)
- {
- bbPutBit(c, 0);
- bbPutBit(c, 1);
- }
- else
- {
- code_prefix_ss11(c, 1 + ((m_off - 1) >> 8));
- bbPutByte(c, (unsigned)m_off - 1);
- }
- m_len = m_len - 1 - (m_off > M2_MAX_OFFSET);
- if (m_len >= 4)
- {
- bbPutBit(c,0);
- bbPutBit(c,0);
- code_prefix_ss11(c, m_len - 4);
- }
- else
- {
- bbPutBit(c, m_len > 1);
- bbPutBit(c, (unsigned)m_len & 1);
- }
-
- c->last_m_off = m_off;
- }
-
- static void
- code_run(struct ucl_compress *c, const uint8_t *ii, unsigned int lit)
- {
- if (lit == 0)
- return;
- c->lit_bytes += lit;
- if (lit > c->result[5])
- c->result[5] = lit;
- do {
- bbPutBit(c, 1);
- bbPutByte(c, *ii++);
- } while (--lit > 0);
- }
-
- /***********************************************************************
- //
- ************************************************************************/
-
- static int
- len_of_coded_match(struct ucl_compress *c, unsigned int m_len, unsigned int
- m_off)
-
- {
- int b;
- if (m_len < 2 || (m_len == 2 && (m_off > M2_MAX_OFFSET))
- || m_off > c->conf.max_offset)
- return -1;
- assert(m_off > 0);
-
- m_len = m_len - 2 - (m_off > M2_MAX_OFFSET);
-
- if (m_off == c->last_m_off)
- b = 1 + 2;
- else
- {
- b = 1 + 10;
- m_off = (m_off - 1) >> 8;
- while (m_off > 0)
- {
- b += 2;
- m_off >>= 1;
- }
- }
-
- b += 2;
- if (m_len < 3)
- return b;
- m_len -= 3;
-
- do {
- b += 2;
- m_len >>= 1;
- } while (m_len > 0);
-
- return b;
- }
-
- int ucl_nrv2b_99_compress(
- const uint8_t *in, unsigned long in_len,
- uint8_t *out, unsigned long *out_len,
- unsigned int *result)
- {
- const uint8_t *ii;
- unsigned int lit;
- unsigned int m_len, m_off;
- struct ucl_compress c_buffer;
- struct ucl_compress * const c = &c_buffer;
- struct ucl_swd *swd;
- unsigned int result_buffer[16];
- int r;
-
- /* max compression */
- #define SC_TRY_LAZY 2
- #define SC_GOOD_LENGTH F
- #define SC_MAX_LAZY F
- #define SC_NICE_LENGTH F
- #define SC_MAX_CHAIN 4096
- #define SC_FLAGS 1
- #define SC_MAX_OFFSET N
-
- memset(c, 0, sizeof(*c));
- c->ip = c->in = in;
- c->in_end = in + in_len;
- c->out = out;
- c->result = result ? result : result_buffer;
- memset(c->result, 0, 16*sizeof(*c->result));
- c->result[0] = c->result[2] = c->result[4] = UINT_MAX;
- result = NULL;
- memset(&c->conf, 0xff, sizeof(c->conf));
- r = bbConfig(c, ENDIAN, BITSIZE);
- if (r == 0)
- r = bbConfig(c, c->conf.bb_endian, c->conf.bb_size);
- if (r != 0)
- return UCL_E_INVALID_ARGUMENT;
- c->bb_op = out;
-
- ii = c->ip; /* point to start of literal run */
- lit = 0;
-
-
- swd = (struct ucl_swd *) malloc(sizeof(*swd));
- if (!swd)
- return UCL_E_OUT_OF_MEMORY;
-
- swd->f = F;
- swd->n = N;
- if (in_len >= 256 && in_len < swd->n)
- swd->n = in_len;
- if (swd->f < 8 || swd->n < 256)
- return UCL_E_INVALID_ARGUMENT;
-
- r = init_match(c,swd,NULL,0, SC_FLAGS);
- if (r != UCL_E_OK)
- {
- free(swd);
- return r;
- }
- if (SC_MAX_CHAIN > 0)
- swd->max_chain = SC_MAX_CHAIN;
- if (SC_NICE_LENGTH > 0)
- swd->nice_length = SC_NICE_LENGTH;
- if (c->conf.max_match < swd->nice_length)
- swd->nice_length = c->conf.max_match;
-
- c->last_m_off = 1;
- r = find_match(c,swd,0,0);
- if (r != UCL_E_OK)
- return r;
- while (c->look > 0)
- {
- unsigned int ahead;
- unsigned int max_ahead;
- int l1, l2;
-
- c->codesize = c->bb_op - out;
-
- m_len = c->m_len;
- m_off = c->m_off;
-
- assert(c->bp == c->ip - c->look);
- assert(c->bp >= in);
- if (lit == 0)
- ii = c->bp;
- assert(ii + lit == c->bp);
- assert(swd->b_char == *(c->bp));
-
- if (m_len < 2 || (m_len == 2 && (m_off > M2_MAX_OFFSET))
- || m_off > c->conf.max_offset)
- {
- /* a literal */
- lit++;
- swd->max_chain = SC_MAX_CHAIN;
- r = find_match(c,swd,1,0);
- assert(r == 0);
- continue;
- }
-
- /* a match */
- assert_match(swd,m_len,m_off);
-
- /* shall we try a lazy match ? */
- ahead = 0;
- if (SC_TRY_LAZY <= 0 || m_len >= SC_MAX_LAZY || m_off ==
- c->last_m_off)
-
- {
- /* no */
- l1 = 0;
- max_ahead = 0;
- }
- else
- {
- /* yes, try a lazy match */
- l1 = len_of_coded_match(c,m_len,m_off);
- assert(l1 > 0);
- max_ahead = SC_TRY_LAZY;
- if ((m_len - 1) < max_ahead) {
- max_ahead = m_len -1;
- }
- }
-
- while (ahead < max_ahead && c->look > m_len)
- {
- if (m_len >= SC_GOOD_LENGTH)
- swd->max_chain = SC_MAX_CHAIN >> 2;
- else
- swd->max_chain = SC_MAX_CHAIN;
- r = find_match(c,swd,1,0);
- ahead++;
-
- assert(r == 0);
- assert(c->look > 0);
- assert(ii + lit + ahead == c->bp);
-
- if (c->m_len < 2)
- continue;
- l2 = len_of_coded_match(c,c->m_len,c->m_off);
- if (l2 < 0)
- continue;
- if (l1 + (int)(ahead + c->m_len - m_len) * 5 > l2 +
- (int)(ahead) * 9)
- {
- c->lazy++;
- assert_match(swd,c->m_len,c->m_off);
- lit += ahead;
- assert(ii + lit == c->bp);
- goto lazy_match_done;
- }
- }
-
- assert(ii + lit + ahead == c->bp);
-
- /* 1 - code run */
- code_run(c,ii,lit);
- lit = 0;
-
- /* 2 - code match */
- code_match(c,m_len,m_off);
- swd->max_chain = SC_MAX_CHAIN;
- r = find_match(c,swd,m_len,1+ahead);
- assert(r == 0);
-
- lazy_match_done: ;
- }
-
- /* store final run */
- code_run(c,ii,lit);
-
- /* EOF */
- bbPutBit(c, 0);
- code_prefix_ss11(c, 0x1000000U);
- bbPutByte(c, 0xff);
-
- bbFlushBits(c, 0);
-
- assert(c->textsize == in_len);
- c->codesize = c->bb_op - out;
- *out_len = c->bb_op - out;
-
- #if 0
- printf("%7ld %7ld -> %7ld %7ld %7ld %ld (max: %d %d %d)\n",
- (long) c->textsize, (long) in_len, (long) c->codesize,
- c->match_bytes, c->lit_bytes, c->lazy,
- c->result[1], c->result[3], c->result[5]);
- #endif
- assert(c->lit_bytes + c->match_bytes == in_len);
-
- swd_exit(swd);
- free(swd);
-
- return UCL_E_OK;
- }
-
-
- void Encode(void) /* compression */
- {
- uint8_t *in, *out;
- unsigned long in_len, out_len;
- uint32_t tw;
- int r;
- fseek(infile, 0, SEEK_END);
- in_len = ftell(infile);
- #ifdef VERBOSE
- if ((signed long)in_len < 0)
- Fprintf((stderr, "Errno: %d", errno));
- #endif
- #if UCLPACK_COMPAT
- {
- uint8_t byte;
- if (fwrite(magic, sizeof(magic), 1, outfile) != 1)
- Error("Can't write.");
- tw = htonl(0); /* flags */
- if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
- Error("Can't write.");
- byte = 0x2b; /* method */
- if (fwrite(&byte, sizeof(byte), 1, outfile) != 1)
- Error("Can't write.");
- byte = 10; /* level */
- if (fwrite(&byte, sizeof(byte), 1, outfile) != 1)
- Error("Can't write.");
- tw = htonl(256*1024); /* block_size */
- if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
- Error("Can't write.");
- tw = htonl(in_len);
- if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
- Error("Can't write."); /* output size of text */
- }
- #else
- tw = host_to_i86ul(in_len);
- if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
- Error("Can't write."); /* output size of text */
- #endif
- if (in_len == 0)
- return;
- rewind(infile);
-
- in = malloc(in_len);
- out_len = in_len + (in_len/8) + 256;
- out = malloc(out_len);
- if (!in || !out) {
- Error("Can't malloc");
- }
- if (fread(in, in_len, 1, infile) != 1) {
- Error("Can't read");
- }
- r = ucl_nrv2b_99_compress(in, in_len, out, &out_len, 0 );
- if (r != UCL_E_OK)
- Error("Compression failure\n");
- #if UCLPACK_COMPAT
- tw = htonl(out_len);
- if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
- Error("Can't write."); /* file size of text */
-
- #endif
- if (fwrite(out, out_len, 1, outfile) != 1) {
- Error("Write error\n");
- }
- #if UCLPACK_COMPAT
- tw = htonl(0); /* EOF marker */
- if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
- Error("Can't write.");
-
- #endif
-
- #ifdef LONG_REPORT
- Fprintf((stdout, "input size %ld bytes\n", in_len));
- Fprintf((stdout, "output size %ld bytes\n", out_len));
- Fprintf((stdout, "input/output %.3f\n", (double)in_len / out_len));
- #else
- Fprintf((stdout, "input/output = %ld/%ld = %.3f\n", in_len, out_len,
- (double)in_len / out_len));
- #endif
-
- }
-
- #endif
-
- #ifdef DECODE
-
- #define GETBIT_8(bb, src, ilen) \
- (((bb = bb & 0x7f ? bb*2 : ((unsigned)src[ilen++]*2+1)) >> 8) & 1)
-
- #define GETBIT_LE16(bb, src, ilen) \
- (bb*=2,bb&0xffff ? (bb>>16)&1 : (ilen+=2,((bb=(src[ilen-2]+src[ilen-1]*256u)*2+1)>>16)&1))
-
- #define GETBIT_LE32(bb, src, ilen) \
- (bc > 0 ? ((bb>>--bc)&1) : (bc=31,\
- bb=*(const uint32_t *)((src)+ilen),ilen+=4,(bb>>31)&1))
-
- #define GETBIT_LE64(bb, src, ilen) \
- (bc > 0 ? ((bb>>--bc)&1) : (bc=63, \
- bb=*(const uint64_t *)((src)+ilen),ilen+=8,(bb>>63)&1))
-
- #if ENDIAN == 0 && BITSIZE == 8
- #define GETBIT(bb, src, ilen) GETBIT_8(bb, src, ilen)
- #endif
- #if ENDIAN == 0 && BITSIZE == 16
- #define GETBIT(bb, src, ilen) GETBIT_LE16(bb, src, ilen)
- #endif
- #if ENDIAN == 0 && BITSIZE == 32
- #define GETBIT(bb, src, ilen) GETBIT_LE32(bb, src, ilen)
- #endif
- #if ENDIAN == 0 && BITSIZE == 64
- #define GETBIT(bb, src, ilen) GETBIT_LE64(bb, src, ilen)
- #endif
- #ifndef GETBIT
- #error "Bad Combination of ENDIAN and BITSIZE values specified"
- #endif
-
- #undef SAFE
-
- #ifdef SAFE
- #define FAIL(x,r) if (x) { Error(r); }
- #else
- #define FAIL(x,r)
- #endif
-
- void Decode(void) /* recover */
- {
- uint32_t tw;
- uint8_t *src, *dst;
- unsigned long max_src_len, src_len, dst_len;
- unsigned long ilen = 0, olen = 0, last_m_off = 1;
- #if BITSIZE <= 32
- uint32_t bb = 0;
- #elif BITSIZE == 64
- uint64_t bb = 0;
- #endif
- unsigned bc = 0;
- #if UCLPACK_COMPAT
- if (fseek(infile, sizeof(magic) + sizeof(tw) + 1 + 1 + sizeof(tw),
- SEEK_SET) != 0)
-
- Error("Seek Error");
- if (fread(&tw, sizeof(tw), 1, infile) < 1)
- Error("Can't read"); /* read size of text */
- dst_len = ntohl(tw);
- if (fread(&tw, sizeof(tw), 1, infile) < 1)
- Error("Can't read"); /* read size of file */
- max_src_len = ntohl(tw);
- #else
- if (fread(&tw, sizeof(tw), 1, infile) < 1)
- Error("Can't read"); /* read size of text */
- dst_len = i86ul_to_host(tw);
- max_src_len = dst_len + (dst_len/8) + 256;
- #endif
- if (dst_len == 0)
- return;
- dst = malloc(dst_len);
- if (!dst)
- Error("Can't malloc");
- src = malloc(max_src_len);
- if (!src)
- Error("Can't malloc");
- src_len = fread(src, 1, max_src_len, infile);
- if (src_len <= 0)
- Error("Can't read");
-
- for(;;) {
- unsigned int m_off, m_len;
- while(GETBIT(bb, src, ilen)) {
- FAIL(ilen >= src_len, "input overrun");
- FAIL(olen >= dst_len, "output overrun");
- dst[olen++] = src[ilen++];
- }
- m_off = 1;
- do {
- m_off = m_off*2 + GETBIT(bb, src, ilen);
- FAIL(ilen >= src_len, "input overrun");
- FAIL(m_off > 0xffffffU +3, "lookbehind overrun");
- } while (!GETBIT(bb, src, ilen));
- if (m_off == 2)
- {
- m_off = last_m_off;
- }
- else
- {
- FAIL(ilen >= src_len, "input overrun");
- m_off = (m_off - 3)*256 + src[ilen++];
- if (m_off == 0xffffffffU)
- break;
- last_m_off = ++m_off;
- }
- m_len = GETBIT(bb, src, ilen);
- m_len = m_len*2 + GETBIT(bb, src, ilen);
- if (m_len == 0)
- {
- m_len++;
- do {
- m_len = m_len*2 + GETBIT(bb, src, ilen);
- FAIL(ilen >= src_len, "input overrun");
- FAIL(m_len >= dst_len, "output overrun");
- } while(!GETBIT(bb, src, ilen));
- m_len += 2;
- }
- m_len += (m_off > 0xd00);
- FAIL(olen + m_len > dst_len, "output overrun");
- FAIL(m_off > olen, "lookbeind overrun");
- {
- const uint8_t *m_pos;
- m_pos = dst + olen - m_off;
- dst[olen++] = *m_pos++;
- do {
- dst[olen++] = *m_pos++;
- } while(--m_len > 0);
- }
- }
- FAIL(ilen < src_len, "input not consumed");
- FAIL(ilen > src_len, "input overrun");
- assert(ilen == src_len);
- Fprintf((stderr, "%12ld\n", olen));
- if (dst_len != olen) {
- fprintf(stderr, "length != expected length\n");
- }
- if (fwrite(dst, olen, 1, outfile) != 1)
- Error("Write error\n");
- free(src);
- free(dst);
- }
- #endif
-
- #ifdef MAIN
- int main(int argc, char *argv[])
- {
- char *s;
- FILE *f;
- int c;
-
- if (argc == 2) {
- outfile = stdout;
- if ((f = tmpfile()) == NULL) {
- perror("tmpfile");
- return EXIT_FAILURE;
- }
- while ((c = getchar()) != EOF)
- fputc(c, f);
- rewind(infile = f);
- }
- else if (argc != 4) {
- Fprintf((stderr, "'nrv2b e file1 file2' encodes file1 into file2.\n"
- "'nrv2b d file2 file1' decodes file2 into file1.\n"));
- return EXIT_FAILURE;
- }
- if (argc == 4) {
- if ((s = argv[1], s[1] || strpbrk(s, "DEde") == NULL)
- || (s = argv[2], (infile = fopen(s, "rb")) == NULL)
- || (s = argv[3], (outfile = fopen(s, "wb")) == NULL)) {
- Fprintf((stderr, "??? %s\n", s));
- return EXIT_FAILURE;
- }
- }
- if (toupper(*argv[1]) == 'E')
- Encode();
- else
- Decode();
- fclose(infile);
- fclose(outfile);
- return EXIT_SUCCESS;
- }
- #endif
|