comparison src/lzfu.c @ 78:535075b4d261

Patch from Joachim Metz for debian packaging, and fix for incorrect length on lz decompression.
author Carl Byington <carl@five-ten-sg.com>
date Tue, 10 Jun 2008 14:28:55 -0700
parents 3cb02cb1e6cd
children 56fa05fd5271
comparison
equal deleted inserted replaced
77:87216aefc6df 78:535075b4d261
68 //printf("total size: %d\n", lzfuhdr.cbSize+4); 68 //printf("total size: %d\n", lzfuhdr.cbSize+4);
69 //printf("raw size : %d\n", lzfuhdr.cbRawSize); 69 //printf("raw size : %d\n", lzfuhdr.cbRawSize);
70 //printf("compressed: %s\n", (lzfuhdr.dwMagic == LZFU_COMPRESSED ? "yes" : "no")); 70 //printf("compressed: %s\n", (lzfuhdr.dwMagic == LZFU_COMPRESSED ? "yes" : "no"));
71 //printf("CRC : %#x\n", lzfuhdr.dwCRC); 71 //printf("CRC : %#x\n", lzfuhdr.dwCRC);
72 //printf("\n"); 72 //printf("\n");
73 out_size = lzfuhdr.cbRawSize + 3; // two braces and a null terminator 73 out_size = lzfuhdr.cbRawSize;
74 out_buf = (char*)xmalloc(out_size); 74 out_buf = (char*)xmalloc(out_size);
75 in_ptr = sizeof(lzfuhdr); 75 in_ptr = sizeof(lzfuhdr);
76 in_size = (lzfuhdr.cbSize < compsize) ? lzfuhdr.cbSize : compsize; 76 // Make sure to correct lzfuhdr.cbSize with 4 bytes before comparing
77 // to compsize
78 in_size = (lzfuhdr.cbSize + 4 < compsize) ? lzfuhdr.cbSize + 4 : compsize;
77 while (in_ptr < in_size) { 79 while (in_ptr < in_size) {
78 flags = (unsigned char)(rtfcomp[in_ptr++]); 80 flags = (unsigned char)(rtfcomp[in_ptr++]);
79 flag_mask = 1; 81 flag_mask = 1;
80 while (flag_mask) { 82 while (flag_mask) {
81 if (flag_mask & flags) { 83 if (flag_mask & flags) {
98 unsigned char c1; 100 unsigned char c1;
99 c1 = dict[(offset+i)%4096]; 101 c1 = dict[(offset+i)%4096];
100 dict[dict_length]=c1; 102 dict[dict_length]=c1;
101 dict_length = (dict_length+1) % 4096; 103 dict_length = (dict_length+1) % 4096;
102 if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1; 104 if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1;
105 // required for dictionary wrap around
106 // otherwise 0 byte values are referenced incorrectly
107 dict[dict_length] = 0;
103 } 108 }
104 } 109 }
105 } else { 110 } else {
106 // one byte available? 111 // one byte available?
107 if (in_ptr < in_size) { 112 if (in_ptr < in_size) {
108 // uncompressed chunk (single byte) 113 // uncompressed chunk (single byte)
109 char c1 = rtfcomp[in_ptr++]; 114 char c1 = rtfcomp[in_ptr++];
110 dict[dict_length] = c1; 115 dict[dict_length] = c1;
111 dict_length = (dict_length+1)%4096; 116 dict_length = (dict_length+1)%4096;
112 if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1; 117 if (out_ptr < out_size) out_buf[out_ptr++] = (char)c1;
118 // required for dictionary wrap around
119 // otherwise 0 byte values are referenced incorrect
120 dict[dict_length] = 0;
113 } 121 }
114 } 122 }
115 flag_mask <<= 1; 123 flag_mask <<= 1;
116 } 124 }
117 } 125 }
118 // the compressed version doesn't appear to drop the closing 126 // the RTF data is terminated with }}\0\0
119 // braces onto the doc, so we do that here.
120 if (out_ptr < out_size) out_buf[out_ptr++] = '}';
121 if (out_ptr < out_size) out_buf[out_ptr++] = '}';
122 *size = out_ptr;
123 if (out_ptr < out_size) out_buf[out_ptr++] = '\0';
124 return out_buf; 127 return out_buf;
125 } 128 }