Updated readme data
This commit is contained in:
parent
7bb2d0750c
commit
5d4a707ca6
@ -1,6 +1,7 @@
|
|||||||
# Bin Rewrite Progress!
|
# Bin Rewrite Progress!
|
||||||
**Note: A lot breaks (Almost everything) for the sake of consistency**
|
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!
|
||||||
Progress: [===- - - - - - - 15% - - - - - - - - -]
|
**Note: A lot breaks (Almost everything)**
|
||||||
|
Progress: [====- - - - - - 20% - - - - - - - - -]
|
||||||
### List of new methods
|
### List of new methods
|
||||||
- [x] bin.newFromBase64(data)
|
- [x] bin.newFromBase64(data)
|
||||||
- [x] bin.newFromBase91(data)
|
- [x] bin.newFromBase91(data)
|
||||||
@ -13,6 +14,8 @@ Progress: [===- - - - - - - 15% - - - - - - - - -]
|
|||||||
- [x] bin.toBase91(s)
|
- [x] bin.toBase91(s)
|
||||||
- [x] bin.fromBase91(s)
|
- [x] bin.fromBase91(s)
|
||||||
- [x] bin.getnumber(num,len,fmt,func)
|
- [x] bin.getnumber(num,len,fmt,func)
|
||||||
|
- [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
|
||||||
|
|
||||||
### List of converted methods and their status
|
### List of converted methods and their status
|
||||||
- [x] log(data,name,fmt)
|
- [x] log(data,name,fmt)
|
||||||
@ -20,9 +23,9 @@ Progress: [===- - - - - - - 15% - - - - - - - - -]
|
|||||||
- [x] bin.load(filename,s,r)
|
- [x] bin.load(filename,s,r)
|
||||||
- [x] bin.new(string data) -- Does not accept b64 or hex data anymore! Use new methods for that ^^^
|
- [x] bin.new(string data) -- Does not accept b64 or hex data anymore! Use new methods for that ^^^
|
||||||
- [x] bin.stream(file,lock)
|
- [x] bin.stream(file,lock)
|
||||||
- [ ] bin.newTempFile(data)
|
- [x] bin.newTempFile(data)
|
||||||
- [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
|
||||||
- [ ] bin:getData(fmt) -- returns the data of the object as a string, supports %x(hex), %X(HEX) and %b(base64)
|
- [x] bin:getData(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)
|
||||||
- [ ] bin.newVFS()
|
- [ ] bin.newVFS()
|
||||||
- [x] bin:tackE(data) -- tacks data onto the end of a file
|
- [x] bin:tackE(data) -- tacks data onto the end of a file
|
||||||
@ -43,7 +46,7 @@ Progress: [===- - - - - - - 15% - - - - - - - - -]
|
|||||||
- [x] bin.tohex(s)
|
- [x] bin.tohex(s)
|
||||||
- [x] bin.fromhex(s)
|
- [x] bin.fromhex(s)
|
||||||
- [x] bin.endianflop(data)
|
- [x] bin.endianflop(data)
|
||||||
- [ ] bin.getVersion()
|
- [x] bin.getVersion()
|
||||||
- [ ] bin.escapeStr(str)
|
- [ ] bin.escapeStr(str)
|
||||||
- [ ] bin.ToStr(tab)
|
- [ ] bin.ToStr(tab)
|
||||||
- [ ] bin.packLLIB(name,tab,ext)
|
- [ ] bin.packLLIB(name,tab,ext)
|
||||||
@ -66,7 +69,7 @@ Progress: [===- - - - - - - 15% - - - - - - - - -]
|
|||||||
- [ ] 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)
|
||||||
- [ ] binobj:canStreamWrite()
|
- [x] binobj:canStreamWrite()
|
||||||
- [x] bitobj:conv(n)
|
- [x] bitobj:conv(n)
|
||||||
- [x] bitobj:tobytes()
|
- [x] bitobj:tobytes()
|
||||||
- [x] bitobj:tonumber()
|
- [x] bitobj:tonumber()
|
||||||
@ -92,8 +95,8 @@ Progress: [===- - - - - - - 15% - - - - - - - - -]
|
|||||||
- [ ] binobj:parseA(n,a,t)
|
- [ ] binobj:parseA(n,a,t)
|
||||||
- [ ] binobj:getHEX(a,b)
|
- [ ] binobj:getHEX(a,b)
|
||||||
- [ ] binobj:cryptM()
|
- [ ] binobj:cryptM()
|
||||||
- [ ] binobj:addBlock(d,n)
|
- [x] binobj:addBlock(d,fit,fmt)
|
||||||
- [ ] binobj:getBlock(t,n)
|
- [x] binobj:getBlock(t,n)
|
||||||
- [x] binobj:seek(n)
|
- [x] binobj:seek(n)
|
||||||
- [ ] binobj:morph(a,b,d)
|
- [ ] binobj:morph(a,b,d)
|
||||||
- [ ] binobj:fill(n,d)
|
- [ ] binobj:fill(n,d)
|
||||||
|
|||||||
989
rewritedata/bin/BigNum.lua
Normal file
989
rewritedata/bin/BigNum.lua
Normal file
@ -0,0 +1,989 @@
|
|||||||
|
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{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_LEN = math.floor( math.log10 ( RADIX ) ) ;
|
||||||
|
|
||||||
|
|
||||||
|
--%%%%%%%% Start of Code %%%%%%%%--
|
||||||
|
|
||||||
|
BigNum = {} ;
|
||||||
|
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
|
||||||
|
local bignum = {} ;
|
||||||
|
setmetatable( bignum , BigNum.mt ) ;
|
||||||
|
BigNum.change( bignum , num ) ;
|
||||||
|
return bignum ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--%%%%%%%%%%%%%%%%%%%% Functions for metatable %%%%%%%%%%%%%%%%%%%%--{{{1
|
||||||
|
--BigNum.mt.sub{{{2
|
||||||
|
function BigNum.mt.sub( num1 , num2 )
|
||||||
|
local temp = BigNum.new() ;
|
||||||
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
|
BigNum.sub( bnum1 , bnum2 , temp ) ;
|
||||||
|
return temp ;
|
||||||
|
end
|
||||||
|
function BigNum.mt.mod( num1 , num2 )
|
||||||
|
return BigNum.new(num1 - (num1/num2)*num2)
|
||||||
|
end
|
||||||
|
--BigNum.mt.add{{{2
|
||||||
|
function BigNum.mt.add( num1 , num2 )
|
||||||
|
local temp = BigNum.new() ;
|
||||||
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
|
BigNum.add( bnum1 , bnum2 , temp ) ;
|
||||||
|
return temp ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.mul{{{2
|
||||||
|
function BigNum.mt.mul( num1 , num2 )
|
||||||
|
local temp = BigNum.new() ;
|
||||||
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
|
BigNum.mul( bnum1 , bnum2 , temp ) ;
|
||||||
|
return temp ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.div{{{2
|
||||||
|
function BigNum.mt.div( num1 , num2 )
|
||||||
|
local bnum1 = {} ;
|
||||||
|
local bnum2 = {} ;
|
||||||
|
local bnum3 = BigNum.new() ;
|
||||||
|
local bnum4 = BigNum.new() ;
|
||||||
|
bnum1 = BigNum.new( num1 ) ;
|
||||||
|
bnum2 = BigNum.new( num2 ) ;
|
||||||
|
BigNum.div( bnum1 , bnum2 , bnum3 , bnum4 ) ;
|
||||||
|
return bnum3 , bnum4 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.tostring{{{2
|
||||||
|
function BigNum.mt.tostring( bnum )
|
||||||
|
local i = 0 ;
|
||||||
|
local j = 0 ;
|
||||||
|
local str = "" ;
|
||||||
|
local temp = "" ;
|
||||||
|
if bnum == nil then
|
||||||
|
return "nil" ;
|
||||||
|
elseif bnum.len > 0 then
|
||||||
|
for i = bnum.len - 2 , 0 , -1 do
|
||||||
|
for j = 0 , RADIX_LEN - string.len( bnum[i] ) - 1 do
|
||||||
|
temp = temp .. '0' ;
|
||||||
|
end
|
||||||
|
temp = temp .. bnum[i] ;
|
||||||
|
end
|
||||||
|
if bnum[bnum.len - 1]==nil then
|
||||||
|
return "nil"
|
||||||
|
end
|
||||||
|
temp = bnum[bnum.len - 1] .. temp ;
|
||||||
|
if bnum.signal == '-' then
|
||||||
|
temp = bnum.signal .. temp ;
|
||||||
|
end
|
||||||
|
return temp ;
|
||||||
|
else
|
||||||
|
return "" ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.pow{{{2
|
||||||
|
function BigNum.mt.pow( num1 , num2 )
|
||||||
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
|
return BigNum.pow( bnum1 , bnum2 ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.eq{{{2
|
||||||
|
function BigNum.mt.eq( num1 , num2 )
|
||||||
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
|
return BigNum.eq( bnum1 , bnum2 ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.lt{{{2
|
||||||
|
function BigNum.mt.lt( num1 , num2 )
|
||||||
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
|
return BigNum.lt( bnum1 , bnum2 ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.le{{{2
|
||||||
|
function BigNum.mt.le( num1 , num2 )
|
||||||
|
local bnum1 = BigNum.new( num1 ) ;
|
||||||
|
local bnum2 = BigNum.new( num2 ) ;
|
||||||
|
return BigNum.le( bnum1 , bnum2 ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--BigNum.mt.unm{{{2
|
||||||
|
function BigNum.mt.unm( num )
|
||||||
|
local ret = BigNum.new( num )
|
||||||
|
if ret.signal == '+' then
|
||||||
|
ret.signal = '-'
|
||||||
|
else
|
||||||
|
ret.signal = '+'
|
||||||
|
end
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
--%%%%%%%%%%%%%%%%%%%% Metatable Definitions %%%%%%%%%%%%%%%%%%%%--{{{1
|
||||||
|
|
||||||
|
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 ;
|
||||||
|
-- arithmetics
|
||||||
|
BigNum.mt.__add = BigNum.mt.add ;
|
||||||
|
BigNum.mt.__sub = BigNum.mt.sub ;
|
||||||
|
BigNum.mt.__mul = BigNum.mt.mul ;
|
||||||
|
BigNum.mt.__div = BigNum.mt.div ;
|
||||||
|
BigNum.mt.__pow = BigNum.mt.pow ;
|
||||||
|
BigNum.mt.__unm = BigNum.mt.unm ;
|
||||||
|
BigNum.mt.__mod = BigNum.mt.mod ;
|
||||||
|
-- Comparisons
|
||||||
|
BigNum.mt.__eq = BigNum.mt.eq ;
|
||||||
|
BigNum.mt.__le = BigNum.mt.le ;
|
||||||
|
BigNum.mt.__lt = BigNum.mt.lt ;
|
||||||
|
--concatenation
|
||||||
|
-- BigNum.me.__concat = ???
|
||||||
|
|
||||||
|
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 )
|
||||||
|
local maxlen = 0 ;
|
||||||
|
local i = 0 ;
|
||||||
|
local carry = 0 ;
|
||||||
|
local signal = '+' ;
|
||||||
|
local old_len = 0 ;
|
||||||
|
--Handle the signals
|
||||||
|
if bnum1 == nil or bnum2 == nil or bnum3 == nil then
|
||||||
|
error("Function BigNum.add: parameter nil") ;
|
||||||
|
elseif bnum1.signal == '-' and bnum2.signal == '+' then
|
||||||
|
bnum1.signal = '+' ;
|
||||||
|
BigNum.sub( bnum2 , bnum1 , bnum3 ) ;
|
||||||
|
|
||||||
|
if not rawequal(bnum1, bnum3) then
|
||||||
|
bnum1.signal = '-' ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
elseif bnum1.signal == '+' and bnum2.signal == '-' then
|
||||||
|
bnum2.signal = '+' ;
|
||||||
|
BigNum.sub( bnum1 , bnum2 , bnum3 ) ;
|
||||||
|
if not rawequal(bnum2, bnum3) then
|
||||||
|
bnum2.signal = '-' ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
elseif bnum1.signal == '-' and bnum2.signal == '-' then
|
||||||
|
signal = '-' ;
|
||||||
|
end
|
||||||
|
--
|
||||||
|
old_len = bnum3.len ;
|
||||||
|
if bnum1.len > bnum2.len then
|
||||||
|
maxlen = bnum1.len ;
|
||||||
|
else
|
||||||
|
maxlen = bnum2.len ;
|
||||||
|
bnum1 , bnum2 = bnum2 , bnum1 ;
|
||||||
|
end
|
||||||
|
--School grade sum
|
||||||
|
for i = 0 , maxlen - 1 do
|
||||||
|
if bnum2[i] ~= nil then
|
||||||
|
bnum3[i] = bnum1[i] + bnum2[i] + carry ;
|
||||||
|
else
|
||||||
|
bnum3[i] = bnum1[i] + carry ;
|
||||||
|
end
|
||||||
|
if bnum3[i] >= RADIX then
|
||||||
|
bnum3[i] = bnum3[i] - RADIX ;
|
||||||
|
carry = 1 ;
|
||||||
|
else
|
||||||
|
carry = 0 ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--Update the answer's size
|
||||||
|
if carry == 1 then
|
||||||
|
bnum3[maxlen] = 1 ;
|
||||||
|
end
|
||||||
|
bnum3.len = maxlen + carry ;
|
||||||
|
bnum3.signal = signal ;
|
||||||
|
for i = bnum3.len, old_len do
|
||||||
|
bnum3[i] = nil ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
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 )
|
||||||
|
local maxlen = 0 ;
|
||||||
|
local i = 0 ;
|
||||||
|
local carry = 0 ;
|
||||||
|
local old_len = 0 ;
|
||||||
|
--Handle the signals
|
||||||
|
|
||||||
|
if bnum1 == nil or bnum2 == nil or bnum3 == nil then
|
||||||
|
error("Function BigNum.sub: parameter nil") ;
|
||||||
|
elseif bnum1.signal == '-' and bnum2.signal == '+' then
|
||||||
|
bnum1.signal = '+' ;
|
||||||
|
BigNum.add( bnum1 , bnum2 , bnum3 ) ;
|
||||||
|
bnum3.signal = '-' ;
|
||||||
|
if not rawequal(bnum1, bnum3) then
|
||||||
|
bnum1.signal = '-' ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
elseif bnum1.signal == '-' and bnum2.signal == '-' then
|
||||||
|
bnum1.signal = '+' ;
|
||||||
|
bnum2.signal = '+' ;
|
||||||
|
BigNum.sub( bnum2, bnum1 , bnum3 ) ;
|
||||||
|
if not rawequal(bnum1, bnum3) then
|
||||||
|
bnum1.signal = '-' ;
|
||||||
|
end
|
||||||
|
if not rawequal(bnum2, bnum3) then
|
||||||
|
bnum2.signal = '-' ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
elseif bnum1.signal == '+' and bnum2.signal == '-' then
|
||||||
|
bnum2.signal = '+' ;
|
||||||
|
BigNum.add( bnum1 , bnum2 , bnum3 ) ;
|
||||||
|
if not rawequal(bnum2, bnum3) then
|
||||||
|
bnum2.signal = '-' ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
--Tests if bnum2 > bnum1
|
||||||
|
if BigNum.compareAbs( bnum1 , bnum2 ) == 2 then
|
||||||
|
BigNum.sub( bnum2 , bnum1 , bnum3 ) ;
|
||||||
|
bnum3.signal = '-' ;
|
||||||
|
return 0 ;
|
||||||
|
else
|
||||||
|
maxlen = bnum1.len ;
|
||||||
|
end
|
||||||
|
old_len = bnum3.len ;
|
||||||
|
bnum3.len = 0 ;
|
||||||
|
--School grade subtraction
|
||||||
|
for i = 0 , maxlen - 1 do
|
||||||
|
if bnum2[i] ~= nil then
|
||||||
|
bnum3[i] = bnum1[i] - bnum2[i] - carry ;
|
||||||
|
else
|
||||||
|
bnum3[i] = bnum1[i] - carry ;
|
||||||
|
end
|
||||||
|
if bnum3[i] < 0 then
|
||||||
|
bnum3[i] = RADIX + bnum3[i] ;
|
||||||
|
carry = 1 ;
|
||||||
|
else
|
||||||
|
carry = 0 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
if bnum3[i] ~= 0 then
|
||||||
|
bnum3.len = i + 1 ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
bnum3.signal = '+' ;
|
||||||
|
--Check if answer's size if zero
|
||||||
|
if bnum3.len == 0 then
|
||||||
|
bnum3.len = 1 ;
|
||||||
|
bnum3[0] = 0 ;
|
||||||
|
end
|
||||||
|
if carry == 1 then
|
||||||
|
error( "Error in function sub" ) ;
|
||||||
|
end
|
||||||
|
for i = bnum3.len , max( old_len , maxlen - 1 ) do
|
||||||
|
bnum3[i] = nil ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
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 )
|
||||||
|
local i = 0 ; j = 0 ;
|
||||||
|
local temp = BigNum.new( ) ;
|
||||||
|
local temp2 = 0 ;
|
||||||
|
local carry = 0 ;
|
||||||
|
local oldLen = bnum3.len ;
|
||||||
|
if bnum1 == nil or bnum2 == nil or bnum3 == nil then
|
||||||
|
error("Function BigNum.mul: parameter nil") ;
|
||||||
|
--Handle the signals
|
||||||
|
elseif bnum1.signal ~= bnum2.signal then
|
||||||
|
BigNum.mul( bnum1 , -bnum2 , bnum3 ) ;
|
||||||
|
bnum3.signal = '-' ;
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
bnum3.len = ( bnum1.len ) + ( bnum2.len ) ;
|
||||||
|
--Fill with zeros
|
||||||
|
for i = 1 , bnum3.len do
|
||||||
|
bnum3[i - 1] = 0 ;
|
||||||
|
end
|
||||||
|
--Places nil where passes through this
|
||||||
|
for i = bnum3.len , oldLen do
|
||||||
|
bnum3[i] = nil ;
|
||||||
|
end
|
||||||
|
--School grade multiplication
|
||||||
|
for i = 0 , bnum1.len - 1 do
|
||||||
|
for j = 0 , bnum2.len - 1 do
|
||||||
|
carry = ( bnum1[i] * bnum2[j] + carry ) ;
|
||||||
|
carry = carry + bnum3[i + j] ;
|
||||||
|
bnum3[i + j] = ( carry % RADIX ) ;
|
||||||
|
temp2 = bnum3[i + j] ;
|
||||||
|
carry = math.floor ( carry / RADIX ) ;
|
||||||
|
end
|
||||||
|
if carry ~= 0 then
|
||||||
|
bnum3[i + bnum2.len] = carry ;
|
||||||
|
end
|
||||||
|
carry = 0 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Update the answer's size
|
||||||
|
for i = bnum3.len - 1 , 1 , -1 do
|
||||||
|
if bnum3[i] ~= nil and bnum3[i] ~= 0 then
|
||||||
|
break ;
|
||||||
|
else
|
||||||
|
bnum3[i] = nil ;
|
||||||
|
end
|
||||||
|
bnum3.len = bnum3.len - 1 ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
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 )
|
||||||
|
local temp = BigNum.new() ;
|
||||||
|
local temp2 = BigNum.new() ;
|
||||||
|
local one = BigNum.new( "1" ) ;
|
||||||
|
local zero = BigNum.new( "0" ) ;
|
||||||
|
--Check division by zero
|
||||||
|
if BigNum.compareAbs( bnum2 , zero ) == 0 then
|
||||||
|
error( "Function BigNum.div: Division by zero" ) ;
|
||||||
|
end
|
||||||
|
--Handle the signals
|
||||||
|
if bnum1 == nil or bnum2 == nil or bnum3 == nil or bnum4 == nil then
|
||||||
|
error( "Function BigNum.div: parameter nil" ) ;
|
||||||
|
elseif bnum1.signal == "+" and bnum2.signal == "-" then
|
||||||
|
bnum2.signal = "+" ;
|
||||||
|
BigNum.div( bnum1 , bnum2 , bnum3 , bnum4 ) ;
|
||||||
|
bnum2.signal = "-" ;
|
||||||
|
bnum3.signal = "-" ;
|
||||||
|
return 0 ;
|
||||||
|
elseif bnum1.signal == "-" and bnum2.signal == "+" then
|
||||||
|
bnum1.signal = "+" ;
|
||||||
|
BigNum.div( bnum1 , bnum2 , bnum3 , bnum4 ) ;
|
||||||
|
bnum1.signal = "-" ;
|
||||||
|
if bnum4 < zero then --Check if remainder is negative
|
||||||
|
BigNum.add( bnum3 , one , bnum3 ) ;
|
||||||
|
BigNum.sub( bnum2 , bnum4 , bnum4 ) ;
|
||||||
|
end
|
||||||
|
bnum3.signal = "-" ;
|
||||||
|
return 0 ;
|
||||||
|
elseif bnum1.signal == "-" and bnum2.signal == "-" then
|
||||||
|
bnum1.signal = "+" ;
|
||||||
|
bnum2.signal = "+" ;
|
||||||
|
BigNum.div( bnum1 , bnum2 , bnum3 , bnum4 ) ;
|
||||||
|
bnum1.signal = "-" ;
|
||||||
|
if bnum4 < zero then --Check if remainder is negative
|
||||||
|
BigNum.add( bnum3 , one , bnum3 ) ;
|
||||||
|
BigNum.sub( bnum2 , bnum4 , bnum4 ) ;
|
||||||
|
end
|
||||||
|
bnum2.signal = "-" ;
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
temp.len = bnum1.len - bnum2.len - 1 ;
|
||||||
|
|
||||||
|
--Reset variables
|
||||||
|
BigNum.change( bnum3 , "0" ) ;
|
||||||
|
BigNum.change( bnum4 , "0" ) ;
|
||||||
|
|
||||||
|
BigNum.copy( bnum1 , bnum4 ) ;
|
||||||
|
|
||||||
|
--Check if can continue dividing
|
||||||
|
while( BigNum.compareAbs( bnum4 , bnum2 ) ~= 2 ) do
|
||||||
|
if bnum4[bnum4.len - 1] >= bnum2[bnum2.len - 1] then
|
||||||
|
BigNum.put( temp , math.floor( bnum4[bnum4.len - 1] / bnum2[bnum2.len - 1] ) , bnum4.len - bnum2.len ) ;
|
||||||
|
temp.len = bnum4.len - bnum2.len + 1 ;
|
||||||
|
else
|
||||||
|
BigNum.put( temp , math.floor( ( bnum4[bnum4.len - 1] * RADIX + bnum4[bnum4.len - 2] ) / bnum2[bnum2.len -1] ) , bnum4.len - bnum2.len - 1 ) ;
|
||||||
|
temp.len = bnum4.len - bnum2.len ;
|
||||||
|
end
|
||||||
|
|
||||||
|
if bnum4.signal ~= bnum2.signal then
|
||||||
|
temp.signal = "-";
|
||||||
|
else
|
||||||
|
temp.signal = "+";
|
||||||
|
end
|
||||||
|
BigNum.add( temp , bnum3 , bnum3 ) ;
|
||||||
|
temp = temp * bnum2 ;
|
||||||
|
BigNum.sub( bnum4 , temp , bnum4 ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Update if the remainder is negative
|
||||||
|
if bnum4.signal == '-' then
|
||||||
|
decr( bnum3 ) ;
|
||||||
|
BigNum.add( bnum2 , bnum4 , bnum4 ) ;
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
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 )
|
||||||
|
local n = BigNum.new( bnum2 ) ;
|
||||||
|
local y = BigNum.new( 1 ) ;
|
||||||
|
local z = BigNum.new( bnum1 ) ;
|
||||||
|
local zero = BigNum.new( "0" ) ;
|
||||||
|
if bnum2 < zero then
|
||||||
|
error( "Function BigNum.exp: domain error" ) ;
|
||||||
|
elseif bnum2 == zero then
|
||||||
|
return y ;
|
||||||
|
end
|
||||||
|
while 1 do
|
||||||
|
if ( n[0] % 2 ) == 0 then
|
||||||
|
n = n / 2 ;
|
||||||
|
else
|
||||||
|
n = n / 2 ;
|
||||||
|
y = z * y ;
|
||||||
|
if n == zero then
|
||||||
|
return y ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
z = z * z ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Português :
|
||||||
|
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 )
|
||||||
|
local a = {} ;
|
||||||
|
local b = {} ;
|
||||||
|
local c = {} ;
|
||||||
|
local d = {} ;
|
||||||
|
local zero = {} ;
|
||||||
|
zero = BigNum.new( "0" ) ;
|
||||||
|
if bnum1 == zero or bnum2 == zero then
|
||||||
|
return BigNum.new( "1" ) ;
|
||||||
|
end
|
||||||
|
a = BigNum.new( bnum1 ) ;
|
||||||
|
b = BigNum.new( bnum2 ) ;
|
||||||
|
a.signal = '+' ;
|
||||||
|
b.signal = '+' ;
|
||||||
|
c = BigNum.new() ;
|
||||||
|
d = BigNum.new() ;
|
||||||
|
while b > zero do
|
||||||
|
BigNum.div( a , b , c , d ) ;
|
||||||
|
a , b , d = b , d , a ;
|
||||||
|
end
|
||||||
|
return a ;
|
||||||
|
end
|
||||||
|
-- Português:
|
||||||
|
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 )
|
||||||
|
if BigNum.compare( bnum1 , bnum2 ) == 0 then
|
||||||
|
return true ;
|
||||||
|
else
|
||||||
|
return false ;
|
||||||
|
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 )
|
||||||
|
if BigNum.compare( bnum1 , bnum2 ) == 2 then
|
||||||
|
return true ;
|
||||||
|
else
|
||||||
|
return false ;
|
||||||
|
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 )
|
||||||
|
local temp = -1 ;
|
||||||
|
temp = BigNum.compare( bnum1 , bnum2 )
|
||||||
|
if temp == 0 or temp == 2 then
|
||||||
|
return true ;
|
||||||
|
else
|
||||||
|
return false ;
|
||||||
|
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 )
|
||||||
|
if bnum1 == nil or bnum2 == nil then
|
||||||
|
error("Function compare: parameter nil") ;
|
||||||
|
elseif bnum1.len > bnum2.len then
|
||||||
|
return 1 ;
|
||||||
|
elseif bnum1.len < bnum2.len then
|
||||||
|
return 2 ;
|
||||||
|
else
|
||||||
|
local i ;
|
||||||
|
for i = bnum1.len - 1 , 0 , -1 do
|
||||||
|
if bnum1[i] > bnum2[i] then
|
||||||
|
return 1 ;
|
||||||
|
elseif bnum1[i] < bnum2[i] then
|
||||||
|
return 2 ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
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 )
|
||||||
|
local signal = 0 ;
|
||||||
|
|
||||||
|
if bnum1 == nil or bnum2 == nil then
|
||||||
|
error("Funtion BigNum.compare: parameter nil") ;
|
||||||
|
elseif bnum1.signal == '+' and bnum2.signal == '-' then
|
||||||
|
return 1 ;
|
||||||
|
elseif bnum1.signal == '-' and bnum2.signal == '+' then
|
||||||
|
return 2 ;
|
||||||
|
elseif bnum1.signal == '-' and bnum2.signal == '-' then
|
||||||
|
signal = 1 ;
|
||||||
|
end
|
||||||
|
if bnum1.len > bnum2.len then
|
||||||
|
return 1 + signal ;
|
||||||
|
elseif bnum1.len < bnum2.len then
|
||||||
|
return 2 - signal ;
|
||||||
|
else
|
||||||
|
local i ;
|
||||||
|
for i = bnum1.len - 1 , 0 , -1 do
|
||||||
|
if bnum1[i] > bnum2[i] then
|
||||||
|
return 1 + signal ;
|
||||||
|
elseif bnum1[i] < bnum2[i] then
|
||||||
|
return 2 - signal ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--%%%%%%%%%%%%%%%%%%%% Low level Functions %%%%%%%%%%%%%%%%%%%%--{{{1
|
||||||
|
--BigNum.copy{{{2
|
||||||
|
function BigNum.copy( bnum1 , bnum2 )
|
||||||
|
if bnum1 ~= nil and bnum2 ~= nil then
|
||||||
|
local i ;
|
||||||
|
for i = 0 , bnum1.len - 1 do
|
||||||
|
bnum2[i] = bnum1[i] ;
|
||||||
|
end
|
||||||
|
bnum2.len = bnum1.len ;
|
||||||
|
else
|
||||||
|
error("Function BigNum.copy: parameter nil") ;
|
||||||
|
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 )
|
||||||
|
local j = 0 ;
|
||||||
|
local len = 0 ;
|
||||||
|
local num = num ;
|
||||||
|
local l ;
|
||||||
|
local oldLen = 0 ;
|
||||||
|
if bnum1 == nil then
|
||||||
|
error( "BigNum.change: parameter nil" ) ;
|
||||||
|
elseif type( bnum1 ) ~= "table" then
|
||||||
|
error( "BigNum.change: parameter error, type unexpected" ) ;
|
||||||
|
elseif num == nil then
|
||||||
|
bnum1.len = 1 ;
|
||||||
|
bnum1[0] = 0 ;
|
||||||
|
bnum1.signal = "+";
|
||||||
|
elseif type( num ) == "table" and num.len ~= nil then --check if num is a big number
|
||||||
|
--copy given table to the new one
|
||||||
|
for i = 0 , num.len do
|
||||||
|
bnum1[i] = num[i] ;
|
||||||
|
end
|
||||||
|
if num.signal ~= '-' and num.signal ~= '+' then
|
||||||
|
bnum1.signal = '+' ;
|
||||||
|
else
|
||||||
|
bnum1.signal = num.signal ;
|
||||||
|
end
|
||||||
|
oldLen = bnum1.len ;
|
||||||
|
bnum1.len = num.len ;
|
||||||
|
elseif type( num ) == "string" or type( num ) == "number" then
|
||||||
|
if string.sub( num , 1 , 1 ) == '+' or string.sub( num , 1 , 1 ) == '-' then
|
||||||
|
bnum1.signal = string.sub( num , 1 , 1 ) ;
|
||||||
|
num = string.sub(num, 2) ;
|
||||||
|
else
|
||||||
|
bnum1.signal = '+' ;
|
||||||
|
end
|
||||||
|
num = string.gsub( num , " " , "" ) ;
|
||||||
|
local sf = string.find( num , "e" ) ;
|
||||||
|
--Handles if the number is in exp notation
|
||||||
|
if sf ~= nil then
|
||||||
|
num = string.gsub( num , "%." , "" ) ;
|
||||||
|
local e = string.sub( num , sf + 1 ) ;
|
||||||
|
e = tonumber(e) ;
|
||||||
|
if e ~= nil and e > 0 then
|
||||||
|
e = tonumber(e) ;
|
||||||
|
else
|
||||||
|
error( "Function BigNum.change: string is not a valid number" ) ;
|
||||||
|
end
|
||||||
|
num = string.sub( num , 1 , sf - 2 ) ;
|
||||||
|
for i = string.len( num ) , e do
|
||||||
|
num = num .. "0" ;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
sf = string.find( num , "%." ) ;
|
||||||
|
if sf ~= nil then
|
||||||
|
num = string.sub( num , 1 , sf - 1 ) ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
l = string.len( num ) ;
|
||||||
|
oldLen = bnum1.len ;
|
||||||
|
if (l > RADIX_LEN) then
|
||||||
|
local mod = l-( math.floor( l / RADIX_LEN ) * RADIX_LEN ) ;
|
||||||
|
for i = 1 , l-mod, RADIX_LEN do
|
||||||
|
bnum1[j] = tonumber( string.sub( num, -( i + RADIX_LEN - 1 ) , -i ) );
|
||||||
|
--Check if string dosn't represents a number
|
||||||
|
if bnum1[j] == nil then
|
||||||
|
error( "Function BigNum.change: string is not a valid number" ) ;
|
||||||
|
bnum1.len = 0 ;
|
||||||
|
return 1 ;
|
||||||
|
end
|
||||||
|
j = j + 1 ;
|
||||||
|
len = len + 1 ;
|
||||||
|
end
|
||||||
|
if (mod ~= 0) then
|
||||||
|
bnum1[j] = tonumber( string.sub( num , 1 , mod ) ) ;
|
||||||
|
bnum1.len = len + 1 ;
|
||||||
|
else
|
||||||
|
bnum1.len = len ;
|
||||||
|
end
|
||||||
|
--Eliminate trailing zeros
|
||||||
|
for i = bnum1.len - 1 , 1 , -1 do
|
||||||
|
if bnum1[i] == 0 then
|
||||||
|
bnum1[i] = nil ;
|
||||||
|
bnum1.len = bnum1.len - 1 ;
|
||||||
|
else
|
||||||
|
break ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
-- string.len(num) <= RADIX_LEN
|
||||||
|
bnum1[j] = tonumber( num ) ;
|
||||||
|
bnum1.len = 1 ;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error( "Function BigNum.change: parameter error, type unexpected" ) ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--eliminates the deprecated higher order 'algarisms'
|
||||||
|
if oldLen ~= nil then
|
||||||
|
for i = bnum1.len , oldLen do
|
||||||
|
bnum1[i] = nil ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return 0 ;
|
||||||
|
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 )
|
||||||
|
if bnum == nil then
|
||||||
|
error("Function BigNum.put: parameter nil") ;
|
||||||
|
end
|
||||||
|
local i = 0 ;
|
||||||
|
for i = 0 , pos - 1 do
|
||||||
|
bnum[i] = 0 ;
|
||||||
|
end
|
||||||
|
bnum[pos] = int ;
|
||||||
|
for i = pos + 1 , bnum.len do
|
||||||
|
bnum[i] = nil ;
|
||||||
|
end
|
||||||
|
bnum.len = pos ;
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
|
||||||
|
--printraw{{{2
|
||||||
|
function printraw( bnum )
|
||||||
|
local i = 0 ;
|
||||||
|
if bnum == nil then
|
||||||
|
error( "Function printraw: parameter nil" ) ;
|
||||||
|
end
|
||||||
|
while 1 == 1 do
|
||||||
|
if bnum[i] == nil then
|
||||||
|
io.write( ' len '..bnum.len ) ;
|
||||||
|
if i ~= bnum.len then
|
||||||
|
io.write( ' ERRO!!!!!!!!' ) ;
|
||||||
|
end
|
||||||
|
io.write( "\n" ) ;
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
|
io.write( 'r'..bnum[i] ) ;
|
||||||
|
i = i + 1 ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--max{{{2
|
||||||
|
function max( int1 , int2 )
|
||||||
|
if int1 > int2 then
|
||||||
|
return int1 ;
|
||||||
|
else
|
||||||
|
return int2 ;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--decr{{{2
|
||||||
|
function decr( bnum1 )
|
||||||
|
local temp = {} ;
|
||||||
|
temp = BigNum.new( "1" ) ;
|
||||||
|
BigNum.sub( bnum1 , temp , bnum1 ) ;
|
||||||
|
return 0 ;
|
||||||
|
end
|
||||||
488
rewritedata/bin/BigRat.lua
Normal file
488
rewritedata/bin/BigRat.lua
Normal file
@ -0,0 +1,488 @@
|
|||||||
|
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%{{{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
|
||||||
@ -109,26 +109,48 @@ for i=0,255 do
|
|||||||
bits.ref[d]=i
|
bits.ref[d]=i
|
||||||
bits.ref["\255"..string.char(i)]=d
|
bits.ref["\255"..string.char(i)]=d
|
||||||
end
|
end
|
||||||
function bits.numToBytes(n,fit,func)
|
function bits.numToBytes(n,fit,fmt,func)
|
||||||
local num=bits.new(n):toSbytes()
|
if fmt=="%e" then
|
||||||
num=bin.endianflop(num)
|
local num=string.reverse(bits.new(n):toSbytes())
|
||||||
local ref={["num"]=num,["fit"]=fit}
|
local ref={["num"]=num,["fit"]=fit}
|
||||||
if fit then
|
if fit then
|
||||||
if fit<#num then
|
if fit<#num then
|
||||||
if func then
|
if func then
|
||||||
print("Warning: attempting to store a number that takes up more space than allotted! Using provided method!")
|
print("Warning: attempting to store a number that takes up more space than allotted! Using provided method!")
|
||||||
func(ref)
|
func(ref)
|
||||||
|
else
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted!")
|
||||||
|
end
|
||||||
|
return ref.num:sub(1,ref.fit)
|
||||||
|
elseif fit==#num then
|
||||||
|
return num
|
||||||
else
|
else
|
||||||
print("Warning: attempting to store a number that takes up more space than allotted!")
|
return string.rep("\0",fit-#num)..num
|
||||||
end
|
end
|
||||||
return ref.num:sub(1,ref.fit)
|
|
||||||
elseif fit==#num then
|
|
||||||
return num
|
|
||||||
else
|
else
|
||||||
return string.rep("\0",fit-#num)..num
|
return num
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
return num
|
local num=string.reverse(bits.new(n):toSbytes())
|
||||||
|
local ref={["num"]=num,["fit"]=fit}
|
||||||
|
if fit then
|
||||||
|
if fit<#num then
|
||||||
|
if func then
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted! Using provided method!")
|
||||||
|
func(ref)
|
||||||
|
else
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted!")
|
||||||
|
end
|
||||||
|
return ref.num:sub(1,ref.fit)
|
||||||
|
elseif fit==#num then
|
||||||
|
return string.reverse(num)
|
||||||
|
else
|
||||||
|
return string.reverse(string.rep("\0",fit-#num)..num)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return string.reverse(num)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function bits:conv(n)
|
function bits:conv(n)
|
||||||
@ -138,13 +160,13 @@ function bits:conv(n)
|
|||||||
n=math.floor(n/2)
|
n=math.floor(n/2)
|
||||||
end
|
end
|
||||||
local str=string.reverse(table.concat(tab))
|
local str=string.reverse(table.concat(tab))
|
||||||
if #str%8~=0 then
|
if #str%8~=0 or #str==0 then
|
||||||
str=string.rep('0',8-#str%8)..str
|
str=string.rep('0',8-#str%8)..str
|
||||||
end
|
end
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
function bits:tonumber(s)
|
function bits:tonumber(s,e)
|
||||||
if type(s)=='string' then
|
if s==0 then
|
||||||
return tonumber(self.data,2)
|
return tonumber(self.data,2)
|
||||||
end
|
end
|
||||||
s=s or 1
|
s=s or 1
|
||||||
|
|||||||
242
rewritedata/bin/infinabits.lua
Normal file
242
rewritedata/bin/infinabits.lua
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
local binNum=require("bin.BigNum")
|
||||||
|
local infinabits={}
|
||||||
|
infinabits.data=''
|
||||||
|
infinabits.t='infinabits'
|
||||||
|
infinabits.__index = infinabits
|
||||||
|
infinabits.__tostring=function(self) return self.data end
|
||||||
|
infinabits.__len=function(self) return (#self.data)/8 end
|
||||||
|
local floor,insert = math.floor, table.insert
|
||||||
|
function basen(n,b)
|
||||||
|
n=BigNum.new(n)
|
||||||
|
if not b or b == 10 then return tostring(n) end
|
||||||
|
local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
local t = {}
|
||||||
|
local sign = ""
|
||||||
|
if n < BigNum.new(0) then
|
||||||
|
sign = "-"
|
||||||
|
n = -n
|
||||||
|
end
|
||||||
|
repeat
|
||||||
|
local d = tonumber(tostring(n % b)) + 1
|
||||||
|
n = n / b
|
||||||
|
insert(t, 1, digits:sub(d,d))
|
||||||
|
until n == BigNum.new(0)
|
||||||
|
return sign .. table.concat(t,"")
|
||||||
|
end
|
||||||
|
function base2to10(num)
|
||||||
|
local n=BigNum.new(0)
|
||||||
|
for i = #num-1,0,-1 do
|
||||||
|
nn=BigNum.new(num:sub(i+1,i+1))*(BigNum.new(2)^((#num-i)-1))
|
||||||
|
n=n+nn
|
||||||
|
end
|
||||||
|
return n
|
||||||
|
end
|
||||||
|
function infinabits.newBitBuffer(n)
|
||||||
|
-- WIP
|
||||||
|
end
|
||||||
|
function infinabits.newConverter(bitsIn,bitsOut)
|
||||||
|
local c={}
|
||||||
|
-- WIP
|
||||||
|
end
|
||||||
|
infinabits.ref={}
|
||||||
|
function infinabits.newByte(d)-- WIP
|
||||||
|
local c={}
|
||||||
|
if type(d)=="string" then
|
||||||
|
if #d>1 or #d<1 then
|
||||||
|
error("A byte must be one character!")
|
||||||
|
else
|
||||||
|
c.data=string.byte(d)
|
||||||
|
end
|
||||||
|
elseif type(d)=="number" then
|
||||||
|
if d>255 or d<0 then
|
||||||
|
error("A byte must be between 0 and 255!")
|
||||||
|
else
|
||||||
|
c.data=d
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error("cannot use type "..type(d).." as an argument! Takes only strings or numbers!")
|
||||||
|
end
|
||||||
|
c.__index=function(self,k)
|
||||||
|
if k>=0 and k<9 then
|
||||||
|
if self.data==0 then
|
||||||
|
return 0
|
||||||
|
elseif self.data==255 then
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
return infinabits.ref[self.data][k]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
c.__tostring=function(self)
|
||||||
|
return infinabits.ref[tostring(self.data)]
|
||||||
|
end
|
||||||
|
setmetatable(c,c)
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
function infinabits.newByteArray(s)-- WIP
|
||||||
|
local c={}
|
||||||
|
if type(s)~="string" then
|
||||||
|
error("Must be a string type or bin/buffer type")
|
||||||
|
elseif type(s)=="table" then
|
||||||
|
if s.t=="sink" or s.t=="buffer" or s.t=="bin" then
|
||||||
|
local data=s:getData()
|
||||||
|
for i=1,#data do
|
||||||
|
c[#c+1]=infinabits.newByte(data:sub(i,i))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
error("Must be a string type or bin/buffer type")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i=1,#s do
|
||||||
|
c[#c+1]=infinabits.newByte(s:sub(i,i))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
function infinabits.new(n,binary)
|
||||||
|
local temp={}
|
||||||
|
temp.t='bit'
|
||||||
|
if type(n)=="string" then
|
||||||
|
if binary then
|
||||||
|
temp.data=n:match("[10]+")
|
||||||
|
else
|
||||||
|
local t={}
|
||||||
|
for i=#n,1,-1 do
|
||||||
|
table.insert(t,infinabits:conv(string.byte(n,i)))
|
||||||
|
end
|
||||||
|
temp.data=table.concat(t)
|
||||||
|
end
|
||||||
|
elseif type(n)=="number" or type(n)=="table" then
|
||||||
|
temp.data=basen(tostring(n),2)
|
||||||
|
end
|
||||||
|
if #temp.data%8~=0 then
|
||||||
|
temp.data=string.rep('0',8-#temp.data%8)..temp.data
|
||||||
|
end
|
||||||
|
setmetatable(temp, infinabits)
|
||||||
|
return temp
|
||||||
|
end
|
||||||
|
for i=0,255 do
|
||||||
|
local d=infinabits.new(i).data
|
||||||
|
infinabits.ref[i]={d:match("(%d)(%d)(%d)(%d)(%d)(%d)(%d)(%d)")}
|
||||||
|
infinabits.ref[tostring(i)]=d
|
||||||
|
infinabits.ref[d]=i
|
||||||
|
infinabits.ref["\255"..string.char(i)]=d
|
||||||
|
end
|
||||||
|
function infinabits.numToBytes(n,fit,func)
|
||||||
|
local num=string.reverse(infinabits.new(BigNum.new(n)):toSbytes())
|
||||||
|
local ref={["num"]=num,["fit"]=fit}
|
||||||
|
if fit then
|
||||||
|
if fit<#num then
|
||||||
|
if func then
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted! Using provided method!")
|
||||||
|
func(ref)
|
||||||
|
else
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted!")
|
||||||
|
end
|
||||||
|
return ref.num:sub(1,ref.fit)
|
||||||
|
elseif fit==#num then
|
||||||
|
return string.reverse(num)
|
||||||
|
else
|
||||||
|
return string.reverse(string.rep("\0",fit-#num)..num)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return string.reverse(num)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function infinabits.numToBytes(n,fit,fmt,func)
|
||||||
|
if fmt=="%e" then
|
||||||
|
local num=string.reverse(infinabits.new(BigNum.new(n)):toSbytes())
|
||||||
|
local ref={["num"]=num,["fit"]=fit}
|
||||||
|
if fit then
|
||||||
|
if fit<#num then
|
||||||
|
if func then
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted! Using provided method!")
|
||||||
|
func(ref)
|
||||||
|
else
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted!")
|
||||||
|
end
|
||||||
|
return ref.num:sub(1,ref.fit)
|
||||||
|
elseif fit==#num then
|
||||||
|
return num
|
||||||
|
else
|
||||||
|
return string.rep("\0",fit-#num)..num
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return num
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
local num=string.reverse(infinabits.new(BigNum.new(n)):toSbytes())
|
||||||
|
local ref={["num"]=num,["fit"]=fit}
|
||||||
|
if fit then
|
||||||
|
if fit<#num then
|
||||||
|
if func then
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted! Using provided method!")
|
||||||
|
func(ref)
|
||||||
|
else
|
||||||
|
print("Warning: attempting to store a number that takes up more space than allotted!")
|
||||||
|
end
|
||||||
|
return ref.num:sub(1,ref.fit)
|
||||||
|
elseif fit==#num then
|
||||||
|
return string.reverse(num)
|
||||||
|
else
|
||||||
|
return string.reverse(string.rep("\0",fit-#num)..num)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return string.reverse(num)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function infinabits:conv(n)
|
||||||
|
local tab={}
|
||||||
|
local one=BigNum.new(1)
|
||||||
|
local n=BigNum.new(n)
|
||||||
|
while n>=one do
|
||||||
|
table.insert(tab,tonumber(tostring(n%2)))
|
||||||
|
n=n/2
|
||||||
|
end
|
||||||
|
local str=string.reverse(table.concat(tab))
|
||||||
|
if #str%8~=0 or #str==0 then
|
||||||
|
str=string.rep('0',8-#str%8)..str
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
function infinabits:tonumber(s)
|
||||||
|
if s==0 then
|
||||||
|
return tonumber(self.data,2)
|
||||||
|
end
|
||||||
|
s=s or 1
|
||||||
|
return tonumber(tostring(base2to10(string.sub(self.data,(8*(s-1))+1,8*s)))) or error('Bounds!')
|
||||||
|
end
|
||||||
|
function infinabits:isover()
|
||||||
|
return #self.data>8
|
||||||
|
end
|
||||||
|
function infinabits:flipbits()
|
||||||
|
tab={}
|
||||||
|
local s=self.data
|
||||||
|
s=s:gsub("1","_")
|
||||||
|
s=s:gsub("0","1")
|
||||||
|
s=s:gsub("_","0")
|
||||||
|
self.data=s
|
||||||
|
end
|
||||||
|
function infinabits:tobytes()
|
||||||
|
local tab={}
|
||||||
|
for i=self:getbytes(),1,-1 do
|
||||||
|
table.insert(tab,string.char(self:tonumber(i)))
|
||||||
|
end
|
||||||
|
return bin.new(table.concat(tab))
|
||||||
|
end
|
||||||
|
function infinabits:toSbytes()
|
||||||
|
local tab={}
|
||||||
|
for i=self:getbytes(),1,-1 do
|
||||||
|
table.insert(tab,string.char(self:tonumber(i)))
|
||||||
|
end
|
||||||
|
return table.concat(tab)
|
||||||
|
end
|
||||||
|
function infinabits:getBin()
|
||||||
|
return self.data
|
||||||
|
end
|
||||||
|
function infinabits:getbytes()
|
||||||
|
return #self.data/8
|
||||||
|
end
|
||||||
|
return infinabits
|
||||||
@ -8,15 +8,13 @@ bin.t='bin'
|
|||||||
bin.__index = bin
|
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
|
||||||
bits={}
|
|
||||||
bits.data=''
|
|
||||||
bits.t='bits'
|
|
||||||
bits.__index = bits
|
|
||||||
bits.__tostring=function(self) return self.data end
|
|
||||||
bits.__len=function(self) return (#self.data)/8 end
|
|
||||||
bin.lastBlockSize=0
|
bin.lastBlockSize=0
|
||||||
bin.streams={} -- FIX FOR THREADING!!!
|
bin.streams={} -- FIX FOR THREADING!!!
|
||||||
-- Helpers
|
-- Helpers
|
||||||
|
function bin.getVersion()
|
||||||
|
return bin.Version[1]..'.'..bin.Version[2]..'.'..bin.Version[3]
|
||||||
|
end
|
||||||
|
require("bin.utils")
|
||||||
if jit then
|
if jit then
|
||||||
bit=require("bit")
|
bit=require("bit")
|
||||||
elseif bit32 then
|
elseif bit32 then
|
||||||
@ -24,10 +22,14 @@ elseif bit32 then
|
|||||||
else
|
else
|
||||||
bit=require("bin.no_jit_bit")
|
bit=require("bin.no_jit_bit")
|
||||||
end
|
end
|
||||||
require("bin.utils")
|
|
||||||
local base64=require("bin.base64")
|
local base64=require("bin.base64")
|
||||||
local base91=require("bin.base91")
|
local base91=require("bin.base91")
|
||||||
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!!!!
|
||||||
|
function bin.setBitsInterface(int)
|
||||||
|
bin.defualtBit=int or bits
|
||||||
|
end
|
||||||
|
bin.setBitsInterface()
|
||||||
function bin.normalizeData(data) -- unified function to allow
|
function bin.normalizeData(data) -- unified function to allow
|
||||||
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
|
||||||
@ -146,6 +148,14 @@ function bin.stream(file,l)
|
|||||||
bin.streams[file]={c,1}
|
bin.streams[file]={c,1}
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
function bin.newTempFile()
|
||||||
|
local c=bin.new()
|
||||||
|
c.file=file
|
||||||
|
c.lock = false
|
||||||
|
c.workingfile=io.tmpfile()
|
||||||
|
c.stream=true
|
||||||
|
return c
|
||||||
|
end
|
||||||
function bin.freshStream(file)
|
function bin.freshStream(file)
|
||||||
bin.new():tofile(file)
|
bin.new():tofile(file)
|
||||||
return bin.stream(file,false)
|
return bin.stream(file,false)
|
||||||
@ -154,6 +164,13 @@ end
|
|||||||
function bin:canStreamWrite()
|
function bin:canStreamWrite()
|
||||||
return (self.stream and not(self.lock))
|
return (self.stream and not(self.lock))
|
||||||
end
|
end
|
||||||
|
function bin:getSeek()
|
||||||
|
if self.stream then
|
||||||
|
return self.workingfile:seek("cur")
|
||||||
|
else
|
||||||
|
return self.pos
|
||||||
|
end
|
||||||
|
end
|
||||||
function bin:seekSet(n)
|
function bin:seekSet(n)
|
||||||
if self.stream then
|
if self.stream then
|
||||||
self.workingfile:seek("set",n-1)
|
self.workingfile:seek("set",n-1)
|
||||||
@ -310,3 +327,53 @@ function bin:close()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
function bin:getBlock(t,n)
|
||||||
|
local data=""
|
||||||
|
if not n then
|
||||||
|
if bin.registerBlocks[t] then
|
||||||
|
return bin.registerBlocks[t][1](nil,self)
|
||||||
|
else
|
||||||
|
error("Unknown format! Cannot read from file: "..tostring(t))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if t=="n" or t=="%e" or t=="%E" then
|
||||||
|
data=self:read(n)
|
||||||
|
local numB=bin.defualtBit.new(data)
|
||||||
|
local numL=bin.defualtBit.new(string.reverse(data))
|
||||||
|
local little=numL:tonumber(0)
|
||||||
|
local big=numB:tonumber(0)
|
||||||
|
if t=="%E" then
|
||||||
|
return big
|
||||||
|
elseif t=="%e" then
|
||||||
|
return little
|
||||||
|
end
|
||||||
|
return big,little
|
||||||
|
elseif t=="s" then
|
||||||
|
return self:read(n)
|
||||||
|
elseif bin.registerBlocks[t] then
|
||||||
|
return bin.registerBlocks[t][1](n,self)
|
||||||
|
else
|
||||||
|
error("Unknown format! Cannot read from file: "..tostring(t))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function bin:addBlock(d,fit,fmt)
|
||||||
|
if type(d)=="number" then
|
||||||
|
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!")
|
||||||
|
end)
|
||||||
|
self:tackE(data)
|
||||||
|
elseif type(d)=="string" then
|
||||||
|
local data=d:sub(1,fit or -1)
|
||||||
|
if data<(fit or #data) then
|
||||||
|
data=data..string.rep("\0",fit-#data)
|
||||||
|
end
|
||||||
|
self:tackE(data)
|
||||||
|
elseif bin.registerBlocks[fmt] then
|
||||||
|
self:tackE(bin.registerBlocks[fmt][2](d,fit,fmt,self,bin.registerBlocks[fmt][2]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
bin.registerBlocks={}
|
||||||
|
function bin.registerBlock(t,funcG,funcA)
|
||||||
|
bin.registerBlocks[t]={funcG,funcA}
|
||||||
|
end
|
||||||
|
|||||||
@ -1,3 +1,17 @@
|
|||||||
|
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)
|
function table.flip(t)
|
||||||
local tt={}
|
local tt={}
|
||||||
for i,v in pairs(t) do
|
for i,v in pairs(t) do
|
||||||
|
|||||||
3207
rewritedata/binold.lua
Normal file
3207
rewritedata/binold.lua
Normal file
File diff suppressed because it is too large
Load Diff
0
rewritedata/read.dat
Normal file
0
rewritedata/read.dat
Normal file
123
rewritedata/read.txt
Normal file
123
rewritedata/read.txt
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
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
|
||||||
BIN
rewritedata/test.dat
Normal file
BIN
rewritedata/test.dat
Normal file
Binary file not shown.
4
rewritedata/test.lua
Normal file
4
rewritedata/test.lua
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package.path="?/init.lua;"..package.path
|
||||||
|
require("bin")
|
||||||
|
test=bin.new(bin.getnumber(12345,4,"%B"))
|
||||||
|
print(test:tonumber())
|
||||||
1
rewritedata/test.txt
Normal file
1
rewritedata/test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hi!Hi!Hi!Hi!Hi!Hi!
|
||||||
Loading…
x
Reference in New Issue
Block a user