blob: cba8c8659f6c63688017736b57b68ce04869a254 [file] [log] [blame]
Rich Lane15cbe842013-04-26 16:04:11 -07001:: # Copyright 2013, Big Switch Networks, Inc.
2:: #
3:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4:: # the following special exception:
5:: #
6:: # LOXI Exception
7:: #
8:: # As a special exception to the terms of the EPL, you may distribute libraries
9:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14:: #
15:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16:: #
17:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18:: # a copy of the EPL at:
19:: #
20:: # http://www.eclipse.org/legal/epl-v10.html
21:: #
22:: # Unless required by applicable law or agreed to in writing, software
23:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25:: # EPL for the specific language governing permissions and limitations
26:: # under the EPL.
27::
28:: include('_copyright.py')
29"""
30Utility functions independent of the protocol version
31"""
32
33:: include('_autogen.py')
34
35import loxi
36import struct
37
38def unpack_array(deserializer, element_size, buf):
39 """
40 Deserialize an array of fixed length elements.
41 The deserializer function should take a buffer and return the new object.
42 """
43 if len(buf) % element_size != 0: raise loxi.ProtocolError("invalid array length")
44 n = len(buf) / element_size
45 return [deserializer(buffer(buf, i*element_size, element_size)) for i in range(n)]
46
47def unpack_list(deserializer, length_fmt, buf, extra_len=0):
48 """
49 Deserialize a list of variable-length entries.
50 'length_fmt' is a struct format string with exactly one non-padding format
51 character that returns the length of the given element, minus extra_len.
52 The deserializer function should take a buffer and return the new object.
53 """
54 entries = []
55 offset = 0
56 length_struct = struct.Struct(length_fmt)
57 n = len(buf)
58 while offset < n:
59 if offset + length_struct.size > len(buf): raise loxi.ProtocolError("entry header overruns list length")
60 length, = length_struct.unpack_from(buf, offset)
61 length += extra_len
62 if length < length_struct.size: raise loxi.ProtocolError("entry length is less than the header length")
63 if offset + length > len(buf): raise loxi.ProtocolError("entry length overruns list length")
64 entries.append(deserializer(buffer(buf, offset, length)))
65 offset += length
66 return entries