Python google.protobuf.internal.decoder._DecodeVarint() Examples

The following are 21 code examples of google.protobuf.internal.decoder._DecodeVarint(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.protobuf.internal.decoder , or try the search function .
Example #1
Source File: corenlp.py    From stanza-old with Apache License 2.0 6 votes vote down vote up
def annotate_proto(self, text, annotators=None):
        """Return a Document protocol buffer from the CoreNLP server, containing annotations of the text.

        :param (str) text: text to be annotated
        :param (list[str]) annotators: a list of annotator names

        :return (CoreNLP_pb2.Document): a Document protocol buffer
        """
        properties = {
            'annotators': ','.join(annotators or self.default_annotators),
            'outputFormat': 'serialized',
            'serializer': 'edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer'
        }
        r = self._request(text, properties)
        buffer = r.content  # bytes

        size, pos = _DecodeVarint(buffer, 0)
        buffer = buffer[pos:(pos + size)]
        doc = CoreNLP_pb2.Document()
        doc.ParseFromString(buffer)
        return doc 
Example #2
Source File: stream.py    From pystream-protobuf with MIT License 6 votes vote down vote up
def _read_varint(self):
        """Read a varint from file, parse it, and return the decoded integer.
        """
        buff = self._fd.read(1)
        if buff == b'':
            return 0

        while (bytearray(buff)[-1] & 0x80) >> 7 == 1:  # while the MSB is 1
            new_byte = self._fd.read(1)
            if new_byte == b'':
                raise EOFError('unexpected EOF.')
            buff += new_byte

        varint, _ = decodeVarint(buff, 0)

        return varint 
Example #3
Source File: stream.py    From pystream-protobuf with MIT License 6 votes vote down vote up
def _async_read_varint(self):
        """Read a varint from an async stream, parse it, and return the decoded
        integer.
        """
        buff = await self._fd.read(1)
        if buff == b'':
            return 0

        while (bytearray(buff)[-1] & 0x80) >> 7 == 1:  # while the MSB is 1
            new_byte = await self._fd.read(1)
            if new_byte == b'':
                raise EOFError('unexpected EOF.')
            buff += new_byte

        varint, _ = decodeVarint(buff, 0)

        return varint 
Example #4
Source File: business.py    From PyMicroChat with GNU General Public License v3.0 5 votes vote down vote up
def set_group_nick_name_buf2resp(buf):
    res = mm_pb2.oplog_resp()
    res.ParseFromString(UnPack(buf))
    try:
        code,__ = decoder._DecodeVarint(res.res.code, 0)
        if code:
            logger.info('设置群昵称失败,错误码:0x{:x},错误信息:{}'.format(code, res.res.msg), 11)
    except:
        code = -1
        logger.info('设置群昵称失败!', 11)
    return code

# 消息撤回请求 
Example #5
Source File: reflection_test.py    From keras-lambda with MIT License 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #6
Source File: reflection_test.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #7
Source File: varint.py    From iLEAPP with MIT License 5 votes vote down vote up
def decode_uvarint(buf, pos):
    """Decode bytearray into a long."""
    # Convert buffer to string
    #buf = buf.decode('latin')
    value, pos = decoder._DecodeVarint(buf, pos)
    return (value, pos) 
Example #8
Source File: varint.py    From unfurl with Apache License 2.0 5 votes vote down vote up
def decode_uvarint(buf, pos):
    """Decode bytearray into a long."""
    # Convert buffer to string
    # buf = buf.decode('latin')
    value, pos = decoder._DecodeVarint(buf, pos)
    return value, pos 
Example #9
Source File: __init__.py    From pynlp with MIT License 5 votes vote down vote up
def from_bytes(bytes_):
    offset = 0
    doc = Document()
    size, pos = _DecodeVarint(bytes_, offset)
    doc.ParseFromString(bytes_[offset + pos:offset + pos + size])
    return doc 
Example #10
Source File: varint.py    From blackboxprotobuf with MIT License 5 votes vote down vote up
def decode_uvarint(buf, pos):
    """Decode bytearray into a long."""
    # Convert buffer to string
    buf = buf.decode('latin')
    value, pos = decoder._DecodeVarint(buf, pos)
    return (value, pos) 
Example #11
Source File: service.py    From ATX with Apache License 2.0 5 votes vote down vote up
def unpack(data):
    '''unpack from delimited data'''
    size, position = decoder._DecodeVarint(data, 0)
    envelope = wire.Envelope() 
    envelope.ParseFromString(data[position:position+size])
    return envelope 
Example #12
Source File: business.py    From PyMicroChat with GNU General Public License v3.0 5 votes vote down vote up
def op_friend_buf2resp(buf):
    res = mm_pb2.oplog_resp()
    res.ParseFromString(UnPack(buf))
    try:
        code,__ = decoder._DecodeVarint(res.res.code, 0)
        if code:
            logger.info('好友操作失败,错误码:0x{:x},错误信息:{}'.format(code, res.res.msg), 11)
    except:
        code = -1
        logger.info('好友操作失败!', 11)
    return code

# 发布群公告请求(仅限群主;自动@所有人) 
Example #13
Source File: reflection_test.py    From lambda-packs with MIT License 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #14
Source File: business.py    From PyMicroChat with GNU General Public License v3.0 5 votes vote down vote up
def UnPack(src, key=b''):
    if len(src) < 0x20:
        raise RuntimeError('Unpack Error!Please check mm protocol!')                                # 协议需要更新
        return b''
    if not key:
        key = Util.sessionKey
    # 解析包头
    nCur = 0
    if src[nCur] == struct.unpack('>B', b'\xbf')[0]:
        nCur += 1                                                                                   # 跳过协议标志位
    nLenHeader = src[nCur] >> 2                                                                     # 包头长度
    bUseCompressed = (src[nCur] & 0x3 == 1)                                                         # 包体是否使用压缩算法:01使用,02不使用
    nCur += 1
    nDecryptType = src[nCur] >> 4                                                                   # 解密算法(固定为AES解密): 05 aes解密 / 07 rsa解密
    nLenCookie = src[nCur] & 0xf                                                                    # cookie长度
    nCur += 1
    nCur += 4                                                                                       # 服务器版本(当前固定返回4字节0)
    uin = struct.unpack('>i', src[nCur:nCur+4])[0]                                                  # uin
    nCur += 4
    cookie_temp = src[nCur:nCur+nLenCookie]                                                         # cookie
    if cookie_temp and not(cookie_temp == Util.cookie):
        Util.cookie = cookie_temp                                                                   # 刷新cookie
    nCur += nLenCookie
    (nCgi, nCur) = decoder._DecodeVarint(src, nCur)                                                 # cgi type
    (nLenProtobuf, nCur) = decoder._DecodeVarint(src, nCur)                                         # 压缩前protobuf长度
    (nLenCompressed, nCur) = decoder._DecodeVarint(src, nCur)                                       # 压缩后protobuf长度
    logger.debug('包头长度:{}\n是否使用压缩算法:{}\n解密算法:{}\ncookie长度:{}\nuin:{}\ncookie:{}\ncgi type:{}\nprotobuf长度:{}\n压缩后protobuf长度:{}'.format(
        nLenHeader, bUseCompressed, nDecryptType, nLenCookie, uin, str(Util.cookie), nCgi, nLenProtobuf, nLenCompressed))
    # 对包体aes解密解压缩
    body = src[nLenHeader:]                                                                         # 取包体数据
    if bUseCompressed:
        protobufData = Util.decompress_and_aesDecrypt(body, key)
    else:
        protobufData = Util.aesDecrypt(body, key)
    logger.debug('解密后数据:%s' % str(protobufData))
    return protobufData

# 登录组包函数 
Example #15
Source File: reflection_test.py    From go2mapillary with GNU General Public License v3.0 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #16
Source File: reflection_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #17
Source File: reflection_test.py    From botchallenge with MIT License 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #18
Source File: reflection_test.py    From sklearn-theano with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #19
Source File: reflection_test.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def ReadVarint(self):
    result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
    return result 
Example #20
Source File: from_binary.py    From pbtk with GNU General Public License v3.0 4 votes vote down vote up
def walk_binary(binr):
    if type(binr) == str:
        try:
            with open(binr, 'rb') as fd:
                binr = fd.read()
        except Exception:
            return
    
    # Search for:
    # ".proto" or ".protodevel", as part of the "name" (1) field
    cursor = 0
    while cursor < len(binr):
        cursor = binr.find(b'.proto', cursor)
        
        if cursor == -1:
            break
        cursor += len('.proto')
        cursor += (binr[cursor:cursor + 5] == b'devel') * 5
        
        # Search back for the (1, length-delimited) marker
        start = binr.rfind(b'\x0a', max(cursor - 1024, 0), cursor)
        
        if start > 0 and binr[start - 1] == 0x0a == (cursor - start - 1):
            start -= 1
        
        # Check whether length byte is coherent
        if start == -1:
            continue
        varint, end = _DecodeVarint(binr, start + 1)
        if cursor - end != varint:
            continue
        
        # Look just after for subsequent markers
        tags = b'\x12\x1a\x22\x2a\x32\x3a\x42\x4a\x50\x58\x62'
        if binr[cursor] not in tags:
            continue
        
        while cursor < len(binr) and binr[cursor] in tags:
            tags = tags[tags.index(binr[cursor]):]
            
            varint, end = _DecodeVarint(binr, cursor + 1)
            cursor = end + varint * (binr[cursor] & 0b111 == 2)
        
        # Parse descriptor
        proto = FileDescriptorProto()
        proto.ParseFromString(binr[start:cursor])
        
        # Convert to ascii
        yield descpb_to_proto(proto) 
Example #21
Source File: varint.py    From pystream-protobuf with MIT License 4 votes vote down vote up
def decode(input_file):
    """Output the decoded value to the standard output.

    Args:
        input_file (file handler):  input file handler.
    """
    print(decodeVarint(input_file.read(), 0)[0])