Class: Resedit::MZHeader

Inherits:
ExeHeader show all
Defined in:
lib/resedit/mz/mz.rb

Constant Summary collapse

MAGIC =
0x5A4D
HSIZE =
0x1C
HDRDESCR =
[:Magic, :BytesInLastBlock, :BlocksInFile, :NumberOfRelocations, :HeaderParagraphs, :MinExtraParagraphs, :MaxExtraParagraphs,
:SS, :SP, :Checksum, :IP, :CS, :RelocTableOffset, :OverlayNumber]

Constants inherited from ExeHeader

ExeHeader::BLK, ExeHeader::HDRUNPACK, ExeHeader::PARA

Constants inherited from Changeable

Changeable::COL_CHANGED, Changeable::COL_ORIGINAL, Changeable::HOW_CHANGED, Changeable::HOW_ORIGINAL, Changeable::LOG

Instance Attribute Summary collapse

Attributes inherited from ExeHeader

#exe, #info

Instance Method Summary collapse

Methods inherited from ExeHeader

#_headerTable, #change, #fieldOffset, #fieldSize, #loadInfo, #loadTables, #loadTail, #mode, #setBodySize, #setHeaderSize, #setInfo

Methods inherited from Changeable

#addData, #bytes, #change, #changed?, #colStr, #colVal, #curcol, #dbgdump, #debug, #fix, #getChanges, #getData, #hex, #hexify, #insert, #mode, #parseHow, #revert, #saveChanges, #saveData, #setData, #size, #undo, #unhexify

Constructor Details

#initialize(exe, file, size) ⇒ MZHeader

Returns a new instance of MZHeader.



13
14
15
16
17
18
19
20
21
# File 'lib/resedit/mz/mz.rb', line 13

def initialize(exe, file, size)
    super(exe, file, size)
    @fsize = size
    @relocFix = 0
    @newOfs = false
    if @info[:RelocTableOffset]>=0x40
        @newOfs = getData(0x3C, 2).unpack("v")[0]
    end
end

Instance Attribute Details

#relocFixObject (readonly)

Returns the value of attribute relocFix.



11
12
13
# File 'lib/resedit/mz/mz.rb', line 11

def relocFix
  @relocFix
end

Instance Method Details

#addHeaderSize(size) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/resedit/mz/mz.rb', line 52

def addHeaderSize(size)
    mode(HOW_CHANGED)
    paras = size/16 + (size%16 == 0 ? 0 : 1)
    insert(headerSize(), "\x00" * (paras * PARA))
    setFileSize(fileSize() + paras * PARA)
    setInfo(:HeaderParagraphs, @info[:HeaderParagraphs] + paras)
    mode(HOW_CHANGED)
end

#addReloc(ofs, trg) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/resedit/mz/mz.rb', line 107

def addReloc(ofs, trg)
    mode(HOW_CHANGED)
    #check relocation exists
    for i in 0..@info[:NumberOfRelocations]-1
        rel = getRelocation(i)
        return false if @exe.body.seg2Linear(rel[0], rel[1]) == ofs
    end
    #add relocation
    rid = @info[:NumberOfRelocations]
    setSpaceForRelocs(@info[:NumberOfRelocations]+1)
    val = @exe.body.linear2seg(ofs)
    setRelocation(rid, val)
    return true
end

#entryObject



23
# File 'lib/resedit/mz/mz.rb', line 23

def entry; sprintf("%04X:%04X", @info[:CS], @info[:IP]) end

#fileSizeObject



72
73
74
75
76
77
78
79
# File 'lib/resedit/mz/mz.rb', line 72

def fileSize()
    return @newOfs if @newOfs
    sz = @info[:BlocksInFile] * BLK
    if @info[:BytesInLastBlock] != 0
        sz -= BLK - @info[:BytesInLastBlock]
    end
    return sz
end

#freeSpace(middle = false) ⇒ Object



93
94
95
96
# File 'lib/resedit/mz/mz.rb', line 93

def freeSpace(middle = false)
    return @info[:RelocTableOffset] - HSIZE  if middle
    return headerSize() - HSIZE - @info[:NumberOfRelocations] * 4
end

#getRelocation(idx) ⇒ Object



