libmspack 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +5 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +4 -0
  6. data/README.md +75 -0
  7. data/Rakefile +22 -0
  8. data/UNLICENSE +24 -0
  9. data/ext/Rakefile +16 -0
  10. data/ext/i386-windows/libmspack.dll +0 -0
  11. data/ext/libmspack/AUTHORS +12 -0
  12. data/ext/libmspack/COPYING.LIB +504 -0
  13. data/ext/libmspack/ChangeLog +491 -0
  14. data/ext/libmspack/Makefile.am +100 -0
  15. data/ext/libmspack/NEWS +0 -0
  16. data/ext/libmspack/README +130 -0
  17. data/ext/libmspack/TODO +8 -0
  18. data/ext/libmspack/cleanup.sh +9 -0
  19. data/ext/libmspack/configure.ac +50 -0
  20. data/ext/libmspack/debian/changelog +6 -0
  21. data/ext/libmspack/debian/control +14 -0
  22. data/ext/libmspack/debian/rules +101 -0
  23. data/ext/libmspack/doc/Doxyfile.in +22 -0
  24. data/ext/libmspack/doc/Makefile.in +14 -0
  25. data/ext/libmspack/doc/szdd_kwaj_format.html +331 -0
  26. data/ext/libmspack/libmspack.pc.in +10 -0
  27. data/ext/libmspack/mspack/cab.h +127 -0
  28. data/ext/libmspack/mspack/cabc.c +24 -0
  29. data/ext/libmspack/mspack/cabd.c +1444 -0
  30. data/ext/libmspack/mspack/chm.h +122 -0
  31. data/ext/libmspack/mspack/chmc.c +24 -0
  32. data/ext/libmspack/mspack/chmd.c +1392 -0
  33. data/ext/libmspack/mspack/crc32.c +95 -0
  34. data/ext/libmspack/mspack/crc32.h +17 -0
  35. data/ext/libmspack/mspack/des.h +15 -0
  36. data/ext/libmspack/mspack/hlp.h +33 -0
  37. data/ext/libmspack/mspack/hlpc.c +24 -0
  38. data/ext/libmspack/mspack/hlpd.c +24 -0
  39. data/ext/libmspack/mspack/kwaj.h +118 -0
  40. data/ext/libmspack/mspack/kwajc.c +24 -0
  41. data/ext/libmspack/mspack/kwajd.c +561 -0
  42. data/ext/libmspack/mspack/lit.h +35 -0
  43. data/ext/libmspack/mspack/litc.c +24 -0
  44. data/ext/libmspack/mspack/litd.c +24 -0
  45. data/ext/libmspack/mspack/lzss.h +66 -0
  46. data/ext/libmspack/mspack/lzssd.c +93 -0
  47. data/ext/libmspack/mspack/lzx.h +221 -0
  48. data/ext/libmspack/mspack/lzxc.c +18 -0
  49. data/ext/libmspack/mspack/lzxd.c +895 -0
  50. data/ext/libmspack/mspack/mspack.def +28 -0
  51. data/ext/libmspack/mspack/mspack.h +2353 -0
  52. data/ext/libmspack/mspack/mszip.h +126 -0
  53. data/ext/libmspack/mspack/mszipc.c +18 -0
  54. data/ext/libmspack/mspack/mszipd.c +514 -0
  55. data/ext/libmspack/mspack/oab.h +60 -0
  56. data/ext/libmspack/mspack/oabc.c +24 -0
  57. data/ext/libmspack/mspack/oabd.c +408 -0
  58. data/ext/libmspack/mspack/qtm.h +128 -0
  59. data/ext/libmspack/mspack/qtmc.c +18 -0
  60. data/ext/libmspack/mspack/qtmd.c +489 -0
  61. data/ext/libmspack/mspack/readbits.h +207 -0
  62. data/ext/libmspack/mspack/readhuff.h +173 -0
  63. data/ext/libmspack/mspack/sha.h +15 -0
  64. data/ext/libmspack/mspack/system.c +239 -0
  65. data/ext/libmspack/mspack/system.h +124 -0
  66. data/ext/libmspack/mspack/szdd.h +39 -0
  67. data/ext/libmspack/mspack/szddc.c +24 -0
  68. data/ext/libmspack/mspack/szddd.c +247 -0
  69. data/ext/libmspack/rebuild.sh +8 -0
  70. data/ext/libmspack/test/cabd_c10 +19 -0
  71. data/ext/libmspack/test/cabd_compare +34 -0
  72. data/ext/libmspack/test/cabd_md5.c +161 -0
  73. data/ext/libmspack/test/cabd_memory.c +179 -0
  74. data/ext/libmspack/test/cabd_test.c +386 -0
  75. data/ext/libmspack/test/cabrip.c +81 -0
  76. data/ext/libmspack/test/chmd_compare +38 -0
  77. data/ext/libmspack/test/chmd_find.c +95 -0
  78. data/ext/libmspack/test/chmd_md5.c +67 -0
  79. data/ext/libmspack/test/chmd_order.c +144 -0
  80. data/ext/libmspack/test/chminfo.c +284 -0
  81. data/ext/libmspack/test/chmx.c +216 -0
  82. data/ext/libmspack/test/error.h +22 -0
  83. data/ext/libmspack/test/expand.c +79 -0
  84. data/ext/libmspack/test/md5.c +457 -0
  85. data/ext/libmspack/test/md5.h +165 -0
  86. data/ext/libmspack/test/md5_fh.h +123 -0
  87. data/ext/libmspack/test/msdecompile_md5 +24 -0
  88. data/ext/libmspack/test/msexpand_md5 +39 -0
  89. data/ext/libmspack/test/multifh.c +435 -0
  90. data/ext/libmspack/test/oabx.c +41 -0
  91. data/ext/libmspack/test/test_files/cabd/1.pl +84 -0
  92. data/ext/libmspack/test/test_files/cabd/2.pl +75 -0
  93. data/ext/libmspack/test/test_files/cabd/bad_folderindex.cab +0 -0
  94. data/ext/libmspack/test/test_files/cabd/bad_nofiles.cab +0 -0
  95. data/ext/libmspack/test/test_files/cabd/bad_nofolders.cab +0 -0
  96. data/ext/libmspack/test/test_files/cabd/bad_signature.cab +0 -0
  97. data/ext/libmspack/test/test_files/cabd/multi_basic_pt1.cab +0 -0
  98. data/ext/libmspack/test/test_files/cabd/multi_basic_pt2.cab +0 -0
  99. data/ext/libmspack/test/test_files/cabd/multi_basic_pt3.cab +0 -0
  100. data/ext/libmspack/test/test_files/cabd/multi_basic_pt4.cab +0 -0
  101. data/ext/libmspack/test/test_files/cabd/multi_basic_pt5.cab +0 -0
  102. data/ext/libmspack/test/test_files/cabd/normal_255c_filename.cab +0 -0
  103. data/ext/libmspack/test/test_files/cabd/normal_2files_1folder.cab +0 -0
  104. data/ext/libmspack/test/test_files/cabd/partial_nodata.cab +0 -0
  105. data/ext/libmspack/test/test_files/cabd/partial_nofiles.cab +0 -0
  106. data/ext/libmspack/test/test_files/cabd/partial_nofolder.cab +0 -0
  107. data/ext/libmspack/test/test_files/cabd/partial_shortextheader.cab +0 -0
  108. data/ext/libmspack/test/test_files/cabd/partial_shortfile1.cab +0 -0
  109. data/ext/libmspack/test/test_files/cabd/partial_shortfile2.cab +0 -0
  110. data/ext/libmspack/test/test_files/cabd/partial_shortfolder.cab +0 -0
  111. data/ext/libmspack/test/test_files/cabd/partial_shortheader.cab +0 -0
  112. data/ext/libmspack/test/test_files/cabd/partial_str_nofname.cab +0 -0
  113. data/ext/libmspack/test/test_files/cabd/partial_str_noninfo.cab +0 -0
  114. data/ext/libmspack/test/test_files/cabd/partial_str_nonname.cab +0 -0
  115. data/ext/libmspack/test/test_files/cabd/partial_str_nopinfo.cab +0 -0
  116. data/ext/libmspack/test/test_files/cabd/partial_str_nopname.cab +0 -0
  117. data/ext/libmspack/test/test_files/cabd/partial_str_shortfname.cab +0 -0
  118. data/ext/libmspack/test/test_files/cabd/partial_str_shortninfo.cab +0 -0
  119. data/ext/libmspack/test/test_files/cabd/partial_str_shortnname.cab +0 -0
  120. data/ext/libmspack/test/test_files/cabd/partial_str_shortpinfo.cab +0 -0
  121. data/ext/libmspack/test/test_files/cabd/partial_str_shortpname.cab +0 -0
  122. data/ext/libmspack/test/test_files/cabd/reserve_---.cab +0 -0
  123. data/ext/libmspack/test/test_files/cabd/reserve_--D.cab +0 -0
  124. data/ext/libmspack/test/test_files/cabd/reserve_-F-.cab +0 -0
  125. data/ext/libmspack/test/test_files/cabd/reserve_-FD.cab +0 -0
  126. data/ext/libmspack/test/test_files/cabd/reserve_H--.cab +0 -0
  127. data/ext/libmspack/test/test_files/cabd/reserve_H-D.cab +0 -0
  128. data/ext/libmspack/test/test_files/cabd/reserve_HF-.cab +0 -0
  129. data/ext/libmspack/test/test_files/cabd/reserve_HFD.cab +0 -0
  130. data/ext/libmspack/test/test_files/cabd/search_basic.cab +0 -0
  131. data/ext/libmspack/test/test_files/cabd/search_tricky1.cab +0 -0
  132. data/ext/libmspack/winbuild.sh +26 -0
  133. data/ext/x86_64-windows/libmspack.dll +0 -0
  134. data/lib/libmspack/constants.rb +9 -0
  135. data/lib/libmspack/exceptions.rb +12 -0
  136. data/lib/libmspack/mscab.rb +722 -0
  137. data/lib/libmspack/mschm.rb +301 -0
  138. data/lib/libmspack/mshlp.rb +15 -0
  139. data/lib/libmspack/mskwaj.rb +124 -0
  140. data/lib/libmspack/mslit.rb +18 -0
  141. data/lib/libmspack/msoab.rb +36 -0
  142. data/lib/libmspack/mspack.rb +208 -0
  143. data/lib/libmspack/msszdd.rb +81 -0
  144. data/lib/libmspack/system.rb +84 -0
  145. data/lib/libmspack/version.rb +4 -0
  146. data/lib/libmspack.rb +121 -0
  147. data/libmspack.gemspec +33 -0
  148. data/spec/libmspack_spec.rb +26 -0
  149. data/spec/spec_helper.rb +5 -0
  150. metadata +309 -0
