Exemple #1
0
	def __init__(self, arg):
		self.dst, self.src, self.c = lex.parse_args(arg)
Exemple #2
0
    def parse(self, fname):
        print "opening file %s..." % (fname)
        self.line_number = 0
        skipping_binary_data = False

        num = 0x1000
        if num:
            l = ['0'] * num
            self.binary_data += l

            self.dummy_enum += 1
            labell = "dummy" + str(self.dummy_enum)

            self.c_data.append("{0}, // padding\n")
            self.h_data.append(" db " + labell + "[" + str(num) +
                               "]; // protective\n")

        fd = open(fname, 'rb')
        for line in fd:
            self.line_number += 1
            line = line.strip()
            if len(line) == 0 or line[0] == ';' or line[0] == chr(0x1a):
                continue

            print "%d:      %s" % (self.line_number, line)

            m = re.match('([@\w]+)\s*::?', line)
            if m is not None:
                line = m.group(1).strip()
                print line
                self.add_label(line)
                continue

            cmd = line.split()
            if len(cmd) == 0:
                continue

            cmd0 = str(cmd[0])
            cmd0l = cmd0.lower()
            if cmd0l == 'if':
                self.push_if(cmd[1])
                continue
            elif cmd0l == 'else':
                self.push_else()
                continue
            elif cmd0l == 'endif':
                self.pop_if()
                continue
            elif cmd0l == 'align':
                continue

            if not self.visible():
                continue

            if cmd0l in ['db', 'dw', 'dd', 'dq']:
                arg = line[len(cmd0):].strip()
                if not skipping_binary_data:
                    print "%d:1: %s" % (self.cur_seg_offset, arg
                                        )  #fixme: COPYPASTE
                    cmd0 = cmd0.lower()
                    binary_width = {'b': 1, 'w': 2, 'd': 4, 'q': 8}[cmd0[1]]
                    b = self.convert_data_to_blob(binary_width,
                                                  lex.parse_args(arg))
                    self.binary_data += b
                    self.cur_seg_offset += len(b)

                    c, h, elements = self.convert_data_to_c(
                        "", binary_width, lex.parse_args(arg))
                    self.c_data += c
                    self.h_data += h
                continue
            elif cmd0l == 'include':
                self.include(os.path.dirname(fname), cmd[1])
                continue
            elif cmd0l == 'endp' or (len(cmd) >= 2
                                     and str(cmd[1].lower()) == 'endp'):
                #self.proc = None
                continue
            elif cmd0l == 'ends':
                print "segement %s ends" % (self.segment)
                self.segment = ""
                continue
            elif cmd0l == 'assume':
                print "skipping: %s" % line
                continue
            elif cmd0l in ['rep', 'repe', 'repne']:
                self.proc.add(cmd0l)
                self.proc.add(" ".join(cmd[1:]))
                continue
            elif cmd0l == 'end':
                if len(cmd) >= 2:
                    self.entry_point = cmd[1].lower()
                continue

            if len(cmd) >= 2:
                cmd1l = cmd[1].lower()
                if cmd1l == 'proc':
                    cmd2l = ""
                    if len(cmd) >= 3:
                        cmd2l = cmd[2].lower()
                    '''
					name = cmd0l
					self.proc = proc(name)
					print "procedure %s, #%d" %(name, len(self.proc_list))
					self.proc_list.append(name)
					self.set_global(name, self.proc)
					'''
                    self.add_label(cmd0l, far=cmd2l, proc=True)
                    continue
                elif cmd1l == 'segment':
                    name = cmd0l
                    self.segment = name

                    binary_width = 1
                    offset = int(len(self.binary_data) / 16)
                    print "segment %s %x" % (name, offset)
                    self.cur_seg_offset = 16

                    num = (0x10 - (len(self.binary_data) & 0xf)) & 0xf
                    if num:
                        l = ['0'] * num
                        self.binary_data += l

                        self.dummy_enum += 1
                        labell = "dummy" + str(self.dummy_enum)

                        self.c_data.append("{" + ",".join(l) +
                                           "}, // padding\n")
                        self.h_data.append(" db " + labell + "[" + str(num) +
                                           "]; // padding\n")

                    num = 0x10
                    l = ['0'] * num
                    self.binary_data += l

                    self.c_data.append("{" + ",".join(l) + "}, // segment " +
                                       name + "\n")
                    self.h_data.append(" db " + name + "[" + str(num) +
                                       "]; // segment " + name + "\n")

                    self.set_global(
                        name, op.var(binary_width, offset, issegment=True))
                    if self.proc == None:
                        name = "mainproc"
                        self.proc = proc(name)
                        #print "procedure %s, #%d" %(name, len(self.proc_list))
                        self.proc_list.append(name)
                        self.set_global(name, self.proc)
                    continue

                elif cmd1l == 'ends':
                    print "segment %s ends" % (self.segment)
                    self.segment = ""
                    continue

            if len(cmd) >= 3:
                cmd1l = cmd[1].lower()
                if cmd1l in ['equ', '=']:
                    if not (cmd0.lower() in self.skip_binary_data):
                        v = " ".join(cmd[2:])
                        print "value1 %s" % v
                        vv = self.fix_dollar(v)
                        vv = " ".join(lex.parse_args(vv))
                        vv = vv.strip()
                        print "value2 %s" % vv

                        size = 0
                        m = re.match(r'byte\s+ptr\s+(.*)', vv)
                        if m is not None:
                            vv = m.group(1).strip()
                            size = 1

                        m = re.match(r'[dq]word\s+ptr\s+(.*)', vv)
                        if m is not None:
                            vv = m.group(1).strip()
                            size = 4

                        m = re.match(r'word\s+ptr\s+(.*)', vv)
                        if m is not None:
                            vv = m.group(1).strip()
                            size = 2
                        '''
						if cmd1l == 'equ':
							self.set_global(cmd0, op.const(vv, size=size))
						elif cmd1l == '=':
							self.reset_global(cmd0, op.const(vv, size=size))
						'''
                        if self.proc is not None:
                            self.proc.add_equ(cmd0.lower(),
                                              vv,
                                              line_number=self.line_number)
                    else:
                        print "skipping binary data for %s" % (cmd0.lower(), )
                        skipping_binary_data = True
                    continue
                elif cmd1l in ['db', 'dw', 'dd', 'dq']:
                    if not (cmd0.lower() in self.skip_binary_data):
                        name = cmd0
                        name = re.sub(r'@', "arb", name)
                        binary_width = {
                            'b': 1,
                            'w': 2,
                            'd': 4,
                            'q': 8
                        }[cmd1l[1]]
                        offset = self.cur_seg_offset
                        arg = line[len(cmd0l):].strip()
                        arg = arg[len(cmd1l):].strip()
                        print "%d: %s" % (offset, arg)
                        b = self.convert_data_to_blob(binary_width,
                                                      lex.parse_args(arg))
                        self.binary_data += b
                        self.cur_seg_offset += len(b)

                        c, h, elements = self.convert_data_to_c(
                            name, binary_width, lex.parse_args(arg))
                        self.c_data += c
                        self.h_data += h

                        print "~size %d elements %d" % (binary_width, elements)
                        self.set_global(
                            name,
                            op.var(binary_width,
                                   offset,
                                   name=name,
                                   segment=self.segment), elements)
                        skipping_binary_data = False
                    else:
                        print "skipping binary data for %s" % (cmd0l, )
                        skipping_binary_data = True
                    continue

            if (self.proc):
                self.proc.add(line, line_number=self.line_number)
            else:
                #print line
                pass

        fd.close()

        num = (0x10 - (len(self.binary_data) & 0xf)) & 0xf
        if num:
            l = ['0'] * num
            self.binary_data += l

            self.dummy_enum += 1
            labell = "dummy" + str(self.dummy_enum)

            self.c_data.append("{" + ",".join(l) + "}, // padding\n")
            self.h_data.append(" db " + labell + "[" + str(num) +
                               "]; // padding\n")

        return self
