Class: Resedit::MZ

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

Constant Summary collapse

ZM =
0x4D5A

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, quiet = false) ⇒ MZ

Returns a new instance of MZ.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/resedit/mz/mz.rb', line 14

def initialize(path, quiet = false)
    raise "File not specified" if !path
    @quiet = quiet
    @path = path.downcase()
    @fsize = File.size(path)
    open(@path,"rb:ascii-8bit"){|f|
        @header = MZHeader.new(self, f, fsize)
        hsz = @header.headerSize()
        @body = MZBody.new(self, f, @header.fileSize() - hsz)
        save = f.read(2)
        zm = save ? save.unpack('v')[0] : nil
        if zm == ZM
            @header.loadChanges(f)
            @body.loadChanges(f)
            log("Change info loaded.")
        end
    }
    @fname = File.basename(@path)
    @name = File.basename(@path, ".*")
    hi = @header.info()
    env().set(:entry, hi[:CS].to_s+":"+hi[:IP].to_s)
    env().set(:append, sprintf("0:0"))
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#fnameObject (readonly)

Returns the value of attribute fname.



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

def fname
  @fname
end

#fsizeObject (readonly)

Returns the value of attribute fsize.



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

def fsize
  @fsize
end

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#append(value, type = nil, where = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/resedit/mz/mz.rb', line 93

def append(value, type=nil, where=nil)
    where = s2i(where) if where
    res = @body.append(getValue(value,type), where)
    s = ""
    res.each{|a|
        if a.is_a?(Array)
            s += sprintf(" %04X:%04X", a[1], a[0])
        else
            s += sprintf(" %08X", a)
        end
    }
    log("Appended at %s",s)
    return res
end

#change(ofs, value, disp = nil, type = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/resedit/mz/mz.rb', line 115

def change(ofs, value, disp=nil, type=nil)
    ofs = s2i(ofs)
    isfile = disp && (disp[0]=='f' || disp[0]=='F') ? true : false
    value = getValue(value, type)
    if isfile
        res = @header.change(ofs,value)
    else
        res = @body.change(ofs,value) + @header.headerSize()
    end
    log("Change added at %08X", res) if res
end

#closeObject



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

def close()
end

#dasm(ofs, size = nil, how = nil) ⇒ Object



134
135
136
137
138
# File 'lib/resedit/mz/mz.rb', line 134

def dasm(ofs, size=nil, how=nil)
    ofs = s2i(ofs ? ofs : "entry")
    size = size ? s2i(size) : [0x20, @body.bytes.length-ofs].min
    @body.dasm(ofs, size, how)
end

#envObject



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

def env() return MZEnv.instance() end

#getValue(value, type) ⇒ Object



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

def getValue(value, type)
    s = env().value2bytes(value, type)
    return s.force_encoding(Encoding::ASCII_8BIT)
end

#hex(ofs, size = nil, how = nil, disp = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/resedit/mz/mz.rb', line 67

def hex(ofs, size=nil, how=nil, disp=nil)
    ofs = ofs ? s2i(ofs) : 0
    size = size ? s2i(size) : 0x100
    isfile = disp && (disp[0]=='f' || disp[0]=='F') ? true : false
    wr = HexWriter.new(ofs)
    how = @header.parseHow(how)
    hsz = 0
    if isfile
        @header.mode(how)
        hsz = @header.headerSize()
        size = @header.hex(wr, ofs, size, how) if ofs < hsz
        ofs -= hsz
        ofs = 0 if ofs < 0
    end
    wr.setSegments(@body.segments, hsz)
    @body.hex(wr, ofs, size, how) if size > 0
    wr.finish()
end

#is?(id) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/resedit/mz/mz.rb', line 52

def is?(id)
    id = id.downcase
    return id == @path || id == @fname || id == @name
end

#log(fmt, *args) ⇒ Object



43
44
45
# File 'lib/resedit/mz/mz.rb', line 43

def log(fmt, *args)
    App::get().log(fmt, *args) if !@quiet
end


58
59
60
61
62
63
64
# File 'lib/resedit/mz/mz.rb', line 58

def print(what, how=nil)
    puts "Header changes:" if what=="changes"
    res = @header.print(what, how)
    puts "Code changes:" if what=="changes"
    res |= @body.print(what, how)
    raise "Don't know how to print: " + what if !res
end

#reloc(ofs) ⇒ Object



127
128
129
130
131
# File 'lib/resedit/mz/mz.rb', line 127

def reloc(ofs)
    ofs = s2i(ofs)
    res = @header.addReloc(ofs)
    log((res ? "Relocation added %08X" : "Relocation %08X already exists"), ofs)
end

#replace(value, type = nil, where = nil) ⇒ Object



109
110
111
112
# File 'lib/resedit/mz/mz.rb', line 109

def replace(value, type=nil, where=nil)
    @body.removeAppend()
    return append(value,type, where)
end

#revert(what) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/resedit/mz/mz.rb', line 147

def revert(what)
    wid = env().s2i_nt(what)
    what = wid[1] ? wid[0] : what
    res = @header.revert(what)
    res |= @body.revert(what)
    raise "Don't know how to revert: "+what if !res
    log("Reverted")
end

#s2i(str) ⇒ Object



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

def s2i(str) return MZEnv.instance().s2i(str) end

#save(filename, final = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/resedit/mz/mz.rb', line 157

def save(filename, final=nil)
    raise "Unknown final: " + final if final && final != "final"
    raise "Filename expected." if !filename
    open(filename, "wb:ascii-8bit"){|f|
        @header.saveData(f)
        @body.saveData(f)
        if !final || final!='final'
            f.write([ZM].pack('v'))
            @header.saveChanges(f)
            @body.saveChanges(f)
        end
    }
end

#valueof(str, type) ⇒ Object



141
142
143
144
# File 'lib/resedit/mz/mz.rb', line 141

def valueof(str, type)
    puts "value of " + str + " is:"
    p getValue(str, type).unpack("H*")
end