
    U                         d Z ddlZddlmZ ddlmZ ddlZddlZddlZddlZddl	Z	ddl
Z
g dZd\  ZZZZZd\  ZZddZd	 Z G d
 de      Z G d de
j.                        ZddZd Zy)zFunctions that read and write gzipped files.

The user of the file doesn't have to worry about the compression,
but random access is not allowed.    N)builtins)range)GzipFileopencompress
decompress)               )r	   r
   c                     t        | ||      S )zShorthand for GzipFile(filename, mode, compresslevel).

    The filename argument is required; mode defaults to 'rb'
    and compresslevel defaults to 9.

    )r   )filenamemodecompresslevels      (lib/third_party/apitools/base/py/gzip.pyr   r   "   s     HdM22    c                 N    | j                  t        j                  d|             y )Nz<L)writestructpack)outputvalues     r   write32ur   ,   s     LLT5)*r   c                   :    e Zd ZdZd	dZd Zd
dZd ZddZd Z	y)_PaddedFilezMinimal read-only file object that prepends a string to the contents
    of an actual file. Shouldn't be used outside of gzip.py, as it lacks
    essential functionality.c                 N    || _         t        |      | _        || _        d| _        y Nr   )_bufferlen_lengthfile_read)selffprepends      r   __init__z_PaddedFile.__init__7   s"    7|	
r   c                    | j                   | j                  j                  |      S | j                   |z   | j                  k  r:| j                   }| xj                   |z  c_         | j                  || j                    S | j                   }d | _         | j                  |d  | j                  j                  || j                  z
  |z         z   S N)r#   r"   readr!   r   )r$   sizer*   s      r   r*   z_PaddedFile.read=   s    ::99>>$''::,::DJJ$J<<TZZ00::DDJ<<&		tdll2T9:; ;r   c                 "   | j                   || _        nZ|r7t        |      | j                   k  r| xj                   t        |      z  c_         y | j                  | j                   d  |z   | _        t        | j                        | _        d| _         y r   )r#   r   r    r!   )r$   r&   readpreviouss      r   r&   z_PaddedFile.prependJ   sl    ::"DLc'ldjj8JJ#g,&J<<

4w>DL4<<(
r   c                 N    | j                   y| j                  | j                   d  S )Nr   )r#   r   r$   s    r   unusedz_PaddedFile.unusedU   s$    ::||DJJK((r   c                 &   |dk(  rc| j                   Wd|| j                   z   cxk  r| j                  k  rn n| xj                   |z  c_         y || j                  | j                   z
  z  }d | _         d | _        | j                  j	                  ||      S )Nr	   r   )r#   r!   r   r"   seek)r$   offsetwhences      r   r2   z_PaddedFile.seekZ   sw    Q;4::1FTZZ'74<<7

f$
$,,33
yy~~ff--r   c                 .    t        | j                  |      S r)   )getattrr"   )r$   names     r   __getattr__z_PaddedFile.__getattr__f   s    tyy$''r   N)r   )r   Fr   )
__name__
__module____qualname____doc__r'   r*   r&   r0   r2   r8    r   r   r   r   2   s%     ;	)

