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
|
print("Hello from array.lua!")
local String = string
local Bit = include("bit.lua");
local XOR = Bit.bxor;
local Array = {};
Array.size = function(array)
return #array;
end
Array.fromString = function(string)
local bytes = {};
local i=1;
local byte = String.byte(string,i);
while byte ~= nil do
bytes[i] = byte;
i = i + 1;
byte = String.byte(string,i);
end
return bytes;
end
Array.toString = function(bytes)
local chars = {};
local i=1;
local byte = bytes[i];
while byte ~= nil do
chars[i] = String.char(byte);
i = i+1;
byte = bytes[i];
end
return table.concat(chars,"");
end
Array.fromStream = function(stream)
local array = {};
local i=1;
local byte = stream();
while byte ~= nil do
array[i] = byte;
i = i+1;
byte = stream();
end
return array;
end
Array.readFromQueue = function(queue,size)
local array = {};
for i=1,size do
array[i] = queue.pop();
end
return array;
end
Array.writeToQueue = function(queue,array)
local size = Array.size(array);
for i=1,size do
queue.push(array[i]);
end
end
Array.toStream = function(array)
local queue = Queue();
local i=1;
local byte = array[i];
while byte ~= nil do
queue.push(byte);
i=i+1;
byte = array[i];
end
return queue.pop;
end
local fromHexTable = {};
for i=0,255 do
fromHexTable[String.format("%02X",i)]=i;
fromHexTable[String.format("%02x",i)]=i;
end
Array.fromHex = function(hex)
local array = {};
for i=1,String.len(hex)/2 do
local h = String.sub(hex,i*2-1,i*2);
array[i] = fromHexTable[h];
end
return array;
end
local toHexTable = {};
for i=0,255 do
toHexTable[i]=String.format("%02X",i);
end
Array.toHex = function(array)
local hex = {};
local i = 1;
local byte = array[i];
while byte ~= nil do
hex[i] = toHexTable[byte];
i=i+1;
byte = array[i];
end
return table.concat(hex,"");
end
Array.concat = function(a,b)
local concat = {};
local out=1;
local i=1;
local byte = a[i];
while byte ~= nil do
concat[out] = byte;
i = i + 1;
out = out + 1;
byte = a[i];
end
local i=1;
local byte = b[i];
while byte ~= nil do
concat[out] = byte;
i = i + 1;
out = out + 1;
byte = b[i];
end
return concat;
end
Array.truncate = function(a,newSize)
local x = {};
for i=1,newSize do
x[i]=a[i];
end
return x;
end
Array.XOR = function(a,b)
local x = {};
for k,v in pairs(a) do
x[k] = XOR(v,b[k]);
end
return x;
end
Array.substitute = function(input,sbox)
local out = {};
for k,v in pairs(input) do
out[k] = sbox[v];
end
return out;
end
Array.permute = function(input,pbox)
local out = {};
for k,v in pairs(pbox) do
out[k] = input[v];
end
return out;
end
Array.copy = function(input)
local out = {};
for k,v in pairs(input) do
out[k] = v;
end
return out;
end
Array.slice = function(input,start,stop)
local out = {};
for i=start,stop do
out[i-start+1] = input[i];
end
return out;
end
return Array;
|