82
83
84
85
# File 'lib/resedit/mz/mz.rb', line 82

def getRelocation(idx)
    raise "Wrong relocation index " if idx<0 || idx >= @info[:NumberOfRelocations]
    return getData(@info[:RelocTableOffset] + idx * 4, 4).unpack('vv')
end

#headerSizeObject



70
# File 'lib/resedit/mz/mz.rb', line 70

def headerSize(); @info[:HeaderParagraphs] * PARA end

#loadChanges(cfg) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/resedit/mz/mz.rb', line 61

def loadChanges(cfg)
    super(cfg)
    mode(HOW_ORIGINAL)
    ocs = @info[:CS]
    mode(HOW_CHANGED)
    ncs = @info[:CS]
    @relocFix = ncs - ocs
end


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/resedit/mz/mz.rb', line 122

def print(what, how)
    mode(parseHow(how))
    if what == "header"
        ofs=0
        @info.each{|k,v|
            printf("%20s:\t%s\n", k.to_s, colVal(ofs, 2))
            ofs+=2
        }
        puts
        fsz = fileSize()
        hsz = headerSize()
        s = colStr(sprintf("%d (%X)", fsz,fsz), changed?(2, 4))
        printf("mz file size: %s\treal file size: %d (0x%X)\n", s, @fsize, @fsize)
        printf("header size: %s\n", colStr(hsz, changed?(8, 2)))
        printf("code size: %s\n", colStr(fsz - hsz, @exe.body.changed?(0)))
        printf("reloc table size: %s\n", colStr(@info[:NumberOfRelocations] * 4, changed?(6, 2)))
        printf("free space in header: before relocs 0x%X,  after relocs 0x%X\n", freeSpace(true), freeSpace())
        printf("reloc fix: 0x%X\n", @relocFix)
        return true
    end
    @exe.body.mode(@mode)
    if what == "reloc"
        ofs = @info[:RelocTableOffset]
        for i in 0..@info[:NumberOfRelocations]-1
            s1 = colVal(ofs,2)
            s2 = colVal(ofs+2,2)
            s3 = @exe.body.segData(getRelocation(i), 2, true)
            printf("%08X\t%s:%s\t= %s\n", ofs, s2, s1, s3)
            ofs += 4
        end
        return true
    end
    return super(what, how)
end

#rebuildHeader(codesize) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/resedit/mz/mz.rb', line 31

def rebuildHeader(codesize)
    mode(HOW_ORIGINAL)
    ss = @info[:SS]
    cs = @info[:CS]
    sz = fileSize()-headerSize()
    codesize += PARA - codesize % PARA if codesize % PARA!=0
    setFileSize(sz + codesize + headerSize())
    paras = codesize / PARA
    setInfo(:SS, ss+paras)
    setInfo(:CS, cs+paras)
    for i in 0..@info[:NumberOfRelocations]-1
        rel = getRelocation(i)
        rel[1] += paras-@relocFix
        fix(@info[:RelocTableOffset] + i * 4, rel.pack('vv'))
    end
    mode(HOW_CHANGED)
    @relocFix = paras
    @exe.env.set(:relocFix, paras.to_s)
    return codesize
end

#setFileSize(size) ⇒ Object



25
26
27
28
29
# File 'lib/resedit/mz/mz.rb', line 25

def setFileSize(size)
    mode(HOW_CHANGED)
    mod = size % BLK
    setInfo(:BytesInLastBlock, [mod, size / BLK + (mod ? 1 : 0)])
end

#setRelocation(idx, data) ⇒ Object



87
88
89
90
# File 'lib/resedit/mz/mz.rb', line 87

def setRelocation(idx, data)
    raise "Wrong relocation index " if idx<0 || idx >= @info[:NumberOfRelocations]
    change(@info[:RelocTableOffset] + idx * 4, data.pack('vv'))
end

#setSpaceForRelocs(count) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/resedit/mz/mz.rb', line 98

def setSpaceForRelocs(count)
    add = count - @info[:NumberOfRelocations]
    return if add<=0
    add -= freeSpace()/4
    addHeaderSize(add*4) if add>0
    setInfo(:NumberOfRelocations, count)
    mode(HOW_CHANGED)
end