Handle large numbers with precision in Redis Lua - lua

I need to handle large numbers in Lua which goes with Redis. Normally you would do that like:
require"bc"
bc.mul(...)
bc.mod(...)
etc.
But unfortunately Redis Lua doesn't support "require". The only approach I've found is inserting a large numbers library written in lua itself directly into the script.
The only such library I could get my hands on: oss.digirati.com.br/luabignum/index.htm
I can strip the library for the purposes of a concrete script but it still remains huge. Is there any way to handle large numbers in Redis Lua more efficiently?
UPDATE1: What if I save the whole library into a key and then access it like:
local BigNumLib = KEYS[1];
BigNumLib.BigNum.mul(KEYS[2],KEYS[3]);
I'm not sure of the syntax and perhaps I'll need to enclose all the library in a lua table {}.

I'm not familiar with how Redis handles Lua code, but why is inserting the library itself into your code is a problem? you should be able to do something like this:
local bc = function()
-- insert the code of BigNum.lua here
return BigNum
end
bc.mul(....)
The code has probably been written before Lua 5.1, so I don't know if there are any compatibility issues, but this should at least give you a start.

Related

What can i get from the lua bytecode retrivied via string.dump()?

I’m using lua + luajit 2.0.4 and I’m wondering - Is it possible to restore the original parts of the code from the dumps of lua functions?
function a(l)
if l > 3 then
print(l*l)
end
end
local b = string.dump(a)
In this example, I am doing the string.dump of the 'a' function, and here I come to the questions like:
Is it possible to write this dump into a .txt file?
Is it possible to get the original names of functions, variables, and upvalues?
Is it possible to get strings, numbers, tables?
Is it possible to restore it to the full code, and if not, is it possible to get a disassembled listing?
"Yes" to all questions with a couple of caveats. For (1), make sure that "b" is used as part of the "mode" parameter in io.open on Windows, as the output of string.dump will have some binary content. For (2), it's only true when string.dump is used without the strip option, which was added in LuaJIT:
string.dump(f [,strip])
An extra argument has been added to string.dump(). If set to true,
'stripped' bytecode without debug information is generated. This
speeds up later bytecode loading and reduces memory usage.
For (4), I found this document to be very useful: http://files.catwell.info/misc/mirror/lua-5.2-bytecode-vm-dirk-laurie/lua52vm.html (it's for Lua 5.2, but most of the content applies to LuaJIT as well); it also include a section on the difference between full and stripped bytecode that may answer some of your questions.

How to bring debugging information when encryption then lua code use luac

