summaryrefslogtreecommitdiff
path: root/xmake/core/base/bytes.lua
blob: 06180c06a363e10f30fbdc317a0f0a6869df60b7 (plain)
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
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- 
-- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        bytes.lua
--

-- define module: bytes
local bytes = bytes or {}
local _instance = _instance or {}

-- load modules
local bit = require('bit')
local ffi = require('ffi')
local os  = require("base/os")

-- define ffi interfaces
ffi.cdef[[
    void* xm_ffi_malloc(unsigned long size);
    void  xm_ffi_free(void* data);
]]

-- new a bytes instance
--
-- bytes(size): allocates a buffer of given size
-- bytes(size, ptr [, manage]): mounts buffer on existing storage (manage memory or not)
-- bytes(str): mounts a buffer from the given string
-- bytes(bytes, start, last): mounts a buffer from another one, with start/last limits
-- bytes(bytes1, bytes2, bytes3, ...): allocates and concat buffer from list of byte buffers
-- bytes(bytes): allocates a buffer from another one (strict replica, sharing memory)
--
function _instance.new(...)
    local args = {...}
    local arg1, arg2, arg3 = unpack(args)
    local instance = table.inherit(_instance)
    if type(arg1) == "number" then
        local size = arg1
        local ptr = arg2 
        if ptr then
            -- bytes(size, ptr [, manage]): mounts buffer on existing storage (manage memory or not)
            local manage = arg3
            if manage then
                instance._CDATA   = ffi.gc(ffi.cast("unsigned char*", ptr), ffi.C.xm_ffi_free)
                instance._MANAGED = true
            else
                instance._CDATA   = ffi.cast("unsigned char*", ptr)
                instance._MANAGED = false
            end
        else
            -- bytes(size): allocates a buffer of given size
            ptr = ffi.C.xm_ffi_malloc(size)
            instance._CDATA   = ffi.gc(ffi.cast("unsigned char*", ptr), ffi.C.xm_ffi_free)
            instance._MANAGED = true
        end
        instance._SIZE     = size
        instance._READONLY = false
    elseif type(arg1) == "string" then
        -- bytes(str): mounts a buffer from the given string
        local str = arg1
        instance._SIZE     = #str
        instance._CDATA    = ffi.cast("unsigned char*", str)
        instance._REF      = str -- keep ref for GC
        instance._MANAGED  = false
        instance._READONLY = true
    elseif type(arg1) == "table" then
        if type(arg2) == 'number' then
            -- bytes(bytes, start, last): mounts a buffer from another one, with start/last limits:
            local b = arg1
            local start = arg2 or 1
            local last = arg3 or b:size()
            if start < 1 or last > b:size() then
                os.raise("incorrect bounds(%d-%d) for bytes(...)!", start, last)
            end
            instance._SIZE     = last - start + 1
            instance._CDATA    = b:cdata() - 1 + start
            instance._REF      = b -- keep lua ref for GC
            instance._MANAGED  = false
            instance._READONLY = b:readonly()
        elseif type(arg2) == "table" then
            -- bytes(bytes1, bytes2, bytes3, ...): allocates and concat buffer from list of byte buffers
            instance._SIZE = 0
            for _, b in ipairs(args) do
                instance._SIZE = instance._SIZE + b:size()
            end
            instance._CDATA = ffi.gc(ffi.cast("unsigned char*", ffi.C.xm_ffi_malloc(instance._SIZE)), ffi.C.xm_ffi_free)
            local offset = 0
            for _, b in ipairs(args) do
                ffi.copy(instance._CDATA + offset, b:cdata(), b:size())
                offset = offset + b:size()
            end
            instance._MANAGED  = true
            instance._READONLY = false
        elseif not arg2 and arg1:size() then
            -- bytes(bytes): allocates a buffer from another one (strict replica, sharing memory)
            local b = arg1
            local start = 1
            local last = arg3 or b:size()
            if start < 1 or last > b:size() then
                os.raise("incorrect bounds(%d-%d)!", start, last)
            end
            instance._SIZE     = last - start + 1
            instance._CDATA    = b:cdata() - 1 + start
            instance._REF      = b -- keep lua ref for GC
            instance._MANAGED  = false
            instance._READONLY = b:readonly()
        end
    end
    if instance:cdata() == nil then
        os.raise("invalid arguments for bytes(...)!")
    end
    setmetatable(instance, _instance)
    return instance
end

-- get bytes size
function _instance:size()
    return self._SIZE
end

-- get bytes data
function _instance:cdata()
    return self._CDATA
end

-- readonly?
function _instance:readonly()
    return self._READONLY
end

-- bytes:ipairs()
function _instance:ipairs()
    local index = 0
    return function (...)
        if index < self:size() then
            index = index + 1
            return index, self[index]
        end
    end
end

-- get a slice of bytes
function _instance:slice(start, last)
    return bytes(self, start, last)
end

-- copy bytes 
function _instance:copy(src)
    if self:readonly() then
        os.raise("%s: cannot be modified!", self)
    end
    if type(src) == 'string' then
        src = bytes(src)
    end
    if src:size() ~= self:size() then
        os.raise("%s: cannot copy bytes, src and dst must have same size(%d->%d)!", self, src:size(), self:size())
    end
    ffi.copy(self:cdata(), src:cdata(), self:size())
    return self
end

-- clone a new bytes buffer
function _instance:clone()
    local new = bytes(self:size())
    new:copy(self)
    return new
end

-- convert bytes to string
function _instance:str(i, j)
    local offset = i and i - 1 or 0
    return ffi.string(self:cdata() + offset, (j or self:size()) - offset)
end

-- get byte or bytes slice at the given index position
--
-- bytes[1]
-- bytes[{1, 2}]
--
function _instance:__index(key)
    if type(key) == "number" then
        if key < 1 or key > self:size() then 
            os.raise("%s: index(%d/%d) out of bounds!", self, key, self:size()) 
        end
        return self._CDATA[key - 1]
    elseif type(key) == "table" then
        local start, last = key[1], key[2]
        return self:slice(start, last)
    end
    return rawget(self, key)
end

-- set byte or bytes slice at the given index position
--
-- bytes[1] = 0x1
-- bytes[{1, 2}] = bytes(2)
--
function _instance:__newindex(key, value)
    if self:readonly() then
        os.raise("%s: cannot modify value at index[%s]!", self, key)
    end
    if type(key) == "number" then
        if key < 1 or key > self:size() then 
            os.raise("%s: index(%d/%d) out of bounds!", self, key, self:size()) 
        end
        self._CDATA[key - 1] = value
        return
    elseif type(key) == "table" then
        local start, last = key[1], key[2]
        self:slice(start,last):copy(value)
        return
    end
    rawset(self, key, value)
end

-- concat two bytes buffer
function _instance:__concat(other)
    local new = bytes(self:size() + other:size())
    new:slice(1, self:size()):copy(self)
    new:slice(self:size() + 1, new:size()):copy(other)
    return new
end

-- tostring(bytes)
function _instance:__tostring()
    local parts = {}
    local size = self:size()
    if size > 16 then
        size = 16
    end
    for i = 1, size do
        parts[i] = bit.tohex(self[i], 2)
    end
    return "<bytes(" .. self:size() .. "): " .. table.concat(parts, " ") .. (self:size() > 16 and "..>" or ">")
end

-- new an bytes instance
function bytes.new(...)
    return _instance.new(...)
end

-- register call function
setmetatable(bytes, {
    __call = function (_, ...) 
        return bytes.new(...) 
    end,
    __tostring = function()
        return "<bytes>"
    end
})

-- return module: bytes
return bytes