lzfu.c

Go to the documentation of this file.
00001  /*
00002      This program is free software; you can redistribute it and/or modify
00003      it under the terms of the GNU General Public License as published by
00004      the Free Software Foundation; either version 2 of the License, or
00005      (at your option) any later version.
00006 
00007      You should have received a copy of the GNU General Public License
00008      along with this program; if not, write to the Free Software Foundation,
00009      Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
00010   */
00011 
00012 #include "define.h"
00013 #include "lzfu.h"
00014 
00015 
00016 #define LZFU_COMPRESSED         0x75465a4c
00017 #define LZFU_UNCOMPRESSED       0x414c454d
00018 
00019 // initital dictionary
00020 #define LZFU_INITDICT   "{\\rtf1\\ansi\\mac\\deff0\\deftab720{\\fonttbl;}" \
00021                         "{\\f0\\fnil \\froman \\fswiss \\fmodern \\fscrip" \
00022                         "t \\fdecor MS Sans SerifSymbolArialTimes Ne" \
00023                         "w RomanCourier{\\colortbl\\red0\\green0\\blue0" \
00024                         "\r\n\\par \\pard\\plain\\f0\\fs20\\b\\i\\u\\tab" \
00025                         "\\tx"
00026 // initial length of dictionary
00027 #define LZFU_INITLENGTH 207
00028 
00029 // header for compressed rtf
00030 typedef struct _lzfuheader {
00031     uint32_t cbSize;
00032     uint32_t cbRawSize;
00033     uint32_t dwMagic;
00034     uint32_t dwCRC;
00035 } lzfuheader;
00036 
00037 
00038 char* pst_lzfu_decompress(char* rtfcomp, uint32_t compsize, size_t *size) {
00039     unsigned char dict[4096];       // the dictionary buffer
00040     unsigned int dict_length = 0;   // the dictionary pointer
00041     lzfuheader lzfuhdr;             // the header of the lzfu block
00042     unsigned char flags;            // 8 bits of flags (1=2byte block pointer into the dict, 0=1 byte literal)
00043     unsigned char flag_mask;        // look at one flag bit each time thru the loop
00044     uint32_t i;
00045     char    *out_buf;
00046     uint32_t out_ptr = 0;
00047     uint32_t out_size;
00048     uint32_t in_ptr;
00049     uint32_t in_size;
00050 
00051     memcpy(dict, LZFU_INITDICT, LZFU_INITLENGTH);
00052     memset(dict + LZFU_INITLENGTH, 0, sizeof(dict) - LZFU_INITLENGTH);
00053     dict_length = LZFU_INITLENGTH;
00054 
00055     memcpy(&lzfuhdr, rtfcomp, sizeof(lzfuhdr));
00056     LE32_CPU(lzfuhdr.cbSize);
00057     LE32_CPU(lzfuhdr.cbRawSize);
00058     LE32_CPU(lzfuhdr.dwMagic);
00059     LE32_CPU(lzfuhdr.dwCRC);
00060     //printf("total size: %d\n", lzfuhdr.cbSize+4);
00061     //printf("raw size  : %d\n", lzfuhdr.cbRawSize);
00062     //printf("compressed: %s\n", (lzfuhdr.dwMagic == LZFU_COMPRESSED ? "yes" : "no"));
00063     //printf("CRC       : %#x\n", lzfuhdr.dwCRC);
00064     //printf("\n");
00065     out_size = lzfuhdr.cbRawSize;
00066     out_buf  = (char*)pst_malloc(out_size);
00067     in_ptr   = sizeof(lzfuhdr);
00068     // Make sure to correct lzfuhdr.cbSize with 4 bytes before comparing
00069     // to compsize
00070     in_size  = (lzfuhdr.cbSize + 4 < compsize) ? lzfuhdr.cbSize + 4 : compsize;
00071     while (in_ptr < in_size) {
00072         flags = (unsigned char)(rtfcomp[in_ptr++]);
00073         flag_mask = 1;
00074         while (flag_mask) {
00075             if (flag_mask & flags) {
00076                 // two bytes available?
00077                 if (in_ptr+1 < in_size) {
00078                     // read 2 bytes from input
00079                     uint16_t blkhdr, offset, length;
00080                     memcpy(&blkhdr, rtfcomp+in_ptr, 2);
00081                     LE16_CPU(blkhdr);
00082                     in_ptr += 2;
00083                     /* swap the upper and lower bytes of blkhdr */
00084                     blkhdr = (((blkhdr&0xFF00)>>8)+
00085                               ((blkhdr&0x00FF)<<8));
00086                     /* the offset is the first 12 bits of the 16 bit value */
00087                     offset = (blkhdr&0xFFF0)>>4;
00088                     /* the length of the dict entry are the last 4 bits */
00089                     length = (blkhdr&0x000F)+2;
00090                     // add the value we are about to print to the dictionary
00091                     for (i=0; i < length; i++) {
00092                         unsigned char c1;
00093                         c1 = dict[(offset+i)%4096];
00094                         dict[dict_length] = c1;
00095                         dict_length = (dict_length+1) % 4096;
00096                         if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1;
00097                         // required for dictionary wrap around
00098                         // otherwise 0 byte values are referenced incorrectly
00099                         dict[dict_length] = 0;
00100                     }
00101                 }
00102             } else {
00103                 // one byte available?
00104                 if (in_ptr < in_size) {
00105                     // uncompressed chunk (single byte)
00106                     char c1 = rtfcomp[in_ptr++];
00107                     dict[dict_length] = c1;
00108                     dict_length = (dict_length+1)%4096;
00109                     if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1;
00110                     // required for dictionary wrap around
00111                     // otherwise 0 byte values are referenced incorrect
00112                     dict[dict_length] = 0;
00113                 }
00114             }
00115             flag_mask <<= 1;
00116         }
00117     }
00118     *size = out_ptr;
00119     return out_buf;
00120 }

Generated on 6 Jul 2016 for 'LibPst' by  doxygen 1.6.1