Esempio n. 1
0
 def write(self):
     self.size = 14
     chunk = self._format.pack(self.size, self.protocol_version,
                               self.profile_version, self.data_size,
                               self.data_type)
     self.crc.value = compute_crc(chunk)
     return chunk + self.crc.write()
Esempio n. 2
0
 def write(self):
     self.size = 14
     chunk = self._format.pack(self.size, self.protocol_version,
                               self.profile_version, self.data_size,
                               self.data_type)
     self.crc.value = compute_crc(chunk)
     return chunk + self.crc.write()
Esempio n. 3
0
    def read(self, chunk):
        (self.size, self.protocol_version, self.profile_version,
         self.data_size, self.data_type) = self._format.unpack(chunk[:12])

        if len(chunk) == 14:
            self.crc.read(chunk[12:])

            if self.crc.value and not self.crc.check(chunk[:12]):
                raise BodyFormatError(
                    "Invalid CRC %x, should be %x" %
                    (compute_crc(chunk[:12]), self.crc.value))
Esempio n. 4
0
    def read(self, chunk):
        (
            self.size, self.protocol_version, self.profile_version,
            self.data_size, self.data_type
        ) = self._format.unpack(chunk[:12])

        if len(chunk) == 14:
            self.crc.read(chunk[12:])

            if self.crc.value and not self.crc.check(chunk[:12]):
                raise BodyFormatError("Invalid CRC %x, should be %x" % (
                    compute_crc(chunk[:12]), self.crc.value))
Esempio n. 5
0
    def body(self):
        if not self._body:
            self._fd.seek(self.header.size)

            body = self._fd.read(self.header.data_size)
            if len(body) != self.header.data_size:
                raise BodyFormatError(
                    "Can't read %d bytes, read %d bytes instead" % (
                        self.header.data_size, len(body)))

            header = "" if self.header.crc.value else self.header_chunk
            if not self.crc.check(header + body):
                raise BodyFormatError("Invalid CRC %x, should be %x" % (
                    compute_crc(body), self.crc.value))

            self._body.read(body)
        return self._body
Esempio n. 6
0
    def body(self):
        if not self._body:
            self._fd.seek(self.header.size)

            body = self._fd.read(self.header.data_size)
            if len(body) != self.header.data_size:
                raise BodyFormatError(
                    "Can't read %d bytes, read %d bytes instead" %
                    (self.header.data_size, len(body)))

            header = "" if self.header.crc.value else self.header_chunk
            if not self.crc.check(header + body):
                raise BodyFormatError("Invalid CRC %x, should be %x" %
                                      (compute_crc(body), self.crc.value))

            self._body.read(body)
        return self._body
Esempio n. 7
0
 def _prepare(self):
     chunk = self.body.write()
     self.header.data_size = len(chunk)
     self.crc.value = compute_crc(chunk)
     return chunk
Esempio n. 8
0
 def _prepare(self):
     chunk = self.body.write()
     self.header.data_size = len(chunk)
     self.crc.value = compute_crc(chunk)
     return chunk