
    Dj
                        d 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m	Z	m
Z
mZ ddlmZmZ ddlmZ  ej        e          ZddZddZ G d d          ZdS )z?Decoding of compressed note data from TextDataEncrypted format.    )annotationsN)ListOptionalUnion   )AttachmentIdNoteBody)	notes_pb2val&Optional[Union[str, bytes, bytearray]]returnOptional[bytes]c                $   | dS t          | t          t          f          r7t                              dt          |                      t          |           S t          | t                    r	 t          j        | d          }t                              dt          |           t          |                     |S # t          j
        $ rB t                              dt          |                      |                     dd	          cY S w xY wdS )
z9Accepts base64 string OR raw bytes and returns raw bytes.Nz notes.decoder.input_bytes len=%dT)validatez*notes.decoder.input_b64 len=%d -> bytes=%dz%notes.decoder.input_str_nonb64 len=%dzutf-8replace)errors)
isinstancebytes	bytearrayLOGGERdebuglenstrbase64	b64decodebinasciiErrorencode)r   outs     [/home/thesage/.hermes/venv/lib/python3.11/site-packages/pyicloud/services/notes/decoding.py_b64_to_bytesr!      s   
{t#y)** 7SBBBSzz#s 
9		9"3666CLL<c#hhC   J~ 	9 	9 	9LL@#c((KKK::gi:88888	9
9 
9s   .AB< <ADDblobr   c                   t          |           dk    r,| d         dk    r | d         dk    rt          j        |           S 	 t          j        |           S # t          j        $ r# t          j        | t          j                   cY S w xY w)N   r      r      )r   gzip
decompresszliberror	MAX_WBITS)r"   s    r    _decompressr,   &   s    
4yyA~~$q'T//d1goot$$$6t$$$: 6 6 6tdn_555556s   A /BBc                      e Zd ZdZddZdS )	BodyDecoderz:Decode TextDataEncrypted (base64, compressed) to NoteBody.text_data_encrypted_b64r   r   Optional[NoteBody]c                   |d S t          |          }|sd S 	 t          |          }n3# t          $ r&}t                              d|           Y d }~d S d }~ww xY w	 t          j                    }|                    |           t          t          |dd           dd           }|rt          |dd           nd }g }|rt                      }	t          |dg           D ]}
t          |
dd           }|rt          |dd           st          |dd           rht          |dd	          pd	}t          |dd           pd }||f}||	v rf|	
                    |           |                    t          ||
                     t                              dt          |          t          |                     t          |||          S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)Nz notes.decoder.decompress_fail %sdocumentnote	note_textattribute_runattachment_infoattachment_identifiertype_uti )
identifierr8   z-notes.decoder.attachments ids=%d note_text=%s)r   textattachment_idsz!notes.decoder.proto_parse_fail %s)r!   r,   	Exceptionr   r   r
   NoteStoreProtoParseFromStringgetattrsetaddappendr   r   boolr	   )selfr/   rawdocemsgr3   r;   idsseenrunaiidentr8   keys                  r    decodezBodyDecoder.decode2   sO    #*4344 	4	c""CC 	 	 	LL;Q???44444		*,,C$$$73
D9964HHD7;E74d333D&(C Vuu"4"== V VC &7>>B 
V$;TBB
V"2z488
V !(,CR H H NB#*2z4#@#@#HD$h/$;;$

<58#T#T#TUUULL?CT

   #DEEEE 	 	 	LL<a@@@44444	s-   ) 
AAAE'G 
G5G00G5N)r/   r   r   r0   )__name__
__module____qualname____doc__rP        r    r.   r.   /   s.        DD- - - - - -rV   r.   )r   r   r   r   )r"   r   r   r   )rT   
__future__r   r   r   r'   loggingr)   typingr   r   r   domainr   r	   protobufr
   	getLoggerrQ   r   r!   r,   r.   rU   rV   r    <module>r]      s    E E " " " " " "      ( ( ( ( ( ( ( ( ( ( * * * * * * * *      		8	$	$9 9 9 9(6 6 6 60 0 0 0 0 0 0 0 0 0rV   