def padded_write(self, productversion, channel): self.fileobj.seek(self.additional_offset) extras = extras_header.build(dict( count=1, sections=[dict( channel=six.u(channel), productversion=six.u(productversion), size=len(channel) + len(productversion) + 2 + 8 + 10, padding=b'\x00' * 10, )], )) self.fileobj.write(extras) self.last_offset = self.fileobj.tell()
def padded_write(self, productversion, channel): self.fileobj.seek(self.additional_offset) extras = extras_header.build( dict( count=1, sections=[ dict( channel=six.u(channel), productversion=six.u(productversion), size=len(channel) + len(productversion) + 2 + 8 + 10, padding=b'\x00' * 10, ) ], )) self.fileobj.write(extras) self.last_offset = self.fileobj.tell()
def write_additional(self, productversion, channel): """Write the additional information to the MAR header. Args: productversion (str): product and version string channel (str): channel string """ self.fileobj.seek(self.additional_offset) extras = extras_header.build(dict( count=1, sections=[dict( channel=channel, productversion=productversion, size=len(channel) + len(productversion) + 2 + 8, padding='', )], )) self.fileobj.write(extras) self.last_offset = self.fileobj.tell()
def write_additional(self, productversion, channel): """Write the additional information to the MAR header. Args: productversion (str): product and version string channel (str): channel string """ self.fileobj.seek(self.additional_offset) extras = extras_header.build(dict( count=1, sections=[dict( channel=six.u(channel), productversion=six.u(productversion), size=len(channel) + len(productversion) + 2 + 8, padding=b'', )], )) self.fileobj.write(extras) self.last_offset = self.fileobj.tell()
def add_signature_block(src_fileobj, dest_fileobj, signing_algorithm, signature=None): """Add a signature block to marfile, a MarReader object. Productversion and channel are preserved, but any existing signatures are overwritten. Args: src_fileobj (file object): The input MAR file to add a signature to dest_fileobj (file object): File object to write new MAR file to. Must be open in w+b mode. signing_algorithm (str): One of 'sha1', or 'sha384' signature (bytes): Signature to write, or None to use a dummy signature """ algo_id = {'sha1': 1, 'sha384': 2}[signing_algorithm] if not signature: signature = make_dummy_signature(algo_id) src_fileobj.seek(0) mardata = mar.parse_stream(src_fileobj) # Header header = mardata.header dest_fileobj.write(mar_header.build(header)) # Signature block sig = dict(algorithm_id=algo_id, size=len(signature), signature=signature, ) # This will be fixed up later filesize = 0 sigs_offset = dest_fileobj.tell() sigs = sigs_header.build(dict( filesize=filesize, count=1, sigs=[sig], )) dest_fileobj.write(sigs) # Write the additional section dest_fileobj.write(extras_header.build(mardata.additional)) # Write the data data_offset = dest_fileobj.tell() src_fileobj.seek(mardata.data_offset) write_to_file(takeexactly(src_fileobj, mardata.data_length), dest_fileobj) # Write the index index_offset = dest_fileobj.tell() index = mardata.index # Adjust the offsets data_offset_delta = data_offset - mardata.data_offset for e in index.entries: e.offset += data_offset_delta dest_fileobj.write(index_header.build(index)) filesize = dest_fileobj.tell() # Go back and update the index offset and filesize dest_fileobj.seek(0) header.index_offset = index_offset dest_fileobj.write(mar_header.build(header)) dest_fileobj.seek(sigs_offset) sigs = sigs_header.build(dict( filesize=filesize, count=1, sigs=[sig], )) dest_fileobj.write(sigs)
def add_signature_block(src_fileobj, dest_fileobj, signing_algorithm, signature=None): """Add a signature block to marfile, a MarReader object. Productversion and channel are preserved, but any existing signatures are overwritten. Args: src_fileobj (file object): The input MAR file to add a signature to dest_fileobj (file object): File object to write new MAR file to. Must be open in w+b mode. signing_algorithm (str): One of 'sha1', or 'sha384' signature (bytes): Signature to write, or None to use a dummy signature """ algo_id = {'sha1': 1, 'sha384': 2}[signing_algorithm] if not signature: signature = make_dummy_signature(algo_id) src_fileobj.seek(0) mardata = mar.parse_stream(src_fileobj) # Header header = mardata.header dest_fileobj.write(mar_header.build(header)) # Signature block sig = dict( algorithm_id=algo_id, size=len(signature), signature=signature, ) # This will be fixed up later filesize = 0 sigs_offset = dest_fileobj.tell() sigs = sigs_header.build(dict( filesize=filesize, count=1, sigs=[sig], )) dest_fileobj.write(sigs) # Write the additional section dest_fileobj.write(extras_header.build(mardata.additional)) # Write the data data_offset = dest_fileobj.tell() src_fileobj.seek(mardata.data_offset) write_to_file(takeexactly(src_fileobj, mardata.data_length), dest_fileobj) # Write the index index_offset = dest_fileobj.tell() index = mardata.index # Adjust the offsets data_offset_delta = data_offset - mardata.data_offset for e in index.entries: e.offset += data_offset_delta dest_fileobj.write(index_header.build(index)) filesize = dest_fileobj.tell() # Go back and update the index offset and filesize dest_fileobj.seek(0) header.index_offset = index_offset dest_fileobj.write(mar_header.build(header)) dest_fileobj.seek(sigs_offset) sigs = sigs_header.build(dict( filesize=filesize, count=1, sigs=[sig], )) dest_fileobj.write(sigs)