Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not docs then os.loadAPI("apis/docs.lua") end
- --Type registry
- local types = {
- ["Bits"]={colors.lightBlue,"A series of bits from 1-8. A byte would be represented by 87654321"},
- ["Byte"]={colors.yellow,"A single byte, number 0-255"},
- ["DecodedBlock"]={colors.lightBlue,"A list of decoded bytes"},
- ["HuffmanCode"]={colors.purple,"A code for decompressing values"}
- }
- for k,v in pairs(types) do
- docs.registerType(k,v[1],v[2])
- end
- --Takes a byte and turns it into a series of bits
- --@Arg Byte byte: A byte 0-255 to be converted
- --@OptionalArg boolean bool: If true, converts the bits to false/true instead of 0/1
- --@Returns Bits bits: A table of the bits from 0-7
- local function byteToBits(byte,bool)
- local bits = {}
- for i=0,7 do
- bits[i] = byte%2
- if bool then bits[i] = bits[i] == 1 end
- byte = math.floor(byte/2)
- end
- return bits
- end
- --Basically your python version of if statements
- --@Arg any element: The element do be converted into a boolean
- --@Returns boolean e: The finished result
- function bool(element)
- if not element then return false end
- if tonumber(element) then return number ~= 0 end
- if type(element) == "string" or type(element) == "table" then return #element ~= 0 end
- if type(element) == "boolean" then return element end
- return true
- end
- --Increments a bit pos and the bytes index
- --@Arg number pos: The position within a byte
- --@Arg number index: The index with a bytes table
- --@Returns number newPos: The new position
- --@Returns number newIndex: The new index
- local function incrementPos(pos,index)
- pos = pos+1
- if pos >= 8 then index = index+1 end
- return pos%8,index
- end
- --Gets the next bit from a byte
- --@Arg table bytes: The list of bytes
- --@Arg table bitpos: {pos= position within bit, index = index within bytes table}
- --@Returns number bit: The bit
- local function nextBit(bytes,bitpos)
- print(bitpos.pos .. " " .. bitpos.index .. " ")
- local b = byteToBits(bytes[bitpos.index])[bitpos.pos]
- --print(b)
- bitpos.pos,bitpos.index = incrementPos(bitpos.pos,bitpos.index)
- return b
- end
- --Gets the next byte, resetting the bit pos to 0
- --@Arg table bytes: the list of bytes
- --@Arg table bitpos: {pos= position within bit, index = index within bytes table}
- --@Returns Byte b: the next byte to be used
- local function nextByte(bytes,bitpos)
- local b = bytes[bitpos.index]
- bitpos.pos,bitpos.index = 0,bitpos.index+1
- return b
- end
- --Gets the next series of bits as a number, big endian
- --@Arg table bytes: the list of bytes
- --@Arg table bitpos: {pos= position within bit, index = index within bytes table}
- --@OptionalArg number amount: The number of bits that should be read
- --@Returns number num: The value of the read bits
- local function nextBits(bytes,bitpos,amount)
- local num = 0
- amount = tonumber(amount) or 1
- for i=1,amount do
- local b = nextBit(bytes,bitpos)
- num = num*2+b
- end
- print(num)
- return num
- end
- --Creates a huffman code
- --@OptionalArg table bytes: The series of bytes to create the sequence from. If not provided, will generate the default huffman
- --@OptionalArg table bitpos: {pos= position within bit, index = index within bytes table}
- --@Returns HuffmanCode h: A table with {decodeNext=function(bytes,bitpos)}
- local function Huffman(bytes,bitpos)
- if not bytes then
- return {decodeNext = function(by,bp)
- local v = nextBits(by,bp,7)
- if v <= 23 then
- return 256+v
- end
- v = 2*v+nextBit(by,bp)
- if v >= 48 and v <= 191 then
- return v - 48
- elseif v >= 192 and v <= 199 then
- return v + 88
- end
- v = 2*v+nextBit(by,bp)
- if v >= 400 then return v+256 end
- return 256 --Exit character
- end,
- decodeLength = function(by,bp,v)
- v = v - 254
- w = math.max(math.floor((v-7)/4),0) --number of additional bits to read
- y = (v-7)%4
- return false
- end
- }
- end
- end
- --from zzlib
- local function hufftable_create(depths)
- local nvalues = #depths
- local nbits = 1
- local bl_count = {}
- local next_code = {}
- for i=1,nvalues do
- local d = depths[i]
- if d > nbits then
- nbits = d
- end
- bl_count[d] = (bl_count[d] or 0) + 1
- end
- local table = {}
- local code = 0
- bl_count[0] = 0
- for i=1,nbits do
- code = (code + (bl_count[i-1] or 0)) * 2
- next_code[i] = code
- end
- for i=1,nvalues do
- local len = depths[i] or 0
- if len > 0 then
- local e = (i-1)*16 + len
- local code = next_code[len]
- local rcode = 0
- for j=1,len do
- rcode = rcode + bit.blshift(bit.band(1,bit.brshift(code,j-1)),len-j)
- end
- for j=0,2^nbits-1,2^len do
- table[j+rcode] = e
- end
- next_code[len] = next_code[len] + 1
- end
- end
- return table,nbits
- end
- --@Type Huffman
- local cnt = { 144, 112, 24, 8 }
- local dpt = { 8, 9, 7, 8 }
- local depths = {}
- for i=1,4 do
- local d = dpt[i]
- for j=1,cnt[i] do
- table.insert(depths,d)
- end
- end
- local LITTABLE,nlit = hufftable_create(depths)
- depths = {}
- for i=1,32 do
- depths[i] = 5
- end
- local DISTTABLE,ndist = hufftable_create(depths)
- --Gets the stupid member thingie
- --@Arg File file: The file opened in rb mode to read
- --@Returns boolean false: If sussy stuff happens
- --@Returns Byte id1, Byte id2, Byte cm, number mtime, Byte xfl, byte opSys, table extra, string name, string comment, number fhcrc, table data, number crc32, number iSize
- local function readMember(file)
- local name, comment = "",""
- local fhcrc
- local extra = {}
- local id1,id2 = file.read(), file.read() --bytes 1 and 2 are for the ids
- local cm = file.read() --byte 3 is for the compression method, usually 0x08
- local flgs = byteToBits(file.read(),true)
- --Ignore flgs[0] because we dont care about text/nontext. Dealing with flgs[1] FHCRC later
- local mtime = 0
- for i=0,3 do
- mtime = file.read()*(256^i)+mtime --changed to little endian
- end
- local xfl = file.read()
- local opSys = file.read()
- if flgs[2] then --FEXTRA
- local len = file.read()
- len = len+256*file.read() --changed to little endian
- while len > 0 do
- local si1 = file.read()
- local si2 = file.read()
- local sLen = file.read()
- sLen = sLen+256*file.read() --changed to little endian
- len = len - 4
- local sData = {}
- while sLen > 0 and len > 0 do
- table.insert(sData,file.read())
- sLen = sLen-1
- len = len-1
- end
- extra[si1+si2*256] = sData --little endian as well for index
- end
- end
- if flgs[3] then --FNAME
- name = ""
- local byte = file.read()
- while byte > 0 do
- name = name .. string.char(byte)
- byte = file.read()
- end
- end
- if flgs[4] then --FCOMMENT
- comment = ""
- local byte = file.read()
- while byte > 0 do
- name = name .. string.char(byte)
- byte = file.read()
- end
- end
- if flgs[1] then --dealing with FHCRC
- fhcrc = file.read()
- fhcrc = fhcrc+256*file.read() --made little endian
- end
- --Reading the data
- local data = {}
- while file.read(0) do
- table.insert(data,file.read())
- end
- local crc32 = 0
- for i=#data-7,#data-4 do
- crc32 = crc32+data[i]*256 --little endian
- end
- local iSize = 0
- for i=#data-3,#data do
- iSize = iSize+256*data[i] --little endian
- end
- return id1,id2,cm,mtime,xfl,opSys,extra,name,comment,fhcrc,data,crc32,iSize
- end
- --Gets the next value from a huffman table
- local function getNextV(huff,bytes,bitpos)
- local b = 0
- for i=1,10 do
- b = b*2+nextBit(bytes,bitpos)
- if huff[b] then return huff[b] end
- end
- return 256
- end
- --@Arg table bytes: A list of bytes from the data
- --@Arg number pos: The position within the bytes to start at, 0-7
- --@Arg number index: The position of the starting byte in the bytes table
- --@OptionalArg table dataBlocks: The list of blocks from the bytes
- local function inflateBlock(bytes,pos,index, dataBlocks)
- if type(dataBlocks) ~= "table" then dataBlocks = {} end
- local bits = byteToBits(bytes[index])
- local bitPos = {pos=pos,index=index} --Creating bitpos table to be used
- local bfinal = nextBit(bytes,bitPos)
- local bType = nextBit(bytes,bitPos)
- bType = 2*bType+nextBit(bytes,bitPos)
- if bType == 3 then return false, "you fcked up dummy" end --3 is reserved and invalid
- if bType == 0 then --Easy case, with no compression
- if bitPos.pos ~= 0 then --If there are remaining bits in the current byte, they get ignored
- bitPos = 0
- index = index+1
- end
- local len = nextByte(bytes,bitPos)
- len = 256*len+nextByte(bytes,bitPos)
- local nlen = nextByte(bytes,bitPos)
- nlen = 256*nlen+nextByte(bytes,bitPos) --Not used
- local data = {}
- for i=1,len do
- table.insert(data,nextByte(bytes,bitPos))
- end
- table.insert(dataBlocks,data)
- if bfinal == 1 then
- return dataBlocks
- end
- return inflateBlock(bytes,bitPos.pos,bitPos.index,dataBlocks)
- end
- local lit_table, dist_table
- if bType == 1 then --Static huffman code, predefined
- lit_table = LITTABLE
- dist_table = DISTTABLE
- end
- if bType == 2 then --Why
- --huffman = Huffman(bytes,bitPos)
- local order = { 17, 18, 19, 1, 9, 8, 10, 7, 11, 6, 12, 5, 13, 4, 14, 3, 15, 2, 16 }
- local hlit = 257 + nextBits(bytes,bitPos,5)
- local hdist = 1 + nextBits(bytes,bitPos,5)
- local hclen = 4 + nextBits(bytes,bitPos,4)
- local depths = {}
- for i=1,hclen do
- local v = nextBits(bytes,bitPos,3)
- depths[order[i]] = v
- end
- for i=hclen+1,19 do
- depths[order[i]] = 0
- end
- local lengthtable,nlen = hufftable_create(depths)
- local i=1
- while i<=hlit+hdist do
- local v = getNextV(lengthtable,bytes,bitPos)
- if v < 16 then
- depths[i] = v
- i = i + 1
- elseif v < 19 then
- local nbt = {2,3,7}
- local nb = nbt[v-15]
- local c = 0
- local n = 3 + nextBits(bytes,bitPos,nb)
- if v == 16 then
- c = depths[i-1]
- elseif v == 18 then
- n = n + 8
- end
- for j=1,n do
- depths[i] = c
- i = i + 1
- end
- else
- error("wrong entry in depth table for literal/length alphabet: "..v);
- end
- end
- local litdepths = {} for i=1,hlit do table.insert(litdepths,depths[i]) end
- lit_table,nlit = hufftable_create(litdepths)
- local distdepths = {} for i=hlit+1,#depths do table.insert(distdepths,depths[i]) end
- dist_table,ndist = hufftable_create(distdepths)
- end
- local lit
- local data = {}
- local startPos = bitPos.pos+8*bitPos.index
- repeat
- lit = getNextV(lit_table,bytes,bitPos)
- if lit < 256 then
- table.insert(data,lit)
- elseif lit > 256 then
- local nbits = 0
- local size = 3
- local dist = 1
- if lit < 265 then
- size = size + lit - 257
- elseif lit < 285 then
- nbits = bit.brshift(lit-261,2)
- size = size + bit.blshift(bit.band(lit-261,3)+4,nbits)
- else
- size = 258
- end
- if nbits > 0 then
- size = size + nextBits(bytes,bitPos,nbits)--bs:getb(nbits)
- end
- local v = getNextV(dist_table,bytes,bitPos)--bs:getv(disttable,ndist)
- if v < 4 then
- dist = dist + v
- else
- nbits = bit.brshift(v-2,1)
- dist = dist + bit.blshift(bit.band(v,1)+2,nbits)
- dist = dist + nextBits(bytes,bitPos,nbits)--bs:getb(nbits)
- end
- local p = bitPos.pos+8*bitPos.index - startPos - dist+1
- while size > 0 do
- table.insert(data,data[p])
- p = p + 1
- size = size - 1
- end
- end
- until lit == 256
- table.insert(dataBlocks,data)
- if bfinal == 1 then return dataBlocks end
- return inflateBlock(bytes, bitPos.pos,bitPos.index,dataBlocks)
- end
- function getUnzippedBytes(path)
- if not fs.exists(path) then return false end
- file = fs.open(path,"rb")
- local id1,id2,cm,mtime,xfl,opSys,extra,name,comment,fhcrc,data,crc32,iSize = readMember(file)
- file.close()
- if id1 ~= 0x1f or id2 ~= 0x8b then
- return false
- end
- if cm ~= 8 then
- return false
- end
- local bytes = inflateBlock(data, 0,1)
- return bytes
- end
Add Comment
Please, Sign In to add comment