Exemple #1
0
    def run(self, process_extra_attribs=None, process_remainder=None):
        """Run the Program.
           process_exta_attribs is a function that processes
           extra attributes of a mapped-to section, and
           process_remainder is a function that processes
           the remaining binary."""
        # Get the input
        input = raw_input("> ")
        # Decode the packet
        result = self.reader.read(converter.hex_to_binary(input.strip()))

        # Format output
        # (self.data -> a list of lists, where each sub-list is a section,
        # containing a section name at index 0 and every other index is a quad-list
        # of [field_name, hex, binary, decimal], note that the first and
        # last sections are the titles and payload respectively)
        for header_section, section_contents in result.items():
            section = []
            section.append(header_section)
            if isinstance(section_contents, basestring):  # for the payload
                section.append(section_contents)
            else:  # for all normal sections
                for key, value in section_contents[0].items():
                    section.append([key, EMPTY, value, converter.binary_to_decimal(value)])
                if process_extra_attribs is not None:  # process optional attributes with provided function, if any
                    process_extra_attribs(header_section, section_contents[1])
            self.data.append(section)
        # Get the Payload (not included in the normal output)
        payload = self.data.pop()  # list ["Payload", binary_value]
        # Get the titles
        titles = self.data.pop(0)  # list ["Field Name", "Hex", "Binary", "Decimal"]
        # Populate 'section_names'
        section_names = []
        for section in self.data:
            section_names.append(section.pop(0))  # Add section name

        # Print output
        print NEW_LINE
        counter = DigitCounter(self.column_width)
        counter.add_divide_line()
        counter.add_line(Line(titles, True))
        counter.add_section_divide_line()
        for index, section in enumerate(self.data):
            counter.add_line(Line(section_names[index], True))
            for quad in section:
                counter.read(Line(quad, False))
            counter.flush_section()
            if section == self.data[-1]:
                counter.add_divide_line()
            else:
                counter.add_section_divide_line()
        counter.flush_all()

        # Process payload
        if process_remainder is not None:
            process_remainder(payload[0], payload[1])
Exemple #2
0
 def add_mapped_section(self, mapping_type, field_value):
     '''Gets a mapped-to section and adds it to the list of sections to be processed.'''
     # bituple is a list of length 2
     bituple = self.mappings[mapping_type]
     # Error if value for field does not match any mapping value
     named_supersection = bituple[0]
     if not bituple[1].has_key(converter.binary_to_decimal(field_value)):
         raise custom_exceptions.NonExistentValueMappingException(field_value, mapping_type)
     # trituple is a list of length 3
     trituple = bituple[1][converter.binary_to_decimal(field_value)]
     maps_to_section = trituple[0]
     name_of_map = trituple[1]
     leftover_attribs = trituple[2]
     # Add the mapped-to section, if it exists
     if self.named_sections[named_supersection].has_key(maps_to_section):
         # Append mapped-to section to end of section list
         self.sections[name_of_map] = self.named_sections[named_supersection][maps_to_section]
     # Otherwise, add the mapped-to section's name with no Fields
     else:
         self.sections[name_of_map] = []
     # Mark extra attributes for the mapped-to section
     self.extra_attribs[name_of_map] = leftover_attribs