Exemple #3
0
	def __init__(self, arg):
		self.regs = []
		for r in lex.parse_args(arg)[0].split():
			self.regs.append(self.parse_arg(r))
Exemple #4
0
	def split(self, text):
		#print "text %s" %text
#		traceback.print_stack(file=sys.stdout)
		return lex.parse_args(text)
Exemple #5
0
	def split(self, text):
		a, b = lex.parse_args(text)
		return self.parse_arg(a), self.parse_arg(b)
Exemple #6
0
	def parse(self, fname):
		print "opening file %s..." %(fname)
		self.line_number = 0
		skipping_binary_data = False

		num = 0x1000
		if num:
						l = [ '0' ] * num
						self.binary_data += l

						self.dummy_enum += 1
						labell = "dummy" + str(self.dummy_enum)

						self.c_data.append("{0}, // padding\n")
						self.h_data.append(" db " + labell + "["+ str(num) + "]; // protective\n")

		fd = open(fname, 'rb')
		for line in fd:
			self.line_number += 1
			line = line.strip()
			if len(line) == 0 or line[0] == ';' or line[0] == chr(0x1a):
				continue

			print "%d:      %s" %(self.line_number, line)

			m = re.match('([@\w]+)\s*::?', line)
			if m is not None:
				line = m.group(1).strip()
				print line
				self.add_label(line)
				continue

			cmd = line.split()
			if len(cmd) == 0:
				continue
			
			cmd0 = str(cmd[0])
			cmd0l = cmd0.lower()
			if cmd0l == 'if':
				self.push_if(cmd[1])
				continue
			elif cmd0l == 'else':
				self.push_else()
				continue
			elif cmd0l == 'endif':
				self.pop_if()
				continue
			elif cmd0l == 'align':
				continue
			
			if not self.visible():
				continue

			if cmd0l in ['db', 'dw', 'dd', 'dq']:
				arg = line[len(cmd0):].strip()
				if not skipping_binary_data:
					print "%d:1: %s" %(self.cur_seg_offset, arg) #fixme: COPYPASTE
					cmd0 = cmd0.lower()
					binary_width = {'b': 1, 'w': 2, 'd': 4, 'q': 8}[cmd0[1]]
					b = self.convert_data_to_blob(binary_width, lex.parse_args(arg))
					self.binary_data += b
					self.cur_seg_offset += len(b)
					
					c, h, elements = self.convert_data_to_c("", binary_width, lex.parse_args(arg))
					self.c_data += c
					self.h_data += h
				continue
			elif cmd0l == 'include':
				self.include(os.path.dirname(fname), cmd[1])
				continue
			elif cmd0l == 'endp' or (len(cmd) >= 2 and str(cmd[1].lower()) == 'endp'):
				#self.proc = None
				continue
			elif cmd0l == 'ends':
				print "segement %s ends" %(self.segment)
				self.segment = ""
				continue
			elif cmd0l == 'assume':
				print "skipping: %s" %line
				continue
			elif cmd0l in ['rep','repe','repne']:
				self.proc.add(cmd0l)
				self.proc.add(" ".join(cmd[1:]))
				continue
			elif cmd0l == 'end':
				if len(cmd) >= 2:
					self.entry_point = cmd[1].lower()
				continue
			
			if len(cmd) >= 2:
				cmd1l = cmd[1].lower()
				if cmd1l == 'proc':
					cmd2l = ""
					if len(cmd) >= 3:
						cmd2l = cmd[2].lower()
					'''
					name = cmd0l
					self.proc = proc(name)
					print "procedure %s, #%d" %(name, len(self.proc_list))
					self.proc_list.append(name)
					self.set_global(name, self.proc)
					'''
					self.add_label(cmd0l, far=cmd2l, proc = True)
					continue
				elif cmd1l == 'segment':
					name = cmd0l
					self.segment = name

					binary_width = 1
					offset = int(len(self.binary_data)/16)
					print "segement %s %x" %(name, offset)
					self.cur_seg_offset = 16

					num = (0x10 - (len(self.binary_data) & 0xf)) & 0xf
					if num:
						l = [ '0' ] * num
						self.binary_data += l

						self.dummy_enum += 1
						labell = "dummy" + str(self.dummy_enum)

						self.c_data.append("{"+ ",".join(l) +"}, // padding\n")
						self.h_data.append(" db " + labell + "["+ str(num) + "]; // padding\n")

					num = 0x10
					l = [ '0' ] * num
					self.binary_data += l

					self.c_data.append("{"+ ",".join(l) +"}, // segment " + name + "\n")
					self.h_data.append(" db " + name + "["+ str(num) + "]; // segment " + name + "\n")

					self.set_global(name, op.var(binary_width, offset, issegment = True))
					if self.proc == None:
						name = "mainproc"
						self.proc = proc(name)
						#print "procedure %s, #%d" %(name, len(self.proc_list))
						self.proc_list.append(name)
						self.set_global(name, self.proc)
					continue

				elif cmd1l == 'ends':
					print "segment %s ends" %(self.segment)
					self.segment = ""
					continue

			if len(cmd) >= 3:
				cmd1l = cmd[1].lower()
				if cmd1l in ['equ','=']:
					if not (cmd0.lower() in self.skip_binary_data):
						v = " ".join(cmd[2:])
						print "value1 %s" %v
						vv = self.fix_dollar(v)
						vv = " ".join(lex.parse_args(vv))
						vv = vv.strip()
						print "value2 %s" %vv

						size = 0
						m = re.match(r'byte\s+ptr\s+(.*)', vv)
						if m is not None:
							vv = m.group(1).strip()
							size = 1

						m = re.match(r'[dq]word\s+ptr\s+(.*)', vv)
						if m is not None:
							vv = m.group(1).strip()
							size = 4

						m = re.match(r'word\s+ptr\s+(.*)', vv)
						if m is not None:
							vv = m.group(1).strip()
							size = 2
						'''
						if cmd1l == 'equ':
							self.set_global(cmd0, op.const(vv, size=size))
						elif cmd1l == '=':
							self.reset_global(cmd0, op.const(vv, size=size))
						'''
						self.proc.add_equ(cmd0.lower(), vv, line_number=self.line_number)
					else:
						print "skipping binary data for %s" % (cmd0.lower(),)
						skipping_binary_data = True
					continue
				elif cmd1l  in ['db', 'dw', 'dd', 'dq']:
					if not (cmd0.lower() in self.skip_binary_data):
						name = cmd0
						name = re.sub(r'@', "arb", name)
						binary_width = {'b': 1, 'w': 2, 'd': 4, 'q': 8}[cmd1l[1]]
						offset = self.cur_seg_offset
						arg = line[len(cmd0l):].strip()
						arg = arg[len(cmd1l):].strip()
						print "%d: %s" %(offset, arg)
						b = self.convert_data_to_blob(binary_width, lex.parse_args(arg))
						self.binary_data += b
						self.cur_seg_offset += len(b)

						c, h, elements = self.convert_data_to_c(name, binary_width, lex.parse_args(arg))
						self.c_data += c
						self.h_data += h
						
						print "~size %d elements %d" %(binary_width, elements)
						self.set_global(name, op.var(binary_width, offset, name=name,segment=self.segment), elements)
						skipping_binary_data = False
					else:
						print "skipping binary data for %s" % (cmd0l,)
						skipping_binary_data = True
					continue

			if (self.proc):
				self.proc.add(line, line_number=self.line_number)
			else:
				#print line
				pass
			
		fd.close()

		num = (0x10 - (len(self.binary_data) & 0xf)) & 0xf
		if num:
						l = [ '0' ] * num
						self.binary_data += l

						self.dummy_enum += 1
						labell = "dummy" + str(self.dummy_enum)

						self.c_data.append("{"+ ",".join(l) +"}, // padding\n")
						self.h_data.append(" db " + labell + "["+ str(num) + "]; // padding\n")

		return self