.(r   r   c                       e Zd ZdZdZdZ	 	 ddZed        Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd dZd dZd Zd Zd!dZd Zd Zed        Zd Zej6                  fdZd Zd Zd Zd Z d Z!d"dZ"d dZ#y)#r   a  The GzipFile class simulates most of the methods of a file object with
    the exception of the readinto() and truncate() methods.

    This class only supports opening files in binary mode. If you need to open
    a compressed file in text mode, use the gzip.open() function.

    Ni   c                 T   |r"d|v sd|v rt        dj                  |            |r	d|vr|dz  }|!t        j                  ||xs d      x}| _        |)t        |dd      }t        |t        j                        sd}|t        |d	d      }|j                  d
      rAt        | _        d| _        d| _        d| _        d| _        || _        d| _        t%        |      }n|j                  d      rft&        | _        | j)                  |       t+        j,                  |t*        j.                  t*        j0                   t*        j2                  d      | _        nt        dj                  |            || _        d| _        || _        | j                  t&        k(  r| j=                          yy)az  Constructor for the GzipFile class.

        At least one of fileobj and filename must be given a
        non-trivial value.

        The new class instance is based on fileobj, which can be a regular
        file, an io.BytesIO object, or any other object which simulates a file.
        It defaults to None, in which case filename is opened to provide
        a file object.

        When fileobj is not None, the filename argument is only used to be
        included in the gzip file header, which may includes the original
        filename of the uncompressed file.  It defaults to the filename of
        fileobj, if discernible; otherwise, it defaults to the empty string,
        and in this case the original filename is not included in the header.

        The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb',
        depending on whether the file will be read or written.  The default
        is the mode of fileobj if discernible; otherwise, the default is 'rb'.
        A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and
        'wb', and 'a' and 'ab'.

        The compresslevel argument is an integer from 0 to 9 controlling the
        level of compression; 1 is fastest and produces the least compression,
        and 9 is slowest and produces the most compression. 0 is no compression
        at all. The default is 9.

        The mtime argument is an optional numeric timestamp to be written
        to the stream when compressing.  All gzip compressed streams
        are required to contain a timestamp.  If omitted or None, the
        current time is used.  This module ignores the timestamp when
        decompressing; however, some programs, such as gunzip, make use
        of it.  The format of the timestamp is the same as that of the
        return value of time.time() and of the st_mtime member of the
        object returned by os.stat().

        tUzInvalid mode: {!r}bNrbr7    r   rTr   r   d   )wa)
ValueErrorformatr   r   	myfileobjr6   
isinstancesixstring_types
startswithREADr   _new_memberextrabuf	extrasize
extrastartr7   min_readsizer   WRITE_init_writezlibcompressobjDEFLATED	MAX_WBITSDEF_MEM_LEVELr   fileobjr3   mtime_write_gzip_header)r$   r   r   r   r^   r_   s         r   r'   zGzipFile.__init__v   s   P SD[C4K188>??CtOCKD?'/}}Xt|t'LLGdnw3Hh(8(89<7FD1D??3DI#D  DMDNDO DI #D!'*G__Z(DIX& ,,]-1]].2nn_-1-?-?-.	0DM 188>??
99##% r   c                     dd l }|j                  dt        d       | j                  t        k(  r!| j
                  dd  dk7  r| j
                  dz   S | j
                  S )Nr   zuse the name attributer
   z.gz)warningswarnDeprecationWarningr   rW   r7   )r$   rc   s     r   r   zGzipFile.filename   sM    .0BAF99$))BC.E"999u$$yyr   c                     | j                   }t        |t              r|j                  }t	        |      }d|dd z   dz   t        t        |             z   dz   S )Nz<gzip r	    >)r^   rM   r   r"   reprhexid)r$   r^   ss      r   __repr__zGzipFile.__repr__   sO    ,,g{+llGM!Ab'!C'#bh-7#==r   c                 2    | j                   rt        d      y)zLRaises a ValueError if the underlying file object has been closed.

        zI/O operation on closed file.N)closedrJ   r/   s    r   _check_closedzGzipFile._check_closed   s     ;;<== r   c                 v    || _         t        j                  d      dz  | _        d| _        g | _        d| _        y Nr       r   )r7   rY   crc32crcr+   writebufbufsize)r$   r   s     r   rX   zGzipFile._init_write   s2    	::c?Z/	r   c                 &   | j                   j                  d       | j                   j                  d       	 t        j                  j	                  | j
                        }t        |t        j                        s|j                  d      }|j                  d      r|d d }d}|rt        }| j                   j                  t        j                  |      j                  d             | j                  }|t        j                         }t!        | j                   t#        |             | j                   j                  d       | j                   j                  d	       |r| j                   j                  |d
z          y y # t        $ r d}Y w xY w)N      zlatin-1s   .gzrb   r   r             )r^   r   ospathbasenamer7   rM   rN   binary_typeencodeendswithUnicodeEncodeErrorFNAMEunichrr_   timer   int)r$   fnameflagsr_   s       r   r`   zGzipFile._write_gzip_header   s0   ;'7#		 GG$$TYY/EeS__5Y/~~f%cr
 E3::e,33I>?

