ó 9(Zc@sždZddlZddlZddlZddlmZddlZddlZddl Zej e ƒZ d„Z d„Zd„Zd„Zd„ZdS( sv Components for handling serialization and deserialization of encryption context data in AWS Encryption SDK messages. iÿÿÿÿN(tSerializationErrorcCsXt|tjjƒs$tdƒ‚ndjt|jƒƒ}tj |||j||ƒS(s)Assembles the Body AAD string for a message body structure. :param message_id: Message ID :type message_id: str :param aad_content_string: ContentAADString object for frame type :type aad_content_string: aws_encryption_sdk.identifiers.ContentAADString :param seq_num: Sequence number of frame :type seq_num: int :param length: Content Length :type length: int :returns: Properly formatted AAD bytes for message body structure. :rtype: bytes :raises SerializationError: if aad_content_string is not known sUnknown aad_content_strings >16s{}sIQ( t isinstancetaws_encryption_sdkt identifierstContentAADStringRtformattlentvaluetstructtpack(t message_idtaad_content_stringtseq_numtlengthtfmt((se/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/internal/formatting/encryption_context.pytassemble_content_aadsc CsÑ|s tƒStƒ}t|ƒ}|tjjjkrFtdƒ‚n|jt j d|ƒƒg}xÃ|j ƒD]µ\}}ywt |tƒr¢t j|ƒ}nt |tƒrÃt j|ƒ}n|jtjjj|ƒtjjj|ƒfƒWqrtk r&tdjtjjjƒƒ‚qrXqrWx™t|dd„ƒD]‚\}}|jt j djdt|ƒdt|ƒƒt|ƒ|t|ƒ|ƒƒt|ƒtjjjkrAtd ƒ‚qAqAWt|ƒS( sÐSerializes the contents of a dictionary into a byte string. :param dict encryption_context: Dictionary of encrytion context keys/values. :returns: Serialized encryption context :rtype: bytes s2The encryption context contains too many elements.s>Hs/Cannot encode dictionary key or value using {}.tkeycSs|dS(Ni((tx((se/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/internal/formatting/encryption_context.pytWss>H{key_size}sH{value_size}stkey_sizet value_sizes$The serialized context is too large.(tbytest bytearrayRRtinternaltdefaultstMAX_BYTE_ARRAY_SIZERtextendRR titemsRtcodecstdecodetappendtstr_opstto_bytest ExceptionRtENCODINGtsorted(tencryption_contexttserialized_contextt dict_sizetencryption_context_listRR((se/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/internal/formatting/encryption_context.pytserialize_encryption_context2sB   "       cCsZy3tjd||ƒ\}||tjdƒfSWn tjk rUtdƒ‚nXdS(sReads a number from a byte array. :param bytes source: Source byte string :param int offset: Point in byte string to start reading :returns: Read number and offset at point after read data :rtype: tuple of ints :raises: SerializationError if unable to unpack s>Hs!Bad format of serialized context.N(Rt unpack_fromtcalcsizeterrorR(tsourcetoffsettshort((se/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/internal/formatting/encryption_context.pyt read_shortgs cCsX||}y*tj|||!tjjjƒ|fSWntk rStdƒ‚nXdS(sTReads a string from a byte string. :param bytes source: Source byte string :param int offset: Point in byte string to start reading :param int length: Length of string to read :returns: Read string and offset at point after read data :rtype: tuple of str and int :raises SerializationError: if unable to unpack s!Bad format of serialized context.N(RRRRRR"R!R(R,R-R tend((se/tmp/pip-build-wDUJoH/aws-encryption-sdk/aws_encryption_sdk/internal/formatting/encryption_context.pyt read_stringws   c CsWt|ƒtjjjkr*tdƒ‚n|dkrGtjdƒiSd}i}td|d|ƒ\}}tjd|ƒx±t |ƒD]£}td|d|ƒ\}}t d|d|d|ƒ\}}td|d|ƒ\}}t d|d|d|ƒ\}}||kr$td ƒ‚n|||s        5