def _GetByteStreamOperation(self, data_type_definition): """Retrieves the byte stream operation. Args: data_type_definition (DataTypeDefinition): data type definition. Returns: ByteStreamOperation: byte stream operation or None if unable to determine. Raises: FormatError: if the byte stream operation cannot be determed from the data type definition. """ if not data_type_definition: raise errors.FormatError(u'Missing data type definition') members = getattr(data_type_definition, u'members', None) if not members: raise errors.FormatError( u'Invalid data type definition missing members') byte_order_string = self._GetStructByteOrderString( data_type_definition) format_string = u''.join([ member_definition.GetStructFormatString() for member_definition in members ]) format_string = u''.join([byte_order_string, format_string]) return StructOperation(format_string)
def _GetStructFormatString(self, data_type_definition): """Retrieves the Python struct format string. Args: data_type_definition (DataTypeDefinition): data type definition. Returns: str: format string as used by Python struct or None if format string cannot be determined. Raises: FormatError: if the struct format string cannot be determed from the data type definition. """ if not data_type_definition: raise errors.FormatError(u'Missing data type definition') error_string = u'' try: format_string = data_type_definition.GetStructFormatString() except (AttributeError, TypeError) as exception: format_string = None error_string = u' with error: {0!s}'.format(exception) if not format_string: raise errors.FormatError( u'Unable to determine format string{0:s}'.format(error_string)) return format_string
def _CheckCompositeMap(self, data_type_definition): """Determines if the data type definition needs a composite map. Args: data_type_definition (DataTypeDefinition): structure data type definition. Returns: bool: True if a composite map is needed, False otherwise. Raises: FormatError: if a composite map is needed cannot be determed from the data type definition. """ if not data_type_definition: raise errors.FormatError(u'Missing data type definition') members = getattr(data_type_definition, u'members', None) if not members: raise errors.FormatError( u'Invalid data type definition missing members') structure_byte_order = data_type_definition.byte_order is_composite_map = False last_member_byte_order = definitions.BYTE_ORDER_NATIVE for member_definition in members: if isinstance(member_definition, data_types.StructureMemberDefinition): member_definition = member_definition.member_data_type_definition if member_definition.IsComposite(): is_composite_map = True continue member_byte_order = member_definition.byte_order if (structure_byte_order != definitions.BYTE_ORDER_NATIVE and structure_byte_order != member_byte_order): raise errors.FormatError(( u'Contficting byte-order definitions in structure: {0:s} and ' u'member: {1:s}').format(data_type_definition.name, member_definition.name)) last_member_byte_order = member_byte_order return is_composite_map
def ReadFileObject(self, definitions_registry, file_object): """Reads data type definitions from a file-like object into the registry. Args: definitions_registry (DataTypeDefinitionsRegistry): data type definitions registry. file_object (file): file-like object to read from. Raises: FormatError: if the definitions values are missing or if the format is incorrect. """ last_definition_object = None error_location = None error_message = None try: yaml_generator = yaml.safe_load_all(file_object) for yaml_definition in yaml_generator: definition_object = self._ReadDefinition( definitions_registry, yaml_definition) if not definition_object: error_location = self._GetFormatErrorLocation( yaml_definition, last_definition_object) error_message = '{0:s} Missing definition object.'.format( error_location) raise errors.FormatError(error_message) definitions_registry.RegisterDefinition(definition_object) last_definition_object = definition_object except errors.DefinitionReaderError as exception: error_message = 'in: {0:s} {1:s}'.format( exception.name or '<NAMELESS>', exception.message) raise errors.FormatError(error_message) except (yaml.reader.ReaderError, yaml.scanner.ScannerError) as exception: error_location = self._GetFormatErrorLocation( {}, last_definition_object) error_message = '{0:s} {1!s}'.format(error_location, exception) raise errors.FormatError(error_message)
def _GetMemberDataTypeMaps(self, data_type_definition, data_type_map_cache): """Retrieves the member data type maps. Args: data_type_definition (DataTypeDefinition): data type definition. data_type_map_cache (dict[str, DataTypeMap]): cached data type maps. Returns: list[DataTypeMap]: member data type maps. Raises: FormatError: if the data type maps cannot be determed from the data type definition. """ if not data_type_definition: raise errors.FormatError(u'Missing data type definition') members = getattr(data_type_definition, u'members', None) if not members: raise errors.FormatError( u'Invalid data type definition missing members') data_type_maps = [] for member_definition in members: if isinstance(member_definition, data_types.StructureMemberDefinition): member_definition = member_definition.member_data_type_definition if member_definition.name not in data_type_map_cache: data_type_map = DataTypeMapFactory.CreateDataTypeMapByType( member_definition) data_type_map_cache[member_definition.name] = data_type_map data_type_maps.append(data_type_map_cache[member_definition.name]) return data_type_maps
def _GetElementDataTypeDefinition(self, data_type_definition): """Retrieves the element data type definition. Args: data_type_definition (DataTypeDefinition): data type definition. Returns: DataTypeDefinition: element data type definition. Raises: FormatError: if the data type map cannot be determed from the data type definition. """ if not data_type_definition: raise errors.FormatError(u'Missing data type definition') element_data_type_definition = getattr( data_type_definition, u'element_data_type_definition', None) if not element_data_type_definition: raise errors.FormatError( u'Invalid data type definition missing element') return element_data_type_definition
def ReadFileObject(self, definitions_registry, file_object): """Reads data type definitions from a file-like object into the registry. Args: definitions_registry (DataTypeDefinitionsRegistry): data type definitions registry. file_object (file): file-like object to read from. Raises: FormatError: if the definitions values are missing or if the format is incorrect. """ yaml_generator = yaml.safe_load_all(file_object) last_definition_object = None error_location = None error_message = None for yaml_definition in yaml_generator: try: definition_object = self.ReadDefinitionFromDict( definitions_registry, yaml_definition) except errors.DefinitionReaderError as exception: definition_object = None if exception.name: error_location = u'In: {0:s}'.format(exception.name) error_message = u''.join(exception.message) if not definition_object: if not error_location: name = yaml_definition.get(u'name', None) if name: error_location = u'In: {0:s}'.format(name) elif last_definition_object: error_location = u'After: {0:s}'.format( last_definition_object.name) else: error_location = u'At start' if not error_message: error_message = u'Missing definition object.' error_message = u'{0:s} {1:s}'.format(error_location, error_message) raise errors.FormatError(error_message) definitions_registry.RegisterDefinition(definition_object) last_definition_object = definition_object
def __init__(self, data_type_definition): """Initializes a data type map. Args: data_type_definition (DataTypeDefinition): data type definition. Raises: FormatError: if the data type map cannot be determed from the data type definition. """ if (data_type_definition.false_value is None and data_type_definition.true_value is None): raise errors.FormatError( u'Boolean data type has no True or False values.') super(BooleanMap, self).__init__(data_type_definition)
def __init__(self, format_string): """Initializes a Python struct-base byte stream operation. Args: format_string (str): format string as used by Python struct. Raises: FormatError: if the struct operation cannot be determed from the data type definition. """ try: struct_object = struct.Struct(format_string) except (TypeError, struct.error) as exception: raise errors.FormatError( (u'Unable to create struct object from data type definition ' u'with error: {0!s}').format(exception)) super(StructOperation, self).__init__() self._struct = struct_object self._struct_format_string = format_string