ó Du—ac@sšdZdZddlZddlZejr6eZnddlmZddlm Z ddl m Z dZ e Z e dZe jZd „Zd „Zed(eƒZed eƒZed*eƒZed eƒZd„Zd„Zd„Zd„Zd„Zd„Zd„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„Z0d„Z1d„Z2d„Z3ej4d e j5ƒZ6d„Z7d„Z8d„Z9d „Z:d!„Z;d"„Z<d#„Z=d$„Z>d%„Z?d&„Z@e@ƒZAdS(+sÏ 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. s kenton@google.com (Kenton Varda)iÿÿÿÿN(tencoder(t wire_format(tmessagegðics‡‡fd†}|S(s¢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. cs”d}d}xtj||ƒ}||d@|>O}|d7}|d@sg|ˆM}ˆ|ƒ}||fS|d7}|dkrtdƒ‚qqWdS(Niiii€ii@s$Too many bytes when decoding varint.(tsixt indexbytest _DecodeError(tbuffertpostresulttshifttb(tmaskt result_type(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt DecodeVarintus       ((R R R ((R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_VarintDecoderks cs5d|d>‰d|>d‰‡‡‡fd†}|S(s0Like _VarintDecoder() but decodes signed values.ics¢d}d}xtj||ƒ}||d@|>O}|d7}|d@su|ˆM}|ˆAˆ}ˆ|ƒ}||fS|d7}|dkrtdƒ‚qqWdS(Niiii€ii@s$Too many bytes when decoding varint.(RRR(RRRR R (R R tsignbit(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyR Œs       ((tbitsR R ((R R RsD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_SignedVarintDecoder†sii@i cCsQ|}x$tj||ƒd@r,|d7}q W|d7}tj|||!ƒ|fS(sç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. i€i(RRt binary_type(RRtstart((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytReadTag©s  cs‡‡fd†}|S(sØ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() cs…|r(t‰‡‡‡‡fd†}|S|rktj|ˆƒ‰tˆƒ‰‡‡‡‡‡fd†}|S‡‡fd†}|SdS(NcsË|jˆƒ}|dkr6|jˆˆ|ƒƒ}nˆ||ƒ\}}||7}||krptdƒ‚nx2||kr¤ˆ||ƒ\}}|j|ƒqsW||krÇ|d=tdƒ‚n|S(NsTruncated message.iÿÿÿÿsPacked element was truncated.(tgettNonet setdefaultRtappend(RRtendRt field_dicttvaluetendpointtelement(t decode_valuetkeytlocal_DecodeVarintt new_default(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytDecodePackedFieldÊs    cs«|jˆƒ}|dkr6|jˆˆ|ƒƒ}nxnˆ||ƒ\}}|j|ƒ|ˆ}|||!ˆks„||kr9||krŸtdƒ‚n|Sq9WdS(NsTruncated message.(RRRRR(RRRRRRRtnew_pos(RRR!t tag_bytesttag_len(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytDecodeRepeatedFieldÝs    cs?ˆ||ƒ\|ˆ<}||kr;|ˆ=tdƒ‚n|S(NsTruncated message.(R(RRRRR(RR(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt DecodeFieldîs  (t _DecodeVarintRtTagBytestlen(t field_numbert is_repeatedt is_packedRR!R"R&R'(Rt wire_type(RR R!R$R%sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytSpecificDecoderÇs ((R.RR/((RR.sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_SimpleDecoder¾s /cs‡‡fd†}t||ƒS(s†Like SimpleDecoder but additionally invokes modify_value on every value before storing it. Usually modify_value is ZigZagDecode. cs%ˆ||ƒ\}}ˆ|ƒ|fS(N((RRRR#(Rt modify_value(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt InnerDecodes(R0(R.RR1R2((RR1sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_ModifiedDecoderùscs:tjˆƒ‰tj‰‡‡‡fd†}t||ƒS(s­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(Ni((RRR#R(tformatt local_unpackt value_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyR2s (tstructtcalcsizetunpackR0(R.R4R2((R4R5R6sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_StructPackDecoders cs(tj‰‡fd†}ttj|ƒS(s‡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!dkr|dd!dkr|dd!dkrZt|fS|dd!dkrwt|fSt|fSˆd |ƒd}||fS( Niisÿis€is€sÿst||ƒ\}}||7}||kr:tdƒ‚n|S(s9Skip a length-delimited value. Returns the new position.sTruncated message.(R(R(RRRRS((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_SkipLengthDelimiteds   cCsKxDt||ƒ\}}t||||ƒ}|dkr=|S|}qWdS(s*Skip sub-group. Returns the new position.iÿÿÿÿN(RRa(RRRR$R#((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt _SkipGroups  cCsdS(sFSkipping an END_GROUP tag returns -1 to tell the parent loop to break.iÿÿÿÿ((RRR((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt _EndGroup$scCs)|d7}||kr%tdƒ‚n|S(s0Skip a fixed32 value. Returns the new position.isTruncated message.(R(RRR((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt _SkipFixed32)s  cCstdƒ‚dS(s;Skip function for unknown wire types. Raises an exception.sTag had invalid wire type.N(R(RRR((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_RaiseInvalidWireType1scs=ttttttttg‰tj‰‡‡fd†}|S(s"Constructs the SkipField function.cs+t|dd!ƒˆ@}ˆ||||ƒS(sSkips 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). ii(R|(RRRR$R.(tWIRETYPE_TO_SKIPPERt wiretype_mask(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyRaFs ( R}R~RR€RR‚RƒRt TAG_TYPE_MASK(Ra((R„R…sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt _FieldSkipper6s  llÿÿÿÿllÿÿ(Bt__doc__t __author__R7RtPY3tinttlongtgoogle.protobuf.internalRRtgoogle.protobufRR=R<R;t DecodeErrorRRRR(t_DecodeSignedVarintt_DecodeVarint32RDRR0R3R:R@RCRKRGt Int32Decodert Int64Decodert UInt32Decodert UInt64Decodert ZigZagDecodet SInt32Decodert SInt64DecoderR?tFixed32DecoderRBtFixed64DecodertSFixed32DecodertSFixed64Decodert FloatDecodert DoubleDecodertboolt BoolDecoderRWRXR_R`R)R^RfRsR{R}R~RR€RR‚RƒR‡Ra(((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytOsv          ;   #  O   . % / 4 J .