Slowly Rewriting the library 15% done!

This commit is contained in:
Ryan 2017-07-31 00:44:13 -04:00
parent 5a147a1d70
commit 7bb2d0750c
11 changed files with 1785 additions and 290 deletions

View File

@ -1,26 +1,32 @@
# Bin Rewrite Progress!
**Note: A lot breaks (Almost everything) for the sake of consistency**
Progress: [==- - - - - - - - 10% - - - - - - - - -]
Progress: [===- - - - - - - 15% - - - - - - - - -]
### List of new methods
- [x] bin.newFromB64(data)
- [x] bin.newFromBase64(data)
- [x] bin.newFromBase91(data)
- [x] bin.newFromHex(data)
- [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
- [ ] 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.fromBase64(s)
- [x] bin.toBase91(s)
- [x] bin.fromBase91(s)
- [x] bin.getnumber(num,len,fmt,func)
### List of converted methods and their status
- [ ] log(data,name,fmt)
- [x] log(data,name,fmt)
- [ ] bin.getLuaVersion()
- [x] ~~bin.load(filename,s,r)~~
- [x] ~~bin.new(string data)~~ -- Does not accept b64 or hex data anymore! Use new methods for that ^^^
- [ ] bin.stream(file,lock)
- [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.stream(file,lock)
- [ ] 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)
- [ ] bits.new(n)
- [x] bits.new(n)
- [ ] bin.newVFS()
- [x] ~~bin:tackE(data)~~ -- tacks data onto the end of a file
- [x] ~~bin:tofile(path)~~
- [x] bin:tackE(data) -- tacks data onto the end of a file
- [x] bin:tofile(path)
- [ ] bin.loadVFS(path)
- [ ] bin:newDataBuffer(s)
- [ ] bin.bufferToBin(b)
@ -34,26 +40,26 @@ Progress: [==- - - - - - - - 10% - - - - - - - - -]
- [ ] bin.NumtoHEX(n)
- [ ] bin.HEXtoBin(s)
- [ ] bin.HEXtoStr(s)
- [x] ~~bin.tohex(s)~~
- [x] ~~bin.fromhex(s)~~
- [ ] bin.endianflop(data)
- [x] bin.tohex(s)
- [x] bin.fromhex(s)
- [x] bin.endianflop(data)
- [ ] bin.getVersion()
- [ ] bin.escapeStr(str)
- [ ] bin.ToStr(tab)
- [ ] bin.packLLIB(name,tab,ext)
- [ ] bin.unpackLLIB(name,exe,todir,over,ext)
- [ ] bin.fileExist(path)
- [ ] ~~bin.fileExist(path)~~ NOW io.fileExists(path)
- [ ] bin.closeto(a,b,v)
- [ ] bin.textToBinary(txt)
- [ ] bin.decodeBits(bindata)
- [ ] bin.trimNul(s)
- [ ] bin.getIndexSize(tab)
- [ ] bits.numToBytes(num,occ)
- [x] bits.numToBytes(num,occ)
- [ ] binobj:tofile(filename)
- [ ] binobj:clone()
- [ ] binobj:compare(other binobj,diff)
- [ ] binobj:sub(a,b)
- [ ] binobj:tonumber(a,b)
- [x] binobj:tonumber(a,b)
- [ ] binobj:getbyte(n)
- [ ] binobj:tobits(i)
- [ ] binobj:getHEX(a,b)
@ -61,11 +67,11 @@ Progress: [==- - - - - - - - 10% - - - - - - - - -]
- [ ] binobj:streamData(a,b)
- [ ] binobj:streamread(a,b)
- [ ] binobj:canStreamWrite()
- [ ] bitobj:conv(n)
- [ ] bitobj:tobytes()
- [ ] bitobj:tonumber()
- [ ] bitobj:isover()
- [ ] bitobj:getBin()
- [x] bitobj:conv(n)
- [x] bitobj:tobytes()
- [x] bitobj:tonumber()
- [x] bitobj:isover()
- [x] bitobj:getBin()
- [ ] binobj:getHash(n)
- [ ] binobj:getData()
- [ ] blockReader:getBlock(name)
@ -88,25 +94,25 @@ Progress: [==- - - - - - - - 10% - - - - - - - - -]
- [ ] binobj:cryptM()
- [ ] binobj:addBlock(d,n)
- [ ] binobj:getBlock(t,n)
- [x] ~~binobj:seek(n)~~
- [x] binobj:seek(n)
- [ ] binobj:morph(a,b,d)
- [ ] binobj:fill(n,d)
- [ ] binobj:fillrandom(n)
- [ ] binobj:shiftbits(n)
- [ ] binobj:shiftbit(n,i)
- [ ] binobj:streamwrite(d,n)
- [ ] binobj:open()
- [ ] binobj:close()
- [ ] ~~binobj:open()~~
- [x] binobj:close()
- [ ] binobj:wipe()
- [ ] binobj:tackB(d)
- [ ] binobj:tackE(d)
- [x] binobj:tackE(d,fit,updateseek)
- [ ] binobj:parse(n,f)
- [ ] binobj:flipbit(i)
- [ ] binobj:gsub()
- [ ] blockWriter:addNamedBlock(name,value)
- [ ] bitobj:add(i)
- [ ] bitobj:sub(i)
- [ ] bitobj:multi(i)
- [ ] bitobj:div(i)
- [ ] bitobj:flipbits()
- [ ] bitobj:getBin()
- [ ] ~~bitobj:add(i)~~
- [ ] ~~bitobj:sub(i)~~
- [ ] ~~bitobj:multi(i)~~
- [ ] ~~bitobj:div(i)~~
- [x] bitobj:flipbits()
- [x] bitobj:getBin()

View File

@ -0,0 +1,33 @@
local base64={}
local bs = { [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','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','0','1','2','3','4','5','6','7','8','9','+','/',
}
local bsd=table.flip(bs)
local char=string.char
function base64.encode(s)
local byte, rep, pad = string.byte, string.rep, 2 - ((#s-1) % 3)
s = (s..rep('\0', pad)):gsub("...", function(cs)
local a, b, c = byte(cs, 1, 3)
return bs[bit.rshift(a,2)] .. bs[bit.bor(bit.lshift(bit.band(a,3),4),bit.rshift(b,4))] .. bs[bit.bor(bit.lshift(bit.band(b,15),2),bit.rshift(c,6))] .. bs[bit.band(c,63)]
end)
return s:sub(1, #s-pad) .. rep('=', pad)
end
function base64.decode(s)
local s=s:match("["..s.."=]+")
local p,cc=s:gsub("=","A")
local r=""
local n=0
s=s:sub(1,#s-#p)..p
for c = 1,#s,4 do
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)]
r = r .. char(bit.band(bit.arshift(n, 16), 0xFF)) .. char(bit.band(bit.arshift(n, 8), 0xFF)) .. char(bit.band(n, 0xFF))
end
return r:sub(1,-(cc+1))
end
return base64

View File

@ -0,0 +1,72 @@
local base91={}
local b91enc={[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',
'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',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$',
'%', '&', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=',
'>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"'
}
local b91dec=table.flip(b91enc)
function base91.decode(d)
local l,v,o,b,n = #d,-1,"",0,0
for i in d:gmatch(".") do
local c=b91dec[i]
if not(c) then
-- Continue
else
if v < 0 then
v = c
else
v = v+c*91
b = bit.bor(b, bit.lshift(v,n))
if bit.band(v,8191) then
n = n + 13
else
n = n + 14
end
while true do
o=o..string.char(bit.band(b,255))
b=bit.rshift(b,8)
n=n-8
if not (n>7) then
break
end
end
v=-1
end
end
end
if v + 1>0 then
o=o..string.char(bit.band(bit.bor(b,bit.lshift(v,n)),255))
end
return o
end
function base91.encode(d)
local b,n,o,l=0,0,"",#d
for i in d:gmatch(".") do
b=bit.bor(b,bit.lshift(string.byte(i),n))
n=n+8
if n>13 then
v=bit.band(b,8191)
if v>88 then
b=bit.rshift(b,13)
n=n-13
else
v=bit.band(b,16383)
b=bit.rshift(b,14)
n=n-14
end
o=o..b91enc[v % 91] .. b91enc[math.floor(v / 91)]
end
end
if n>0 then
o=o..b91enc[b % 91]
if n>7 or b>90 then
o=o .. b91enc[math.floor(b / 91)]
end
end
return o
end
return base91

187
rewritedata/bin/bits.lua Normal file
View File

@ -0,0 +1,187 @@
local 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
function bits.newBitBuffer(n)
--
end
function bits.newConverter(bitsIn,bitsOut)
local c={}
--
end
bits.ref={}
function bits.newByte(d)
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 bits.ref[self.data][k]
end
end
end
c.__tostring=function(self)
return bits.ref[tostring(self.data)]
end
setmetatable(c,c)
return c
end
function bits.newByteArray(s)
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]=bits.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]=bits.newByte(s:sub(i,i))
end
end
return c
end
function bits.new(n,s)
if type(n)=='string' then
local t=tonumber(n,2)
if t and #n<8 and not(s) then
t=nil
end
if not(t) then
t={}
for i=#n,1,-1 do
table.insert(t,bits:conv(string.byte(n,i)))
end
n=table.concat(t)
else
n=t
end
end
local temp={}
temp.t='bit'
setmetatable(temp, bits)
if type(n)~='string' then
local tab={}
while n>=1 do
table.insert(tab,n%2)
n=math.floor(n/2)
end
local str=string.reverse(table.concat(tab))
if #str%8~=0 then
str=string.rep('0',8-#str%8)..str
end
temp.data=str
else
temp.data=n
end
setmetatable({__tostring=function(self) return self.data end},temp)
return temp
end
for i=0,255 do
local d=bits.new(i).data
bits.ref[i]={d:match("(%d)(%d)(%d)(%d)(%d)(%d)(%d)(%d)")}
bits.ref[tostring(i)]=d
bits.ref[d]=i
bits.ref["\255"..string.char(i)]=d
end
function bits.numToBytes(n,fit,func)
local num=bits.new(n):toSbytes()
num=bin.endianflop(num)
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
end
function bits:conv(n)
local tab={}
while n>=1 do
table.insert(tab,n%2)
n=math.floor(n/2)
end
local str=string.reverse(table.concat(tab))
if #str%8~=0 then
str=string.rep('0',8-#str%8)..str
end
return str
end
function bits:tonumber(s)
if type(s)=='string' then
return tonumber(self.data,2)
end
s=s or 1
return tonumber(string.sub(self.data,(8*(s-1))+1,8*s),2) or error('Bounds!')
end
function bits:isover()
return #self.data>8
end
function bits:flipbits()
tab={}
for i=1,#self.data do
if string.sub(self.data,i,i)=='1' then
table.insert(tab,'0')
else
table.insert(tab,'1')
end
end
self.data=table.concat(tab)
end
function bits: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 bits: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 bits:getBin()
return self.data
end
function bits:getbytes()
return #self.data/8
end
return bits

View File

@ -16,116 +16,25 @@ bits.__tostring=function(self) return self.data end
bits.__len=function(self) return (#self.data)/8 end
bin.lastBlockSize=0
bin.streams={} -- FIX FOR THREADING!!!
bin.base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
-- Helpers
function bin.fileExist(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)
if jit then
bit=require("bit")
elseif bit32 then
bit=bit32
else
return false
bit=require("bin.no_jit_bit")
end
return p
end
function bin.toB64(data)
local bytes = {}
local result = ""
for spos=0,string.len(data)-1,3 do
for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
result = string.format('%s%s%s%s%s',result,bin.base64chars[bits.rsh(bytes[1],2)],bin.base64chars[bits.lor(bits.lsh((bytes[1] % 4),4), bits.rsh(bytes[2],4))] or "=",((#data-spos) > 1) and bin.base64chars[bits.lor(bits.lsh(bytes[2] % 16,2), bits.rsh(bytes[3],6))] or "=",((#data-spos) > 2) and bin.base64chars[(bytes[3] % 64)] or "=")
end
return result
end
bin.base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
function bin.fromB64(data)
local chars = {}
local result=""
for dpos=0,string.len(data)-1,4 do
for char=1,4 do chars[char] = bin.base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
result = string.format('%s%s%s%s',result,string.char(bits.lor(bits.lsh(chars[1],2), bits.rsh(chars[2],4))),(chars[3] ~= nil) and string.char(bits.lor(bits.lsh(chars[2],4), bits.rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(bits.lor(bits.lsh(chars[3],6) % 192, (chars[4]))) or "")
end
return result
end
function bin.toHex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
function bin.fromHex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
-- Constructors
function bin.new(data)
data=tostring(data or "")
local c = {}
setmetatable(c, bin)
c.data=data
c.Type="bin"
c.t="bin"
c.pos=1
c.stream=false
return c
end
function bin.newFromB64(data)
return bin.new(bin.fromB64(data))
end
function bin.newFromHex(data)
return bin.new(bin.fromHex(data))
end
function bin.load(path)
if type(path) ~= "string" then error("Path must be a string!") end
local f = io.open(path, 'rb')
local content = f:read('*a')
f:close()
return bin.new(content)
end
function bin.stream(file,l)
if not(l==false) then l=true end
local c=bin.new()
c.Type="streamable"
c.t="streamable"
if bin.streams[file]~=nil then -- FIX FOR THREADING!!!
c.file=file
c.lock = l
c.workingfile=bin.streams[file].workingfile
c.stream=true
return c
end
if bin.fileExist(file) then
c.file=file
c.lock = l
c.workingfile=io.open(file,'rb+')
else
c.file=file
c.lock = l
c.workingfile=io.open(file,'w')
io.close(c.workingfile)
c.workingfile=io.open(file,'rb+')
end
c.stream=true
bin.streams[file]=c -- FIX FOR THREADING!!!
return c
end
function bin.freshStream(file)
bin.new():tofile(file)
return bin.stream(file,false)
end
function bin.normalizeData(data)
require("bin.utils")
local base64=require("bin.base64")
local base91=require("bin.base91")
bits=require("bin.bits")
function bin.normalizeData(data) -- unified function to allow
if type(data)=="string" then return data end
if type(data)=="table" then
if data.Type=="bin" or data.Type=="streamable" then
return data:getData()
elseif data.Type=="bits" then
-- LATER
return data:toSbytes()
elseif data.Type=="buffer" then
-- LATER
elseif data.Type=="sink" then
@ -143,6 +52,104 @@ function bin.normalizeData(data)
end
end
end
function bin.fileExist(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 bin.toHex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end
function bin.fromHex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
function bin.toBase64(s)
return base64.encode(s)
end
function bin.fromBase64(s)
return base64.decode(s)
end
function bin.toBase91(s)
return base91.encode(s)
end
function bin.fromBase91(s)
return base91.decode(s)
end
-- Constructors
function bin.new(data)
data=tostring(data or "")
local c = {}
setmetatable(c, bin)
c.data=data
c.Type="bin"
c.t="bin"
c.pos=1
c.stream=false
return c
end
function bin.newFromBase64(data)
return bin.new(bin.fromBase64(data))
end
function bin.newFromBase91(data)
return bin.new(bin.fromBase91(data))
end
function bin.newFromHex(data)
return bin.new(bin.fromHex(data))
end
function bin.load(path)
if type(path) ~= "string" then error("Path must be a string!") end
local f = io.open(path, 'rb')
local content = f:read('*a')
f:close()
return bin.new(content)
end
function bin.stream(file,l)
if not(l==false) then l=true end
local c=bin.new()
c.Type="streamable"
c.t="streamable"
if bin.streams[file]~=nil then
c.file=file
c.lock = l
c.workingfile=bin.streams[file][1].workingfile
bin.streams[file][2]=bin.streams[file][2]+1
c.stream=true
return c
end
if bin.fileExist(file) then
c.file=file
c.lock = l
c.workingfile=io.open(file,'rb+')
else
c.file=file
c.lock = l
c.workingfile=io.open(file,'w')
io.close(c.workingfile)
c.workingfile=io.open(file,'rb+')
end
c.stream=true
bin.streams[file]={c,1}
return c
end
function bin.freshStream(file)
bin.new():tofile(file)
return bin.stream(file,false)
end
-- Core Methods
function bin:canStreamWrite()
return (self.stream and not(self.lock))
@ -184,6 +191,8 @@ function bin:write(data,size)
local size=tonumber(size or dsize)
if dsize>size then
data = data:sub(1,size)
elseif dsize<size then
data=data..string.rep("\0",size-dsize)
end
if self:canStreamWrite() then
self.workingfile:write(data)
@ -248,10 +257,32 @@ function bin:getSize(fmt)
return len
end
end
function bin:tackE(data,size)
function bin:tackE(data,size,h)
local data=bin.normalizeData(data)
local cur=self:getSize()
self:seekSet(self:getSize()+1)
self:write(data,size)
if h then
self:seekSet(cur+1)
end
end
function bin:tonumber(a,b)
local temp={}
if a then
temp.data=self:sub(a,b)
else
temp=self
end
local l,r=0,0
local g=#temp.data
for i=1,g do
r=r+(256^(g-i))*string.byte(string.sub(temp.data,i,i))
l=l+(256^(i-1))*string.byte(string.sub(temp.data,i,i))
end
return r,l
end
function bin.endianflop(data)
return string.reverse(data)
end
function bin:tofile(name)
if self.stream then return end
@ -260,34 +291,22 @@ function bin:tofile(name)
file:write(self.data)
file:close()
end
-- Tests
test=bin.freshStream("../test.dat",false) -- you must stream unlocked to be able to write to it
test2=bin.load("../test.dat")
test3=bin.new()
print("From Stream\n-----------")
test:seek(2)
test:write("Like Food",4)
test:write("!!",2)
test:seek(10)
test:write("Hmmmmmm")
test:tackE("THE END YO!!!")
test:write("@")
print(test)
print("\nFrom Virtual File\n-----------------")
test2:seek(2)
test2:write("Like Food",4)
test2:write("!!",2)
test2:seek(10)
test2:write("Hmmmmmm")
test2:tackE("THE END YO!!!")
test2:write("@")
print(test2)
print("\nFrom Virtual File2\n-----------------")
test3:seek(2)
test3:write("Like Food",4)
test3:write("!!",2)
test3:seek(10)
test3:write("Hmmmmmm")
test3:tackE("THE END YO!!!")
test3:write("@")
print(test3)
function bin.getnumber(num,len,fmt,func)
local num=bits.numToBytes(num,len,func)
if fmt=="%B" then
return bin.endianflop(num)
end
return num
end
function bin:close()
if self.stream then
if bin.streams[self.file][2]==1 then
bin.streams[self.file]=nil
self.workingfile:close()
else
bin.streams[self.file][2]=bin.streams[self.file][2]-1
self.workingfile=io.tmpfile()
self.workingfile:close()
end
end
end

396
rewritedata/bin/md5.lua Normal file
View File

@ -0,0 +1,396 @@
local md5 = {
_VERSION = "md5.lua 1.1.0",
_DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)",
_URL = "https://github.com/kikito/md5.lua",
_LICENSE = [[
MIT LICENSE
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.
]]
}
-- bit lib implementions
local char, byte, format, rep, sub =
string.char, string.byte, string.format, string.rep, string.sub
local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift
local ok, bit = pcall(require, 'bit')
if ok then
bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift = bit.bor, bit.band, bit.bnot, bit.bxor, bit.rshift, bit.lshift
else
ok, bit = pcall(require, 'bit32')
if ok then
bit_not = bit.bnot
local tobit = function(n)
return n <= 0x7fffffff and n or -(bit_not(n) + 1)
end
local normalize = function(f)
return function(a,b) return tobit(f(tobit(a), tobit(b))) end
end
bit_or, bit_and, bit_xor = normalize(bit.bor), normalize(bit.band), normalize(bit.bxor)
bit_rshift, bit_lshift = normalize(bit.rshift), normalize(bit.lshift)
else
local function tbl2number(tbl)
local result = 0
local power = 1
for i = 1, #tbl do
result = result + tbl[i] * power
power = power * 2
end
return result
end
local function expand(t1, t2)
local big, small = t1, t2
if(#big < #small) then
big, small = small, big
end
-- expand small
for i = #small + 1, #big do
small[i] = 0
end
end
local to_bits -- needs to be declared before bit_not
bit_not = function(n)
local tbl = to_bits(n)
local size = math.max(#tbl, 32)
for i = 1, size do
if(tbl[i] == 1) then
tbl[i] = 0
else
tbl[i] = 1
end
end
return tbl2number(tbl)
end
-- defined as local above
to_bits = function (n)
if(n < 0) then
-- negative
return to_bits(bit_not(math.abs(n)) + 1)
end
-- to bits table
local tbl = {}
local cnt = 1
local last
while n > 0 do
last = n % 2
tbl[cnt] = last
n = (n-last)/2
cnt = cnt + 1
end
return tbl
end
bit_or = function(m, n)
local tbl_m = to_bits(m)
local tbl_n = to_bits(n)
expand(tbl_m, tbl_n)
local tbl = {}
for i = 1, #tbl_m do
if(tbl_m[i]== 0 and tbl_n[i] == 0) then
tbl[i] = 0
else
tbl[i] = 1
end
end
return tbl2number(tbl)
end
bit_and = function(m, n)
local tbl_m = to_bits(m)
local tbl_n = to_bits(n)
expand(tbl_m, tbl_n)
local tbl = {}
for i = 1, #tbl_m do
if(tbl_m[i]== 0 or tbl_n[i] == 0) then
tbl[i] = 0
else
tbl[i] = 1
end
end
return tbl2number(tbl)
end
bit_xor = function(m, n)
local tbl_m = to_bits(m)
local tbl_n = to_bits(n)
expand(tbl_m, tbl_n)
local tbl = {}
for i = 1, #tbl_m do
if(tbl_m[i] ~= tbl_n[i]) then
tbl[i] = 1
else
tbl[i] = 0
end
end
return tbl2number(tbl)
end
bit_rshift = function(n, bits)
local high_bit = 0
if(n < 0) then
-- negative
n = bit_not(math.abs(n)) + 1
high_bit = 0x80000000
end
local floor = math.floor
for i=1, bits do
n = n/2
n = bit_or(floor(n), high_bit)
end
return floor(n)
end
bit_lshift = function(n, bits)
if(n < 0) then
-- negative
n = bit_not(math.abs(n)) + 1
end
for i=1, bits do
n = n*2
end
return bit_and(n, 0xFFFFFFFF)
end
end
end
-- convert little-endian 32-bit int to a 4-char string
local function lei2str(i)
local f=function (s) return char( bit_and( bit_rshift(i, s), 255)) end
return f(0)..f(8)..f(16)..f(24)
end
-- convert raw string to big-endian int
local function str2bei(s)
local v=0
for i=1, #s do
v = v * 256 + byte(s, i)
end
return v
end
-- convert raw string to little-endian int
local function str2lei(s)
local v=0
for i = #s,1,-1 do
v = v*256 + byte(s, i)
end
return v
end
-- cut up a string in little-endian ints of given size
local function cut_le_str(s,...)
local o, r = 1, {}
local args = {...}
for i=1, #args do
table.insert(r, str2lei(sub(s, o, o + args[i] - 1)))
o = o + args[i]
end
return r
end
local swap = function (w) return str2bei(lei2str(w)) end
-- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh)
-- 10/02/2001 jcw@equi4.com
local CONSTS = {
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476
}
local f=function (x,y,z) return bit_or(bit_and(x,y),bit_and(-x-1,z)) end
local g=function (x,y,z) return bit_or(bit_and(x,z),bit_and(y,-z-1)) end
local h=function (x,y,z) return bit_xor(x,bit_xor(y,z)) end
local i=function (x,y,z) return bit_xor(y,bit_or(x,-z-1)) end
local z=function (ff,a,b,c,d,x,s,ac)
a=bit_and(a+ff(b,c,d)+x+ac,0xFFFFFFFF)
-- be *very* careful that left shift does not cause rounding!
return bit_or(bit_lshift(bit_and(a,bit_rshift(0xFFFFFFFF,s)),s),bit_rshift(a,32-s))+b
end
local function transform(A,B,C,D,X)
local a,b,c,d=A,B,C,D
local t=CONSTS
a=z(f,a,b,c,d,X[ 0], 7,t[ 1])
d=z(f,d,a,b,c,X[ 1],12,t[ 2])
c=z(f,c,d,a,b,X[ 2],17,t[ 3])
b=z(f,b,c,d,a,X[ 3],22,t[ 4])
a=z(f,a,b,c,d,X[ 4], 7,t[ 5])
d=z(f,d,a,b,c,X[ 5],12,t[ 6])
c=z(f,c,d,a,b,X[ 6],17,t[ 7])
b=z(f,b,c,d,a,X[ 7],22,t[ 8])
a=z(f,a,b,c,d,X[ 8], 7,t[ 9])
d=z(f,d,a,b,c,X[ 9],12,t[10])
c=z(f,c,d,a,b,X[10],17,t[11])
b=z(f,b,c,d,a,X[11],22,t[12])
a=z(f,a,b,c,d,X[12], 7,t[13])
d=z(f,d,a,b,c,X[13],12,t[14])
c=z(f,c,d,a,b,X[14],17,t[15])
b=z(f,b,c,d,a,X[15],22,t[16])
a=z(g,a,b,c,d,X[ 1], 5,t[17])
d=z(g,d,a,b,c,X[ 6], 9,t[18])
c=z(g,c,d,a,b,X[11],14,t[19])
b=z(g,b,c,d,a,X[ 0],20,t[20])
a=z(g,a,b,c,d,X[ 5], 5,t[21])
d=z(g,d,a,b,c,X[10], 9,t[22])
c=z(g,c,d,a,b,X[15],14,t[23])
b=z(g,b,c,d,a,X[ 4],20,t[24])
a=z(g,a,b,c,d,X[ 9], 5,t[25])
d=z(g,d,a,b,c,X[14], 9,t[26])
c=z(g,c,d,a,b,X[ 3],14,t[27])
b=z(g,b,c,d,a,X[ 8],20,t[28])
a=z(g,a,b,c,d,X[13], 5,t[29])
d=z(g,d,a,b,c,X[ 2], 9,t[30])
c=z(g,c,d,a,b,X[ 7],14,t[31])
b=z(g,b,c,d,a,X[12],20,t[32])
a=z(h,a,b,c,d,X[ 5], 4,t[33])
d=z(h,d,a,b,c,X[ 8],11,t[34])
c=z(h,c,d,a,b,X[11],16,t[35])
b=z(h,b,c,d,a,X[14],23,t[36])
a=z(h,a,b,c,d,X[ 1], 4,t[37])
d=z(h,d,a,b,c,X[ 4],11,t[38])
c=z(h,c,d,a,b,X[ 7],16,t[39])
b=z(h,b,c,d,a,X[10],23,t[40])
a=z(h,a,b,c,d,X[13], 4,t[41])
d=z(h,d,a,b,c,X[ 0],11,t[42])
c=z(h,c,d,a,b,X[ 3],16,t[43])
b=z(h,b,c,d,a,X[ 6],23,t[44])
a=z(h,a,b,c,d,X[ 9], 4,t[45])
d=z(h,d,a,b,c,X[12],11,t[46])
c=z(h,c,d,a,b,X[15],16,t[47])
b=z(h,b,c,d,a,X[ 2],23,t[48])
a=z(i,a,b,c,d,X[ 0], 6,t[49])
d=z(i,d,a,b,c,X[ 7],10,t[50])
c=z(i,c,d,a,b,X[14],15,t[51])
b=z(i,b,c,d,a,X[ 5],21,t[52])
a=z(i,a,b,c,d,X[12], 6,t[53])
d=z(i,d,a,b,c,X[ 3],10,t[54])
c=z(i,c,d,a,b,X[10],15,t[55])
b=z(i,b,c,d,a,X[ 1],21,t[56])
a=z(i,a,b,c,d,X[ 8], 6,t[57])
d=z(i,d,a,b,c,X[15],10,t[58])
c=z(i,c,d,a,b,X[ 6],15,t[59])
b=z(i,b,c,d,a,X[13],21,t[60])
a=z(i,a,b,c,d,X[ 4], 6,t[61])
d=z(i,d,a,b,c,X[11],10,t[62])
c=z(i,c,d,a,b,X[ 2],15,t[63])
b=z(i,b,c,d,a,X[ 9],21,t[64])
return bit_and(A+a,0xFFFFFFFF),bit_and(B+b,0xFFFFFFFF),
bit_and(C+c,0xFFFFFFFF),bit_and(D+d,0xFFFFFFFF)
end
----------------------------------------------------------------
local function md5_update(self, s)
self.pos = self.pos + #s
s = self.buf .. s
for ii = 1, #s - 63, 64 do
local X = cut_le_str(sub(s,ii,ii+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
assert(#X == 16)
X[0] = table.remove(X,1) -- zero based!
self.a,self.b,self.c,self.d = transform(self.a,self.b,self.c,self.d,X)
end
self.buf = sub(s, math.floor(#s/64)*64 + 1, #s)
return self
end
local function md5_finish(self)
local msgLen = self.pos
local padLen = 56 - msgLen % 64
if msgLen % 64 > 56 then padLen = padLen + 64 end
if padLen == 0 then padLen = 64 end
local s = char(128) .. rep(char(0),padLen-1) .. lei2str(bit_and(8*msgLen, 0xFFFFFFFF)) .. lei2str(math.floor(msgLen/0x20000000))
md5_update(self, s)
assert(self.pos % 64 == 0)
return lei2str(self.a) .. lei2str(self.b) .. lei2str(self.c) .. lei2str(self.d)
end
----------------------------------------------------------------
function md5.new()
return { a = CONSTS[65], b = CONSTS[66], c = CONSTS[67], d = CONSTS[68],
pos = 0,
buf = '',
update = md5_update,
finish = md5_finish }
end
function md5.tohex(s)
return format("%08x%08x%08x%08x", str2bei(sub(s, 1, 4)), str2bei(sub(s, 5, 8)), str2bei(sub(s, 9, 12)), str2bei(sub(s, 13, 16)))
end
function md5.sum(s)
return md5.new():update(s):finish()
end
function md5.sumhexa(s)
return md5.tohex(md5.sum(s))
end
return md5

View File

@ -0,0 +1,545 @@
--[[
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
(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 floor = math.floor
local MOD = 2^32
local MODM = MOD-1
local function memoize(f)
local mt = {}
local t = setmetatable({}, mt)
function mt:__index(k)
local v = f(k); t[k] = v
return v
end
return t
end
local function make_bitop_uncached(t, m)
local function bitop(a, b)
local res,p = 0,1
while a ~= 0 and b ~= 0 do
local am, bm = a%m, b%m
res = res + t[am][bm]*p
a = (a - am) / m
b = (b - bm) / m
p = p*m
end
res = res + (a+b)*p
return res
end
return bitop
end
local function make_bitop(t)
local op1 = make_bitop_uncached(t,2^1)
local op2 = memoize(function(a)
return memoize(function(b)
return op1(a, b)
end)
end)
return make_bitop_uncached(op2, 2^(t.n or 1))
end
-- ok? probably not if running on a 32-bit int Lua number type platform
function M.tobit(x)
return x % 2^32
end
M.bxor = make_bitop {[0]={[0]=0,[1]=1},[1]={[0]=1,[1]=0}, n=4}
local bxor = M.bxor
function M.bnot(a) return MODM - a end
local bnot = M.bnot
function M.band(a,b) return ((a+b) - bxor(a,b))/2 end
local band = M.band
function M.bor(a,b) return MODM - band(MODM - a, MODM - b) end
local bor = M.bor
local lshift, rshift -- forward declare
function M.rshift(a,disp) -- Lua5.2 insipred
if disp < 0 then return lshift(a,-disp) end
return floor(a % 2^32 / 2^disp)
end
rshift = M.rshift
function M.lshift(a,disp) -- Lua5.2 inspired
if disp < 0 then return rshift(a,-disp) end
return (a * 2^disp) % 2^32
end
lshift = M.lshift
function M.tohex(x, n) -- BitOp style
n = n or 8
local up
if n <= 0 then
if n == 0 then return '' end
up = true
n = - n
end
x = band(x, 16^n-1)
return ('%0'..n..(up and 'X' or 'x')):format(x)
end
local tohex = M.tohex
function M.extract(n, field, width) -- Lua5.2 inspired
width = width or 1
return band(rshift(n, field), 2^width-1)
end
local extract = M.extract
function M.replace(n, v, field, width) -- Lua5.2 inspired
width = width or 1
local mask1 = 2^width-1
v = band(v, mask1) -- required by spec?
local mask = bnot(lshift(mask1, field))
return band(n, mask) + lshift(v, field)
end
local replace = M.replace
function M.bswap(x) -- BitOp style
local a = band(x, 0xff); x = rshift(x, 8)
local b = band(x, 0xff); x = rshift(x, 8)
local c = band(x, 0xff); x = rshift(x, 8)
local d = band(x, 0xff)
return lshift(lshift(lshift(a, 8) + b, 8) + c, 8) + d
end
local bswap = M.bswap
function M.rrotate(x, disp) -- Lua5.2 inspired
disp = disp % 32
local low = band(x, 2^disp-1)
return rshift(x, disp) + lshift(low, 32-disp)
end
local rrotate = M.rrotate
function M.lrotate(x, disp) -- Lua5.2 inspired
return rrotate(x, -disp)
end
local lrotate = M.lrotate
M.rol = M.lrotate -- LuaOp inspired
M.ror = M.rrotate -- LuaOp insipred
function M.arshift(x, disp) -- Lua5.2 inspired
local z = rshift(x, disp)
if x >= 0x80000000 then z = z + lshift(2^disp-1, 32-disp) end
return z
end
local arshift = M.arshift
function M.btest(x, y) -- Lua5.2 inspired
return band(x, y) ~= 0
end
--
-- Start Lua 5.2 "bit32" compat section.
--
M.bit32 = {} -- Lua 5.2 'bit32' compatibility
local function bit32_bnot(x)
return (-1 - x) % MOD
end
M.bit32.bnot = bit32_bnot
local function bit32_bxor(a, b, c, ...)
local z
if b then
a = a % MOD
b = b % MOD
z = bxor(a, b)
if c then
z = bit32_bxor(z, c, ...)
end
return z
elseif a then
return a % MOD
else
return 0
end
end
M.bit32.bxor = bit32_bxor
local function bit32_band(a, b, c, ...)
local z
if b then
a = a % MOD
b = b % MOD
z = ((a+b) - bxor(a,b)) / 2
if c then
z = bit32_band(z, c, ...)
end
return z
elseif a then
return a % MOD
else
return MODM
end
end
M.bit32.band = bit32_band
local function bit32_bor(a, b, c, ...)
local z
if b then
a = a % MOD
b = b % MOD
z = MODM - band(MODM - a, MODM - b)
if c then
z = bit32_bor(z, c, ...)
end
return z
elseif a then
return a % MOD
else
return 0
end
end
M.bit32.bor = bit32_bor
function M.bit32.btest(...)
return bit32_band(...) ~= 0
end
function M.bit32.lrotate(x, disp)
return lrotate(x % MOD, disp)
end
function M.bit32.rrotate(x, disp)
return rrotate(x % MOD, disp)
end
function M.bit32.lshift(x,disp)
if disp > 31 or disp < -31 then return 0 end
return lshift(x % MOD, disp)
end
function M.bit32.rshift(x,disp)
if disp > 31 or disp < -31 then return 0 end
return rshift(x % MOD, disp)
end
function M.bit32.arshift(x,disp)
x = x % MOD
if disp >= 0 then
if disp > 31 then
return (x >= 0x80000000) and MODM or 0
else
local z = rshift(x, disp)
if x >= 0x80000000 then z = z + lshift(2^disp-1, 32-disp) end
return z
end
else
return lshift(x, -disp)
end
end
function M.bit32.extract(x, field, ...)
local width = ... or 1
if field < 0 or field > 31 or width < 0 or field+width > 32 then error 'out of range' end
x = x % MOD
return extract(x, field, ...)
end
function M.bit32.replace(x, v, field, ...)
local width = ... or 1
if field < 0 or field > 31 or width < 0 or field+width > 32 then error 'out of range' end
x = x % MOD
v = v % MOD
return replace(x, v, field, ...)
end
--
-- Start LuaBitOp "bit" compat section.
--
M.bit = {} -- LuaBitOp "bit" compatibility
function M.bit.tobit(x)
x = x % MOD
if x >= 0x80000000 then x = x - MOD end
return x
end
local bit_tobit = M.bit.tobit
function M.bit.tohex(x, ...)
return tohex(x % MOD, ...)
end
function M.bit.bnot(x)
return bit_tobit(bnot(x % MOD))
end
local function bit_bor(a, b, c, ...)
if c then
return bit_bor(bit_bor(a, b), c, ...)
elseif b then
return bit_tobit(bor(a % MOD, b % MOD))
else
return bit_tobit(a)
end
end
M.bit.bor = bit_bor
local function bit_band(a, b, c, ...)
if c then
return bit_band(bit_band(a, b), c, ...)
elseif b then
return bit_tobit(band(a % MOD, b % MOD))
else
return bit_tobit(a)
end
end
M.bit.band = bit_band
local function bit_bxor(a, b, c, ...)
if c then
return bit_bxor(bit_bxor(a, b), c, ...)
elseif b then
return bit_tobit(bxor(a % MOD, b % MOD))
else
return bit_tobit(a)
end
end
M.bit.bxor = bit_bxor
function M.bit.lshift(x, n)
return bit_tobit(lshift(x % MOD, n % 32))
end
function M.bit.rshift(x, n)
return bit_tobit(rshift(x % MOD, n % 32))
end
function M.bit.arshift(x, n)
return bit_tobit(arshift(x % MOD, n % 32))
end
function M.bit.rol(x, n)
return bit_tobit(lrotate(x % MOD, n % 32))
end
function M.bit.ror(x, n)
return bit_tobit(rrotate(x % MOD, n % 32))
end
function M.bit.bswap(x)
return bit_tobit(bswap(x % MOD))
end
return M

360
rewritedata/bin/utils.lua Normal file
View File

@ -0,0 +1,360 @@
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

View File

View File

@ -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.