Ejemplo n.º 1
0
 def serialize_style(self):
     """
     Serializes the style portion of this record.
     """
     bits = BitStream()
     bits.zero_fill(8)  # NumFillBits and NumLineBits
     return bits
Ejemplo n.º 2
0
 def serialize_style(self):
     """
     Serializes the style portion of this record.
     """
     bits = BitStream()
     bits.zero_fill(8) # NumFillBits and NumLineBits
     return bits
Ejemplo n.º 3
0
def test_Bit_read():
    bits = BitStream("1001")
    assert bits.read(Bit) == 1
    assert bits.read(Bit) == 0
    assert bits.read(Bit) == 0
    assert bits.read(Bit) == 1
    assert bits.bits_available == 0
    py.test.raises(IndexError, bits.read, Bit)
Ejemplo n.º 4
0
def test_Bit_read():
    bits = BitStream("1001")
    assert bits.read(Bit) == 1
    assert bits.read(Bit) == 0
    assert bits.read(Bit) == 0
    assert bits.read(Bit) == 1
    assert bits.bits_available == 0
    py.test.raises(IndexError, bits.read, Bit)
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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
Ejemplo n.º 7
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()
Ejemplo n.º 8
0
def test_constructor():
    bits = BitStream("10")
    assert bits.bits == [True, False]

    bits = BitStream("10101100")
    assert bits.bits == [True, False, True, False, True, True, False, False]

    bits = BitStream("  1  ")
    assert len(bits) == 1

    bits = BitStream([True, False, True, False])
    assert str(bits) == "1010"
Ejemplo n.º 9
0
    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()
Ejemplo n.º 10
0
    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()
Ejemplo n.º 11
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
Ejemplo n.º 12
0
    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
Ejemplo n.º 13
0
 def as_bitstream(self):
     bitstream = BitStream()
     bitstream.byte_aligned = getattr(self.create_fields, "byte_aligned", False)
     self.TEMP_FIELDS = {}
     statements = {}
     for statement in self.create_fields():
         statement = IStructStatement(statement)
         statement._pre_write(self)
         statements[statement] = statement
     self.writing = True
     for statement in self.create_fields():
         statement = IStructStatement(statements.get(statement, statement))
         statement._struct_write(self, bitstream)
     del self.TEMP_FIELDS
     self.writing = False
     bitstream.seek(0)
     return bitstream
Ejemplo n.º 14
0
def test_matrix_read():
    for tup, bits in matrix_testcases:
        matrix = TestMatrix.from_bitstream(BitStream(bits))
        assert matrix.a == tup[0]
        assert matrix.b == tup[1]
        assert matrix.c == tup[2]
        assert matrix.d == tup[3]
        assert matrix.tx == tup[4]
        assert matrix.ty == tup[5]
Ejemplo n.º 15
0
 def as_bitstream(self):
     bitstream = BitStream()
     bitstream.byte_aligned = getattr(self.create_fields, "byte_aligned",
                                      False)
     self.TEMP_FIELDS = {}
     statements = {}
     for statement in self.create_fields():
         statement = IStructStatement(statement)
         statement._pre_write(self)
         statements[statement] = statement
     self.writing = True
     for statement in self.create_fields():
         statement = IStructStatement(statements.get(statement, statement))
         statement._struct_write(self, bitstream)
     del self.TEMP_FIELDS
     self.writing = False
     bitstream.seek(0)
     return bitstream
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
    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()
Ejemplo n.º 19
0
    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()
Ejemplo n.º 20
0
    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()
Ejemplo n.º 21
0
    def dataReceived(self, data):
        self.recvd = self.recvd + data
        while len(self.recvd) >= self.headerSize:
            header = self.recvd[:self.headerSize]
            length, commandid = self.headerSize.unpack(header)
            if len(self.recvd) < self.headerSize + length:
                break
            data = self.recvd[self.headerSize:self.headerSize + length]

            command = commands.get_in_command(commandid)()
            command.raw_data = data
            command.data = BitStream(data)
            self.commandReceived(command)
Ejemplo n.º 22
0
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
Ejemplo n.º 23
0
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
Ejemplo n.º 24
0
    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()
Ejemplo n.º 25
0
    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()
Ejemplo n.º 26
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()
Ejemplo n.º 27
0
    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()
Ejemplo n.º 28
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()
Ejemplo n.º 29
0
    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
Ejemplo n.º 30
0
    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()