=IIKEs5z*7#7#LLuw/  " 	E	s   A*F FFc                 L    t        j                  d      dz  | _        d| _        y rs   )rY   ru   rv   r+   r/   s    r   
_init_readzGzipFile._init_read  s    ::c?Z/	r   c                     | j                   j                  |      }t        |      |k  rH| j                   j                  |t        |      z
        }|st        d      ||z  }t        |      |k  rH|S )NzACompressed file ended before the end-of-stream marker was reached)r^   r*   r    EOFError)r$   ndatarC   s       r   _read_exactzGzipFile._read_exact  sr    ||  #$i!m!!!c$i-0A  B C CAID $i!m r   c                    | j                   j                  d      }|dk(  ry|dk7  rt        d      t        j                  d| j                  d            \  }}| _        |dk7  rt        d      |t        z  r8t        j                  d	| j                  d            \  }| j                  |       |t        z  r%	 | j                   j                  d      }|r|dk(  rn$|t        z  r%	 | j                   j                  d      }|r|dk(  rn$|t        z  r| j                  d       | j                   j                         }|r,| j                  j                  |      }| j                  |       y
)Nr
   r   Frz   zNot a gzipped filez<BBIxxr   zUnknown compression methodz<HTr	   r~   )r^   r*   OSErrorr   unpackr   r_   FEXTRAr   FCOMMENTFHCRCr0   r   _add_read_data)r$   magicmethodflag	extra_lenrm   r0   
uncompresss           r   _read_gzip_headerzGzipFile._read_gzip_header  sN   !!!$C<K.//#)==4;K;KA;N#O djQ;677&=tT-=-=a-@AJIY'%<LL%%a(AL  (?LL%%a(AL  %<Q$$&33F;J
+r   c                 L   | j                          | j                  t        k7  rdd l}t	        |j
                  d      | j                  t        d      t        |t              r|j                         }t        |      dkD  r| j                  j                  | j                  j                  |             | xj                  t        |      z  c_        t        j                   || j"                        dz  | _        | xj$                  t        |      z  c_        t        |      S )Nr   z$write() on read-only GzipFile objectz!write() on closed GzipFile objectrt   )rq   r   rW   errnor   EBADFr^   rJ   rM   