Exemple #7
0
    def parse(self, fname):
        # print "opening file %s..." %(fname, basedir)
        skipping_binary_data = False
        fd = open(fname, 'rb')
        for line in fd:
            line = line.strip()
            if len(line) == 0 or line[0] == ';' or line[0] == chr(0x1a):
                continue

            # print line
            m = re.match('(\w+)\s*?:', line)
            if m is not None:
                line = line[len(m.group(0)):].strip()
                if self.visible():
                    name = m.group(1)
                    if not (name.lower() in self.skip_binary_data):
                        if self.proc is not None:
                            self.proc.add_label(name)
                        print "offset %s -> %d" % (name, len(self.binary_data))
                        self.set_offset(name, (
                        len(self.binary_data), self.proc, len(self.proc.stmts) if self.proc is not None else 0))
                        skipping_binary_data = False
                    else:
                        print "skipping binary data for %s" % (name,)
                        skipping_binary_data = True
            # print line

            cmd = line.split()
            if len(cmd) == 0:
                continue

            cmd0 = str(cmd[0])
            if cmd0 == 'if':
                self.push_if(cmd[1])
                continue
            elif cmd0 == 'else':
                self.push_else()
                continue
            elif cmd0 == 'endif':
                self.pop_if()
                continue

            if not self.visible():
                continue

            if cmd0 == 'db' or cmd0 == 'dw' or cmd0 == 'dd':
                arg = line[len(cmd0):].strip()
                if not skipping_binary_data:
                    print "%d:1: %s" % (len(self.binary_data), arg)  # fixme: COPYPASTE
                    binary_width = {'b': 1, 'w': 2, 'd': 4}[cmd0[1]]
                    self.binary_data += self.compact_data(binary_width, lex.parse_args(arg))
                continue
            elif cmd0 == 'include':
                self.include(os.path.dirname(fname), cmd[1])
                continue
            elif cmd0 == 'endp':
                self.proc = None
                continue
            elif cmd0 == 'assume':
                print "skipping: %s" % line
                continue
            elif cmd0 == 'rep':
                self.proc.add(cmd0)
                self.proc.add(" ".join(cmd[1:]))
                continue

            if len(cmd) >= 3:
                cmd1 = cmd[1]
                if cmd1 == 'equ':
                    if not (cmd0.lower() in self.skip_binary_data):
                        v = cmd[2]
                        self.set_global(cmd0, op.const(self.fix_dollar(v)))
                    else:
                        print "skipping binary data for %s" % (cmd0.lower(),)
                        skipping_binary_data = True
                elif cmd1 == 'db' or cmd1 == 'dw' or cmd1 == 'dd':
                    if not (cmd0.lower() in self.skip_binary_data):
                        binary_width = {'b': 1, 'w': 2, 'd': 4}[cmd1[1]]
                        offset = len(self.binary_data)
                        arg = line[len(cmd0):].strip()
                        arg = arg[len(cmd1):].strip()
                        print "%d: %s" % (offset, arg)
                        self.binary_data += self.compact_data(binary_width, lex.parse_args(arg))
                        self.set_global(cmd0.lower(), op.var(binary_width, offset))
                        skipping_binary_data = False
                    else:
                        print "skipping binary data for %s" % (cmd0.lower(),)
                        skipping_binary_data = True
                    continue
                elif cmd1 == 'proc':
                    name = cmd0.lower()
                    self.proc = proc(name)
                    print "procedure %s, #%d" % (name, len(self.proc_list))
                    self.proc_list.append(name)
                    self.set_global(name, self.proc)
                    continue
            if self.proc:
                self.proc.add(line)
            else:
                # print line
                pass

        fd.close()
        return self
