Bin Version 5.0.0!
Completely rewritten. Removed the garbage and added some new features. Checkout BinRewrite.md to view changes
This commit is contained in:
parent
7094656723
commit
37f50e007d
208
BinRewrite.md
208
BinRewrite.md
@ -1,121 +1,121 @@
|
|||||||
# Bin Rewrite Progress!
|
# Bin Rewrite Progress!
|
||||||
My vision for the bin library is to provide great and consistant support for binary minipulation... The old version was not consistant with how I wanted things to work. Great things to come!
|
My vision for the bin library is to provide great and consistant support for binary minipulation... The old version was not consistant with how I wanted things to work. Great things to come!
|
||||||
**Note: A lot breaks (Almost everything)**
|
**Note: A lot breaks**
|
||||||
Progress: [=====- - - - - 25% - - - - - - - - -]
|
Progress: [==========90%======== -] All that is left is the virtual file system stuff! Everything else is all good!
|
||||||
|
A lot of useless and unneeded functions have been removed while adding some more useful ones!
|
||||||
### List of new methods
|
### List of new methods
|
||||||
- [x] bin.newFromBase64(data)
|
- [x] bin.newFromBase64(data) -- returns a bin object from base64 data
|
||||||
- [x] bin.newFromBase91(data)
|
- [x] bin.newFromBase91(data) -- returns a bin object from base91 data
|
||||||
- [x] bin.newFromHex(data)
|
- [x] bin.newFromHex(data) -- returns a bin object from hex data
|
||||||
- [x] bin:read(n) -- works just like normal read on a file for both stream/bin files
|
- [x] bin:read(n) -- works just like normal read on a file for both stream/bin files
|
||||||
- [x] bin:sub(a,b) -- works like string.sub() but for bin objects
|
- [x] bin:sub(a,b) -- works like string.sub() but for bin objects
|
||||||
- [x] bin:seekSet(n) -- sets the seek position on the file
|
- [x] bin:seekSet(n) -- sets the seek position on the file
|
||||||
- [x] bin.toBase64(s)
|
- [x] bin.toBase64(s) -- returns a base64 encoded string from the input string
|
||||||
- [x] bin.fromBase64(s)
|
- [x] bin.fromBase64(s) -- converts the base64 data back into its original string
|
||||||
- [x] bin.toBase91(s)
|
- [x] bin.toBase91(s) -- returns a base91 encoded string from the input string
|
||||||
- [x] bin.fromBase91(s)
|
- [x] bin.fromBase91(s) -- converts the base91 data back into its original string
|
||||||
- [x] bin.getnumber(num,len,fmt,func)
|
- [x] bin.getnumber(num,len,fmt,func) -- turns num into a binary string, len is what to fit it into, 4 bytes, 5+ bytes. fmt is either %e for little endian or %E for big endian defualts to %E
|
||||||
- [x] New infinabits! Works like bits, but supports numbers up to infinity
|
- [x] New infinabits! Works like bits, but supports numbers up to infinity
|
||||||
- [x] bin.registerBlock(t,funcG,funcA) -- allows you to add custom block commands, I implemented the table block command using this feature
|
- [x] bin.registerBlock(t,funcG,funcA) -- allows you to add custom block commands, I implemented the table block command using this feature, look at extrablocks.lua in the bin folder
|
||||||
|
- [x] binobj:getMD5Hash() -- returns the md5 hash of the bin file
|
||||||
|
- [x] bin.getBitsInNum(x) -- returns the number of bits needed to store a number
|
||||||
|
|
||||||
### List of converted methods and their status
|
### List of converted methods and their status
|
||||||
- [x] log(data,name,fmt)
|
- [x] log(data,name,fmt)
|
||||||
- [ ] bin.getLuaVersion()
|
~~bin.getLuaVersion()~~
|
||||||
- [x] bin.load(filename,s,r)
|
- [x] bin.load(filename,s,r) -- loads a file into memory as a virtual file
|
||||||
- [x] bin.new(string data) -- Does not accept b64 or hex data anymore! Use new methods for that ^^^
|
- [x] bin.new(data) -- Does not accept b64 or hex data anymore! Use new methods for that ^^^ -- creats a new virtual file
|
||||||
- [x] bin.stream(file,lock)
|
- [x] bin.stream(file,lock) -- opens a file in updating mode. Works just like a binobj
|
||||||
- [x] bin.newTempFile(data)
|
- [x] bin.newTempFile(data) -- creates a streamed file usine lua's io.tmpfile() so its removed when the program is done
|
||||||
- [x] bin:getSize(fmt) -- gets the size of the string fmt is how you want to format it. left empte returns number of bites as a lua number supports lua formats! And also I added the %b format for base64
|
- [x] bin:getSize(fmt) -- gets the size of the string fmt is how you want to format it. left empte returns number of bites as a lua number supports lua formats! And also I added the %b format for base64
|
||||||
- [x] bin:getData(fmt) -- returns the data of the object as a string, supports %x(hex), %X(HEX) and %b(base64)
|
- [x] binobj:getData(a,b,fmt) -- returns the data of the object as a string, supports %x(hex), %X(HEX) and %b(base64)
|
||||||
- [x] bits.new(n)
|
- [x] bits.new(n) -- creats a bits objects. This library is all about numbers and manipulation at the bit level
|
||||||
- [ ] bin.newVFS()
|
- [ ] bin.newVFS() -- soon
|
||||||
- [x] bin:tackE(data) -- tacks data onto the end of a file
|
- [x] bin:tackE(data) -- tacks data onto the end of a file
|
||||||
- [x] bin:tofile(path)
|
- [x] binobj:tofile(path) -- binobj to a file on the disk. Does not work with stream files! Might allow it to rename stream files at a future point
|
||||||
- [ ] bin.loadVFS(path)
|
- [ ] bin.loadVFS(path) -- soon
|
||||||
- [x] bin:newDataBuffer(size,fill)
|
- [x] bin:newDataBuffer(size,fill) -- creates a buffer object. Buffers allow fast and easy byte manipulation
|
||||||
- [ ] bin.bufferToBin(b)
|
~~bin.bufferToBin(b)~~
|
||||||
- [ ] bin.binToBuffer(b)
|
~~bin.binToBuffer(b)~~
|
||||||
- [ ] bin:getDataBuffer(a,b)
|
~~bin:getDataBuffer(a,b)~~
|
||||||
- [ ] bin.newNamedBlock(indexSize)
|
~~bin.newNamedBlock(indexSize)~~
|
||||||
- [ ] bin.newStreamedNamedBlock(indexSize,path)
|
~~bin.newStreamedNamedBlock(indexSize,path)~~
|
||||||
- [ ] bin.loadNamedBlock(path)
|
~~bin.loadNamedBlock(path)~~
|
||||||
- [ ] bin.namedBlockManager(arg)
|
~~bin.namedBlockManager(arg)~~
|
||||||
- [ ] bin.randomName(n,ext)
|
- [x] bin.randomName(n,ext) -- returns a random filesystem safe string, can add an extension to be created with it as well
|
||||||
- [ ] ~~bin.NumtoHEX(n)~~
|
~~bin.NumtoHEX(n)~~
|
||||||
- [ ] ~~bin.HEXtoBin(s)~~
|
~~bin.HEXtoBin(s)~~
|
||||||
- [ ] ~~bin.HEXtoStr(s)~~
|
~~bin.HEXtoStr(s)~~
|
||||||
- [x] bin.tohex(s)
|
- [x] bin.tohex(s) -- converts a string into hex
|
||||||
- [x] bin.fromhex(s)
|
- [x] bin.fromhex(s) -- takes hex data and turns it into a string
|
||||||
- [x] bin.endianflop(data)
|
- [x] bin.endianflop(data) -- swaps between big/little endian
|
||||||
- [x] bin.getVersion()
|
- [x] bin.getVersion() -- returns the libraries version as a string
|
||||||
- [ ] bin.escapeStr(str)
|
~~bin.escapeStr(str)~~
|
||||||
- [ ] ~~bin.ToStr(tab)~~
|
~~bin.ToStr(tab)~~
|
||||||
- [ ] ~~bin.packLLIB(name,tab,ext)~~
|
~~bin.packLLIB(name,tab,ext)~~
|
||||||
- [ ] ~~bin.unpackLLIB(name,exe,todir,over,ext)~~
|
~~bin.unpackLLIB(name,exe,todir,over,ext)~~
|
||||||
- [ ] ~~bin.fileExist(path)~~ NOW io.fileExists(path)
|
~~bin.fileExist(path)~~ NOW io.fileExists(path)
|
||||||
- [ ] ~~bin.closeto(a,b,v)~~
|
~~bin.closeto(a,b,v)~~
|
||||||
- [ ] bin.textToBinary(txt)
|
~~bin.textToBinary(txt)~~
|
||||||
- [ ] bin.decodeBits(bindata)
|
~~bin.decodeBits(bindata)~~
|
||||||
- [ ] bin.trimNul(s)
|
- [x] bin.trimNul(s) -- removes the nul character from the end of a string
|
||||||
- [ ] bin.getIndexSize(tab)
|
~~bin.getIndexSize(tab)~~
|
||||||
- [x] bits.numToBytes(num,occ)
|
- [x] bits.numToBytes(n,fit,func) -- converts a number into a string, helper function for bin.getnumber(num,len,fmt,func), allows for more control
|
||||||
- [ ] binobj:tofile(filename)
|
~~binobj:clone()~~
|
||||||
- [ ] binobj:clone()
|
~~binobj:compare(other binobj,diff)~~
|
||||||
- [ ] binobj:compare(other binobj,diff)
|
- [x] binobj:sub(a,b) -- works like string.sub, but for binobjs
|
||||||
- [ ] binobj:sub(a,b)
|
|
||||||
- [x] binobj:tonumber(a,b)
|
- [x] binobj:tonumber(a,b)
|
||||||
- [ ] binobj:getbyte(n)
|
~~binobj:getbyte(n)~~
|
||||||
- [ ] binobj:tobits(i)
|
~~binobj:tobits(i)~~
|
||||||
- [ ] binobj:getHEX(a,b)
|
~~binobj:getHEX(a,b)~~
|
||||||
- [ ] binobj:scan(s,n,f)
|
~~binobj:scan(s,n,f)~~
|
||||||
- [ ] binobj:streamData(a,b)
|
~~binobj:streamData(a,b)~~
|
||||||
- [ ] binobj:streamread(a,b)
|
~~binobj:streamread(a,b)~~
|
||||||
- [x] binobj:canStreamWrite()
|
- [x] binobj:canStreamWrite() -- returns true if a a binobj can be streamed written to
|
||||||
- [x] bitobj:conv(n)
|
- [x] bitobj:conv(n)
|
||||||
- [x] bitobj:tobytes()
|
- [x] bitobj:tobytes()
|
||||||
- [x] bitobj:tonumber()
|
- [x] bitobj:tonumber()
|
||||||
- [x] bitobj:isover()
|
- [x] bitobj:isover()
|
||||||
- [x] bitobj:getBin()
|
- [x] binobj:getHash(n) -- returns a fast, but insecure hash
|
||||||
- [ ] binobj:getHash(n)
|
~~blockReader:getBlock(name)~~
|
||||||
- [ ] binobj:getData()
|
~~binobj:setEndOfFile(n)~~
|
||||||
- [ ] blockReader:getBlock(name)
|
~~binobj:reverse()~~
|
||||||
- [ ] binobj:setEndOfFile(n)
|
- [x] binobj:flipbits() -- filps the bits in the binobj
|
||||||
- [ ] binobj:reverse()
|
~~binobj:segment(a,b)~~
|
||||||
- [ ] binobj:flipbits()
|
~~binobj:insert(a,i)~~
|
||||||
- [ ] binobj:segment(a,b)
|
~~binobj:parseN(n)~~
|
||||||
- [ ] binobj:insert(a,i)
|
~~binobj:getlength()~~ -- now is getSize()
|
||||||
- [ ] binobj:parseN(n)
|
~~binobj:shift(n)~~
|
||||||
- [ ] binobj:getlength()
|
~~binobj:delete(a,b)~~
|
||||||
- [ ] binobj:shift(n)
|
- [x] binobj:encrypt(seed) -- encrypts a file
|
||||||
- [ ] binobj:delete(a,b)
|
- [x] binobj:decrypt(seed) -- decrypts a file
|
||||||
- [ ] binobj:encrypt(seed)
|
~~binobj:shuffle()~~
|
||||||
- [ ] binobj:decrypt(seed)
|
~~binobj:mutate(a,i)~~
|
||||||
- [ ] binobj:shuffle()
|
~~binobj:merge(o)~~ -- binobj:tackE(data) does this so there is no need for merge
|
||||||
- [ ] binobj:mutate(a,i)
|
~~binobj:parseA(n,a,t)~~
|
||||||
- [ ] binobj:merge(o,t)
|
~~binobj:getHEX(a,b)~~
|
||||||
- [ ] binobj:parseA(n,a,t)
|
~~binobj:cryptM()~~
|
||||||
- [ ] binobj:getHEX(a,b)
|
- [x] binobj:addBlock(d,fit,fmt) -- adds a block to the bin obj
|
||||||
- [ ] binobj:cryptM()
|
- [x] binobj:getBlock(t,n) -- gets the block from a binobj
|
||||||
- [x] binobj:addBlock(d,fit,fmt)
|
- [x] binobj:setSeek(n) -- **starts at 1 not 0**
|
||||||
- [x] binobj:getBlock(t,n)
|
~~binobj:morph(a,b,d)~~
|
||||||
- [x] binobj:seek(n)
|
~~binobj:fill(n,d)~~
|
||||||
- [ ] binobj:morph(a,b,d)
|
~~binobj:fillrandom(n)~~
|
||||||
- [ ] binobj:fill(n,d)
|
~~binobj:shiftbits(n)~~
|
||||||
- [ ] binobj:fillrandom(n)
|
~~binobj:shiftbit(n,i)~~
|
||||||
- [ ] binobj:shiftbits(n)
|
~~binobj:streamwrite(d,n)~~
|
||||||
- [ ] binobj:shiftbit(n,i)
|
~~binobj:open()~~
|
||||||
- [ ] binobj:streamwrite(d,n)
|
- [x] binobj:close() -- closes a streamed file
|
||||||
- [ ] ~~binobj:open()~~
|
~~binobj:wipe()~~
|
||||||
- [x] binobj:close()
|
~~binobj:tackB(d)~~
|
||||||
- [ ] binobj:wipe()
|
- [x] binobj:tackE(d,fit,updateseek) -- adds data onto the end of the file
|
||||||
- [ ] binobj:tackB(d)
|
~~binobj:parse(n,f)~~
|
||||||
- [x] binobj:tackE(d,fit,updateseek)
|
~~binobj:flipbit(i)~~
|
||||||
- [ ] binobj:parse(n,f)
|
~~binobj:gsub()~~
|
||||||
- [ ] binobj:flipbit(i)
|
~~blockWriter:addNamedBlock(name,value)~~
|
||||||
- [ ] binobj:gsub()
|
~~bitobj:add(i)~~
|
||||||
- [ ] blockWriter:addNamedBlock(name,value)
|
~~bitobj:sub(i)~~
|
||||||
- [ ] ~~bitobj:add(i)~~
|
~~bitobj:multi(i)~~
|
||||||
- [ ] ~~bitobj:sub(i)~~
|
~~bitobj:div(i)~~
|
||||||
- [ ] ~~bitobj:multi(i)~~
|
- [x] bitobj:flipbits() -- flips the bits of a bitobj
|
||||||
- [ ] ~~bitobj:div(i)~~
|
- [x] bitobj:getBin() -- returns the binobj of the bit data
|
||||||
- [x] bitobj:flipbits()
|
|
||||||
- [x] bitobj:getBin()
|
|
||||||
@ -1,9 +1,10 @@
|
|||||||
# bin
|
# bin
|
||||||
|
|
||||||
The binary manipulation library make file management a simple task.</br>
|
The binary manipulation library make file management a simple task.</br>
|
||||||
Allows for a wide range of features that can be used, the library is a bit of a mess though and isn't at a point that I am happy with yet.</br>
|
Rewrite done for the most part: Checkout BinRewrite.md to view changes
|
||||||
Basic usage:
|
Basic usage:
|
||||||
```lua
|
```lua
|
||||||
|
-- Some changes are needed for this to work
|
||||||
require("bin")
|
require("bin")
|
||||||
print("TEST - 1")
|
print("TEST - 1")
|
||||||
test=bin.new("I am a string in a bin")
|
test=bin.new("I am a string in a bin")
|
||||||
@ -41,7 +42,7 @@ true
|
|||||||
table: 0x001e3f40
|
table: 0x001e3f40
|
||||||
```
|
```
|
||||||
|
|
||||||
# The bin library has all of these features
|
# The bin library **had** all of these features, a lot has been striped use the BinRewrite for info on what stayed!
|
||||||
|
|
||||||
Note: Examples of everything in action wll be made eventually...</br>
|
Note: Examples of everything in action wll be made eventually...</br>
|
||||||
nil = log(data,name,fmt) -- data is the text that you want to log to a file, the name argument only needs to be called with the first log. It tells where to log to. If name is used again it will change the location of the log file.</br>
|
nil = log(data,name,fmt) -- data is the text that you want to log to a file, the name argument only needs to be called with the first log. It tells where to log to. If name is used again it will change the location of the log file.</br>
|
||||||
|
|||||||
@ -1,57 +1,9 @@
|
|||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{1
|
|
||||||
--
|
|
||||||
-- File Name: bignum.lua
|
|
||||||
-- Package Name: BigNum
|
|
||||||
--
|
|
||||||
-- Project: Big Numbers library for Lua
|
|
||||||
-- Mantainers: fmp - Frederico Macedo Pessoa
|
|
||||||
-- msm - Marco Serpa Molinaro
|
|
||||||
--
|
|
||||||
-- History:
|
|
||||||
-- Version Autor Date Notes
|
|
||||||
-- 1.1 fmp/msm 12/11/2004 Some bug fixes (thanks Isaac Gouy)
|
|
||||||
-- alfa fmp/msm 03/22/2003 Start of Development
|
|
||||||
-- beta fmp/msm 07/11/2003 Release
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Big numbers manipulation library for Lua.
|
|
||||||
-- A Big Number is a table with as many numbers as necessary to represent
|
|
||||||
-- its value in base 'RADIX'. It has a field 'len' containing the num-
|
|
||||||
-- ber of such numbers and a field 'signal' that may assume the values
|
|
||||||
-- '+' and '-'.
|
|
||||||
--
|
|
||||||
--$.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%% Constants used in the file %%%%%%%%--{{{1
|
|
||||||
RADIX = 10^7 ;
|
RADIX = 10^7 ;
|
||||||
RADIX_LEN = math.floor( math.log10 ( RADIX ) ) ;
|
RADIX_LEN = math.floor( math.log10 ( RADIX ) ) ;
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%% Start of Code %%%%%%%%--
|
|
||||||
|
|
||||||
BigNum = {} ;
|
BigNum = {} ;
|
||||||
BigNum.mt = {} ;
|
BigNum.mt = {} ;
|
||||||
|
|
||||||
|
|
||||||
--BigNum.new{{{1
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
--
|
|
||||||
-- Function: New
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Creates a new Big Number based on the parameter num.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- num - a string, number or BigNumber.
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- A Big Number, or a nil value if an error occured.
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
|
|
||||||
function BigNum.new( num ) --{{{2
|
function BigNum.new( num ) --{{{2
|
||||||
local bignum = {} ;
|
local bignum = {} ;
|
||||||
setmetatable( bignum , BigNum.mt ) ;
|
setmetatable( bignum , BigNum.mt ) ;
|
||||||
@ -59,8 +11,6 @@ function BigNum.new( num ) --{{{2
|
|||||||
return bignum ;
|
return bignum ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Functions for metatable %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
--BigNum.mt.sub{{{2
|
|
||||||
function BigNum.mt.sub( num1 , num2 )
|
function BigNum.mt.sub( num1 , num2 )
|
||||||
local temp = BigNum.new() ;
|
local temp = BigNum.new() ;
|
||||||
local bnum1 = BigNum.new( num1 ) ;
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
@ -71,7 +21,6 @@ end
|
|||||||
function BigNum.mt.mod( num1 , num2 )
|
function BigNum.mt.mod( num1 , num2 )
|
||||||
return BigNum.new(num1 - (num1/num2)*num2)
|
return BigNum.new(num1 - (num1/num2)*num2)
|
||||||
end
|
end
|
||||||
--BigNum.mt.add{{{2
|
|
||||||
function BigNum.mt.add( num1 , num2 )
|
function BigNum.mt.add( num1 , num2 )
|
||||||
local temp = BigNum.new() ;
|
local temp = BigNum.new() ;
|
||||||
local bnum1 = BigNum.new( num1 ) ;
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
@ -80,7 +29,6 @@ function BigNum.mt.add( num1 , num2 )
|
|||||||
return temp ;
|
return temp ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.mul{{{2
|
|
||||||
function BigNum.mt.mul( num1 , num2 )
|
function BigNum.mt.mul( num1 , num2 )
|
||||||
local temp = BigNum.new() ;
|
local temp = BigNum.new() ;
|
||||||
local bnum1 = BigNum.new( num1 ) ;
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
@ -89,7 +37,6 @@ function BigNum.mt.mul( num1 , num2 )
|
|||||||
return temp ;
|
return temp ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.div{{{2
|
|
||||||
function BigNum.mt.div( num1 , num2 )
|
function BigNum.mt.div( num1 , num2 )
|
||||||
local bnum1 = {} ;
|
local bnum1 = {} ;
|
||||||
local bnum2 = {} ;
|
local bnum2 = {} ;
|
||||||
@ -101,7 +48,6 @@ function BigNum.mt.div( num1 , num2 )
|
|||||||
return bnum3 , bnum4 ;
|
return bnum3 , bnum4 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.tostring{{{2
|
|
||||||
function BigNum.mt.tostring( bnum )
|
function BigNum.mt.tostring( bnum )
|
||||||
local i = 0 ;
|
local i = 0 ;
|
||||||
local j = 0 ;
|
local j = 0 ;
|
||||||
@ -129,35 +75,30 @@ function BigNum.mt.tostring( bnum )
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.pow{{{2
|
|
||||||
function BigNum.mt.pow( num1 , num2 )
|
function BigNum.mt.pow( num1 , num2 )
|
||||||
local bnum1 = BigNum.new( num1 ) ;
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
local bnum2 = BigNum.new( num2 ) ;
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
return BigNum.pow( bnum1 , bnum2 ) ;
|
return BigNum.pow( bnum1 , bnum2 ) ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.eq{{{2
|
|
||||||
function BigNum.mt.eq( num1 , num2 )
|
function BigNum.mt.eq( num1 , num2 )
|
||||||
local bnum1 = BigNum.new( num1 ) ;
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
local bnum2 = BigNum.new( num2 ) ;
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
return BigNum.eq( bnum1 , bnum2 ) ;
|
return BigNum.eq( bnum1 , bnum2 ) ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.lt{{{2
|
|
||||||
function BigNum.mt.lt( num1 , num2 )
|
function BigNum.mt.lt( num1 , num2 )
|
||||||
local bnum1 = BigNum.new( num1 ) ;
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
local bnum2 = BigNum.new( num2 ) ;
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
return BigNum.lt( bnum1 , bnum2 ) ;
|
return BigNum.lt( bnum1 , bnum2 ) ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.le{{{2
|
|
||||||
function BigNum.mt.le( num1 , num2 )
|
function BigNum.mt.le( num1 , num2 )
|
||||||
local bnum1 = BigNum.new( num1 ) ;
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
local bnum2 = BigNum.new( num2 ) ;
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
return BigNum.le( bnum1 , bnum2 ) ;
|
return BigNum.le( bnum1 , bnum2 ) ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.mt.unm{{{2
|
|
||||||
function BigNum.mt.unm( num )
|
function BigNum.mt.unm( num )
|
||||||
local ret = BigNum.new( num )
|
local ret = BigNum.new( num )
|
||||||
if ret.signal == '+' then
|
if ret.signal == '+' then
|
||||||
@ -168,13 +109,8 @@ function BigNum.mt.unm( num )
|
|||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Metatable Definitions %%%%%%%%%%%%%%%%%%%%--{{{1
|
BigNum.mt.__metatable = "hidden"
|
||||||
|
|
||||||
BigNum.mt.__metatable = "hidden" ; -- answer to getmetatable(aBignum)
|
|
||||||
-- BigNum.mt.__index = "inexistent field" ; -- attempt to acess nil valued field
|
|
||||||
-- BigNum.mt.__newindex = "not available" ; -- attempt to create new field
|
|
||||||
BigNum.mt.__tostring = BigNum.mt.tostring ;
|
BigNum.mt.__tostring = BigNum.mt.tostring ;
|
||||||
-- arithmetics
|
|
||||||
BigNum.mt.__add = BigNum.mt.add ;
|
BigNum.mt.__add = BigNum.mt.add ;
|
||||||
BigNum.mt.__sub = BigNum.mt.sub ;
|
BigNum.mt.__sub = BigNum.mt.sub ;
|
||||||
BigNum.mt.__mul = BigNum.mt.mul ;
|
BigNum.mt.__mul = BigNum.mt.mul ;
|
||||||
@ -182,36 +118,10 @@ BigNum.mt.__div = BigNum.mt.div ;
|
|||||||
BigNum.mt.__pow = BigNum.mt.pow ;
|
BigNum.mt.__pow = BigNum.mt.pow ;
|
||||||
BigNum.mt.__unm = BigNum.mt.unm ;
|
BigNum.mt.__unm = BigNum.mt.unm ;
|
||||||
BigNum.mt.__mod = BigNum.mt.mod ;
|
BigNum.mt.__mod = BigNum.mt.mod ;
|
||||||
-- Comparisons
|
|
||||||
BigNum.mt.__eq = BigNum.mt.eq ;
|
BigNum.mt.__eq = BigNum.mt.eq ;
|
||||||
BigNum.mt.__le = BigNum.mt.le ;
|
BigNum.mt.__le = BigNum.mt.le ;
|
||||||
BigNum.mt.__lt = BigNum.mt.lt ;
|
BigNum.mt.__lt = BigNum.mt.lt ;
|
||||||
--concatenation
|
|
||||||
-- BigNum.me.__concat = ???
|
|
||||||
|
|
||||||
setmetatable( BigNum.mt, { __index = "inexistent field", __newindex = "not available", __metatable="hidden" } ) ;
|
setmetatable( BigNum.mt, { __index = "inexistent field", __newindex = "not available", __metatable="hidden" } ) ;
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Basic Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: ADD
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Adds two Big Numbers.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - Numbers to be added.
|
|
||||||
-- bnum3 - result
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- bnum3 is the result of the sum.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--Funcao BigNum.add{{{2
|
|
||||||
function BigNum.add( bnum1 , bnum2 , bnum3 )
|
function BigNum.add( bnum1 , bnum2 , bnum3 )
|
||||||
local maxlen = 0 ;
|
local maxlen = 0 ;
|
||||||
local i = 0 ;
|
local i = 0 ;
|
||||||
@ -273,34 +183,11 @@ function BigNum.add( bnum1 , bnum2 , bnum3 )
|
|||||||
return 0 ;
|
return 0 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: SUB
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Subtracts two Big Numbers.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - Numbers to be subtracted.
|
|
||||||
-- bnum3 - result
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- bnum3 is the result of the subtraction.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--Funcao BigNum.sub{{{2
|
|
||||||
function BigNum.sub( bnum1 , bnum2 , bnum3 )
|
function BigNum.sub( bnum1 , bnum2 , bnum3 )
|
||||||
local maxlen = 0 ;
|
local maxlen = 0 ;
|
||||||
local i = 0 ;
|
local i = 0 ;
|
||||||
local carry = 0 ;
|
local carry = 0 ;
|
||||||
local old_len = 0 ;
|
local old_len = 0 ;
|
||||||
--Handle the signals
|
|
||||||
|
|
||||||
if bnum1 == nil or bnum2 == nil or bnum3 == nil then
|
if bnum1 == nil or bnum2 == nil or bnum3 == nil then
|
||||||
error("Function BigNum.sub: parameter nil") ;
|
error("Function BigNum.sub: parameter nil") ;
|
||||||
elseif bnum1.signal == '-' and bnum2.signal == '+' then
|
elseif bnum1.signal == '-' and bnum2.signal == '+' then
|
||||||
@ -373,28 +260,6 @@ function BigNum.sub( bnum1 , bnum2 , bnum3 )
|
|||||||
return 0 ;
|
return 0 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: MUL
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Multiplies two Big Numbers.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - Numbers to be multiplied.
|
|
||||||
-- bnum3 - result
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- bnum3 is the result of the multiplication.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.mul{{{2
|
|
||||||
--can't be made in place
|
|
||||||
function BigNum.mul( bnum1 , bnum2 , bnum3 )
|
function BigNum.mul( bnum1 , bnum2 , bnum3 )
|
||||||
local i = 0 ; j = 0 ;
|
local i = 0 ; j = 0 ;
|
||||||
local temp = BigNum.new( ) ;
|
local temp = BigNum.new( ) ;
|
||||||
@ -445,29 +310,6 @@ function BigNum.mul( bnum1 , bnum2 , bnum3 )
|
|||||||
return 0 ;
|
return 0 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: DIV
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Divides bnum1 by bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - Numbers to be divided.
|
|
||||||
-- bnum3 - result
|
|
||||||
-- bnum4 - remainder
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- bnum3 is the result of the division.
|
|
||||||
-- bnum4 is the remainder of the division.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.div{{{2
|
|
||||||
function BigNum.div( bnum1 , bnum2 , bnum3 , bnum4 )
|
function BigNum.div( bnum1 , bnum2 , bnum3 , bnum4 )
|
||||||
local temp = BigNum.new() ;
|
local temp = BigNum.new() ;
|
||||||
local temp2 = BigNum.new() ;
|
local temp2 = BigNum.new() ;
|
||||||
@ -544,25 +386,6 @@ function BigNum.div( bnum1 , bnum2 , bnum3 , bnum4 )
|
|||||||
return 0 ;
|
return 0 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Compound Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: POW / EXP
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Computes a big number which represents the bnum2-th power of bnum1.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1 - base
|
|
||||||
-- bnum2 - expoent
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns a big number which represents the bnum2-th power of bnum1.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.exp{{{2
|
|
||||||
function BigNum.pow( bnum1 , bnum2 )
|
function BigNum.pow( bnum1 , bnum2 )
|
||||||
local n = BigNum.new( bnum2 ) ;
|
local n = BigNum.new( bnum2 ) ;
|
||||||
local y = BigNum.new( 1 ) ;
|
local y = BigNum.new( 1 ) ;
|
||||||
@ -589,22 +412,6 @@ end
|
|||||||
-- Português :
|
-- Português :
|
||||||
BigNum.exp = BigNum.pow
|
BigNum.exp = BigNum.pow
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: GCD / MMC
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Computes the greatest commom divisor of bnum1 and bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - positive numbers
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns a big number witch represents the gcd between bnum1 and bnum2.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.gcd{{{2
|
|
||||||
function BigNum.gcd( bnum1 , bnum2 )
|
function BigNum.gcd( bnum1 , bnum2 )
|
||||||
local a = {} ;
|
local a = {} ;
|
||||||
local b = {} ;
|
local b = {} ;
|
||||||
@ -630,24 +437,6 @@ end
|
|||||||
-- Português:
|
-- Português:
|
||||||
BigNum.mmc = BigNum.gcd
|
BigNum.mmc = BigNum.gcd
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Comparison Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: EQ
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Compares two Big Numbers.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - numbers
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns true if they are equal or false otherwise.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.eq{{{2
|
|
||||||
function BigNum.eq( bnum1 , bnum2 )
|
function BigNum.eq( bnum1 , bnum2 )
|
||||||
if BigNum.compare( bnum1 , bnum2 ) == 0 then
|
if BigNum.compare( bnum1 , bnum2 ) == 0 then
|
||||||
return true ;
|
return true ;
|
||||||
@ -656,22 +445,6 @@ function BigNum.eq( bnum1 , bnum2 )
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: LT
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Verifies if bnum1 is lesser than bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - numbers
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns true if bnum1 is lesser than bnum2 or false otherwise.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.lt{{{2
|
|
||||||
function BigNum.lt( bnum1 , bnum2 )
|
function BigNum.lt( bnum1 , bnum2 )
|
||||||
if BigNum.compare( bnum1 , bnum2 ) == 2 then
|
if BigNum.compare( bnum1 , bnum2 ) == 2 then
|
||||||
return true ;
|
return true ;
|
||||||
@ -680,23 +453,6 @@ function BigNum.lt( bnum1 , bnum2 )
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: LE
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Verifies if bnum1 is lesser or equal than bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - numbers
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns true if bnum1 is lesser or equal than bnum2 or false otherwise.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.le{{{2
|
|
||||||
function BigNum.le( bnum1 , bnum2 )
|
function BigNum.le( bnum1 , bnum2 )
|
||||||
local temp = -1 ;
|
local temp = -1 ;
|
||||||
temp = BigNum.compare( bnum1 , bnum2 )
|
temp = BigNum.compare( bnum1 , bnum2 )
|
||||||
@ -707,24 +463,6 @@ function BigNum.le( bnum1 , bnum2 )
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: Compare Absolute Values
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Compares absolute values of bnum1 and bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - numbers
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 1 - |bnum1| > |bnum2|
|
|
||||||
-- 2 - |bnum1| < |bnum2|
|
|
||||||
-- 0 - |bnum1| = |bnum2|
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.compareAbs{{{2
|
|
||||||
function BigNum.compareAbs( bnum1 , bnum2 )
|
function BigNum.compareAbs( bnum1 , bnum2 )
|
||||||
if bnum1 == nil or bnum2 == nil then
|
if bnum1 == nil or bnum2 == nil then
|
||||||
error("Function compare: parameter nil") ;
|
error("Function compare: parameter nil") ;
|
||||||
@ -745,25 +483,6 @@ function BigNum.compareAbs( bnum1 , bnum2 )
|
|||||||
return 0 ;
|
return 0 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: Compare
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Compares values of bnum1 and bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - numbers
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 1 - |bnum1| > |bnum2|
|
|
||||||
-- 2 - |bnum1| < |bnum2|
|
|
||||||
-- 0 - |bnum1| = |bnum2|
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.compare{{{2
|
|
||||||
function BigNum.compare( bnum1 , bnum2 )
|
function BigNum.compare( bnum1 , bnum2 )
|
||||||
local signal = 0 ;
|
local signal = 0 ;
|
||||||
|
|
||||||
@ -793,9 +512,6 @@ function BigNum.compare( bnum1 , bnum2 )
|
|||||||
return 0 ;
|
return 0 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Low level Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
--BigNum.copy{{{2
|
|
||||||
function BigNum.copy( bnum1 , bnum2 )
|
function BigNum.copy( bnum1 , bnum2 )
|
||||||
if bnum1 ~= nil and bnum2 ~= nil then
|
if bnum1 ~= nil and bnum2 ~= nil then
|
||||||
local i ;
|
local i ;
|
||||||
@ -808,25 +524,6 @@ function BigNum.copy( bnum1 , bnum2 )
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: Change
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Changes the value of a BigNum.
|
|
||||||
-- This function is called by BigNum.new.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- bnum1, bnum2 - numbers
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 1 - |bnum1| > |bnum2|
|
|
||||||
-- 2 - |bnum1| < |bnum2|
|
|
||||||
-- 0 - |bnum1| = |bnum2|
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.change{{{2
|
|
||||||
function BigNum.change( bnum1 , num )
|
function BigNum.change( bnum1 , num )
|
||||||
local j = 0 ;
|
local j = 0 ;
|
||||||
local len = 0 ;
|
local len = 0 ;
|
||||||
@ -933,9 +630,6 @@ function BigNum.change( bnum1 , num )
|
|||||||
return 0 ;
|
return 0 ;
|
||||||
end
|
end
|
||||||
|
|
||||||
--BigNum.put{{{2
|
|
||||||
--Places int in the position pos of bignum, fills before with zeroes and
|
|
||||||
--after with nil.
|
|
||||||
function BigNum.put( bnum , int , pos )
|
function BigNum.put( bnum , int , pos )
|
||||||
if bnum == nil then
|
if bnum == nil then
|
||||||
error("Function BigNum.put: parameter nil") ;
|
error("Function BigNum.put: parameter nil") ;
|
||||||
227
bin/BigRat.lua
Normal file
227
bin/BigRat.lua
Normal file
@ -0,0 +1,227 @@
|
|||||||
|
require( "BigNum" ) ;
|
||||||
|
|
||||||
|
BigRat = {} ;
|
||||||
|
BigRat.mt = {} ;
|
||||||
|
function BigRat.new( num1 , num2 ) --{{{2
|
||||||
|
local bigrat = {} ;
|
||||||
|
local f ;
|
||||||
|
setmetatable(bigrat, BigRat.mt) ;
|
||||||
|
if type( num1 ) == "table" then
|
||||||
|
if num1.num ~= nil and num1.den ~= nil then
|
||||||
|
bigrat.num = BigNum.new( num1.num ) ;
|
||||||
|
bigrat.den = BigNum.new( num1.den ) ;
|
||||||
|
else
|
||||||
|
bigrat.num = BigNum.new( num1 ) ;
|
||||||
|
bigrat.den = BigNum.new( "1" ) ;
|
||||||
|
end
|
||||||
|
elseif num1 ~= nil then
|
||||||
|
if num2 == nil then
|
||||||
|
bigrat.den = BigNum.new( "1" ) ;
|
||||||
|
else
|
||||||
|
bigrat.den = BigNum.new( num2 ) ;
|
||||||
|
end
|
||||||
|
bigrat.num = BigNum.new( num1 ) ;
|
||||||
|
else
|
||||||
|
bigrat.den = BigNum.new( ) ;
|
||||||
|
bigrat.num = BigNum.new( ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Update the signals
|
||||||
|
if bigrat.den.signal == "-" then
|
||||||
|
if bigrat.num.signal == "-" then
|
||||||
|
bigrat.num.signal = "+" ;
|
||||||
|
else
|
||||||
|
bigrat.num.signal = "-" ;
|
||||||
|
end
|
||||||
|
bigrat.den.signal = "+" ;
|
||||||
|
end
|
||||||
|
|
||||||
|
return bigrat ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.sub( num1 , num2 )
|
||||||
|
local temp = BigRat.new() ;
|
||||||
|
local brat1 = BigRat.new( num1 ) ;
|
||||||
|
local brat2 = BigRat.new( num2 ) ;
|
||||||
|
BigRat.sub( brat1 , brat2 , temp ) ;
|
||||||
|
return temp ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.add( num1 , num2 )
|
||||||
|
local temp = BigRat.new() ;
|
||||||
|
local brat1 = BigRat.new( num1 ) ;
|
||||||
|
local brat2 = BigRat.new( num2 ) ;
|
||||||
|
BigRat.add( brat1 , brat2 , temp ) ;
|
||||||
|
return temp ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.mul( num1 , num2 )
|
||||||
|
local temp = BigRat.new() ;
|
||||||
|
local brat1 = BigRat.new( num1 ) ;
|
||||||
|
local brat2 = BigRat.new( num2 ) ;
|
||||||
|
BigRat.mul( brat1 , brat2 , temp ) ;
|
||||||
|
return temp ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.div( num1 , num2 )
|
||||||
|
local brat1 = BigRat.new( num1 ) ;
|
||||||
|
local brat2 = BigRat.new( num2 ) ;
|
||||||
|
local brat3 = BigRat.new() ;
|
||||||
|
local brat4 = BigRat.new() ;
|
||||||
|
BigRat.div( brat1 , brat2 , brat3 , brat4 ) ;
|
||||||
|
return brat3 , brat4 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.tostring( brat )
|
||||||
|
BigRat.simplify( brat ) ;
|
||||||
|
return BigNum.mt.tostring( brat.num ) .. " / " .. BigNum.mt.tostring( brat.den ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.pow ( num1 , num2 )
|
||||||
|
local brat1 = BigRat.new( num1 ) ;
|
||||||
|
local brat2 = BigRat.new( num2 ) ;
|
||||||
|
return BigRat.pow( brat1 , brat2 )
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.eq ( num1 , num2 )
|
||||||
|
return BigRat.eq( num1 , num2 )
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.lt ( num1 , num2 )
|
||||||
|
return BigRat.lt( num1 , num2 )
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.le ( num1 , num2 )
|
||||||
|
return BigRat.le( num1 , num2 )
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mt.unm ( num )
|
||||||
|
local ret = BigRat.new( num )
|
||||||
|
if ret.num.signal == '-' then
|
||||||
|
ret.num.signal = '+'
|
||||||
|
else
|
||||||
|
ret.num.signal = '-'
|
||||||
|
end
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
BigRat.mt.__metatable = "hidden"
|
||||||
|
BigRat.mt.__tostring = BigRat.mt.tostring
|
||||||
|
BigRat.mt.__add = BigRat.mt.add
|
||||||
|
BigRat.mt.__sub = BigRat.mt.sub
|
||||||
|
BigRat.mt.__mul = BigRat.mt.mul
|
||||||
|
BigRat.mt.__div = BigRat.mt.div
|
||||||
|
BigRat.mt.__pow = BigRat.mt.pow
|
||||||
|
BigRat.mt.__unm = BigRat.mt.unm
|
||||||
|
BigRat.mt.__eq = BigRat.mt.eq
|
||||||
|
BigRat.mt.__le = BigRat.mt.le
|
||||||
|
BigRat.mt.__lt = BigRat.mt.lt
|
||||||
|
setmetatable( BigRat.mt, { __index = "inexistent field", __newindex = "not available", __metatable="hidden" } ) ;
|
||||||
|
function BigRat.add( brat1 , brat2 , brat3 )
|
||||||
|
brat3.den = brat1.den * brat2.den ;
|
||||||
|
brat3.num = ( brat1.num * brat2.den ) + ( brat2.num * brat1.den ) ;
|
||||||
|
return brat3 ;
|
||||||
|
end
|
||||||
|
function BigRat.sub( brat1 , brat2 , brat3 )
|
||||||
|
brat3.den = brat1.den * brat2.den ;
|
||||||
|
brat3.num = ( brat1.num * brat2.den ) - ( brat2.num * brat1.den ) ;
|
||||||
|
return brat3 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.mul( brat1 , brat2 , brat3 )
|
||||||
|
brat3.num = brat1.num * brat2.num ;
|
||||||
|
brat3.den = brat1.den * brat2.den ;
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.div( brat1 , brat2 , brat3 )
|
||||||
|
brat3.num = brat1.num * brat2.den ;
|
||||||
|
brat3.den = brat1.den * brat2.num ;
|
||||||
|
return brat3 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.pow( bnum1 , bnum2 )
|
||||||
|
if bnum1 == nil or bnum2 == nil then
|
||||||
|
error( "Function BigRat.pow: parameter nil" ) ;
|
||||||
|
end
|
||||||
|
local x = BigRat.new( "8" ) ;
|
||||||
|
local n = BigRat.new( bnum2.den ) ;
|
||||||
|
local n2 ;
|
||||||
|
local y = BigRat.new( ) ;
|
||||||
|
local i ;
|
||||||
|
local temp = BigRat.new( ) ;
|
||||||
|
|
||||||
|
BigRat.simplify( bnum2 ) ;
|
||||||
|
temp.num = BigNum.exp( bnum1.num , bnum2.num ) ;
|
||||||
|
temp.den = BigNum.exp( bnum1.den , bnum2.num ) ;
|
||||||
|
n2 = n - 1 ;
|
||||||
|
|
||||||
|
for i = 0 , 4 do
|
||||||
|
y.num = x.num ^ n2.num ;
|
||||||
|
y.den = x.den ^ n2.num ;
|
||||||
|
x = (( temp / y ) + ( n2 * x )) / n ;
|
||||||
|
end
|
||||||
|
return x ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.simplify( brat )
|
||||||
|
if brat == nil then
|
||||||
|
error( "Function BigRat.simplify: parameter nil" ) ;
|
||||||
|
end
|
||||||
|
local gcd = BigNum.new( ) ;
|
||||||
|
local temp = BigRat.new( brat ) ;
|
||||||
|
local devnull = BigNum.new( ) ;
|
||||||
|
local zero = BigNum.new( "0" ) ;
|
||||||
|
--Check if numerator is zero
|
||||||
|
if BigNum.compareAbs( brat.num , zero ) == 0 then
|
||||||
|
brat.den = BigNum.new( "1" ) ;
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
gcd = BigNum.gcd( brat.num , brat.den ) ;
|
||||||
|
BigNum.div( temp.num , gcd , brat.num , devnull ) ;
|
||||||
|
BigNum.div( temp.den , gcd , brat.den , devnull ) ;
|
||||||
|
--Update the signal
|
||||||
|
if brat.num.signal == '-' and brat.den.signal == '-' then
|
||||||
|
brat.num.signal = '+' ;
|
||||||
|
brat.den.signal = '+' ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.eq( brat1 , brat2 )
|
||||||
|
if BigRat.compare( brat1 , brat2 ) == 0 then
|
||||||
|
return true ;
|
||||||
|
else
|
||||||
|
return false ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.lt( brat1 , brat2 )
|
||||||
|
if BigRat.compare( brat1 , brat2 ) == 2 then
|
||||||
|
return true ;
|
||||||
|
else
|
||||||
|
return false ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.le( brat1 , brat2 )
|
||||||
|
local temp = -1 ;
|
||||||
|
temp = BigRat.compare( brat1 , brat2 )
|
||||||
|
if temp == 0 or temp == 2 then
|
||||||
|
return true ;
|
||||||
|
else
|
||||||
|
return false ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function BigRat.compare( bnum1 , bnum2 )
|
||||||
|
local temp ;
|
||||||
|
temp = bnum1 - bnum2 ;
|
||||||
|
if temp.num[0] == 0 and temp.num.len == 1 then --Check if is zero
|
||||||
|
return 0 ;
|
||||||
|
elseif temp.num.signal == "-" then
|
||||||
|
return 2 ;
|
||||||
|
else
|
||||||
|
return 1 ;
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -22,10 +22,7 @@ function base64.decode(s)
|
|||||||
local n=0
|
local n=0
|
||||||
s=s:sub(1,#s-#p)..p
|
s=s:sub(1,#s-#p)..p
|
||||||
for c = 1,#s,4 do
|
for c = 1,#s,4 do
|
||||||
n = bit.lshift(bsd[s:sub(c, c)], 18)
|
n = bit.lshift(bsd[s:sub(c, c)], 18) + bit.lshift(bsd[s:sub(c+1, c+1)], 12) + bit.lshift(bsd[s:sub(c + 2, c + 2)], 6) + bsd[s:sub(c + 3, c + 3)]
|
||||||
+ bit.lshift(bsd[s:sub(c+1, c+1)], 12)
|
|
||||||
+ bit.lshift(bsd[s:sub(c + 2, c + 2)], 6)
|
|
||||||
+ bsd[s:sub(c + 3, c + 3)]
|
|
||||||
r = r .. char(bit.band(bit.arshift(n, 16), 0xFF)) .. char(bit.band(bit.arshift(n, 8), 0xFF)) .. char(bit.band(n, 0xFF))
|
r = r .. char(bit.band(bit.arshift(n, 16), 0xFF)) .. char(bit.band(bit.arshift(n, 8), 0xFF)) .. char(bit.band(n, 0xFF))
|
||||||
end
|
end
|
||||||
return r:sub(1,-(cc+1))
|
return r:sub(1,-(cc+1))
|
||||||
@ -84,7 +84,8 @@ function bits.new(n,s)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
local temp={}
|
local temp={}
|
||||||
temp.t='bit'
|
temp.t='bits'
|
||||||
|
temp.Type="bits"
|
||||||
setmetatable(temp, bits)
|
setmetatable(temp, bits)
|
||||||
if type(n)~='string' then
|
if type(n)~='string' then
|
||||||
local tab={}
|
local tab={}
|
||||||
@ -1,7 +1,8 @@
|
|||||||
|
local __CURRENTVERSION=1
|
||||||
bin.registerBlock("t",function(SIZE_OR_NIL,ref)
|
bin.registerBlock("t",function(SIZE_OR_NIL,ref)
|
||||||
local header=ref:read(3)
|
local header=ref:read(3)
|
||||||
if not header:match("(LT.)") then error("Not a valid table struct!") end
|
if not header:match("(LT.)") then error("Not a valid table struct!") end
|
||||||
if bin.defualtBit.new(header:sub(3,3)):tonumber(1)~=0 then error("Incompatible Version of LuaTable!") end
|
if bin.defualtBit.new(header:sub(3,3)):tonumber(1)>__CURRENTVERSION then error("Incompatible Version of LuaTable!") end
|
||||||
local len=ref:getBlock("n",4) -- hehe lets make life easier
|
local len=ref:getBlock("n",4) -- hehe lets make life easier
|
||||||
local tab={}
|
local tab={}
|
||||||
local ind
|
local ind
|
||||||
@ -20,6 +21,14 @@ bin.registerBlock("t",function(SIZE_OR_NIL,ref)
|
|||||||
if dt=="N" then
|
if dt=="N" then
|
||||||
tab[ind]=ref:getBlock("n",4)
|
tab[ind]=ref:getBlock("n",4)
|
||||||
n=n+4
|
n=n+4
|
||||||
|
elseif dt=="I" then
|
||||||
|
tab[ind]=math.huge()
|
||||||
|
ref:getBlock("n",4)
|
||||||
|
n=n+4
|
||||||
|
elseif dt=="i" then
|
||||||
|
tab[ind]=-math.huge()
|
||||||
|
ref:getBlock("n",4)
|
||||||
|
n=n+4
|
||||||
elseif dt=="S" then
|
elseif dt=="S" then
|
||||||
local nn=ref:getBlock("n",4)
|
local nn=ref:getBlock("n",4)
|
||||||
tab[ind]=ref:read(nn)
|
tab[ind]=ref:read(nn)
|
||||||
@ -34,23 +43,40 @@ bin.registerBlock("t",function(SIZE_OR_NIL,ref)
|
|||||||
elseif dt=="T" then
|
elseif dt=="T" then
|
||||||
local cur=ref:getSeek()
|
local cur=ref:getSeek()
|
||||||
local size=ref:getBlock("n",4)
|
local size=ref:getBlock("n",4)
|
||||||
ref:seekSet(cur)
|
ref:setSeek(cur)
|
||||||
ref:read(4)
|
ref:read(4)
|
||||||
local data=bin.new(ref:read(size))
|
local data=bin.new(ref:read(size))
|
||||||
local dat=data:getBlock("t")
|
local dat=data:getBlock("t")
|
||||||
tab[ind]=dat
|
if dat.__RECURSIVE then
|
||||||
|
tab[ind]=tab
|
||||||
|
else
|
||||||
|
tab[ind]=dat
|
||||||
|
end
|
||||||
n=n+data:getSize()+4
|
n=n+data:getSize()+4
|
||||||
end
|
end
|
||||||
if n==len then break end
|
if n==len then break end
|
||||||
end
|
end
|
||||||
return tab
|
return bin.resolveType(tab)
|
||||||
end,function(d,fit,fmt,self,rec)
|
end,function(d,fit,fmt,self,rec,tabsaw)
|
||||||
-- INGORE FIT WE ARE CREATING A STRUCT!!!
|
-- INGORE FIT WE ARE CREATING A STRUCT!!!
|
||||||
-- fmt will apply to all numbers
|
-- fmt will apply to all numbers
|
||||||
|
local __rem=nil
|
||||||
|
if not tabsaw then rem=true end
|
||||||
|
local tabsaw=tabsaw or {}
|
||||||
|
if rem then
|
||||||
|
table.insert(tabsaw,d)
|
||||||
|
end
|
||||||
local bData={}
|
local bData={}
|
||||||
for i,v in pairs(d) do -- this is for tables, all but userdata is fine. Depending on where you are using lua functions may or may not work
|
for i,v in pairs(d) do -- this is for tables, all but userdata is fine. Depending on where you are using lua functions may or may not work
|
||||||
local tp=type(v):sub(1,1):upper() -- uppercase of datatype
|
local tp=type(v):sub(1,1):upper() -- uppercase of datatype
|
||||||
if type(i)=="number" then -- Lets handle indexies
|
if type(i)=="number" then -- Lets handle indexies
|
||||||
|
if v==math.huge then
|
||||||
|
tp="I"
|
||||||
|
v=0
|
||||||
|
elseif v==-math.huge then
|
||||||
|
tp="i"
|
||||||
|
v=0
|
||||||
|
end
|
||||||
table.insert(bData,"N"..tp..bin.defualtBit.numToBytes(i,4)) -- number index?
|
table.insert(bData,"N"..tp..bin.defualtBit.numToBytes(i,4)) -- number index?
|
||||||
elseif type(i)=="string" then
|
elseif type(i)=="string" then
|
||||||
if #i>255 then error("A string index cannot be larger than 255 bytes!") end
|
if #i>255 then error("A string index cannot be larger than 255 bytes!") end
|
||||||
@ -72,11 +98,28 @@ end,function(d,fit,fmt,self,rec)
|
|||||||
table.insert(bData,bin.defualtBit.numToBytes(#dump,4)) -- add length of dumped string
|
table.insert(bData,bin.defualtBit.numToBytes(#dump,4)) -- add length of dumped string
|
||||||
table.insert(bData,dump) -- add it
|
table.insert(bData,dump) -- add it
|
||||||
elseif type(v)=="table" then -- tables...
|
elseif type(v)=="table" then -- tables...
|
||||||
local data=rec(v,nil,"t",self,rec)
|
if tabsaw[1]==v then
|
||||||
|
v={__RECURSIVE=i}
|
||||||
|
else
|
||||||
|
tabsaw[i]=v
|
||||||
|
end
|
||||||
|
local data=rec(v,nil,"t",self,rec,tabsaw)
|
||||||
table.insert(bData,bin.defualtBit.numToBytes(#data,4)) -- add length of string
|
table.insert(bData,bin.defualtBit.numToBytes(#data,4)) -- add length of string
|
||||||
table.insert(bData,data) -- add string
|
table.insert(bData,data) -- add string
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local data=table.concat(bData)
|
local data=table.concat(bData)
|
||||||
return "LT\0"..bin.defualtBit.numToBytes(#data,4)..data
|
return "LT"..string.char(__CURRENTVERSION)..bin.defualtBit.numToBytes(#data,4)..data
|
||||||
|
end)
|
||||||
|
bin.registerBlock("b",function(SIZE_OR_NIL,ref)
|
||||||
|
return ({["\255"]=true,["\0"]=false})[ref:read(1)]
|
||||||
|
end,function(d)
|
||||||
|
return ({[true]="\255",[false]="\0"})[d]
|
||||||
|
end)
|
||||||
|
bin.registerBlock("f",function(SIZE_OR_NIL,ref)
|
||||||
|
local nn=ref:getBlock("n",4)
|
||||||
|
return loadstring(ref:read(nn))
|
||||||
|
end,function(d)
|
||||||
|
local dump=string.dump(d)
|
||||||
|
return bin.defualtBit.numToBytes(#dump,4)..dump
|
||||||
end)
|
end)
|
||||||
@ -96,7 +96,8 @@ function infinabits.newByteArray(s)-- WIP
|
|||||||
end
|
end
|
||||||
function infinabits.new(n,binary)
|
function infinabits.new(n,binary)
|
||||||
local temp={}
|
local temp={}
|
||||||
temp.t='bit'
|
temp.t="infinabits"
|
||||||
|
temp.Type="infinabits"
|
||||||
if type(n)=="string" then
|
if type(n)=="string" then
|
||||||
if binary then
|
if binary then
|
||||||
temp.data=n:match("[10]+")
|
temp.data=n:match("[10]+")
|
||||||
@ -1,5 +1,3 @@
|
|||||||
-- Well Its finally time for that massive rewrite that has been long awaited for
|
|
||||||
-- We need to keep things thread safe or the rewrite would have been in vain... Also this will ensure that all features are working perfectly
|
|
||||||
bin={}
|
bin={}
|
||||||
bin.Version={5,0,0}
|
bin.Version={5,0,0}
|
||||||
bin.stage='stable'
|
bin.stage='stable'
|
||||||
@ -9,7 +7,7 @@ bin.__index = bin
|
|||||||
bin.__tostring=function(self) return self:getData() end
|
bin.__tostring=function(self) return self:getData() end
|
||||||
bin.__len=function(self) return self:getlength() end
|
bin.__len=function(self) return self:getlength() end
|
||||||
bin.lastBlockSize=0
|
bin.lastBlockSize=0
|
||||||
bin.streams={} -- FIX FOR THREADING!!!
|
bin.streams={}
|
||||||
-- Helpers
|
-- Helpers
|
||||||
function bin.getVersion()
|
function bin.getVersion()
|
||||||
return bin.Version[1]..'.'..bin.Version[2]..'.'..bin.Version[3]
|
return bin.Version[1]..'.'..bin.Version[2]..'.'..bin.Version[3]
|
||||||
@ -22,15 +20,18 @@ elseif bit32 then
|
|||||||
else
|
else
|
||||||
bit=require("bin.no_jit_bit")
|
bit=require("bin.no_jit_bit")
|
||||||
end
|
end
|
||||||
local base64=require("bin.base64")
|
base64=require("bin.base64")
|
||||||
local base91=require("bin.base91")
|
base91=require("bin.base91")
|
||||||
|
bin.lzw=require("bin.lzw") -- A WIP
|
||||||
bits=require("bin.bits")
|
bits=require("bin.bits")
|
||||||
infinabits=require("bin.infinabits") -- like the bits library but works past 32 bits for 32bit lua and 64 bits for 64 bit lua... infinabits!!!!
|
infinabits=require("bin.infinabits") -- like the bits library but works past 32 bits for 32bit lua and 64 bits for 64 bit lua.
|
||||||
|
bin.md5=require("bin.md5")
|
||||||
|
randomGen=require("bin.random")
|
||||||
function bin.setBitsInterface(int)
|
function bin.setBitsInterface(int)
|
||||||
bin.defualtBit=int or bits
|
bin.defualtBit=int or bits
|
||||||
end
|
end
|
||||||
bin.setBitsInterface()
|
bin.setBitsInterface()
|
||||||
function bin.normalizeData(data) -- unified function to allow
|
function bin.normalizeData(data) -- unified function to allow for all types to string
|
||||||
if type(data)=="string" then return data end
|
if type(data)=="string" then return data end
|
||||||
if type(data)=="table" then
|
if type(data)=="table" then
|
||||||
if data.Type=="bin" or data.Type=="streamable" or data.Type=="buffer" then
|
if data.Type=="bin" or data.Type=="streamable" or data.Type=="buffer" then
|
||||||
@ -40,7 +41,7 @@ function bin.normalizeData(data) -- unified function to allow
|
|||||||
elseif data.Type=="sink" then
|
elseif data.Type=="sink" then
|
||||||
-- LATER
|
-- LATER
|
||||||
else
|
else
|
||||||
error("I do not know how to handle this data!")
|
return ""
|
||||||
end
|
end
|
||||||
elseif type(data)=="userdata" then
|
elseif type(data)=="userdata" then
|
||||||
if tostring(data):sub(1,4)=="file" then
|
if tostring(data):sub(1,4)=="file" then
|
||||||
@ -49,9 +50,35 @@ function bin.normalizeData(data) -- unified function to allow
|
|||||||
local dat=data:read("*a")
|
local dat=data:read("*a")
|
||||||
data:seek("set",cur)
|
data:seek("set",cur)
|
||||||
return dat
|
return dat
|
||||||
|
else
|
||||||
|
error("File handles are the only userdata that can be used!")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
function bin.resolveType(tab) -- used in getblock for auto object creation. Internal method
|
||||||
|
if tab.Type then
|
||||||
|
if tab.Type=="bin" then
|
||||||
|
return bin.new(tab.data)
|
||||||
|
elseif tab.Type=="streamable" then
|
||||||
|
if bin.fileExist(tab.file) then return nil,"Cannot load the stream file, source file does not exist!" end
|
||||||
|
return bin.stream(tab.file,tab.lock)
|
||||||
|
elseif tab.Type=="buffer" then
|
||||||
|
local buf=bin.newDataBuffer(tab.size)
|
||||||
|
buf[1]=tab:getData()
|
||||||
|
return buf
|
||||||
|
elseif tab.Type=="bits" then
|
||||||
|
local b=bits.new("")
|
||||||
|
b.data=tab.data
|
||||||
|
return b
|
||||||
|
elseif tab.Type=="infinabits" then
|
||||||
|
local b=infinabits.new("")
|
||||||
|
b.data=tab.data
|
||||||
|
return b
|
||||||
|
elseif tab.Type=="sink" then
|
||||||
|
return bin.newSync(tab.data)
|
||||||
|
end
|
||||||
|
else return tab end
|
||||||
|
end
|
||||||
function bin.fileExist(path)
|
function bin.fileExist(path)
|
||||||
g=io.open(path or '','r')
|
g=io.open(path or '','r')
|
||||||
if path =='' then
|
if path =='' then
|
||||||
@ -92,7 +119,7 @@ function bin.fromBase91(s)
|
|||||||
end
|
end
|
||||||
-- Constructors
|
-- Constructors
|
||||||
function bin.new(data)
|
function bin.new(data)
|
||||||
data=tostring(data or "")
|
data=bin.normalizeData(data)
|
||||||
local c = {}
|
local c = {}
|
||||||
setmetatable(c, bin)
|
setmetatable(c, bin)
|
||||||
c.data=data
|
c.data=data
|
||||||
@ -164,12 +191,12 @@ function bin:canStreamWrite()
|
|||||||
end
|
end
|
||||||
function bin:getSeek()
|
function bin:getSeek()
|
||||||
if self.stream then
|
if self.stream then
|
||||||
return self.workingfile:seek("cur")
|
return self.workingfile:seek("cur")+1
|
||||||
else
|
else
|
||||||
return self.pos
|
return self.pos
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function bin:seekSet(n)
|
function bin:setSeek(n)
|
||||||
if self.stream then
|
if self.stream then
|
||||||
self.workingfile:seek("set",n-1)
|
self.workingfile:seek("set",n-1)
|
||||||
else
|
else
|
||||||
@ -197,6 +224,7 @@ function bin:read(n)
|
|||||||
else
|
else
|
||||||
local data=self.data:sub(self.pos,self.pos+n-1)
|
local data=self.data:sub(self.pos,self.pos+n-1)
|
||||||
self.pos=self.pos+n
|
self.pos=self.pos+n
|
||||||
|
if data=="" then return end
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -236,15 +264,19 @@ function bin:sub(a,b)
|
|||||||
end
|
end
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
function bin:getData(fmt)
|
function bin:getData(a,b,fmt)
|
||||||
local data=""
|
local data=""
|
||||||
if self.stream then
|
if a or b then
|
||||||
local cur=self.workingfile:seek("cur")
|
data=self:sub(a,b)
|
||||||
self.workingfile:seek("set",0)
|
|
||||||
data=self.workingfile:read("*a")
|
|
||||||
self.workingfile:seek("set",cur)
|
|
||||||
else
|
else
|
||||||
data=self.data
|
if self.stream then
|
||||||
|
local cur=self.workingfile:seek("cur")
|
||||||
|
self.workingfile:seek("set",0)
|
||||||
|
data=self.workingfile:read("*a")
|
||||||
|
self.workingfile:seek("set",cur)
|
||||||
|
else
|
||||||
|
data=self.data
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if fmt=="%x" or fmt=="hex" then
|
if fmt=="%x" or fmt=="hex" then
|
||||||
return bin.toHex(data):lower()
|
return bin.toHex(data):lower()
|
||||||
@ -252,6 +284,8 @@ function bin:getData(fmt)
|
|||||||
return bin.toHex(data)
|
return bin.toHex(data)
|
||||||
elseif fmt=="%b" or fmt=="b64" then
|
elseif fmt=="%b" or fmt=="b64" then
|
||||||
return bin.toB64(data)
|
return bin.toB64(data)
|
||||||
|
elseif fmt then
|
||||||
|
return bin.new(data):getBlock(fmt,#data)
|
||||||
end
|
end
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
@ -275,10 +309,10 @@ end
|
|||||||
function bin:tackE(data,size,h)
|
function bin:tackE(data,size,h)
|
||||||
local data=bin.normalizeData(data)
|
local data=bin.normalizeData(data)
|
||||||
local cur=self:getSize()
|
local cur=self:getSize()
|
||||||
self:seekSet(self:getSize()+1)
|
self:setSeek(self:getSize()+1)
|
||||||
self:write(data,size)
|
self:write(data,size)
|
||||||
if h then
|
if h then
|
||||||
self:seekSet(cur+1)
|
self:setSeek(cur+1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function bin:tonumber(a,b)
|
function bin:tonumber(a,b)
|
||||||
@ -355,6 +389,7 @@ function bin:getBlock(t,n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
function bin:addBlock(d,fit,fmt)
|
function bin:addBlock(d,fit,fmt)
|
||||||
|
if not fmt then fmt=type(d):sub(1,1) end
|
||||||
if type(d)=="number" then
|
if type(d)=="number" then
|
||||||
local data=bin.defualtBit.numToBytes(d,fit or 4,fmt,function()
|
local data=bin.defualtBit.numToBytes(d,fit or 4,fmt,function()
|
||||||
error("Overflow! Space allotted for number is smaller than the number takes up. Increase the fit!")
|
error("Overflow! Space allotted for number is smaller than the number takes up. Increase the fit!")
|
||||||
@ -362,7 +397,7 @@ function bin:addBlock(d,fit,fmt)
|
|||||||
self:tackE(data)
|
self:tackE(data)
|
||||||
elseif type(d)=="string" then
|
elseif type(d)=="string" then
|
||||||
local data=d:sub(1,fit or -1)
|
local data=d:sub(1,fit or -1)
|
||||||
if data<(fit or #data) then
|
if #data<(fit or #data) then
|
||||||
data=data..string.rep("\0",fit-#data)
|
data=data..string.rep("\0",fit-#data)
|
||||||
end
|
end
|
||||||
self:tackE(data)
|
self:tackE(data)
|
||||||
@ -374,4 +409,171 @@ bin.registerBlocks={}
|
|||||||
function bin.registerBlock(t,funcG,funcA)
|
function bin.registerBlock(t,funcG,funcA)
|
||||||
bin.registerBlocks[t]={funcG,funcA}
|
bin.registerBlocks[t]={funcG,funcA}
|
||||||
end
|
end
|
||||||
|
function bin.newDataBuffer(size,fill) -- fills with \0 or nul or with what you enter
|
||||||
|
local c={}
|
||||||
|
local fill=fill or "\0"
|
||||||
|
c.data={self=c}
|
||||||
|
c.Type="buffer"
|
||||||
|
c.size=size or 0 -- 0 means an infinite buffer, sometimes useful
|
||||||
|
for i=1,size do
|
||||||
|
c.data[i]=fill
|
||||||
|
end
|
||||||
|
local mt={
|
||||||
|
__index=function(t,k)
|
||||||
|
if type(k)=="number" then
|
||||||
|
local data=t.data[k]
|
||||||
|
if data then
|
||||||
|
return string.byte(data)
|
||||||
|
else
|
||||||
|
error("Index out of range!")
|
||||||
|
end
|
||||||
|
elseif type(k)=="string" then
|
||||||
|
local num=tonumber(k)
|
||||||
|
if num then
|
||||||
|
local data=t.data[num]
|
||||||
|
if data then
|
||||||
|
return data
|
||||||
|
else
|
||||||
|
error("Index out of range!")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error("Only number-strings and numbers can be indexed!")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error("Only number-strings and numbers can be indexed!")
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
__newindex=function(t,k,v)
|
||||||
|
if type(k)~="number" then error("Can only set a buffers data with a numeric index!") end
|
||||||
|
local data=""
|
||||||
|
if type(v)=="string" then
|
||||||
|
data=v
|
||||||
|
elseif type(v)=="number" then
|
||||||
|
data=string.char(v)
|
||||||
|
else
|
||||||
|
-- try to normalize the data of type v
|
||||||
|
data=bin.normalizeData(v)
|
||||||
|
end
|
||||||
|
t:fillBuffer(k,data)
|
||||||
|
end,
|
||||||
|
__tostring=function(t)
|
||||||
|
return t:getData()
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
function c:fillBuffer(a,data)
|
||||||
|
local len=#data
|
||||||
|
if len==1 then
|
||||||
|
self.data[a]=data
|
||||||
|
else
|
||||||
|
local i=a-1
|
||||||
|
for d in data:gmatch(".") do
|
||||||
|
i=i+1
|
||||||
|
if i>c.size then
|
||||||
|
return #data-i+a
|
||||||
|
end
|
||||||
|
self.data[i]=d
|
||||||
|
end
|
||||||
|
return #data-i+(a-1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function c:getData(a,b,fmt) -- LATER
|
||||||
|
local dat=bin.new(table.concat(self.data,"",a,b))
|
||||||
|
local n=dat:getSize()
|
||||||
|
return dat:getBlock(fmt or "s",n)
|
||||||
|
end
|
||||||
|
function c:getSize()
|
||||||
|
return #self:getData()
|
||||||
|
end
|
||||||
|
setmetatable(c,mt)
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
function bin:newDataBufferFromStream(pos,size,fill) -- fills with \0 or nul or with what you enter IF the nothing exists inside the bin file.
|
||||||
|
local s=self:getSize()
|
||||||
|
if not self.stream then error("Can only created a streamed buffer on a streamable file!") end
|
||||||
|
if s==0 then
|
||||||
|
self:write(string.rep("\0",pos+size))
|
||||||
|
end
|
||||||
|
self:setSeek(1)
|
||||||
|
local c=bin.newDataBuffer(size,fill)
|
||||||
|
rawset(c,"pos",pos)
|
||||||
|
rawset(c,"size",size)
|
||||||
|
rawset(c,"fill",fill)
|
||||||
|
rawset(c,"bin",self)
|
||||||
|
rawset(c,"sync",function(self)
|
||||||
|
local cur=self.bin:getSeek()
|
||||||
|
self.bin:setSeek(self.pos)
|
||||||
|
self.bin:write(self:getData(),size)
|
||||||
|
self.bin:setSeek(cur)
|
||||||
|
end)
|
||||||
|
c:fillBuffer(1,self:sub(pos,pos+size))
|
||||||
|
function c:fillBuffer(a,data)
|
||||||
|
local len=#data
|
||||||
|
if len==1 then
|
||||||
|
self.data[a]=data
|
||||||
|
self:sync()
|
||||||
|
else
|
||||||
|
local i=a-1
|
||||||
|
for d in data:gmatch(".") do
|
||||||
|
i=i+1
|
||||||
|
if i>c.size then
|
||||||
|
self:sync()
|
||||||
|
return #data-i+a
|
||||||
|
end
|
||||||
|
self.data[i]=d
|
||||||
|
end
|
||||||
|
self:sync()
|
||||||
|
return #data-i+(a-1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
function bin:getMD5Hash()
|
||||||
|
self:setSeek(1)
|
||||||
|
local len=self:getSize()
|
||||||
|
local md5=bin.md5.new()
|
||||||
|
local SIZE=2048
|
||||||
|
if len>SIZE then
|
||||||
|
local dat=self:read(SIZE)
|
||||||
|
while dat~=nil do
|
||||||
|
md5:update(dat)
|
||||||
|
dat=self:read(SIZE)
|
||||||
|
end
|
||||||
|
return bin.md5.tohex(md5:finish()):upper()
|
||||||
|
else
|
||||||
|
return bin.md5.sumhexa(self:getData()):upper()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function bin:getHash()
|
||||||
|
if self:getSize()==0 then
|
||||||
|
return "NaN"
|
||||||
|
end
|
||||||
|
n=32
|
||||||
|
local rand = randomGen:newND(1,self:getSize(),self:getSize())
|
||||||
|
local h,g={},0
|
||||||
|
for i=1,n do
|
||||||
|
g=rand:nextInt()
|
||||||
|
table.insert(h,bin.toHex(self:sub(g,g)))
|
||||||
|
end
|
||||||
|
return table.concat(h,'')
|
||||||
|
end
|
||||||
|
function bin:flipbits()
|
||||||
|
if self:canStreamWrite() then
|
||||||
|
self:setSeek(1)
|
||||||
|
for i=1,self:getSize() do
|
||||||
|
self:write(string.char(255-string.byte(self:sub(i,i))))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local temp={}
|
||||||
|
for i=1,#self.data do
|
||||||
|
table.insert(temp,string.char(255-string.byte(string.sub(self.data,i,i))))
|
||||||
|
end
|
||||||
|
self.data=table.concat(temp,'')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function bin:encrypt()
|
||||||
|
self:flipbits()
|
||||||
|
end
|
||||||
|
function bin:decrypt()
|
||||||
|
self:flipbits()
|
||||||
|
end
|
||||||
require("bin.extraBlocks") -- registered blocks that you can use
|
require("bin.extraBlocks") -- registered blocks that you can use
|
||||||
73
bin/lzw.lua
Normal file
73
bin/lzw.lua
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
--[[
|
||||||
|
LZW String Compression demo for Gideros
|
||||||
|
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
|
||||||
|
(C) 2013 - Guava7
|
||||||
|
]]
|
||||||
|
CLZWCompression = {}
|
||||||
|
function CLZWCompression:InitDictionary(isEncode)
|
||||||
|
self.mDictionary = {}
|
||||||
|
-- local s = " !#$%&'\"()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
||||||
|
local s={}
|
||||||
|
for i=1,255 do
|
||||||
|
s[#s+1]=string.char(i)
|
||||||
|
end
|
||||||
|
s=table.concat(s)
|
||||||
|
local len = #s
|
||||||
|
for i = 1, len do
|
||||||
|
if isEncode then
|
||||||
|
self.mDictionary[s:sub(i, i)] = i
|
||||||
|
else
|
||||||
|
self.mDictionary[i] = s:sub(i, i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
self.mDictionaryLen = len
|
||||||
|
end
|
||||||
|
function CLZWCompression:Encode(sInput)
|
||||||
|
self:InitDictionary(true)
|
||||||
|
local s = ""
|
||||||
|
local ch
|
||||||
|
local len = #sInput
|
||||||
|
local result = {}
|
||||||
|
local dic = self.mDictionary
|
||||||
|
local temp
|
||||||
|
for i = 1, len do
|
||||||
|
ch = sInput:sub(i, i)
|
||||||
|
temp = s..ch
|
||||||
|
if dic[temp] then
|
||||||
|
s = temp
|
||||||
|
else
|
||||||
|
result[#result + 1] = dic[s]
|
||||||
|
self.mDictionaryLen = self.mDictionaryLen + 1
|
||||||
|
dic[temp] = self.mDictionaryLen
|
||||||
|
s = ch
|
||||||
|
end
|
||||||
|
end
|
||||||
|
result[#result + 1] = dic[s]
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
function CLZWCompression:Decode(data)
|
||||||
|
self:InitDictionary(false)
|
||||||
|
local dic = self.mDictionary
|
||||||
|
local entry
|
||||||
|
local ch
|
||||||
|
local prevCode, currCode
|
||||||
|
local result = {}
|
||||||
|
prevCode = data[1]
|
||||||
|
result[#result + 1] = dic[prevCode]
|
||||||
|
for i = 2, #data do
|
||||||
|
currCode = data[i]
|
||||||
|
entry = dic[currCode]
|
||||||
|
if entry then
|
||||||
|
ch = entry:sub(1, 1)
|
||||||
|
result[#result + 1] = entry
|
||||||
|
else
|
||||||
|
ch = dic[prevCode]:sub(1, 1)
|
||||||
|
result[#result + 1] = dic[prevCode]..ch
|
||||||
|
end
|
||||||
|
dic[#dic + 1] = dic[prevCode]..ch
|
||||||
|
prevCode = currCode
|
||||||
|
end
|
||||||
|
return table.concat(result)
|
||||||
|
end
|
||||||
|
|
||||||
|
return CLZWCompression
|
||||||
@ -6,25 +6,6 @@ local md5 = {
|
|||||||
MIT LICENSE
|
MIT LICENSE
|
||||||
|
|
||||||
Copyright (c) 2013 Enrique García Cota + Adam Baldwin + hanzao + Equi 4 Software
|
Copyright (c) 2013 Enrique García Cota + Adam Baldwin + hanzao + Equi 4 Software
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
copy of this software and associated documentation files (the
|
|
||||||
"Software"), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included
|
|
||||||
in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
||||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,210 +1,7 @@
|
|||||||
--[[
|
--[[
|
||||||
|
|
||||||
LUA MODULE
|
|
||||||
|
|
||||||
bit.numberlua - Bitwise operations implemented in pure Lua as numbers,
|
|
||||||
with Lua 5.2 'bit32' and (LuaJIT) LuaBitOp 'bit' compatibility interfaces.
|
|
||||||
|
|
||||||
SYNOPSIS
|
|
||||||
|
|
||||||
local bit = require 'bit.numberlua'
|
|
||||||
print(bit.band(0xff00ff00, 0x00ff00ff)) --> 0xffffffff
|
|
||||||
|
|
||||||
-- Interface providing strong Lua 5.2 'bit32' compatibility
|
|
||||||
local bit32 = require 'bit.numberlua'.bit32
|
|
||||||
assert(bit32.band(-1) == 0xffffffff)
|
|
||||||
|
|
||||||
-- Interface providing strong (LuaJIT) LuaBitOp 'bit' compatibility
|
|
||||||
local bit = require 'bit.numberlua'.bit
|
|
||||||
assert(bit.tobit(0xffffffff) == -1)
|
|
||||||
|
|
||||||
DESCRIPTION
|
|
||||||
|
|
||||||
This library implements bitwise operations entirely in Lua.
|
|
||||||
This module is typically intended if for some reasons you don't want
|
|
||||||
to or cannot install a popular C based bit library like BitOp 'bit' [1]
|
|
||||||
(which comes pre-installed with LuaJIT) or 'bit32' (which comes
|
|
||||||
pre-installed with Lua 5.2) but want a similar interface.
|
|
||||||
|
|
||||||
This modules represents bit arrays as non-negative Lua numbers. [1]
|
|
||||||
It can represent 32-bit bit arrays when Lua is compiled
|
|
||||||
with lua_Number as double-precision IEEE 754 floating point.
|
|
||||||
|
|
||||||
The module is nearly the most efficient it can be but may be a few times
|
|
||||||
slower than the C based bit libraries and is orders or magnitude
|
|
||||||
slower than LuaJIT bit operations, which compile to native code. Therefore,
|
|
||||||
this library is inferior in performane to the other modules.
|
|
||||||
|
|
||||||
The `xor` function in this module is based partly on Roberto Ierusalimschy's
|
|
||||||
post in http://lua-users.org/lists/lua-l/2002-09/msg00134.html .
|
|
||||||
|
|
||||||
The included BIT.bit32 and BIT.bit sublibraries aims to provide 100%
|
|
||||||
compatibility with the Lua 5.2 "bit32" and (LuaJIT) LuaBitOp "bit" library.
|
|
||||||
This compatbility is at the cost of some efficiency since inputted
|
|
||||||
numbers are normalized and more general forms (e.g. multi-argument
|
|
||||||
bitwise operators) are supported.
|
|
||||||
|
|
||||||
STATUS
|
|
||||||
|
|
||||||
WARNING: Not all corner cases have been tested and documented.
|
|
||||||
Some attempt was made to make these similar to the Lua 5.2 [2]
|
|
||||||
and LuaJit BitOp [3] libraries, but this is not fully tested and there
|
|
||||||
are currently some differences. Addressing these differences may
|
|
||||||
be improved in the future but it is not yet fully determined how to
|
|
||||||
resolve these differences.
|
|
||||||
|
|
||||||
The BIT.bit32 library passes the Lua 5.2 test suite (bitwise.lua)
|
|
||||||
http://www.lua.org/tests/5.2/ . The BIT.bit library passes the LuaBitOp
|
|
||||||
test suite (bittest.lua). However, these have not been tested on
|
|
||||||
platforms with Lua compiled with 32-bit integer numbers.
|
|
||||||
|
|
||||||
API
|
|
||||||
|
|
||||||
BIT.tobit(x) --> z
|
|
||||||
|
|
||||||
Similar to function in BitOp.
|
|
||||||
|
|
||||||
BIT.tohex(x, n)
|
|
||||||
|
|
||||||
Similar to function in BitOp.
|
|
||||||
|
|
||||||
BIT.band(x, y) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 and BitOp but requires two arguments.
|
|
||||||
|
|
||||||
BIT.bor(x, y) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 and BitOp but requires two arguments.
|
|
||||||
|
|
||||||
BIT.bxor(x, y) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 and BitOp but requires two arguments.
|
|
||||||
|
|
||||||
BIT.bnot(x) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 and BitOp.
|
|
||||||
|
|
||||||
BIT.lshift(x, disp) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 (warning: BitOp uses unsigned lower 5 bits of shift),
|
|
||||||
|
|
||||||
BIT.rshift(x, disp) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 (warning: BitOp uses unsigned lower 5 bits of shift),
|
|
||||||
|
|
||||||
BIT.extract(x, field [, width]) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2.
|
|
||||||
|
|
||||||
BIT.replace(x, v, field, width) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2.
|
|
||||||
|
|
||||||
BIT.bswap(x) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2.
|
|
||||||
|
|
||||||
BIT.rrotate(x, disp) --> z
|
|
||||||
BIT.ror(x, disp) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 and BitOp.
|
|
||||||
|
|
||||||
BIT.lrotate(x, disp) --> z
|
|
||||||
BIT.rol(x, disp) --> z
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 and BitOp.
|
|
||||||
|
|
||||||
BIT.arshift
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 and BitOp.
|
|
||||||
|
|
||||||
BIT.btest
|
|
||||||
|
|
||||||
Similar to function in Lua 5.2 with requires two arguments.
|
|
||||||
|
|
||||||
BIT.bit32
|
|
||||||
|
|
||||||
This table contains functions that aim to provide 100% compatibility
|
|
||||||
with the Lua 5.2 "bit32" library.
|
|
||||||
|
|
||||||
bit32.arshift (x, disp) --> z
|
|
||||||
bit32.band (...) --> z
|
|
||||||
bit32.bnot (x) --> z
|
|
||||||
bit32.bor (...) --> z
|
|
||||||
bit32.btest (...) --> true | false
|
|
||||||
bit32.bxor (...) --> z
|
|
||||||
bit32.extract (x, field [, width]) --> z
|
|
||||||
bit32.replace (x, v, field [, width]) --> z
|
|
||||||
bit32.lrotate (x, disp) --> z
|
|
||||||
bit32.lshift (x, disp) --> z
|
|
||||||
bit32.rrotate (x, disp) --> z
|
|
||||||
bit32.rshift (x, disp) --> z
|
|
||||||
|
|
||||||
BIT.bit
|
|
||||||
|
|
||||||
This table contains functions that aim to provide 100% compatibility
|
|
||||||
with the LuaBitOp "bit" library (from LuaJIT).
|
|
||||||
|
|
||||||
bit.tobit(x) --> y
|
|
||||||
bit.tohex(x [,n]) --> y
|
|
||||||
bit.bnot(x) --> y
|
|
||||||
bit.bor(x1 [,x2...]) --> y
|
|
||||||
bit.band(x1 [,x2...]) --> y
|
|
||||||
bit.bxor(x1 [,x2...]) --> y
|
|
||||||
bit.lshift(x, n) --> y
|
|
||||||
bit.rshift(x, n) --> y
|
|
||||||
bit.arshift(x, n) --> y
|
|
||||||
bit.rol(x, n) --> y
|
|
||||||
bit.ror(x, n) --> y
|
|
||||||
bit.bswap(x) --> y
|
|
||||||
|
|
||||||
DEPENDENCIES
|
|
||||||
|
|
||||||
None (other than Lua 5.1 or 5.2).
|
|
||||||
|
|
||||||
DOWNLOAD/INSTALLATION
|
|
||||||
|
|
||||||
If using LuaRocks:
|
|
||||||
luarocks install lua-bit-numberlua
|
|
||||||
|
|
||||||
Otherwise, download <https://github.com/davidm/lua-bit-numberlua/zipball/master>.
|
|
||||||
Alternately, if using git:
|
|
||||||
git clone git://github.com/davidm/lua-bit-numberlua.git
|
|
||||||
cd lua-bit-numberlua
|
|
||||||
Optionally unpack:
|
|
||||||
./util.mk
|
|
||||||
or unpack and install in LuaRocks:
|
|
||||||
./util.mk install
|
|
||||||
|
|
||||||
REFERENCES
|
|
||||||
|
|
||||||
[1] http://lua-users.org/wiki/FloatingPoint
|
|
||||||
[2] http://www.lua.org/manual/5.2/
|
|
||||||
[3] http://bitop.luajit.org/
|
|
||||||
|
|
||||||
LICENSE
|
LICENSE
|
||||||
|
|
||||||
(c) 2008-2011 David Manura. Licensed under the same terms as Lua (MIT).
|
(c) 2008-2011 David Manura. Licensed under the same terms as Lua (MIT).
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
(end license)
|
|
||||||
|
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
local M = {_TYPE='module', _NAME='bit.numberlua', _VERSION='0.3.1.20120131'}
|
local M = {_TYPE='module', _NAME='bit.numberlua', _VERSION='0.3.1.20120131'}
|
||||||
@ -345,10 +142,6 @@ function M.btest(x, y) -- Lua5.2 inspired
|
|||||||
return band(x, y) ~= 0
|
return band(x, y) ~= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
--
|
|
||||||
-- Start Lua 5.2 "bit32" compat section.
|
|
||||||
--
|
|
||||||
|
|
||||||
M.bit32 = {} -- Lua 5.2 'bit32' compatibility
|
M.bit32 = {} -- Lua 5.2 'bit32' compatibility
|
||||||
|
|
||||||
|
|
||||||
@ -463,11 +256,6 @@ function M.bit32.replace(x, v, field, ...)
|
|||||||
return replace(x, v, field, ...)
|
return replace(x, v, field, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Start LuaBitOp "bit" compat section.
|
|
||||||
--
|
|
||||||
|
|
||||||
M.bit = {} -- LuaBitOp "bit" compatibility
|
M.bit = {} -- LuaBitOp "bit" compatibility
|
||||||
|
|
||||||
function M.bit.tobit(x)
|
function M.bit.tobit(x)
|
||||||
232
bin/random.lua
Normal file
232
bin/random.lua
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
--[[----------------------------------------
|
||||||
|
Random
|
||||||
|
Not all of this is mine
|
||||||
|
------------------------------------------]]
|
||||||
|
--[[------------------------------------
|
||||||
|
RandomLua v0.3.1
|
||||||
|
Pure Lua Pseudo-Random Numbers Generator
|
||||||
|
Under the MIT license.
|
||||||
|
copyright(c) 2011 linux-man
|
||||||
|
--]]------------------------------------
|
||||||
|
|
||||||
|
local math_floor = math.floor
|
||||||
|
|
||||||
|
local function normalize(n)
|
||||||
|
return n % 0x80000000
|
||||||
|
end
|
||||||
|
|
||||||
|
local function bit_and(a, b)
|
||||||
|
local r = 0
|
||||||
|
local m = 0
|
||||||
|
for m = 0, 31 do
|
||||||
|
if (a % 2 == 1) and (b % 2 == 1) then r = r + 2^m end
|
||||||
|
if a % 2 ~= 0 then a = a - 1 end
|
||||||
|
if b % 2 ~= 0 then b = b - 1 end
|
||||||
|
a = a / 2 b = b / 2
|
||||||
|
end
|
||||||
|
return normalize(r)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function bit_or(a, b)
|
||||||
|
local r = 0
|
||||||
|
local m = 0
|
||||||
|
for m = 0, 31 do
|
||||||
|
if (a % 2 == 1) or (b % 2 == 1) then r = r + 2^m end
|
||||||
|
if a % 2 ~= 0 then a = a - 1 end
|
||||||
|
if b % 2 ~= 0 then b = b - 1 end
|
||||||
|
a = a / 2 b = b / 2
|
||||||
|
end
|
||||||
|
return normalize(r)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function bit_xor(a, b)
|
||||||
|
local r = 0
|
||||||
|
local m = 0
|
||||||
|
for m = 0, 31 do
|
||||||
|
if a % 2 ~= b % 2 then r = r + 2^m end
|
||||||
|
if a % 2 ~= 0 then a = a - 1 end
|
||||||
|
if b % 2 ~= 0 then b = b - 1 end
|
||||||
|
a = a / 2 b = b / 2
|
||||||
|
end
|
||||||
|
return normalize(r)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function seed()
|
||||||
|
return normalize(os.time())
|
||||||
|
end
|
||||||
|
|
||||||
|
--Mersenne twister
|
||||||
|
local mersenne_twister = {}
|
||||||
|
mersenne_twister.__index = mersenne_twister
|
||||||
|
|
||||||
|
function mersenne_twister:randomseed(s)
|
||||||
|
if not s then s = seed() end
|
||||||
|
self.mt[0] = normalize(s)
|
||||||
|
for i = 1, 623 do
|
||||||
|
self.mt[i] = normalize(0x6c078965 * bit_xor(self.mt[i-1], math_floor(self.mt[i-1] / 0x40000000)) + i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function mersenne_twister:random(a, b)
|
||||||
|
local y
|
||||||
|
if self.index == 0 then
|
||||||
|
for i = 0, 623 do
|
||||||
|
y = self.mt[(i + 1) % 624] % 0x80000000
|
||||||
|
self.mt[i] = bit_xor(self.mt[(i + 397) % 624], math_floor(y / 2))
|
||||||
|
if y % 2 ~= 0 then self.mt[i] = bit_xor(self.mt[i], 0x9908b0df) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
y = self.mt[self.index]
|
||||||
|
y = bit_xor(y, math_floor(y / 0x800))
|
||||||
|
y = bit_xor(y, bit_and(normalize(y * 0x80), 0x9d2c5680))
|
||||||
|
y = bit_xor(y, bit_and(normalize(y * 0x8000), 0xefc60000))
|
||||||
|
y = bit_xor(y, math_floor(y / 0x40000))
|
||||||
|
self.index = (self.index + 1) % 624
|
||||||
|
if not a then return y / 0x80000000
|
||||||
|
elseif not b then
|
||||||
|
if a == 0 then return y
|
||||||
|
else return 1 + (y % a)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return a + (y % (b - a + 1))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function twister(s)
|
||||||
|
local temp = {}
|
||||||
|
setmetatable(temp, mersenne_twister)
|
||||||
|
temp.mt = {}
|
||||||
|
temp.index = 0
|
||||||
|
temp:randomseed(s)
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
|
||||||
|
--Linear Congruential Generator
|
||||||
|
local linear_congruential_generator = {}
|
||||||
|
linear_congruential_generator.__index = linear_congruential_generator
|
||||||
|
|
||||||
|
function linear_congruential_generator:random(a, b)
|
||||||
|
local y = (self.a * self.x + self.c) % self.m
|
||||||
|
self.x = y
|
||||||
|
if not a then return y / 0x10000
|
||||||
|
elseif not b then
|
||||||
|
if a == 0 then return y
|
||||||
|
else return 1 + (y % a) end
|
||||||
|
else
|
||||||
|
return a + (y % (b - a + 1))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function linear_congruential_generator:randomseed(s)
|
||||||
|
if not s then s = seed() end
|
||||||
|
self.x = normalize(s)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function lcg(s, r)
|
||||||
|
local temp = {}
|
||||||
|
setmetatable(temp, linear_congruential_generator)
|
||||||
|
temp.a, temp.c, temp.m = 1103515245, 12345, 0x10000 --from Ansi C
|
||||||
|
if r then
|
||||||
|
if r == 'nr' then temp.a, temp.c, temp.m = 1664525, 1013904223, 0x10000 --from Numerical Recipes.
|
||||||
|
elseif r == 'mvc' then temp.a, temp.c, temp.m = 214013, 2531011, 0x10000 end--from MVC
|
||||||
|
end
|
||||||
|
temp:randomseed(s)
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Multiply-with-carry
|
||||||
|
local multiply_with_carry = {}
|
||||||
|
multiply_with_carry.__index = multiply_with_carry
|
||||||
|
|
||||||
|
function multiply_with_carry:random(a, b)
|
||||||
|
local m = self.m
|
||||||
|
local t = self.a * self.x + self.c
|
||||||
|
local y = t % m
|
||||||
|
self.x = y
|
||||||
|
self.c = math_floor(t / m)
|
||||||
|
if not a then return y / 0x10000
|
||||||
|
elseif not b then
|
||||||
|
if a == 0 then return y
|
||||||
|
else return 1 + (y % a) end
|
||||||
|
else
|
||||||
|
return a + (y % (b - a + 1))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function multiply_with_carry:randomseed(s)
|
||||||
|
if not s then s = seed() end
|
||||||
|
self.c = self.ic
|
||||||
|
self.x = normalize(s)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function mwc(s, r)
|
||||||
|
local temp = {}
|
||||||
|
setmetatable(temp, multiply_with_carry)
|
||||||
|
temp.a, temp.c, temp.m = 1103515245, 12345, 0x10000 --from Ansi C
|
||||||
|
if r then
|
||||||
|
if r == 'nr' then temp.a, temp.c, temp.m = 1664525, 1013904223, 0x10000 --from Numerical Recipes.
|
||||||
|
elseif r == 'mvc' then temp.a, temp.c, temp.m = 214013, 2531011, 0x10000 end--from MVC
|
||||||
|
end
|
||||||
|
temp.ic = temp.c
|
||||||
|
temp:randomseed(s)
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
-- Little bind for the methods: My code starts
|
||||||
|
local randomGen={}
|
||||||
|
randomGen.__index=randomGen
|
||||||
|
function randomGen:new(s)
|
||||||
|
local temp={}
|
||||||
|
setmetatable(temp,randomGen)
|
||||||
|
temp[1]=twister()
|
||||||
|
temp[2]=lcg()
|
||||||
|
temp[3]=mwc()
|
||||||
|
temp.pos=1
|
||||||
|
for i=1,3 do
|
||||||
|
temp[i]:randomseed(s)
|
||||||
|
end
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
function randomGen:randomseed(s)
|
||||||
|
self.pos=1
|
||||||
|
self[1]:randomseed(s)
|
||||||
|
self[2]:randomseed(s)
|
||||||
|
self[3]:randomseed(s)
|
||||||
|
end
|
||||||
|
function randomGen:randomInt(a,b)
|
||||||
|
local t=self[self.pos]:random(a,b)
|
||||||
|
self.pos=self.pos+1
|
||||||
|
if self.pos>3 then
|
||||||
|
self.pos=1
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
function randomGen:newND(a,b,s)
|
||||||
|
if not(a) or not(b) then error('You must include a range!') end
|
||||||
|
local temp=randomGen:new(s)
|
||||||
|
temp.a=a
|
||||||
|
temp.b=b
|
||||||
|
temp.range=b-a+1
|
||||||
|
temp.dups={no=0}
|
||||||
|
function temp:nextInt()
|
||||||
|
local t=self:randomInt(self.a,self.b)
|
||||||
|
if self.dups[t]==nil then
|
||||||
|
self.dups[t]=true
|
||||||
|
self.dups.no=self.dups.no+1
|
||||||
|
else
|
||||||
|
return self:nextInt()
|
||||||
|
end
|
||||||
|
if self.dups.no==self.range then
|
||||||
|
function self:nextInt()
|
||||||
|
return 1,true
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
else
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function temp:nextIInt()
|
||||||
|
return function() return self:nextInt() end
|
||||||
|
end
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
return randomGen
|
||||||
99
bin/utils.lua
Normal file
99
bin/utils.lua
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
function table.print(tbl, indent)
|
||||||
|
if not indent then indent = 0 end
|
||||||
|
for k, v in pairs(tbl) do
|
||||||
|
formatting = string.rep(" ", indent) .. k .. ": "
|
||||||
|
if type(v) == "table" then
|
||||||
|
print(formatting)
|
||||||
|
table.print(v, indent+1)
|
||||||
|
elseif type(v) == 'boolean' then
|
||||||
|
print(formatting .. tostring(v))
|
||||||
|
else
|
||||||
|
print(formatting .. tostring(v))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function table.flip(t)
|
||||||
|
local tt={}
|
||||||
|
for i,v in pairs(t) do
|
||||||
|
tt[v]=i
|
||||||
|
end
|
||||||
|
return tt
|
||||||
|
end
|
||||||
|
function io.cleanName(name)
|
||||||
|
name=name:gsub("\\","")
|
||||||
|
name=name:gsub("/","")
|
||||||
|
name=name:gsub(":","")
|
||||||
|
name=name:gsub("*","")
|
||||||
|
name=name:gsub("%?","")
|
||||||
|
name=name:gsub("\"","''")
|
||||||
|
name=name:gsub("<","")
|
||||||
|
name=name:gsub(">","")
|
||||||
|
name=name:gsub("|","")
|
||||||
|
return name
|
||||||
|
end
|
||||||
|
function math.numfix(n,x)
|
||||||
|
local str=tostring(n)
|
||||||
|
if #str<x then
|
||||||
|
str=('0'):rep(x-#str)..str
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
function bin.stripFileName(path)
|
||||||
|
path=path:gsub("\\","/")
|
||||||
|
local npath=path:reverse()
|
||||||
|
a=npath:find("/",1,true)
|
||||||
|
npath=npath:sub(a)
|
||||||
|
npath=npath:reverse()
|
||||||
|
return npath
|
||||||
|
end
|
||||||
|
function bin.randomName(n,ext)
|
||||||
|
n=n or math.random(7,15)
|
||||||
|
if ext then
|
||||||
|
a,b=ext:find('.',1,true)
|
||||||
|
if a and b then
|
||||||
|
ext=ext:sub(2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local str,h = '',0
|
||||||
|
strings = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
|
||||||
|
for i=1,n do
|
||||||
|
h = math.random(1,#strings)
|
||||||
|
str = str..''..strings[h]
|
||||||
|
end
|
||||||
|
return str..'.'..(ext or 'tmp')
|
||||||
|
end
|
||||||
|
function bin.trimNul(str)
|
||||||
|
return str:match("(.-)[%z]*$")
|
||||||
|
end
|
||||||
|
function io.mkDir(dirname)
|
||||||
|
os.execute('mkdir "' .. dirname..'"')
|
||||||
|
end
|
||||||
|
function string.lines(str)
|
||||||
|
local t = {}
|
||||||
|
local function helper(line) table.insert(t, line) return '' end
|
||||||
|
helper((str:gsub('(.-)\r?\n', helper)))
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
function log(data,name,fmt)
|
||||||
|
if name then
|
||||||
|
name=io.cleanName(name)
|
||||||
|
end
|
||||||
|
if not bin.logger then
|
||||||
|
bin.logger = bin.stream(name or 'lua.log',false)
|
||||||
|
elseif bin.logger and name then
|
||||||
|
bin.logger:close()
|
||||||
|
bin.logger = bin.stream(name or 'lua.log',false)
|
||||||
|
end
|
||||||
|
local d=os.date('*t',os.time())
|
||||||
|
bin.logger:tackE((fmt or '['..math.numfix(d.month,2)..'-'..math.numfix(d.day,2)..'-'..d.year..'|'..math.numfix(d.hour,2)..':'..math.numfix(d.min,2)..':'..math.numfix(d.sec,2)..']\t')..data..'\r\n')
|
||||||
|
end
|
||||||
|
function table.max(t)
|
||||||
|
if #t == 0 then return end
|
||||||
|
local value = t[1]
|
||||||
|
for i = 2, #t do
|
||||||
|
if (value < t[i]) then
|
||||||
|
value = t[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return value
|
||||||
|
end
|
||||||
@ -1,488 +0,0 @@
|
|||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{1
|
|
||||||
--
|
|
||||||
-- File Name: bigrat.lua
|
|
||||||
-- Package Name: BigRat
|
|
||||||
--
|
|
||||||
-- Project: Big Rationals library for Lua
|
|
||||||
-- Mantainers: fmp - Frederico Macedo Pessoa
|
|
||||||
-- msm - Marco Serpa Molinaro
|
|
||||||
--
|
|
||||||
-- History:
|
|
||||||
-- Version Autor Date Notes
|
|
||||||
-- alfa fmp/msm 03/22/2003 Start of Development
|
|
||||||
-- beta fmp/msm 07/11/2003 Release
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Big rationals manipulation library for Lua.
|
|
||||||
-- Uses BigNum Library.
|
|
||||||
-- A Big Rational is a table with a field numerator and a field denominator
|
|
||||||
-- consisting of BigNums whose role is well described by their names.
|
|
||||||
-- It also has a field signal which assumes the values '+' and '-'.
|
|
||||||
--
|
|
||||||
--$.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
|
|
||||||
|
|
||||||
--%%%%%%%% Start of Code %%%%%%%%--
|
|
||||||
|
|
||||||
require( "BigNum" ) ;
|
|
||||||
|
|
||||||
BigRat = {} ;
|
|
||||||
BigRat.mt = {} ;
|
|
||||||
|
|
||||||
--BigRat.new{{{1
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
||||||
--
|
|
||||||
-- Function: New
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Creates a new Big Rational based with numerator num1
|
|
||||||
-- and denominator num2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- num1 - numerator: a string, number or BigNumber.
|
|
||||||
-- num2 - denominator: a string, number or BigNumber.
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- A Big Rational, or a nil value if an error occured.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
function BigRat.new( num1 , num2 ) --{{{2
|
|
||||||
local bigrat = {} ;
|
|
||||||
local f ;
|
|
||||||
setmetatable(bigrat, BigRat.mt) ;
|
|
||||||
if type( num1 ) == "table" then
|
|
||||||
--Check if is a BigRat
|
|
||||||
if num1.num ~= nil and num1.den ~= nil then
|
|
||||||
bigrat.num = BigNum.new( num1.num ) ;
|
|
||||||
bigrat.den = BigNum.new( num1.den ) ;
|
|
||||||
else
|
|
||||||
bigrat.num = BigNum.new( num1 ) ;
|
|
||||||
bigrat.den = BigNum.new( "1" ) ;
|
|
||||||
end
|
|
||||||
elseif num1 ~= nil then
|
|
||||||
if num2 == nil then
|
|
||||||
bigrat.den = BigNum.new( "1" ) ;
|
|
||||||
else
|
|
||||||
bigrat.den = BigNum.new( num2 ) ;
|
|
||||||
end
|
|
||||||
bigrat.num = BigNum.new( num1 ) ;
|
|
||||||
else
|
|
||||||
bigrat.den = BigNum.new( ) ;
|
|
||||||
bigrat.num = BigNum.new( ) ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--Update the signals
|
|
||||||
if bigrat.den.signal == "-" then
|
|
||||||
if bigrat.num.signal == "-" then
|
|
||||||
bigrat.num.signal = "+" ;
|
|
||||||
else
|
|
||||||
bigrat.num.signal = "-" ;
|
|
||||||
end
|
|
||||||
bigrat.den.signal = "+" ;
|
|
||||||
end
|
|
||||||
|
|
||||||
return bigrat ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Functions for metatable %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
--BigRat.mt.sub{{{2
|
|
||||||
function BigRat.mt.sub( num1 , num2 )
|
|
||||||
local temp = BigRat.new() ;
|
|
||||||
local brat1 = BigRat.new( num1 ) ;
|
|
||||||
local brat2 = BigRat.new( num2 ) ;
|
|
||||||
BigRat.sub( brat1 , brat2 , temp ) ;
|
|
||||||
return temp ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.add{{{2
|
|
||||||
function BigRat.mt.add( num1 , num2 )
|
|
||||||
local temp = BigRat.new() ;
|
|
||||||
local brat1 = BigRat.new( num1 ) ;
|
|
||||||
local brat2 = BigRat.new( num2 ) ;
|
|
||||||
BigRat.add( brat1 , brat2 , temp ) ;
|
|
||||||
return temp ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.mul{{{2
|
|
||||||
function BigRat.mt.mul( num1 , num2 )
|
|
||||||
local temp = BigRat.new() ;
|
|
||||||
local brat1 = BigRat.new( num1 ) ;
|
|
||||||
local brat2 = BigRat.new( num2 ) ;
|
|
||||||
BigRat.mul( brat1 , brat2 , temp ) ;
|
|
||||||
return temp ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.div{{{2
|
|
||||||
function BigRat.mt.div( num1 , num2 )
|
|
||||||
local brat1 = BigRat.new( num1 ) ;
|
|
||||||
local brat2 = BigRat.new( num2 ) ;
|
|
||||||
local brat3 = BigRat.new() ;
|
|
||||||
local brat4 = BigRat.new() ;
|
|
||||||
BigRat.div( brat1 , brat2 , brat3 , brat4 ) ;
|
|
||||||
return brat3 , brat4 ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.tostring{{{2
|
|
||||||
function BigRat.mt.tostring( brat )
|
|
||||||
BigRat.simplify( brat ) ;
|
|
||||||
return BigNum.mt.tostring( brat.num ) .. " / " .. BigNum.mt.tostring( brat.den ) ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.pow{{{2
|
|
||||||
function BigRat.mt.pow ( num1 , num2 )
|
|
||||||
local brat1 = BigRat.new( num1 ) ;
|
|
||||||
local brat2 = BigRat.new( num2 ) ;
|
|
||||||
return BigRat.pow( brat1 , brat2 )
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.eq{{{2
|
|
||||||
function BigRat.mt.eq ( num1 , num2 )
|
|
||||||
return BigRat.eq( num1 , num2 )
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.lt{{{2
|
|
||||||
function BigRat.mt.lt ( num1 , num2 )
|
|
||||||
return BigRat.lt( num1 , num2 )
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.le{{{2
|
|
||||||
function BigRat.mt.le ( num1 , num2 )
|
|
||||||
return BigRat.le( num1 , num2 )
|
|
||||||
end
|
|
||||||
|
|
||||||
--BigRat.mt.unm{{{2
|
|
||||||
function BigRat.mt.unm ( num )
|
|
||||||
local ret = BigRat.new( num )
|
|
||||||
if ret.num.signal == '-' then
|
|
||||||
ret.num.signal = '+'
|
|
||||||
else
|
|
||||||
ret.num.signal = '-'
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Metatable Definitions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
|
|
||||||
BigRat.mt.__metatable = "hidden" ; -- answer to getmetatable(aBignum)
|
|
||||||
-- BigRat.mt.__index = "inexistent field" ; -- attempt to acess nil valued field
|
|
||||||
-- BigRat.mt.__newindex = "not available" ; -- attempt to create new field
|
|
||||||
BigRat.mt.__tostring = BigRat.mt.tostring ;
|
|
||||||
-- arithmetics
|
|
||||||
BigRat.mt.__add = BigRat.mt.add ;
|
|
||||||
BigRat.mt.__sub = BigRat.mt.sub ;
|
|
||||||
BigRat.mt.__mul = BigRat.mt.mul ;
|
|
||||||
BigRat.mt.__div = BigRat.mt.div ;
|
|
||||||
BigRat.mt.__pow = BigRat.mt.pow ;
|
|
||||||
BigRat.mt.__unm = BigRat.mt.unm ;
|
|
||||||
-- Comparisons
|
|
||||||
BigRat.mt.__eq = BigRat.mt.eq ;
|
|
||||||
BigRat.mt.__le = BigRat.mt.le ;
|
|
||||||
BigRat.mt.__lt = BigRat.mt.lt ;
|
|
||||||
--concatenation
|
|
||||||
-- BigRat.me.__concat = ???
|
|
||||||
|
|
||||||
-- protect metatable BigRat.mt
|
|
||||||
setmetatable( BigRat.mt, { __index = "inexistent field", __newindex = "not available", __metatable="hidden" } ) ;
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Basic Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: ADD
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Adds two Big Rationals.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1, brat2 - Rationals to be added.
|
|
||||||
-- brat3 - result
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- brat3 is the result of the sum.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--Funcao BigRat.add{{{2
|
|
||||||
function BigRat.add( brat1 , brat2 , brat3 )
|
|
||||||
brat3.den = brat1.den * brat2.den ;
|
|
||||||
brat3.num = ( brat1.num * brat2.den ) + ( brat2.num * brat1.den ) ;
|
|
||||||
return brat3 ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: SUB
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Subtracts two Big Rationals.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1, brat2 - Rationals to be subtracted.
|
|
||||||
-- brat3 - result
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- brat3 is the result of the subtraction.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--Funcao BigRat.sub{{{2
|
|
||||||
function BigRat.sub( brat1 , brat2 , brat3 )
|
|
||||||
brat3.den = brat1.den * brat2.den ;
|
|
||||||
brat3.num = ( brat1.num * brat2.den ) - ( brat2.num * brat1.den ) ;
|
|
||||||
return brat3 ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: MUL
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Multiplies two Big Rationals.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1, brat2 - Rationals to be multiplied.
|
|
||||||
-- brat3 - result
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- brat3 is the result of the multiplication.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigRat.mul{{{2
|
|
||||||
function BigRat.mul( brat1 , brat2 , brat3 )
|
|
||||||
brat3.num = brat1.num * brat2.num ;
|
|
||||||
brat3.den = brat1.den * brat2.den ;
|
|
||||||
return 0 ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: DIV
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Divides brat1 by brat2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1, brat2 - Rationals to be divided.
|
|
||||||
-- brat3 - result
|
|
||||||
-- brat4 - remainder
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0
|
|
||||||
--
|
|
||||||
-- Exit assertions:
|
|
||||||
-- brat3 is the result of the division.
|
|
||||||
-- brat4 is the remainder of the division.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigRat.div{{{2
|
|
||||||
function BigRat.div( brat1 , brat2 , brat3 )
|
|
||||||
brat3.num = brat1.num * brat2.den ;
|
|
||||||
brat3.den = brat1.den * brat2.num ;
|
|
||||||
return brat3 ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Compound Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: POW / EXP
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Computes a big rational which represents the brat2-th power of brat1.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1 - base
|
|
||||||
-- brat2 - expoent
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns a big rational which represents the brat2-th power of bnum1.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigRat.pow{{{2
|
|
||||||
function BigRat.pow( bnum1 , bnum2 )
|
|
||||||
if bnum1 == nil or bnum2 == nil then
|
|
||||||
error( "Function BigRat.pow: parameter nil" ) ;
|
|
||||||
end
|
|
||||||
local x = BigRat.new( "8" ) ;
|
|
||||||
local n = BigRat.new( bnum2.den ) ;
|
|
||||||
local n2 ;
|
|
||||||
local y = BigRat.new( ) ;
|
|
||||||
local i ;
|
|
||||||
local temp = BigRat.new( ) ;
|
|
||||||
|
|
||||||
BigRat.simplify( bnum2 ) ;
|
|
||||||
--Powering...
|
|
||||||
temp.num = BigNum.exp( bnum1.num , bnum2.num ) ;
|
|
||||||
temp.den = BigNum.exp( bnum1.den , bnum2.num ) ;
|
|
||||||
|
|
||||||
--Root extraction...
|
|
||||||
--First aprox.
|
|
||||||
n2 = n - 1 ;
|
|
||||||
|
|
||||||
for i = 0 , 4 do
|
|
||||||
y.num = x.num ^ n2.num ;
|
|
||||||
y.den = x.den ^ n2.num ;
|
|
||||||
x = (( temp / y ) + ( n2 * x )) / n ;
|
|
||||||
end
|
|
||||||
return x ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: Simplify
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Divides numerator and denominator of a Big Rational by their G.C.D.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat - Rational to be simplified.
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 0 - OK.
|
|
||||||
-- 1 - otherwise.
|
|
||||||
--
|
|
||||||
-- Exit Assertion:
|
|
||||||
-- brat is simplified.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigRat.simplify{{{2
|
|
||||||
function BigRat.simplify( brat )
|
|
||||||
if brat == nil then
|
|
||||||
error( "Function BigRat.simplify: parameter nil" ) ;
|
|
||||||
end
|
|
||||||
local gcd = BigNum.new( ) ;
|
|
||||||
local temp = BigRat.new( brat ) ;
|
|
||||||
local devnull = BigNum.new( ) ;
|
|
||||||
local zero = BigNum.new( "0" ) ;
|
|
||||||
--Check if numerator is zero
|
|
||||||
if BigNum.compareAbs( brat.num , zero ) == 0 then
|
|
||||||
brat.den = BigNum.new( "1" ) ;
|
|
||||||
return 0 ;
|
|
||||||
end
|
|
||||||
gcd = BigNum.gcd( brat.num , brat.den ) ;
|
|
||||||
BigNum.div( temp.num , gcd , brat.num , devnull ) ;
|
|
||||||
BigNum.div( temp.den , gcd , brat.den , devnull ) ;
|
|
||||||
--Update the signal
|
|
||||||
if brat.num.signal == '-' and brat.den.signal == '-' then
|
|
||||||
brat.num.signal = '+' ;
|
|
||||||
brat.den.signal = '+' ;
|
|
||||||
end
|
|
||||||
return 0 ;
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%% Comparison Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: EQ
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Compares two Big Rationals.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1,brat2 - Rationals
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns true if they are equal or false otherwise.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigRat.eq{{{2
|
|
||||||
function BigRat.eq( brat1 , brat2 )
|
|
||||||
if BigRat.compare( brat1 , brat2 ) == 0 then
|
|
||||||
return true ;
|
|
||||||
else
|
|
||||||
return false ;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: LT
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Verifies if bnum1 is lesser than bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1, brat2 - Rationals
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns true if brat1 is lesser than brat2 or false otherwise.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigRat.lt{{{2
|
|
||||||
function BigRat.lt( brat1 , brat2 )
|
|
||||||
if BigRat.compare( brat1 , brat2 ) == 2 then
|
|
||||||
return true ;
|
|
||||||
else
|
|
||||||
return false ;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: LE
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Verifies if bnum1 is lesser or equal than bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1, brat2 - Rationals
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- Returns true if brat1 is lesser or equal than brat2 or false otherwise.
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigRat.le{{{2
|
|
||||||
function BigRat.le( brat1 , brat2 )
|
|
||||||
local temp = -1 ;
|
|
||||||
temp = BigRat.compare( brat1 , brat2 )
|
|
||||||
if temp == 0 or temp == 2 then
|
|
||||||
return true ;
|
|
||||||
else
|
|
||||||
return false ;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{2
|
|
||||||
--
|
|
||||||
-- Function: Compare
|
|
||||||
--
|
|
||||||
--
|
|
||||||
-- Description:
|
|
||||||
-- Compares values of bnum1 and bnum2.
|
|
||||||
--
|
|
||||||
-- Parameters:
|
|
||||||
-- brat1, brat2 - Rationals
|
|
||||||
--
|
|
||||||
-- Returns:
|
|
||||||
-- 1 - |brat1| > |brat2|
|
|
||||||
-- 2 - |brat1| < |brat2|
|
|
||||||
-- 0 - |brat1| = |brat2|
|
|
||||||
--
|
|
||||||
-- %%%%%%%% --
|
|
||||||
--BigNum.compare{{{2
|
|
||||||
function BigRat.compare( bnum1 , bnum2 )
|
|
||||||
local temp ;
|
|
||||||
temp = bnum1 - bnum2 ;
|
|
||||||
if temp.num[0] == 0 and temp.num.len == 1 then --Check if is zero
|
|
||||||
return 0 ;
|
|
||||||
elseif temp.num.signal == "-" then
|
|
||||||
return 2 ;
|
|
||||||
else
|
|
||||||
return 1 ;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,374 +0,0 @@
|
|||||||
function table.print(tbl, indent)
|
|
||||||
if not indent then indent = 0 end
|
|
||||||
for k, v in pairs(tbl) do
|
|
||||||
formatting = string.rep(" ", indent) .. k .. ": "
|
|
||||||
if type(v) == "table" then
|
|
||||||
print(formatting)
|
|
||||||
table.print(v, indent+1)
|
|
||||||
elseif type(v) == 'boolean' then
|
|
||||||
print(formatting .. tostring(v))
|
|
||||||
else
|
|
||||||
print(formatting .. tostring(v))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function table.flip(t)
|
|
||||||
local tt={}
|
|
||||||
for i,v in pairs(t) do
|
|
||||||
tt[v]=i
|
|
||||||
end
|
|
||||||
return tt
|
|
||||||
end
|
|
||||||
function io.cleanName(name)
|
|
||||||
name=name:gsub("\\","")
|
|
||||||
name=name:gsub("/","")
|
|
||||||
name=name:gsub(":","")
|
|
||||||
name=name:gsub("*","")
|
|
||||||
name=name:gsub("%?","")
|
|
||||||
name=name:gsub("\"","''")
|
|
||||||
name=name:gsub("<","")
|
|
||||||
name=name:gsub(">","")
|
|
||||||
name=name:gsub("|","")
|
|
||||||
return name
|
|
||||||
end
|
|
||||||
function math.numfix(n,x)
|
|
||||||
local str=tostring(n)
|
|
||||||
if #str<x then
|
|
||||||
str=('0'):rep(x-#str)..str
|
|
||||||
end
|
|
||||||
return str
|
|
||||||
end
|
|
||||||
function bin.stripFileName(path)
|
|
||||||
path=path:gsub("\\","/")
|
|
||||||
local npath=path:reverse()
|
|
||||||
a=npath:find("/",1,true)
|
|
||||||
npath=npath:sub(a)
|
|
||||||
npath=npath:reverse()
|
|
||||||
return npath
|
|
||||||
end
|
|
||||||
function io.mkDir(dirname)
|
|
||||||
os.execute('mkdir "' .. dirname..'"')
|
|
||||||
end
|
|
||||||
function string.lines(str)
|
|
||||||
local t = {}
|
|
||||||
local function helper(line) table.insert(t, line) return '' end
|
|
||||||
helper((str:gsub('(.-)\r?\n', helper)))
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
function log(data,name,fmt)
|
|
||||||
if name then
|
|
||||||
name=io.cleanName(name)
|
|
||||||
end
|
|
||||||
if not bin.logger then
|
|
||||||
bin.logger = bin.stream(name or 'lua.log',false)
|
|
||||||
elseif bin.logger and name then
|
|
||||||
bin.logger:close()
|
|
||||||
bin.logger = bin.stream(name or 'lua.log',false)
|
|
||||||
end
|
|
||||||
local d=os.date('*t',os.time())
|
|
||||||
bin.logger:tackE((fmt or '['..math.numfix(d.month,2)..'-'..math.numfix(d.day,2)..'-'..d.year..'|'..math.numfix(d.hour,2)..':'..math.numfix(d.min,2)..':'..math.numfix(d.sec,2)..']\t')..data..'\r\n')
|
|
||||||
end
|
|
||||||
function io.mkFile(filename,data,tp)
|
|
||||||
if not(tp) then tp='wb' end
|
|
||||||
if not(data) then data='' end
|
|
||||||
file = io.open(filename, tp)
|
|
||||||
if file==nil then return end
|
|
||||||
file:write(data)
|
|
||||||
file:close()
|
|
||||||
end
|
|
||||||
function io.getWorkingDir()
|
|
||||||
return io.popen'cd':read'*l'
|
|
||||||
end
|
|
||||||
function io.getAllItems(dir)
|
|
||||||
local t=os.capture("cd \""..dir.."\" & dir /a-d | find",true):lines()
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
function os._getOS()
|
|
||||||
if package.config:sub(1,1)=='\\' then
|
|
||||||
return 'windows'
|
|
||||||
else
|
|
||||||
return 'unix'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function os.getOS(t)
|
|
||||||
if not t then
|
|
||||||
return os._getOS()
|
|
||||||
end
|
|
||||||
if os._getOS()=='unix' then
|
|
||||||
fh,err = io.popen('uname -o 2>/dev/null','r')
|
|
||||||
if fh then
|
|
||||||
osname = fh:read()
|
|
||||||
end
|
|
||||||
if osname then return osname end
|
|
||||||
end
|
|
||||||
local winver='Unknown Version'
|
|
||||||
local a,b,c=os.capture('ver'):match('(%d+).(%d+).(%d+)')
|
|
||||||
local win=a..'.'..b..'.'..c
|
|
||||||
if type(t)=='string' then
|
|
||||||
win=t
|
|
||||||
end
|
|
||||||
if win=='4.00.950' then
|
|
||||||
winver='95'
|
|
||||||
elseif win=='4.00.1111' then
|
|
||||||
winver='95 OSR2'
|
|
||||||
elseif win=='4.00.1381' then
|
|
||||||
winver='NT 4.0'
|
|
||||||
elseif win=='4.10.1998' then
|
|
||||||
winver='98'
|
|
||||||
elseif win=='4.10.2222' then
|
|
||||||
winver='98 SE'
|
|
||||||
elseif win=='4.90.3000' then
|
|
||||||
winver='ME'
|
|
||||||
elseif win=='5.00.2195' then
|
|
||||||
winver='2000'
|
|
||||||
elseif win=='5.1.2600' then
|
|
||||||
winver='XP'
|
|
||||||
elseif win=='5.2.3790' then
|
|
||||||
winver='Server 2003'
|
|
||||||
elseif win=='6.0.6000' then
|
|
||||||
winver='Vista/Windows Server 2008'
|
|
||||||
elseif win=='6.0.6002' then
|
|
||||||
winver='Vista SP2'
|
|
||||||
elseif win=='6.1.7600' then
|
|
||||||
winver='7/Windows Server 2008 R2'
|
|
||||||
elseif win=='6.1.7601' then
|
|
||||||
winver='7 SP1/Windows Server 2008 R2 SP1'
|
|
||||||
elseif win=='6.2.9200' then
|
|
||||||
winver='8/Windows Server 2012'
|
|
||||||
elseif win=='6.3.9600' then
|
|
||||||
winver='8.1/Windows Server 2012'
|
|
||||||
elseif win=='6.4.9841' then
|
|
||||||
winver='10 Technical Preview 1'
|
|
||||||
elseif win=='6.4.9860' then
|
|
||||||
winver='10 Technical Preview 2'
|
|
||||||
elseif win=='6.4.9879' then
|
|
||||||
winver='10 Technical Preview 3'
|
|
||||||
elseif win=='10.0.9926' then
|
|
||||||
winver='10 Technical Preview 4'
|
|
||||||
end
|
|
||||||
return 'Windows '..winver
|
|
||||||
end
|
|
||||||
function os.capture(cmd, raw)
|
|
||||||
local f = assert(io.popen(cmd, 'r'))
|
|
||||||
local s = assert(f:read('*a'))
|
|
||||||
f:close()
|
|
||||||
if raw then return s end
|
|
||||||
s = string.gsub(s, '^%s+', '')
|
|
||||||
s = string.gsub(s, '%s+$', '')
|
|
||||||
s = string.gsub(s, '[\n\r]+', ' ')
|
|
||||||
return s
|
|
||||||
end
|
|
||||||
function io.scanDir(directory)
|
|
||||||
directory=directory or io.getDir()
|
|
||||||
local i, t, popen = 0, {}, io.popen
|
|
||||||
if os.getOS()=='unix' then
|
|
||||||
for filename in popen('ls -a "'..directory..'"'):lines() do
|
|
||||||
i = i + 1
|
|
||||||
t[i] = filename
|
|
||||||
end
|
|
||||||
else
|
|
||||||
for filename in popen('dir "'..directory..'" /b'):lines() do
|
|
||||||
i = i + 1
|
|
||||||
t[i] = filename
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
function io.getDir(dir)
|
|
||||||
if not dir then return io.getWorkingDir() end
|
|
||||||
if os.getOS()=='unix' then
|
|
||||||
return os.capture('cd '..dir..' ; cd')
|
|
||||||
else
|
|
||||||
return os.capture('cd '..dir..' & cd')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function string.split(str, pat)
|
|
||||||
local t = {} -- NOTE: use {n = 0} in Lua-5.0
|
|
||||||
local fpat = '(.-)' .. pat
|
|
||||||
local last_end = 1
|
|
||||||
local s, e, cap = str:find(fpat, 1)
|
|
||||||
while s do
|
|
||||||
if s ~= 1 or cap ~= '' then
|
|
||||||
table.insert(t,cap)
|
|
||||||
end
|
|
||||||
last_end = e+1
|
|
||||||
s, e, cap = str:find(fpat, last_end)
|
|
||||||
end
|
|
||||||
if last_end <= #str then
|
|
||||||
cap = str:sub(last_end)
|
|
||||||
table.insert(t, cap)
|
|
||||||
end
|
|
||||||
return t
|
|
||||||
end
|
|
||||||
function io.fileExists(path)
|
|
||||||
g=io.open(path or '','r')
|
|
||||||
if path =='' then
|
|
||||||
p='empty path'
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
if g~=nil and true or false then
|
|
||||||
p=(g~=nil and true or false)
|
|
||||||
end
|
|
||||||
if g~=nil then
|
|
||||||
io.close(g)
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
return p
|
|
||||||
end
|
|
||||||
function io.getDirectories(dir,l)
|
|
||||||
if dir then
|
|
||||||
dir=dir..'\\'
|
|
||||||
else
|
|
||||||
dir=''
|
|
||||||
end
|
|
||||||
local temp2=io.scanDir(dir)
|
|
||||||
for i=#temp2,1,-1 do
|
|
||||||
if io.fileExists(dir..temp2[i]) then
|
|
||||||
table.remove(temp2,i)
|
|
||||||
elseif l then
|
|
||||||
temp2[i]=dir..temp2[i]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return temp2
|
|
||||||
end
|
|
||||||
function io.getFiles(dir,l)
|
|
||||||
if dir then
|
|
||||||
dir=dir..'\\'
|
|
||||||
else
|
|
||||||
dir=''
|
|
||||||
end
|
|
||||||
local temp2=io.scanDir(dir)
|
|
||||||
for i=#temp2,1,-1 do
|
|
||||||
if io.dirExists(dir..temp2[i]) then
|
|
||||||
table.remove(temp2,i)
|
|
||||||
elseif l then
|
|
||||||
temp2[i]=dir..temp2[i]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return temp2
|
|
||||||
end
|
|
||||||
function io.readFile(file)
|
|
||||||
local f = io.open(file, 'rb')
|
|
||||||
local content = f:read('*all')
|
|
||||||
f:close()
|
|
||||||
return content
|
|
||||||
end
|
|
||||||
function table.print(tbl, indent)
|
|
||||||
if not indent then indent = 0 end
|
|
||||||
for k, v in pairs(tbl) do
|
|
||||||
formatting = string.rep(' ', indent) .. k .. ': '
|
|
||||||
if type(v) == 'table' then
|
|
||||||
print(formatting)
|
|
||||||
table.print(v, indent+1)
|
|
||||||
else
|
|
||||||
print(formatting .. tostring(v))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function io.dirExists(strFolderName)
|
|
||||||
strFolderName = strFolderName or io.getDir()
|
|
||||||
local fileHandle, strError = io.open(strFolderName..'\\*.*','r')
|
|
||||||
if fileHandle ~= nil then
|
|
||||||
io.close(fileHandle)
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
if string.match(strError,'No such file or directory') then
|
|
||||||
return false
|
|
||||||
else
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function io.getFullName(name)
|
|
||||||
local temp=name or arg[0]
|
|
||||||
if string.find(temp,'\\',1,true) or string.find(temp,'/',1,true) then
|
|
||||||
temp=string.reverse(temp)
|
|
||||||
a,b=string.find(temp,'\\',1,true)
|
|
||||||
if not(a) or not(b) then
|
|
||||||
a,b=string.find(temp,'/',1,true)
|
|
||||||
end
|
|
||||||
return string.reverse(string.sub(temp,1,b-1))
|
|
||||||
end
|
|
||||||
return temp
|
|
||||||
end
|
|
||||||
function io.getName(file)
|
|
||||||
local name=io.getFullName(file)
|
|
||||||
name=string.reverse(name)
|
|
||||||
a,b=string.find(name,'.',1,true)
|
|
||||||
name=string.sub(name,a+1,-1)
|
|
||||||
return string.reverse(name)
|
|
||||||
end
|
|
||||||
function io.getPathName(path)
|
|
||||||
return path:sub(1,#path-#io.getFullName(path))
|
|
||||||
end
|
|
||||||
function table.merge(t1, t2)
|
|
||||||
for k,v in pairs(t2) do
|
|
||||||
if type(v) == 'table' then
|
|
||||||
if type(t1[k] or false) == 'table' then
|
|
||||||
table.merge(t1[k] or {}, t2[k] or {})
|
|
||||||
else
|
|
||||||
t1[k] = v
|
|
||||||
end
|
|
||||||
else
|
|
||||||
t1[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return t1
|
|
||||||
end
|
|
||||||
function io.splitPath(str)
|
|
||||||
return string.split(str,'[\\/]+')
|
|
||||||
end
|
|
||||||
function io.pathToTable(path)
|
|
||||||
local p=io.splitPath(path)
|
|
||||||
local temp={}
|
|
||||||
temp[p[1]]={}
|
|
||||||
local last=temp[p[1]]
|
|
||||||
for i=2,#p do
|
|
||||||
snd=last
|
|
||||||
last[p[i]]={}
|
|
||||||
last=last[p[i]]
|
|
||||||
end
|
|
||||||
return temp,last,snd
|
|
||||||
end
|
|
||||||
function io.parseDir(dir,t)
|
|
||||||
io.tempFiles={}
|
|
||||||
function _p(dir)
|
|
||||||
local dirs=io.getDirectories(dir,true)
|
|
||||||
local files=io.getFiles(dir,true)
|
|
||||||
for i=1,#files do
|
|
||||||
p,l,s=io.pathToTable(files[i])
|
|
||||||
if t then
|
|
||||||
s[io.getFullName(files[i])]=io.readFile(files[i])
|
|
||||||
else
|
|
||||||
s[io.getFullName(files[i])]=io.open(files[i],'r+')
|
|
||||||
end
|
|
||||||
table.merge(io.tempFiles,p)
|
|
||||||
end
|
|
||||||
for i=1,#dirs do
|
|
||||||
table.merge(io.tempFiles,io.pathToTable(dirs[i]))
|
|
||||||
_p(dirs[i],t)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
_p(dir)
|
|
||||||
return io.tempFiles
|
|
||||||
end
|
|
||||||
function io.parsedir(dir,f)
|
|
||||||
io.tempFiles={}
|
|
||||||
function _p(dir,f)
|
|
||||||
local dirs=io.getDirectories(dir,true)
|
|
||||||
local files=io.getFiles(dir,true)
|
|
||||||
for i=1,#files do
|
|
||||||
if not f then
|
|
||||||
table.insert(io.tempFiles,files[i])
|
|
||||||
else
|
|
||||||
f(files[i])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
for i=1,#dirs do
|
|
||||||
_p(dirs[i],f)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
_p(dir,f)
|
|
||||||
return io.tempFiles
|
|
||||||
end
|
|
||||||
File diff suppressed because it is too large
Load Diff
@ -1,123 +0,0 @@
|
|||||||
nil = log(data,name,fmt) -- data is the text that you want to log to a file, the name argument only needs to be called with the first log. It tells where to log to. If name is used again it will change the location of the log file.
|
|
||||||
string,string,string = bin.getLuaVersion() -- returns PUC/JIT,major,minor
|
|
||||||
|
|
||||||
Constructors
|
|
||||||
------------
|
|
||||||
binobj = bin.load(filename,s,r) -- creates binobj from file in s and r nil then reads entire file but if not s is the start point of reading and r is either the #to read after s or from s to '#' (like string.sub())
|
|
||||||
binobj = bin.new(string data) -- creates binobj from a string
|
|
||||||
binobj = bin.stream(file,lock) -- creates a streamable binobj lock is defult to true if locked file is read only
|
|
||||||
binobj = bin.newTempFile(data) -- creates a tempfile in stream mode
|
|
||||||
bitobj = bits.new(n) -- creates bitobj from a number
|
|
||||||
vfs = bin.newVFS() -- creates a new virtual file system --Beta
|
|
||||||
vfs = bin.loadVFS(path) -- loads a saved .lvfs file --Beta
|
|
||||||
buf = bin:newDataBuffer(s) -- creates a databuffer
|
|
||||||
binobj = bin.bufferToBin(b) -- converts a buffer object to a bin object
|
|
||||||
buf = bin.binToBuffer(b) -- converts a bin object to a buffer obj
|
|
||||||
buf = bin:getDataBuffer(a,b) -- gets a speical buffer that opperates on a streamed file. It works just like a regular data buffer
|
|
||||||
blockWriter = bin.newNamedBlock(indexSize) -- returns a block writer object index size is the size of the index where labels and pointers are stored
|
|
||||||
blockWriter = bin.newStreamedNamedBlock(indexSize,path) -- returns a streamed version of the above path is the path to write the file
|
|
||||||
blockReader = bin.loadNamedBlock(path) -- returns a block reader object, path is where the file is located
|
|
||||||
blockHandler= bin.namedBlockManager(arg) -- returns a block handler object, if arg is a string it will loade a named block file, if its a number or nil it will create a nambed block object
|
|
||||||
|
|
||||||
Note: the blockWriter that isn't streamed needs to have tofile(path) called on it to write it to a file
|
|
||||||
Note: the streamed blockWriter must have the close method used when you are done writing to it!
|
|
||||||
|
|
||||||
Helpers
|
|
||||||
-------
|
|
||||||
string = bin.randomName(n,ext) -- creates a random file name if n and ext is nil then a random length is used, and '.tmp' extension is added
|
|
||||||
string = bin.NumtoHEX(n) -- turns number into hex
|
|
||||||
binobj = bin.HEXtoBin(s)*D -- turns hex data into binobj
|
|
||||||
string = bin.HEXtoStr(s)*D -- turns hex data into string/text
|
|
||||||
string = bin.tohex(s) -- turns string to hex
|
|
||||||
string = bin.fromhex(s) -- turns hex to string
|
|
||||||
string = bin.endianflop(data) -- flips the high order bits to the low order bits and viseversa
|
|
||||||
string = bin.getVersion() -- returns the version as a string
|
|
||||||
string = bin.escapeStr(str) -- system function that turns functions into easy light
|
|
||||||
string = bin.ToStr(tab) -- turns a table into a string (even functions are dumped; used to create compact data files)
|
|
||||||
nil = bin.packLLIB(name,tab,ext) -- turns a bunch of 'files' into 1 file tab is a table of file names, ext is extension if nil .llib is used Note: Currently does not support directories within .llib
|
|
||||||
nil = bin.unpackLLIB(name,exe,todir,over,ext) -- takes that file and makes the files Note: if exe is true and a .lua file is in the .llib archive than it is ran after extraction ext is extension if nil .llib is used
|
|
||||||
boolean = bin.fileExist(path) -- returns true if the file exist false otherwise
|
|
||||||
boolean*= bin.closeto(a,b,v) -- test data to see how close it is (a,b=tested data v=#difference (v must be <=255))
|
|
||||||
String = bin.textToBinary(txt) -- turns text into binary data 10101010's
|
|
||||||
binobj = bin.decodeBits(bindata) -- turns binary data into text
|
|
||||||
string = bin.trimNul(s) -- terminates string at the nul char
|
|
||||||
number = bin.getIndexSize(tab) -- used to get the index size of labels given to a named block
|
|
||||||
string = bits.numToBytes(num,occ) -- returns the number in base256 string data, occ is the space the number will take up
|
|
||||||
|
|
||||||
Assessors
|
|
||||||
---------
|
|
||||||
nil*** = binobj:tofile(filename) -- writes binobj data as a file
|
|
||||||
binobj* = binobj:clone() -- clones and returns a binobj
|
|
||||||
number* = binobj:compare(other binobj,diff) -- returns 0-100 % of simularity based on diff factor (diff must be <=255)
|
|
||||||
string = binobj:sub(a,b) -- returns string data like segment but dosen't alter the binobject
|
|
||||||
num,num = binobj:tonumber(a,b) -- converts from a-b (if a and b are nil it uses the entire binobj) into a base 10 number so 'AXG' in data becomes 4675649 returns big,little endian
|
|
||||||
number = binobj:getbyte(n) -- gets byte at location and converts to base 10 number
|
|
||||||
bitobj = binobj:tobits(i) -- returns the 8bits of data as a bitobj Ex: if value of byte was a 5 it returns a bitobj with a value of: '00000101'
|
|
||||||
string = binobj:getHEX(a,b) -- gets the HEX data from 'a' to 'b' if both a,b are nil returns entire file as hex
|
|
||||||
a,b = binobj:scan(s,n,f) -- searches a binobj for 's'; n is where to start looking, 'f' is weather or not to flip the string data entered 's'
|
|
||||||
string = binobj:streamData(a,b) -- reads data from a to b or a can be a data handle... I will explain this and more in offical documentation
|
|
||||||
string# = binobj:streamread(a,b) -- reads data from a stream object between a and b (note: while other functions start at 1 for both stream and non stream 0 is the starting point for this one)
|
|
||||||
boolean = binobj:canStreamWrite() -- returns true if the binobj is streamable and isn't locked
|
|
||||||
string = bitobj:conv(n) -- converts number to binary bits (system used)
|
|
||||||
binobj = bitobj:tobytes() -- converts bit obj into a string byte (0-255)
|
|
||||||
number = bitobj:tonumber() -- converts '10101010' to a number
|
|
||||||
boolean = bitobj:isover() -- returns true if the bits exceed 8 bits false if 8 or less
|
|
||||||
string = bitobj:getBin() -- returns the binary 10100100's of the data as a string
|
|
||||||
string = binobj:getHash(n) -- returns a Hash of a file (This is my own method of hashing btw) n is the length you want the hash to be
|
|
||||||
string = binobj:getData() -- returns the bin object as a string
|
|
||||||
depends = blockReader:getBlock(name) -- returns the value associated with the name, values can be any lua data except userdata
|
|
||||||
|
|
||||||
Mutators (Changes affect the actual object or if streaming the actual file) bin:remove()
|
|
||||||
--------
|
|
||||||
nil = binobj:setEndOfFile(n) -- sets the end of a file
|
|
||||||
nil = binobj:reverse() -- reverses binobj data ex: hello --> olleh
|
|
||||||
nil = binobj:flipbits() -- flips the binary bits
|
|
||||||
nil** = binobj:segment(a,b) -- gets a segment of the binobj data works just like string.sub(a,b) without str
|
|
||||||
nil* = binobj:insert(a,i) -- inserts i (string or number(converts into string)) in position a
|
|
||||||
nil* = binobj:parseN(n) -- removes ever (nth) byte of data
|
|
||||||
nil = binobj:getlength() -- gets length or size of binary data
|
|
||||||
nil* = binobj:shift(n) -- shift the binary data by n positive --> negitive <--
|
|
||||||
nil* = binobj:delete(a,b) -- deletes part of a binobj data Usage: binobj:delete(#) deletes at pos # binobj:delete(#1,#2) deletes from #1 to #2 binobj:delete('string') deletes all instances of 'byte' as a string Use string.char(#) or '\#' to get byte as a string
|
|
||||||
nil* = binobj:encrypt(seed) -- encrypts data using a seed, seed may be left blank
|
|
||||||
nil* = binobj:decrypt(seed) -- decrypts data encrypted with encrypt(seed)
|
|
||||||
nil* = binobj:shuffle() -- Shuffles the data randomly Note: there is no way to get it back!!! If original is needed clone beforehand
|
|
||||||
nil** = binobj:mutate(a,i) -- changes position a's value to i
|
|
||||||
nil = binobj:merge(o,t) -- o is the binobj you are merging if t is true it merges the new data to the left of the binobj EX: b:merge(o,true) b='yo' o='data' output: b='datayo' b:merge(o) b='yo' o='data' output: b='yodata'
|
|
||||||
nil* = binobj:parseA(n,a,t) -- n is every byte where you add, a is the data you are adding, t is true or false true before false after
|
|
||||||
nil = binobj:getHEX(a,b) -- returns the HEX of the bytes between a,b inclusive
|
|
||||||
nil = binobj:cryptM() -- a mirrorable encryptor/decryptor
|
|
||||||
nil = binobj:addBlock(d,n) -- adds a block of data to a binobj s is size d is data e is a bool if true then encrypts string values. if data is larger than 'n' then data is lost. n is the size of bytes the data is Note: n is no longer needed but you must use getBlock(type) to get it back
|
|
||||||
nil = binobj:getBlock(t,n) -- gets block of code by type
|
|
||||||
nil = binobj:seek(n) -- used with getBlock EX below with all 3
|
|
||||||
nil* = binobj:morph(a,b,d) -- changes data between point a and b, inclusive, to d
|
|
||||||
nil = binobj:fill(n,d) -- fills binobj with data 'd' for n
|
|
||||||
nil = binobj:fillrandom(n) -- fills binobj with random data for n
|
|
||||||
nil = binobj:shiftbits(n) -- shifts all bits by n amount
|
|
||||||
nil = binobj:shiftbit(n,i) -- shifts a bit ai index i by n
|
|
||||||
nil# = binobj:streamwrite(d,n) -- writes to the streamable binobj d data n position
|
|
||||||
nil# = binobj:open() -- opens the streamable binobj
|
|
||||||
nil# = binobj:close() -- closes the streamable binobj
|
|
||||||
nil = binobj:wipe() -- erases all data in the file
|
|
||||||
nil* = binobj:tackB(d) -- adds data to the beginning of a file
|
|
||||||
nil = binobj:tackE(d) -- adds data to the end of a file
|
|
||||||
nil = binobj:parse(n,f) -- loops through each byte calling function 'f' with the args(i,binobj,data at i)
|
|
||||||
nil = binobj:flipbit(i) -- flips the binary bit at position i
|
|
||||||
nil* = binobj:gsub() -- just like string:gsub(), but mutates self
|
|
||||||
nil = blockWriter:addNamedBlock(name,value) -- writes a named block to the file with name 'name' and the value 'value'
|
|
||||||
|
|
||||||
Note: numbers are written in Big-endian use bin.endianflop(d) to filp to Little-endian
|
|
||||||
|
|
||||||
Note: binobj:tonumber() returns big,little endian so if printing do: b,l=binobj:tonumber() print(l) print(b)
|
|
||||||
|
|
||||||
nil = bitobj:add(i) -- adds i to the bitobj i can be a number (base 10) or a bitobj
|
|
||||||
nil = bitobj:sub(i) -- subs i to the bitobj i can be a number (base 10) or a bitobj
|
|
||||||
nil = bitobj:multi(i) -- multiplys i to the bitobj i can be a number (base 10) or a bitobj
|
|
||||||
nil = bitobj:div(i) -- divides i to the bitobj i can be a number (base 10) or a bitobj
|
|
||||||
nil = bitobj:flipbits() -- filps the bits 1 --> 0, 0 --> 1
|
|
||||||
string = bitobj:getBin() -- returns 1's & 0's of the bitobj
|
|
||||||
|
|
||||||
# stream objects only
|
|
||||||
* not compatible with stream files
|
|
||||||
** works but do not use with large files or it works to some degree
|
|
||||||
*** in stream objects all changes are made directly to the file, so there is no need to do tofile()
|
|
||||||
*D
|
|
||||||
Binary file not shown.
@ -1,4 +0,0 @@
|
|||||||
package.path="?/init.lua;"..package.path
|
|
||||||
require("bin")
|
|
||||||
test=bin.new(bin.getnumber(12345,4,"%B"))
|
|
||||||
print(test:tonumber())
|
|
||||||
@ -1 +0,0 @@
|
|||||||
Hi!Hi!Hi!Hi!Hi!Hi!
|
|
||||||
@ -1,137 +0,0 @@
|
|||||||
package.path="?/init.lua;"..package.path
|
|
||||||
require("bin")
|
|
||||||
--~ test=bin.new()
|
|
||||||
--~ test:addBlock({
|
|
||||||
--~ test=134534,
|
|
||||||
--~ 23452,
|
|
||||||
--~ 65723,
|
|
||||||
--~ 45744,
|
|
||||||
--~ 523463,
|
|
||||||
--~ test2=6234562,
|
|
||||||
--~ test3="HELLO WORLD!",
|
|
||||||
--~ test4=true,
|
|
||||||
--~ test5=false,
|
|
||||||
--~ test6={a=1,b=2,3,4},
|
|
||||||
--~ test6={c=1345,d=2345,3567,4789,{1,true,false,"HI"},c={1,2,3,"HI2"}},
|
|
||||||
--~ test7=function() print("Hello World!") end
|
|
||||||
--~ },nil,"t")
|
|
||||||
--~ test:addBlock(1234,4)
|
|
||||||
--~ test:tofile("test.dat")
|
|
||||||
--~ test2=bin.load("test.dat")
|
|
||||||
--~ t=test2:getBlock("t")
|
|
||||||
--~ table.print(t)
|
|
||||||
--~ print("-----")
|
|
||||||
--~ print(test2:getBlock("n",4))
|
|
||||||
|
|
||||||
--~ print("bfType:",test:getBlock("s",2))
|
|
||||||
--~ print("bfSize:",test:getBlock("%E",4))
|
|
||||||
--~ print("bfReserved1:",test:getBlock("%E",2))
|
|
||||||
--~ print("bfReserved2:",test:getBlock("%E",2))
|
|
||||||
--~ print("bfOffBits:",test:getBlock("%E",4))
|
|
||||||
--~ print("biSize:",test:getBlock("%E",4))
|
|
||||||
--~ print("biWidth:",test:getBlock("%E",4))
|
|
||||||
--~ print("biHeight:",test:getBlock("%E",4))
|
|
||||||
--~ print("biPlanes:",test:getBlock("%E",2))
|
|
||||||
--~ print("biBitCount:",test:getBlock("%E",2))
|
|
||||||
--~ print("biCompression:",test:getBlock("%E",4))
|
|
||||||
--~ print("biSizeImage:",test:getBlock("%E",4))
|
|
||||||
--~ print("biXPelsPerMeter:",test:getBlock("%E",4))
|
|
||||||
--~ print("biYPelsPerMeter:",test:getBlock("%E",4))
|
|
||||||
--~ print("biClrUsed:",test:getBlock("%E",4))
|
|
||||||
--~ print("biClrImportant:",test:getBlock("%E",4))
|
|
||||||
|
|
||||||
-- allocate space in a file and work with it directly. No need to worry about seeking and such!
|
|
||||||
function bin:newDataBufferFromBin(size,fill) -- fills with \0 or nul or with what you enter IF the nothing exists inside the bin file.
|
|
||||||
--
|
|
||||||
end
|
|
||||||
function bin.newDataBuffer(size,fill) -- fills with \0 or nul or with what you enter
|
|
||||||
local c={}
|
|
||||||
local fill=fill or "\0"
|
|
||||||
c.data={self=c}
|
|
||||||
c.Type="buffer"
|
|
||||||
c.size=size or 0 -- 0 means an infinite buffer, sometimes useful
|
|
||||||
for i=1,size do
|
|
||||||
c.data[i]=fill
|
|
||||||
end
|
|
||||||
local mt={
|
|
||||||
__index=function(t,k)
|
|
||||||
if type(k)=="number" then
|
|
||||||
local data=t.data[k]
|
|
||||||
if data then
|
|
||||||
return string.byte(data)
|
|
||||||
else
|
|
||||||
error("Index out of range!")
|
|
||||||
end
|
|
||||||
elseif type(k)=="string" then
|
|
||||||
local num=tonumber(k)
|
|
||||||
if num then
|
|
||||||
local data=t.data[num]
|
|
||||||
if data then
|
|
||||||
return data
|
|
||||||
else
|
|
||||||
error("Index out of range!")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
error("Only number-strings and numbers can be indexed!")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
error("Only number-strings and numbers can be indexed!")
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
__newindex=function(t,k,v)
|
|
||||||
if type(k)~="number" then error("Can only set a buffers data with a numeric index!") end
|
|
||||||
local data=""
|
|
||||||
if type(v)=="string" then
|
|
||||||
data=v
|
|
||||||
elseif type(v)=="number" then
|
|
||||||
data=string.char(v)
|
|
||||||
else
|
|
||||||
-- try to normalize the data of type v
|
|
||||||
data=bin.normalizeData(v)
|
|
||||||
end
|
|
||||||
t:fillBuffer(k,data)
|
|
||||||
end,
|
|
||||||
__tostring=function(t)
|
|
||||||
return t:getData()
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
function c:fillBuffer(a,data)
|
|
||||||
local len=#data
|
|
||||||
if len==1 then
|
|
||||||
self.data[a]=data
|
|
||||||
else
|
|
||||||
local i=a-1
|
|
||||||
for d in data:gmatch(".") do
|
|
||||||
i=i+1
|
|
||||||
if i>c.size then
|
|
||||||
return #data-i+a
|
|
||||||
end
|
|
||||||
self.data[i]=d
|
|
||||||
end
|
|
||||||
return #data-i+(a-1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function c:getData(a,b,fmt) -- LATER
|
|
||||||
local dat=bin.new(table.concat(self.data,"",a,b))
|
|
||||||
local n=dat:getSize()
|
|
||||||
return dat:getBlock(fmt or "s",n)
|
|
||||||
end
|
|
||||||
function c:getSize()
|
|
||||||
return #self:getData()
|
|
||||||
end
|
|
||||||
setmetatable(c,mt)
|
|
||||||
return c
|
|
||||||
end
|
|
||||||
test=bin.newDataBuffer(16)
|
|
||||||
test[1]=bits.numToBytes(1234,2,"%E")
|
|
||||||
test[5]=bin.new("Hello")
|
|
||||||
test[10]=24
|
|
||||||
print(test:getData(1,4,"n"))
|
|
||||||
print(test["5"])
|
|
||||||
print(test["6"])
|
|
||||||
print(test["7"])
|
|
||||||
print(test["8"])
|
|
||||||
print(test["9"])
|
|
||||||
print(test[10])
|
|
||||||
print(test)
|
|
||||||
print(test:getSize())
|
|
||||||
Loading…
x
Reference in New Issue
Block a user