annotate lzfu.c @ 5:3e9cea0af4a1

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