Browse files

updated code that deals with crc values to be signed on all versions …

…of Python, the bz2 modules is available starting with Python 2.3
  • Loading branch information...
1 parent b7c2536 commit b5a2eec5fec96532c1afd90e520b6d5f27757855 @fancycode committed Apr 26, 2010
Showing with 10 additions and 22 deletions.
  1. +7 −15 py7zlib.py
  2. +3 −7 tests/test_7zfiles.py
View
22 py7zlib.py
@@ -29,10 +29,7 @@
from struct import pack, unpack
from zlib import crc32
import zlib
-try:
- import bz2
-except ImportError:
- bz2 = None
+import bz2
from cStringIO import StringIO
MAGIC_7Z = '7z\xbc\xaf\x27\x1c'
@@ -121,10 +118,7 @@ def _readBoolean(self, file, count, checkall=0):
return result
def checkcrc(self, crc, data):
- check = crc32(data)
- # make crc unsigned
- # XXX: better way to do this?
- check = unpack('<l', pack('<L', check))[0]
+ check = crc32(data) & 0xffffffffL
return crc == check
@@ -224,7 +218,7 @@ class Digests(Base):
def __init__(self, file, count):
self.defined = self._readBoolean(file, count, checkall=1)
- self.crcs = [unpack('<l', file.read(4))[0] for x in xrange(count)]
+ self.crcs = [unpack('<L', file.read(4))[0] for x in xrange(count)]
UnpackDigests = Digests
@@ -458,9 +452,8 @@ def __init__(self, info, start, src_start, size, folder, archive, maxsize=None):
COMPRESSION_METHOD_COPY: '_read_copy',
COMPRESSION_METHOD_LZMA: '_read_lzma',
COMPRESSION_METHOD_MISC_ZIP: '_read_zip',
+ COMPRESSION_METHOD_MISC_BZIP: '_read_bzip',
}
- if bz2 is not None:
- self._decoders[COMPRESSION_METHOD_MISC_BZIP] = '_read_bzip'
def reset(self):
self.pos = 0
@@ -551,15 +544,14 @@ def __init__(self, file):
raise FormatError, 'not a 7z file'
self.version = unpack('BB', file.read(2))
- self.startheadercrc = unpack('<l', file.read(4))[0]
+ self.startheadercrc = unpack('<L', file.read(4))[0]
self.nextheaderofs, data = self._readReal64Bit(file)
crc = crc32(data)
self.nextheadersize, data = self._readReal64Bit(file)
crc = crc32(data, crc)
data = file.read(4)
- self.nextheadercrc = unpack('<l', data)[0]
- crc = crc32(data, crc)
- crc = unpack('<l', pack('<L', crc))[0]
+ self.nextheadercrc = unpack('<L', data)[0]
+ crc = crc32(data, crc) & 0xffffffffL
if crc != self.startheadercrc:
raise FormatError, 'invalid header data'
self.afterheader = file.tell()
View
10 tests/test_7zfiles.py
@@ -23,7 +23,6 @@
# $Id$
#
import os
-import sys
import pylzma
from py7zlib import Archive7z, EncryptedArchiveError
import unittest
@@ -110,12 +109,9 @@ def test_deflate64(self):
# test loading of deflate64 compressed files
self._test_archive('deflate64.7z')
- if sys.version_info[:2] >= (2,4):
- # the bz2 module is available starting with Python 2.4
-
- def test_bzip2(self):
- # test loading of bzip2 compressed files
- self._test_archive('bzip2.7z')
+ def test_bzip2(self):
+ # test loading of bzip2 compressed files
+ self._test_archive('bzip2.7z')
def test_copy(self):
# test loading of copy compressed files

0 comments on commit b5a2eec

Please sign in to comment.