Class: Resedit::ExeFile

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

Direct Known Subclasses

BW, LE, MZ, Multiexe

Constant Summary collapse

HDRCLASS =
nil
BODYCLASS =
nil
CFGEXT =
".mzcfg.json"
MODE =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, quiet = false) ⇒ ExeFile

Returns a new instance of ExeFile.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/resedit/classes/exefile.rb', line 225

def initialize(path=nil, quiet = false)
    @quiet = quiet
    @path=path
    @env = Env.new(self)
    if @path!=nil
        @path = path.downcase()
        fsize = File.size(path)
        open(@path,"rb:ascii-8bit"){|f|
            load(f, fsize)
        }
        if File.exist?(@path+CFGEXT)
            cfg = JSON.parse(File.read(@path+CFGEXT))
            loadConfig(cfg)
        end
        @fname = File.basename(@path)
        @name = File.basename(@path, ".*")
    end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



223
224
225
# File 'lib/resedit/classes/exefile.rb', line 223

def body
  @body
end

#envObject (readonly)

Returns the value of attribute env.



223
224
225
# File 'lib/resedit/classes/exefile.rb', line 223

def env
  @env
end

#fnameObject (readonly)

Returns the value of attribute fname.



223
224
225
# File 'lib/resedit/classes/exefile.rb', line 223

def fname
  @fname
end

#headerObject (readonly)

Returns the value of attribute header.



223
224
225
# File 'lib/resedit/classes/exefile.rb', line 223

def header
  @header
end

#nameObject (readonly)

Returns the value of attribute name.



223
224
225
# File 'lib/resedit/classes/exefile.rb', line 223

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



223
224
225
# File 'lib/resedit/classes/exefile.rb', line 223

def path
  @path
end

Instance Method Details

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



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/resedit/classes/exefile.rb', line 305

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



327
328
329
330
331
332
333
334
335
336
337
# File 'lib/resedit/classes/exefile.rb', line 327

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



260
# File 'lib/resedit/classes/exefile.rb', line 260

def close(); end

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



341
342
343
344
345
# File 'lib/resedit/classes/exefile.rb', line 341

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, self.class::MODE)
end

#getValue(value, type) ⇒ Object



299
300
301
302
# File 'lib/resedit/classes/exefile.rb', line 299

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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/resedit/classes/exefile.rb', line 279

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)
    hsz = 0
    if isfile
        @header.mode(@header.parseHow(how))
        hsz = @header.headerSize()
        size = @header.hex(wr, ofs, size, how) if ofs < hsz
        ofs -= hsz
        ofs = 0 if ofs < 0
        wr.addr = 0
    end
    @body.hex(wr, ofs, size, how) if size > 0
    wr.finish()
end

#hexify(str) ⇒ Object



297
# File 'lib/resedit/classes/exefile.rb', line 297

def hexify(str); @header.hexify(str) end

#is?(id) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
268
# File 'lib/resedit/classes/exefile.rb', line 265

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

#load(f, fsize, prev = nil) ⇒ Object



246
247
248
249
250
251
# File 'lib/resedit/classes/exefile.rb', line 246

def load(f, fsize, prev=nil)
    @header = self.class::HDRCLASS.new(self, f, fsize)
    @body = self.class::BODYCLASS.new(self, f, @header.fileSize() - @header.headerSize())
    @env.set(:entry, @header.entry())
    @env.set(:append, sprintf("0"))
end

#loadConfig(cfg) ⇒ Object



253
254
255
256
257
258
# File 'lib/resedit/classes/exefile.rb', line 253

def loadConfig(cfg)
    cfg = cfg[self.class.name]
    raise "Wrong config: #{self.class.name} expected" if !cfg
    @header.loadChanges(cfg['header'])
    @body.loadChanges(cfg['body'])
end

#log(fmt, *args) ⇒ Object



261
# File 'lib/resedit/classes/exefile.rb', line 261

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


270
271
272
273
274
275
276
# File 'lib/resedit/classes/exefile.rb', line 270

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

#readRelocated(ofs, size) ⇒ Object



339
# File 'lib/resedit/classes/exefile.rb', line 339

def readRelocated(ofs, size); @body.readRelocated(ofs, size) end

#reloc(ofs, target = nil) ⇒ Object



387
388
389
390
391
392
393
# File 'lib/resedit/classes/exefile.rb', line 387

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

#relocfind(value, type = nil) ⇒ Object



395
396
397
398
399
400
401
402
403
404
# File 'lib/resedit/classes/exefile.rb', line 395

def relocfind(value, type=nil);
    value = getValue(value, type)
    res = @body.findRelocValue(value)
    log("relocs not found") if !res
    return nil if !res
    res.each{|k,v|
        log("found at #{k.to_s(16)} relocs: #{v.map{|a| a.to_s(16)}}")
    }
    return res
end

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



321
322
323
324
# File 'lib/resedit/classes/exefile.rb', line 321

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

#revert(what) ⇒ Object



354
355
356
357
358
359
360
361
# File 'lib/resedit/classes/exefile.rb', line 354

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



262
# File 'lib/resedit/classes/exefile.rb', line 262

def s2i(str) return @env.s2i(str) end

#save(filename, final = nil) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/resedit/classes/exefile.rb', line 375

def save(filename, final=nil)
    raise "Wrong 'final' word" if final and final != 'final'
    raise "Filename expected." if !filename
    open(filename, "wb:ascii-8bit"){|f|
        saveFile(f)
    }
    return if final
    open(filename+CFGEXT, "w"){|f|
        f.write(JSON.pretty_generate(saveConfig()))
    }
end

#saveConfigObject



363
364
365
366
367
368
# File 'lib/resedit/classes/exefile.rb', line 363

def saveConfig()
    cfg = {}
    cfg['header'] = @header.saveChanges()
    cfg['body'] = @body.saveChanges()
    return {self.class.name => cfg}
end

#saveFile(f) ⇒ Object



370
371
372
373
# File 'lib/resedit/classes/exefile.rb', line 370

def saveFile(f)
    @header.saveData(f)
    @body.saveData(f)
end

#stringfind(size = nil) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
# File 'lib/resedit/classes/exefile.rb', line 406

def stringfind(size=nil)
    size = (size==nil || size=='') ? 3 : s2i(size)
    res = @body.findStrings(size)
    log("strings not found") if !res
    return nil if !res
    log("%d strings found:\n", res.length)
    res.each{|a|
        log("%s at %08X relocs: #{a[2].map{|v| v.to_s(16)}}", a[0], a[1])
    }
    return res
end

#valueof(str, type = nil) ⇒ Object



348
349
350
351
# File 'lib/resedit/classes/exefile.rb', line 348

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

#wsizeObject



244
# File 'lib/resedit/classes/exefile.rb', line 244

def wsize; self.class::MODE/8 end