annotate src/lzfu.c @ 33:12cac756bc05 stable-0-5-5

enable -d option, but if not specified, don't generate a debug file
author carl
date Tue, 10 Jul 2007 20:23:55 -0700
parents c508ee15dfca
children 6fe121a971c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
1 /*
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
2 This program is free software; you can redistribute it and/or modify
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 it under the terms of the GNU General Public License as published by
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 the Free Software Foundation; either version 2 of the License, or
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 (at your option) any later version.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7 You should have received a copy of the GNU General Public License
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 along with this program; if not, write to the Free Software Foundation,
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 #include "define.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13 #include "libpst.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14 #include <sys/types.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15 #include <string.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16 #include <stdio.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18 #ifndef _MSC_VER
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 #include <stdint.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22 #ifdef _MSC_VER
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23 #define uint32_t unsigned int
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
24 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
25
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
26 #include "lzfu.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
27
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
28 #define LZFU_COMPRESSED 0x75465a4c
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
29 #define LZFU_UNCOMPRESSED 0x414c454d
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
30
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
31 // initital dictionary
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
32 #define LZFU_INITDICT "{\\rtf1\\ansi\\mac\\deff0\\deftab720{\\fonttbl;}" \
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
33 "{\\f0\\fnil \\froman \\fswiss \\fmodern \\fscrip" \
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
34 "t \\fdecor MS Sans SerifSymbolArialTimes Ne" \
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
35 "w RomanCourier{\\colortbl\\red0\\green0\\blue0" \
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
36 "\r\n\\par \\pard\\plain\\f0\\fs20\\b\\i\\u\\tab" \
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
37 "\\tx"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
38 // initial length of dictionary
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
39 #define LZFU_INITLENGTH 207
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
40
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
41 // header for compressed rtf
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
42 typedef struct _lzfuheader {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
43 uint32_t cbSize;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
44 uint32_t cbRawSize;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
45 uint32_t dwMagic;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
46 uint32_t dwCRC;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
47 } lzfuheader;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
48
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
49
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
50 /**
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
51 We always need to add 0x10 to the buffer offset because we need to skip past the header info
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
54 unsigned char* lzfu_decompress (unsigned char* rtfcomp) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
55 // the dictionary buffer
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
56 unsigned char dict[4096];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
57 // the dictionary pointer
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
58 unsigned int dict_length=0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
59 // the header of the lzfu block
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
60 lzfuheader lzfuhdr;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
61 // container for the data blocks
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
62 unsigned char flags;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
63 // temp value for determining the bits in the flag
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
64 unsigned char flag_mask;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
65 unsigned int i, in_size;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
66 unsigned char *out_buf;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
67 unsigned int out_ptr = 0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
68
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69 memcpy(dict, LZFU_INITDICT, LZFU_INITLENGTH);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
70 dict_length = LZFU_INITLENGTH;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
71 memcpy(&lzfuhdr, rtfcomp, sizeof(lzfuhdr));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
72 LE32_CPU(lzfuhdr.cbSize); LE32_CPU(lzfuhdr.cbRawSize);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73 LE32_CPU(lzfuhdr.dwMagic); LE32_CPU(lzfuhdr.dwCRC);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
74 /* printf("total size: %d\n", lzfuhdr.cbSize+4);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
75 printf("raw size : %d\n", lzfuhdr.cbRawSize);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76 printf("compressed: %s\n", (lzfuhdr.dwMagic == LZFU_COMPRESSED ? "yes" : "no"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
77 printf("CRC : %#x\n", lzfuhdr.dwCRC);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
78 printf("\n");*/
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
79 out_buf = (unsigned char*)xmalloc(lzfuhdr.cbRawSize+20); //plus 4 cause we have 2x'}' and a \0
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
80 in_size = 0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
81 // we add plus one here cause when referencing an array, the index is always one less
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
82 // (ie, when accessing 2 element array, highest index is [1])
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
83 while (in_size+0x11 < lzfuhdr.cbSize) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
84 memcpy(&flags, &(rtfcomp[in_size+0x10]), 1);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
85 in_size += 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
86
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
87 flag_mask = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
88 while (flag_mask != 0 && in_size+0x11 < lzfuhdr.cbSize) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
89 if (flag_mask & flags) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
90 // read 2 bytes from input
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
91 unsigned short int blkhdr, offset, length;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
92 memcpy(&blkhdr, &(rtfcomp[in_size+0x10]), 2);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
93 LE16_CPU(blkhdr);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
94 in_size += 2;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
95 /* swap the upper and lower bytes of blkhdr */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
96 blkhdr = (((blkhdr&0xFF00)>>8)+
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
97 ((blkhdr&0x00FF)<<8));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
98 /* the offset is the first 24 bits of the 32 bit value */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
99 offset = (blkhdr&0xFFF0)>>4;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
100 /* the length of the dict entry are the last 8 bits */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
101 length = (blkhdr&0x000F)+2;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
102 // add the value we are about to print to the dictionary
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
103 for (i=0; i < length; i++) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
104 unsigned char c1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
105 c1 = dict[(offset+i)%4096];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
106 dict[dict_length]=c1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
107 dict_length = (dict_length+1) % 4096;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
108 out_buf[out_ptr++] = c1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
109 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
110 } else {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
111 // uncompressed chunk (single byte)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
112 char c1 = rtfcomp[in_size+0x10];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
113 in_size ++;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
114 dict[dict_length] = c1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
115 dict_length = (dict_length+1)%4096;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
116 out_buf[out_ptr++] = c1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
117 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
118 flag_mask <<= 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
119 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
120 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
121 // the compressed version doesn't appear to drop the closing braces onto the doc.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
122 // we should do that
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
123 out_buf[out_ptr++] = '}';
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
124 out_buf[out_ptr++] = '}';
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
125 out_buf[out_ptr++] = '\0';
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
126 return out_buf;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
127 }