î
[á[\«K  ã            $   @   sS  d  Z  d d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ g$ Z d% d& l Z d% d& l Z d% d' l Td% d( l m Z m Z d% Z d) Z d* Z	 d+ Z
 d, Z Gd- d „  d e j ƒ Z d. d/ d& d0 d) d1 d& d2 d& d3 d& d4 d& d5 d& d6 d! „Z e d) d& d& d7 d" „ Z e d& d& d8 d# „ Z d& S)9aS  Interface to the liblzma compression library.

This module provides a class for reading and writing compressed files,
classes for incremental (de)compression, and convenience functions for
one-shot (de)compression.

These classes and functions support both the XZ and legacy LZMA
container formats, as well as raw compressed data streams.
Z
CHECK_NONEZCHECK_CRC32ZCHECK_CRC64ZCHECK_SHA256ZCHECK_ID_MAXZCHECK_UNKNOWNZFILTER_LZMA1ZFILTER_LZMA2ZFILTER_DELTAZ
FILTER_X86ZFILTER_IA64Z
FILTER_ARMZFILTER_ARMTHUMBZFILTER_POWERPCZFILTER_SPARCÚFORMAT_AUTOÚ	FORMAT_XZZFORMAT_ALONEZ
FORMAT_RAWZMF_HC3ZMF_HC4ZMF_BT2ZMF_BT3ZMF_BT4Z	MODE_FASTZMODE_NORMALZPRESET_DEFAULTZPRESET_EXTREMEÚLZMACompressorÚLZMADecompressorÚLZMAFileÚ	LZMAErrorÚopenÚcompressÚ
decompressZis_check_supportedé    N)Ú*)Ú_encode_filter_propertiesÚ_decode_filter_propertiesé   é   é   i    c               @   sW  e  Z d  Z d Z d d d d d d7 d d d d d	 d
 „Z d d „  Z e d d „  ƒ Z d d „  Z d d „  Z	 d d „  Z
 d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d d  „  Z d! d" d# „ Z d! d$ d% „ Z d8 d& d' „ Z d9 d( d) „ Z d: d* d+ „ Z d; d, d- „ Z d. d/ „  Z d0 d1 „  Z d2 d3 d4 „ Z d5 d6 „  Z d S)<r   a@  A file object providing transparent LZMA (de)compression.

    An LZMAFile can act as a wrapper for an existing file object, or
    refer directly to a named file on disk.

    Note that LZMAFile provides a *binary* file interface - data read
    is returned as bytes, and data to be written must be given as bytes.
    NÚrÚformatÚcheckr   ÚpresetÚfiltersc         	   C   sÆ  d |  _  d |  _ t |  _ d |  _ d |  _ | d k rÈ | d k rT t d ƒ ‚ n  | d k	 ro t d ƒ ‚ n  | d k r„ t } n  t } i | d	 6| d
 6|  _	 t
 |  j	   |  _ d |  _ d |  _ nc | d k r| d k ré t } n  t } t d	 | d | d | d
 | ƒ |  _ n t d j | ƒ ƒ ‚ t | t t f ƒ rƒd | k rY| d 7} n  t j | | ƒ |  _  d |  _ | |  _ n? t | d ƒ s¡t | d ƒ r¶| |  _  | |  _ n t d ƒ ‚ d S)aå  Open an LZMA-compressed file in binary mode.

        filename can be either an actual file name (given as a str or
        bytes object), in which case the named file is opened, or it can
        be an existing file object to read from or write to.

        mode can be "r" for reading (default), "w" for (over)writing,
        "x" for creating exclusively, or "a" for appending. These can
        equivalently be given as "rb", "wb", "xb" and "ab" respectively.

        format specifies the container format to use for the file.
        If mode is "r", this defaults to FORMAT_AUTO. Otherwise, the
        default is FORMAT_XZ.

        check specifies the integrity check to use. This argument can
        only be used when opening a file for writing. For FORMAT_XZ,
        the default is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not
        support integrity checks - for these formats, check must be
        omitted, or be CHECK_NONE.

        When opening a file for reading, the *preset* argument is not
        meaningful, and should be omitted. The *filters* argument should
        also be omitted, except when format is FORMAT_RAW (in which case
        it is required).

        When opening a file for writing, the settings used by the
        compressor can be specified either as a preset compression
        level (with the *preset* argument), or in detail as a custom
        filter chain (with the *filters* argument). For FORMAT_XZ and
        FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset
        level. For FORMAT_RAW, the caller must always specify a filter
        chain; the raw compressor does not support preset compression
        levels.

        preset (if provided) should be an integer in the range 0-9,
        optionally OR-ed with the constant PRESET_EXTREME.

        filters (if provided) should be a sequence of dicts. Each dict
        should have an entry for "id" indicating ID of the filter, plus
        additional entries for options to the filter.
        NFr
   r   r   ÚrbzACannot specify an integrity check when opening a file for readingzICannot specify a preset compression level when opening a file for readingr   r   ó    ÚwÚwbÚaÚabÚxÚxbr   r   zInvalid mode: {!r}ÚbTÚreadÚwritez1filename must be a str or bytes object, or a fileéÿÿÿÿ)r   zrbr!   )r   zwbr   r   r   r   )Ú_fpÚ_closefpÚ_MODE_CLOSEDÚ_modeÚ_posÚ_sizeÚ
