def getParams(data): """ return the parameters as tuple for the opcode at position 0 special treatment is done for the opcode with variable parameters """ code = data[0] params = [] ofs = 1 for p in opparams[code]: params.append({1: u1, 2: u2, 4: u4}[p](data[ofs:])) ofs += p if code == opcode["ilookupswitch"]: npairs = params[1] for i in range(npairs): params.append((u4(data[ofs:]), u2(data[ofs+4:]), )) ofs += 6 elif code == opcode["itableswitch"]: lowbytes = utils.signed4(params[1]) highbytes = utils.signed4(params[2]) for i in range(highbytes - lowbytes): params.append(u2(data[ofs:])) ofs += 2 elif code == opcode["slookupswitch"]: npairs = params[1] for i in range(npairs): params.append((u2(data[ofs:]), u2(data[ofs+2:]), )) ofs += 4 elif code == opcode["stableswitch"]: lowbytes = utils.signed2(params[1]) highbytes = utils.signed2(params[2]) for i in range(highbytes - lowbytes + 1): params.append(u2(data[ofs:])) ofs += 2 return ofs, params
def getParams(data): """ return the parameters as tuple for the opcode at position 0 special treatment is done for the opcode with variable parameters """ code = data[0] params = [] ofs = 1 for p in opparams[code]: params.append({1: u1, 2: u2, 4: u4}[p](data[ofs:])) ofs += p if code == opcode["ilookupswitch"]: npairs = params[1] for i in xrange(npairs): params.append((u4(data[ofs:]), u2(data[ofs+4:]), )) ofs += 6 elif code == opcode["itableswitch"]: lowbytes = utils.signed4(params[1]) highbytes = utils.signed4(params[2]) for i in xrange(highbytes - lowbytes): params.append(u2(data[ofs:])) ofs += 2 elif code == opcode["slookupswitch"]: npairs = params[1] for i in xrange(npairs): params.append((u2(data[ofs:]), u2(data[ofs+2:]), )) ofs += 4 elif code == opcode["stableswitch"]: lowbytes = utils.signed2(params[1]) highbytes = utils.signed2(params[2]) for i in xrange(highbytes - lowbytes + 1): params.append(u2(data[ofs:])) ofs += 2 return ofs, params
def slookupswitch(self, default, npairs, *pairs): key = self.frame.pop() for pair in pairs: match, offset = pair match = utils.signed2(match) if key == match: return utils.signed2(offset) elif key < match: return utils.signed2(default) return utils.signed2(default)
def __init__(self, offset, cap_file, resolver): self.offset = offset sf = cap_file.StaticField if offset < sf.array_init_count * 2: offset = offset // 2 aii = sf.array_init[offset] convertfunc = { 2: lambda x: bool(x[0]), 3: lambda x: utils.signed1(x[0]), 4: lambda x: utils.signed2((x[0] << 8) + x[1]), 5: lambda x: utils.signed4((x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3]) }[aii.type] elemsize = {2: 1, 3: 1, 4: 2, 5: 4}[aii.type] index = 0 value = [] while index < aii.count: value.append(convertfunc(aii.values[index:])) index += elemsize self.val = value elif offset < sf.reference_count * 2: self.val = None else: offset -= sf.reference_count * 2 if offset <= sf.default_value_count: self.val = None else: print("I am a non-default primitive type") raise NotImplementedError("non default primitive static field")
def __init__(self, offset, cap_file, resolver): self.offset = offset sf = cap_file.StaticField if offset < sf.array_init_count * 2: offset = offset // 2 aii = sf.array_init[offset] convertfunc = {2: lambda x: bool(x[0]), 3: lambda x: utils.signed1(x[0]), 4: lambda x: utils.signed2((x[0] << 8) + x[1]), 5: lambda x: utils.signed4((x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3]) }[aii.type] elemsize = {2: 1, 3: 1, 4: 2, 5: 4}[aii.type] index = 0 value = [] while index < aii.count: value.append(convertfunc(aii.values[index:])) index += elemsize self.val = value elif offset < sf.reference_count * 2: self.val = None else: offset -= sf.reference_count * 2 if offset <= sf.default_value_count: self.val = None else: print "I am a non-default primitive type" raise NotImplementedError("non default primitive static field")
def anewarray(self, index): count = utils.signed2(self.frame.pop()) if count < 0: raise python.lang.NegativeArraySizeException() # We don't really care of the type of the elements array = [None for i in xrange(count)] self.frame.push(array)
def _ifxx_w(self, branch, op): val = self.frame.pop() if {'eq': val == 0, 'ne': val != 0, 'lt': val < 0, 'le': val <= 0, 'gt': val > 0, 'ge': val >= 0}[op]: return utils.signed2(branch)
def getPar(data): """ return the size of the parameters for the opcode at index 0 """ code = data[0] if code == opcode["ilookupswitch"]: npairs = u2(data[3:5]) return npairs*6 + 4 elif code == opcode["itableswitch"]: lowbytes = utils.signed2(u4(data[3:7])) highbytes = utils.signed4(u4(data[7:11])) return (highbytes - lowbytes + 1)*2 + 10 elif code == opcode["slookupswitch"]: npairs = u2(data[3:5]) return npairs*4 + 4 elif code == opcode["stableswitch"]: lowbytes = utils.signed2(u2(data[3:5])) highbytes = utils.signed2(u2(data[5:7])) return (highbytes - lowbytes + 1)*2 + 6 else: return oppar[code]
def _if_scmpxx_w(self, branch, op): val2 = self.frame.pop() val1 = self.frame.pop() if {'eq': val1 == val2, 'ne': val1 != val2, 'lt': val1 < val2, 'le': val1 <= val2, 'gt': val1 > val2, 'ge': val1 >= val2}[op]: return utils.signed2(branch)
def _if_acmpxx_w(self, branch, op): val2 = self.frame.pop() val1 = self.frame.pop() if {'eq': val1 is val2, 'ne': val1 is not val2}[op]: return utils.signed2(branch)
def stableswitch(self, default, low, high, *offsets): index = self.frame.pop() try: return utils.signed2(offsets[index - low]) except IndexError: return utils.signed2(default)
def newarray(self, elemtype): count = utils.signed2(self.frame.pop()) if count < 0: raise python.lang.NegativeArraySizeException() array = [{10:False, 11:0, 12:0, 13:0}[elemtype] for i in xrange(count)] self.frame.push(array)
def sspush(self, short): self.frame.push(utils.signed2(short))
def sinc_w(self, index, byte): self.frame.locals[index] += utils.signed2(byte)
def jsr(self, branch): self.frame.push(self.frame.ip+3)# jsr-branch1-branch2 = 3 return utils.signed2(branch)
def goto_w(self, branch): return utils.signed2(branch)
def ifnonnull_w(self, branch): val = self.frame.pop() if val is not None: return utils.signed2(branch)
def ssub(self): val2 = self.frame.pop() val1 = self.frame.pop() self.frame.push(utils.signed2((val1 - val2) & 0xffff))