I wrote the following code in the file "orgin.lua"
if test==nil then
print(aa["bb"]["cc"]) -- to produce a crash
end
print(1120)
when it crash ,it will generate the following information:
lua: origin.lua:3: attempt to index global 'aa' (a nil value)
In order to prevent decompilation and make sure the code is safe,I use the following command to convert my code:
luac -o -s test.lua origin.lua
I know the argument -s is strip debug information, then it do not show the number of rows when crash:
lua: ?:0: attempt to index global 'aa' (a nil value)
but how to bring debugging information when encryption then lua code use luac?Is there any solution?
There is no way to do this built into Lua, but there are some work-arounds.
If you only need line numbers, then one option is to leave the line numbers in the chunk. Line numbers are not that useful for reverse engineering (unluac currently doesn't use them at all), so it shouldn't affect security. Lua doesn't provide an option for this, but it is easy to modify Lua to leave them in when stripping. From ldump.c
n = (D->strip) ? 0 : f->sizelineinfo;
can be changed to
n = f->sizelineinfo;
(Disclaimer: untested)
A more complicated option would be to modify the Lua runtime to output the virtual machine program counter instead of the line number, and also output information describing the location of the current function in the chunk (e.g. top level, first function, second function nested in third function, etc). Then the line number could be looked up by the developer in a non-stripped version of the chunk. (Here is a reference to someone using this approach on lua-l -- no source code was provided, though.)
Note that preventing decompilation is not true security. It may help against casual attacks, but Lua bytecode is not hard to read.
luac does not encrypt the output. It compiles your Lua source code to bytecode, that's all. The code is neither encrypted nor does it run any faster, only the loadtime is shorter since the compilation step is not needed.
If you want your code to be encrypted, I suggest to encrypt the bytecode using e.g. AES-256 and then decode it in memory just before handing it to the Lua state. This way the bytecode is encrypted on disk, but decripted in memory.
The overhead is low. We use this technique since years.

Bringing userdata into Lua without C (or how much C do I need to learn to do this)

Hi and thanks in advance. I'm trying to see if there's a way to avoid learning C (which I don't know at all) to turn userdata into a Lua table.
I'm using an application which lets users write addons using Lua scripts. These addons can query the application's underlying data. However the query results are returned as userdata and I need them to be available back in the script.
I haven't been able to find anything in the applications documentation about working with returned datasets. The only description given is that the operation I'm running:
Executes the currently assigned query string and uses the results to construct either a DataSet object if multiple result sets are returned or a DataTable if one or no result sets are returned.
In my case I'll be getting multiple result sets, I'm referring to it as userdata because that's what I get when I call type() on the query result.
I've looked at the Lua documentation which seems like it could be useful but lacking any familiarity with C I have no idea what I'm looking at. The metatable tells me I have access to the __index, __newindex, __tostring, and __gc metamethods. Calling tostring gives me a seven-digit integer which is (I think) completely unrelated to my data. It's possible I'm not even getting the data I want back at all, but I can't figure a way to check that. Online resources either say this can't be done or provide solutions in C which are probably clever, clean, and awesome but I don't understand them.
So I'm looking for some more definitive guidance on whether I can/should do this, if there's a way to do this without C (or just by blindly copying someone else's code), or -- if I need to use C for this -- if we're talking about the 90 minute or the 7 hour Lynda Introduction to C course to get where I need to be?
(and apologies if I've inadvertently violated any community norms with this question...it's my first time posting)

how do i simulate signed 32bit integer in lua

In Java, when I do following left shift operation, I get a negative result due to integer / long overflow:
0xAAAAAAAA << 7 gives me -183251938048
But, In Lua since everything is a Lua number which is 52 bit float; I am not able to trigger overflow upon left shift:
bit_lshift(0xAAAAAAAA,7) gives me 1431655680
How do I simulate 32bit signed integer in Lua??
You write some C functions that handle this and then export them to Lua.
Though generally, Lua code shouldn't be touching things this low-level.
You are looking for bit manipulating libraries in Lua. One such library is bitop from the author of LuaJIT, which directly contains it without the need for installation. You can also install it in standard Lua.
Another library is the bit32 library, which is contained in Lua 5.2.
Both libraries let you manipulate 32-bit numbers. For example with bitop:
local bit = require 'bit
print(bit.lshift(0xAAAAAAAA, 7)) --> 1431655680
I do not know how you got the negative number, since 1431655680 is what I get by doing (0xAAAAAAAA<<7)&0xFFFFFFFF in C (and also doing that in a "programming calculator").
I hope I'm not seen as trolling for saying this, but the best way to simulate Java from Lua would be to use Java from Lua.
If you need to emulate Java, chances are that your Lua is already embedded in it. Just expose Java's binary operations to the Lua program, so it can use them.

How to dissect a field in an already dissected package in Wireshark using Lua?

I have a protocol that is built on UDP and that is partly dissected by a third party dll in Wireshark. I now want to create a custom dissector to apply to the remaining field "data".
Is it possible to do so and should I use a dissector, post-dissector or a listener or a combination of them to accomplish this? Or do I have to re-write the third party dissector to one that calls my dissector on the remaining data?
As John Zwinck mentioned, you probably do want something like a chained dissector, which you can manage fairly straightforwardly in either Lua or C. To that end, you certainly do want to implement your logic as a dissector. In Lua, something like this:
do
--TODO set up your extra "data" field
local tcp_table = DissectorTable.get("tcp.port")
local third_party_dissector tcp_table:get_dissector(PROTO_PORT)
function your_protocol.dissector(tvb, pinfo, tree)
--call the third party dissector
third_party_dissector:call(tvb, pinfo, tree)
--TODO do what you need with the data
end
--take over the port your protocol runs over
tcp_table_add(PROTO_PORT, your_protocol)
end
Keep the API on hand, but keep in mind also that Lua dissectors in Wireshark are really just for prototyping; they are less efficient than equivalent C-based dissectors, and the API tends to lag several versions behind the C dissection API.
When I wanted to do something like this I found it surprisingly complex and unpleasant (relative to Lua dissector development in general). There is some mention of "chained dissectors" here: http://wiki.wireshark.org/Lua/Dissectors . From what I read (I never did get mine working, but I didn't try too hard), it seems easier to make chained dissectors in C than in Lua. Still, try following the example on that page, which thankfully has enough comments to make it pretty clear.

Resources