-
Notifications
You must be signed in to change notification settings - Fork 1
/
zip.lua
407 lines (355 loc) · 8.96 KB
/
zip.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
local interface = {}
local ZIP_MAGIC_NUMBER = 'PK\x03\x04'
local EOCD_MAGIC_NUMBER = 'PK\x05\x06'
local CD_ENTRY_MAGIC_NUMBER = 'PK\x01\x02'
interface.readFlags = 'r'
if(
love and
love.system and
love.system.getOS() == 'Windows'
) then
interface.readFlags = interface.readFlags .. 'b'
end
local function bytesToNumber(str, len, offset, reverse)
if(len == nil) then
len = math.min(#str, 4)
end
assert(type(len) == 'number', 'len must be number.')
assert(len > 0, 'len must be bigger than 0.')
assert(len <= 4, 'len cannot be more than 4, due to 32 bit limitations.')
--print(offset, #str - len)
offset = (
(type(offset) == 'number') and
((offset) <= (#str - len)) and
offset
) or 0
if(reverse) then
str = str:sub(1 + offset, offset + len):reverse()
offset = 0
end
local n = 0
for i = 1, len, 1 do
local byte = string.byte(str:sub(
i + offset,
i + offset
))
--n = (n + (byte << (8 * (i - 1))))
--https://ebens.me/post/simulate-bitwise-shift-operators-in-lua/
n = (n + (byte * (2 ^ (8 * (i - 1)))))
end
return n
end
--local function getMissingHeaderInfo()end
local function parseHeader(str, offset) --return {}, n
offset = offset or 0
local isLocalHeader = false
if(
str:sub(
offset + 1,
offset + 2
) == 'PK'
) then
local sigPart2 = str:sub(
offset + 3,
offset + 4
)
if(sigPart2 == '\3\4') then
isLocalHeader = true
elseif(sigPart2 ~= '\1\2') then
error('unknown header type')
end
else
error('not a header')
end
local currentOffset = offset + 4
local versionMade
if(not isLocalHeader) then
versionMade = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
end
local minVersionToExtract = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
local generalBitFlag = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
local compressionMethod = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
local lastModifiedTime = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
local lastModifiedDate = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
local fileCRC32 = bytesToNumber(
str,
4,
currentOffset,
false
)
currentOffset = currentOffset + 4
local compressedSize = bytesToNumber(
str,
4,
currentOffset,
false
)
currentOffset = currentOffset + 4
local uncompressedSize = bytesToNumber(
str,
4,
currentOffset,
false
)
currentOffset = currentOffset + 4
local fileNameLength = bytesToNumber(
str,
2,
currentOffset,
false
)
--print(fileNameLength)
currentOffset = currentOffset + 2
local extraFieldLength = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
local fileCommentLength, fileDiskNumber, fileAttributesInt, fileAttributesExt, localFileHeaderOffset
if(not isLocalHeader) then
fileCommentLength = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
fileDiskNumber = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
fileAttributesInt = bytesToNumber(
str,
2,
currentOffset,
false
)
currentOffset = currentOffset + 2
fileAttributesExt = bytesToNumber(
str,
4,
currentOffset,
false
)
currentOffset = currentOffset + 4
localFileHeaderOffset = bytesToNumber(
str,
4,
currentOffset,
false
)
currentOffset = currentOffset + 4
end
local missingInfo = false
local fileName, extraField, fileComment
if(currentOffset >= #str) then
missingInfo = true
fileName = {
s = currentOffset,
l = fileNameLength
}
extraField = extraFieldLength
fileComment = fileCommentLength
currentOffset = currentOffset + fileNameLength + extraFieldLength + (fileCommentLength or 0)
else
fileName = str:sub(currentOffset + 1, currentOffset + fileNameLength)
currentOffset = currentOffset + fileNameLength
extraField = str:sub(currentOffset + 1, currentOffset + extraFieldLength)
currentOffset = currentOffset + extraFieldLength
if(fileCommentLength) then
fileComment = str:sub(currentOffset + 1, currentOffset + fileCommentLength)
currentOffset = currentOffset + fileCommentLength
end
end
return {
isLocalHeader = isLocalHeader,
missingInfo = missingInfo,
fileName = fileName,
fileComment = fileComment,
extraField = extraField,
versionMade = versionMade,
minVersionToExtract = minVersionToExtract,
generalBitFlag = generalBitFlag,
compressionMethod = compressionMethod,
lastModifiedDate = lastModifiedDate,
lastModifiedTime = lastModifiedTime,
fileCRC32 = fileCRC32,
compressedSize = compressedSize,
uncompressedSize = uncompressedSize,
fileDiskNumber = fileDiskNumber,
internalFileAttributes = fileAttributesInt,
externalFileAttributes = fileAttributesExt,
localFileHeaderOffset = localFileHeaderOffset
}, currentOffset - offset
end
local function getFile(self, path)
local fileEntry = self.files[path]
if(fileEntry) then
local fh = self._fileHandle
fh:seek('set', fileEntry.localFileHeaderOffset)
local success, errIfNotSuccess, headerSize = pcall(parseHeader, fh:read(30))
if(success) then
errIfNotSuccess = nil
fh:seek(
'set',
fileEntry.localFileHeaderOffset + headerSize
)
return fh:read(fileEntry.compressedSize)
else
return nil, errIfNotSuccess
end
else
return nil, 'file not found'
end
end
local function kill(self)
if(not self.killed) then
self._fileHandle:close()
self._fileHandle = nil
self.files = nil
self.getFile = nil
self.killed = true
end
end
function interface.open(path)
local archive, err = io.open(path, interface.readFlags)
if(archive) then
--is zip archive??
if(
archive:read(#ZIP_MAGIC_NUMBER) ==
ZIP_MAGIC_NUMBER
) then
local archiveSize = archive:seek('end')
--print(archiveSize)
local instanceTable = {
getFile = getFile,
kill = kill,
_fileHandle = archive
}
--find end of eocd, for directory listing
local files = {}
instanceTable.files = files
do
local eocd = ''
local eocdFound = false
local currentPosition = archiveSize
while(currentPosition > 0) do
local seeked = math.min(
currentPosition,
22
)
currentPosition = currentPosition - seeked
archive:seek('set', currentPosition)
local chunk = archive:read(seeked)
eocd = chunk .. eocd
chunk = nil
local eocdMagicNumberPos = string.find(eocd, EOCD_MAGIC_NUMBER)
if(eocdMagicNumberPos) then
eocdFound = true
eocd = eocd:sub(eocdMagicNumberPos)
break
end
end
currentPosition = nil
if(eocdFound) then
eocdFound = nil
local diskNumber = bytesToNumber(eocd, 2, 4, false)
instanceTable.diskNumber = diskNumber
do
local commentLength = bytesToNumber(eocd, 2, 20, false)
if(commentLength ~= 0) then
instanceTable.comment = eocd:sub(21)
end
end
--local cdStartDisk = bytesToNumber(eocd, 2, 6, false)
--local cdRecordsOnDisk = bytesToNumber(eocd, 2, 8, false)
--local totalCdRecords = bytesToNumber(eocd, 2, 10, false)
local cdSize = bytesToNumber(eocd, 4, 12, false)
local cdStart = bytesToNumber(eocd, 4, 16, false)
--done with the eocd
eocd = nil
--files...
archive:seek('set', cdStart)
cdStart = nil
local centralDirectory = archive:read(cdSize)
cdSize = nil
do
local lastPosition = 0
while(lastPosition < #centralDirectory) do
local success, header, size = pcall(parseHeader, centralDirectory, lastPosition)
if(success) then
if(
(header.fileDiskNumber == diskNumber) and
(header.compressionMethod == 0)
) then
files[header.fileName] = header
header.isLocalHeader = nil
header.fileDiskNumber = nil
end
else
archive:close()
return nil, 'Broken archive - invalid entry in EOCD'
end
--otherwise ignore the file
lastPosition = lastPosition + size
end
end
centralDirectory = nil
--ok all done, return an instance
return instanceTable
else
archive:close()
return nil, 'Broken archive - no EOCD'
end
end
else
archive:close()
return nil, 'Not valid archive.'
end
else
--archive:close() --it was already broken to begin with
return nil, err
end
end
return interface