pyldb: avoid segfault when adding an element with no name
[nivanova/samba-autobuild/.git] / python / samba / ndr.py
1 # -*- coding: utf-8 -*-
2
3 # Unix SMB/CIFS implementation.
4 # Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20
21 """Network Data Representation (NDR) marshalling and unmarshalling."""
22
23
24 def ndr_pack(object):
25     """Pack a NDR object.
26
27     :param object: Object to pack
28     :return: String object with marshalled object.
29     """
30     ndr_pack = getattr(object, "__ndr_pack__", None)
31     if ndr_pack is None:
32         raise TypeError("%r is not a NDR object" % object)
33     return ndr_pack()
34
35
36 def ndr_unpack(cls, data, allow_remaining=False):
37     """NDR unpack an object.
38
39     :param cls: Class of the object to unpack
40     :param data: Buffer to unpack
41     :param allow_remaining: allows remaining data at the end (default=False)
42     :return: Unpacked object
43     """
44     object = cls()
45     ndr_unpack = getattr(object, "__ndr_unpack__", None)
46     if ndr_unpack is None:
47         raise TypeError("%r is not a NDR object" % object)
48     ndr_unpack(data, allow_remaining=allow_remaining)
49     return object
50
51
52 def ndr_print(object):
53     ndr_print = getattr(object, "__ndr_print__", None)
54     if ndr_print is None:
55         raise TypeError("%r is not a NDR object" % object)
56     return ndr_print()
57
58
59 def ndr_pack_in(object, bigendian=False, ndr64=False):
60     """Pack the input of an NDR function object.
61
62     :param object: Object to pack
63     :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
64     :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
65     :return: String object with marshalled object.
66     """
67     ndr_pack_in_fn = getattr(object, "__ndr_pack_in__", None)
68     if ndr_pack_in_fn is None:
69         raise TypeError("%r is not a NDR function object" % object)
70     return ndr_pack_in_fn(bigendian=bigendian, ndr64=ndr64)
71
72
73 def ndr_unpack_in(object, data, bigendian=False, ndr64=False, allow_remaining=False):
74     """Unpack the input of an NDR function object.
75
76     :param cls: Class of the object to unpack
77     :param data: Buffer to unpack
78     :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
79     :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
80     :param allow_remaining: allows remaining data at the end (default=False)
81     :return: Unpacked object
82     """
83     ndr_unpack_in_fn = getattr(object, "__ndr_unpack_in__", None)
84     if ndr_unpack_in_fn is None:
85         raise TypeError("%r is not a NDR function object" % object)
86     ndr_unpack_in_fn(data, bigendian=bigendian, ndr64=ndr64,
87                      allow_remaining=allow_remaining)
88     return object
89
90
91 def ndr_print_in(object):
92     ndr_print_in_fn = getattr(object, "__ndr_print_in__", None)
93     if ndr_print_in_fn is None:
94         raise TypeError("%r is not a NDR function object" % object)
95     return ndr_print_in_fn()
96
97
98 def ndr_pack_out(object, bigendian=False, ndr64=False):
99     """Pack the output of an NDR function object.
100
101     :param object: Object to pack
102     :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
103     :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
104     :return: String object with marshalled object.
105     """
106     ndr_pack_out_fn = getattr(object, "__ndr_pack_out__", None)
107     if ndr_pack_out_fn is None:
108         raise TypeError("%r is not a NDR function object" % object)
109     return ndr_pack_out_fn(bigendian=bigendian, ndr64=ndr64)
110
111
112 def ndr_unpack_out(object, data, bigendian=False, ndr64=False, allow_remaining=False):
113     """Unpack the output of an NDR function object.
114
115     :param cls: Class of the object to unpack
116     :param data: Buffer to unpack
117     :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
118     :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
119     :param allow_remaining: allows remaining data at the end (default=False)
120     :return: Unpacked object
121     """
122     ndr_unpack_out_fn = getattr(object, "__ndr_unpack_out__", None)
123     if ndr_unpack_out_fn is None:
124         raise TypeError("%r is not a NDR function object" % object)
125     ndr_unpack_out_fn(data, bigendian=bigendian, ndr64=ndr64,
126                       allow_remaining=allow_remaining)
127     return object
128
129
130 def ndr_print_out(object):
131     ndr_print_out_fn = getattr(object, "__ndr_print_out__", None)
132     if ndr_print_out_fn is None:
133         raise TypeError("%r is not a NDR function object" % object)
134     return ndr_print_out_fn()