
    q&flF                        d dl Z d dlZd dlZd dlZd dlZddlmZ ej                  ZdZdZ	dZ
dZdZd	Zd
ZdZdZdZdZdZdZdZeefZeZd ZeZdZdZd Zd Z  G d d      Z!d#dZ" G d d      Z# G d d      Z$d Z%d Z&d  Z'd$d!Z(d$d"Z)y)%    N   )compat                               	   
         c                 6    t        |       j                  d      S )Nascii)strencode)is    C/usr/lib/python3/dist-packages/hgext/fsmonitor/pywatchman/pybser.pytobytesr   :   s    q6==!!    s        s            c                     d| cxk  rdk  ry d| cxk  rdk  ry d| cxk  rdk  ry	 t        d
      | cxk  rt        d      k  ry t        dt        |       z         )z5Return the smallest size int that can store the valuei   r   i i     i   i   l         l       zCannot represent value: )longRuntimeErrorr   )xs    r   	_int_sizer#   G   sz    T 	A		 
 		'Z	' 
(	!	"a	C40B+C	C 
D 5A>??r   c                 H    | |   }t        |t              st        |f      }|S N)
isinstancebytes)bufposrets      r   _buf_posr+   U   s%    
c(Cc5!SFmJr   c                   *    e Zd Zd Zd Zd Zd Zd Zy)_bser_bufferc                    || _         t        j                  d      | _        | j                   dk(  rTt	        j
                  t        t        t                    dz   | j                  dt               t        t              | _	        y | j                   dk(  sJ t	        j
                  t        t        t                    dz   | j                  dt               t        t              | _	        y )Ni    r      sr   r   )bser_versionctypescreate_string_bufferr(   struct	pack_intor   lenEMPTY_HEADERwposEMPTY_HEADER_V2)selfversions     r   __init__z_bser_buffer.__init__^   s    #..t4!L)*T1488Q L)DI$$)))O,-4	 O,DIr   c                 D   t        j                  | j                        | j                  z
  |k  rqt        j                  | j                  t        j                  | j                        dz         t        j                  | j                        | j                  z
  |k  rpy y )Nr   )r1   sizeofr(   r7   resize)r9   sizes     r   ensure_sizez_bser_buffer.ensure_sizep   sd    mmDHH%		1D8MM$((FMM$(($;a$?@ mmDHH%		1D8r   c                 >   t        |      }|dz   }| j                  |       |dk(  r2t        j                  d| j                  | j
                  t        |       n|dk(  r2t        j                  d| j                  | j
                  t        |       ny|dk(  r2t        j                  d| j                  | j
                  t        |       nB|dk(  r2t        j                  d| j                  | j
                  t        |       nt        d	      | xj
                  |z  c_        y )
Nr   s   =cbr   s   =chr   s   =cir   s   =cqz Cannot represent this long value)r#   r@   r3   r4   r(   r7   	BSER_INT8
BSER_INT16
BSER_INT32
BSER_INT64r!   )r9   valr?   to_writes       r   append_longz_bser_buffer.append_longt   s    ~!8"19VTXXtyy)SIQYVTXXtyy*cJQYVTXXtyy*cJQYVTXXtyy*cJABB		X	r   c           	      D   t        |t              r|j                  d      }t        |      }t	        |      }d|z   |z   }| j                  |       |dk(  rGt        j                  dt        |      z   dz   | j                  | j                  t        t        ||       n|dk(  rGt        j                  dt        |      z   dz   | j                  | j                  t        t        ||       n|dk(  rGt        j                  dt        |      z   dz   | j                  | j                  t        t        ||       nW|d	k(  rGt        j                  d
t        |      z   dz   | j                  | j                  t        t        ||       nt!        d      | xj                  |z  c_
        y )Nutf-8r   r      =ccbr/      =cchr      =ccir      =ccqz"Cannot represent this string value)r&   unicoder   r5   r#   r@   r3   r4   r   r(   r7   BSER_BYTESTRINGrB   rC   rD   rE   r!   )r9   ss_lenr?   rG   s        r   append_stringz_bser_buffer.append_string   sk   a!!AAt8e#"19'%.(4/		 QY'%.(4/		 QY'%.(4/		 QY'%.(4/		 CDD		X	r   c                 	   t        |t              rdd}| j                  |       |rt        }nt        }t        j                  d| j                  | j                  |       | xj                  |z  c_        y |Yd}| j                  |       t        j                  d| j                  | j                  t               | xj                  |z  c_        y t        |t        t        f      r| j                  |       y t        |t              r| j                  |       y t        |t              rZd}| j                  |       t        j                  d| j                  | j                  t         |       | xj                  |z  c_        y t        |t"        j$                        rt        |t"        j&                        ryt)        |      }t+        |      }d|z   }| j                  |       |dk(  r7t        j                  d| j                  | j                  t,        t.        |       n|dk(  r7t        j                  d| j                  | j                  t,        t0        |       n|dk(  r7t        j                  d	| j                  | j                  t,        t2        |       nG|d
k(  r7t        j                  d| j                  | j                  t,        t4        |       nt7        d      | xj                  |z  c_        |j9                         }|D ]'  \  }}| j                  |       | j;                  |       ) y t        |t"        j<                        rpt        |t"        j&                        rUt)        |      }t+        |      }d|z   }| j                  |       |dk(  r7t        j                  d| j                  | j                  t>        t.        |       n|dk(  r7t        j                  d| j                  | j                  t>        t0        |       n|dk(  r7t        j                  d	| j                  | j                  t>        t2        |       nG|d
k(  r7t        j                  d| j                  | j                  t>        t4        |       nt7        d      | xj                  |z  c_        |D ]  }| j;                  |        y t7        d      )Nr   s   =c	   s   =cdr   rK   rL   r   rM   r   rN   z#Cannot represent this mapping valuez$Cannot represent this sequence valuez#Cannot represent unknown value type) r&   boolr@   	BSER_TRUE
BSER_FALSEr3   r4   r(   r7   	BSER_NULLintr    rH   STRING_TYPESrS   float	BSER_REALabcMappingSizedr5   r#   BSER_OBJECTrB   rC   rD   rE   r!   itemsappend_recursiveIterable
BSER_ARRAY)	r9   rF   needed	to_encodeval_lenr?   	iteritemskvs	            r   rc   z_bser_buffer.append_recursive   s   c4 FV$%	&	UDHHdiiCIII[FV$UDHHdiiCIIIc4[)S!\*s#U#FV$VTXXtyy)SIIIIS[[)jcii.H#hGW%DXFV$qy  HHII   HHII   HHII   HHII ##HIIIII		I! )1""1%%%a() S\\*z#syy/I#hGW%DXFV$qy  TXXtyy*i   HHII   HHII   HHII ##IJJIII )%%a() DEEr   N)__name__
__module____qualname__r;   r@   rH   rS   rc    r   r   r-   r-   ]   s    -$A 1frFr   r-   c                    t        |      }|j                  |        |dk(  r?|j                  t        t              z
  }t        j                  d|j                  d|       n`|j                  t        t              z
  }t        j                  d|j                  d|       t        j                  d|j                  d|       |j                  j                  d |j                   S )N)r:   r      =i   r      )
r-   rc   r7   r5   r6   r3   r4   r(   r8   raw)objr:   capabilitiesbser_bufobj_lens        r   dumpsry   ,  s    G,Hc"!|--#l"33a9--#o"66a>a9<<Ohmm,,r   c                   (    e Zd ZdZd Zd Zd Zd Zy)_BunserDict_keys_valuesc                      || _         || _        y r%   r|   )r9   keysvaluess      r   r;   z_BunserDict.__init__@  s    
r   c                 $    | j                  |      S r%   )__getitem__)r9   names     r   __getattr__z_BunserDict.__getattr__D  s    %%r   c                     t        |t        t        f      r| j                  |   S |j	                  d      r|dd  }	 | j                  | j
                  j                  |         S # t        $ r t        d|z        w xY w)Nst_rr   z_BunserDict has no key %s)	r&   rZ   r    r~   
startswithr}   index
ValueErrorKeyError)r9   keys     r   r   z_BunserDict.__getitem__G  s{    cC;'<<$$^^E" ab'C	><<

 0 0 566 	>6<==	>s   'A% %A=c                 ,    t        | j                        S r%   )r5   r}   )r9   s    r   __len__z_BunserDict.__len__S  s    4::r   N)rl   rm   rn   	__slots__r;   r   r   r   ro   r   r   r{   r{   =  s    $I&
>r   r{   c                   H    e Zd Zd
dZed        Zd Zd Zd Zd Z	d Z
d	 Zy)BunserNc                 V    || _         || _        |d | _        y |d| _        y || _        y )Nstrictmutablevalue_encodingvalue_errors)r9   r   r   r   s       r   r;   zBunser.__init__X  s5    ,! $D! (D ,Dr   c                 j   	 t        | |      }|t        k(  rd}d}n]|t        k(  rd}d}nO|t
        k(  rd}d}nA|t        k(  rd}d	}n3t        d
t        j                  |      j                  d      d|      t        j                  || |dz         d   }|||z   fS # t        $ r t        d      w xY w)Nz+Invalid bser int encoding, pos out of ranger   s   =brr   s   =h   rq   rU   s   =qzInvalid bser int encoding 0xr   z at position r   r   )r+   
IndexErrorr   rB   rC   rD   rE   binasciihexlifydecoder3   unpack_from)r(   r)   int_typerf   fmtint_vals         r   	unser_intzBunser.unser_intc  s    	LS)H y FC#FC#FC#FC##H-44W=sD  $$S#sQw7:v&&)  	LJKK	Ls   B B2c                     | j                  ||dz         \  }}t        j                  t        |      dz   ||      d   }|j	                  d      ||z   fS )Nr   r/   r   rJ   )r   r3   r   r   r   r9   r(   r)   str_lenstr_vals        r   unser_utf8_stringzBunser.unser_utf8_string}  sU    ~~c373$$WW%5%<c3GJw'w77r   c                     | j                  ||dz         \  }}t        j                  t        |      dz   ||      d   }| j                  &|j                  | j                  | j                        }|||z   fS )Nr   r/   r   )r   r3   r   r   r   r   r   r   s        r   unser_bytestringzBunser.unser_bytestring  st    ~~c373$$WW%5%<c3GJ*nnT%8%8$:K:KLGw''r   c                     | j                  ||dz         \  }}g }t        |      D ](  }| j                  ||      \  }}|j                  |       * | j                  st        |      }||fS Nr   )r   rangeloads_recursiveappendr   tuple)r9   r(   r)   arr_lenarr_arr_items          r   unser_arrayzBunser.unser_array  so    ~~c373w 	!A 00c:MHcJJx 	! ||*CCxr   c                 l   | j                  ||dz         \  }}| j                  ri }ng }g }t        |      D ]`  }| j                  ||      \  }}| j	                  ||      \  }	}| j                  r|	|<   ?j                  |       j                  |	       b | j                  st              }|fS r   )r   r   r   r   r   r   r{   )
r9   r(   r)   rx   ru   r   valsr   r   rF   s
             r   unser_objectzBunser.unser_object  s    ~~c373<<CDDw 	!A--c37HC++C5HC||CC C 	! ||dD)CCxr   c                 R   t        ||dz         }|t        k7  rt        d      t        | j                  d      }|j                  ||dz         \  }}| j                  ||      \  }}g }t        |      D ]  }| j                  ri }	ng }
