diff --git a/.gitignore b/.gitignore index c57d483..c2c3650 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ crypto.lua crypto.lua crypto.lua +crypto.lua diff --git a/bin/init.lua b/bin/init.lua index 82977cf..3728c6f 100644 --- a/bin/init.lua +++ b/bin/init.lua @@ -1,5 +1,5 @@ bin={} -bin.Version={5,0,6} +bin.Version={5,1,0} bin.stage='stable' bin.data='' bin.t='bin' diff --git a/bin/numbers/bits.lua b/bin/numbers/bits.lua index 0f8bafb..579f320 100644 --- a/bin/numbers/bits.lua +++ b/bin/numbers/bits.lua @@ -5,6 +5,7 @@ bits.Type='bits' bits.__index = bits bits.__tostring=function(self) return self.data end bits.__len=function(self) return (#self.data)/8 end +local floor,insert = math.floor, table.insert function bits.newBitBuffer(n) -- end @@ -12,6 +13,22 @@ function bits.newConverter(bitsIn,bitsOut) local c={} -- end +function basen(n,b) + if not b or b == 10 then return tostring(n) end + local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + local t = {} + local sign = "" + if n < 0 then + sign = "-" + n = -n + end + repeat + local d = n % b + 1 + n = n / b + insert(t, 1, digits:sub(d,d)) + until n == 0 + return sign .. table.concat(t,"") +end bits.ref={} function bits.newByte(d) local c={} @@ -67,42 +84,27 @@ function bits.newByteArray(s) 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={} +function bits.new(n,binary) + local temp={} + temp.t="bits" + temp.Type="bits" + 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,bits:conv(string.byte(n,i))) end - n=table.concat(t) - else - n=t + temp.data=table.concat(t) end + elseif type(n)=="number" or type(n)=="table" then + temp.data=basen(n,2) + end + if #temp.data%8~=0 then + temp.data=string.rep('0',8-#temp.data%8)..temp.data end - local temp={} - temp.t='bits' - temp.Type="bits" 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 - temp.data=string.rep('0',8-(#str))..str - elseif #str==0 then - temp.data="00000000" - end - else - temp.data=n or "00000000" - end - setmetatable({__tostring=function(self) return self.data end},temp) return temp end for i=0,255 do @@ -112,48 +114,25 @@ for i=0,255 do bits.ref[d]=i bits.ref["\255"..string.char(i)]=d end -function bits.numToBytes(n,fit,fmt,func) - if fmt=="%e" then - 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 num +function bits.numToBytes(n,fit,func) + 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 - return string.rep("\0",fit-#num)..num + print("Warning: attempting to store a number that takes up more space than allotted!") end - else - return num - end - - else - 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 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 bits:conv(n) diff --git a/crypto.lua b/crypto.lua index 90a1578..cc07f93 100644 --- a/crypto.lua +++ b/crypto.lua @@ -1,44 +1,6 @@ -function bubbleSort(A) - local itemCount=#A - local hasChanged - repeat - for i=1,#A do - io.write(string.char(A[i])) - end - io.write("\n") - hasChanged = false - itemCount=itemCount - 1 - for i = 1, itemCount do - if A[i] > A[i + 1] then - A[i], A[i + 1] = A[i + 1], A[i] - hasChanged = true - end - end - until hasChanged == false -end -reflist={"A","S","S","I","G","N","M","E","N","T"} -list={} -list2={} -for i,v in pairs(reflist) do - list[#list+1]=string.byte(v) - list2[#list2+1]=string.byte(v) -end -function SelectionSort(f) - for k = 1, #f-1 do - for i=1,#f do - io.write(string.char(f[i])) - end - io.write("\n") - local idx = k - for i = k+1, #f do - if f[i] < f[idx] then - idx = i - end - end - f[k], f[idx] = f[idx], f[k] - end -end -print("Bubble Sort") -bubbleSort(list) -print("Selection Sort") -SelectionSort(list2) +package.path="?/init.lua;"..package.path +require("bin") +--~ test=bin.load("test.dat") +--~ print(test:getBlock("n",4)) +--~ print(test:getBlock("n",4)) +--~ tab=test:getBlock("t") diff --git a/rockspecs/bin-5.1-0.rockspec b/rockspecs/bin-5.1-0.rockspec new file mode 100644 index 0000000..cbf08aa --- /dev/null +++ b/rockspecs/bin-5.1-0.rockspec @@ -0,0 +1,38 @@ +package = "bin" +version = "5.1-0" +source = { + url = "git://github.com/rayaman/bin.git", + tag = "v5.1.0", +} +description = { + summary = "Lua Binary ManIpulatioN library", + detailed = [[ + This library contains many methods for working with files at the binary level. It can handle sterilization of all lua objects except userdata. It can even handle self recursion in talbes. It provides a bit, bits, infinabits, base64/91, lzw, md5 hashing, bignum, random, and a virtual file system(Soon, check out oldbin.lua for that) module. + The bit library is the same that comes with luajit. the bits/infinabits library deals with 1's and 0's used for numbers. bits is faster than infinabits, but is limited to 32/64 bits based on which version of lua you are working on. Base64/91 is provided, but since it is done in pure lua it is slower. Check out the github for more info. + ]], + homepage = "https://github.com/rayaman/bin", + license = "MIT" +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + -- Note the required Lua syntax when listing submodules as keys + ["bin.init"] = "bin/init.lua", + ["bin.converters.base64"] = "bin/converters/base64.lua", + ["bin.converters.base91"] = "bin/converters/base91.lua", + ["bin.numbers.BigNum"] = "bin/numbers/BigNum.lua", + ["bin.numbers.BigRat"] = "bin/numbers/BigRat.lua", + ["bin.numbers.bits"] = "bin/numbers/bits.lua", + ["bin.support.extrablocks"] = "bin/support/extrablocks.lua", + ["bin.numbers.infinabits"] = "bin/numbers/infinabits.lua", + ["bin.compressors.lzw"] = "bin/compressors/lzw.lua", + ["bin.hashes.md5"] = "bin/hashes/md5.lua", + ["bin.numbers.no_jit_bit"] = "bin/numbers/no_jit_bit.lua", + ["bin.numbers.random"] = "bin/numbers/random.lua", + ["bin.support.utils"] = "bin/support/utils.lua", + ["bin.support.vfs"] = "bin/support/vfs.lua", + } +} \ No newline at end of file diff --git a/test.dat b/test.dat index 583e14d..64cb999 100644 Binary files a/test.dat and b/test.dat differ