~ubuntu-branches/ubuntu/saucy/python2.7/saucy-proposed

« back to all changes in this revision

Viewing changes to Lib/gzip.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-05-15 19:15:16 UTC
  • mto: (36.1.23 sid)
  • mto: This revision was merged to the branch mainline in revision 87.
  • Revision ID: package-import@ubuntu.com-20130515191516-zmv6to904wemey7s
Tags: upstream-2.7.5
ImportĀ upstreamĀ versionĀ 2.7.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    # or unsigned.
22
22
    output.write(struct.pack("<L", value))
23
23
 
 
24
def read32(input):
 
25
    return struct.unpack("<I", input.read(4))[0]
 
26
 
24
27
def open(filename, mode="rb", compresslevel=9):
25
28
    """Shorthand for GzipFile(filename, mode, compresslevel).
26
29
 
181
184
        self.crc = zlib.crc32("") & 0xffffffffL
182
185
        self.size = 0
183
186
 
184
 
    def _read_exact(self, n):
185
 
        data = self.fileobj.read(n)
186
 
        while len(data) < n:
187
 
            b = self.fileobj.read(n - len(data))
188
 
            if not b:
189
 
                raise EOFError("Compressed file ended before the "
190
 
                               "end-of-stream marker was reached")
191
 
            data += b
192
 
        return data
193
 
 
194
187
    def _read_gzip_header(self):
195
188
        magic = self.fileobj.read(2)
196
189
        if magic != '\037\213':
197
190
            raise IOError, 'Not a gzipped file'
198
 
 
199
 
        method, flag, self.mtime = struct.unpack("<BBIxx", self._read_exact(8))
 
191
        method = ord( self.fileobj.read(1) )
200
192
        if method != 8:
201
193
            raise IOError, 'Unknown compression method'
 
194
        flag = ord( self.fileobj.read(1) )
 
195
        self.mtime = read32(self.fileobj)
 
196
        # extraflag = self.fileobj.read(1)
 
197
        # os = self.fileobj.read(1)
 
198
        self.fileobj.read(2)
202
199
 
203
200
        if flag & FEXTRA:
204
201
            # Read & discard the extra field, if present
205
 
            self._read_exact(struct.unpack("<H", self._read_exact(2)))
 
202
            xlen = ord(self.fileobj.read(1))
 
203
            xlen = xlen + 256*ord(self.fileobj.read(1))
 
204
            self.fileobj.read(xlen)
206
205
        if flag & FNAME:
207
206
            # Read and discard a null-terminated string containing the filename
208
207
            while True:
216
215
                if not s or s=='\000':
217
216
                    break
218
217
        if flag & FHCRC:
219
 
            self._read_exact(2)     # Read & discard the 16-bit header CRC
 
218
            self.fileobj.read(2)     # Read & discard the 16-bit header CRC
220
219
 
221
220
    def write(self,data):
222
221
        self._check_closed()
250
249
 
251
250
        readsize = 1024
252
251
        if size < 0:        # get the whole thing
253
 
            while self._read(readsize):
254
 
                readsize = min(self.max_read_chunk, readsize * 2)
255
 
            size = self.extrasize
 
252
            try:
 
253
                while True:
 
254
                    self._read(readsize)
 
255
                    readsize = min(self.max_read_chunk, readsize * 2)
 
256
            except EOFError:
 
257
                size = self.extrasize
256
258
        else:               # just get some more of it
257
 
            while size > self.extrasize:
258
 
                if not self._read(readsize):
259
 
                    if size > self.extrasize:
260
 
                        size = self.extrasize
261
 
                    break
262
 
                readsize = min(self.max_read_chunk, readsize * 2)
 
259
            try:
 
260
                while size > self.extrasize:
 
261
                    self._read(readsize)
 
262
                    readsize = min(self.max_read_chunk, readsize * 2)
 
263
            except EOFError:
 
264
                if size > self.extrasize:
 
265
                    size = self.extrasize
263
266
 
264
267
        offset = self.offset - self.extrastart
265
268
        chunk = self.extrabuf[offset: offset + size]
274
277
 
275
278
    def _read(self, size=1024):
276
279
        if self.fileobj is None:
277
 
            return False
 
280
            raise EOFError, "Reached EOF"
278
281
 
279
282
        if self._new_member:
280
283
            # If the _new_member flag is set, we have to
285
288
            pos = self.fileobj.tell()   # Save current position
286
289
            self.fileobj.seek(0, 2)     # Seek to end of file
287
290
            if pos == self.fileobj.tell():
288
 
                return False
 
291
                raise EOFError, "Reached EOF"
289
292
            else:
290
293
                self.fileobj.seek( pos ) # Return to original position
291
294
 
302
305
 
303
306
        if buf == "":
304
307
            uncompress = self.decompress.flush()
305
 
            self.fileobj.seek(-len(self.decompress.unused_data), 1)
306
308
            self._read_eof()
307
309
            self._add_read_data( uncompress )
308
 
            return False
 
310
            raise EOFError, 'Reached EOF'
309
311
 
310
312
        uncompress = self.decompress.decompress(buf)
311
313
        self._add_read_data( uncompress )
315
317
            # so seek back to the start of the unused data, finish up
316
318
            # this member, and read a new gzip header.
317
319
            # (The number of bytes to seek back is the length of the unused
318
 
            # data)
319
 
            self.fileobj.seek(-len(self.decompress.unused_data), 1)
 
320
            # data, minus 8 because _read_eof() will rewind a further 8 bytes)
 
321
            self.fileobj.seek( -len(self.decompress.unused_data)+8, 1)
320
322
 
321
323
            # Check the CRC and file size, and set the flag so we read
322
324
            # a new member on the next call
323
325
            self._read_eof()
324
326
            self._new_member = True
325
 
        return True
326
327
 
327
328
    def _add_read_data(self, data):
328
329
        self.crc = zlib.crc32(data, self.crc) & 0xffffffffL
333
334
        self.size = self.size + len(data)
334
335
 
335
336
    def _read_eof(self):
336
 
        # We've read to the end of the file.
 
337
        # We've read to the end of the file, so we have to rewind in order
 
338
        # to reread the 8 bytes containing the CRC and the file size.
337
339
        # We check the that the computed CRC and size of the
338
340
        # uncompressed data matches the stored values.  Note that the size
339
341
        # stored is the true file size mod 2**32.
340
 
        crc32, isize = struct.unpack("<II", self._read_exact(8))
 
342
        self.fileobj.seek(-8, 1)
 
343
        crc32 = read32(self.fileobj)
 
344
        isize = read32(self.fileobj)  # may exceed 2GB
341
345
        if crc32 != self.crc:
342
346
            raise IOError("CRC check failed %s != %s" % (hex(crc32),
343
347
                                                         hex(self.crc)))