-
Notifications
You must be signed in to change notification settings - Fork 0
/
shifting.lua
154 lines (137 loc) · 6.01 KB
/
shifting.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
function array_lshiftrotate_n(buffer, cursor, n)
-- Rotate-shift the buffer, to adjust for the single-bit values.
local raw_array = buffer:bytes(cursor):raw(0)
local hex_array
for index = 0, n-2
do
raw_array = array_lshiftrotate(raw_array, false)
end
raw_array, hex_array = array_lshiftrotate(raw_array, true)
-- Skip the first byte, which is garbage left over from the rotation.
raw_array = raw_array:sub(2)
hex_array = hex_array:sub(3)
local byte_array = ByteArray.new(hex_array)
buffer = ByteArray.new(hex_array):tvb("Bit Shifted")
cursor = 0
return buffer, cursor
end
function array_lshiftrotate(array, with_hex_array)
-- The bytes are aligned like this: AAA..... BBBAAAAA ...BBBBB
-- 567 56701234 56701234
-- Procedure: Work from right to left
-- Copy MSB from byte, shift to the left
-- Write saved MSB into LSB of byte to the right
-- AA...... BBAAAAAA ..BBBBBB
-- A....... BAAAAAAA .BBBBBBB
-- ........ AAAAAAAA BBBBBBBB
local length = string.len(array)
local hex_table = {}
local binary_table = {}
for index = length, 1, -1
do
-- Copy MSB
local last_msb = bit.band((string.byte(array, index) or 0), 0x80)
-- Shift byte to the left
local shifted = bit.lshift((string.byte(array, index) or 0), 1)
-- Cleanup: Strip leftmost bit, and blank out new rightmost
shifted = bit.band(shifted, 0xFE)
local right_byte = 0x00
if (index == length)
then
binary_table[index] = string.char(shifted)
if (with_hex_array or false)
then
hex_table[index] = string.format("%02x", shifted)
end
else
-- If there's a byte to the right, carry over the last MSB into the LSB of the right byte
-- First shift the MSB to the LSB
-- Fetch the right byte from the table | |
-- Fill the new LSB in | |
-- | | | |
right_byte = bit.bor((string.byte(binary_table[index+1]) or 0), bit.rshift(last_msb, 7))
right_byte = bit.band(right_byte, 0xFF)
-- Now fill them into the array
binary_table[index+1] = string.char(right_byte)
binary_table[index] = string.char(shifted)
if (with_hex_array or false)
then
hex_table[index+1] = string.format("%02x", right_byte)
hex_table[index] = string.format("%02x", shifted)
end
end
end
return table.concat(binary_table), table.concat(hex_table)
end
-- Unused
function array_lshiftrotate_slow(array, with_hex_array)
-- The bytes are aligned like this: AAA..... BBBAAAAA ...BBBBB
-- 567 56701234 56701234
-- Procedure: Work from right to left
-- Copy MSB from byte, shift to the left
-- Write saved MSB into LSB of byte to the right
-- AA...... BBAAAAAA ..BBBBBB
-- A....... BAAAAAAA .BBBBBBB
-- ........ AAAAAAAA BBBBBBBB
local length = string.len(array)
local hex_array
with_hex_array = true
if (with_hex_array or false)
then
hex_array = array
end
for index = length, 1, -1
do
-- Copy MSB
local last_msb = bit.band((string.byte(array, index) or 0), 0x80)
-- Shift byte to the left
local shifted = bit.lshift((string.byte(array, index) or 0), 1)
-- Cleanup: Strip leftmost bit, and blank out new rightmost
shifted = bit.band(shifted, 0xFE)
local right_byte = 0x00
if (index < length)
then
-- If there's a byte to the right, carry over the last MSB into the LSB of the right byte
-- First shift the MSB to the LSB
-- local lsb = bit.rshift(last_msb, 7)
right_byte = bit.bor((string.byte(array, index+1) or 0), bit.rshift(last_msb, 7))
right_byte = bit.band(right_byte, 0xFF)
-- Now fill them into the array
array = array:sub(1, index-1) .. string.char(shifted) .. string.char(right_byte) .. array:sub(index+2)
if (with_hex_array or false)
then
hex_array = hex_array:sub(1, (index-1))
.. string.format("%02x", shifted)
.. string.format("%02x", right_byte)
.. hex_array:sub((index+3))
end
else
array = array:sub(1, index-1) .. string.char(shifted) .. array:sub(index+1)
if (with_hex_array or false)
then
hex_array = hex_array:sub(1, (index-1)) .. string.format("%02x", shifted)
end
end
end
return array, hex_array
end
-- Unused
function array_lshift(array)
local length = string.len(array)
local hex_array = array
for index = 1, length
do
-- Shift byte to the left
local shifted = bit.lshift((string.byte(array, index) or 0), 1)
-- Strip leftmost bit, and blank out new rightmost
shifted = bit.band(shifted, 0xFE)
if (index < length)
then
-- If there's a next byte, carry over the next leftmost bit
shifted = bit.bor(shifted, bit.rshift(bit.band((string.byte(array, index+1) or 0), 0x80), 7))
end
array = array:sub(1, index-1) .. string.char(bit.band(shifted, 0xFF)) .. array:sub(index+1)
hex_array = hex_array:sub(1, (index-1)*2) .. string.format("%02x", bit.band(shifted, 0xFF)) .. hex_array:sub((index-1)*2+2)
end
return array, hex_array
end