-
Notifications
You must be signed in to change notification settings - Fork 0
/
filebuffer.lua
548 lines (439 loc) · 13.6 KB
/
filebuffer.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
--->use-luvit<---
local ffi = require 'ffi'
local cast, typeof = ffi.cast, ffi.typeof
local assert, type, tbins, tbrem = assert, type, table.insert, table.remove
ffi.cdef [[
//void* malloc(size_t size);
//void free(void* mem);
//void* memset(void* ptr, int val, size_t n);
typedef struct _FILE FILE;
FILE *fopen(const char *filename, const char *mode);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
int fflush(FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int fgetc(FILE *stream);
int fputc(int ch, FILE *stream);
int fputs(const char *string, FILE *stream);
int fclose(FILE *stream);
]]
if ffi.abi('win') then
ffi.cdef [[ int access(const char *pathname, int how) __asm__("_access"); ]]
else
ffi.cdef [[ int access(const char *pathname, int how); ]]
end
if ffi.abi('64bit') then
if ffi.abi('win') then
ffi.cdef [[
int fseek(FILE *stream, int64_t offset, int whence) __asm__("_fseeki64");
int64_t ftell(FILE *stream) __asm__("_ftelli64");
]]
else
ffi.cdef [[
int fseek(FILE *stream, int64_t offset, int whence) __asm__("fseeko64");
int64_t ftell(FILE *stream) __asm__("ftello64");
]]
end
else
ffi.cdef [[
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
]]
end
local SEEK = {
SET = 0,
CUR = 1,
END = 2,
}
if ffi.abi('win') then
ffi.cdef [[
uint16_t bswap16(uint16_t x) __asm__("_byteswap_ushort");
uint32_t bswap32(uint32_t x) __asm__("_byteswap_ulong");
uint64_t bswap64(uint64_t x) __asm__("_byteswap_uint64");
]]
else
ffi.cdef [[
uint16_t bswap16(uint16_t x) __asm__("__builtin_bswap16");
uint32_t bswap32(uint32_t x) __asm__("__builtin_bswap32");
uint64_t bswap64(uint64_t x) __asm__("__builtin_bswap64");
]]
end
ffi.cdef [[
union arrToU16 { uint8_t arr[2]; uint16_t n; };
union arrToU32 { uint8_t arr[4]; uint32_t n; };
union arrToU64 { uint8_t arr[8]; uint64_t n; };
union u16ToArr { uint16_t n; uint8_t arr[2]; };
union u32ToArr { uint32_t n; uint8_t arr[4]; };
union u64ToArr { uint64_t n; uint8_t arr[8]; };
]]
-- local C = (ffi.os == 'Windows') and ffi.load('msvcrt') or ffi.C
local U = (ffi.os == 'Windows') and ffi.load('ucrtbase') or ffi.C
local i8, i16, i32, i64 = typeof 'int8_t', typeof 'int16_t', typeof 'int32_t', typeof 'int64_t'
local u8_2, u8_4, u8_8 = typeof 'uint8_t[2]', typeof 'uint8_t[4]', typeof 'uint8_t[8]'
local c_arr = typeof 'char[?]'
local conv = {}
do
local arrToU16, u16ToArr = typeof 'union arrToU16', typeof 'union u16ToArr'
local arrToU32, u32ToArr = typeof 'union arrToU32', typeof 'union u32ToArr'
local arrToU64, u64ToArr = typeof 'union arrToU64', typeof 'union u64ToArr'
if ffi.abi('le') then
conv.asLE16 = function(arr) return arrToU16(arr).n end
conv.asLE32 = function(arr) return arrToU32(arr).n end
conv.asLE64 = function(arr) return arrToU64(arr).n end
conv.fromLE16 = function(n) return u16ToArr(n).arr end
conv.fromLE32 = function(n) return u32ToArr(n).arr end
conv.fromLE64 = function(n) return u64ToArr(n).arr end
conv.asBE16 = function(arr) return U.bswap16(arrToU16(arr).n) end
conv.asBE32 = function(arr) return U.bswap32(arrToU32(arr).n) end
conv.asBE64 = function(arr) return U.bswap64(arrToU64(arr).n) end
conv.fromBE16 = function(n) return u16ToArr(U.bswap16(n)).arr end
conv.fromBE32 = function(n) return u32ToArr(U.bswap32(n)).arr end
conv.fromBE64 = function(n) return u64ToArr(U.bswap64(n)).arr end
else
conv.asLE16 = function(arr) return U.bswap16(arrToU16(arr).n) end
conv.asLE32 = function(arr) return U.bswap32(arrToU32(arr).n) end
conv.asLE64 = function(arr) return U.bswap64(arrToU64(arr).n) end
conv.fromLE16 = function(n) return u16ToArr(U.bswap16(n)).arr end
conv.fromLE32 = function(n) return u32ToArr(U.bswap32(n)).arr end
conv.fromLE64 = function(n) return u64ToArr(U.bswap64(n)).arr end
conv.asBE16 = function(arr) return arrToU16(arr).n end
conv.asBE32 = function(arr) return arrToU32(arr).n end
conv.asBE64 = function(arr) return arrToU64(arr).n end
conv.fromBE16 = function(n) return u16ToArr(n).arr end
conv.fromBE32 = function(n) return u32ToArr(n).arr end
conv.fromBE64 = function(n) return u64ToArr(n).arr end
end
end
-- TODO make all cdecls into typeof return calls
---@alias endianness
---|>'"le"' # Little-endian
---| '"be"' # Big-endian
--- File buffer for sequential and 0-indexed reading/writing.
---@class FileBuffer
---@field pos number # Position to read next
---@field mode endianness # Endianness of buffer reads/writes
---@field rw boolean # Is the buffer also writable
---@field pos_stack number[] # Position stack for push and pop operations
---@field file ffi.cdata* # File handle
---@field bufsize number # fopen buffer size
---@field _funcs table private
local FileBuffer = {}
---@class FileBuffer
local LEfn = {}
---@class FileBuffer
local BEfn = {}
local function FileBuffer__index(self, key)
if type(key) == 'number' then
assert(key >= 0, "Index cannot be negative.")
self:push():seek(key)
local b = U.fgetc(self.file)
self:pop()
assert(b ~= -1, "Reached end of file.")
return b
end
if key == 'pos' then
return U.ftell(self.file)
end
return self._funcs[key] or FileBuffer[key]
end
---@return FileBuffer?, string?
function FileBuffer.from(filename, options)
options = options or {}
local self = setmetatable({}, {__index = FileBuffer__index})
self.mode = options.mode == 'be' and 'be' or 'le'
self.rw = options.rw and true or false
self.pos_stack = {}
self.bufsize = options.bufsize or 0
self._funcs = (self.mode == 'be') and BEfn or LEfn
-- Since we can't somehow get errno, use access to check exist and r/w
if U.access(filename, 0) == -1 then
return nil, "ENOENT: no such file."
end
if U.access(filename, self.rw and 0x6 or 0x4) == -1 then
return nil, "EACCES: cannot access file."
end
self.file = U.fopen(filename, self.rw and 'rb+' or 'rb')
if self.file == nil then
return nil, "Unknown error."
end
ffi.gc(self.file, U.fclose)
if self.bufsize > 0 then
-- Buffer with specified size
U.setvbuf(self.file, nil, 0, self.bufsize)
elseif self.bufsize < 0 then
-- No buffer
U.setvbuf(self.file, nil, 4, 0)
end
return self
end
--- Other ---
function FileBuffer:checkWrite()
assert(self.rw, "Buffer is read-only, write operations not supported.")
-- needed due to buffering
U.fseek(self.file, 0, SEEK.CUR)
end
function FileBuffer:flush()
U.fflush(self.file)
end
function FileBuffer:close()
U.fclose(ffi.gc(self.file, nil))
setmetatable(self, {__index = function() error("FileBuffer already closed.") end})
end
--- Position functions ---
--- Advance pos by `n`, returning current position before advance
function FileBuffer:advance(n)
local current = self.pos
U.fseek(self.file, n, SEEK.CUR)
return current
end
function FileBuffer:push()
tbins(self.pos_stack, self.pos)
return self
end
function FileBuffer:pop()
local n = assert(tbrem(self.pos_stack), "Nothing to pop from position stack.")
U.fseek(self.file, n, SEEK.SET)
return self
end
---@param pos number
function FileBuffer:seek(pos)
assert(type(pos) == 'number' or type(pos) == 'cdata', "pos must be a number.")
U.fseek(self.file, pos, pos < 0 and SEEK.END or SEEK.SET)
return self
end
--- Read functions ---
local function complement8(n)
return cast(i8, n)
end
local function complement16(n)
return cast(i16, n)
end
local function complement32(n)
-- weird things happen with casting directly to an int32_t
return cast(i32, cast(i64, n))
end
local function complement64(n)
return cast(i64, n)
end
--
function FileBuffer:iterold()
return function()
local pos = self.pos
local b = U.fgetc(self.file)
if b ~= -1 then
return pos, b
end
end
end
local u8_buf = typeof 'uint8_t[?]'
function FileBuffer:iter(bufsize)
bufsize = bufsize or 4096
local buf = u8_buf(bufsize)
local pos, n, i = 0, 0, 0
return function()
if i == 0 then
pos = U.ftell(self.file)
n = U.fread(buf, 1, bufsize, self.file)
if n == 0 then return end
i = n
end
local j = n - i
i = i - 1
return pos + j, buf[j]
end
end
--
function LEfn:read_u8()
local b = U.fgetc(self.file)
assert(b ~= -1, "Reached end of file.")
return b
end
BEfn.read_u8 = LEfn.read_u8
function LEfn:read_i8()
return complement8(self:read_u8())
end
BEfn.read_i8 = LEfn.read_i8
--
function LEfn:read_u16()
local buf = u8_2()
local br = U.fread(buf, 1, 2, self.file)
if br < 2 then
self:advance(-br)
error("Reached end of file.")
end
return conv.asLE16(buf)
end
function BEfn:read_u16()
local buf = u8_2()
local br = U.fread(buf, 1, 2, self.file)
if br < 2 then
self:advance(-br)
error("Reached end of file.")
end
return conv.asBE16(buf)
end
function LEfn:read_i16()
return complement16(self:read_u16())
end
BEfn.read_i16 = LEfn.read_i16
--
function LEfn:read_u32()
local buf = u8_4()
local br = U.fread(buf, 1, 4, self.file)
if br < 4 then
self:advance(-br)
error("Reached end of file.")
end
return conv.asLE32(buf)
end
function BEfn:read_u32()
local buf = u8_4()
local br = U.fread(buf, 1, 4, self.file)
if br < 4 then
self:advance(-br)
error("Reached end of file.")
end
return conv.asBE32(buf)
end
function LEfn:read_i32()
return complement32(self:read_u32())
end
BEfn.read_i32 = LEfn.read_i32
--
function LEfn:read_u64()
local buf = u8_8()
local br = U.fread(buf, 1, 8, self.file)
if br < 8 then
self:advance(-br)
error("Reached end of file.")
end
return conv.asLE64(buf)
end
function BEfn:read_u64()
local buf = u8_8()
local br = U.fread(buf, 1, 8, self.file)
if br < 8 then
self:advance(-br)
error("Reached end of file.")
end
return conv.asBE64(buf)
end
function LEfn:read_i64()
return complement64(self:read_u64())
end
BEfn.read_i64 = LEfn.read_i64
--
local ffistr = ffi.string
function FileBuffer:read_bytes(len)
assert(type(len) == 'number' and len >= 0, "len must be a positive integer.")
local buf = c_arr(len)
assert(U.fread(buf, 1, len, self.file) == len, "Reached end of file.")
return ffistr(buf, len)
end
--- Write functions ---
function LEfn:write_u8(val)
self:checkWrite()
assert(U.fputc(val, self.file) ~= -1, "Reached end of file.")
return self
end
BEfn.write_u8 = LEfn.write_u8
function LEfn:write_i8(val)
self:checkWrite()
assert(U.fputc(val, self.file) ~= -1, "Reached end of file.")
return self
end
BEfn.write_i8 = LEfn.write_u8
--
function LEfn:write_u16(val)
self:checkWrite()
local bw = U.fwrite(conv.fromLE16(val), 1, 2, self.file)
if bw < 2 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
function LEfn:write_i16(val)
self:checkWrite()
local bw = U.fwrite(conv.fromLE16(val), 1, 2, self.file)
if bw < 2 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
function BEfn:write_u16(val)
self:checkWrite()
local bw = U.fwrite(conv.fromBE16(val), 1, 2, self.file)
if bw < 2 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
BEfn.write_i16 = BEfn.write_u16
--
function LEfn:write_u32(val)
self:checkWrite()
local bw = U.fwrite(conv.fromLE32(val), 1, 4, self.file)
if bw < 4 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
function LEfn:write_i32(val)
self:checkWrite()
local bw = U.fwrite(conv.fromLE32(val), 1, 4, self.file)
if bw < 4 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
function BEfn:write_u32(val)
self:checkWrite()
local bw = U.fwrite(conv.fromBE32(val), 1, 4, self.file)
if bw < 4 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
BEfn.write_i32 = BEfn.write_u32
--
function LEfn:write_u64(val)
self:checkWrite()
local bw = U.fwrite(conv.fromLE64(val), 1, 8, self.file)
if bw < 8 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
function LEfn:write_i64(val)
self:checkWrite()
local bw = U.fwrite(conv.fromLE64(val), 1, 8, self.file)
if bw < 8 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
function BEfn:write_u64(val)
self:checkWrite()
local bw = U.fwrite(conv.fromBE64(val), 1, 8, self.file)
if bw < 8 then
self:advance(-bw)
error("Reached end of file and cannot write further.")
end
return self
end
BEfn.write_i64 = BEfn.write_u64
--
function FileBuffer:write_bytes(val)
self:checkWrite()
assert(U.fputs(val, self.file) ~= -1, "Reached end of file.")
return self
end
return FileBuffer