3 SLj;z@sdZdZddlZddlZejr"eZddlmZddlm Z ddl m Z dZ e Z e dZe jZdd Zd d Zed>eZed eZed@eZedeZddZddZddZddZddZddZddZee jeZee jeZ ee jeZ!ee jeZ"ee jee j#Z$ee jee j#Z%ee j&dZ'ee j(dZ)ee j&dZ*ee j(d Z+eZ,eZ-ee jee.Z/d!d"Z0d#d$Z1d%d&Z2d'd(Z3ej4d e j5Z6d)d*Z7d+d,Z8d-d.Z9d/d0Z:d1d2Z;d3d4Zd9d:Z?d;d<Z@e@ZAdS)Aa 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)N)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`tj||}||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_type/usr/lib/python3.6/decoder.py DecodeVarintus z$_VarintDecoder..DecodeVarintr)rrrr)rrr_VarintDecoderks rcs,d|d>d|>dfdd}|S)z0Like _VarintDecoder() but decodes signed values.rcszd}d}xltj||}||d@|>O}|d7}|d@sZ|M}|A}|}||fS|d7}|dkr tdq WdS)Nrrrrrr z$Too many bytes when decoding varint.)r r r )r rrrr)rrsignbitrrrs  z*_SignedVarintDecoder..DecodeVarintr)bitsrrr)rrrr_SignedVarintDecoders  rrr cCs@|}xtj||d@r |d7}qW|d7}tj||||fS)aRead a tag from the buffer, 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. rr)r r Z binary_type)r rstartrrrReadTags  rcsfdd}|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() csd|rtfdd}|S|rNtj|tfdd}|Sfdd}|SdS)Ncs|j}|dkr"|j|}||\}}||7}||krHtdx$||krl||\}}|j|qJW||kr|d=td|S)NzTruncated message.rzPacked element was truncated.)get setdefaultr append)r rendr field_dictvalueendpointelement) decode_valuekeylocal_DecodeVarint new_defaultrrDecodePackedFields  zB_SimpleDecoder..SpecificDecoder..DecodePackedFieldcsx|j}|dkr"|j|}xP||\}}|j||}|||ks\||kr$||krltd|Sq$WdS)NzTruncated message.)rr r!r )r rr"rr#r$r&new_pos)r'r(r* tag_bytestag_lenrrDecodeRepeatedFields  zD_SimpleDecoder..SpecificDecoder..DecodeRepeatedFieldcs,||\|<}||kr(|=td|S)NzTruncated message.)r )r rr"rr#)r'r(rr DecodeFields z<_SimpleDecoder..SpecificDecoder..DecodeField) _DecodeVarintrTagByteslen) field_number is_repeated is_packedr(r*r+r/r0)r' wire_type)r(r)r*r-r.rSpecificDecoders z'_SimpleDecoder..SpecificDecoderr)r7r'r8r)r'r7r_SimpleDecoders /r9csfdd}t||S)zLike SimpleDecoder but additionally invokes modify_value on every value before storing it. Usually modify_value is ZigZagDecode. cs||\}}||fS)Nr)r rrr,)r' modify_valuerr InnerDecodesz%_ModifiedDecoder..InnerDecode)r9)r7r'r:r;r)r'r:r_ModifiedDecodersr<cs*tjtjfdd}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)r rr,r)format local_unpack value_sizerrr;sz'_StructPackDecoder..InnerDecode)structcalcsizeunpackr9)r7r=r;r)r=r>r?r_StructPackDecoders  rCcstjfdd}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}|||}|dddkrl|dddkrl|dddkrLt|fS|dddkrdt|fSt|fSd |d}||fS) Nsrszrrr;)s  z"_FloatDecoder..InnerDecode)r@rBr9rWIRETYPE_FIXED32)r;r)r>r _FloatDecoder s rMcstjfdd}ttj|S)zkReturns a decoder for a double field. This code works around a bug in struct.unpack for not-a-number. csb|d}|||}|dddkrL|dddkrL|dddkrLt|fSd|d}||fS) Nrsrszrrr;Ks z#_DoubleDecoder..InnerDecode)r@rBr9rWIRETYPE_FIXED64)r;r)r>r_DoubleDecoderCs rRcspj|r&tfdd}|S|rXtjtjtfdd}|Sfdd}|SdS)Nc s|j}|dkr"|j|}||\}}||7}||krHtdxf||kr|}t||\}}|jkrz|j|qJ|jsg|_tjt j } |jj| |||fqJW||kr|jkr|d=n|jd=td|S)NzTruncated message.rzPacked element was truncated.rr) rr r _DecodeSignedVarint32values_by_numberr!_unknown_fieldsrr2rWIRETYPE_VARINT) r rr"rr#r$r%value_start_posr&r-) enum_typer4r(r)r*rrr+es0     z&EnumDecoder..DecodePackedFieldcs|j}|dkr"|j|}xt||\}}|jkrH|j|n$|jsTg|_|jj|||f|}|||ks||kr$||krtd|Sq$WdS)NzTruncated message.)rr rSrTr!rUr )r rr"rr#r$r&r,)rXr(r*r-r.rrr/s    z(EnumDecoder..DecodeRepeatedFieldcsl|}t||\}}||kr"td|jkr6||<n2|jsBg|_tjtj}|jj||||f|S)NzTruncated message.) rSr rTrUrr2rrVr!)r rr"rr#rWZ enum_valuer-)rXr4r(rrr0s  z EnumDecoder..DecodeField)rXr1rr2rrVr3)r4r5r6r(r*r+r/r0r)rXr4r(r)r*r-r.r EnumDecoderasrYz}zd|jf|_WYdd}~XnXdS)Nzutf-8z%s in field: %s)UnicodeDecodeErrorZ full_namereason)Zbyte_stre)r( local_unicoderr_ConvertToUnicodes  z(StringDecoder.._ConvertToUnicodecs|j}|dkr"|j|}xd||\}}||}||krJtd|j||||}|||ks||kr$|Sq$WdS)NzTruncated string.)rr r r!)r rr"rr#r$sizer,)r^r(r)r*r-r.rrr/s z*StringDecoder..DecodeRepeatedFieldcs>||\}}||}||kr&td||||<|S)NzTruncated string.)r )r rr"rr#r_r,)r^r(r)rrr0s z"StringDecoder..DecodeFieldN)r1r Z text_typerr2rWIRETYPE_LENGTH_DELIMITEDr3)r4r5r6r(r*r/r0r)r^r(r)r]r*r-r.r StringDecoders racsLt|r6tj|tjtfdd}|Sfdd}|SdS)z$Returns a decoder for a bytes field.cs|j}|dkr"|j|}x`||\}}||}||krJtd|j||||}|||ks|||kr$|Sq$WdS)NzTruncated string.)rr r r!)r rr"rr#r$r_r,)r(r)r*r-r.rrr/s z)BytesDecoder..DecodeRepeatedFieldcs:||\}}||}||kr&td||||<|S)NzTruncated string.)r )r rr"rr#r_r,)r(r)rrr0s z!BytesDecoder..DecodeFieldN)r1rr2rr`r3)r4r5r6r(r*r/r0r)r(r)r*r-r.r BytesDecodersrbcsdtj|tjt|rJtj|tjtfdd}|Sfdd}|SdS)z$Returns a decoder for a group field.cs|j}|dkr"|j|}x|j}|dkrF|j|}|jj|||}|}|||ksx||krtd|}|||ks||kr$|Sq$WdS)NzMissing group end tag.)rr add_InternalParser )r rr"rr#r$r,) end_tag_bytes end_tag_lenr(r*r-r.rrr/,s  z)GroupDecoder..DecodeRepeatedFieldcs\|j}|dkr"|j|}|j|||}|}|||ksP||krXtd|S)NzMissing group end tag.)rr rdr )r rr"rr#r$r,)rerfr(r*rrr0As z!GroupDecoder..DecodeFieldN)rr2rWIRETYPE_END_GROUPr3WIRETYPE_START_GROUP)r4r5r6r(r*r/r0r)rerfr(r*r-r.r GroupDecoder s ricsNt|r6tj|tjtfdd}|Sfdd}|SdS)z&Returns a decoder for a message field.cs|j}|dkr"|j|}xl||\}}||}||krJtd|jj||||krhtd|}|||ks||kr$|Sq$WdS)NzTruncated message.zUnexpected end-group tag.)rr r rcrd)r rr"rr#r$r_r,)r(r)r*r-r.rrr/Ys z+MessageDecoder..DecodeRepeatedFieldcsf|j}|dkr"|j|}||\}}||}||krHtd|j||||krbtd|S)NzTruncated message.zUnexpected end-group tag.)rr r rd)r rr"rr#r$r_r,)r(r)r*rrr0os z#MessageDecoder..DecodeFieldN)r1rr2rr`r3)r4r5r6r(r*r/r0r)r(r)r*r-r.rMessageDecoderOsrjcsNtjdtjtjdtjtjdtjttt}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; } } rFrErc s>|}d}d}d }xx||\} }| kr8||\}}q| kr\||\} }|| }}q| krhPqt|||| }|d krtdqW||krtd|d krtd|d krtd|jj|} | dk r|j| } | dkr|j| | jj} | j||||kr:tdn&|j s"g|_ |j j t |||f|S) NrzMissing group end tag.zTruncated message.z MessageSet item missing type_id.z MessageSet item missing message.zUnexpected end-group tag.rrrrrr) SkipFieldr Z ExtensionsZ_FindExtensionByNumberrr message_type_concrete_classrdrUr!MESSAGE_SET_ITEM_TAG) r rr"rr#Zmessage_set_item_startZtype_idZ message_startZ message_endr-r_ extensionr$)item_end_tag_bytesr) local_ReadTagmessage_tag_bytestype_id_tag_bytesrr DecodeItemsF      z)MessageSetItemDecoder..DecodeItem) rr2rrVr`rgrr1rk)Z descriptorZlocal_SkipFieldrtr)rpr)rqrrrsrMessageSetItemDecoders0rucsB|tj|jtjtt|jfdd}|S)z"Returns a decoder for a map field.c sj}|j}|dkr*|j|}x||\}}||}||krRtd|j|j||||krttdr||jj|jn |j||j<|}|||ks||kr,|Sq,WdS)NzTruncated message.zUnexpected end-group tag.) rmrr r ZClearrdr(Z MergeFromr$) r rr"rr#Zsubmsgr$r_r,)is_message_mapr(r)rlr*r-r.rr DecodeMaps$  zMapDecoder..DecodeMap)rr2Znumberrr`r3r1rl)Zfield_descriptorr*rvrwr)rvr(r)rlr*r-r.r MapDecodersrxcCsBx$t|||dd@r$|d7}qW|d7}||kr>td|S)z/Skip a varint value. Returns the new position.rrzTruncated message.)ordr )r rr"rrr _SkipVarints  rzcCs|d7}||krtd|S)z0Skip a fixed64 value. Returns the new position.rNzTruncated message.)r )r rr"rrr _SkipFixed64 sr{cCs*t||\}}||7}||kr&td|S)z9Skip a length-delimited value. Returns the new position.zTruncated message.)r1r )r rr"r_rrr_SkipLengthDelimiteds r|cCs6x0t||\}}t||||}|dkr*|S|}qWdS)z*Skip sub-group. Returns the new position.rNr)rrk)r rr"r-r,rrr _SkipGroups r}cCsdS)zFSkipping an END_GROUP tag returns -1 to tell the parent loop to break.rrr)r rr"rrr _EndGroup$sr~cCs|d7}||krtd|S)z0Skip a fixed32 value. Returns the new position.rDzTruncated message.)r )r rr"rrr _SkipFixed32)srcCs tddS)z;Skip function for unknown wire types. Raises an exception.zTag had invalid wire type.N)r )r rr"rrr_RaiseInvalidWireType1srcs,ttttttttgtjfdd}|S)z"Constructs the SkipField function.cs$t|dd@}||||S)aSkips a field with the specified tag. |pos| should point to the byte immediately after the tag. Returns: The new position (after the tag value), or -1 if the tag is an end-group tag (in which case the calling loop should break). rr)ry)r rr"r-r7)WIRETYPE_TO_SKIPPER wiretype_maskrrrkFs z _FieldSkipper..SkipField) rzr{r|r}r~rrrZ TAG_TYPE_MASK)rkr)rrr _FieldSkipper6srllll)B__doc__ __author__r@r ZPY3intZlongZgoogle.protobuf.internalrrZgoogle.protobufrrKrJrIZ DecodeErrorr rrr1Z_DecodeSignedVarintZ_DecodeVarint32rSrr9r<rCrMrRrYrVZ Int32DecoderZ Int64DecoderZ UInt32DecoderZ UInt64DecoderZ ZigZagDecodeZ SInt32DecoderZ SInt64DecoderrLZFixed32DecoderrQZFixed64DecoderZSFixed32DecoderZSFixed64DecoderZ FloatDecoderZ DoubleDecoderboolZ BoolDecoderrarbrirjr2rhrnrurxrzr{r|r}r~rrrrkrrrrOsv       ;#O         .%/4J.