ValueErrorr   Ú
_MODE_READÚ
_init_argsr   Ú_decompressorÚ_bufferÚ_buffer_offsetr   Ú_MODE_WRITEr   Ú_compressorr   Ú
isinstanceÚstrÚbytesÚbuiltinsr   ÚhasattrÚ	TypeError)ÚselfÚfilenameÚmoder   r   r   r   Z	mode_code© r9   ú/usr/lib/python3.4/lzma.pyÚ__init__1   sD    +										zLZMAFile.__init__c             C   s¹   |  j  t k r d Szb |  j  t t f k r@ d |  _ d |  _ n4 |  j  t k rt |  j j |  j	 j
 ƒ  ƒ d |  _	 n  Wd z |  j r” |  j j ƒ  n  Wd d |  _ d |  _ t |  _  XXd S)z«Flush and close the file.

        May be called more than once without error. Once the file is
        closed, any other operation on it will raise a ValueError.
        Nr   F)r%   r$   r)   Ú_MODE_READ_EOFr+   r,   r.   r"   r    r/   Úflushr#   Úclose)r6   r9   r9   r:   r>   ˆ   s    				zLZMAFile.closec             C   s   |  j  t k S)zTrue if this file is closed.)r%   r$   )r6   r9   r9   r:   Úclosed    s    zLZMAFile.closedc             C   s   |  j  ƒ  |  j j ƒ  S)z3Return the file descriptor for the underlying file.)Ú_check_not_closedr"   Úfileno)r6   r9   r9   r:   rA   ¥   s    
zLZMAFile.filenoc             C   s   |  j  ƒ  o |  j j ƒ  S)z)Return whether the file supports seeking.)Úreadabler"   Úseekable)r6   r9   r9   r:   rC   ª   s    zLZMAFile.seekablec             C   s   |  j  ƒ  |  j t t f k S)z/Return whether the file was opened for reading.)r@   r%   r)   r<   )r6   r9   r9   r:   rB   ®   s    
zLZMAFile.readablec             C   s   |  j  ƒ  |  j t k S)z/Return whether the file was opened for writing.)r@   r%   r.   )r6   r9   r9   r:   Úwritable³   s    
zLZMAFile.writablec             C   s   |  j  r t d ƒ ‚ n  d  S)NzI/O operation on closed file)r?   r(   )r6   r9   r9   r:   r@   º   s    	zLZMAFile._check_not_closedc             C   s5   |  j  t t f k r1 |  j ƒ  t j d ƒ ‚ n  d  S)NzFile not open for reading)r%   r)   r<   r@   ÚioÚUnsupportedOperation)r6   r9   r9   r:   Ú_check_can_read¾   s    
zLZMAFile._check_can_readc             C   s/   |  j  t k r+ |  j ƒ  t j d ƒ ‚ n  d  S)NzFile not open for writing)r%   r.   r@   rE   rF   )r6   r9   r9   r:   Ú_check_can_writeÃ   s    
zLZMAFile._check_can_writec             C   sV   |  j  t t f k r1 |  j ƒ  t j d ƒ ‚ n  |  j j ƒ  sR t j d ƒ ‚ n  d  S)Nz3Seeking is only supported on files open for readingz3The underlying file object does not support seeking)r%   r)   r<   r@   rE   rF   r"   rC   )r6   r9   r9   r:   Ú_check_can_seekÈ   s
    
