def ReadFromPayload(self, payload_file, hasher=None): """Reads the payload header from a file. Reads the payload header from the |payload_file| and updates the |hasher| if one is passed. The parsed header is stored in the _PayloadHeader instance attributes. Args: payload_file: a file object hasher: an optional hasher to pass the value through Returns: None. Raises: PayloadError if a read error occurred or the header is invalid. """ # Verify magic magic = common.Read(payload_file, len(self._MAGIC), hasher=hasher) if magic != self._MAGIC: raise PayloadError('invalid payload magic: %s' % magic) self.version = _ReadInt(payload_file, self._VERSION_SIZE, True, hasher=hasher) self.manifest_len = _ReadInt(payload_file, self._MANIFEST_LEN_SIZE, True, hasher=hasher) self.size = (len(self._MAGIC) + self._VERSION_SIZE + self._MANIFEST_LEN_SIZE) self.metadata_signature_len = 0 if self.version == common.BRILLO_MAJOR_PAYLOAD_VERSION: self.size += self._METADATA_SIGNATURE_LEN_SIZE self.metadata_signature_len = _ReadInt( payload_file, self._METADATA_SIGNATURE_LEN_SIZE, True, hasher=hasher)
def _ReadManifest(self): """Reads and returns the payload manifest. Returns: A string containing the payload manifest in binary form. Raises: PayloadError if a read error occurred. """ if not self.header: raise PayloadError('payload header not present') return common.Read(self.payload_file, self.header.manifest_len, hasher=self.manifest_hasher)
def ReadDataBlob(self, offset, length): """Reads and returns a single data blob from the update payload. Args: offset: offset to the beginning of the blob from the end of the manifest length: the blob's length Returns: A string containing the raw blob data. Raises: PayloadError if a read error occurred. """ return common.Read(self.payload_file, length, offset=self.payload_file_offset + self.data_offset + offset)
def _ReadMetadataSignature(self): """Reads and returns the metadata signatures. Returns: A string containing the metadata signatures protobuf in binary form or an empty string if no metadata signature found in the payload. Raises: PayloadError if a read error occurred. """ if not self.header: raise PayloadError('payload header not present') return common.Read(self.payload_file, self.header.metadata_signature_len, offset=self.payload_file_offset + self.header.size + self.header.manifest_len)
def _ReadInt(file_obj, size, is_unsigned, hasher=None): """Reads a binary-encoded integer from a file. It will do the correct conversion based on the reported size and whether or not a signed number is expected. Assumes a network (big-endian) byte ordering. Args: file_obj: a file object size: the integer size in bytes (2, 4 or 8) is_unsigned: whether it is signed or not hasher: an optional hasher to pass the value through Returns: An "unpacked" (Python) integer value. Raises: PayloadError if an read error occurred. """ return struct.unpack(common.IntPackingFmtStr(size, is_unsigned), common.Read(file_obj, size, hasher=hasher))[0]