Exemple #8
0
 def split(self, text):
     a, b = lex.parse_args(text)
     return self.parse_arg(a), self.parse_arg(b)
Exemple #9
0
    def parse(self, fname):
        #		print "opening file %s..." %(fname, basedir)
        skipping_binary_data = False
        fd = open(fname, 'rb')
        for line in fd:
            line = line.strip()
            if len(line) == 0 or line[0] == ';' or line[0] == chr(0x1a):
                continue

            #print line
            m = re.match('(\w+)\s*?:', line)
            if m is not None:
                line = line[len(m.group(0)):].strip()
                if self.visible():
                    name = m.group(1)
                    if not (name.lower() in self.skip_binary_data):
                        if self.proc is not None:
                            self.proc.add_label(name)
                        print "offset %s -> %d" % (name, len(self.binary_data))
                        self.set_offset(name,
                                        (len(self.binary_data), self.proc,
                                         len(self.proc.stmts)
                                         if self.proc is not None else 0))
                        skipping_binary_data = False
                    else:
                        print "skipping binary data for %s" % (name, )
                        skipping_binary_data = True
            #print line

            cmd = line.split()
            if len(cmd) == 0:
                continue

            cmd0 = str(cmd[0])
            if cmd0 == 'if':
                self.push_if(cmd[1])
                continue
            elif cmd0 == 'else':
                self.push_else()
                continue
            elif cmd0 == 'endif':
                self.pop_if()
                continue

            if not self.visible():
                continue

            if cmd0 == 'db' or cmd0 == 'dw' or cmd0 == 'dd':
                arg = line[len(cmd0):].strip()
                if not skipping_binary_data:
                    print "%d:1: %s" % (len(self.binary_data), arg
                                        )  #fixme: COPYPASTE
                    binary_width = {'b': 1, 'w': 2, 'd': 4}[cmd0[1]]
                    self.binary_data += self.compact_data(
                        binary_width, lex.parse_args(arg))
                continue
            elif cmd0 == 'include':
                self.include(os.path.dirname(fname), cmd[1])
                continue
            elif cmd0 == 'endp':
                self.proc = None
                continue
            elif cmd0 == 'assume':
                print "skipping: %s" % line
                continue
            elif cmd0 == 'rep':
                self.proc.add(cmd0)
                self.proc.add(" ".join(cmd[1:]))
                continue

            if len(cmd) >= 3:
                cmd1 = cmd[1]
                if cmd1 == 'equ':
                    if not (cmd0.lower() in self.skip_binary_data):
                        v = cmd[2]
                        self.set_global(cmd0, op.const(self.fix_dollar(v)))
                    else:
                        print "skipping binary data for %s" % (cmd0.lower(), )
                        skipping_binary_data = True
                elif cmd1 == 'db' or cmd1 == 'dw' or cmd1 == 'dd':
                    if not (cmd0.lower() in self.skip_binary_data):
                        binary_width = {'b': 1, 'w': 2, 'd': 4}[cmd1[1]]
                        offset = len(self.binary_data)
                        arg = line[len(cmd0):].strip()
                        arg = arg[len(cmd1):].strip()
                        print "%d: %s" % (offset, arg)
                        self.binary_data += self.compact_data(
                            binary_width, lex.parse_args(arg))
                        self.set_global(cmd0.lower(),
                                        op.var(binary_width, offset))
                        skipping_binary_data = False
                    else:
                        print "skipping binary data for %s" % (cmd0.lower(), )
                        skipping_binary_data = True
                    continue
                elif cmd1 == 'proc':
                    name = cmd0.lower()
                    self.proc = proc(name)
                    print "procedure %s, #%d" % (name, len(self.proc_list))
                    self.proc_list.append(name)
                    self.set_global(name, self.proc)
                    continue
            if (self.proc):
                self.proc.add(line)
            else:
                #print line
                pass

        fd.close()
        return self