Ejemplo n.º 31
0
    def as_bitstream(self):
        """
        Serializes this record, according to the following format.

        ================= ============
        Format            Parameter
        ================= ============

        SHAPERECORD[...]  the shape records
        UB[24]            always 0
        ================= ============
        """

        if EndShapeRecord not in self.records:
            self.records.append(EndShapeRecord)

        bits = BitStream()
        bits += self.serialize_style()
        recn = len(self.records)
        for i, record in enumerate(self.records):
            bits += record

        return bits
Ejemplo n.º 32
0
def test_sequence_to_IFormat():
    bits = BitStream()

    bits.write([True, False, True])
    assert str(bits) == "101"
Ejemplo n.º 33
0
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
Ejemplo n.º 34
0
 def serialize_data(self):
     bits = BitStream()
     bits.write(self.depth, UI16)
     return bits.serialize()
Ejemplo n.º 35
0
def test_FormatArray_write():
    bits = BitStream()

    bits.write([True, False] * 3, Bit[:][6])
    assert str(bits) == "101010"
Ejemplo n.º 36
0
def test_matrix_write():
    for tup, bits in matrix_testcases:
        matrix = TestMatrix(*tup)
        assert matrix.as_bitstream() == BitStream(bits)
Ejemplo n.º 37
0
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
Ejemplo n.º 38
0
def test_skip_flush():
    bits = BitStream("111")
    bits.seek(0)
    bits.skip_flush()
    assert str(bits) == "111"
    assert bits.bits_available == 3

    bits.seek(0, os.SEEK_END)
    bits.skip_flush()
    assert str(bits) == "111"
    assert bits.bits_available == 0

    bits = BitStream("11110000 111")
    bits.skip_flush()
    assert bits.tell() == 0
    assert bits.bits_available == 11

    bits.seek(1, os.SEEK_CUR)
    bits.skip_flush()
    assert bits.tell() == 8
    assert bits.bits_available == 3

    bits.skip_flush()
    assert bits.tell() == 8
    assert bits.bits_available == 3

    bits = BitStream("11110000 11100011 101")
    bits.skip_flush()
    assert bits.tell() == 0
    assert bits.bits_available == 19

    bits.seek(1, os.SEEK_CUR)
    bits.cursor += 1
    bits.skip_flush()
    assert bits.tell() == 8
    assert bits.bits_available == 11

    bits.skip_flush()
    assert bits.tell() == 8
    assert bits.bits_available == 11

    bits.seek(1, os.SEEK_CUR)
    bits.skip_flush()
    assert bits.tell() == 16
    assert bits.bits_available == 3
Ejemplo n.º 39
0
    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])
Ejemplo n.º 40
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()
Ejemplo n.º 41
0
def test_sequence_to_IFormat():
    bits = BitStream()

    bits.write([True, False, True])
    assert str(bits) == "101"
Ejemplo n.º 42
0
    def serialize_data(self):
        bits = BitStream()
        bits.write(self.characterid, UI16)
        bits.write(self.mc.num_frames, UI16)

        return bits.serialize + self.mc.serialize()
Ejemplo n.º 43
0
 def as_bitstream(self):
     bits = BitStream()
     bits.write(self.TYPE, UI16)
     bits.write(Struct.as_bitstream(self))
Ejemplo n.º 44
0
    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()
Ejemplo n.º 45
0
    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])
Ejemplo n.º 46
0
def test_flush():
    # Test when bits has no data.
    bits = BitStream("")
    bits.flush()
    assert str(bits) == ""
    assert bits.bits_available == 0

    # Test when cursor == 0
    bits = BitStream("1111")
    bits.seek(0)
    bits.flush()
    assert str(bits) == "11110000"
    assert bits.bits_available == 0

    # Test when cursor != 0.
    bits = BitStream("11100")
    bits.seek(0, os.SEEK_END)
    bits.flush()
    assert str(bits) == "11100000"
    assert bits.bits_available == 0

    # Test when already flush.
    bits = BitStream("11111111")
    bits.seek(0)
    bits.flush()
    assert str(bits) == "11111111"
    assert bits.bits_available == 0

    bits = BitStream("11111111 11111")
    bits.seek(0, os.SEEK_END)
    bits.flush()
    assert str(bits) == "1111111111111000"
    assert bits.bits_available == 0
