-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathhexdecheaderwdx.lua
115 lines (106 loc) · 3.53 KB
/
hexdecheaderwdx.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
-- hexdecheaderwdx.lua
local skipsysfiles = true -- skip system files (character, block or fifo files on linux)
local showoffset = true -- show offset near field name
local values = {
"66 74 79 70 69 73 6F 6D",
"ftypisom",
}
local fields = {
-- field name, offset, number of bytes, type, result(boolean field)
{"header", "0x0", 8, "hex", nil},
{"header", "0x0", 15, "dec", nil},
{"header", "0x0", 4, "raw", nil},
{"some offset", "0x20B0", 16, "hex", nil},
{"test check", "0x4", 8, "hex", values[1]},
{"test check", "0x4", 8, "raw", values[2]},
}
function ContentGetSupportedField(FieldIndex)
if (fields[FieldIndex + 1] ~= nil) then
local fieldname = fields[FieldIndex + 1][1];
if (tonumber(fields[FieldIndex + 1][2]) > 0) and (showoffset == true) then
fieldname = fieldname .. " " .. fields[FieldIndex + 1][2];
if (fields[FieldIndex + 1][5] ~= nil) and (fields[FieldIndex + 1][4] ~= "raw") then
fieldname = fieldname .. ": " .. fields[FieldIndex + 1][5];
end
end
if (fields[FieldIndex + 1][4] ~= "hex") then
fieldname = fieldname .. " (" .. fields[FieldIndex + 1][4] .. ")";
end
if (fields[FieldIndex + 1][5] ~= nil) then
return fieldname, '', 6;
else
return fieldname, getUnitsString(fields[FieldIndex + 1][3]), 8;
end
end
return '', '', 0; -- ft_nomorefields
end
function ContentGetValue(FileName, FieldIndex, UnitIndex, flags)
local attr = SysUtils.FileGetAttr(FileName);
if (isWrongAttr(attr) == true) then
return nil;
end
local f = io.open(FileName, "rb");
local result = nil;
if (f ~= nil) then
f:seek("set", tonumber(fields[FieldIndex + 1][2]));
if (fields[FieldIndex + 1][5] ~= nil) then
bytes = f:read(fields[FieldIndex + 1][3]);
if (getBytes(fields[FieldIndex + 1][4], bytes) == fields[FieldIndex + 1][5]) then
result = true;
else
result = false;
end
else
bytes = f:read(UnitIndex + 1);
result = getBytes(fields[FieldIndex + 1][4], bytes);
end
f:close();
end
return result;
end
function getFormattedString(formatstring, bytes)
if (bytes == nil) then
return nil;
else
local t = {};
for b in string.gfind(bytes, ".") do
table.insert(t, string.format(formatstring, string.byte(b)));
end
local result = table.concat(t);
return result;
end
end
function getBytes(ftype, bytes)
if (ftype == "raw") then
return bytes;
end
local formatstring = "%02X ";
if (ftype == "dec") then
formatstring = "%03d ";
end
local result = getFormattedString(formatstring, bytes);
if (result ~= nil) then
return result:sub(1, - 2);
end
return nil;
end
function getUnitsString(num)
if (num < 1) then
return '';
end
local result = "1 byte";
if (num > 1) then
for i = 2, num do
result = result .. '|' .. i .. " bytes";
end
end
return result;
end
function isWrongAttr(vattr)
if (vattr < 0) or (math.floor(vattr / 0x00000010) % 2 ~= 0) then
return true;
elseif (math.floor(vattr / 0x00000004) % 2 ~= 0) and (skipsysfiles == true) then
return true;
end
return false;
end