1 /**************************************************************
2 Form adapted from lzhuf.c
3 written by Haruyasu Yoshizaki 11/20/1988
4 some minor changes 4/6/1989
5 comments translated by Haruhiko Okumura 4/7/1989
7 minor beautifications and adjustments for compiling under Linux
8 by Markus Gutschke <gutschk@math.uni-muenster.de>
11 Modifications to allow use as a filter by Ken Yap
12 <ken_yap@users.sourceforge.net>.
16 Small mod to cope with running on big-endian machines
17 by Jim Hague <jim.hague@acm.org)
20 Make compression statistics report shorter
21 by Ken Yap <ken_yap@users.sourceforge.net>.
24 Replaced algorithm with nrv2b from ucl the compression
25 library from upx. That code is:
26 Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
27 And is distributed under the terms of the GPL.
28 The conversion was performed
29 by Eric Biederman <ebiederman@lnxi.com>.
32 **************************************************************/
33 #define UCLPACK_COMPAT 0
43 #include <netinet/in.h>
51 #define Fprintf(x) fprintf x
57 FILE *infile, *outfile;
59 #if defined(ENCODE) || defined(DECODE)
68 static __inline__ __attribute__((noreturn)) void Error(char *message)
70 Fprintf((stderr, "\n%s\n", message));
74 /* These will be a complete waste of time on a lo-endian */
75 /* system, but it only gets done once so WTF. */
76 static unsigned long i86ul_to_host(unsigned long ul)
78 unsigned long res = 0;
87 for (i = 3; i >= 0; i--)
88 res = (res << 8) + u.c[i];
92 static unsigned long host_to_i86ul(unsigned long ul)
101 for (i = 0; i < 4; i++)
113 /* magic file header for compressed files */
114 static const unsigned char magic[8] =
115 { 0x00, 0xe9, 0x55, 0x43, 0x4c, 0xff, 0x01, 0x1a };
120 /********** NRV2B_99 compression **********/
122 #define N (1024*1024ul) /* size of ring buffer */
123 #define THRESHOLD 1 /* lower limit for match length */
124 #define F 2048 /* upper limit for match length */
125 #define M2_MAX_OFFSET 0xd00
127 /* note: to use default values pass -1, i.e. initialize
128 * this struct by a memset(x,0xff,sizeof(x)) */
129 struct ucl_compress_config
133 unsigned int max_offset;
134 unsigned int max_match;
146 unsigned int look; /* bytes in lookahead buffer */
151 unsigned int last_m_len;
152 unsigned int last_m_off;
154 const unsigned char *bp;
155 const unsigned char *ip;
156 const unsigned char *in;
157 const unsigned char *in_end;
162 unsigned bb_c_endian;
166 unsigned char *bb_op;
168 struct ucl_compress_config conf;
169 unsigned int *result;
171 unsigned int textsize; /* text size counter */
172 unsigned int codesize; /* code size counter */
173 unsigned int printcount; /* counter for reporting progress every 1K
178 unsigned long lit_bytes;
179 unsigned long match_bytes;
180 unsigned long rep_bytes;
186 #define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
189 #define UCL_E_INVALID_ARGUMENT 1
190 #define UCL_E_OUT_OF_MEMORY 2
191 #define UCL_E_ERROR 3
193 /***********************************************************************
195 ************************************************************************/
197 #define SWD_HSIZE 16384
198 #define SWD_MAX_CHAIN 2048
201 (((0x9f5f*(((((uint32_t)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
203 #define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
204 #define NIL2 UINT_MAX
208 /* public - "built-in" */
211 unsigned int threshold;
213 /* public - configuration */
214 unsigned int max_chain;
215 unsigned int nice_length;
217 unsigned int lazy_insert;
219 /* public - output */
224 #if defined(SWD_BEST_OFF)
225 unsigned int best_off[ SWD_BEST_OFF ];
229 struct ucl_compress *c;
231 #if defined(SWD_BEST_OFF)
232 unsigned int best_pos[ SWD_BEST_OFF ];
237 const uint8_t *dict_end;
238 unsigned int dict_len;
241 unsigned int ip; /* input pointer (lookahead) */
242 unsigned int bp; /* buffer pointer */
243 unsigned int rp; /* remove pointer */
246 unsigned char *b_wrap;
248 unsigned int node_count;
249 unsigned int first_rp;
251 unsigned char b [ N + F + F ];
252 unsigned int head3 [ SWD_HSIZE ];
253 unsigned int succ3 [ N + F ];
254 unsigned int best3 [ N + F ];
255 unsigned int llen3 [ SWD_HSIZE ];
256 unsigned int head2 [ 65536U ];
259 #define s_head3(s,key) s->head3[key]
262 #if !defined( NDEBUG)
263 static void assert_match(const struct ucl_swd * swd, unsigned int m_len,
267 const struct ucl_compress *c = swd->c;
271 if (m_off <= (unsigned int) (c->bp - c->in))
273 assert(c->bp - m_off + m_len < c->ip);
274 assert(memcmp(c->bp, c->bp - m_off, m_len) == 0);
278 assert(swd->dict != NULL);
279 d_off = m_off - (unsigned int) (c->bp - c->in);
280 assert(d_off <= swd->dict_len);
283 assert(memcmp(c->bp, swd->dict_end - d_off, d_off) ==
286 assert(c->in + m_len - d_off < c->ip);
287 assert(memcmp(c->bp + d_off, c->in, m_len - d_off) ==
293 assert(memcmp(c->bp, swd->dict_end - d_off, m_len) ==
300 # define assert_match(a,b,c) ((void)0)
303 /***********************************************************************
305 ************************************************************************/
309 void swd_initdict(struct ucl_swd *s, const uint8_t *dict, unsigned int dict_len)
312 s->dict = s->dict_end = NULL;
315 if (!dict || dict_len <= 0)
319 dict += dict_len - s->n;
324 s->dict_len = dict_len;
325 s->dict_end = dict + dict_len;
326 memcpy(s->b,dict,dict_len);
332 void swd_insertdict(struct ucl_swd *s, unsigned int node, unsigned int len)
336 s->node_count = s->n - len;
341 key = HEAD3(s->b,node);
342 s->succ3[node] = s_head3(s,key);
343 s->head3[key] = (unsigned int)(node);
344 s->best3[node] = (unsigned int)(s->f + 1);
346 assert(s->llen3[key] <= s->n);
348 key = HEAD2(s->b,node);
349 s->head2[key] = (unsigned int)(node);
355 /***********************************************************************
357 ************************************************************************/
361 int swd_init(struct ucl_swd *s, const uint8_t *dict, unsigned int dict_len)
370 s->threshold = THRESHOLD;
371 if (s->n > N || s->f > F)
372 return UCL_E_INVALID_ARGUMENT;
375 s->max_chain = SWD_MAX_CHAIN;
376 s->nice_length = s->f;
380 s->b_size = s->n + s->f;
381 if (s->b_size + s->f >= UINT_MAX)
383 s->b_wrap = s->b + s->b_size;
384 s->node_count = s->n;
386 memset(s->llen3, 0, sizeof(s->llen3[0]) * SWD_HSIZE);
387 for (i = 0; i < 65536U; i++)
391 swd_initdict(s,dict,dict_len);
395 assert(s->ip + s->f <= s->b_size);
397 s->look = (unsigned int) (s->c->in_end - s->c->ip);
402 memcpy(&s->b[s->ip],s->c->ip,s->look);
406 if (s->ip == s->b_size)
409 if (s->look >= 2 && s->dict_len > 0)
410 swd_insertdict(s,0,s->dict_len);
413 if (s->rp >= s->node_count)
414 s->rp -= s->node_count;
416 s->rp += s->b_size - s->node_count;
425 void swd_exit(struct ucl_swd *s)
431 #define swd_pos2off(s,pos) \
432 (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
434 /***********************************************************************
436 ************************************************************************/
439 void swd_getbyte(struct ucl_swd *s)
443 if ((c = getbyte(*(s->c))) < 0)
450 s->b[s->ip] = (uint8_t)(c);
452 s->b_wrap[s->ip] = (uint8_t)(c);
454 if (++s->ip == s->b_size)
456 if (++s->bp == s->b_size)
458 if (++s->rp == s->b_size)
461 /***********************************************************************
462 // remove node from lists
463 ************************************************************************/
466 void swd_remove_node(struct ucl_swd *s, unsigned int node)
468 if (s->node_count == 0)
473 if (s->first_rp != UINT_MAX)
475 if (node != s->first_rp)
476 printf("Remove %5d: %5d %5d %5d %5d %6d %6d\n",
478 node, s->rp, s->ip, s->bp, s->first_rp,
479 s->ip - node, s->ip - s->bp);
480 assert(node == s->first_rp);
481 s->first_rp = UINT_MAX;
485 key = HEAD3(s->b,node);
486 assert(s->llen3[key] > 0);
489 key = HEAD2(s->b,node);
490 assert(s->head2[key] != NIL2);
491 if ((unsigned int) s->head2[key] == node)
492 s->head2[key] = NIL2;
499 /***********************************************************************
501 ************************************************************************/
505 void swd_accept(struct ucl_swd *s, unsigned int n)
507 assert(n <= s->look);
513 swd_remove_node(s,s->rp);
515 /* add bp into HEAD3 */
516 key = HEAD3(s->b,s->bp);
517 s->succ3[s->bp] = s_head3(s,key);
518 s->head3[key] = (unsigned int)(s->bp);
519 s->best3[s->bp] = (unsigned int)(s->f + 1);
521 assert(s->llen3[key] <= s->n);
523 /* add bp into HEAD2 */
524 key = HEAD2(s->b,s->bp);
525 s->head2[key] = (unsigned int)(s->bp);
531 /***********************************************************************
533 ************************************************************************/
536 void swd_search(struct ucl_swd *s, unsigned int node, unsigned int cnt)
538 const unsigned char *p1;
539 const unsigned char *p2;
540 const unsigned char *px;
542 unsigned int m_len = s->m_len;
543 const unsigned char * b = s->b;
544 const unsigned char * bp = s->b + s->bp;
545 const unsigned char * bx = s->b + s->bp + s->look;
546 unsigned char scan_end1;
548 assert(s->m_len > 0);
550 scan_end1 = bp[m_len - 1];
551 for ( ; cnt-- > 0; node = s->succ3[node])
557 assert(m_len < s->look);
560 p2[m_len - 1] == scan_end1 &&
561 p2[m_len] == p1[m_len] &&
566 assert(memcmp(bp,&b[node],3) == 0);
569 do {} while (++p1 < px && *p1 == *++p2);
573 if (memcmp(bp,&b[node],i) != 0)
574 printf("%5ld %5ld %02x%02x %02x%02x\n",
575 (long)s->bp, (long) node,
576 bp[0], bp[1], b[node], b[node+1]);
578 assert(memcmp(bp,&b[node],i) == 0);
580 #if defined(SWD_BEST_OFF)
581 if (i < SWD_BEST_OFF)
583 if (s->best_pos[i] == 0)
584 s->best_pos[i] = node + 1;
589 s->m_len = m_len = i;
591 if (m_len == s->look)
593 if (m_len >= s->nice_length)
595 if (m_len > (unsigned int) s->best3[node])
597 scan_end1 = bp[m_len - 1];
603 static int swd_search2(struct ucl_swd *s)
607 assert(s->look >= 2);
608 assert(s->m_len > 0);
610 key = s->head2[ HEAD2(s->b,s->bp) ];
614 if (memcmp(&s->b[s->bp],&s->b[key],2) != 0)
615 printf("%5ld %5ld %02x%02x %02x%02x\n", (long)s->bp, (long)key,
616 s->b[s->bp], s->b[s->bp+1], s->b[key], s->b[key+1]);
618 assert(memcmp(&s->b[s->bp],&s->b[key],2) == 0);
619 #if defined(SWD_BEST_OFF)
620 if (s->best_pos[2] == 0)
621 s->best_pos[2] = key + 1;
632 /***********************************************************************
634 ************************************************************************/
637 void swd_findbest(struct ucl_swd *s)
640 unsigned int cnt, node;
643 assert(s->m_len > 0);
645 /* get current head, add bp into HEAD3 */
646 key = HEAD3(s->b,s->bp);
647 node = s->succ3[s->bp] = s_head3(s,key);
648 cnt = s->llen3[key]++;
649 assert(s->llen3[key] <= s->n + s->f);
650 if (cnt > s->max_chain && s->max_chain > 0)
652 s->head3[key] = (unsigned int)(s->bp);
654 s->b_char = s->b[s->bp];
656 if (s->m_len >= s->look)
661 s->best3[s->bp] = (unsigned int)(s->f + 1);
667 swd_search(s,node,cnt);
669 s->m_off = swd_pos2off(s,s->m_pos);
670 s->best3[s->bp] = (unsigned int)(s->m_len);
672 #if defined(SWD_BEST_OFF)
676 for (i = 2; i < SWD_BEST_OFF; i++)
677 if (s->best_pos[i] > 0)
679 swd_pos2off(s,s->best_pos[i]-1);
687 swd_remove_node(s,s->rp);
689 /* add bp into HEAD2 */
690 key = HEAD2(s->b,s->bp);
691 s->head2[key] = (unsigned int)(s->bp);
695 /***********************************************************************
697 ************************************************************************/
700 init_match ( struct ucl_compress *c, struct ucl_swd *s,
701 const uint8_t *dict, unsigned int dict_len,
711 c->last_m_len = c->last_m_off = 0;
713 c->textsize = c->codesize = c->printcount = 0;
714 c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
717 r = swd_init(s,dict,dict_len);
724 s->use_best_off = (flags & 1) ? 1 : 0;
729 find_match ( struct ucl_compress *c, struct ucl_swd *s,
730 unsigned int this_len, unsigned int skip )
736 assert(this_len >= skip);
737 swd_accept(s, this_len - skip);
738 c->textsize += this_len - skip + 1;
742 assert(this_len <= 1);
743 c->textsize += this_len - skip;
746 s->m_len = THRESHOLD;
749 memset(s->best_pos,0,sizeof(s->best_pos));
765 c->look = s->look + 1;
767 c->bp = c->ip - c->look;
770 /* brute force match search */
771 if (c->m_len > THRESHOLD && c->m_len + 1 <= c->look)
773 const uint8_t *ip = c->bp;
774 const uint8_t *m = c->bp - c->m_off;
775 const uint8_t *in = c->in;
786 if (memcmp(in,ip,c->m_len+1) == 0)
787 printf("%p %p %p %5d\n",
788 in, ip, m, c->m_len);
799 static int bbConfig(struct ucl_compress *c, int endian, int bitsize)
805 c->bb_c_endian = endian;
809 if (bitsize != 8 && bitsize != 16 && bitsize != 32)
812 c->bb_c_s8 = bitsize / 8;
814 c->bb_b = 0; c->bb_k = 0;
820 static void bbWriteBits(struct ucl_compress *c)
822 uint8_t *p = c->bb_p;
823 uint32_t b = c->bb_b;
825 p[0] = (uint8_t)(b >> 0);
828 p[1] = (uint8_t)(b >> 8);
831 p[2] = (uint8_t)(b >> 16);
832 p[3] = (uint8_t)(b >> 24);
838 static void bbPutBit(struct ucl_compress *c, unsigned bit)
840 assert(bit == 0 || bit == 1);
841 assert(c->bb_k <= c->bb_c_s);
843 if (c->bb_k < c->bb_c_s)
847 assert(c->bb_p == NULL);
849 c->bb_op += c->bb_c_s8;
851 assert(c->bb_p != NULL);
852 assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
854 c->bb_b = (c->bb_b << 1) + bit;
859 assert(c->bb_p != NULL);
860 assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
864 c->bb_op += c->bb_c_s8;
871 static void bbPutByte(struct ucl_compress *c, unsigned b)
873 /**printf("putbyte %p %p %x (%d)\n", op, bb_p, x, bb_k);*/
874 assert(c->bb_p == NULL || c->bb_p + c->bb_c_s8 <= c->bb_op);
875 *c->bb_op++ = (uint8_t)(b);
878 static void bbFlushBits(struct ucl_compress *c, unsigned filler_bit)
882 assert(c->bb_k <= c->bb_c_s);
883 while (c->bb_k != c->bb_c_s)
884 bbPutBit(c, filler_bit);
893 /***********************************************************************
895 ************************************************************************/
898 static void code_prefix_ss11(struct ucl_compress *c, uint32_t i)
910 bbPutBit(c, (i & t) ? 1 : 0);
914 bbPutBit(c, (unsigned)i & 1);
919 code_match(struct ucl_compress *c, unsigned int m_len, const unsigned int m_off)
922 while (m_len > c->conf.max_match)
924 code_match(c, c->conf.max_match - 3, m_off);
925 m_len -= c->conf.max_match - 3;
928 c->match_bytes += m_len;
929 if (m_len > c->result[3])
930 c->result[3] = m_len;
931 if (m_off > c->result[1])
932 c->result[1] = m_off;
936 if (m_off == c->last_m_off)
943 code_prefix_ss11(c, 1 + ((m_off - 1) >> 8));
944 bbPutByte(c, (unsigned)m_off - 1);
946 m_len = m_len - 1 - (m_off > M2_MAX_OFFSET);
951 code_prefix_ss11(c, m_len - 4);
955 bbPutBit(c, m_len > 1);
956 bbPutBit(c, (unsigned)m_len & 1);
959 c->last_m_off = m_off;
963 code_run(struct ucl_compress *c, const uint8_t *ii, unsigned int lit)
968 if (lit > c->result[5])
976 /***********************************************************************
978 ************************************************************************/
981 len_of_coded_match(struct ucl_compress *c, unsigned int m_len, unsigned int
986 if (m_len < 2 || (m_len == 2 && (m_off > M2_MAX_OFFSET))
987 || m_off > c->conf.max_offset)
991 m_len = m_len - 2 - (m_off > M2_MAX_OFFSET);
993 if (m_off == c->last_m_off)
998 m_off = (m_off - 1) >> 8;
1014 } while (m_len > 0);
1019 int ucl_nrv2b_99_compress(
1020 const uint8_t *in, unsigned long in_len,
1021 uint8_t *out, unsigned long *out_len,
1022 unsigned int *result)
1026 unsigned int m_len, m_off;
1027 struct ucl_compress c_buffer;
1028 struct ucl_compress * const c = &c_buffer;
1029 struct ucl_swd *swd;
1030 unsigned int result_buffer[16];
1033 /* max compression */
1034 #define SC_TRY_LAZY 2
1035 #define SC_GOOD_LENGTH F
1036 #define SC_MAX_LAZY F
1037 #define SC_NICE_LENGTH F
1038 #define SC_MAX_CHAIN 4096
1040 #define SC_MAX_OFFSET N
1042 memset(c, 0, sizeof(*c));
1044 c->in_end = in + in_len;
1046 c->result = result ? result : result_buffer;
1047 memset(c->result, 0, 16*sizeof(*c->result));
1048 c->result[0] = c->result[2] = c->result[4] = UINT_MAX;
1050 memset(&c->conf, 0xff, sizeof(c->conf));
1051 r = bbConfig(c, ENDIAN, BITSIZE);
1053 r = bbConfig(c, c->conf.bb_endian, c->conf.bb_size);
1055 return UCL_E_INVALID_ARGUMENT;
1058 ii = c->ip; /* point to start of literal run */
1062 swd = (struct ucl_swd *) malloc(sizeof(*swd));
1064 return UCL_E_OUT_OF_MEMORY;
1068 if (in_len >= 256 && in_len < swd->n)
1070 if (swd->f < 8 || swd->n < 256)
1071 return UCL_E_INVALID_ARGUMENT;
1073 r = init_match(c,swd,NULL,0, SC_FLAGS);
1079 if (SC_MAX_CHAIN > 0)
1080 swd->max_chain = SC_MAX_CHAIN;
1081 if (SC_NICE_LENGTH > 0)
1082 swd->nice_length = SC_NICE_LENGTH;
1083 if (c->conf.max_match < swd->nice_length)
1084 swd->nice_length = c->conf.max_match;
1087 r = find_match(c,swd,0,0);
1093 unsigned int max_ahead;
1096 c->codesize = c->bb_op - out;
1101 assert(c->bp == c->ip - c->look);
1102 assert(c->bp >= in);
1105 assert(ii + lit == c->bp);
1106 assert(swd->b_char == *(c->bp));
1108 if (m_len < 2 || (m_len == 2 && (m_off > M2_MAX_OFFSET))
1109 || m_off > c->conf.max_offset)
1113 swd->max_chain = SC_MAX_CHAIN;
1114 r = find_match(c,swd,1,0);
1120 assert_match(swd,m_len,m_off);
1122 /* shall we try a lazy match ? */
1124 if (SC_TRY_LAZY <= 0 || m_len >= SC_MAX_LAZY || m_off ==
1134 /* yes, try a lazy match */
1135 l1 = len_of_coded_match(c,m_len,m_off);
1137 max_ahead = SC_TRY_LAZY;
1138 if ((m_len - 1) < max_ahead) {
1139 max_ahead = m_len -1;
1143 while (ahead < max_ahead && c->look > m_len)
1145 if (m_len >= SC_GOOD_LENGTH)
1146 swd->max_chain = SC_MAX_CHAIN >> 2;
1148 swd->max_chain = SC_MAX_CHAIN;
1149 r = find_match(c,swd,1,0);
1153 assert(c->look > 0);
1154 assert(ii + lit + ahead == c->bp);
1158 l2 = len_of_coded_match(c,c->m_len,c->m_off);
1161 if (l1 + (int)(ahead + c->m_len - m_len) * 5 > l2 +
1165 assert_match(swd,c->m_len,c->m_off);
1167 assert(ii + lit == c->bp);
1168 goto lazy_match_done;
1172 assert(ii + lit + ahead == c->bp);
1178 /* 2 - code match */
1179 code_match(c,m_len,m_off);
1180 swd->max_chain = SC_MAX_CHAIN;
1181 r = find_match(c,swd,m_len,1+ahead);
1187 /* store final run */
1192 code_prefix_ss11(c, 0x1000000U);
1197 assert(c->textsize == in_len);
1198 c->codesize = c->bb_op - out;
1199 *out_len = c->bb_op - out;
1202 printf("%7ld %7ld -> %7ld %7ld %7ld %ld (max: %d %d %d)\n",
1203 (long) c->textsize, (long) in_len, (long) c->codesize,
1204 c->match_bytes, c->lit_bytes, c->lazy,
1205 c->result[1], c->result[3], c->result[5]);
1207 assert(c->lit_bytes + c->match_bytes == in_len);
1216 void Encode(void) /* compression */
1219 unsigned long in_len, out_len;
1222 fseek(infile, 0, SEEK_END);
1223 in_len = ftell(infile);
1225 if ((signed long)in_len < 0)
1226 Fprintf((stderr, "Errno: %d", errno));
1231 if (fwrite(magic, sizeof(magic), 1, outfile) != 1)
1232 Error("Can't write.");
1233 tw = htonl(0); /* flags */
1234 if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
1235 Error("Can't write.");
1236 byte = 0x2b; /* method */
1237 if (fwrite(&byte, sizeof(byte), 1, outfile) != 1)
1238 Error("Can't write.");
1239 byte = 10; /* level */
1240 if (fwrite(&byte, sizeof(byte), 1, outfile) != 1)
1241 Error("Can't write.");
1242 tw = htonl(256*1024); /* block_size */
1243 if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
1244 Error("Can't write.");
1246 if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
1247 Error("Can't write."); /* output size of text */
1250 tw = host_to_i86ul(in_len);
1251 if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
1252 Error("Can't write."); /* output size of text */
1258 in = malloc(in_len);
1259 out_len = in_len + (in_len/8) + 256;
1260 out = malloc(out_len);
1262 Error("Can't malloc");
1264 if (fread(in, in_len, 1, infile) != 1) {
1265 Error("Can't read");
1267 r = ucl_nrv2b_99_compress(in, in_len, out, &out_len, 0 );
1268 if (r != UCL_E_OK) {
1269 Error("Error during compression.");
1272 tw = htonl(out_len);
1273 if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
1274 Error("Can't write."); /* file size of text */
1277 if (fwrite(out, out_len, 1, outfile) != 1) {
1278 Error("Write error\n");
1281 tw = htonl(0); /* EOF marker */
1282 if (fwrite(&tw, sizeof(tw), 1, outfile) != 1)
1283 Error("Can't write.");
1288 Fprintf((stderr, "input size %ld bytes\n", in_len));
1289 Fprintf((stderr, "output size %ld bytes\n", out_len));
1290 Fprintf((stderr, "input/output %.3f\n", (double)in_len / out_len));
1292 Fprintf((stderr, "input/output = %ld/%ld = %.3f\n", in_len, out_len,
1293 (double)in_len / out_len));
1302 #define GETBIT_8(bb, src, ilen) \
1303 (((bb = bb & 0x7f ? bb*2 : ((unsigned)src[ilen++]*2+1)) >> 8) & 1)
1305 #define GETBIT_LE16(bb, src, ilen) \
1306 (bb*=2,bb&0xffff ? (bb>>16)&1 : (ilen+=2,((bb=(src[ilen-2]+src[ilen-1]*256u)*2+1)>>16)&1))
1308 #define GETBIT_LE32(bb, src, ilen) \
1309 (bc > 0 ? ((bb>>--bc)&1) : (bc=31,\
1310 bb=*(const uint32_t *)((src)+ilen),ilen+=4,(bb>>31)&1))
1312 #if ENDIAN == 0 && BITSIZE == 8
1313 #define GETBIT(bb, src, ilen) GETBIT_8(bb, src, ilen)
1315 #if ENDIAN == 0 && BITSIZE == 16
1316 #define GETBIT(bb, src, ilen) GETBIT_LE16(bb, src, ilen)
1318 #if ENDIAN == 0 && BITSIZE == 32
1319 #define GETBIT(bb, src, ilen) GETBIT_LE32(bb, src, ilen)
1323 #error "Bad Combination of ENDIAN and BITSIZE values specified"
1329 #define FAIL(x,r) if (x) { Error(r); }
1334 void Decode(void) /* recover */
1338 unsigned long max_src_len, src_len, dst_len;
1339 unsigned long ilen = 0, olen = 0, last_m_off = 1;
1343 if (fseek(infile, sizeof(magic) + sizeof(tw) + 1 + 1 + sizeof(tw),
1346 Error("Seek Error");
1347 if (fread(&tw, sizeof(tw), 1, infile) < 1)
1348 Error("Can't read"); /* read size of text */
1349 dst_len = ntohl(tw);
1350 if (fread(&tw, sizeof(tw), 1, infile) < 1)
1351 Error("Can't read"); /* read size of file */
1352 max_src_len = ntohl(tw);
1354 if (fread(&tw, sizeof(tw), 1, infile) < 1)
1355 Error("Can't read"); /* read size of text */
1356 dst_len = i86ul_to_host(tw);
1357 max_src_len = dst_len + (dst_len/8) + 256;
1361 dst = malloc(dst_len);
1363 Error("Can't malloc");
1364 src = malloc(max_src_len);
1366 Error("Can't malloc");
1367 src_len = fread(src, 1, max_src_len, infile);
1369 Error("Can't read");
1372 unsigned int m_off, m_len;
1373 while(GETBIT(bb, src, ilen)) {
1374 FAIL(ilen >= src_len, "input overrun");
1375 FAIL(olen >= dst_len, "output overrun");
1376 dst[olen++] = src[ilen++];
1380 m_off = m_off*2 + GETBIT(bb, src, ilen);
1381 FAIL(ilen >= src_len, "input overrun");
1382 FAIL(m_off > 0xffffffU +3, "lookbehind overrun");
1383 } while (!GETBIT(bb, src, ilen));
1390 FAIL(ilen >= src_len, "input overrun");
1391 m_off = (m_off - 3)*256 + src[ilen++];
1392 if(m_off == 0xffffffffU)
1394 last_m_off = ++m_off;
1396 m_len = GETBIT(bb, src, ilen);
1397 m_len = m_len*2 + GETBIT(bb, src, ilen);
1402 m_len = m_len*2 + GETBIT(bb, src, ilen);
1403 FAIL(ilen >= src_len, "input overrun");
1404 FAIL(m_len >= dst_len, "output overrun");
1405 } while(!GETBIT(bb, src, ilen));
1408 m_len += (m_off > 0xd00);
1409 FAIL(olen + m_len > dst_len, "output overrun");
1410 FAIL(m_off > olen, "lookbeind overrun");
1412 const uint8_t *m_pos;
1413 m_pos = dst + olen - m_off;
1414 dst[olen++] = *m_pos++;
1416 dst[olen++] = *m_pos++;
1417 } while(--m_len > 0);
1420 FAIL(ilen < src_len, "input not consumed");
1421 FAIL(ilen > src_len, "input overrun");
1422 assert(ilen == src_len);
1423 Fprintf((stderr, "%12ld\n", olen));
1424 if (dst_len != olen) {
1425 fprintf(stderr, "length != expected length\n");
1427 if (fwrite(dst, olen, 1, outfile) != 1)
1428 Error("Write error\n");
1435 int main(int argc, char *argv[])
1443 if ((f = tmpfile()) == NULL) {
1445 return EXIT_FAILURE;
1447 while ((c = getchar()) != EOF)
1451 else if (argc != 4) {
1452 Fprintf((stderr, "'nrv2b e file1 file2' encodes file1 into file2.\n"
1453 "'nrv2b d file2 file1' decodes file2 into file1.\n"));
1455 return EXIT_FAILURE;
1458 if ((s = argv[1], s[1] || strpbrk(s, "DEde") == NULL)
1459 || (s = argv[2], (infile = fopen(s, "rb")) == NULL)
1460 || (s = argv[3], (outfile = fopen(s, "wb")) == NULL)) {
1461 Fprintf((stderr, "??? %s\n", s));
1462 return EXIT_FAILURE;
1465 if (toupper(*argv[1]) == 'E')
1471 return EXIT_SUCCESS;