memoryviewtobytesr    r   r   r+   rY   ru   rv   r3   )r$   r   r   s      r   r   zGzipFile.write6  s    99%++'MNN<<@AA dJ'<<>Dt9q=LLt}}55d;<IIT"Izz$1J>DHKK3t9$K4yr   c                    | j                          | j                  t        k7  rdd l}t	        |j
                  d      | j                  dk  r| j                  yd}|dk  rI| j                  |      r+t        | j                  |dz        }| j                  |      r+| j                  }ne|| j                  kD  rV| j                  |      s|| j                  kD  r| j                  }n)t        | j                  |dz        }|| j                  kD  rV| j                  | j                  z
  }| j                  |||z    }| j                  |z
  | _        | xj                  |z  c_        |S )Nr   z$read() on write-only GzipFile objectr      r
   )rq   r   rQ   r   r   r   rT   r^   r#   minmax_read_chunkr3   rU   rS   )r$   r+   r   readsizer3   chunks         r   r*   zGzipFile.readK  s+   99%++'MNN>>Q4<<#7!8**X&t22HqLA **X&>>D'zz(+dnn,#~~t22HqLA ' t.fftm4$.tr   c                 "   | j                          | j                  t        k7  rdd l}t	        |j
                  d      | j                  dk  r| j                  y| j                  dk  r1| j                         r!	 | j                  dk  r| j                         r!|dk  s|| j                  kD  r| j                  }| j                  | j                  z
  }| j                  |||z    }| xj                  |z  c_        | xj                  |z  c_	        |S )Nr   z%read1() on write-only GzipFile objectr   )rq   r   rQ   r   r   r   rT   r^   r#   r3   rU   rS   )r$   r+   r   r3   r   s        r   read1zGzipFile.read1h  s    99%++'NOO>>Q4<<#7 nn!djjl nn!djjl!8tdnn,>>Dt.fftm4$tr   c                    | j                   t        k7  rdd l}t        |j                  d      |dk  rd}| j
                  dk(  rc| j                  y| j
                  dk(  rG| j                  t        |d            r,	 | j
                  dk(  r| j                  t        |d            r,| j                  | j                  z
  }| j
                  }|t        | j                        |z
  k(  sJ | j                  |||z    S )Nr   z$peek() on write-only GzipFile objectrG   r   r   )r   rQ   r   r   r   rT   r^   r#   maxr3   rU   r    rS   )r$   r   r   r3   	remainings        r   peekzGzipFile.peek~  s    99%++'MNN s7A>>Q||# ..A%$**SD\*B ..A%$**SD\*Bt.NN	C.7777}}VFQJ//r   c                 z    t        |      | j                  z   | _        | xj                  t        |      z  c_        y r)   )r    rT   r3   )r$   bufs     r   _unreadzGzipFile._unread  s(    SDNN2s3xr   c                    | j                   y| j                  rQ| j                          | j                         syt	        j
                  t        j                         | _        d| _        | j                   j                  |      }|dk(  rl| j                  j                         }| j                   j                  | j                  j                  d       | j                          | j                  |       y| j                  j                  |      }| j                  |       | j                  j                  dk7  rG| j                   j                  | j                  j                  d       | j                          d| _        y)NFr   T)r^   rR   r   r   rY   decompressobjr\   r   r*   flushr&   unused_data	_read_eofr   )r$   r+   r   r   s       r   r#   zGzipFile._read  s*   << OO))+"00$..ADO$D ll%
 #:..0J LL  !<!<dCNN
+__//4
J'??&&#- LL  !<!<dC NN#Dr   c                 J   t        j                  || j                        dz  | _        | j                  | j                  z
  }| j
                  |d  |z   | _        | j                  t        |      z   | _        | j                  | _        | j                  t        |      z   | _        y Nrt   )	rY   ru   rv   r3   rU   rS   rT   r    r+   )r$   r   r3   s      r   r   zGzipFile._add_read_data  sy    ::dDHH-
