annotate src/lzfu.c @ 36:6fe121a971c9 stable-0-5-7

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