예제 #1
0
def handleTexts(text):
    t = text[1]
    tt = re.split(r'\s*†\s*', t)
    if len(tt) == 2:
        t1 = tt[0].strip()
        t2 = asdis.unescape(tt[1].strip())
        return (t1, t2)
    else:
        t = asdis.unescape(t.strip())
        return t
예제 #2
0
def handleTexts(text):
    t = text[1]
    tt = re.split(r'\s*†\s*', t)
    if len(tt) == 2:
        t1 = tt[0].strip()
        t2 = asdis.unescape(tt[1].strip())
        return (t1, t2)
    else:
        t = asdis.unescape(t.strip())
        return t
예제 #3
0
파일: bgias.py 프로젝트: mchubby/Bgi_tools
def parse(asmtxt, inputpo):
    """
    Parse the .bsd disassembly into structured data using given .po resources
    Returns: tuple(array of lists, dict, array of bytes, bytes, dict)
    - instrs: list is (fcn:str, args:array, pos:integer, index:integer)
    - symbols: dict { str: integer } for labels and resources
    - bintexts: strings in text section, encoded
    - hdrtext: header identifier
    - defines: metadata defined in bsd header
    """
    instrs = []
    symbols = {}
    texts = []
    pos = 0
    hdrtext = None
    defines = {}
    for lineidx, line in enumerate(asmtxt.split('\n')):
        line = line.strip()
        line = asdis.remove_comment(line)
        if not line:
            continue
        if asdis.re_header.match(line):
            hdrtext, = asdis.re_header.match(line).groups()
            hdrtext = asdis.unescape(hdrtext)
        elif asdis.re_define.match(line):
            name, offset_s = asdis.re_define.match(line).groups()
            defines[name] = offset_s
        elif asdis.re_label.match(line):
            symbol, = asdis.re_label.match(line).groups()
            symbols[symbol] = pos
        elif asdis.re_instr.match(line):
            fcn, args, strings = parse_instr(line, lineidx + 1, inputpo)
            record = fcn, args, pos, lineidx + 1
            texts.extend(strings)
            instrs.append(record)
            try:
                opcode = bgiop.rops[fcn]
            except KeyError:
                raise asdis.InvalidFunction('Invalid function @ line %d' % (lineidx + 1))
            pos += struct.calcsize(bgiop.ops[opcode][0]) + 4
        else:
            raise asdis.InvalidInstructionFormat(
                'Invalid instruction format @ line {:d}'.format(lineidx + 1))
    bintexts = []
    for text in texts:
        symbols[text] = pos
        text = asdis.unescape(text[1:-1])
        itext = text.encode(buriko_setup.ienc)
        itext = buriko_common.unescape_private_sequence(itext)
        bintexts.append(itext)
        pos += len(itext) + 1
    return instrs, symbols, bintexts, hdrtext, defines
예제 #4
0
def insert(data, texts):
    hdrsize, = struct.unpack('<I', data[0:4])
    code = data[hdrsize:]

    bpop.clear_offsets()
    size = get_code_end(code)
    pos = 0
    pos_text = size
    while pos_text % 0x10 != 0:
        pos_text += 1

    code_out = io.BytesIO()
    string_out = io.BytesIO()

    while pos < size:
        addr = pos
        op = code[addr]
        if op not in bpop.ops:
            raise Exception('size unknown for op %02x @ offset %05x' %
                            (op, addr))
        pos += 1

        instr = struct.pack('<B', op)
        fmt, pfmt, fcn = bpop.ops[op]
        if fmt:
            n = struct.calcsize(fmt)
            if op != 0x05:  # push_string
                instr += code[pos:pos + n]
            else:
                args = struct.unpack(fmt, code[pos:pos + n])
                instr += struct.pack(fmt, pos_text - addr)

                line = texts.pop(0).split('\n')[1]
                line = asdis.unescape(line)
                if line[:3] == '>> ':
                    text = line[3:].encode(bpconfig.senc)
                    string_out.write(text + b'\0')
                    pos_text += len(text) + 1
                elif line[:3] == '<< ':
                    text = line[3:].encode(bpconfig.ienc)
                    string_out.write(text + b'\0')
                    pos_text += len(text) + 1
                else:
                    raise "格式错误!"
            pos += n

        code_out.write(instr)

    while string_out.tell() % 0x10 != 0:
        string_out.write(b'\0')
    while code_out.tell() % 0x10 != 0:
        code_out.write(b'\0')
    code_out.write(string_out.getvalue())
    out_code = code_out.getvalue()
    out_hdr = struct.pack('<IIII', 0x10, len(out_code), 0, 0)
    return out_hdr + out_code
