Class: Resedit::LEBody

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

Constant Summary

Constants inherited from Changeable

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

Instance Attribute Summary

Attributes inherited from ExeBody

#exe

Instance Method Summary collapse

Methods inherited from ExeBody

#addrFormatter, #dasm, #hex, #printDasm, #removeAppend, #revert, #textAt

Methods inherited from Changeable

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

Constructor Details

#initialize(exe, file, fsize) ⇒ LEBody

Returns a new instance of LEBody.



214
215
216
# File 'lib/resedit/mz/le.rb', line 214

def initialize(exe, file, fsize)
    super(exe, file, fsize)
end

Instance Method Details

#addr2raw(addr, raiseOnError = true) ⇒ Object



255
256
257
258
259
260
# File 'lib/resedit/mz/le.rb', line 255

def addr2raw(addr, raiseOnError=true)
    s = sections.find{|s| s[0]<=addr && s[0]+s[2]>addr}
    return nil if !s && !raiseOnError
    raise "Not virtual address #{addr.to_s(16)}" if !s
    return s[1]+addr-s[0]
end

#append(bytes, where = nil) ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/resedit/mz/le.rb', line 278

def append(bytes, where=nil)
    @relocs = nil
    mode(HOW_CHANGED)
    pos = @exe.header.addSegment(bytes.length)
    sz = size
    insert(sz, "\x00"*(pos-sz))
    insert(pos, bytes)
    return [raw2addr(pos), pos]
end

#clearRelocsObject



233
# File 'lib/resedit/mz/le.rb', line 233

def clearRelocs; @relocs=nil end

#findRelocValue(value) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/resedit/mz/le.rb', line 288

def findRelocValue(value)
    rel = relocations()
    cnt = rel.length
    ret={}
    vlen = value.length
    fsz = size
    rel.each{|o,r|
        r.each{|k,v|
            raw = addr2raw(v, false)
            next if raw==nil || raw+vlen>fsz
            vl = getData(raw, vlen)
            next if vl!=value
            ret[v] = [] if !ret[v]
            ret[v] += [raw2addr(k)]
        }
    }
    return nil if ret.length==0
    return ret
end

#findStrings(minsize) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/resedit/mz/le.rb', line 308

def findStrings(minsize)
    rel = relocations()
    cnt = rel.length
    fsz = size
    proced = {}
    ret = []
    rel.each{|o,r|
        r.each{|k,v|
            if proced[v]!=nil
                ret[proced[v]][2]+=[raw2addr(k)] if proced[v]>=0
                next
            end
            raw = addr2raw(v, false)
            if raw==nil || raw+minsize>fsz
                proced[v] = -1
                next
            end
            txt = textAt(raw, minsize)
            if txt != nil
                ret+= [[txt, v, [raw2addr(k)]]]
            end
            proced[v] = txt==nil ? -1 : ret.length-1
        }
    }
    return nil if ret.length==0
    return ret
end

#formatAddress(raw) ⇒ Object



245
246
247
# File 'lib/resedit/mz/le.rb', line 245

def formatAddress(raw)
    return sprintf("%08X %08X", raw, raw2addr(raw))
end

#raw2addr(ofs) ⇒ Object



249
250
251
252
253
# File 'lib/resedit/mz/le.rb', line 249

def raw2addr(ofs)
    s = sections.find{|s| s[1]<=ofs && s[1]+s[2]>ofs}
    raise "Not raw offset #{ofs.to_s(16)}" if !s
    return s[0]+ofs-s[1]
end

#readRelocated(ofs, size) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/resedit/mz/le.rb', line 262

def readRelocated(ofs, size);
    rel = relocations()
    d = getData(ofs, size)
    @relocs.each{|o,v|
        next if o>ofs+size
        v.each{|a, r|
            next if a>ofs+size
            break if a<ofs
            pos = a-ofs
            d = d[0,pos] + [r].pack("V") + (d[pos+4..-1] or '')
        }
        break if o<ofs
    }
    return d[0,size]
end

#relocationsObject



235
236
237
238
239
240
241
242
243
# File 'lib/resedit/mz/le.rb', line 235

def relocations()
    if !@relocs
        @relocs = @exe.header.readRelocs().sort_by{|k,v| k}.reverse.to_h
        @relocs.each{|k,v|
            @relocs[k] = v.sort_by{|k2,v2| k2}.reverse.to_h
        }
    end
    @relocs
end

#sectionsObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/resedit/mz/le.rb', line 218

def sections()
    if !@sex
        ofs = 0
        @sex=[]
        @exe.header.tables[:Objects].each{|descr|
            sz = descr[4] * @exe.header.info[:PageSize]
            if sz!=0
                @sex += [[descr[1], ofs, sz]]
                ofs += sz
            end
        }
    end
    @sex
end