def test_UB_read(): bits = BitStream("101010") assert bits.read(UB[6]) == 42 assert bits.bits_available == 0 bits = BitStream("010 0111011001 101010 10001 10111") assert bits.read(UB[3]) == 2 assert bits.bits_available == 26 assert bits.read(UB[10]) == 0b0111011001 assert bits.bits_available == 16 assert bits.read(UB[6]) == 0b101010 assert bits.bits_available == 10 assert bits.read(UB[5]) == 0b10001 assert bits.bits_available == 5 assert bits.read(UB[5]) == 0b10111 assert bits.bits_available == 0 bits = BitStream() bits.write("\xDD\xEE\xFF", ByteString) bits.seek(0) result = bits.read(UB[24]) assert result == 0xDDEEFF assert bits.bits_available == 0 bits.seek(0) result = bits.read(UB[24:"<"]) assert result == 0xFFEEDD assert bits.bits_available == 0
def get_data_stub(self): bits = BitStream() bits += Rect(XMax=self.width, YMax=self.height) bits.flush() bits.write(self.fps, FIXED8) bits.write(self.num_frames, UI16) return bits.serialize()
def serialize_data(self): bits = BitStream() bits.write(self.shapeid, UI16) bits.write(self.depth, UI16) bits += self.transform bits += self.colortransform return bits.serialize()
def serialize_style(self): bits = BitStream() bits += serialize_style_list(self.fills) bits += serialize_style_list(self.strokes) self.fillbits = nbits(len(self.fills)) self.linebits = nbits(len(self.strokes)) bits.write(self.fillbits, UB[4]) bits.write(self.linebits, UB[4]) return bits
def serialize_data(self): DefineShape._current_variant = self.variant self.shape.calculate_bounds() bits = BitStream() bits.write(self.characterid, UI16) bits += self.shape.shape_bounds bits += self.shape DefineShape._current_variant = None return bits.serialize()
def test_BitStream_specialized_format_write(): L = [1, 0, True, False] bits = BitStream() bits.write(L) assert str(bits) == "1010" bits = BitStream("11") bits.write(L) assert str(bits) == "1010" bits = BitStream() bits.write(L[3:], BitStream[1]) bits.write(L[2:], BitStream[1]) assert str(bits) == "01" test = BitStream() test.write("SWF", ByteString) test.seek(0) bits = BitStream() bits.write(test, BitStream["<"]) bits.seek(0) result = bits.read(ByteString[3]) assert result == "FWS" assert bits.bits_available == 0
def test_BitStream_specialized_format_read(): bits = BitStream("1011001") result = bits.read(BitStream[4]) assert result == [True, False, True, True] assert bits.bits_available == 3 py.test.raises(IndexError, bits.read, BitStream[4]) bits = BitStream() bits.write("SWF", ByteString) bits.seek(0) result = bits.read(BitStream[24:"<"]) result = result.read(ByteString[3]) assert result == "FWS" assert bits.bits_available == 0
def serialize_data(self): """ Serailizes this tag, according to the following format. ======== ========= Format Parameter ======== ========= Reserved UI16 - 0 Password MD5 ======== ========= """ bits = BitStream() bits.write(0, UI16) bits.write(self.password, CString) return bits.serialize()
def serialize_data(self): """ Serializes this tag, according to the following format. ======= ========= Format Parameter ======= ========= U[32] flags CSTRING name ABC abc file ======= ========= """ bits = BitStream() bits.write(self.flags, UI32) bits.write(self.name, CString) return bits.serialize() + self.abc.serialize()
def as_bitstream(self): """ Serializes this record, according to the following format. ====== ========= Format Parameter ====== ========= U[10] type U[6] length ====== ========= """ bits = BitStream() bits.write((self.id << 6) | min(self.length, 0x3F), UI16) if self.length >= 0x3F: bits.write(self.length, SI32) return bits
def test_CString_write(): # Testing basics. bits = BitStream() bits.write("FWS", CString) assert bits.bits_available == 0 assert len(bits) == 32 bits.seek(0) result = bits.read(Byte) assert result == ord("F") assert bits.bits_available == 24 result = bits.read(Byte) assert result == ord("W") assert bits.bits_available == 16 result = bits.read(Byte) assert result == ord("S") assert bits.bits_available == 8 result = bits.read(Byte) assert result == 0 assert bits.bits_available == 0
def serialize(self): code = "" code += s_u32(self._name_index) flags = BitStream() flags.write(False) flags.write(bool(self.metadata)) # Has Metadata flags.write(self.is_override) # Is Override flags.write(self.is_final) # Is Final flags.write(self.kind, UB[4]) # kind code += flags.serialize() code += self.serialize_inner() if self.metadata: code += s_u32(len(self.metadata)) for m in self._metadata_indices: code += s_u32(m) return code
def test_Byte_read(): # Testing basics. bits = BitStream("10101010 11001100") result = bits.read(Byte[1]) assert result == 0b10101010 assert bits.bits_available == 8 result = bits.read(Byte[1]) assert result == 0b11001100 assert bits.bits_available == 0 # Testing out-of-bounds checking. py.test.raises(ValueError, bits.read, Byte) # Testing length. bits.seek(0) result = bits.read(Byte[2]) assert result == 0b1010101011001100 assert bits.bits_available == 0 # Testing endianness. bits.seek(0) result = bits.read(Byte[2:"<"]) assert result == 0b1100110010101010 assert bits.bits_available == 0 # Testing length behavior. bits.seek(0) result = bits.read(Byte) # This should read one byte. assert result == 0b10101010 assert bits.bits_available == 8 # Make length not divisible by 8. bits.seek(0, os.SEEK_END) bits.write(Zero) bits.seek(0) result = bits.read(Byte) # But this should still work. assert result == 0b10101010 assert bits.bits_available == 9
def test_ByteString_read(): # Testing basics. bits = BitStream("00101010 00101111") result = bits.read(ByteString[1]) assert result == chr(0b00101010) assert bits.bits_available == 8 # Testing length. bits.seek(0) result = bits.read(ByteString[2]) assert result == chr(0b00101010) + chr(0b00101111) assert bits.bits_available == 0 # Testing endianness. bits.seek(0) result = bits.read(ByteString[2:"<"]) assert result == chr(0b00101111) + chr(0b00101010) assert bits.bits_available == 0 # Testing with no length provided. bits.seek(0) result = bits.read(ByteString) assert result == chr(0b00101010) + chr(0b00101111) # Testing non-flush length. # Make length not divisible by 8. bits.seek(0, os.SEEK_END) bits.write(Zero) bits.seek(0) result = bits.read(ByteString[2]) # This should still work. assert result == chr(0b00101010) + chr(0b00101111) assert bits.bits_available == 1 bits.seek(0) py.test.raises(ValueError, bits.read, ByteString) # But this should fail.
def serialize_style_list(lst): bits = BitStream() if len(lst) < 0xFF: bits.write(len(lst), UI8) else: bits.write(0xFF, UI8) bits.write(len(lst), UI16) for style in lst: bits += style return bits
def serialize_data(self): DefineShape._current_variant = self.variant self.shape.calculate_bounds() bits = BitStream() bits.write(self.characterid, UI16) # Shape ID bits += self.shape.shape_bounds # ShapeBounds Rect bits += self.shape.edge_bounds # EdgeBounds Rect bits.write(Zero[6]) # Reserved bits.write(self.shape.has_scaling) # UsesNonScalingStrokes bits.write(self.shape.has_non_scaling) # UsesScalingStrokes bits += self.shape # ShapeWithStyle DefineShape._current_variant = None return bits.serialize()
def test_UB_write(): bits = BitStream() bits.write(0b1111, UB[4]) assert len(bits) == 4 and str(bits) == "1111" bits = BitStream() bits.write(0b1111, UB[8]) assert len(bits) == 8 and str(bits) == "00001111" bits = BitStream() bits.write(0xDDEEFF, UB) bits.seek(0) result = bits.read(ByteString[3]) assert result == "\xDD\xEE\xFF" assert bits.bits_available == 0 bits = BitStream() bits.write(0xDDEEFF, UB["<"]) bits.seek(0) result = bits.read(ByteString[3]) assert result == "\xFF\xEE\xDD" assert bits.bits_available == 0
def test_CString_read(): # Testing basics. bits = BitStream() test_data = "test 123\x01\xFF" bits.write(test_data, ByteString) bits.write(Zero[8]) bits.seek(0) result = bits.read(CString) assert result == test_data # Testing error-handling. bits = BitStream() bits.write("adsfasfdgjklhrgokrjygaosaf", ByteString) bits.seek(0) py.test.raises(ValueError, bits.read, CString)
def serialize_data(self): """ Serializes this tag, according to the following format. ======= ============= Format Parameter ======= ============= U[16] character id CSTRING classsname ======= ============= """ bits = BitStream() bits.write(len(self.symbols), UI16) for char_id, classname in self.symbols.iteritems(): bits.write(char_id, UI16) bits.write(classname, CString) return bits.serialize()
def serialize_data(self): bits = BitStream() bits.write(Zero[3]) bits.write(self.hasMetadata) bits.write(self.useAS3) bits.write(Zero[2]) bits.write(self.useNetwork) bits.write(Zero[24]) return bits.serialize()
def serialize_data(self): bits = BitStream() bits.write(self.characterid, UI16) bits.write(self.mc.num_frames, UI16) return bits.serialize + self.mc.serialize()
def test_FormatArray_write(): bits = BitStream() bits.write([True, False]*3, Bit[:][6]) assert str(bits) == "101010"
def serialize_data(self): HasCharacterId = (self.charid is not None) and (not self.update) bits = BitStream() bits.write(False) # HasClipActions bits.write(False) # HasClipDepth bits.write(self.name is not None) # HasName bits.write(False) # HasRatio bits.write(self.colortransform is not None) bits.write(self.transform is not None) bits.write(HasCharacterId) # HasCharacterId bits.write(self.update) # FlagMove bits.write(self.depth, UI16) if HasCharacterId: bits.write(self.charid, UI16) if self.name is not None: bits.write(self.name, CString) if self.transform is not None: bits += self.transform if self.colortransform is not None: bits += self.colortransform return bits.serialize()
def test_sequence_to_IFormat(): bits = BitStream() bits.write([True, False, True]) assert str(bits) == "101"
def serialize_data(self): bits = BitStream() bits.write(self.depth, UI16) return bits.serialize()
def as_bitstream(self): bits = BitStream() bits.write(self.TYPE, UI16) bits.write(Struct.as_bitstream(self))
def test_Bit_write(): bits = BitStream() # Testing IFormat adapting. bits.write(True) bits.write(False) # Testing Bit format. bits.write(1, Bit) bits.write(0, Bit) # Testing Bit format adaption by Bool. bits.write(bits, Bit) # Should be True. bits.write([], Bit) # Should be False. # Testing boolean formats. bits.write(One) bits.write(Zero) assert str(bits) == "10101010" assert bits.bits_available == 0
def serialize_data(self): bits = BitStream() bits.write(self.characterid, UI16) bits += self.rect bits.flush() flags = BitStream() flags.write(self.text != "") flags.write(self.wordwrap) flags.write(self.multiline) flags.write(self.password) flags.write(self.readonly) flags.write(self.color is not None) flags.write(self.maxlength is not None) flags.write(self.font is not None) flags.write(self.fontclass is not None) flags.write(self.autosize) flags.write(self.layout is not None) flags.write(not self.selectable) flags.write(self.border) flags.write(self.wasstatic) flags.write(self.isHTML) flags.write(self.outlines) bits += flags if self.font is not None: bits.write(self.font.id, UI16) # Doesn't exist yet. bits.write(self.size, UI16) if self.fontclass is not None: bits.write(self.fontclass, CString) if self.color is not None: bits += self.color if self.maxlength is not None: bits.write(self.maxlength, UI16) if self.layout is not None: bits += self.layout # Doesn't exist yet. bits.write(self.variable, CString) if self.text != "": bits.write(self.text, CString) return bits.serialize()
def test_Byte_write(): # Testing basics. bits = BitStream() bits.write(42, Byte) assert str(bits) == "00101010" # Testing signed numbers. bits = BitStream() bits.write(-42, SignedByte) assert str(bits) == "11010110" # Testing multiple-byte values. bits = BitStream() bits.write(1033, Byte) assert str(bits) == "0000010000001001" # Testing explicit length. bits = BitStream() bits.write(1033, Byte[3]) assert str(bits) == "000000000000010000001001" # Testing explicit length with 0. bits = BitStream() bits.write(0, Byte[3]) assert str(bits) == "000000000000000000000000" # Testing leftover < 0. bits = BitStream() py.test.raises(ValueError, bits.write, 1033, Byte[1]) # Testing endianness. bits = BitStream() bits.write(1033, Byte["<"]) assert str(bits) == "0000100100000100" # Testing endianness and explicit length. bits = BitStream() bits.write(1033, Byte[3:"<"]) assert str(bits) == "000010010000010000000000" # Testing endianness and explicit length with 0. bits = BitStream() bits.write(0, Byte[3:"<"]) assert str(bits) == "000000000000000000000000"
def _write(self, bs, cursor, value): from fusion.bitstream.bitstream import BitStream bits = BitStream() if value in (0, 0.0): # value is zero bits.write(Zero[self.length]) elif value == -0.0: bits.write(One) bits.write(Zero[self.length-1]) elif isnan(value): bits.write(One [self.length]) elif value == float("-inf"): # negative infinity bits.write(One [self._N_EXPN_BITS[self.length] + 1]) # sign merged bits.write(Zero[self._N_FRAC_BITS[self.length]]) elif value == float("inf"): # positive infinity bits.write(Zero) bits.write(One [self._N_EXPN_BITS[self.length]]) bits.write(Zero[self._N_FRAC_BITS[self.length]]) else: if value < 0: bits.write(One) value = ~value + 1 else: bits.write(Zero) exp = self._EXPN_BIAS[self.length] if value < 1: while int(value) != 1: value *= 2 exp -= 1 else: while int(value) != 1: value /= 2 exp += 1 if exp < 0 or exp > (1 << self._N_EXPN_BITS[self.length]): raise ValueError("Exponent out of range in %s." % (self,)) frac_total = 1 << self._N_FRAC_BITS[self.length] bits.write(exp, UB[self._N_EXPN_BITS[self.length]]) bits.write(int((value-1)*frac_total) & (frac_total - 1), UB[self._N_FRAC_BITS[self.length]]) bs.write(bits, BitStream[self.endianness])