t        t        |            D ]Z  }t        ||      t        k(  r|dz  }d }n| j                  ||      \  }}| j                  r||   }|	|<   J
j                  |       \ | j                  st        |
      }	|j                  	        ||fS )Nr   zExpect ARRAY to follow TEMPLATErJ   )r   r   )r+   re   r!   r   r   r   r   r   r5   	BSER_SKIPr   r   r{   )r9   r(   r)   val_typekeys_bunserr   nitemsr   r   ru   r   keyidxeler   s                 r   unser_templatezBunser.unser_template  s(   Cq)z!@AAT\\'J++Cq9	cnnS#.v 	A||D	* %C%21HCC#33C=HC<<v,C"CHKK$% <<!$-JJsO-	. Cxr   c                    t        ||      }|t        k(  s|t        k(  s|t        k(  s	|t        k(  r| j                  ||      S |t        k(  r$t        j                  d||dz         d   }||dz   fS |t        k(  rd|dz   fS |t        k(  rd|dz   fS |t        k(  rd |dz   fS |t        k(  r| j                  ||      S |t        k(  r| j                  ||      S |t         k(  r| j#                  ||      S |t$        k(  r| j'                  ||      S |t(        k(  r| j+                  ||      S t-        dt/        j0                  |      j3                  d      z        )	Ns   =dr   r   rU   TFzunhandled bser opcode 0x%sr   )r+   rB   rC   rD   rE   r   r]   r3   r   rW   rX   rY   rP   r   BSER_UTF8STRINGr   re   r   ra   r   BSER_TEMPLATEr   r   r   r   r   )r9   r(   r)   r   rF   s        r   r   zBunser.loads_recursive  sj   C%	!:%:%:%>>#s++"$$UCq9!<Cq>!"#'?"#37##"#'?"(((c22())#s33###C--$$$S#..&&&sC00,""8,33G<= r   TNN)rl   rm   rn   r;   staticmethodr   r   r   r   r   r   r   ro   r   r   r   r   W  s:    	- ' '28
