ctdb: Save a few lines with talloc_zero()
[samba4-gss.git] / python / samba / ndr.py
blob4207ee2a3184e90a6eb53450b290f4d70bec9d85
1 # -*- coding: utf-8 -*-
3 # Unix SMB/CIFS implementation.
4 # Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
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.
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.
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/>.
21 """Network Data Representation (NDR) marshalling and unmarshalling."""
24 def ndr_pack(object):
25 """Pack a NDR object.
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()
36 def ndr_unpack(cls, data, allow_remaining=False):
37 """NDR unpack an object.
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
52 def ndr_print(object):
53 ndr_print = getattr(object, "__ndr_print__", None)
54 if ndr_print is None:
55 raise TypeError(f"{object} is not a NDR object")
56 return ndr_print()
59 def ndr_deepcopy(object):
60 """Create a deep copy of a NDR object, using pack/unpack
62 :param object: Object to copy
63 :return: The object copy
64 """
65 ndr_pack = getattr(object, "__ndr_pack__", None)
66 if ndr_pack is None:
67 raise TypeError("%r is not a NDR object" % object)
68 data = ndr_pack()
69 cls = type(object)
70 copy = cls()
71 ndr_unpack = getattr(copy, "__ndr_unpack__", None)
72 if ndr_unpack is None:
73 raise TypeError("%r is not a NDR object" % copy)
74 ndr_unpack(data, allow_remaining=False)
75 return copy
78 def ndr_pack_in(object, bigendian=False, ndr64=False):
79 """Pack the input of an NDR function object.
81 :param object: Object to pack
82 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
83 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
84 :return: String object with marshalled object.
85 """
86 ndr_pack_in_fn = getattr(object, "__ndr_pack_in__", None)
87 if ndr_pack_in_fn is None:
88 raise TypeError("%r is not a NDR function object" % object)
89 return ndr_pack_in_fn(bigendian=bigendian, ndr64=ndr64)
92 def ndr_unpack_in(object, data, bigendian=False, ndr64=False, allow_remaining=False):
93 """Unpack the input of an NDR function object.
95 :param cls: Class of the object to unpack
96 :param data: Buffer to unpack
97 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
98 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
99 :param allow_remaining: allows remaining data at the end (default=False)
100 :return: Unpacked object
102 ndr_unpack_in_fn = getattr(object, "__ndr_unpack_in__", None)
103 if ndr_unpack_in_fn is None:
104 raise TypeError("%r is not a NDR function object" % object)
105 ndr_unpack_in_fn(data, bigendian=bigendian, ndr64=ndr64,
106 allow_remaining=allow_remaining)
107 return object
110 def ndr_print_in(object):
111 ndr_print_in_fn = getattr(object, "__ndr_print_in__", None)
112 if ndr_print_in_fn is None:
113 raise TypeError("%r is not a NDR function object" % object)
114 return ndr_print_in_fn()
117 def ndr_pack_out(object, bigendian=False, ndr64=False):
118 """Pack the output of an NDR function object.
120 :param object: Object to pack
121 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
122 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
123 :return: String object with marshalled object.
125 ndr_pack_out_fn = getattr(object, "__ndr_pack_out__", None)
126 if ndr_pack_out_fn is None:
127 raise TypeError("%r is not a NDR function object" % object)
128 return ndr_pack_out_fn(bigendian=bigendian, ndr64=ndr64)
131 def ndr_unpack_out(object, data, bigendian=False, ndr64=False, allow_remaining=False):
132 """Unpack the output of an NDR function object.
134 :param cls: Class of the object to unpack
135 :param data: Buffer to unpack
136 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
137 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
138 :param allow_remaining: allows remaining data at the end (default=False)
139 :return: Unpacked object
141 ndr_unpack_out_fn = getattr(object, "__ndr_unpack_out__", None)
142 if ndr_unpack_out_fn is None:
143 raise TypeError("%r is not a NDR function object" % object)
144 ndr_unpack_out_fn(data, bigendian=bigendian, ndr64=ndr64,
145 allow_remaining=allow_remaining)
146 return object
149 def ndr_print_out(object):
150 ndr_print_out_fn = getattr(object, "__ndr_print_out__", None)
151 if ndr_print_out_fn is None:
152 raise TypeError("%r is not a NDR function object" % object)
153 return ndr_print_out_fn()