Ejemplo n.º 47
0
def test_cursor():
    bits = BitStream("01001101")
    assert bits.tell() == 0
    bits.seek(1, os.SEEK_END)
    assert bits.bits_available == 1
    assert bits.read_bit() == 1
    py.test.raises(IndexError, bits.read_bit)

    bits.seek(0)
    assert bits.bits_available == 8

    result = bits.read_bit()
    assert result == 0

    result = bits.read_bits(2)
    assert result == [True, False]

    bits.seek(1, os.SEEK_CUR)
    assert bits.bits_available == 4
    assert bits.read_bits(2) == [True, True]

    bits.seek(1, os.SEEK_END)
    assert bits.bits_available == 0

    bits.seek(0)
    result = bits.read_bits(8)
    assert str(result) == str(bits)
Ejemplo n.º 48
0
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
Ejemplo n.º 49
0
from fusion.bitstream.bitstream import BitStream

from fusion.swf.records import Rect

from fusion.bitstream.formats import SB, FB, Bit
from fusion.bitstream.structs import Struct, Fields, NBits
from fusion.bitstream.structs import Field, Local

from zope.interface import implementedBy

# rect_data = "01110" + "%s"*4 % tuple(("0"*(15-len(s))+s for s in (bin(s*20)[2:] for s in (20, 80, 600, 800))))
rect_data = BitStream(
    "01111000000110010000010111011100000000011001000000011111010000000")


class TestRect(Struct):
    def __init__(self, XMin=0, YMin=0, XMax=0, YMax=0):
        super(TestRect, self).__init__(locals())

    def create_fields(self):
        yield NBits[5]
        yield Fields("XMin XMax YMin YMax", SB[NBits]) * 20


def test_rect_write():
    rect = TestRect(20, 80, 600, 800)
    assert rect.as_bitstream() == rect_data


def test_rect_read():
    rect = TestRect.from_bitstream(rect_data)
Ejemplo n.º 50
0
def test_FloatValue_read():
    # Testing 16-bit float values.
    # Note that these are Flash's FLOAT16, with a different exponent
    # bias than the extended IEEE 754 spec.
    bits = BitStream("0100000000000000")
    result = bits.read(FloatFormat[16])
    assert result == 1
    assert bits.bits_available == 0

    bits = BitStream("0111110000000000")
    result = bits.read(FloatFormat[16])
    assert result == float("inf")
    assert bits.bits_available == 0

    bits = BitStream("1111110000000000")
    result = bits.read(FloatFormat[16])
    assert result == float("-inf")
    assert bits.bits_available == 0

    # Testing 32-bit float values.
    bits = BitStream("0 01111100 01000000000000000000000")
    result = bits.read(FloatFormat[32])
    assert result == 0.15625
    assert bits.bits_available == 0

    bits = BitStream("0 10000011 10010000000000000000000")
    result = bits.read(FloatFormat[32])
    assert result == 25
    assert bits.bits_available == 0

    bits = BitStream("0 11111111 00000000000000000000000")
    result = bits.read(FloatFormat[32])
    assert result == float("inf")
    assert bits.bits_available == 0

    bits = BitStream("1 11111111 00000000000000000000000")
    result = bits.read(FloatFormat[32])
    assert result == float("-inf")
    assert bits.bits_available == 0

    # Testing 32-bit float values.
    bits = BitStream(
        "0011111111110000000000000000000000000000000000000000000000000000")
    result = bits.read(FloatFormat[64])
    assert result == 1
    assert bits.bits_available == 0

    bits = BitStream(
        "0111111111110000000000000000000000000000000000000000000000000000")
    result = bits.read(FloatFormat[64])
    assert result == float("inf")
    assert bits.bits_available == 0

    bits = BitStream(
        "1111111111110000000000000000000000000000000000000000000000000000")
    result = bits.read(FloatFormat[64])
    assert result == float("-inf")
    assert bits.bits_available == 0
Ejemplo n.º 51
0
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"
Ejemplo n.º 52
0
def test_FormatArray_read():
    bits = BitStream("010101")

    result = bits.read(Bit[:][6])
    assert result == [False, True] * 3
Ejemplo n.º 53
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)
Ejemplo n.º 54
0
    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()
Ejemplo n.º 55
0
def test_FormatArray_write():
    bits = BitStream()

    bits.write([True, False]*3, Bit[:][6])
    assert str(bits) == "101010"
Ejemplo n.º 56
0
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
Ejemplo n.º 57
0
 def as_bitstream(self):
     bits = BitStream()
     bits.write(self.TYPE, UI16)
     bits.write(Struct.as_bitstream(self))
Ejemplo n.º 58
0
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