zLZMAFile._check_can_seekc             C   s  |  j  t k r d Sxú |  j t |  j ƒ k r|  j j pI |  j j t	 ƒ } | s† |  j j
 rw t |  _  |  j |  _ d St d ƒ ‚ n  |  j j
 rî t |  j   |  _ y |  j j | ƒ |  _ Wqt k
 rê t |  _  |  j |  _ d SYqXn |  j j | ƒ |  _ d |  _ q Wd S)NFzACompressed file ended before the end-of-stream marker was reachedr
   T)r%   r<   r-   Úlenr,   r+   Úunused_datar"   r   Ú_BUFFER_SIZEÚeofr&   r'   ÚEOFErrorr   r*   r	   r   )r6   Zrawblockr9   r9   r:   Ú_fill_bufferÒ   s,    		zLZMAFile._fill_bufferTc             C   sŒ   |  j  |  j d  … |  _  d |  _ g  } xJ |  j ƒ  rt | rP | j |  j  ƒ n  |  j t |  j  ƒ 7_ d |  _  q+ W| rˆ d j | ƒ Sd  S)Nr
   r   )r,   r-   rO   Úappendr&   rJ   Újoin)r6   Úreturn_dataÚblocksr9   r9   r:   Ú	_read_allõ   s    	zLZMAFile._read_allc             C   sC  |  j  | } | t |  j ƒ k rd |  j |  j  | … } | |  _  |  j t | ƒ 7_ | r` | Sd  S|  j |  j  d  … |  _ d |  _  g  } x | d k r+|  j ƒ  r+| t |  j ƒ k  rÛ |  j d  | … } | |  _  n |  j } d |  _ | r| j | ƒ n  |  j t | ƒ 7_ | t | ƒ 8} q W| r?d j | ƒ Sd  S)Nr
   r   )r-   rJ   r,   r&   rO   rP   rQ   )r6   ÚnrR   ÚendÚdatarS   r9   r9   r:   Ú_read_block  s*    				zLZMAFile._read_blockc             C   s.   |  j  ƒ  |  j ƒ  s d S|  j |  j d … S)z¿Return buffered data without advancing the file position.

        Always returns at least one byte of data, unless at EOF.
        The exact number of bytes returned is unspecified.
        r   N)rG   rO   r,   r-   )r6   Úsizer9   r9   r:   Úpeek!  s    
zLZMAFile.peekc             C   sA   |  j  ƒ  | d k r d S| d k  r0 |  j ƒ  S|  j | ƒ Sd S)z±Read up to size uncompressed bytes from the file.

        If size is negative or omitted, read until EOF is reached.
        Returns b"" if the file is already at EOF.
        r
   r   N)rG   rT   rX   )r6   rY   r9   r9   r:   r   ,  s    

zLZMAFile.readc             C   sÁ   |  j  ƒ  | d k s; |  j t |  j ƒ k r? |  j ƒ  r? d S| d k r€ |  j |  j |  j | … } |  j t | ƒ 7_ n( |  j |  j d … } d |  _ d |  _ |  j t | ƒ 7_ | S)z¨Read up to size uncompressed bytes, while trying to avoid
        making multiple reads from the underlying stream.

        Returns b"" if the file is at EOF.
        r
   r   N)rG   r-   rJ   r,   rO   r&   )r6   rY   rW   r9   r9   r:   Úread1:  s    	
%		zLZMAFile.read1c             C   sŒ   |  j  ƒ  | d k  ry |  j j d |  j ƒ d } | d k ry |  j |  j | … } | |  _ |  j t | ƒ 7_ | Sn  t j j |  | ƒ S)a  Read a line of uncompressed bytes from the file.

        The terminating newline (if present) is retained. If size is
        non-negative, no more than size bytes will be read (in which
        case the line may be incomplete). Returns b'' if already at EOF.
        r
   s   
r   )	rG   r,   Úfindr-   r&   rJ   rE   ÚBufferedIOBaseÚreadline)r6   rY   rV   Úliner9   r9   r:   r^   T  s    
	zLZMAFile.readlinec             C   sK   |  j  ƒ  |  j j | ƒ } |  j j | ƒ |  j t | ƒ 7_ t | ƒ S)z÷Write a bytes object to the file.

        Returns the number of uncompressed bytes written, which is
        always len(data). Note that due to buffering, the file on disk
        may not reflect the data written until close() is called.
        )rH   r/   r   r"   r    r&   rJ   )r6   rW   Z
compressedr9   r9   r:   r    f  s
    
zLZMAFile.writec             C   sM   |  j  j d d ƒ t |  _ d |  _ t |  j   |  _ d |  _ d |  _	 d  S)Nr
   r   )
r"   Úseekr)   r%   r&   r   r*   r+   r,   r-   )r6   r9   r9   r:   Ú_rewindt  s    			zLZMAFile._rewindr
   c             C   sË   |  j  ƒ  | d k r no | d k r5 |  j | } nS | d k rs |  j d k  rc |  j d d ƒ n  |  j | } n t d j | ƒ ƒ ‚ | |  j k  r¤ |  j ƒ  n | |  j 8} |  j | d d ƒ|  j S)að  Change the file position.

        The new position is specified by offset, relative to the
        position indicated by whence. Possible values for whence are:

            0: start of stream (default): offset must not be negative
            1: current stream position
            2: end of stream; offset must not be positive

        Returns the new file position.

        Note that seeking is emulated, sp depending on the parameters,
        this operation may be extremely slow.
        r
   r   r   rR   FzInvalid value for whence: {})rI   r&   r'   rT   r(   r   ra   rX   )r6   ÚoffsetÚwhencer9   r9   r:   r`   |  s    
