@@ -4,8 +4,7 @@ if not LIB then print("Please run unzip instead") os.exit(1) end
44--- @param r function The reader function
55--- @param w function The writer function
66--- @param f file The output file handle to allow seeking the sliding window
7- --- @param z boolean Is this deflate64
8- function inflate (r , w , f , z )
7+ function inflate (r , w , f )
98
109 -- Z = size of sliding window
1110 -- b = buffer
@@ -61,13 +60,6 @@ function inflate(r, w, f, z)
6160 return v
6261 end
6362
64- --- Appends a decoded byte to the output buffer and updates the sliding window history.
65- --- @param by number The byte value (0-255 ) to append.
66- function ab (by )
67- op = op + 1
68- w (string.char (by ))
69- end
70-
7163 --- Constructs a canonical Huffman decoding table from a list of code lengths.
7264 --- @param ls table An array where the index is the symbol and the value is its bit length.
7365 --- @return table A Huffman object containing the lookup table (` tab` ) and the maximum code length (` max` ).
@@ -161,7 +153,21 @@ function inflate(r, w, f, z)
161153 l = lo + hi * 256 -- calculate length
162154 lo , hi = rb (8 ), rb (8 ) -- read n-length
163155 if (l ~ (lo + hi * 256 )) ~= 0xFFFF then error (" stored block LEN/NLEN mismatch" , 0 ) end
164- for _ = 1 , l do ab (rb (8 )) end -- copy raw data
156+
157+ -- Copy raw data
158+ while l > 0 do
159+ if p > # b then if fi () == 0 then error (" !EOF" , 0 ) end end
160+
161+ -- k = Take amount
162+ -- c = Chunk taken
163+ local k , c = math.min (l , # b - p + 1 )
164+
165+ c = b :sub (p , p + k - 1 )
166+ w (c )
167+ op = op + k
168+ p = p + k
169+ l = l - k
170+ end
165171
166172 elseif t == 1 or t == 2 then
167173
@@ -227,7 +233,8 @@ function inflate(r, w, f, z)
227233 local s = rh (h )
228234
229235 if s < 256 then
230- ab (s )
236+ op = op + 1
237+ w (string.char (s ))
231238 elseif s == 256 then
232239 break
233240 else
@@ -257,7 +264,7 @@ function inflate(r, w, f, z)
257264 dv = db + (dx > 0 and rb (dx ) or 0 )
258265
259266 if dv <= 0 or dv > op then
260- print (" !Distance" , dv , Z , op ) os.exit (1 )
267+ print (" !Distance" , dv , op ) os.exit (1 )
261268 end
262269
263270 -- Read from output history using the file-backed sliding window
0 commit comments