예제 #5
0
def parse(asmtxt):
	instrs = []
	symbols = {}
	text_set = set()
	pos = 0
	hdrtext = None
	defines = {}
	for id, line in enumerate(asmtxt.split('\n')):
		line = line.strip()
		line = asdis.remove_comment(line)
		if not line:
			continue
		if asdis.re_header.match(line):
			hdrtext, = asdis.re_header.match(line).groups()
			hdrtext = asdis.unescape(hdrtext)
		elif asdis.re_define.match(line):
			name, offset_s = asdis.re_define.match(line).groups()
			defines[name] = offset_s
		elif asdis.re_label.match(line):
			symbol, = asdis.re_label.match(line).groups()
			symbols[symbol] = pos
		elif asdis.re_instr.match(line):
			fcn, args, strings = parse_instr(line, id+1)	
			record = fcn, args, pos, id+1
			text_set.update(strings)
			instrs.append(record)
			try:
				op = bgiop.rops[fcn]
			except KeyError:
				raise asdis.InvalidFunction('Invalid function @ line %d' % (id+1))
			pos += struct.calcsize(bgiop.ops[op][0]) + 4
		else:
			raise asdis.InvalidInstructionFormat('Invalid instruction format @ line %d' % (id+1))
	texts = []
	for text in text_set:
		symbols[text] = pos
		text = asdis.unescape(text[1:-1])
		texts.append(text)
		pos += len(text.encode('cp932')) + 1
	return instrs, symbols, texts, hdrtext, defines
예제 #6
0
def parse(asmtxt):
	instrs = []
	symbols = {}
	text_set = set()
	pos = 0
	hdrtext = None
	defines = {}
	for id, line in enumerate(asmtxt.split('\n')):
		line = line.strip()
		line = asdis.remove_comment(line)
		if not line:
			continue
		if asdis.re_header.match(line):
			hdrtext, = asdis.re_header.match(line).groups()
			hdrtext = asdis.unescape(hdrtext)
		elif asdis.re_define.match(line):
			name, offset_s = asdis.re_define.match(line).groups()
			defines[name] = offset_s
		elif asdis.re_label.match(line):
			symbol, = asdis.re_label.match(line).groups()
			symbols[symbol] = pos
		elif asdis.re_instr.match(line):
			fcn, args, strings = parse_instr(line, id+1)	
			record = fcn, args, pos, id+1
			text_set.update(strings)
			instrs.append(record)
			try:
				op = bgiop.rops[fcn]
			except KeyError:
				raise asdis.InvalidFunction('Invalid function @ line %d' % (id+1))
			pos += struct.calcsize(bgiop.ops[op][0]) + 4
		else:
			raise asdis.InvalidInstructionFormat('Invalid instruction format @ line %d' % (id+1))
	texts = []
	for text in text_set:
		symbols[text] = pos
		text = asdis.unescape(text[1:-1])
		texts.append(text)
		pos += len(text.encode('cp932')) + 1
	return instrs, symbols, texts, hdrtext, defines
예제 #7
0
def parse(asmtxt):
    instrs = []
    symbols = {}
    text_set = set()
    pos = 0
    for id, line in enumerate(asmtxt.split('\n')):
        line = line.strip()
        line = asdis.remove_comment(line)
        if not line:
            continue
        if asdis.re_label.match(line):
            symbol, = asdis.re_label.match(line).groups()
            symbols[symbol] = pos
        elif asdis.re_instr.match(line):
            fcn, args, strings = parse_instr(line, id + 1)
            record = fcn, args, pos, id + 1
            text_set.update(strings)
            instrs.append(record)
            try:
                op = bpop.rops[fcn]
            except KeyError:
                raise asdis.InvalidFunction('Invalid function @ line %d' %
                                            (id + 1))
            pos += struct.calcsize(bpop.ops[op][0]) + 1
        else:
            raise asdis.InvalidInstructionFormat(
                'Invalid instruction format @ line %d' % (id + 1))
    while pos % 0x10 != 0:
        pos += 1
    texts = []
    for text in text_set:
        symbols[text] = pos
        text = asdis.unescape(text[1:-1])
        texts.append(text)
        # print(str(text.encode('gbk', 'gbk_err'), 'gbk'))
        pos += len(text.encode(text_coding, 'gbk_err')) + 1
    while pos % 0x10 != 0:
        pos += 1
    size = pos
    return instrs, symbols, texts, size