-
Notifications
You must be signed in to change notification settings - Fork 3
/
cbormisc.lua
277 lines (240 loc) · 6.61 KB
/
cbormisc.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
-- ***************************************************************
--
-- Copyright 2016 by Sean Conner. All Rights Reserved.
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or (at your
-- option) any later version.
--
-- This library is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
-- License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with this library; if not, see <http://www.gnu.org/licenses/>.
--
-- Comments, questions and criticisms can be sent to: sean@conman.org
--
-- ====================================================================
--
-- Output in the CBOR dianostic format
--
-- luacheck: globals _ENV TYPE TAG SIMPLE diagnostic pdiagnostic
-- luacheck: ignore 611
-- ***************************************************************
local string = require "string"
local math = require "math"
local cbor_c = require "org.conman.cbor_c"
local _VERSION = _VERSION
local setmetatable = setmetatable
local tostring = tostring
local pcall = pcall
local type = type
if _VERSION == "Lua 5.1" then
module "org.conman.cbormisc" -- luacheck: ignore
else
_ENV = {} -- luacheck: ignore
end
-- ***************************************************************
local char_trans =
{
['\a'] = '\\a',
['\b'] = '\\b',
['\t'] = '\\t',
['\n'] = '\\n',
['\v'] = '\\v',
['\f'] = '\\f',
['\r'] = '\\r',
['"'] = '\\"',
['\\'] = '\\\\',
}
local function safestring(v)
if type(v) == 'string' then
return '"' .. v:gsub(".",function(c)
if char_trans[c] then
return char_trans[c]
end
local b = c:byte()
if b < 32 or b > 126 then
return string.format("\\%03d",b)
else
return c
end
end) .. '"'
else
return tostring(v)
end
end
-- *************************************************************
local function bintext(packet,pos,info,value,ctype,f)
if info == 31 then
local res = "(_ "
local comma = ""
while true do
local ltype,nvalue,npos = diagnostic(packet,pos)
if ltype == '__break' then
return ctype,res .. ")",npos
end
res = res .. comma .. nvalue
comma = ", "
pos = npos
end
end
local bt = packet:sub(pos,pos + value - 1)
return ctype,f(bt),pos + value
end
-- ***************************************************************
TYPE =
{
[0x00] = function(_,pos,_,value)
return 'UINT',tostring(value),pos
end,
[0x20] = function(_,pos,_,value)
return 'NINT',tostring(-1 - value),pos
end,
[0x40] = function(packet,pos,info,value)
return bintext(packet,pos,info,value,'BIN',function(c)
c = c:gsub(".",function(ch)
return string.format("%02X",ch:byte())
end)
return string.format("h'%s'",c)
end)
end,
[0x60] = function(packet,pos,info,value)
return bintext(packet,pos,info,value,'TEXT',safestring)
end,
[0x80] = function(packet,pos,info,value)
local res = "["
if info == 31 then
res = res .. "_ "
end
for i = 1 , value do
local ctype,s,npos = diagnostic(packet,pos)
if ctype == '__break' then break end
res = res .. s
if i < value then res = res .. ", " end
pos = npos
end
return 'ARRAY',res .. "]",pos
end,
[0xA0] = function(packet,pos,info,value)
local res = "{"
if info == 31 then
res = res .. "_ "
end
for i = 1 , value do
local ctypen,name,npos = diagnostic(packet,pos)
if ctypen == '__break' then break end
local _,val,npos2 = diagnostic(packet,npos)
res = res .. string.format("%s: %s",name,val)
if i < value then res = res .. ", " end
pos = npos2
end
return 'MAP',res .. "}",pos
end,
[0xC0] = function(packet,pos,_,value)
local _,s,npos = diagnostic(packet,pos)
local ctype = TAG[value]
return ctype,string.format("%s(%s)",ctype,s),npos
end,
[0xE0] = function(_,pos,info,value)
if info >= 25 and info <= 27 then
return '__float',SIMPLE[info](value),pos
elseif info == 31 then
return '__break',math.huge,pos
else
return SIMPLE[value],SIMPLE[value],pos
end
end,
}
-- ***************************************************************
TAG = setmetatable(
{
[ 0] = "_datetime",
[ 1] = "_epoch",
[ 2] = "_pbignum",
[ 3] = "_nbignum",
[ 4] = "_decimalfraction",
[ 5] = "_bigfloat",
[ 21] = "_tobase64url",
[ 22] = "_tobase64",
[ 23] = "_tobase16",
[ 24] = "_cbor",
[ 32] = "_url",
[ 33] = "_base64url",
[ 34] = "_base64",
[ 35] = "_regex",
[ 36] = "_mime",
[55799] = "_magic_cbor",
[ 25] = "_nthstring",
[ 26] = "_perlobj",
[ 27] = "_serialobj",
[ 28] = "_shareable",
[ 29] = "_sharedref",
[ 30] = "_rational",
[ 37] = "_uuid",
[ 38] = "_language",
[ 39] = "_id",
[ 256] = "_stringref",
[ 257] = "_bmime",
[ 264] = "_decimalfractionexp",
[ 265] = "_bigfloatexp",
[22098] = "_indirection",
},
{
__index = function(_,key)
return tostring(key)
end
}
)
-- ***************************************************************
local function simple(value)
if value ~= value then
return 'NaN'
elseif value == math.huge then
return 'Infinity'
elseif value == -math.huge then
return '-Infinity'
else
return string.format('%f',value)
end
end
-- ***************************************************************
SIMPLE = setmetatable(
{
[20] = 'false',
[21] = 'true',
[22] = 'null',
[23] = 'undefined',
[25] = simple,
[26] = simple,
[27] = simple,
[31] = '__break',
},
{
__index = function(_,key)
return string.format("simple(%d)",key)
end
}
)
-- ***************************************************************
function diagnostic(packet,pos)
pos = pos or 1
local ctype,info,value,npos = cbor_c.decode(packet,pos)
return TYPE[ctype](packet,npos,info,value)
end
-- ***************************************************************
function pdiagnostic(packet,pos)
local okay,result = pcall(diagnostic,packet,pos)
if okay then
return result
else
return nil,result
end
end
-- ***************************************************************
if _VERSION > "Lua 5.1" then
return _ENV
end