zLZMAFile.seekc             C   s   |  j  ƒ  |  j S)z!Return the current file position.)r@   r&   )r6   r9   r9   r:   Útell¥  s    
zLZMAFile.tellr!   r!   r!   r!   r!   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r;   r>   Úpropertyr?   rA   rC   rB   rD   r@   rG   rH   rI   rO   rT   rX   rZ   r   r[   r^   r    ra   r`   rd   r9   r9   r9   r:   r   &   s0   		!V
#)r   r   r   r   r   ÚencodingÚerrorsÚnewlinec            C   så   d | k r1 d | k r‚ t  d | f ƒ ‚ q‚ nQ | d k	 rL t  d ƒ ‚ n  | d k	 rg t  d ƒ ‚ n  | d k	 r‚ t  d ƒ ‚ n  | j d d ƒ }	 t |  |	 d	 | d
 | d | d | ƒ}
 d | k rÝ t j |
 | | | ƒ S|
 Sd S)a   Open an LZMA-compressed file in binary or text mode.

    filename can be either an actual file name (given as a str or bytes
    object), in which case the named file is opened, or it can be an
    existing file object to read from or write to.

    The mode argument can be "r", "rb" (default), "w", "wb", "x", "xb",
    "a", or "ab" for binary mode, or "rt", "wt", "xt", or "at" for text
    mode.

    The format, check, preset and filters arguments specify the
    compression settings, as for LZMACompressor, LZMADecompressor and
    LZMAFile.

    For binary mode, this function is equivalent to the LZMAFile
    constructor: LZMAFile(filename, mode, ...). In this case, the
    encoding, errors and newline arguments must not be provided.

    For text mode, a LZMAFile object is created, and wrapped in an
    io.TextIOWrapper instance with the specified encoding, error
    handling behavior, and line ending(s).

    Útr   zInvalid mode: %rNz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary modeÚ r   r   r   r   )r(   Úreplacer   rE   ÚTextIOWrapper)r7   r8   r   r   r   r   rj   rk   rl   Zlz_modeZbinary_filer9   r9   r:   r   «  s    c             C   s,   t  | | | | ƒ } | j |  ƒ | j ƒ  S)zãCompress a block of data.

    Refer to LZMACompressor's docstring for a description of the
    optional arguments *format*, *check*, *preset* and *filters*.

    For incremental compression, use an LZMACompressor instead.
    )r   r   r=   )rW   r   r   r   r   Úcompr9   r9   r:   r   Ú  s    c             C   s˜   g  } x‚ t  | | | ƒ } y | j |  ƒ } Wn t k
 rO | rH Pn ‚  Yn X| j | ƒ | j su t d ƒ ‚ n  | j }  |  s	 Pq	 q	 d j | ƒ S)záDecompress a block of data.

    Refer to LZMADecompressor's docstring for a description of the
    optional arguments *format*, *check* and *filters*.

    For incremental decompression, use an LZMADecompressor instead.
    zACompressed data ended before the end-of-stream marker was reachedr   )r   r	   r   rP   rM   rK   rQ   )rW   r   Zmemlimitr   ZresultsZdecompZresr9   r9   r:   r	   æ  s     		)rh   Ú__all__r3   rE   Z_lzmar   r   r$   r)   r<   r.   rL   r]   r   r   r   r   r   r	   r9   r9   r9   r:   Ú<module>	   s2   
ÿ †-