Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7KB

  1. local mtar = {}
  2. local versions = {}
  3. versions[0] = {nlf = ">I2", flf = ">I2"} -- original version format
  4. versions[1] = {nlf = ">I2", flf = ">I8"} -- extended file size format
  5. mtar.versions = versions
  6. local function cleanPath(path)
  7. local pt = {}
  8. for segment in path:gmatch("[^/]+") do
  9. if segment == ".." then
  10. pt[#pt] = nil
  11. elseif segment ~= "." then
  12. pt[#pt+1] = segment
  13. end
  14. end
  15. return table.concat(pt,"/")
  16. end
  17. function mtar.genHeader(fname,len,version) -- string number -- string -- generate a header for file *fname* when provided with file length *len*
  18. version=version or 1
  19. return string.format("\255\255%s%s%s%s", string.char(version), string.pack(versions[version].nlf,fname:len()), fname, string.pack(versions[version].flf,len))
  20. end
  21. function mtar.iter(stream) -- table -- function -- Given buffer *stream*, returns an iterator suitable for use with *for* that returns, for each iteration, the file name, a function to read from the file, and the length of the file.
  22. local remain = 0
  23. local function read(n)
  24. local rb = stream:read(math.min(n,remain)) or ""
  25. remain = remain - rb:len()
  26. return rb
  27. end
  28. return function()
  29. while remain > 0 do
  30. remain=remain-(#stream:read(math.min(remain,2048)) or "")
  31. end
  32. local version = 0
  33. local nlen = string.unpack(">I2", stream:read(2) or "\0\0")
  34. if nlen == 0 then
  35. return
  36. elseif nlen == 65535 then -- versioned header
  37. version = string.byte(stream:read(1))
  38. nlen = string.unpack(versions[version].nlf, stream:read(string.packsize(versions[version].nlf)))
  39. end
  40. local name = cleanPath(stream:read(nlen))
  41. remain = string.unpack(versions[version].flf, stream:read(string.packsize(versions[version].flf)))
  42. return name, read, remain
  43. end
  44. end
  45. return mtar