@@ -0,0 +1,895 @@
1
+ /* This file is part of libmspack.
2
+ * (C) 2003-2013 Stuart Caie.
3
+ *
4
+ * The LZX method was created by Jonathan Forbes and Tomi Poutanen, adapted
5
+ * by Microsoft Corporation.
6
+ *
7
+ * libmspack is free software; you can redistribute it and/or modify it under
8
+ * the terms of the GNU Lesser General Public License (LGPL) version 2.1
9
+ *
10
+ * For further details, see the file COPYING.LIB distributed with libmspack
11
+ */
12
+
13
+ /* LZX decompression implementation */
14
+
15
+ #include <system.h>
16
+ #include <lzx.h>
17
+
18
+ /* Microsoft's LZX document (in cab-sdk.exe) and their implementation
19
+ * of the com.ms.util.cab Java package do not concur.
20
+ *
21
+ * In the LZX document, there is a table showing the correlation between
22
+ * window size and the number of position slots. It states that the 1MB
23
+ * window = 40 slots and the 2MB window = 42 slots. In the implementation,
24
+ * 1MB = 42 slots, 2MB = 50 slots. The actual calculation is 'find the
25
+ * first slot whose position base is equal to or more than the required
26
+ * window size'. This would explain why other tables in the document refer
27
+ * to 50 slots rather than 42.
28
+ *
29
+ * The constant NUM_PRIMARY_LENGTHS used in the decompression pseudocode
30
+ * is not defined in the specification.
31
+ *
32
+ * The LZX document does not state the uncompressed block has an
33
+ * uncompressed length field. Where does this length field come from, so
34
+ * we can know how large the block is? The implementation has it as the 24
35
+ * bits following after the 3 blocktype bits, before the alignment
36
+ * padding.
37
+ *
38
+ * The LZX document states that aligned offset blocks have their aligned
39
+ * offset huffman tree AFTER the main and length trees. The implementation
40
+ * suggests that the aligned offset tree is BEFORE the main and length
41
+ * trees.
42
+ *
43
+ * The LZX document decoding algorithm states that, in an aligned offset
44
+ * block, if an extra_bits value is 1, 2 or 3, then that number of bits
45
+ * should be read and the result added to the match offset. This is
46
+ * correct for 1 and 2, but not 3, where just a huffman symbol (using the
47
+ * aligned tree) should be read.
48
+ *
49
+ * Regarding the E8 preprocessing, the LZX document states 'No translation
50
+ * may be performed on the last 6 bytes of the input block'. This is
51
+ * correct. However, the pseudocode provided checks for the *E8 leader*
52
+ * up to the last 6 bytes. If the leader appears between -10 and -7 bytes
53
+ * from the end, this would cause the next four bytes to be modified, at
54
+ * least one of which would be in the last 6 bytes, which is not allowed
55
+ * according to the spec.
56
+ *
57
+ * The specification states that the huffman trees must always contain at
58
+ * least one element. However, many CAB files contain blocks where the
59
+ * length tree is completely empty (because there are no matches), and
60
+ * this is expected to succeed.
61
+ *
62
+ * The errors in LZX documentation appear have been corrected in the
63
+ * new documentation for the LZX DELTA format.
64
+ *
65
+ * http://msdn.microsoft.com/en-us/library/cc483133.aspx
66
+ *
67
+ * However, this is a different format, an extension of regular LZX.
68
+ * I have noticed the following differences, there may be more:
69
+ *
70
+ * The maximum window size has increased from 2MB to 32MB. This also
71
+ * increases the maximum number of position slots, etc.
72
+ *
73
+ * If the match length is 257 (the maximum possible), this signals
74
+ * a further length decoding step, that allows for matches up to
75
+ * 33024 bytes long.
76
+ *
77
+ * The format now allows for "reference data", supplied by the caller.
78
+ * If match offsets go further back than the number of bytes
79
+ * decompressed so far, that is them accessing the reference data.
80
+ */
81
+
82
+ /* import bit-reading macros and code */
83
+ #define BITS_TYPE struct lzxd_stream
84
+ #define BITS_VAR lzx
85
+ #define BITS_ORDER_MSB
86
+ #define READ_BYTES do { \
87
+ unsigned char b0, b1; \
88
+ READ_IF_NEEDED; b0 = *i_ptr++; \
89
+ READ_IF_NEEDED; b1 = *i_ptr++; \
90
+ INJECT_BITS((b1 << 8) | b0, 16); \
91
+ } while (0)
92
+ #include <readbits.h>
93
+
94
+ /* import huffman-reading macros and code */
95
+ #define TABLEBITS(tbl) LZX_##tbl##_TABLEBITS
96
+ #define MAXSYMBOLS(tbl) LZX_##tbl##_MAXSYMBOLS
97
+ #define HUFF_TABLE(tbl,idx) lzx->tbl##_table[idx]
98
+ #define HUFF_LEN(tbl,idx) lzx->tbl##_len[idx]
99
+ #define HUFF_ERROR return lzx->error = MSPACK_ERR_DECRUNCH
100
+ #include <readhuff.h>
101
+
102
+ /* BUILD_TABLE(tbl) builds a huffman lookup table from code lengths */
103
+ #define BUILD_TABLE(tbl) \
104
+ if (make_decode_table(MAXSYMBOLS(tbl), TABLEBITS(tbl), \
105
+ &HUFF_LEN(tbl,0), &HUFF_TABLE(tbl,0))) \
106
+ { \
107
+ D(("failed to build %s table", #tbl)) \
108
+ return lzx->error = MSPACK_ERR_DECRUNCH; \
109
+ }
110
+
111
+ #define BUILD_TABLE_MAYBE_EMPTY(tbl) do { \
112
+ lzx->tbl##_empty = 0; \
113
+ if (make_decode_table(MAXSYMBOLS(tbl), TABLEBITS(tbl), \
114
+ &HUFF_LEN(tbl,0), &HUFF_TABLE(tbl,0))) \
115
+ { \
116
+ for (i = 0; i < MAXSYMBOLS(tbl); i++) { \
117
+ if (HUFF_LEN(tbl, i) > 0) { \
118
+ D(("failed to build %s table", #tbl)) \
119
+ return lzx->error = MSPACK_ERR_DECRUNCH; \
120
+ } \
121
+ } \
122
+ /* empty tree - allow it, but don't decode symbols with it */ \
123
+ lzx->tbl##_empty = 1; \
124
+ } \
125
+ } while (0)
126
+
127
+ /* READ_LENGTHS(tablename, first, last) reads in code lengths for symbols
128
+ * first to last in the given table. The code lengths are stored in their
129
+ * own special LZX way.
130
+ */
131
+ #define READ_LENGTHS(tbl, first, last) do { \
132
+ STORE_BITS; \
133
+ if (lzxd_read_lens(lzx, &HUFF_LEN(tbl, 0), (first), \
134
+ (unsigned int)(last))) return lzx->error; \
135
+ RESTORE_BITS; \
136
+ } while (0)
137
+
138
+ static int lzxd_read_lens(struct lzxd_stream *lzx, unsigned char *lens,
139
+ unsigned int first, unsigned int last)
140
+ {
141
+ /* bit buffer and huffman symbol decode variables */
142
+ register unsigned int bit_buffer;
143
+ register int bits_left, i;
144
+ register unsigned short sym;
145
+ unsigned char *i_ptr, *i_end;
146
+
147
+ unsigned int x, y;
148
+ int z;
149
+
150
+ RESTORE_BITS;
151
+
152
+ /* read lengths for pretree (20 symbols, lengths stored in fixed 4 bits) */
153
+ for (x = 0; x < 20; x++) {
154
+ READ_BITS(y, 4);
155
+ lzx->PRETREE_len[x] = y;
156
+ }
157
+ BUILD_TABLE(PRETREE);
158
+
159
+ for (x = first; x < last; ) {
160
+ READ_HUFFSYM(PRETREE, z);
161
+ if (z == 17) {
162
+ /* code = 17, run of ([read 4 bits]+4) zeros */
163
+ READ_BITS(y, 4); y += 4;
164
+ while (y--) lens[x++] = 0;
165
+ }
166
+ else if (z == 18) {
167
+ /* code = 18, run of ([read 5 bits]+20) zeros */
168
+ READ_BITS(y, 5); y += 20;
169
+ while (y--) lens[x++] = 0;
170
+ }
171
+ else if (z == 19) {
172
+ /* code = 19, run of ([read 1 bit]+4) [read huffman symbol] */
173
+ READ_BITS(y, 1); y += 4;
174
+ READ_HUFFSYM(PRETREE, z);
175
+ z = lens[x] - z; if (z < 0) z += 17;
176
+ while (y--) lens[x++] = z;
177
+ }
178
+ else {
179
+ /* code = 0 to 16, delta current length entry */
180
+ z = lens[x] - z; if (z < 0) z += 17;
181
+ lens[x++] = z;
182
+ }
183
+ }
184
+
185
+ STORE_BITS;
186
+
187
+ return MSPACK_ERR_OK;
188
+ }
189
+
190
+ /* LZX static data tables:
191
+ *
192
+ * LZX uses 'position slots' to represent match offsets. For every match,
193
+ * a small 'position slot' number and a small offset from that slot are
194
+ * encoded instead of one large offset.
195
+ *
196
+ * The number of slots is decided by how many are needed to encode the
197
+ * largest offset for a given window size. This is easy when the gap between
198
+ * slots is less than 128Kb, it's a linear relationship. But when extra_bits
199
+ * reaches its limit of 17 (because LZX can only ensure reading 17 bits of
200
+ * data at a time), we can only jump 128Kb at a time and have to start
201
+ * using more and more position slots as each window size doubles.
202
+ *
203
+ * position_base[] is an index to the position slot bases
204
+ *
205
+ * extra_bits[] states how many bits of offset-from-base data is needed.
206
+ *
207
+ * They are calculated as follows:
208
+ * extra_bits[i] = 0 where i < 4
209
+ * extra_bits[i] = floor(i/2)-1 where i >= 4 && i < 36
210
+ * extra_bits[i] = 17 where i >= 36
211
+ * position_base[0] = 0
212
+ * position_base[i] = position_base[i-1] + (1 << extra_bits[i-1])
213
+ */
214
+ static const unsigned int position_slots[11] = {
215
+ 30, 32, 34, 36, 38, 42, 50, 66, 98, 162, 290
216
+ };
217
+ static const unsigned char extra_bits[36] = {
218
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
219
+ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16
220
+ };
221
+ static const unsigned int position_base[290] = {
222
+ 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512,
223
+ 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768,
224
+ 49152, 65536, 98304, 131072, 196608, 262144, 393216, 524288, 655360,
225
+ 786432, 917504, 1048576, 1179648, 1310720, 1441792, 1572864, 1703936,
226
+ 1835008, 1966080, 2097152, 2228224, 2359296, 2490368, 2621440, 2752512,
227
+ 2883584, 3014656, 3145728, 3276800, 3407872, 3538944, 3670016, 3801088,
228
+ 3932160, 4063232, 4194304, 4325376, 4456448, 4587520, 4718592, 4849664,
229
+ 4980736, 5111808, 5242880, 5373952, 5505024, 5636096, 5767168, 5898240,
230
+ 6029312, 6160384, 6291456, 6422528, 6553600, 6684672, 6815744, 6946816,
231
+ 7077888, 7208960, 7340032, 7471104, 7602176, 7733248, 7864320, 7995392,
232
+ 8126464, 8257536, 8388608, 8519680, 8650752, 8781824, 8912896, 9043968,
233
+ 9175040, 9306112, 9437184, 9568256, 9699328, 9830400, 9961472, 10092544,
234
+ 10223616, 10354688, 10485760, 10616832, 10747904, 10878976, 11010048,
235
+ 11141120, 11272192, 11403264, 11534336, 11665408, 11796480, 11927552,
236
+ 12058624, 12189696, 12320768, 12451840, 12582912, 12713984, 12845056,
237
+ 12976128, 13107200, 13238272, 13369344, 13500416, 13631488, 13762560,
238
+ 13893632, 14024704, 14155776, 14286848, 14417920, 14548992, 14680064,
239
+ 14811136, 14942208, 15073280, 15204352, 15335424, 15466496, 15597568,
240
+ 15728640, 15859712, 15990784, 16121856, 16252928, 16384000, 16515072,
241
+ 16646144, 16777216, 16908288, 17039360, 17170432, 17301504, 17432576,
242
+ 17563648, 17694720, 17825792, 17956864, 18087936, 18219008, 18350080,
243
+ 18481152, 18612224, 18743296, 18874368, 19005440, 19136512, 19267584,
244
+ 19398656, 19529728, 19660800, 19791872, 19922944, 20054016, 20185088,
245
+ 20316160, 20447232, 20578304, 20709376, 20840448, 20971520, 21102592,
246
+ 21233664, 21364736, 21495808, 21626880, 21757952, 21889024, 22020096,
247
+ 22151168, 22282240, 22413312, 22544384, 22675456, 22806528, 22937600,
248
+ 23068672, 23199744, 23330816, 23461888, 23592960, 23724032, 23855104,
249
+ 23986176, 24117248, 24248320, 24379392, 24510464, 24641536, 24772608,
250
+ 24903680, 25034752, 25165824, 25296896, 25427968, 25559040, 25690112,
251
+ 25821184, 25952256, 26083328, 26214400, 26345472, 26476544, 26607616,
252
+ 26738688, 26869760, 27000832, 27131904, 27262976, 27394048, 27525120,
253
+ 27656192, 27787264, 27918336, 28049408, 28180480, 28311552, 28442624,
254
+ 28573696, 28704768, 28835840, 28966912, 29097984, 29229056, 29360128,
255
+ 29491200, 29622272, 29753344, 29884416, 30015488, 30146560, 30277632,
256
+ 30408704, 30539776, 30670848, 30801920, 30932992, 31064064, 31195136,
257
+ 31326208, 31457280, 31588352, 31719424, 31850496, 31981568, 32112640,
258
+ 32243712, 32374784, 32505856, 32636928, 32768000, 32899072, 33030144,
259
+ 33161216, 33292288, 33423360
260
+ };
261
+
262
+ static void lzxd_reset_state(struct lzxd_stream *lzx) {
263
+ int i;
264
+
265
+ lzx->R0 = 1;
266
+ lzx->R1 = 1;
267
+ lzx->R2 = 1;
268
+ lzx->header_read = 0;
269
+ lzx->block_remaining = 0;
270
+ lzx->block_type = LZX_BLOCKTYPE_INVALID;
271
+
272
+ /* initialise tables to 0 (because deltas will be applied to them) */
273
+ for (i = 0; i < LZX_MAINTREE_MAXSYMBOLS; i++) lzx->MAINTREE_len[i] = 0;
274
+ for (i = 0; i < LZX_LENGTH_MAXSYMBOLS; i++) lzx->LENGTH_len[i] = 0;
275
+ }
276
+
277
+ /*-------- main LZX code --------*/
278
+
279
+ struct lzxd_stream *lzxd_init(struct mspack_system *system,
280
+ struct mspack_file *input,
281
+ struct mspack_file *output,
282
+ int window_bits,
283
+ int reset_interval,
284
+ int input_buffer_size,
285
+ off_t output_length,
286
+ char is_delta)
287
+ {
288
+ unsigned int window_size = 1 << window_bits;
289
+ struct lzxd_stream *lzx;
290
+
291
+ if (!system) return NULL;
292
+
293
+ /* LZX DELTA window sizes are between 2^17 (128KiB) and 2^25 (32MiB),
294
+ * regular LZX windows are between 2^15 (32KiB) and 2^21 (2MiB)
295
+ */
296
+ if (is_delta) {
297
+ if (window_bits < 17 || window_bits > 25) return NULL;
298
+ }
299
+ else {
300
+ if (window_bits < 15 || window_bits > 21) return NULL;
301
+ }
302
+
303
+ input_buffer_size = (input_buffer_size + 1) & -2;
304
+ if (!input_buffer_size) return NULL;
305
+
306
+ /* allocate decompression state */
307
+ if (!(lzx = (struct lzxd_stream *) system->alloc(system, sizeof(struct lzxd_stream)))) {
308
+ return NULL;
309
+ }
310
+
311
+ /* allocate decompression window and input buffer */
312
+ lzx->window = (unsigned char *) system->alloc(system, (size_t) window_size);
313
+ lzx->inbuf = (unsigned char *) system->alloc(system, (size_t) input_buffer_size);
314
+ if (!lzx->window || !lzx->inbuf) {
315
+ system->free(lzx->window);
316
+ system->free(lzx->inbuf);
317
+ system->free(lzx);
318
+ return NULL;
319
+ }
320
+
321
+ /* initialise decompression state */
322
+ lzx->sys = system;
323
+ lzx->input = input;
324
+ lzx->output = output;
325
+ lzx->offset = 0;
326
+ lzx->length = output_length;
327
+
328
+ lzx->inbuf_size = input_buffer_size;
329
+ lzx->window_size = 1 << window_bits;
330
+ lzx->ref_data_size = 0;
331
+ lzx->window_posn = 0;
332
+ lzx->frame_posn = 0;
333
+ lzx->frame = 0;
334
+ lzx->reset_interval = reset_interval;
335
+ lzx->intel_filesize = 0;
336
+ lzx->intel_curpos = 0;
337
+ lzx->intel_started = 0;
338
+ lzx->error = MSPACK_ERR_OK;
339
+ lzx->num_offsets = position_slots[window_bits - 15] << 3;
340
+ lzx->is_delta = is_delta;
341
+
342
+ lzx->o_ptr = lzx->o_end = &lzx->e8_buf[0];
343
+ lzxd_reset_state(lzx);
344
+ INIT_BITS;
345
+ return lzx;
346
+ }
347
+
348
+ int lzxd_set_reference_data(struct lzxd_stream *lzx,
349
+ struct mspack_system *system,
350
+ struct mspack_file *input,
351
+ unsigned int length)
352
+ {
353
+ if (!lzx) return MSPACK_ERR_ARGS;
354
+
355
+ if (!lzx->is_delta) {
356
+ D(("only LZX DELTA streams support reference data"))
357
+ return MSPACK_ERR_ARGS;
358
+ }
359
+ if (lzx->offset) {
360
+ D(("too late to set reference data after decoding starts"))
361
+ return MSPACK_ERR_ARGS;
362
+ }
363
+ if (length > lzx->window_size) {
364
+ D(("reference length (%u) is longer than the window", length))
365
+ return MSPACK_ERR_ARGS;
366
+ }
367
+ if (length > 0 && (!system || !input)) {
368
+ D(("length > 0 but no system or input"))
369
+ return MSPACK_ERR_ARGS;
370
+ }
371
+
372
+ lzx->ref_data_size = length;
373
+ if (length > 0) {
374
+ /* copy reference data */
375
+ unsigned char *pos = &lzx->window[lzx->window_size - length];
376
+ int bytes = system->read(input, pos, length);
377
+ /* length can't be more than 2^25, so no signedness problem */
378
+ if (bytes < (int)length) return MSPACK_ERR_READ;
379
+ }
380
+ lzx->ref_data_size = length;
381
+ return MSPACK_ERR_OK;
382
+ }
383
+
384
+ void lzxd_set_output_length(struct lzxd_stream *lzx, off_t out_bytes) {
385
+ if (lzx) lzx->length = out_bytes;
386
+ }
387
+
388
+ int lzxd_decompress(struct lzxd_stream *lzx, off_t out_bytes) {
389
+ /* bitstream and huffman reading variables */
390
+ register unsigned int bit_buffer;
391
+ register int bits_left, i=0;
392
+ unsigned char *i_ptr, *i_end;
393
+ register unsigned short sym;
394
+
395
+ int match_length, length_footer, extra, verbatim_bits, bytes_todo;
396
+ int this_run, main_element, aligned_bits, j;
397
+ unsigned char *window, *runsrc, *rundest, buf[12];
398
+ unsigned int frame_size=0, end_frame, match_offset, window_posn;
399
+ unsigned int R0, R1, R2;
400
+
401
+ /* easy answers */
402
+ if (!lzx || (out_bytes < 0)) return MSPACK_ERR_ARGS;
403
+ if (lzx->error) return lzx->error;
404
+
405
+ /* flush out any stored-up bytes before we begin */
406
+ i = lzx->o_end - lzx->o_ptr;
407
+ if ((off_t) i > out_bytes) i = (int) out_bytes;
408
+ if (i) {
409
+ if (lzx->sys->write(lzx->output, lzx->o_ptr, i) != i) {
410
+ return lzx->error = MSPACK_ERR_WRITE;
411
+ }
412
+ lzx->o_ptr += i;
413
+ lzx->offset += i;
414
+ out_bytes -= i;
415
+ }
416
+ if (out_bytes == 0) return MSPACK_ERR_OK;
417
+
418
+ /* restore local state */
419
+ RESTORE_BITS;
420
+ window = lzx->window;
421
+ window_posn = lzx->window_posn;
422
+ R0 = lzx->R0;
423
+ R1 = lzx->R1;
424
+ R2 = lzx->R2;
425
+
426
+ end_frame = (unsigned int)((lzx->offset + out_bytes) / LZX_FRAME_SIZE) + 1;
427
+
428
+ while (lzx->frame < end_frame) {
429
+ /* have we reached the reset interval? (if there is one?) */
430
+ if (lzx->reset_interval && ((lzx->frame % lzx->reset_interval) == 0)) {
431
+ if (lzx->block_remaining) {
432
+ D(("%d bytes remaining at reset interval", lzx->block_remaining))
433
+ return lzx->error = MSPACK_ERR_DECRUNCH;
434
+ }
435
+
436
+ /* re-read the intel header and reset the huffman lengths */
437
+ lzxd_reset_state(lzx);
438
+ R0 = lzx->R0;
439
+ R1 = lzx->R1;
440
+ R2 = lzx->R2;
441
+ }
442
+
443
+ /* LZX DELTA format has chunk_size, not present in LZX format */
444
+ if (lzx->is_delta) {
445
+ ENSURE_BITS(16);
446
+ REMOVE_BITS(16);
447
+ }
448
+
449
+ /* read header if necessary */
450
+ if (!lzx->header_read) {
451
+ /* read 1 bit. if bit=0, intel filesize = 0.
452
+ * if bit=1, read intel filesize (32 bits) */
453
+ j = 0; READ_BITS(i, 1); if (i) { READ_BITS(i, 16); READ_BITS(j, 16); }
454
+ lzx->intel_filesize = (i << 16) | j;
455
+ lzx->header_read = 1;
456
+ }
457
+
458
+ /* calculate size of frame: all frames are 32k except the final frame
459
+ * which is 32kb or less. this can only be calculated when lzx->length
460
+ * has been filled in. */
461
+ frame_size = LZX_FRAME_SIZE;
462
+ if (lzx->length && (lzx->length - lzx->offset) < (off_t)frame_size) {
463
+ frame_size = lzx->length - lzx->offset;
464
+ }
465
+
466
+ /* decode until one more frame is available */
467
+ bytes_todo = lzx->frame_posn + frame_size - window_posn;
468
+ while (bytes_todo > 0) {
469
+ /* initialise new block, if one is needed */
470
+ if (lzx->block_remaining == 0) {
471
+ /* realign if previous block was an odd-sized UNCOMPRESSED block */
472
+ if ((lzx->block_type == LZX_BLOCKTYPE_UNCOMPRESSED) &&
473
+ (lzx->block_length & 1))
474
+ {
475
+ READ_IF_NEEDED;
476
+ i_ptr++;
477
+ }
478
+
479
+ /* read block type (3 bits) and block length (24 bits) */
480
+ READ_BITS(lzx->block_type, 3);
481
+ READ_BITS(i, 16); READ_BITS(j, 8);
482
+ lzx->block_remaining = lzx->block_length = (i << 8) | j;
483
+ /*D(("new block t%d len %u", lzx->block_type, lzx->block_length))*/
484
+
485
+ /* read individual block headers */
486
+ switch (lzx->block_type) {
487
+ case LZX_BLOCKTYPE_ALIGNED:
488
+ /* read lengths of and build aligned huffman decoding tree */
489
+ for (i = 0; i < 8; i++) { READ_BITS(j, 3); lzx->ALIGNED_len[i] = j; }
490
+ BUILD_TABLE(ALIGNED);
491
+ /* no break -- rest of aligned header is same as verbatim */
492
+ case LZX_BLOCKTYPE_VERBATIM:
493
+ /* read lengths of and build main huffman decoding tree */
494
+ READ_LENGTHS(MAINTREE, 0, 256);
495
+ READ_LENGTHS(MAINTREE, 256, LZX_NUM_CHARS + lzx->num_offsets);
496
+ BUILD_TABLE(MAINTREE);
497
+ /* if the literal 0xE8 is anywhere in the block... */
498
+ if (lzx->MAINTREE_len[0xE8] != 0) lzx->intel_started = 1;
499
+ /* read lengths of and build lengths huffman decoding tree */
500
+ READ_LENGTHS(LENGTH, 0, LZX_NUM_SECONDARY_LENGTHS);
501
+ BUILD_TABLE_MAYBE_EMPTY(LENGTH);
502
+ break;
503
+
504
+ case LZX_BLOCKTYPE_UNCOMPRESSED:
505
+ /* because we can't assume otherwise */
506
+ lzx->intel_started = 1;
507
+
508
+ /* read 1-16 (not 0-15) bits to align to bytes */
509
+ ENSURE_BITS(16);
510
+ if (bits_left > 16) i_ptr -= 2;
511
+ bits_left = 0; bit_buffer = 0;
512
+
513
+ /* read 12 bytes of stored R0 / R1 / R2 values */
514
+ for (rundest = &buf[0], i = 0; i < 12; i++) {
515
+ READ_IF_NEEDED;
516
+ *rundest++ = *i_ptr++;
517
+ }
518
+ R0 = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
519
+ R1 = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
520
+ R2 = buf[8] | (buf[9] << 8) | (buf[10] << 16) | (buf[11] << 24);
521
+ break;
522
+
523
+ default:
524
+ D(("bad block type"))
525
+ return lzx->error = MSPACK_ERR_DECRUNCH;
526
+ }
527
+ }
528
+
529
+ /* decode more of the block:
530
+ * run = min(what's available, what's needed) */
531
+ this_run = lzx->block_remaining;
532
+ if (this_run > bytes_todo) this_run = bytes_todo;
533
+
534
+ /* assume we decode exactly this_run bytes, for now */
535
+ bytes_todo -= this_run;
536
+ lzx->block_remaining -= this_run;
537
+
538
+ /* decode at least this_run bytes */
539
+ switch (lzx->block_type) {
540
+ case LZX_BLOCKTYPE_VERBATIM:
541
+ while (this_run > 0) {
542
+ READ_HUFFSYM(MAINTREE, main_element);
543
+ if (main_element < LZX_NUM_CHARS) {
544
+ /* literal: 0 to LZX_NUM_CHARS-1 */
545
+ window[window_posn++] = main_element;
546
+ this_run--;
547
+ }
548
+ else {
549
+ /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
550
+ main_element -= LZX_NUM_CHARS;
551
+
552
+ /* get match length */
553
+ match_length = main_element & LZX_NUM_PRIMARY_LENGTHS;
554
+ if (match_length == LZX_NUM_PRIMARY_LENGTHS) {
555
+ if (lzx->LENGTH_empty) {
556
+ D(("LENGTH symbol needed but tree is empty"))
557
+ return lzx->error = MSPACK_ERR_DECRUNCH;
558
+ }
559
+ READ_HUFFSYM(LENGTH, length_footer);
560
+ match_length += length_footer;
561
+ }
562
+ match_length += LZX_MIN_MATCH;
563
+
564
+ /* get match offset */
565
+ switch ((match_offset = (main_element >> 3))) {
566
+ case 0: match_offset = R0; break;
567
+ case 1: match_offset = R1; R1=R0; R0 = match_offset; break;
568
+ case 2: match_offset = R2; R2=R0; R0 = match_offset; break;
569
+ case 3: match_offset = 1; R2=R1; R1=R0; R0 = match_offset; break;
570
+ default:
571
+ extra = (match_offset >= 36) ? 17 : extra_bits[match_offset];
572
+ READ_BITS(verbatim_bits, extra);
573
+ match_offset = position_base[match_offset] - 2 + verbatim_bits;
574
+ R2 = R1; R1 = R0; R0 = match_offset;
575
+ }
576
+
577
+ /* LZX DELTA uses max match length to signal even longer match */
578
+ if (match_length == LZX_MAX_MATCH && lzx->is_delta) {
579
+ int extra_len = 0;
580
+ ENSURE_BITS(3); /* 4 entry huffman tree */
581
+ if (PEEK_BITS(1) == 0) {
582
+ REMOVE_BITS(1); /* '0' -> 8 extra length bits */
583
+ READ_BITS(extra_len, 8);
584
+ }
585
+ else if (PEEK_BITS(2) == 2) {
586
+ REMOVE_BITS(2); /* '10' -> 10 extra length bits + 0x100 */
587
+ READ_BITS(extra_len, 10);
588
+ extra_len += 0x100;
589
+ }
590
+ else if (PEEK_BITS(3) == 6) {
591
+ REMOVE_BITS(3); /* '110' -> 12 extra length bits + 0x500 */
592
+ READ_BITS(extra_len, 12);
593
+ extra_len += 0x500;
594
+ }
595
+ else {
596
+ REMOVE_BITS(3); /* '111' -> 15 extra length bits */
597
+ READ_BITS(extra_len, 15);
598
+ }
599
+ match_length += extra_len;
600
+ }
601
+
602
+ if ((window_posn + match_length) > lzx->window_size) {
603
+ D(("match ran over window wrap"))
604
+ return lzx->error = MSPACK_ERR_DECRUNCH;
605
+ }
606
+
607
+ /* copy match */
608
+ rundest = &window[window_posn];
609
+ i = match_length;
610
+ /* does match offset wrap the window? */
611
+ if (match_offset > window_posn) {
612
+ if (match_offset > lzx->offset &&
613
+ (match_offset - window_posn) > lzx->ref_data_size)
614
+ {
615
+ D(("match offset beyond LZX stream"))
616
+ return lzx->error = MSPACK_ERR_DECRUNCH;
617
+ }
618
+ /* j = length from match offset to end of window */
619
+ j = match_offset - window_posn;
620
+ if (j > (int) lzx->window_size) {
621
+ D(("match offset beyond window boundaries"))
622
+ return lzx->error = MSPACK_ERR_DECRUNCH;
623
+ }
624
+ runsrc = &window[lzx->window_size - j];
625
+ if (j < i) {
626
+ /* if match goes over the window edge, do two copy runs */
627
+ i -= j; while (j-- > 0) *rundest++ = *runsrc++;
628
+ runsrc = window;
629
+ }
630
+ while (i-- > 0) *rundest++ = *runsrc++;
631
+ }
632
+ else {
633
+ runsrc = rundest - match_offset;
634
+ while (i-- > 0) *rundest++ = *runsrc++;
635
+ }
636
+
637
+ this_run -= match_length;
638
+ window_posn += match_length;
639
+ }
640
+ } /* while (this_run > 0) */
641
+ break;
642
+
643
+ case LZX_BLOCKTYPE_ALIGNED:
644
+ while (this_run > 0) {
645
+ READ_HUFFSYM(MAINTREE, main_element);
646
+ if (main_element < LZX_NUM_CHARS) {
647
+ /* literal: 0 to LZX_NUM_CHARS-1 */
648
+ window[window_posn++] = main_element;
649
+ this_run--;
650
+ }
651
+ else {
652
+ /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
653
+ main_element -= LZX_NUM_CHARS;
654
+
655
+ /* get match length */
656
+ match_length = main_element & LZX_NUM_PRIMARY_LENGTHS;
657
+ if (match_length == LZX_NUM_PRIMARY_LENGTHS) {
658
+ if (lzx->LENGTH_empty) {
659
+ D(("LENGTH symbol needed but tree is empty"))
660
+ return lzx->error = MSPACK_ERR_DECRUNCH;
661
+ }
662
+ READ_HUFFSYM(LENGTH, length_footer);
663
+ match_length += length_footer;
664
+ }
665
+ match_length += LZX_MIN_MATCH;
666
+
667
+ /* get match offset */
668
+ switch ((match_offset = (main_element >> 3))) {
669
+ case 0: match_offset = R0; break;
670
+ case 1: match_offset = R1; R1 = R0; R0 = match_offset; break;
671
+ case 2: match_offset = R2; R2 = R0; R0 = match_offset; break;
672
+ default:
673
+ extra = (match_offset >= 36) ? 17 : extra_bits[match_offset];
674
+ match_offset = position_base[match_offset] - 2;
675
+ if (extra > 3) {
676
+ /* verbatim and aligned bits */
677
+ extra -= 3;
678
+ READ_BITS(verbatim_bits, extra);
679
+ match_offset += (verbatim_bits << 3);
680
+ READ_HUFFSYM(ALIGNED, aligned_bits);
681
+ match_offset += aligned_bits;
682
+ }
683
+ else if (extra == 3) {
684
+ /* aligned bits only */
685
+ READ_HUFFSYM(ALIGNED, aligned_bits);
686
+ match_offset += aligned_bits;
687
+ }
688
+ else if (extra > 0) { /* extra==1, extra==2 */
689
+ /* verbatim bits only */
690
+ READ_BITS(verbatim_bits, extra);
691
+ match_offset += verbatim_bits;
692
+ }
693
+ else /* extra == 0 */ {
694
+ /* ??? not defined in LZX specification! */
695
+ match_offset = 1;
696
+ }
697
+ /* update repeated offset LRU queue */
698
+ R2 = R1; R1 = R0; R0 = match_offset;
699
+ }
700
+
701
+ /* LZX DELTA uses max match length to signal even longer match */
702
+ if (match_length == LZX_MAX_MATCH && lzx->is_delta) {
703
+ int extra_len = 0;
704
+ ENSURE_BITS(3); /* 4 entry huffman tree */
705
+ if (PEEK_BITS(1) == 0) {
706
+ REMOVE_BITS(1); /* '0' -> 8 extra length bits */
707
+ READ_BITS(extra_len, 8);
708
+ }
709
+ else if (PEEK_BITS(2) == 2) {
710
+ REMOVE_BITS(2); /* '10' -> 10 extra length bits + 0x100 */
711
+ READ_BITS(extra_len, 10);
712
+ extra_len += 0x100;
713
+ }
714
+ else if (PEEK_BITS(3) == 6) {
715
+ REMOVE_BITS(3); /* '110' -> 12 extra length bits + 0x500 */
716
+ READ_BITS(extra_len, 12);
717
+ extra_len += 0x500;
718
+ }
719
+ else {
720
+ REMOVE_BITS(3); /* '111' -> 15 extra length bits */
721
+ READ_BITS(extra_len, 15);
722
+ }
723
+ match_length += extra_len;
724
+ }
725
+
726
+ if ((window_posn + match_length) > lzx->window_size) {
727
+ D(("match ran over window wrap"))
728
+ return lzx->error = MSPACK_ERR_DECRUNCH;
729
+ }
730
+
731
+ /* copy match */
732
+ rundest = &window[window_posn];
733
+ i = match_length;
734
+ /* does match offset wrap the window? */
735
+ if (match_offset > window_posn) {
736
+ if (match_offset > lzx->offset &&
737
+ (match_offset - window_posn) > lzx->ref_data_size)
738
+ {
739
+ D(("match offset beyond LZX stream"))
740
+ return lzx->error = MSPACK_ERR_DECRUNCH;
741
+ }
742
+ /* j = length from match offset to end of window */
743
+ j = match_offset - window_posn;
744
+ if (j > (int) lzx->window_size) {
745
+ D(("match offset beyond window boundaries"))
746
+ return lzx->error = MSPACK_ERR_DECRUNCH;
747
+ }
748
+ runsrc = &window[lzx->window_size - j];
749
+ if (j < i) {
750
+ /* if match goes over the window edge, do two copy runs */
751
+ i -= j; while (j-- > 0) *rundest++ = *runsrc++;
752
+ runsrc = window;
753
+ }
754
+ while (i-- > 0) *rundest++ = *runsrc++;
755
+ }
756
+ else {
757
+ runsrc = rundest - match_offset;
758
+ while (i-- > 0) *rundest++ = *runsrc++;
759
+ }
760
+
761
+ this_run -= match_length;
762
+ window_posn += match_length;
763
+ }
764
+ } /* while (this_run > 0) */
765
+ break;
766
+
767
+ case LZX_BLOCKTYPE_UNCOMPRESSED:
768
+ /* as this_run is limited not to wrap a frame, this also means it
769
+ * won't wrap the window (as the window is a multiple of 32k) */
770
+ rundest = &window[window_posn];
771
+ window_posn += this_run;
772
+ while (this_run > 0) {
773
+ if ((i = i_end - i_ptr) == 0) {
774
+ READ_IF_NEEDED;
775
+ }
776
+ else {
777
+ if (i > this_run) i = this_run;
778
+ lzx->sys->copy(i_ptr, rundest, (size_t) i);
779
+ rundest += i;
780
+ i_ptr += i;
781
+ this_run -= i;
782
+ }
783
+ }
784
+ break;
785
+
786
+ default:
787
+ return lzx->error = MSPACK_ERR_DECRUNCH; /* might as well */
788
+ }
789
+
790
+ /* did the final match overrun our desired this_run length? */
791
+ if (this_run < 0) {
792
+ if ((unsigned int)(-this_run) > lzx->block_remaining) {
793
+ D(("overrun went past end of block by %d (%d remaining)",
794
+ -this_run, lzx->block_remaining ))
795
+ return lzx->error = MSPACK_ERR_DECRUNCH;
796
+ }
797
+ lzx->block_remaining -= -this_run;
798
+ }
799
+ } /* while (bytes_todo > 0) */
800
+
801
+ /* streams don't extend over frame boundaries */
802
+ if ((window_posn - lzx->frame_posn) != frame_size) {
803
+ D(("decode beyond output frame limits! %d != %d",
804
+ window_posn - lzx->frame_posn, frame_size))
805
+ return lzx->error = MSPACK_ERR_DECRUNCH;
806
+ }
807
+
808
+ /* re-align input bitstream */
809
+ if (bits_left > 0) ENSURE_BITS(16);
810
+ if (bits_left & 15) REMOVE_BITS(bits_left & 15);
811
+
812
+ /* check that we've used all of the previous frame first */
813
+ if (lzx->o_ptr != lzx->o_end) {
814
+ D(("%ld avail bytes, new %d frame", lzx->o_end-lzx->o_ptr, frame_size))
815
+ return lzx->error = MSPACK_ERR_DECRUNCH;
816
+ }
817
+
818
+ /* does this intel block _really_ need decoding? */
819
+ if (lzx->intel_started && lzx->intel_filesize &&
820
+ (lzx->frame <= 32768) && (frame_size > 10))
821
+ {
822
+ unsigned char *data = &lzx->e8_buf[0];
823
+ unsigned char *dataend = &lzx->e8_buf[frame_size - 10];
824
+ signed int curpos = lzx->intel_curpos;
825
+ signed int filesize = lzx->intel_filesize;
826
+ signed int abs_off, rel_off;
827
+
828
+ /* copy e8 block to the e8 buffer and tweak if needed */
829
+ lzx->o_ptr = data;
830
+ lzx->sys->copy(&lzx->window[lzx->frame_posn], data, frame_size);
831
+
832
+ while (data < dataend) {
833
+ if (*data++ != 0xE8) { curpos++; continue; }
834
+ abs_off = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
835
+ if ((abs_off >= -curpos) && (abs_off < filesize)) {
836
+ rel_off = (abs_off >= 0) ? abs_off - curpos : abs_off + filesize;
837
+ data[0] = (unsigned char) rel_off;
838
+ data[1] = (unsigned char) (rel_off >> 8);
839
+ data[2] = (unsigned char) (rel_off >> 16);
840
+ data[3] = (unsigned char) (rel_off >> 24);
841
+ }
842
+ data += 4;
843
+ curpos += 5;
844
+ }
845
+ lzx->intel_curpos += frame_size;
846
+ }
847
+ else {
848
+ lzx->o_ptr = &lzx->window[lzx->frame_posn];
849
+ if (lzx->intel_filesize) lzx->intel_curpos += frame_size;
850
+ }
851
+ lzx->o_end = &lzx->o_ptr[frame_size];
852
+
853
+ /* write a frame */
854
+ i = (out_bytes < (off_t)frame_size) ? (unsigned int)out_bytes : frame_size;
855
+ if (lzx->sys->write(lzx->output, lzx->o_ptr, i) != i) {
856
+ return lzx->error = MSPACK_ERR_WRITE;
857
+ }
858
+ lzx->o_ptr += i;
859
+ lzx->offset += i;
860
+ out_bytes -= i;
861
+
862
+ /* advance frame start position */
863
+ lzx->frame_posn += frame_size;
864
+ lzx->frame++;
865
+
866
+ /* wrap window / frame position pointers */
867
+ if (window_posn == lzx->window_size) window_posn = 0;
868
+ if (lzx->frame_posn == lzx->window_size) lzx->frame_posn = 0;
869
+
870
+ } /* while (lzx->frame < end_frame) */
871
+
872
+ if (out_bytes) {
873
+ D(("bytes left to output"))
874
+ return lzx->error = MSPACK_ERR_DECRUNCH;
875
+ }
876
+
877
+ /* store local state */
878
+ STORE_BITS;
879
+ lzx->window_posn = window_posn;
880
+ lzx->R0 = R0;
881
+ lzx->R1 = R1;
882
+ lzx->R2 = R2;
883
+
884
+ return MSPACK_ERR_OK;
885
+ }
886
+
887
+ void lzxd_free(struct lzxd_stream *lzx) {
888
+ struct mspack_system *sys;
889
+ if (lzx) {
890
+ sys = lzx->sys;
891
+ sys->free(lzx->inbuf);
892
+ sys->free(lzx->window);
893
+ sys->free(lzx);
894
+ }
895
+ }