Class: Resedit::MZHeader

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

Constant Summary collapse

MAGIC =
0x5a4D
BLK =
0x200
PARA =
0x10
HSIZE =
0x1C

Constants inherited from Changeable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Changeable

#addData, #bytes, #changed?, #colStr, #colVal, #curcol, #dbgdump, #debug, #getChanges, #getData, #hex, #insert, #loadChanges, #parseHow, #revert, #saveChanges, #saveData, #undo

Constructor Details

#initialize(mz, file, size) ⇒ MZHeader



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

def initialize(mz, file, size)
    raise "Not MZ file" if size < HSIZE
    @mz = mz
    super(file, HSIZE)
    @fsize = size
    @_infoOrig = loadInfo()
    @_info = nil
    @info = @_infoOrig
    raise "Not MZ file" if MAGIC != @info[:Magic]
    addData(file, headerSize() - HSIZE)
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



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

def info
  @info
end

#mzObject (readonly)

Returns the value of attribute mz.



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

def mz
  @mz
end

Instance Method Details

#addHeaderSize(size) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/resedit/mz/mz_header.rb', line 78

def addHeaderSize(size)
    mode(HOW_CHANGED)
    paras = size/16 + (size%16 == 0 ? 0 : 1)
    append("00" * (paras * PARA))
    changeSize(fileSize() + paras * PARA)
    change(8, [@info[:HeaderParagraphs] + paras].pack('v'))
end

#addReloc(ofs) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/resedit/mz/mz_header.rb', line 117

def addReloc(ofs)
    mode(HOW_CHANGED)
    #check relocation exists
    for i in 0..@info[:NumberOfRelocations]-1
        rel = getRelocation(i)
        if @mz.body.seg2Linear(rel[0], rel[1]) == ofs
            return false
        end
    end
    #add relocation
    if freeSpace()<4
        addHeaderSize(4)
        mode(HOW_CHANGED)
    end
    val = @mz.body.linear2seg(ofs)
    pos = @info[:RelocTableOffset]+@info[:NumberOfRelocations]*4
    cnt = @info[:NumberOfRelocations]
    change(pos, val.pack('vv'))
    change(6, [cnt+1].pack('v'))
    return true
end

#change(ofs, bytes) ⇒ Object



37
38
39
40
# File 'lib/resedit/mz/mz_header.rb', line 37

def change(ofs, bytes)
    super(ofs, bytes)
    @_info = nil if (ofs < HSIZE)
end

#changeSize(size) ⇒ Object



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

def changeSize(size)
    mode(HOW_CHANGED)
    mod = size % BLK
    ch = [mod, size / BLK + (mod ? 1 : 0)]
    change(2, ch.pack('vv'))
end

#fileSizeObject



92
93
94
95
96
97
98
# File 'lib/resedit/mz/mz_header.rb', line 92

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

#freeSpace(middle = false) ⇒ Object



112
113
114
115
# File 'lib/resedit/mz/mz_header.rb', line 112

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

#getRelocation(idx) ⇒ Object



101
102
103
104
# File 'lib/resedit/mz/mz_header.rb', line 101

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

#headerSizeObject



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

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

#loadInfoObject



43
44
45
46
47
48
49
50
# File 'lib/resedit/mz/mz_header.rb', line 43

def loadInfo()
    v = getData(0, HSIZE).unpack('v*')
    return  {:Magic => v[0], :BytesInLastBlock => v[1], :BlocksInFile => v[2], :NumberOfRelocations => v[3],
             :HeaderParagraphs => v[4], :MinExtraParagraphs => v[5], :MaxExtraParagraphs => v[6],
             :SS => v[7], :SP => v[8], :Checksum => v[9], :IP => v[10], :CS => v[11],
             :RelocTableOffset => v[12], :OverlayNumber => v[13]
             }
end

#mode(how) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/resedit/mz/mz_header.rb', line 26

def mode(how)
    super(how)
    if @mode == HOW_ORIGINAL
        @info = @_infoOrig
    else
        @_info = loadInfo() if  !@_info
        @info = @_info
    end
end


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/resedit/mz/mz_header.rb', line 139

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, @mz.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())
        return true
    end
    @mz.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 = @mz.body.segData(getRelocation(i), 2, true)
            printf("%08X\t%s:%s\t= %s\n", ofs, s2, s1, s3)
            ofs += 4
        end
        return true
    end
end

#rebuildHeader(codesize) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/resedit/mz/mz_header.rb', line 60

def rebuildHeader(codesize)
    mode(HOW_ORIGINAL)
    ss = @info[:SS]
    cs = @info[:CS]
    sz = fileSize()-headerSize()
    codesize += PARA - codesize % PARA if codesize % PARA!=0
    changeSize(sz + codesize + headerSize())
    paras = codesize / PARA
    change(14, [ss+paras].pack('v'))
    change(22, [cs+paras].pack('v'))
    for i in 0..@mz.header.info[:NumberOfRelocations]-1
        rel = getRelocation(i)
        rel[1]+=paras
        setRelocation(i, rel)
    end
    return codesize
end

#setRelocation(idx, data) ⇒ Object



106
107
108
109
# File 'lib/resedit/mz/mz_header.rb', line 106

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