B s `@sJdZdZddlZddlZddlZdZejr0eZnddl Z e e dZ ddl mZddl mZddl mZdd lmZd Ze ZedZejZd d Zd dZedeZedeZedeZedeZddZddZddZ ddZ!ddZ"ddZ#dKd d!Z$eej%eZ&eej%eZ'eej%eZ(eej%eZ)e ej%eej*Z+e ej%eej*Z,e!ej-d"Z.e!ej/d#Z0e!ej-d$Z1e!ej/d%Z2e"Z3e#Z4e ej%ee5Z6dLd&d'Z7dMd(d)Z8d*d+Z9d,d-Z:e;d.ej<Z=d/d0Z>d1d2Z?d3d4Z@d5d6ZAd7d8ZBd9d:ZCd;d<ZDdNd=d>ZEd?d@ZFdAdBZGdCdDZHdEdFZIdGdHZJdIdJZKeKZLdS)Oa Code for decoding protocol buffer primitives. This code is very similar to encoder.py -- read the docs for that module first. A "decoder" is a function with the signature: Decode(buffer, pos, end, message, field_dict) The arguments are: buffer: The string containing the encoded message. pos: The current position in the string. end: The position in the string where the current message ends. May be less than len(buffer) if we're reading a sub-message. message: The message object into which we're parsing. field_dict: message._fields (avoids a hashtable lookup). The decoder reads the field and stores it into field_dict, returning the new buffer position. A decoder for a repeated field may proactively decode all of the elements of that field, if they appear consecutively. Note that decoders may throw any of the following: IndexError: Indicates a truncated message. struct.error: Unpacking of a fixed-width field failed. message.DecodeError: Other errors. Decoders are expected to raise an exception if they are called with pos > end. This allows callers to be lax about bounds checking: it's fineto read past "end" as long as you are sure that someone else will notice and throw an exception later on. Something up the call stack is expected to catch IndexError and struct.error and convert them to message.DecodeError. Decoders are constructed using decoder constructors with the signature: MakeDecoder(field_number, is_repeated, is_packed, key, new_default) The arguments are: field_number: The field number of the field we want to decode. is_repeated: Is the field a repeated field? (bool) is_packed: Is the field a packed field? (bool) key: The key to use when looking up the field within field_dict. (This is actually the FieldDescriptor but nothing in this file should depend on that.) new_default: A function which takes a message object as a parameter and returns a new instance of the default value for this field. (This is called for repeated fields and sub-messages, when an instance does not already exist.) As with encoders, we define a decoder constructor for every type of field. Then, for every field of every message class we construct an actual decoder. That decoder goes into a dict indexed by tag, so when we decode a message we repeatedly read a tag, look up the corresponding decoder, and invoke it. z kenton@google.com (Kenton Varda)Niz[\ud800-\udfff]) containers)encoder) wire_format)messagegcsfdd}|S)aReturn an encoder for a basic varint value (does not include tag). Decoded values will be bitwise-anded with the given mask before being returned, e.g. to limit them to 32 bits. The returned decoder does not take the usual "end" parameter -- the caller is expected to do bounds checking after the fact (often the caller can defer such checking until later). The decoder returns a (value, new_pos) pair. csnd}d}x`t||}||d@|>O}|d7}|d@sN|M}|}||fS|d7}|dkr tdq WdS)Nr@z$Too many bytes when decoding varint.)six indexbytes _DecodeError)bufferposresultshiftb)mask result_typeD/tmp/pip-unpacked-wheel-u0__x60w/google/protobuf/internal/decoder.py DecodeVarintzs z$_VarintDecoder..DecodeVarintr)rrrr)rrr_VarintDecoderps rcs,d|d>d|>dfdd}|S)z0Like _VarintDecoder() but decodes signed values.rcszd}d}xlt||}||d@|>O}|d7}|d@sZ|M}|A}|}||fS|d7}|dkr tdq WdS)Nrrrrr r z$Too many bytes when decoding varint.)r r r )rrrrr)rrsignbitrrrs  z*_SignedVarintDecoder..DecodeVarintr)bitsrrr)rrrr_SignedVarintDecoders  rlr l cCsB|}xt||d@r |d7}qW|d7}|||}||fS)aRead a tag from the memoryview, and return a (tag_bytes, new_pos) tuple. We return the raw bytes of the tag rather than decoding them. The raw bytes can then be used to look up the proper decoder. This effectively allows us to trade some work that would be done in pure-python (decoding a varint) for work that is done in C (searching for a byte string in a hash table). In a low-level language it would be much cheaper to decode the varint and use that, but not in Python. Args: buffer: memoryview object of the encoded bytes pos: int of the current position to start from Returns: Tuple[bytes, int] of the tag data and new position. rr)r r tobytes)rrstart tag_bytesrrrReadTags  r csdfdd }|S)zReturn a constructor for a decoder for fields of a particular type. Args: wire_type: The field's wire type. decode_value: A function which decodes an individual value, e.g. _DecodeVarint() Fc sf|rtfdd}|S|rNt|tfdd}|Sfdd}|SdS)Ncs|}|dkr"||}||\}}||7}||krHtdx$||krl||\}}||qJW||kr|d=td|S)NzTruncated message.zPacked element was truncated.)get setdefaultr append)rrendr field_dictvalueendpointelement) decode_valuekeylocal_DecodeVarint new_defaultrrDecodePackedFields  zB_SimpleDecoder..SpecificDecoder..DecodePackedFieldcsx|}|dkr"||}xP||\}}|||}|||ks\||kr$||krltd|Sq$WdS)NzTruncated message.)r"r#r$r )rrr%rr&r'r)new_pos)r*r+r-rtag_lenrrDecodeRepeatedFields  zD_SimpleDecoder..SpecificDecoder..DecodeRepeatedFieldcs@||\}}||krtdr4|s4|dn||<|S)NzTruncated message.)r pop)rrr%rr& new_value)clear_if_defaultr*r+rr DecodeFieldsz<_SimpleDecoder..SpecificDecoder..DecodeField) _DecodeVarintrTagByteslen) field_number is_repeated is_packedr+r-r4r.r1r5)r* wire_type)r4r+r,r-rr0rSpecificDecoders  z'_SimpleDecoder..SpecificDecoder)Fr)r<r*r=r)r*r<r_SimpleDecoders 2r>csfdd}t||S)zLike SimpleDecoder but additionally invokes modify_value on every value before storing it. Usually modify_value is ZigZagDecode. cs||\}}||fS)Nr)rrrr/)r* modify_valuerr InnerDecodesz%_ModifiedDecoder..InnerDecode)r>)r<r*r?r@r)r*r?r_ModifiedDecoder srAcs*ttjfdd}t||S)zReturn a constructor for a decoder for a fixed-width field. Args: wire_type: The field's wire type. format: The format string to pass to struct.unpack(). cs&|}|||d}||fS)Nrr)rrr/r)format local_unpack value_sizerrr@*sz'_StructPackDecoder..InnerDecode)structcalcsizeunpackr>)r<rBr@r)rBrCrDr_StructPackDecoders  rHcstjfdd}ttj|S)zReturns a decoder for a float field. This code works around a bug in struct.unpack for non-finite 32-bit floating-point values. cs|d}|||}|dddkrp|dddkrp|dddkrPt|fS|dddkrht|fSt|fSd |d}||fS) a#Decode serialized float to a float and new position. Args: buffer: memoryview of the serialized bytes pos: int, position in the memory view to start at. Returns: Tuple[float, int] of the deserialized float value and new position in the serialized data. srsz.InnerDecode)rErGr>rWIRETYPE_FIXED32)r@r)rCr _FloatDecoder1s !rRcstjfdd}ttj|S)zkReturns a decoder for a double field. This code works around a bug in struct.unpack for not-a-number. csf|d}|||}|dddkrP|dddkrP|dddkrPt|fSd|d}||fS) a"Decode serialized double to a double and new position. Args: buffer: memoryview of the serialized bytes. pos: int, position in the memory view to start at. Returns: Tuple[float, int] of the decoded double value and new position in the serialized data. r srsz.InnerDecode)rErGr>rWIRETYPE_FIXED64)r@r)rCr_DoubleDecoder^s rWFc stj|r&tfdd}|S|rZttjtfdd}|Sfdd}|SdS)z!Returns a decoder for enum field.c s|}|dkr"||}||\}}||7}||krHtdx||kr|}t||\}}|jkrz||qJ|jsg|_tt j } |j| ||| f|j dkrt |_ |j t j |qJW||kr|jkr|d=n|jd=|j jd=td|S)aDecode serialized packed enum to its value and a new position. Args: buffer: memoryview of the serialized bytes. pos: int, position in the memory view to start at. end: int, end position of serialized data message: Message object to store unknown fields in field_dict: Map[Descriptor, Any] to store decoded values in. Returns: int, new position in serialized data. NzTruncated message.r!zPacked element was truncated.)r"r#r _DecodeSignedVarint32values_by_numberr$_unknown_fieldsrr7rWIRETYPE_VARINTr_unknown_field_setrUnknownFieldSet_addZ_values) rrr%rr&r'r(value_start_posr)r) enum_typer9r+r,r-rrr.s:         z&EnumDecoder..DecodePackedFieldcs|}|dkr"||}xt||\}}|jkrH||nN|jsTg|_|j|||f|jdkrt |_|j t j ||}|||ks||kr$||krt d|Sq$WdS)aDecode serialized repeated enum to its value and a new position. Args: buffer: memoryview of the serialized bytes. pos: int, position in the memory view to start at. end: int, end position of serialized data message: Message object to store unknown fields in field_dict: Map[Descriptor, Any] to store decoded values in. Returns: int, new position in serialized data. NzTruncated message.)r"r#rXrYr$rZrr\rr]r^rr[r )rrr%rr&r'r)r/)r`r9r+r-rr0rrr1s(      z(EnumDecoder..DecodeRepeatedFieldcs|}t||\}}||kr"tdr:|s:|d|S|jkrN||<n\|jsZg|_ttj}|j |||| f|j dkrt |_ |j tj||S)aDecode serialized repeated enum to its value and a new position. Args: buffer: memoryview of the serialized bytes. pos: int, position in the memory view to start at. end: int, end position of serialized data message: Message object to store unknown fields in field_dict: Map[Descriptor, Any] to store decoded values in. Returns: int, new position in serialized data. zTruncated message.N)rXr r2rYrZrr7rr[r$rr\rr]r^)rrr%rr&r_Z enum_valuer)r4r`r9r+rrr5s(       z EnumDecoder..DecodeFieldN)r`r6rr7rr[r8) r9r:r;r+r-r4r.r1r5r)r4r`r9r+r,r-rr0r EnumDecoders1'$raz._ConvertToUnicodecs|}|dkr"||}xd||\}}||}||krJtd|||||}|||ks||kr$|Sq$WdS)NzTruncated string.)r"r#r r$)rrr%rr&r'sizer/)ror+r,r-rr0rrr1Rs z*StringDecoder..DecodeRepeatedFieldcsT||\}}||}||kr&tdr<|s<|dn||||<|S)NzTruncated string.)r r2)rrr%rr&rpr/)ror4r+r,rrr5csz"StringDecoder..DecodeFieldN) r6r text_typeAssertionErrorrr7rWIRETYPE_LENGTH_DELIMITEDr8) r9r:r;r+r-rmr4r1r5r) ror4rmr+r,rnr-rr0r StringDecoder1s rtcsVt|r t|r>t|tjtfdd}|Sfdd}|SdS)z$Returns a decoder for a bytes field.cs|}|dkr"||}xd||\}}||}||krJtd|||||}|||ks||kr$|Sq$WdS)NzTruncated string.)r"r#r r$r)rrr%rr&r'rpr/)r+r,r-rr0rrr1{s z)BytesDecoder..DecodeRepeatedFieldcsT||\}}||}||kr&tdr<|s<|dn||||<|S)NzTruncated string.)r r2r)rrr%rr&rpr/)r4r+r,rrr5sz!BytesDecoder..DecodeFieldN)r6rrrr7rrsr8)r9r:r;r+r-r4r1r5r)r4r+r,r-rr0r BytesDecoderps rucslt|tjt|rt|rRt|tjtfdd}|Sfdd}|SdS)z$Returns a decoder for a group field.cs|}|dkr"||}x|}|dkrF||}||||}|}|||ksx||krtd|}|||ks||kr$|Sq$WdS)NzMissing group end tag.)r"r#add_InternalParser )rrr%rr&r'r/) end_tag_bytes end_tag_lenr+r-rr0rrr1s  z)GroupDecoder..DecodeRepeatedFieldcs\|}|dkr"||}||||}|}|||ksP||krXtd|S)NzMissing group end tag.)r"r#rwr )rrr%rr&r'r/)rxryr+r-rrr5s z!GroupDecoder..DecodeFieldN)rr7rWIRETYPE_END_GROUPr8rrWIRETYPE_START_GROUP)r9r:r;r+r-r1r5r)rxryr+r-rr0r GroupDecoders r|csVt|r t|r>t|tjtfdd}|Sfdd}|SdS)z&Returns a decoder for a message field.cs|}|dkr"||}xl||\}}||}||krJtd|||||krhtd|}|||ks||kr$|Sq$WdS)NzTruncated message.zUnexpected end-group tag.)r"r#r rvrw)rrr%rr&r'rpr/)r+r,r-rr0rrr1s z+MessageDecoder..DecodeRepeatedFieldcsf|}|dkr"||}||\}}||}||krHtd|||||krbtd|S)NzTruncated message.zUnexpected end-group tag.)r"r#r rw)rrr%rr&r'rpr/)r+r,r-rrr5s z#MessageDecoder..DecodeFieldN)r6rrrr7rrsr8)r9r:r;r+r-r1r5r)r+r,r-rr0rMessageDecodersr}rcsNtdtjtdtjtdtjttt}fdd}|S)aReturns a decoder for a MessageSet item. The parameter is the message Descriptor. The message set message looks like this: message MessageSet { repeated group Item = 1 { required int32 type_id = 2; required string message = 3; } } rKrJrcs|}d}d}d}xx||\} }| kr8||\}}q| kr\||\} }|| }}q| krhPqt|||| }|dkrtdqW||krtd|dkrtd|dkrtd|j|} | dk r0|| } | dkr| j} t| ds|j| | | | } | ||||krtdn^|j s>g|_ |j t|||f|jdkrpt|_|j|tj||||S) aDecode serialized message set to its value and new position. Args: buffer: memoryview of the serialized bytes. pos: int, position in the memory view to start at. end: int, end position of serialized data message: Message object to store unknown fields in field_dict: Map[Descriptor, Any] to store decoded values in. Returns: int, new position in serialized data. r!zMissing group end tag.zTruncated message.z MessageSet item missing type_id.z MessageSet item missing message.N_concrete_classzUnexpected end-group tag.) SkipFieldr Z ExtensionsZ_FindExtensionByNumberr" message_typehasattrZ_FACTORYZ GetPrototyper#r~rwrZr$MESSAGE_SET_ITEM_TAGrr\rr]r^rrs)rrr%rr&Zmessage_set_item_startZtype_id message_startZ message_endrrp extensionr'r)item_end_tag_bytesr, local_ReadTagmessage_tag_bytestype_id_tag_bytesrr DecodeItemsX            z)MessageSetItemDecoder..DecodeItem) rr7rr[rsrzr r6r)Z descriptorZlocal_SkipFieldrr)rr,rrrrMessageSetItemDecodersHrcsB|t|jtjtt|jfdd}|S)z"Returns a decoder for a map field.c s}|}|dkr*||}x||\}}||}||krRtd||||||krttdr||j|jn |j||j<|}|||ks||kr,|Sq,WdS)NzTruncated message.zUnexpected end-group tag.) r~r"r#r ZClearrwr+ZCopyFromr') rrr%rr&Zsubmsgr'rpr/)is_message_mapr+r,rr-rr0rr DecodeMapks$  zMapDecoder..DecodeMap)rr7numberrrsr8r6r)Zfield_descriptorr-rrr)rr+r,rr-rr0r MapDecoder`srcCsFx(t|||dd@r(|d7}qW|d7}||krBtd|S)z/Skip a varint value. Returns the new position.rrzTruncated message.)ordrr )rrr%rrr _SkipVarints  rcCs|d7}||krtd|S)z0Skip a fixed64 value. Returns the new position.rSzTruncated message.)r )rrr%rrr _SkipFixed64srcCs$|d}td|||d|fS)zDecode a fixed64.rSz.SkipField) rrrrrrrrZ TAG_TYPE_MASK)rr)rrr _FieldSkippersr)F)FF)F)N)M__doc__ __author__rErer rgPY3intlongrecompileurhZgoogle.protobuf.internalrrrZgoogle.protobufrrPrOrNrjr rrr6Z_DecodeSignedVarintZ_DecodeVarint32rXr r>rArHrRrWrar[Z Int32DecoderZ Int64DecoderZ UInt32DecoderZ UInt64DecoderZ ZigZagDecodeZ SInt32DecoderZ SInt64DecoderrQZFixed32DecoderrVZFixed64DecoderZSFixed32DecoderZSFixed64DecoderZ FloatDecoderZ DoubleDecoderboolZ BoolDecoderrtrur|r}r7r{rrrrrrrrrrrrrrrrrrrrOs        ?-)           ? (/4b.