1
# Copyright (C) 2006-2011 Canonical Ltd
2
# Written by Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Legacy breezy specific gzip tunings."""
23
__all__ = ["chunks_to_gzip"]
27
"""Return i as an unsigned integer, assuming it fits in 32 bits.
29
If it's >= 2GB when viewed as a 32-bit unsigned int, return a long.
37
"""Return the low-order 32 bits of an int, as a non-negative int."""
41
def chunks_to_gzip(chunks, factory=zlib.compressobj,
42
level=zlib.Z_DEFAULT_COMPRESSION, method=zlib.DEFLATED,
43
width=-zlib.MAX_WBITS, mem=zlib.DEF_MEM_LEVEL,
45
"""Create a gzip file containing chunks and return its content.
47
:param chunks: An iterable of strings. Each string can have arbitrary
51
b'\037\213' # self.fileobj.write('\037\213') # magic header
52
b'\010' # self.fileobj.write('\010') # compression method
53
# fname = self.filename[:-3]
57
b'\x00' # self.fileobj.write(chr(flags))
58
b'\0\0\0\0' # write32u(self.fileobj, long(time.time()))
59
b'\002' # self.fileobj.write('\002')
60
b'\377' # self.fileobj.write('\377')
62
b'' # self.fileobj.write(fname + '\000')
64
# using a compressobj avoids a small header and trailer that the compress()
65
# utility function adds.
66
compress = factory(level, method, width, mem, 0)
70
crc = crc32(chunk, crc)
71
total_len += len(chunk)
72
zbytes = compress.compress(chunk)
75
result.append(compress.flush())
76
# size may exceed 2GB, or even 4GB
77
result.append(struct.pack("<LL", LOWU32(crc), LOWU32(total_len)))