/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tuned_gzip.py

First attempt to merge .dev and resolve the conflicts (but tests are 
failing)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
# Written by Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
29
29
# we want a \n preserved, break on \n only splitlines.
30
30
import bzrlib
31
31
 
32
 
__all__ = ["GzipFile"]
 
32
__all__ = ["GzipFile", "bytes_to_gzip"]
 
33
 
 
34
 
 
35
def bytes_to_gzip(bytes, factory=zlib.compressobj,
 
36
    level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
 
37
    width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
 
38
    crc32=zlib.crc32):
 
39
    """Create a gzip file containing bytes and return its content."""
 
40
    result = [
 
41
        '\037\213'  # self.fileobj.write('\037\213')  # magic header
 
42
        '\010'      # self.fileobj.write('\010')      # compression method
 
43
                    # fname = self.filename[:-3]
 
44
                    # flags = 0
 
45
                    # if fname:
 
46
                    #     flags = FNAME
 
47
        '\x00'      # self.fileobj.write(chr(flags))
 
48
        '\0\0\0\0'  # write32u(self.fileobj, long(time.time()))
 
49
        '\002'      # self.fileobj.write('\002')
 
50
        '\377'      # self.fileobj.write('\377')
 
51
                    # if fname:
 
52
        ''          #     self.fileobj.write(fname + '\000')
 
53
        ]
 
54
    # using a compressobj avoids a small header and trailer that the compress()
 
55
    # utility function adds.
 
56
    compress = factory(level, method, width, mem, 0)
 
57
    result.append(compress.compress(bytes))
 
58
    result.append(compress.flush())
 
59
    result.append(struct.pack("<L", LOWU32(crc32(bytes))))
 
60
    # size may exceed 2GB, or even 4GB
 
61
    result.append(struct.pack("<L", LOWU32(len(bytes))))
 
62
    return ''.join(result)
33
63
 
34
64
 
35
65
class GzipFile(gzip.GzipFile):
119
149
 
120
150
        if buf == "":
121
151
            self._add_read_data(self.decompress.flush())
122
 
            assert len(self.decompress.unused_data) >= 8, "what does flush do?"
 
152
            if len(self.decompress.unused_data) < 8:
 
153
                raise AssertionError("what does flush do?")
123
154
            self._gzip_tail = self.decompress.unused_data[0:8]
124
155
            self._read_eof()
125
156
            # tell the driving read() call we have stuffed all the data
145
176
                self._gzip_tail = self.decompress.unused_data[0:8]
146
177
            elif seek_length < 0:
147
178
                # we haven't read enough to check the checksum.
148
 
                assert -8 < seek_length, "too great a seek."
 
179
                if not (-8 < seek_length):
 
180
                    raise AssertionError("too great a seek")
149
181
                buf = self.fileobj.read(-seek_length)
150
182
                self._gzip_tail = self.decompress.unused_data + buf
151
183
            else:
170
202
        # We then check the that the computed CRC and size of the
171
203
        # uncompressed data matches the stored values.  Note that the size
172
204
        # stored is the true file size mod 2**32.
173
 
        assert len(self._gzip_tail) == 8, "gzip trailer is incorrect length."
 
205
        if not (len(self._gzip_tail) == 8):
 
206
            raise AssertionError("gzip trailer is incorrect length.")
174
207
        crc32, isize = struct.unpack("<LL", self._gzip_tail)
175
208
        # note that isize is unsigned - it can exceed 2GB
176
209
        if crc32 != U32(self.crc):