forked from Wiladams/LAPHLibs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemorystream.lua
More file actions
385 lines (291 loc) · 7.52 KB
/
memorystream.lua
File metadata and controls
385 lines (291 loc) · 7.52 KB
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
local ffi = require "ffi"
local stream = require "stream"
local MemoryStream = {}
setmetatable(MemoryStream, {
__call = function(self, ...)
return self:new(...)
end,
})
local MemoryStream_mt = {
__index = MemoryStream;
}
function MemoryStream.init(self, buff, bufflen, byteswritten)
if not buff then return nil end
if not bufflen then return nil end
byteswritten = byteswritten or 0
local obj = {
Length = bufflen,
Buffer = buff,
Position = 0,
BytesWritten = byteswritten,
}
setmetatable(obj, MemoryStream_mt)
return obj
end
-- MemoryStream constructors
--
-- MemoryStream:new(8192) -- Create a buffer with 8192 bytes
-- MemoryStream:new("string" [, len]) -- create a buffer atop some lua string
-- MemoryStream:new(cdata, len) -- create a buffer atop some cdata structure with length
function MemoryStream.new(self, ...)
local buff = nil;
local bufflen = nil;
local byteswritten = 0;
local nargs = select('#', ...);
if nargs == 1 then
if type(select(1, ...)=="number") then
bufflen = select(1,...);
buff = ffi.new("uint8_t[?]", bufflen)
byteswritten = 0
elseif type(select(1,...)=="string") then
buff = select(1, ...);
bufflen = #buff;
byteswritten = bufflen;
end
elseif nargs == 2 then
if type(select(1, ...))=="string" then
if type(select(2,...)) ~= "number" then
return nil;
end
buff = select(1,...);
bufflen = #buff;
byteswritten = bufflen;
elseif type(select(1,...))=="ctype" then
buff = select(1,...);
bufflen = ffi.sizeof(buff);
byteswritten = bufflen;
end
end
return self:init(buff, bufflen, byteswritten);
end
function MemoryStream:reset()
self.Position = 0
self.BytesWritten = 0
end
function MemoryStream:length()
return self.Length
end
function MemoryStream:position(pos, origin)
if pos ~= nil then
return self:seek(pos, origin);
end
return self.Position
end
function MemoryStream:remaining()
return self.Length - self.Position
end
function MemoryStream:bytesReadyToBeRead()
return self.BytesWritten - self.Position
end
function MemoryStream:canRead()
return self:bytesReadyToBeRead() > 0
end
function MemoryStream:seek(pos, origin)
origin = origin or stream.SEEK_SET
if origin == stream.SEEK_CUR then
local newpos = self.Position + pos
if newpos >= 0 and newpos < self.Length then
self.Position = newpos
end
elseif origin == stream.SEEK_SET then
if pos >= 0 and pos < self.Length then
self.Position = pos;
end
elseif origin == stream.SEEK_END then
local newpos = self.Length-1 + pos
if newpos >= 0 and newpos < self.Length then
self.Position = newpos
end
end
return self.Position
end
--[[
Reading interface
--]]
-- The Bytes() function acts as an iterator on bytes
-- from the stream.
function MemoryStream:bytes(maxbytes)
local buffptr = ffi.cast("const uint8_t *", self.Buffer);
local bytesleft = maxbytes or math.huge
local pos = -1
local function closure()
--print("-- REMAINING: ", bytesleft)
-- if we've read the maximum nuber of bytes
-- then just return nil to indicate finished
if bytesleft == 0 then return end
pos = pos + 1
-- We've reached the end of the stream
if pos >= self.Position then
return nil
end
bytesleft = bytesleft - 1
return buffptr[pos]
end
return closure
end
function MemoryStream:readByte()
local buffptr = ffi.cast("const uint8_t *", self.Buffer);
local pos = self.Position
if pos < self.BytesWritten then
self.Position = pos + 1
return buffptr[pos];
end
return nil, "eof"
end
function MemoryStream:readBytes(buff, count, offset)
offset = offset or 0
local pos = self.Position
local remaining = self:remaining()
local src = ffi.cast("const uint8_t *", self.Buffer)+pos
local dst = ffi.cast("uint8_t *", buff)+offset
local maxbytes = math.min(count, remaining)
if maxbytes < 1 then
return nil, "eof"
end
ffi.copy(dst, src, maxbytes)
self.Position = pos + maxbytes
return maxbytes
end
function MemoryStream:readString(count)
local pos = self.Position
local remaining = self.Length - pos
local maxbytes = math.min(count, remaining)
if maxbytes < 1 then return nil end
local src = ffi.cast("const uint8_t *", self.Buffer)+pos
-- advance the stream position
self.Position = pos + maxbytes
return ffi.string(src, maxbytes)
end
-- Read characters from a stream until the specified
-- ending is found, or until the stream runs out of bytes
local CR = string.byte("\r")
local LF = string.byte("\n")
function MemoryStream:readLine(maxbytes)
--print("-- MemoryStream:ReadLine()");
local readytoberead = self:bytesReadyToBeRead()
maxbytes = maxbytes or readytoberead
local maxlen = math.min(maxbytes, readytoberead)
local buffptr = ffi.cast("uint8_t *", self.Buffer);
local nchars = 0;
local bytesconsumed = 0;
local startptr = buffptr + self.Position
local abyte
local err
--print("-- MemoryStream:ReadLine(), maxlen: ", maxlen);
for n=1, maxlen do
abyte, err = self:readByte()
if not abyte then
break
end
bytesconsumed = bytesconsumed + 1
if abyte == LF then
break
elseif abyte ~= CR then
nchars = nchars+1
end
end
-- End of File, nothing consumed
if bytesconsumed == 0 then
return nil, "eof"
end
-- A blank line
if nchars == 0 then
return ''
end
-- an actual line of data
return ffi.string(startptr, nchars);
end
--[[
Writing interface
--]]
function MemoryStream:writeByte(byte)
-- don't write a nil value
-- a nil is not the same as a '0'
if not byte then return end
local pos = self.Position
if pos < self.Length-1 then
(ffi.cast("uint8_t *", self.Buffer)+pos)[0] = byte
self.Position = pos + 1
if self.Position > self.BytesWritten then
self.BytesWritten = self.Position
end
return 1
end
return false
end
function MemoryStream:writeBytes(buff, count, offset)
offset = offset or 0
local pos = self.Position
local size = self.Length
local remaining = size - pos
local maxbytes = math.min(remaining, count)
if maxbytes <= 0
then return 0
end
local dst = ffi.cast("uint8_t *", self.Buffer)+pos
local src = ffi.cast("const uint8_t *", buff)+offset
ffi.copy(dst, src, maxbytes);
self.Position = pos + maxbytes;
if self.Position > self.BytesWritten then
self.BytesWritten = self.Position
end
return maxbytes;
end
function MemoryStream:writeString(str, count, offset)
offset = offset or 0
count = count or #str
--print("-- MemoryStream:WriteString():", str);
return self:writeBytes(str, count, offset)
end
--[[
Write the specified number of bytes from the current
stream into the specified stream.
Start from the current position in the current stream
--]]
function MemoryStream:writeStream(stream, size)
local count = 0
local abyte = stream:readByte()
while abyte and count < size do
self:writeByte(abyte)
count = count + 1
abyte = stream:readByte()
end
return count
end
function MemoryStream:writeLine(line)
local status, err
if line then
status, err = self:writeString(line)
if err then
return nil, err
end
end
-- write the terminator
status, err = self:writeString("\r\n");
return status, err
end
--[[
Moving big chunks around
--]]
function MemoryStream:copyTo(stream)
-- copy from the beginning
-- to the current position
local remaining = self.BytesWritten
local byteswritten = 0
while (byteswritten < remaining) do
byteswritten = byteswritten + stream:writeBytes(self.Buffer, self.Position, byteswritten)
end
end
--[[
Utility
--]]
function MemoryStream:toString()
local len = self.Position
if len > 0 then
--print("Buffer: ", self.Buffer, len);
local str = ffi.string(self.Buffer, len)
return str;
end
return nil
end
return MemoryStream;