I was wondering, how would I make a simple lua string or entire code look look C++ compiled code but run as regular vanilla lua?
print("Test string") -- How would this look like C++ compiler code?
With Lua you can not directly dump print to a binary Format.
...as i know.
Dumping a Function to a Binary is easy doing with own defined Functions...
> -- Lua 5.4
> myfunc = function() print("Teststring") return end
> string.dump(myfunc, true)
uaT�
�
xV(w#����
��DGG��print�Teststring������
> load(string.dump(myfunc, true))()
Teststring
As you can see, like in a compiled C Binary the Constants are not obfuscated.
More obfuscating you can reach with converting the binary String to Bytecode...
> string.dump(myfunc, true):byte(1, -1)
27 76 117 97 84 0 25 147 13 10 26 10 4 8 8 120 86 0 0 0 0 0 0 0 00 0 0 40 119 64 1 128 129 129 0 0 2 133 11 0 0 0 131 128 0 0 68 0 21 71 0 1 0 71 0 1 0 130 4 134 112 114 105 110 116 4 139 84 101 115 116 115 116 114 105 110 103 129 0 0 0 128 128 128 128 128
...and for converting back later lets put it into a table...
> byte_code_tab = {string.dump(myfunc, true):byte(1, -1)}
> table.concat(byte_code_tab,',')
27,76,117,97,84,0,25,147,13,10,26,10,4,8,8,120,86,0,0,0,0,0,0,0,0,0,0,0,40,119,64,1,128,129,129,0,0,2,133,11,0,0,0,131,128,0,0,68,0,2,1,71,0,1,0,71,0,1,0,130,4,134,112,114,105,110,116,4,139,84,101,115,116,115,116,114,105,110,103,129,0,0,0,128,128,128,128,128
...now a function is needed to get it back...
> bytes_dec = function(tab) local txt = '' for k, v in pairs(tab) do txt = txt .. tostring(v):char() end return txt end
> bytes_dec(byte_code_tab)
uaT�
�
xV(w#����
��DGG��print�Teststring������
> load(bytes_dec(byte_code_tab))()
Teststring
EDIT
To show how it work with a single Lua file that returning a table with a __call metamethod check out this...
-- obfsc.lua
return setmetatable({27,76,117,97,84,0,25,147,13,10,26,10,4,8,8,120,86,0,0,0,0,0,0,0,0,0,0,0,40,119,64,1,128,129,129,0,0,2,133,11,0,0,0,131,128,0,0,68,0,2,1,71,0,1,0,71,0,1,0,130,4,134,112,114,105,110,116,4,139,84,101,115,116,115,116,114,105,110,103,129,0,0,0,128,128,128,128,128},
{__call = function(self, ...)
local txt = ''
for k, v in pairs(self) do
txt = txt .. tostring(v):char()
end
return load(txt)()
end})
...the bytes_dec function is stored in the __call metamethod...
$ /usr/local/bin/lua
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
> require('obfsc')
table: 0x565d3650 ./obfsc.lua
> require('obfsc')()
Teststring
...and do also the load()
But it is up to you where you store: bytes_dec()
Another nice method is ROT.
Its very simple and also old but good enough for de/obfuscating.
An Impression...
$ /bin/lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> rot=require('rot')
> -- Lets rotate the Banner
> print(rot('Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio'))
5!`unqnu``/092)'(4`hi`qyytmrpqr`
5!n/2'l`m)/ 51
> -- Now read source of rot.lua into rot_src and print it
> rot_src = io.open('rot.lua'):read('*a')
> print(rot_src)
-- rot.lua
local rotator = function(...)
local args, rot, c = {...}, {}, ''
for i = 1, 63 do rot[c.char(i)] = c.char(i + 64) end
for i = 64, 127 do rot[c.char(i)] = c.char(i - 64) end
return args[1]:gsub('.', rot)
end
return rotator
> -- Obfuscate the source and print it
> rot_obfsc = rot(rot_src)
> print(rot_obfsc)
mm`2/4n,5!J,/#!,`2/4!4/2`}`&5.#4)/.hnnniJ,/#!,`!2'3l`2/4l`#`}`;nnn=l`;=l`ggJJ&/2`)`}`ql`vs`$/`2/4#(!2h)i`}`#n#(!2h)`k`vti`%.$J&/2`)`}`vtl`qrw`$/`2/4#(!2h)i`}`#n#(!2h)`m`vti`%.$JJ2%452.`!2'3z'35"hgngl`2/4iJ%.$JJ2%452.`2/4!4/2J
> -- Deobfuscate and print on the fly
> print(rot(rot_obfsc))
-- rot.lua
local rotator = function(...)
local args, rot, c = {...}, {}, ''
for i = 1, 63 do rot[c.char(i)] = c.char(i + 64) end
for i = 64, 127 do rot[c.char(i)] = c.char(i - 64) end
return args[1]:gsub('.', rot)
end
return rotator
236
I need to convert some musical note inputs representing a chord to numbers above it's root note 0 using Lua.
So from the midi data we get the notes of a C13 Chord
input: C, E, G, A#, D, F, A
as the root note 0 is C we start on the C note,
below we have 2 octaves of a piano keyboard, 12 notes on each where chords are played
0C 1C# 2D 3D# 4E 5F 6F# 7G 8G# 9A 10A# 11B 12C 13C# 14D 15D# 16E 17F 18F# 19G 20G# 21A 22A# 23B
so C is the root note 0
D,F,A are played on the next octave
result: 0,4,7,10,14,17,21
so if we have a D chord
input: D,F#,A
D is the root note 0
all notes played on the first octave
0D 1D# 2E 3F 4F# 5G 6G# 7A 8A# 9B 10C 11C# 12D 13D# 14E 15F 16F# 17G 18G# 19A 20A# 21B 22C 23C#
result: 0,4,7
G#m7#9 Chord
input: G#,B,D#,F#,B
0G# 1A 2A# 3B 4C 5C# 6D 7D# 8E 9F 10F# 11G 12G# 13A 14A# 15B 16C 17C# 18D 19D# 20E 21F 22F# 23G
result: 0,3,7,10,15
Something like this may work:
local function notes2nums(input)
local map = {A = 9, ["A#"] = 10, B = 11, C = 0, ["C#"] = 1, D = 2, ["D#"] = 3, E = 4, F = 5, ["F#"] = 6, G = 7, ["G#"] = 8}
local base, prev
return (input:gsub("([^,]+)", function(note)
local num = map[note] or error(("Unexpected note value '%s'"):format(note))
base = base or num
num = num - base
if prev and num < prev then num = num + 12 end
prev = num
return tostring(num)
end))
end
print(notes2nums("D,F#,A"))
print(notes2nums("C,E,G,A#,D,F,A"))
print(notes2nums("G#,B,D#,F#,B"))
This prints:
0,4,7
0,4,7,10,14,17,21
0,3,7,10,15
Consider the following matrix in MATLAB:
01 02 03 04 05 06 07
08 09 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
I have to generate directional variograms for such 7 x 7 windows(moving) of an image. I will use nlfilter for the process but for developing the function to calculate variograms I am not able to decide how to select elements in the window. For example when I consider the central value 25, in EW direction I have to consider only 25, 26, 27 and 28; in NE direction I have to consider only 25, 19, 13 and 07 when the lag chosen is 1. Is there any standard command to do so?
You can also do it like this:
A = eye(5);
v = A(1:size(A,1)+1:end);
resulting in
v = [1 1 1 1 1]
You can write a function to get these elements yourself easily:
A = [01 02 03 04 05 06 07
08 09 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];
c = (size(A)+1)/2;
EW = A(c(1),c(2):end)
NE = diag(A(c(1):-1:1,c(2):end))
Just write this code in a function (preferably an m-file), perform your operation and pass the result back.
The diag function returns the diagonal elements of a matrix (or returns a diagonal matrix when passed a vector).
This is Generic matrix solution (not for MATLAB)
suppose matrix AxB =
[01 AA 03 04 05 06 07
08 09 AA 11 12 13 AA
AA 16 17 AA 19 AA 21
22 AA 24 25 AA 27 28
AA 30 AA 32 33 34 35
36 AA 38 AA 40 41 42
43 44 AA 46 AA 48 49];
in this matrix we want to search continuously 3 times appearence of AA diagonally.
Solution:-
step 1
for whole matrix we have to create 4 seperate for loops to search the appearence of AA continuously 3 times
i am adding method through which a user can search all loop and can find the item.
local function check_win( matrx_table)
local counter = 1
local term = "AA"
local D = 1
-- for horizontal check for win---
for i = 1, no_rows, 1 do
for j= 1, no_Columns, 1 do
if((j+1) <= no_Columns) then
if(table_mXn[i][j] == term and table_mXn[i][j+1] == term)then
counter = counter + 1;
else
counter = 1
end
if(counter == 3)then
return counter
end
end
end
end
counter = 1
-- for vertical check for win--
for i = 1, no_Columns, 1 do
for j= no_rows, 1, -1 do
if((j-1) >= 1) then
if(table_mXn[j][i] == term and table_mXn[j-1][i] == term)then
counter = counter + 1;
else
counter = 1
end
if(counter == 3)then
return counter
end
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 1--
for m = 1, no_rows, 1 do
D = 1
for i =m, no_rows,1 do
if(i+1 <= no_rows and D+1 <= no_Columns)then
if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D + 1
end
end
end
counter = 1
D = 1
-- for diagonol right half check for win in figure loop 2--
for m = 1, no_rows, 1 do
D = m
for i =1, no_rows,1 do
if(i+1 <= no_rows and D+1 <= no_Columns)then
if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D + 1
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 3--
for m = 1, no_rows, 1 do
D = no_Columns
for i =m, no_rows,1 do
if(i+1 <= no_rows and D-1 >= 1)then
if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D - 1
end
end
end
counter = 1
D = 1
-- for diagonol left half check for win in figure loop 4--
for m = no_Columns, 1, -1 do
D = m
for i =1, no_rows,1 do
if(i+1 <= no_rows and D-1 >= 1)then
if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
counter = counter + 1;
print("hhhhh")
else
counter = 1
end
if(counter == 3)then
return counter
end
D = D - 1
end
end
end
end
now you can call this method any where in class and can check in that matrix the searchable item is available or not in repeatedly order Horizontally, Vertically and diagonally.