K iԒ~dZdZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ejZ dZ d Zd Zed eZed eZed eZedeZdZdZdZdZdZdZdZ d)dZee j:eZee j:eZee j:eZ ee j:eZ!ee j:ee jDZ#ee j:ee jDZ$ee jJdZ&ee jNdZ(ee jJdZ)ee jNdZ*eZ+eZ,ee j:ee-Z. d)dZ/ d)dZ0dZ1dZ2e jfde jhZ5d Z6d!Z7d"Z8d#Z9d$Z:d%Z;e;a d+d(Z?y),a 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)message) containers)encoder) wire_formatcpt|tjrtjd|dkry| S)aIReturns whether or not a scalar value is the default value of its type. Specifically, this should be used to determine presence of implicit-presence fields, where we disallow custom defaults. Args: value: A scalar value to check. Returns: True if the value is equivalent to a default value, False otherwise. g?rF) isinstancenumbersNumbermathcopysign)values f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/google/protobuf/internal/decoder.pyIsDefaultScalarValuerKs3w~~&4==e+Dq+H c&ddtffd }|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. posc,d}d} | |jdd}n ||}|dz }||dz|zz}|dzs|z}|}||S||fS|dz }|dk\r t d]#t$r&}|dk(rYd}~ytdt|zd}~wwxYw) NrzFail to read varint %s@$Too many bytes when decoding varint.)read IndexError ValueErrorstr _DecodeError)bufferrresultshiftbemask result_types r DecodeVarintz$_VarintDecoder..DecodeVarintjs F E  @kk!nQ! 3K q !d(u$%f$h$V$v763-7 qje "ABB+  @ aZ5A>? ?  @sA$$ B-B7BBN)int)r$r%r&s`` r_VarintDecoderr)`sCC4 rc:d|dz zd|zdz fd}|S)z0Like _VarintDecoder() but decodes signed values.rcd}d} ||}||dz|zz}|dz }|dzs|z}|z z }|}||fS|dz }|dk\r tdI)Nrrrrrrr)r)rrr r!r"r$r%signbits rr&z*_SignedVarintDecoder..DecodeVarints F E +a !d(u$%f Qhc$h$7"g-V$} qje "ABB r)bitsr%r&r$r,s ` @@r_SignedVarintDecoderr/s- $(O' t)q$C rlrl cj|}||dzr|dz }||dzr|dz }|||j}||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)tobytes)rrstart tag_bytess rReadTagr5sT" %s d1HC s d(#U3'')) CrcJt|d\}}tj|S)zDecode a tag from the bytes. Args: tag_bytes: the bytes of the tag Returns: Tuple[int, int] of the tag field number and wire type. r) _DecodeVarintr UnpackTag)r4tag_s r DecodeTagr;s%9a ((3   s ##rc dfd }|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() c |rt d  fd }|S|r.tj|  t  d  fd }|Sd fd }|S)Nc~|j }||j  |} ||\}}||z }||kDr td||kr# ||\}}|j|||kr#||kDr|d=td|S)NTruncated message.Packed element was truncated.get setdefaultrappend) rrendr field_dict current_depthr endpointelement decode_valuekeylocal_DecodeVarint new_defaults rDecodePackedFieldzB_SimpleDecoder..SpecificDecoder..DecodePackedFields s# =''[-AB%,VS93C c>12 2Hn'4.7C ,,w Hn >Bi<= = rc~|j }||j  |}  ||\}}|j|| z}||| k7s||k\r||kDr td|SBNr?)rCrDrEr)rrrFrrGrHr rJnew_posrKrLrNr4tag_lens rDecodeRepeatedFieldzD_SimpleDecoder..SpecificDecoder..DecodeRepeatedFields s# =''[-AB%+FC8 7G ,,w '!# GC I -C} !566Nrc~||\}}||kDr tdrt|r|j d|S|| <|SrQrrpop) rrrFrrGrH new_valueclear_if_defaultrKrLs r DecodeFieldz<_SimpleDecoder..SpecificDecoder..DecodeField sZ '4C 912 2  4Y ? ..d # &*S/ rr)r7rTagByteslen) field_number is_repeated is_packedrLrNrYrOrTrZrMr4rSrK wire_types ``` @@@rSpecificDecoderz'_SimpleDecoder..SpecificDecodersa(?@& ""<;iIg?@&!  rFr-)rarKrbs`` r_SimpleDecoderrds(-<| rc*fd}t||S)zLike SimpleDecoder but additionally invokes modify_value on every value before storing it. Usually modify_value is ZigZagDecode. c0||\}}||fSr'r-)rrr rRrK modify_values r InnerDecodez%_ModifiedDecoder..InnerDecode#s$$VS1VW  ' **r)rd)rarKrgrhs `` r_ModifiedDecoderris+  ; //rcxtjtjfd}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(). c4|z}|||d}||fSNrr-)rrrRr format local_unpack value_sizes rrhz'_StructPackDecoder..InnerDecode;s0JG &&W"5 6q 9F G r)structcalcsizeunpackrd)rarmrhrnros ` @@r_StructPackDecoderrs)s0v&*,  ; //rcbtjfd}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. c|dz}|||j}|dddvrO|dddk\rG|dddk7rtj|fS|dddk(rtj |fStj|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. rsz.InnerDecodeKsAgGW%--/K AaK'K!,<,G Qq _ ,'"" Qq W $ 7##hh  $ ,Q /F G r)rprrrdrWIRETYPE_FIXED32rhrns @r _FloatDecoderrBs), B  44k BBrcbtjfd}ttj|S)zkReturns a decoder for a double field. This code works around a bug in struct.unpack for not-a-number. c|dz}|||j}|dddvr"|dddk\r|dddk7rtj|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. rrxrsz.InnerDecodewsAgG#g&..0L a k ) !A ' ) !A "A Ahh  $ -a 0F G r)rprrrdrWIRETYPE_FIXED64rs @r_DoubleDecoderros( ,:  44k BBrc j |rt d  fd }|S|r.DecodePackedFieldsF nnS!e %%c;w+?@*637ox#oh C/00 (N.vs;# i00 0 ,,w ((&(G #&&|'2'B'BD)  ! ! ( (&5==?@ B (N x i00 0Bi:;;%%b):;; jrc~|j }||j  |} t||\}}| jvr|j |nA|j sg|_|j j |||j f| z}||| k7s||k\r||kDr td|S)Decode 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. r?)rCrDrrrErr2r)rrrFrrGrHr rJrRrrLrNr4rSs rrTz(EnumDecoder..DecodeRepeatedFields nnS!e %%c;w+?@ 263?' i00 0 ,,w ((&(G #  ! ! ( (&W-5578 :  '# ) +w#~ s]34 4.% rc~|}t||\}}||kDr td rt|r|j d|S| jvr|| <|S|j sg|_t j tj}|j j||||jf|S)rr?N) rrrrWrrrr\rrrEr2) rrrFrrGrHr enum_valuer4rYrr^rLs rrZz EnumDecoder..DecodeFields o/<z3 s/00 2:>sD! y11 1$ 3j&&$&' !$$\%0%@%@B && s3;;= > @jrr[)rr7rr\rrr]) r^r_r`rLrNrYrOrTrZrrMr4rSs ` ``` @@@@r EnumDecoderrszmm)&=>--^   {/J/JKI)nG=>%%N   D r._ConvertToUnicodeEsL H (G$e L  &'7ah  s ! AAAc~|j }||j  |}  ||\}}||z}||kDr td|j ||||z}||| k7s||k(r|SPNzTruncated string.rB)rrrFrrGrHr sizerRrrLrMrNr4rSs rrTz*StringDecoder..DecodeRepeatedFieldVs nnS!e %%c;w+?@ (5 s* S=01 1 &vc'':;< '# ) +w#~. rc~ ||\}}||z}||kDr td rt|r|j d|S|||| <|SrrV) rrrFrrGrHrrRrrYrLrMs rrZz"StringDecoder..DecodeFieldlsn &vs3ktSd g 3.// 248sD!n,F3w,?@ 3 nrr[r7rr\rWIRETYPE_LENGTH_DELIMITEDr]) r^r_r`rLrNrYrTrZrrMr4rSs ``` @@@@r StringDecoderr?si%   !,!F!FHI)nG=>&    rc t|rJ|r.DecodeRepeatedFields nnS!e %%c;w+?@ (5 s* S=01 1 VC(0023 '# ) +w#~. rc~ ||\}}||z}||kDr tdrt|r|j d|S|||j| <|Sr)rrrWr2) rrrFrrGrHrrRrYrLrMs rrZz!BytesDecoder..DecodeFieldsr &vs3ktSd g 3.// 248sD!n!W-557 3 nrr[r) r^r_r`rLrNrYrTrZrMr4rSs ``` @@@r BytesDecoderr{s]%   !,!F!FHI)nG=>&   rc tj|tjt |rJ|r=tj|tj  t  d fd }|Sdfd }|S)z$Returns a decoder for a group field.c|j }||j  |} |j }||j  |}|dz }|tkDr td|j j ||||}|dz}| z}|||k7s||kDr td| z}||| k7s||k(r|SNr2Error parsing message: too many levels of nesting.Missing group end tag.)rCrD_recursion_limitradd_InternalParse)rrrFrrGrHr rR end_tag_bytes end_tag_lenrLrNr4rSs rrTz)GroupDecoder..DecodeRepeatedFieldsnnS!e %%c;w+?@ s# =''[-AB% + +B  iik((c=I k/ #g - /7S=56 6 '# ) +w#~.) rc|j }||j  |}|dz }|tkDr td|j ||||}|dz}| z}|||k7s||kDr td|Sr)rCrDrrr) rrrFrrGrHr rRrrrLrNs rrZz!GroupDecoder..DecodeFieldsnnS!e %%c;w+?@qm ) )OPP  c= AcqmKg G  -3344 nrr[)rr\rWIRETYPE_END_GROUPr]WIRETYPE_START_GROUP) r^r_r`rLrNrTrZrrr4rSs `` @@@@r GroupDecoderrs""<#.#A#AC-M"+   !,!A!ACI)nG=>8  rc t|rJ|r.DecodeRepeatedFieldsnnS!e %%c;w+?@ (5 s* S=12 2 + +B   IIK & &vsG] K  89 9  '# ) +w#~.1 rc |j }||j  |} ||\}}||z}||kDr td|dz }|tkDr td|j |||||k7r td|dz}|S)Nr?rrr)rCrDrrr) rrrFrrGrHr rrRrLrMrNs rrZz#MessageDecoder..DecodeFieldsnnS!e %%c;w+?@&vs3ktSd g 3/00qm ) )OPP   fc7M Bg M677qm nrr[r) r^r_r`rLrNrTrZrMr4rSs `` @@@rMessageDecoderrs^%   !,!F!FHI)nG=>@ ( rrctjdtjtjdtjtjdtj t tfd}|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; } } ryrwrc |}d}d}d} ||\} }| k(r ||\}}nO| k(r||\} }|| zx}}n6| k(rn1t| \} } t|||| | \} }|dk(r tdn||kDr td|dk(r td|dk(r td|jj |}||j |}|M|j }t|dstj||j||j}|j||||k7r td|S|jsg|_ |jjt|||j!f|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@rr? MessageSet item missing type_id. MessageSet item missing message._concrete_class)r;_DecodeUnknownFieldr Extensions_FindExtensionByNumberrC message_typehasattrmessage_factoryGetMessageClassrDrrrrEMESSAGE_SET_ITEM_TAGr2)rrrFrrGmessage_set_item_starttype_id message_start message_endr4rr^rar: extensionr ritem_end_tag_bytesrM local_ReadTagmessage_tag_bytestype_id_tag_bytess r DecodeItemz)MessageSetItemDecoder..DecodeItemFs!GMK &vs3y# ' '+FC8# ) ) 263 ?})D00k * * "+I"6 i$VS#|YO3 "989 9  Sy - .."} ; << ; <<""99'BInnY'e  -- |%67  ) ), 7%% |3357   fmK @K O677 J  $ $"$ $$ (>s!C!K!K!M NP Jr)rr\rrrrr5r7) descriptorrrrMrrrs @@@@@rMessageSetItemDecoderr1sp&&q+*E*EF&&q+*O*OP'';+I+IJ-$@@D rctjdtjtjdtjtjdtj fd}|S)z0Returns a decoder for a Unknown MessageSet item.ryrwrcd}t|}d}d} t||\}}| k(rt||\}}nR| k(rt||\}}||zx}}n6| k(rn1t|\}} t ||||| \} }|dk(r t dw||kDr t ddk(r t d|dk(r t d||||j fS)Nrr@rr?rr)r]r5r7r;rrr2)rrrFrrr4rrr^rar:rrrs rDecodeUnknownItemz7UnknownMessageSetItemDecoder..DecodeUnknownItems C f+CMK  -y# ' '&vs3# ) ) -fc :})D00k * * "+I"6 i$VS#|YO3 "989 9  Sy - .."} ; << ; << VM+6>>@ AAr)rr\rrrr)rrrrs @@@rUnknownMessageSetItemDecoderrs^&&q+*E*EF&&q+*O*OP'';+I+IJB< rc|tj|jtjt t |jdfd }|S)z"Returns a decoder for a map field.c~ j}|j }||j |}  ||\}}||z} | |kDr td|j |j ||| | k7r td r)||j j|jn|j||j <| z}|| |k7s| |k(r| S)Nr?r) rrCrDrClearrrLCopyFromr )rrrFrrGrHsubmsgr rrRis_message_maprLrMrrNr4rSs r DecodeMapzMapDecoder..DecodeMaps  ) ) +F NN3 E }##CW)=>e &vs3ktSd g 3/00 lln   vsG 4 ?677  fjj""6<<0"LLfjj g c   )W^- rr[)rr\numberrrr]r7r) field_descriptorrNrrrLrMrr4rSs `` @@@@@r MapDecoderrsY #/66*DDF)  N'$!..,< rcH|dz}tjd|||d|fS)zDecode a fixed64.rrrrprrrrrRs r_DecodeFixed64rs- !G' --fS1 21 5w ??rcH|dz}tjd|||d|fS)zDecode a fixed32.rvrrrrs r_DecodeFixed32rs/ !G' --fS1 21 5w ??rdc|ayr')r) new_limits rSetRecursionLimitrsrc:tj}|||kr}t||\}}t|d\}}t j |\}} | tj k(r ||fSt||||| |\} }|j|| | |w||kr}||fS)zFDecode UnknownFieldSet. Returns the UnknownFieldSet and new position.r) rUnknownFieldSetr5r7rr8rr_add) rrend_posrHunknown_field_setr4r9r:r^radatas r_DecodeUnknownFieldSetrs!0023=vs+YY*HS!)33C8L)K222  S !! &WlI}KT3<D9 3= S !!rc|tjk(rt||\}}n)|tjk(rt ||\}}n|tj k(rt ||\}}n|tjk(r+t||\}}||||zj}||z }n|tjk(rrtj|tj}|dz }|tk\r tdt||||\}}|dz}||t!|z ||k7r*td|tjk(rytd||kDr td||fS)zCDecode a unknown field. Returns the UnknownField and new position.rrr)rr@zWrong wire type in tag.r?)rrr7rrrrrr2rrr\rrrrr]) rrrr^rarHrrrs rrrse +---,KT3K000 -KT3K000 -KT3K999,KT3 #c$h  ' ' )D4KCK444$$k44MQM(( M NN&vsG]KID#QM cC &&-> 1 22K222  0 117] + ,, rrcrlr[)@__doc__ __author__r r rpgoogle.protobufrgoogle.protobuf.internalrrr DecodeErrorrrr)r/r(r7_DecodeSignedVarint_DecodeVarint32rr5r;rdrirsrrrr Int32Decoder Int64Decoder UInt32Decoder UInt64Decoder ZigZagDecode SInt32Decoder SInt64DecoderrFixed32DecoderrFixed64DecoderSFixed32DecoderSFixed64Decoder FloatDecoder DoubleDecoderbool BoolDecoderrrrrr\rrrrrrrDEFAULT_RECURSION_LIMITrrrrr-rrrs40d0  #/,0 "" *$N0}c2 *2s3!4,R54 $ GT 002*CZ%CR"'EV!68 !46 {::OL {::MJ  +2J2JL   0H0HJ %[%A%A4H$[%A%A4H$[%A%A4H$[%A%A4H  6 $)9z#(-`;|AL(w'';+K+KLWt%R)X@ @ * "(BC#r