Advertisement
osmarks

base1

Sep 23rd, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. -- DO NOT USE
  2. -- ENCODE-ONLY
  3.  
  4.  
  5. if not fs.exists"bignum"then local a=http.get"https://raw.githubusercontent.com/ennorehling/euler/master/BigNum.lua"local b=fs.open("bignum","w")b.write(a.readAll())a.close()b.close()end
  6.  
  7. -- this is ported from https://github.com/qntm/base1/blob/master/index.js
  8. -- use the comments there
  9.  
  10. dofile "bignum"
  11.  
  12. local base = BigNum.new(256)
  13.  
  14. local base1 = {}
  15.  
  16. local function yield()
  17.     os.queueEvent "dummy"
  18.     os.pullEvent "dummy"
  19. end
  20.  
  21. function base1.encodeL(bin)
  22.     local len = #bin
  23.     local out = BigNum.new(0)
  24.  
  25.     -- Integerise binary data
  26.     for i = 1, len do
  27.         local val = string.byte(bin:sub(i, i))
  28.         out = (out * base) + val
  29.         yield()
  30.     end
  31.  
  32.     local binlength = 0
  33.     local blocksize = base ^ BigNum.new(binlength)
  34.    
  35.     while binlength < len do
  36.         out = out + blocksize
  37.         blocksize = blocksize * base
  38.         binlength = binlength + 1
  39.         yield()
  40.     end
  41.  
  42.     return out
  43. end
  44.  
  45. function base1.decodeL(l)
  46.     local binlength = 0
  47.     local blocksize = base ^ BigNum.new(binlength)
  48.     while l <= blocksize do
  49.         l = l - blocksize
  50.         blocksize = blocksize * base
  51.         binlength = binlength + 1
  52.     end
  53.  
  54.     local out = ""
  55.     local byte = binlength - 1
  56.     while byte >= 0 do
  57.        
  58.     end
  59. end
  60.  
  61. print(base1.encodeL "hi")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement