def transcribe_value(B): ''' Transcribe this `Block`, the inverse of parse_value. ''' transcription = [] is_indirect = B.indirect span = B.span if is_indirect: # aside from the span, everything else comes from the superblock B = B.superblock block_type = B.type assert block_type >= 0, "block_type(%s) => %d" % (B, B.type) block_typed = block_type != BlockType.BT_HASHCODE flags = ((F_BLOCK_INDIRECT if is_indirect else 0) | (F_BLOCK_TYPED if block_typed else 0) | 0 # no F_BLOCK_TYPE_FLAGS ) transcription.append(BSUInt.transcribe_value(flags)) transcription.append(BSUInt.transcribe_value(span)) if block_typed: transcription.append(BSUInt.transcribe_value(block_type)) # no block_type_flags if block_type == BlockType.BT_HASHCODE: transcription.append(B.hashcode.transcribe_b()) elif block_type == BlockType.BT_RLE: transcription.append(B.octet) elif block_type == BlockType.BT_LITERAL: transcription.append(B.data) elif block_type == BlockType.BT_SUBBLOCK: transcription.append(BSUInt.transcribe_value(B.offset)) transcription.append(BlockRecord.transcribe_value(B.superblock)) else: raise ValueError("unsupported Block type 0x%02x: %s" % (block_type, B)) block_bs = b''.join(flatten_transcription(transcription)) return BSData(block_bs).transcribe()
def transcribe(self): ''' Transcribe this packet. ''' is_request = self.is_request channel = self.channel bss = [ BSUInt.transcribe_value(self.tag), BSUInt.transcribe_value((0x01 if channel != 0 else 0x00) | (0x02 if is_request else 0x00) | (self.flags << 2)), BSUInt.transcribe_value(channel) if channel != 0 else b'', BSUInt.transcribe_value(self.rq_type) if is_request else b'', self.payload ] length = sum(len(bs) for bs in bss) # spit out a BSData manually to avoid pointless bytes.join yield BSUInt.transcribe_value(length) yield bss
def transcribe(self): ''' Serialise to binary format. ''' E = self.dirent flags = 0 type_ = E.type if E.name: flags |= DirentFlags.HASNAME meta = None if E.isindirect else E.meta if meta: flags |= DirentFlags.HASMETA if E.uuid: flags |= DirentFlags.HASUUID block = None if type_ is DirentType.INDIRECT else E.block if block is None: flags |= DirentFlags.NOBLOCK if E._prev_dirent_blockref is not None: flags |= DirentFlags.HASPREVDIRENT extended_data = E.get_extended_data() if extended_data: flags |= DirentFlags.EXTENDED yield BSUInt.transcribe_value(type_) yield BSUInt.transcribe_value(flags) if flags & DirentFlags.HASNAME: yield BSString.transcribe_value(E.name) if flags & DirentFlags.HASMETA: yield BSString.transcribe_value(meta.textencode()) if flags & DirentFlags.HASUUID: bs = E.uuid.bytes if len(bs) != 16: raise RuntimeError("len(E.uuid.bytes) != 16: %r" % (bs, )) yield bs if not flags & DirentFlags.NOBLOCK: yield BlockRecord.transcribe_value(block) if flags & DirentFlags.HASPREVDIRENT: assert isinstance(E._prev_dirent_blockref, _Block) yield BlockRecord.transcribe_value(E._prev_dirent_blockref) if flags & DirentFlags.EXTENDED: yield extended_data
def transcribe(self): ''' Transcribe this data chunk as a data record. ''' yield BSUInt.transcribe_value(self.flags) yield BSData.transcribe_value(self._data)
def transcribe(self): yield BSString.transcribe_value(self.hashclass.HASHNAME) start_hashcode = self.start_hashcode if start_hashcode is not None: yield HashCodeField.transcribe_value(start_hashcode) yield BSUInt.transcribe_value(self.length)
def transcribe_value(hashcode): ''' Serialise a hashcode. ''' yield BSUInt.transcribe_value(hashcode.HASHENUM) yield hashcode
def data_offset(self): ''' The offset of the data chunk within the transcribed `DataRecord`. ''' return (len(BSUInt.transcribe_value(self.flags)) + BSData.data_offset_for(self._data))