Class: Resedit::CodePatch::Chunk

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

Constant Summary collapse

NOP =
"\x90"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, format, splitter = NOP) ⇒ Chunk

Returns a new instance of Chunk.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/resedit/convert/codepatch.rb', line 11

def initialize(data, format, splitter=NOP)
    splitter.force_encoding(Encoding::ASCII_8BIT)
    @data, @format = data, format
    @ofs, @size = [], []
    ofs=0
    data.split(splitter).each{ |part|
        @ofs += [ofs]
        @size += [part.length()]
        ofs += part.length+1
    }
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/resedit/convert/codepatch.rb', line 9

def data
  @data
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/resedit/convert/codepatch.rb', line 9

def format
  @format
end

#ofsObject (readonly)

Returns the value of attribute ofs.



9
10
11
# File 'lib/resedit/convert/codepatch.rb', line 9

def ofs
  @ofs
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/resedit/convert/codepatch.rb', line 9

def size
  @size
end

Instance Method Details

#addr(idx, adr) ⇒ Object



23
24
25
26
27
# File 'lib/resedit/convert/codepatch.rb', line 23

def addr(idx, adr)
    ret = [adr[0], adr[1]]
    ret[0] += @ofs[idx]
    return ret
end

#hexdataObject



29
30
31
# File 'lib/resedit/convert/codepatch.rb', line 29

def hexdata()
    @data.each_byte.map { |b| sprintf("%02X",b) }.join
end

#mkadr(adr) ⇒ Object



51
52
53
54
55
# File 'lib/resedit/convert/codepatch.rb', line 51

def mkadr(adr)
    ret = (adr[0]&0xFF).chr + ((adr[0]>>8)&0xFF).chr
    ret += (adr[1]&0xFF).chr + ((adr[1]>>8)&0xFF).chr
    return ret
end

#value(idx, size, adr = nil, rep = "\xFF\xFF\xFF\xFF") ⇒ Object



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

def value(idx, size, adr = nil, rep = "\xFF\xFF\xFF\xFF")
    NOP.force_encoding(Encoding::ASCII_8BIT)
    rep.force_encoding(Encoding::ASCII_8BIT)
    ret = data[@ofs[idx], @size[idx]]
    if adr and rep
        #replace with seg:ofs
        ret.gsub!(rep, mkadr(adr))
    end
    raise "Code is bigger #{ret.length()} than expected #{size}" if ret.length>size
    while ret.length()<size
        ret += NOP
    end
    if @format == CodePatch::FMT_HEXSTRING
        ret = ret.each_byte.map { |b| sprintf("%02X",b) }.join
    end
    return ret
end