ó
l]›]c           @` sµ   d  Z  d d l m Z m Z m Z m Z d d l Z d d l m Z m	 Z	 m
 Z
 d d l m Z m Z e j d k Z d Z d	 „  Z e d
 „ Z d „  Z d e d „ Z d „  Z d S(   uÈ   
Functions for parsing and dumping using the ASN.1 DER encoding. Exports the
following items:

 - emit()
 - parse()
 - peek()

Other type classes are defined that help compose the types listed above.
i    (   t   unicode_literalst   divisiont   absolute_importt   print_functionNi   (   t   byte_clst   chr_clst	   type_name(   t   int_from_bytest   int_to_bytesi   u<   Insufficient data - %s bytes requested but only %s availablec         C` s,  t  |  t ƒ s( t d t |  ƒ ƒ ‚ n  |  d k  s@ |  d k rS t d |  ƒ ‚ n  t  | t ƒ s{ t d t | ƒ ƒ ‚ n  | d k  s“ | d k r¦ t d | ƒ ‚ n  t  | t ƒ sÎ t d t | ƒ ƒ ‚ n  | d k  rí t d	 | ƒ ‚ n  t  | t ƒ st d
 t | ƒ ƒ ‚ n  t |  | | | ƒ | S(   u™  
    Constructs a byte string of an ASN.1 DER-encoded value

    This is typically not useful. Instead, use one of the standard classes from
    asn1crypto.core, or construct a new class with specific fields, and call the
    .dump() method.

    :param class_:
        An integer ASN.1 class value: 0 (universal), 1 (application),
        2 (context), 3 (private)

    :param method:
        An integer ASN.1 method value: 0 (primitive), 1 (constructed)

    :param tag:
        An integer ASN.1 tag value

    :param contents:
        A byte string of the encoded byte contents

    :return:
        A byte string of the ASN.1 DER value (header and contents)
    u!   class_ must be an integer, not %si    i   u*   class_ must be one of 0, 1, 2 or 3, not %su!   method must be an integer, not %si   u   method must be 0 or 1, not %su   tag must be an integer, not %su%   tag must be greater than zero, not %su&   contents must be a byte string, not %s(   t
   isinstancet   intt	   TypeErrorR   t
   ValueErrorR   t   _dump_header(   t   class_t   methodt   tagt   contents(    (    s0   lib/python2.7/site-packages/asn1crypto/parser.pyt   emit   s    c         C` sv   t  |  t ƒ s( t d t |  ƒ ƒ ‚ n  t |  ƒ } t |  | ƒ \ } } | rr | | k rr t d | | ƒ ‚ n  | S(   ul  
    Parses a byte string of ASN.1 BER/DER-encoded data.

    This is typically not useful. Instead, use one of the standard classes from
    asn1crypto.core, or construct a new class with specific fields, and call the
    .load() class method.

    :param contents:
        A byte string of BER/DER-encoded data

    :param strict:
        A boolean indicating if trailing data should be forbidden - if so, a
        ValueError will be raised when trailing data exists

    :raises:
        ValueError - when the contents do not contain an ASN.1 header or are truncated in some way
        TypeError - when contents is not a byte string

    :return:
        A 6-element tuple:
         - 0: integer class (0 to 3)
         - 1: integer method
         - 2: integer tag
         - 3: byte string header
         - 4: byte string content
         - 5: byte string trailer
    u&   contents must be a byte string, not %su4   Extra data - %d bytes of trailing data were provided(   R	   R   R   R   t   lent   _parseR   (   R   t   strictt   contents_lent   infot   consumed(    (    s0   lib/python2.7/site-packages/asn1crypto/parser.pyt   parseJ   s    c         C` sG   t  |  t ƒ s( t d t |  ƒ ƒ ‚ n  t |  t |  ƒ ƒ \ } } | S(   uW  
    Parses a byte string of ASN.1 BER/DER-encoded data to find the length

    This is typically used to look into an encoded value to see how long the
    next chunk of ASN.1-encoded data is. Primarily it is useful when a
    value is a concatenation of multiple values.

    :param contents:
        A byte string of BER/DER-encoded data

    :raises:
        ValueError - when the contents do not contain an ASN.1 header or are truncated in some way
        TypeError - when contents is not a byte string

    :return:
        An integer with the number of bytes occupied by the ASN.1 value
    u&   contents must be a byte string, not %s(   R	   R   R   R   R   R   (   R   R   R   (    (    s0   lib/python2.7/site-packages/asn1crypto/parser.pyt   peekq   s    c         C` s¸  | | d k  r- t  t d | | f ƒ ‚ n  | } t rI t |  | ƒ n |  | } | d 7} | d @} | d k rß d } xc t rÛ t r˜ t |  | ƒ n |  | } | d 7} | d 9} | | d @7} | d ?d k r| Pq| q| Wn  t rõ t |  | ƒ n |  | } | d 7} | d ?d k rB| r1| | | d @f S| | d @}	 n| d @}
 |
 r| |
 7} | t |  | |
 | !d t ƒ}	 | r]| |	 f SnÍ | }	 xU |	 | k  rít |  | |	 d	 t ƒ\ } }	 |	 | k r™|  |	 d |	 !d
 k r™Pq™q™W| rþ| |	 f S|	 | k r#t  t |	 | f ƒ ‚ n  | d ?| d ?d @| |  | | !|  | |	 d !d
 f |	 f S|	 | k r‚t  t |	 | f ƒ ‚ n  | d ?| d ?d @| |  | | !|  | |	 !d f |	 f S(   u  
    Parses a byte string into component parts

    :param encoded_data:
        A byte string that contains BER-encoded data

    :param data_len:
        The integer length of the encoded data

    :param pointer:
        The index in the byte string to parse from

    :param lengths_only:
        A boolean to cause the call to return a 2-element tuple of the integer
        number of bytes in the header and the integer number of bytes in the
        contents. Internal use only.

    :return:
        A 2-element tuple:
         - 0: A tuple of (class_, method, tag, header, content, trailer)
         - 1: An integer indicating how many bytes were consumed
    i   i   i   i    i€   i   i   t   signedt   lengths_onlys     i   i   t    (   R   t   _INSUFFICIENT_DATA_MESSAGEt   _PY2t   ordt   TrueR   t   FalseR   (   t   encoded_datat   data_lent   pointerR   t   startt   first_octetR   t   numt   length_octett   contents_endt   length_octetst   sub_header_end(    (    s0   lib/python2.7/site-packages/asn1crypto/parser.pyR   ‹   sh     

	 

 


!#



c   	      C` s  d } d } | |  d >O} | | d >O} | d k r• d } xA | d k r} t  | | d @Bƒ | } | sp d } n  | d ?} q= Wt  | d Bƒ | } n | t  | | Bƒ 7} t | ƒ } | d k rÔ | t  | ƒ 7} n0 t | ƒ } | t  d t | ƒ Bƒ 7} | | 7} | S(	   uÇ  
    Constructs the header bytes for an ASN.1 object

    :param class_:
        An integer ASN.1 class value: 0 (universal), 1 (application),
        2 (context), 3 (private)

    :param method:
        An integer ASN.1 method value: 0 (primitive), 1 (constructed)

    :param tag:
        An integer ASN.1 tag value

    :param contents:
        A byte string of the encoded byte contents

    :return:
        A byte string of the ASN.1 DER header
    R   i    i   i   i   i   i€   i   (   R   R   R   (	   R   R   R   R   t   headert   id_numt   cont_bitt   lengtht   length_bytes(    (    s0   lib/python2.7/site-packages/asn1crypto/parser.pyR   ï   s(    	
(   i   (   t   __doc__t
   __future__R    R   R   R   t   syst   _typesR   R   R   t   utilR   R   t   version_infoR   R   R   R"   R   R   R   R   (    (    (    s0   lib/python2.7/site-packages/asn1crypto/parser.pyt   <module>   s   "	1'	d