51 lines
1.1 KiB
Lua
51 lines
1.1 KiB
Lua
local partition = {}
|
|
|
|
local entryPattern = "(" .. ("."):rep(20) .. ")(....)(....)(....)"
|
|
|
|
local function cint(n,l)
|
|
local t={}
|
|
for i = 0, 7 do
|
|
t[i+1] = (n >> (i * 8)) & 0xFF
|
|
end
|
|
return string.reverse(string.char(table.unpack(t)):sub(1,l))
|
|
end
|
|
local function toint(s)
|
|
local n = 0
|
|
local i = 1
|
|
for p in s:gmatch(".") do
|
|
n = n << 8
|
|
n = n | string.byte(p)
|
|
i=i+1
|
|
end
|
|
return n
|
|
end
|
|
|
|
function partition.parse(sector)
|
|
local pt = {}
|
|
for name,ptype,sstart,slen in sector:gmatch(entryPattern) do
|
|
local pte = {}
|
|
pte.name = name:gsub("\0","")
|
|
if pte.name:len() > 0 then
|
|
pte.type = ptype
|
|
pte.start = toint(sstart)
|
|
pte.len = toint(slen)
|
|
pte.last = pte.start + toint(slen)
|
|
pt[#pt+1] = pte
|
|
end
|
|
end
|
|
return pt
|
|
end
|
|
function partition.generate(pt)
|
|
local ps = ""
|
|
for k,v in pairs(pt) do
|
|
if not v.len then
|
|
v.len = v.last - v.start
|
|
end
|
|
-- ps = ps .. ("\0"):rep(math.max(20-v.name:len(),0)) .. v.name:sub(1,20) .. v.type:sub(1,4) .. cint(v.start,4) .. cint(v.len,4)
|
|
ps = ps .. (("\0"):rep(20) .. v.name):sub(-20) .. v.type:sub(1,4) .. cint(v.start,4) .. cint(v.len,4)
|
|
end
|
|
return ps..("\0"):rep(512-ps:len())
|
|
end
|
|
|
|
return partition
|