:t.fg.5#d)3++IID	)	r   c                    t        j                  d| j                  d            \  }}|| j                  k7  r-t	        dt        |      dt        | j                              || j                  dz  k7  rt	        d      d}|dk(  r!| j                  j                  d      }|dk(  r!|r| j                  j                  |d	       y y )
Nz<IIr   zCRC check failed z != rt   z!Incorrect length of data producedr~   r	   T)
r   r   r   rv   r   rk   r+   r^   r*   r&   )r$   ru   isizecs       r   r   zGzipFile._read_eof  s    
 }}UD,<,<Q,?@uDHHU9<TXXH I Ityy:-.=>>
 7l!!!$A 7lLL  D) r   c                     | j                   d u S r)   r^   r/   s    r   rp   zGzipFile.closed  s    ||t##r   c                    | j                   }|y d | _         	 | j                  t        k(  rX|j                  | j                  j                                t        || j                         t        || j                  dz         | j                  }|rd | _	        |j                          y y # | j                  }|rd | _	        |j                          w w xY wr   )r^   r   rW   r   r   r   r   rv   r+   rL   close)r$   r^   rL   s      r   r   zGzipFile.close  s    ,,?
	"yyE!dmm1134$((+$))j"89I!%!  I!%! s   A+B* *(Cc                     | j                          | j                  t        k(  rO| j                  j	                  | j
                  j                  |             | j                  j                          y y r)   )rq   r   rW   r^   r   r   r   )r$   	zlib_modes     r   r   zGzipFile.flush  sP    99LLt}}229=>LL  r   c                 6    | j                   j                         S )zInvoke the underlying file object's fileno() method.

        This will raise AttributeError if the underlying file object
        doesn't support fileno().
        )r^   filenor/   s    r   r   zGzipFile.fileno  s     ||""$$r   c                     | j                   t        k7  rt        d      | j                  j	                  d       d| _        d| _        d| _        d| _        d| _	        y)z[Return the uncompressed stream file position indicator to the
        beginning of the filezCan't rewind in write moder   Tr   N)
r   rQ   r   r^   r2   rR   rS   rT   rU   r3   r/   s    r   rewindzGzipFile.rewind  sR     99677!r   c                 (    | j                   t        k(  S r)   )r   rQ   r/   s    r   readablezGzipFile.readable  s    yyD  r   c                 (    | j                   t        k(  S r)   )r   rW   r/   s    r   writablezGzipFile.writable  s    yyE!!r   c                      y)NTr>   r/   s    r   seekablezGzipFile.seekable  s    r   c                 x   |r |dk(  r| j                   |z   }nt        d      | j                  t        k(  r|| j                   k  rt	        d      || j                   z
  }t        d      }t        |dz        D ]  }| j                  |        | j                  t        |dz               | j                   S | j                  t        k(  rf|| j                   k  r| j                          || j                   z
  }t        |dz        D ]  }| j                  d        | j                  |dz         | j                   S )Nr	   zSeek from end not supportedzNegative seek in write moder   )r3   rJ   r   rW   r   bytesr   r   rQ   r   r*   )r$   r3   r4   countr   is         r   r2   zGzipFile.seek  s
   {v- !>??99#;<<T[[(E$KE5D=)

5! *JJuUT\*+ {{ YY$#T[[(E5D=)		$ *IIedl#{{r   c                 &   |dk  r| j                   | j                  z
  }| j                  j                  d|      dz   }|dkD  r?| xj                  ||z
  z  c_        | xj                   ||z
  z  c_         | j                  || S t
        j                  }| j                  }n|}g }|dk7  r| j                  |      }|j                  d      }||k  s|dk(  rt        |      |kD  r|dz
  }|dk\  s|dk(  r/|j                  |d |dz           | j                  ||dz   d         n4|j                  |       |t        |      z
  }t        ||dz        }|dk7  r|| j                  kD  rt        || j                  dz  d      | _        dj                  |      S )Nr      
r	   rg   r   r
   i   )r3   rU   rS   findrT   sysmaxsizerV   r*   r    appendr   r   join)r$   r+   r3   r   r   bufsr   s          r   readlinezGzipFile.readline2  s}   !8[[4??2F""5&1A5A1u!f*,q6z)}}VQ//;;D((HHai		(#AuA 	qBw3q6D=1HAvcAfq1uI&Qq1uvY' KKN#a&=D4A.H' ai( d''' #Hd.?.?!.CS IDxx~r   )NN	   NN)rg   )r   r9   )$r:   r;   r<   r=   rL   r   r'   propertyr   rn   rq   rX   r`   r   r   r   r   r*   r   r   r   r#   r   r   rp   r   rY   Z_SYNC_FLUSHr   r   r   r   r   r   r2   r   r>   r   r   r   r   j   s     I%N+/6:T&l  >>04#J*:,0* *X*** $ $"" #// !%
!"2%r   r   c                     t        j                         }t        |d|      5 }|j                  |        ddd       |j	                         S # 1 sw Y   |j	                         S xY w)zCompress data in one shot and return the compressed string.
    Optional argument is the compression level, in range of 0-9.
    wb)r^   r   r   N)ioBytesIOr   r   getvalue)r   r   r   r%   s       r   r   r   Z  sM     **,C	#D	F!	 
G<<> 
G<<>s   AA%c                     t        t        j                  |             5 }|j                         cddd       S # 1 sw Y   yxY w)zYDecompress a gzip compressed string in one shot.
    Return the decompressed string.
    r   N)r   r   r   r*   )r   r%   s     r   r   r   d  s,     
"**T*	+qvvx 
,	+	+s	   :A)rD   r   )r   )r=   rN   	six.movesr   r   r   r   r   r   rY   r   __all__FTEXTr   r   r   r   rQ   rW   r   r   objectr   BufferedIOBaser   r   r   r>   r   r   <module>r      s{   %     
  	  	
8(6 %ufeXe3+5(& 5(pmr   m`r   