protolib.py revision 10107
13914Ssaidi@eecs.umich.edu#!/usr/bin/env python
23914Ssaidi@eecs.umich.edu
33914Ssaidi@eecs.umich.edu# Copyright (c) 2013 ARM Limited
43914Ssaidi@eecs.umich.edu# All rights reserved
53914Ssaidi@eecs.umich.edu#
63914Ssaidi@eecs.umich.edu# The license below extends only to copyright in the software and shall
73914Ssaidi@eecs.umich.edu# not be construed as granting a license to any other intellectual
83914Ssaidi@eecs.umich.edu# property including but not limited to intellectual property relating
93914Ssaidi@eecs.umich.edu# to a hardware implementation of the functionality of the software
103914Ssaidi@eecs.umich.edu# licensed hereunder.  You may use the software subject to the license
113914Ssaidi@eecs.umich.edu# terms below provided that you ensure that this notice is replicated
123914Ssaidi@eecs.umich.edu# unmodified and in its entirety in all distributions of the software,
133914Ssaidi@eecs.umich.edu# modified or unmodified, in source code or in binary form.
143914Ssaidi@eecs.umich.edu#
153914Ssaidi@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
163914Ssaidi@eecs.umich.edu# modification, are permitted provided that the following conditions are
173914Ssaidi@eecs.umich.edu# met: redistributions of source code must retain the above copyright
183914Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
193914Ssaidi@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
203914Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
213914Ssaidi@eecs.umich.edu# documentation and/or other materials provided with the distribution;
223914Ssaidi@eecs.umich.edu# neither the name of the copyright holders nor the names of its
233914Ssaidi@eecs.umich.edu# contributors may be used to endorse or promote products derived from
243914Ssaidi@eecs.umich.edu# this software without specific prior written permission.
253914Ssaidi@eecs.umich.edu#
263914Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
273914Ssaidi@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
283914Ssaidi@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
293914Ssaidi@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
303914Ssaidi@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
313914Ssaidi@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
323914Ssaidi@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
333914Ssaidi@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
343914Ssaidi@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
353914Ssaidi@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
363914Ssaidi@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
373914Ssaidi@eecs.umich.edu#
383914Ssaidi@eecs.umich.edu# Copyright 2008 Google Inc.  All rights reserved.
393914Ssaidi@eecs.umich.edu# http://code.google.com/p/protobuf/
403914Ssaidi@eecs.umich.edu#
413914Ssaidi@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
423914Ssaidi@eecs.umich.edu# modification, are permitted provided that the following conditions are
433914Ssaidi@eecs.umich.edu# met:
443914Ssaidi@eecs.umich.edu#
453914Ssaidi@eecs.umich.edu#     * Redistributions of source code must retain the above copyright
463914Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer.
473914Ssaidi@eecs.umich.edu#     * Redistributions in binary form must reproduce the above
483914Ssaidi@eecs.umich.edu# copyright notice, this list of conditions and the following disclaimer
493914Ssaidi@eecs.umich.edu# in the documentation and/or other materials provided with the
503914Ssaidi@eecs.umich.edu# distribution.
513914Ssaidi@eecs.umich.edu#     * Neither the name of Google Inc. nor the names of its
523943Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
533914Ssaidi@eecs.umich.edu# this software without specific prior written permission.
543914Ssaidi@eecs.umich.edu#
553914Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
563914Ssaidi@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
573943Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
583943Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
593943Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
603914Ssaidi@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
613914Ssaidi@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
623914Ssaidi@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
633914Ssaidi@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
643914Ssaidi@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
653914Ssaidi@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
663914Ssaidi@eecs.umich.edu#
673914Ssaidi@eecs.umich.edu# Authors: Andreas Hansson
683914Ssaidi@eecs.umich.edu#          Radhika Jagtap
693914Ssaidi@eecs.umich.edu
703914Ssaidi@eecs.umich.edu# This file is a library of commonly used functions used when interfacing
713914Ssaidi@eecs.umich.edu# with protobuf python messages. For eg, the decode scripts for different
723914Ssaidi@eecs.umich.edu# types of proto objects can use the same function to decode a single message
733914Ssaidi@eecs.umich.edu
743914Ssaidi@eecs.umich.eduimport struct
753914Ssaidi@eecs.umich.edu
763914Ssaidi@eecs.umich.edudef DecodeVarint(in_file):
773914Ssaidi@eecs.umich.edu    """
783914Ssaidi@eecs.umich.edu    The decoding of the Varint32 is copied from
793914Ssaidi@eecs.umich.edu    google.protobuf.internal.decoder and is only repeated here to
803914Ssaidi@eecs.umich.edu    avoid depending on the internal functions in the library. If the
813914Ssaidi@eecs.umich.edu    end of file is reached, return (0, 0).
823914Ssaidi@eecs.umich.edu    """
833914Ssaidi@eecs.umich.edu    result = 0
843914Ssaidi@eecs.umich.edu    shift = 0
853914Ssaidi@eecs.umich.edu    pos = 0
863914Ssaidi@eecs.umich.edu    # Use a 32-bit mask
873914Ssaidi@eecs.umich.edu    mask = 0xffffffff
883914Ssaidi@eecs.umich.edu    while 1:
893914Ssaidi@eecs.umich.edu        c = in_file.read(1)
903914Ssaidi@eecs.umich.edu        if len(c) == 0:
913943Sbinkertn@umich.edu            return (0, 0)
923914Ssaidi@eecs.umich.edu        b = struct.unpack('<B', c)[0]
933914Ssaidi@eecs.umich.edu        result |= ((b & 0x7f) << shift)
943914Ssaidi@eecs.umich.edu        pos += 1
953914Ssaidi@eecs.umich.edu        if not (b & 0x80):
963914Ssaidi@eecs.umich.edu            if result > 0x7fffffffffffffff:
973914Ssaidi@eecs.umich.edu                result -= (1 << 64)
983914Ssaidi@eecs.umich.edu                result |= ~mask
993914Ssaidi@eecs.umich.edu            else:
1003914Ssaidi@eecs.umich.edu                result &= mask
1013943Sbinkertn@umich.edu                return (result, pos)
1023914Ssaidi@eecs.umich.edu            shift += 7
1033914Ssaidi@eecs.umich.edu            if shift >= 64:
1043914Ssaidi@eecs.umich.edu                raise IOError('Too many bytes when decoding varint.')
1053914Ssaidi@eecs.umich.edu
1063914Ssaidi@eecs.umich.edudef decodeMessage(in_file, message):
1073914Ssaidi@eecs.umich.edu    """
1083914Ssaidi@eecs.umich.edu    Attempt to read a message from the file and decode it. Return
1093914Ssaidi@eecs.umich.edu    False if no message could be read.
1103914Ssaidi@eecs.umich.edu    """
1113914Ssaidi@eecs.umich.edu    try:
1123914Ssaidi@eecs.umich.edu        size, pos = DecodeVarint(in_file)
1133914Ssaidi@eecs.umich.edu        if size == 0:
1143914Ssaidi@eecs.umich.edu            return False
1153914Ssaidi@eecs.umich.edu        buf = in_file.read(size)
1163914Ssaidi@eecs.umich.edu        message.ParseFromString(buf)
1173914Ssaidi@eecs.umich.edu        return True
118    except IOError:
119        return False
120
121def EncodeVarint(out_file, value):
122  """
123  The encoding of the Varint32 is copied from
124  google.protobuf.internal.encoder and is only repeated here to
125  avoid depending on the internal functions in the library.
126  """
127  bits = value & 0x7f
128  value >>= 7
129  while value:
130    out_file.write(struct.pack('<B', 0x80|bits))
131    bits = value & 0x7f
132    value >>= 7
133  out_file.write(struct.pack('<B', bits))
134
135def encodeMessage(out_file, message):
136    """
137    Encoded a message with the length prepended as a 32-bit varint.
138    """
139    out = message.SerializeToString()
140    EncodeVarint(out_file, len(out))
141    out_file.write(out)
142