Skip to content

Commit 720df52

Browse files
committed
Refactored PE95TIME.LUA script to have a file size under 1536 bytes (3 blocks).
1 parent 0e66b64 commit 720df52

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

demo/xtra/PE95TIME.LUA

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
#!/usr/bin/env lua
22

3-
-- This script will set PE executable timestamps to a constant so NT builds are repeatable
3+
--Set PE timestamps to a constant so builds are repeatable
44
C="cur" E="Unexpected EOF" F="<I4" S="set" V=809222400
5-
local function rs(f)
6-
local b = f:read(2) if not b or #b < 2 then error(E) end return string.unpack("<I2", b)
7-
end local function rd(f)
8-
local b = f:read(4) if not b or #b < 4 then error(E) end return string.unpack(F, b)
9-
end local function wd(f, pos, val)
10-
f:seek(S, pos) f:write(string.pack(F, val))
11-
end local function pe_offset(f)
12-
f:seek(S, 0) local mz = f:read(2) if mz ~= "MZ" then error("Not MZ") end f:seek(S, 0x3C) return rd(f)
13-
end local function find_rsrc(f, peho)
14-
f:seek(S, peho) local sig = f:read(4) if sig ~= "PE\0\0" then error("Invalid PE") end f:seek(C, 2)
15-
local nos = rs(f) f:seek(C, 12) local soh = rs(f) f:seek(C, 2) local ohs, m = f:seek(), rs(f)
16-
if m ~= 0x10b and m ~= 0x20b then error("Unknown magic: " .. string.format("%x", m)) end f:seek(S, ohs + soh)
17-
for _ = 1, nos do
18-
local ss, name = f:seek(), f:read(8):gsub("\0.*", "") f:seek(C, 4) local _, sord, ptrd = rd(f), rd(f), rd(f) f:seek(S, ss + 40)
19-
if name == ".rsrc" then return { s = sord, r = ptrd } end
20-
end return nil
21-
end local function patch(f, bo, oir)
22-
local dp = bo + oir + 4 wd(f, dp, V) f:seek(C, 4) local nn, ni = rs(f), rs(f) local te, eo = nn + ni, dp + 16
23-
for i = 0, te -1 do local ep = eo + (i * 8) f:seek(S, ep) local dodr = rd(f) if (dodr & 0x80000000) ~= 0 then local o = dodr & 0x7FFFFFFF patch(f, bo, o) end end
5+
local function R(f,l) local b=f:read(l) if not b or #b<l then error(E) end return string.unpack(l==2 and"<I2"or F,b) end
6+
local function W(f,p,v) f:seek(S,p) f:write(string.pack(F,v)) end
7+
local function peo(f) f:seek(S,0) local mz=f:read(2) if mz~="MZ" then error("!MZ") end f:seek(S,0x3C) return R(f,4) end
8+
local function rsrc(f,p)
9+
f:seek(S,p) local sig=f:read(4) if sig~="PE\0\0" then error("Invalid PE") end f:seek(C,2)
10+
local n=R(f,2) f:seek(C,12) local s=R(f,2) f:seek(C,2) local h,m=f:seek(),R(f,2)
11+
if m~=0x10b and m~=0x20b then error("Unknown magic: "..string.format("%x",m)) end f:seek(S,h+s)
12+
for _=1,n do
13+
local ss,o=f:seek(),f:read(8):gsub("\0.*",""),f:seek(C,4) local _,t,q=R(f,4),R(f,4),R(f,4) f:seek(S,ss+40)
14+
if o==".rsrc" then return {s=t,r=q} end
15+
end return nil end
16+
local function pat(f,bo,oir)
17+
local dp=bo+oir+4 W(f,dp,V) f:seek(C,4) local nn,ni=R(f,2),R(f,2) local te,eo=nn+ni,dp+16
18+
for i=0,te-1 do local ep=eo+(i*8) f:seek(S,ep) local d=R(f,4) if (d &0x80000000)~=0 then local o=d&0x7FFFFFFF pat(f,bo,o) end end
2419
end
25-
if #arg < 1 then os.exit(1) end
26-
local f = assert(io.open(arg[1], "r+b")) local p = pe_offset(f) wd(f, p+8, V) local r = find_rsrc(f, p)
20+
if #arg<1 then os.exit(1) end
21+
local f=assert(io.open(arg[1],"r+b")) local p=peo(f) W(f,p+8,V) local r=rsrc(f,p)
2722
if not r then error(".rsrc not found") end
28-
patch(f, r.r, 0) f:close()
29-
print(".rsrc @: " .. r.r .. ", size: " .. r.s .. " DONE")
23+
pat(f,r.r,0) f:close()
24+
print(".rsrc @: "..r.r..", size: "..r.s.." DONE")

0 commit comments

Comments
 (0)