Beispiel #1
0
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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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