예제 #1
0
    def _parse_cffile(self, offset):
        """ Parse a CFFILE entry """
        fmt = '<I'      # uncompressed size
        fmt += 'I'      # uncompressed offset of this file in the folder
        fmt += 'H'      # index into the CFFOLDER area
        fmt += 'H'      # date
        fmt += 'H'      # time
        fmt += 'H'      # attribs
        try:
            vals = struct.unpack_from(fmt, self._buf_file, offset)
        except struct.error as e:
            raise CorruptionError(str(e))

        # debugging
        if os.getenv('PYTHON_CABARCHIVE_DEBUG'):
            print "CFFILE", vals

        # parse filename
        offset += struct.calcsize(fmt)
        filename = ''
        for i in range(0, 255):
            filename_c = self._buf_file[offset + i]
            if filename_c == b'\0':
                break
            filename += filename_c

        # add file
        f = CabFile(filename)
        f._date_decode(vals[3])
        f._time_decode(vals[4])
        f._attr_decode(vals[5])
        f.contents = self._folder_data[vals[2]][vals[1]:vals[1] + vals[0]]
        if len(f.contents) != vals[0]:
            raise CorruptionError("Corruption inside archive, %s is size %i but "
                                  "expected size %i" % (filename, len(f.contents), vals[0]))
        self.files.append(f)

        # return offset to next entry
        return 16 + len(filename) + 1