ó
ķrUc           @   sV   d  Z  d d l m Z m Z e d  Z d Z d   Z d   Z d   Z d   Z	 d	 S(
   ss  VARBLOCK file support

The VARBLOCK file format is as follows, where || denotes byte concatenation:

    FILE := VERSION || BLOCK || BLOCK ...

    BLOCK := LENGTH || DATA

    LENGTH := varint-encoded length of the subsequent data. Varint comes from
    Google Protobuf, and encodes an integer into a variable number of bytes.
    Each byte uses the 7 lowest bits to encode the value. The highest bit set
    to 1 indicates the next byte is also part of the varint. The last byte will
    have this bit set to 0.

This file format is called the VARBLOCK format, in line with the varint format
used to denote the block sizes.

i’’’’(   t   bytet   bt    i   c         C   s”   d } d } x t  r |  j d  } t |  d k rY | d k rF d St d |   n  t |  } | | d @d | >7} | d 7} | d @s | | f Sq Wd S(	   sw  Reads a varint from the file.

    When the first byte to be read indicates EOF, (0, 0) is returned. When an
    EOF occurs when at least one byte has been read, an EOFError exception is
    raised.

    @param infile: the file-like object to read from. It should have a read()
        method.
    @returns (varint, length), the read varint and the number of read bytes.
    i    i   s,   EOF while reading varint, value is %i so fari   i   i   N(   i    i    (   t   Truet   readt   lent   EOFErrort   ord(   t   infilet   varintt
   read_bytest   charR    (    (    s-   c:\Python27\lib\site-packages\rsa\varblock.pyt   read_varint*   s    	

c         C   s   | d k r |  j  t  d Sd } xZ | d k r | d @} | d ?} | d k r_ | d O} n  |  j  t |   | d 7} q& W| S(   s©   Writes a varint to a file.

    @param outfile: the file-like object to write to. It should have a write()
        method.
    @returns the number of written bytes.
    i    i   i   i   i   (   t   writet	   ZERO_BYTER    (   t   outfilet   valuet   written_bytest   to_write(    (    s-   c:\Python27\lib\site-packages\rsa\varblock.pyt   write_varintJ   s    

c         c   sß   |  j  d  } t |  d k r0 t d   n  t |  } | t k r[ t d |   n  x} t rŚ t |   \ } } | d k r | d k r Pn  |  j  |  } t |  } | | k rŅ t d | | f   n  | Vq^ Wd S(   sÖ   Generator, yields each block in the input file.

    @param infile: file to read, is expected to have the VARBLOCK format as
        described in the module's docstring.
    @yields the contents of each block.
    i   i    s&   Unable to read VARBLOCK version numbers!   VARBLOCK version %i not supporteds.   Block size is %i, but could read only %i bytesN(   R   R   R   R   t   VARBLOCK_VERSIONt
   ValueErrorR   R   (   R   t
   first_chart   versiont
   block_sizeR
   t   blockt	   read_size(    (    s-   c:\Python27\lib\site-packages\rsa\varblock.pyt   yield_varblocksg   s     		c         c   sQ   xJ t  rL |  j |  } t |  } | d k r4 Pn  | V| | k  r Pq q Wd S(   sÅ   Generator, yields each block of ``blocksize`` bytes in the input file.

    :param infile: file to read and separate in blocks.
    :returns: a generator that yields the contents of each block
    i    N(   R   R   R   (   R   t	   blocksizeR   R
   (    (    s-   c:\Python27\lib\site-packages\rsa\varblock.pyt   yield_fixedblocks   s    	N(
   t   __doc__t   rsa._compatR    R   R   R   R   R   R   R   (    (    (    s-   c:\Python27\lib\site-packages\rsa\varblock.pyt   <module>"   s   	 		"