Q,Qc@sbdZdZddlZddlmZddlmZddlmZdZe Z edZ ej Z d Z d Ze d&Zed(Ze d*Zed,ZdZdZdZdZdZdZeejeZZeejeZeejeZeejeZeejeejZ eejeejZ!eej"dZ#eej$dZ%eej"dZ&eej$dZ'eZ(eZ)eejee*Z+dZ,dZ-dZ.dZ/ej0d ej1Z2dZ3dZ4dZ5dZ6d Z7d!Z8d"Z9d#Z:d$Z;e;Z<dS(-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)iN(tencoder(t wire_format(tmessagegicstfd}|S(sReturn 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. csd}d}xr||}||d@|>O}|d7}|d@sY|M}||fS|d7}|dkrtdqqdS(Niiiiii@s$Too many bytes when decoding varint.(t _DecodeError(tbuffertpostresulttshifttb(t local_ordtmask(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt DecodeVarintps      (tord(R R ((R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_VarintDecoderes  cstfd}|S(s0Like _VarintDecoder() but decodes signed values.csd}d}x||}||d@|>O}|d7}|d@s}|dkri|d 8}|O}n |M}||fS|d7}|dkrtdqqdS( NiiiiIi@is$Too many bytes when decoding varint.l(R(RRRRR(R R (sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyR s        (R (R R ((R R sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_SignedVarintDecodersii@i cCsF|}x"t||d@r*|d7}q W|d7}|||!|fS(sRead 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. ii(R (RRtstart((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytReadTags  csfd}|S(sReturn 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(tfd}|S|rktj|tfd}|Sfd}|SdS(Ncs|j}|dkr6|j|}n||\}}||7}||krptdnx2||kr||\}}|j|qsW||kr|d=tdn|S(NsTruncated message.isPacked 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.pytDecodePackedFields    cs|j}|dkr6|j|}nxm||\}}|j||}|||!ks||kr9||krtdn|Sq9dS(NsTruncated message.(RRRRR(RRRRRRRtnew_pos(RRRt tag_bytesttag_len(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytDecodeRepeatedFields    cs?||\|<}||kr;|=tdn|S(NsTruncated message.(R(RRRRR(RR(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt DecodeFields  (t _DecodeVarintRtTagBytestlen(t field_numbert is_repeatedt is_packedRRRR"R#(Rt wire_type(RRRR R!sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytSpecificDecoders ((R*RR+((RR*sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_SimpleDecoders /csfd}t||S(sLike 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(R,(R*RR-R.((RR-sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_ModifiedDecoderscs:tjtjfd}t||S(sReturn 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((RRRR(tformatt local_unpackt value_size(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyR.s (tstructtcalcsizetunpackR,(R*R0R.((R0R1R2sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_StructPackDecoders cs(tjfd}ttj|S(sReturns 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}|||!}|ddkrx|ddkrx|dd!dkrTt|fS|ddkrnt|fSt|fSd |d}||fS( Niisisitsst||\}}||7}||kr:tdn|S(s9Skip a length-delimited value. Returns the new position.sTruncated message.(R$R(RRRRB((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt_SkipLengthDelimiteds   cCsJxCt||\}}t||||}|dkr=|S|}qdS(s*Skip sub-group. Returns the new position.iN(RRQ(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 _EndGroupscCs)|d7}||kr%tdn|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 _SkipFixed32s  cCstddS(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_RaiseInvalidWireTypescsFttttttttgtjt fd}|S(s"Constructs the SkipField function.cs(|d@}||||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). i((RRRR R*(tWIRETYPE_TO_SKIPPERR t wiretype_mask(sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyRQs ( RdReRfRgRhRiRjRt TAG_TYPE_MASKR (RQ((RkR RlsD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pyt _FieldSkippers  llllIIII(=t__doc__t __author__R3tgoogle.protobuf.internalRRtgoogle.protobufRR:R9R8t DecodeErrorRR RR$t_DecodeSignedVarintt_DecodeVarint32t_DecodeSignedVarint32RR,R/R6R=RARat Int32Decodert EnumDecodert Int64Decodert UInt32Decodert UInt64Decodert ZigZagDecodet SInt32Decodert SInt64DecoderR<tFixed32DecoderR@tFixed64DecodertSFixed32DecodertSFixed64Decodert FloatDecodert DoubleDecodertboolt BoolDecoderRGRHRORPR%RNRURcRdReRfRgRhRiRjRnRQ(((sD/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.pytOsl           ;   $ !   & % / 7 L     !