3 SÉLj;zã@sðdZdZddlZddlZejr"eZddlmZddlm Z ddl m Z dZ e Z e dZe jZdd „Zd d „Zed>eƒZed eƒZed@eƒZedeƒZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zee jeƒZee jeƒZ ee jeƒZ!ee jeƒZ"ee jee j#ƒZ$ee jee j#ƒZ%ee j&dƒZ'ee j(dƒZ)ee j&dƒZ*ee j(d ƒZ+eƒZ,eƒZ-ee jee.ƒZ/d!d"„Z0d#d$„Z1d%d&„Z2d'd(„Z3ej4d e j5ƒZ6d)d*„Z7d+d,„Z8d-d.„Z9d/d0„Z:d1d2„Z;d3d4„Zd9d:„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)Úmessagegðcs‡‡fdd„}|S)a¢Return 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 tdƒ‚q WdS)Nrééé€éé@z$Too many bytes when decoding varint.)ÚsixÚ indexbytesÚ _DecodeError)ÚbufferÚposÚresultÚshiftÚb)ÚmaskÚ result_type©ú/usr/lib/python3.6/decoder.pyÚ DecodeVarintus z$_VarintDecoder..DecodeVarintr)rrrr)rrrÚ_VarintDecoderks rcs,d|d>‰d|>d‰‡‡‡fdd„}|S)z0Like _VarintDecoder() but decodes signed values.rcszd}d}xltj||ƒ}||d@|>O}|d7}|d@sZ|ˆM}|ˆAˆ}ˆ|ƒ}||fS|d7}|dkr tdƒ‚q WdS)Nrrrrrr z$Too many bytes when decoding varint.)r r r )r rrrr)rrÚsignbitrrrŒs  z*_SignedVarintDecoder..DecodeVarintr)Úbitsrrr)rrrrÚ_SignedVarintDecoder†s  rrr é cCs@|}xtj||ƒd@r |d7}qW|d7}tj|||…ƒ|fS)açRead 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 rÚstartrrrÚReadTag©s  rcs‡‡fdd„}|S)zØReturn 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|rt‰‡‡‡‡fdd„}|S|rNtj|ˆƒ‰tˆƒ‰‡‡‡‡‡fdd„}|S‡‡fdd„}|SdS)Ncsˆ|jˆƒ}|dkr"|jˆˆ|ƒƒ}ˆ||ƒ\}}||7}||krHtdƒ‚x$||krlˆ||ƒ\}}|j|ƒqJW||kr„|d=tdƒ‚|S)NzTruncated message.rzPacked element was truncated.éÿÿÿÿ)ÚgetÚ setdefaultr Úappend)r rÚendrÚ field_dictÚvalueÚendpointÚelement)Ú decode_valueÚkeyÚlocal_DecodeVarintÚ new_defaultrrÚDecodePackedFieldÊs  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_bytesÚtag_lenrrÚDecodeRepeatedFieldÝs  zD_SimpleDecoder..SpecificDecoder..DecodeRepeatedFieldcs,ˆ||ƒ\|ˆ<}||kr(|ˆ=tdƒ‚|S)NzTruncated message.)r )r rr"rr#)r'r(rrÚ DecodeFieldîs z<_SimpleDecoder..SpecificDecoder..DecodeField)Ú _DecodeVarintrÚTagBytesÚlen)Ú field_numberÚ is_repeatedÚ is_packedr(r*r+r/r0)r'Ú wire_type)r(r)r*r-r.rÚSpecificDecoderÇs z'_SimpleDecoder..SpecificDecoderr)r7r'r8r)r'r7rÚ_SimpleDecoder¾s /r9cs‡‡fdd„}t||ƒS)z†Like 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Ú_ModifiedDecoderùsr<cs*tjˆƒ‰tj‰‡‡‡fdd„}t||ƒS)z­Return 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)ÚstructÚcalcsizeÚunpackr9)r7r=r;r)r=r>r?rÚ_StructPackDecoders  rCcstj‰‡fdd„}ttj|ƒS)z‡Returns 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}|||…}|dd…dkrl|dd…dkrl|dd…dkrLt|fS|dd…dkrdt|fSt|fSˆd |ƒd}||fS) Néésÿéó€rs€óÿzrrr;)s  z"_FloatDecoder..InnerDecode)r@rBr9rÚWIRETYPE_FIXED32)r;r)r>rÚ _FloatDecoder s rMcstj‰‡fdd„}ttj|ƒS)zkReturns a decoder for a double field. This code works around a bug in struct.unpack for not-a-number. csb|d}|||…}|dd…dkrL|dd…dkrL|dd…dkrLt|fSˆd|ƒd}||fS) Nérsÿéóðrsðzrrr;Ks z#_DoubleDecoder..InnerDecode)r@rBr9rÚWIRETYPE_FIXED64)r;r)r>rÚ_DoubleDecoderCs rRcspˆj‰|r&t‰‡‡‡‡‡fdd„}|S|rXtjˆtjƒ‰tˆƒ‰‡‡‡‡‡fdd„}|S‡‡‡fdd„}|SdS)Nc sÞ|jˆƒ}|dkr"|jˆˆ|ƒƒ}ˆ||ƒ\}}||7}||krHtdƒ‚xf||kr®|}t||ƒ\}}|ˆjkrz|j|ƒqJ|js†g|_tjˆt j ƒ} |jj| |||…fƒqJW||krÚ|ˆjkrÊ|d=n|jd=tdƒ‚|S)NzTruncated message.rzPacked element was truncated.rr) rr r Ú_DecodeSignedVarint32Úvalues_by_numberr!Ú_unknown_fieldsrr2rÚWIRETYPE_VARINT) r rr"rr#r$r%Úvalue_start_posr&r-)Ú enum_typer4r(r)r*rrr+es0     z&EnumDecoder..DecodePackedFieldcs¨|jˆƒ}|dkr"|jˆˆ|ƒƒ}x€t||ƒ\}}|ˆjkrH|j|ƒn$|jsTg|_|jjˆ|||…fƒ|ˆ}|||…ˆksŒ||kr$||krœtdƒ‚|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|_tjˆtjƒ}|jj||||…fƒ|S)NzTruncated message.) rSr rTrUrr2rrVr!)r rr"rr#rWZ enum_valuer-)rXr4r(rrr0›s  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_nameÚreason)Zbyte_strÚe)r(Ú local_unicoderrÚ_ConvertToUnicodeÓs  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)rrr0ñs z"StringDecoder..DecodeFieldN) r1r Z text_typeÚAssertionErrorrr2rÚWIRETYPE_LENGTH_DELIMITEDr3)r4r5r6r(r*r/r0r)r^r(r)r]r*r-r.rÚ StringDecoderÍs rbcsVt‰| st‚|r@tj|tjƒ‰tˆƒ‰‡‡‡‡‡fdd„}|S‡‡fdd„}|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)r1r`rr2rrar3)r4r5r6r(r*r/r0r)r(r)r*r-r.rÚ BytesDecoderûs rccsntj|tjƒ‰tˆƒ‰| s t‚|rTtj|tjƒ‰tˆƒ‰‡‡‡‡‡‡fdd„}|S‡‡‡‡fdd„}|SdS)z$Returns a decoder for a group field.cs¬|jˆƒ}|dkr"|jˆˆ|ƒƒ}x„|jˆƒ}|dkrF|jˆˆ|ƒƒ}|jƒj|||ƒ}|ˆ}|||…ˆksx||kr€tdƒ‚|ˆ}|||…ˆ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 rer )r rr"rr#r$r,)rfrgr(r*rrr0As z!GroupDecoder..DecodeFieldN)rr2rÚWIRETYPE_END_GROUPr3r`ÚWIRETYPE_START_GROUP)r4r5r6r(r*r/r0r)rfrgr(r*r-r.rÚ GroupDecoder s  rjcsXt‰| st‚|r@tj|tjƒ‰tˆƒ‰‡‡‡‡‡fdd„}|S‡‡‡fdd„}|SdS)z&Returns a decoder for a message field.cs”|jˆƒ}|dkr"|jˆˆ|ƒƒ}xlˆ||ƒ\}}||}||krJtdƒ‚|jƒj|||ƒ|krhtdƒ‚|ˆ}|||…ˆksˆ||kr$|Sq$WdS)NzTruncated message.zUnexpected end-group tag.)rr r rdre)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 re)r rr"rr#r$r_r,)r(r)r*rrr0os z#MessageDecoder..DecodeFieldN)r1r`rr2rrar3)r4r5r6r(r*r/r0r)r(r)r*r-r.rÚMessageDecoderOs rkcsNtjdtjƒ‰tjdtjƒ‰tjdtjƒ‰t‰t‰t}‡‡‡‡‡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 krtdƒ‚qW||krštdƒ‚|d krªtdƒ‚|d krºtdƒ‚|jj|ƒ} | dk �r|j| ƒ} | dk�rö|j| | jjƒƒ} | j|||ƒ|k�r:tdƒ‚n&|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_classrerUr!ÚMESSAGE_SET_ITEM_TAG) r rr"rr#Zmessage_set_item_startZtype_idZ message_startZ message_endr-r_Ú extensionr$)Úitem_end_tag_bytesr)Ú local_ReadTagÚmessage_tag_bytesÚtype_id_tag_bytesrrÚ DecodeItem›sF      z)MessageSetItemDecoder..DecodeItem) rr2rrVrarhrr1rl)Z descriptorZlocal_SkipFieldrur)rqr)rrrsrtrÚMessageSetItemDecoder…s0rvcsB|‰tj|jtjƒ‰tˆƒ‰t‰|j‰‡‡‡‡‡‡‡fdd„}|S)z"Returns a decoder for a map field.c sĈjƒ}|jˆƒ}|dkr*|jˆˆ|ƒƒ}x”ˆ||ƒ\}}||}||krRtdƒ‚|jƒ|j|||ƒ|krttdƒ‚ˆrŒ||jj|jƒn |j||j<|ˆ}|||…ˆks¸||kr,|Sq,WdS)NzTruncated message.zUnexpected end-group tag.) rnrr r ZClearrer(Z MergeFromr$) r rr"rr#Zsubmsgr$r_r,)Úis_message_mapr(r)rmr*r-r.rrÚ DecodeMapÚs$  zMapDecoder..DecodeMap)rr2Znumberrrar3r1rm)Zfield_descriptorr*rwrxr)rwr(r)rmr*r-r.rÚ MapDecoderÏsrycCsBx$t|||d…ƒd@r$|d7}qW|d7}||kr>tdƒ‚|S)z/Skip a varint value. Returns the new position.rrzTruncated message.)Úordr )r rr"rrrÚ _SkipVarintýs  r{cCs|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)rrl)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$srcCs|d7}||krtdƒ‚|S)z0Skip a fixed32 value. Returns the new position.rDzTruncated message.)r )r rr"rrrÚ _SkipFixed32)sr€cCs tdƒ‚dS)z;Skip function for unknown wire types. Raises an exception.zTag had invalid wire type.N)r )r rr"rrrÚ_RaiseInvalidWireType1sr�cs,ttttttttg‰tj‰‡‡fdd„}|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)rz)r rr"r-r7)ÚWIRETYPE_TO_SKIPPERÚ wiretype_maskrrrlFs z _FieldSkipper..SkipField) r{r|r}r~rr€r�rZ TAG_TYPE_MASK)rlr)r‚rƒrÚ _FieldSkipper6sr„llÿÿÿÿllÿÿ)BÚ__doc__Ú __author__r@r ZPY3ÚintZlongZgoogle.protobuf.internalrrZgoogle.protobufrrKrJrIZ DecodeErrorr rrr1Z_DecodeSignedVarintZ_DecodeVarint32rSrr9r<rCrMrRrYrVZ Int32DecoderZ Int64DecoderZ UInt32DecoderZ UInt64DecoderZ ZigZagDecodeZ SInt32DecoderZ SInt64DecoderrLZFixed32DecoderrQZFixed64DecoderZSFixed32DecoderZSFixed64DecoderZ FloatDecoderZ DoubleDecoderÚboolZ BoolDecoderrbrcrjrkr2rirorvryr{r|r}r~rr€r�r„rlrrrrÚOsv       ;#O         .%/4J.