(
, D r   r   c                 :   | dd t         dd k(  rd}d}t        j                  | d      \  }}ni| dd t        dd k(  rOt	        |       dk  rt        d      d}t        j                  d| d      d   }t        j                  | d      \  }}nt        d      ||||fS )Nr   r   r   r   zInvalid BSER headerI   )r6   r   r   r8   r5   r   r3   r   )r(   r0   bser_capabilitiesexpected_lenpos2s        r   _pdu_info_helperr     s    
1Qx<!$$#--c15d	Qq_Qq)	)s8a<233"..sC;A>#--c15d.//*L$>>r   c                 @    t        |       }|d   |d   |d   |d   z   fS )Nr   r   r   rr   r   r(   infos     r   pdu_infor     s.    C D7DGT!WtAw...r   c                 .    t        |       }|d   |d   z   S )Nr   rr   r   r   s     r   pdu_lenr     s    C D7T!Wr   c                     t        |       }|d   }|d   }t        |       ||z   k7  rt        d||z   t        |       fz        t        |||      }|j	                  | |      d   S )au  Deserialize a BSER-encoded blob.

    @param buf: The buffer to deserialize.
    @type buf: bytes

    @param mutable: Whether to return mutable results.
    @type mutable: bool

    @param value_encoding: Optional codec to use to decode values. If
                           unspecified or None, return values as bytestrings.
    @type value_encoding: str

    @param value_errors: Optional error handler for codec. 'strict' by default.
                         The other most common argument is 'surrogateescape' on
                         Python 3. If value_encoding is None, this is ignored.
    @type value_errors: str
    r   rr   z!bser data len %d != header len %dr   r   )r   r5   r   r   r   )r(   r   r   r   r   r   r)   bunsers           r   loadsr     s    & C D7L
q'C
3x<#%%/<#3Es3x2PP
 	
 %!F !!#s+A..r   c                 6    ddl m} |j                  | |||      S )Nr   )load) r   )fpr   r   r   r   s        r   r   r   1  s    99R.,??r   )r   r   r   )*r   collectionsr1   r3   sysr   r   r^   re   ra   rP   rB   rC   rD   rE   r]   rW   rX   rY   r   r   r   r   r'   r[   rO   r   rZ   r    r6   r8   r#   r+   r-   ry   r{   r   r   r   r   r   r   ro   r   r   <module>r      s   :     
 oo
	


		
		U|
" 
 /A@LF LF^-" 4W Wt?"/

"/J@r   