samba-tool tests: add tests for userPassword
[Samba.git] / python / samba / ndr.py
blobd84e9d6f9e04b9b74fb9078267cade74597f3baf
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("%r is not a NDR object" % object)
56 return ndr_print()
58 def ndr_pack_in(object, bigendian=False, ndr64=False):
59 """Pack the input of an NDR function object.
61 :param object: Object to pack
62 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
63 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
64 :return: String object with marshalled object.
65 """
66 ndr_pack_in_fn = getattr(object, "__ndr_pack_in__", None)
67 if ndr_pack_in_fn is None:
68 raise TypeError("%r is not a NDR function object" % object)
69 return ndr_pack_in_fn(bigendian=bigendian, ndr64=ndr64)
72 def ndr_unpack_in(object, data, bigendian=False, ndr64=False, allow_remaining=False):
73 """Unpack the input of an NDR function object.
75 :param cls: Class of the object to unpack
76 :param data: Buffer to unpack
77 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
78 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
79 :param allow_remaining: allows remaining data at the end (default=False)
80 :return: Unpacked object
81 """
82 ndr_unpack_in_fn = getattr(object, "__ndr_unpack_in__", None)
83 if ndr_unpack_in_fn is None:
84 raise TypeError("%r is not a NDR function object" % object)
85 ndr_unpack_in_fn(data, bigendian=bigendian, ndr64=ndr64,
86 allow_remaining=allow_remaining)
87 return object
90 def ndr_print_in(object):
91 ndr_print_in_fn = getattr(object, "__ndr_print_in__", None)
92 if ndr_print_in_fn is None:
93 raise TypeError("%r is not a NDR function object" % object)
94 return ndr_print_in_fn()
97 def ndr_pack_out(object, bigendian=False, ndr64=False):
98 """Pack the output of an NDR function object.
100 :param object: Object to pack
101 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
102 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
103 :return: String object with marshalled object.
105 ndr_pack_out_fn = getattr(object, "__ndr_pack_out__", None)
106 if ndr_pack_out_fn is None:
107 raise TypeError("%r is not a NDR function object" % object)
108 return ndr_pack_out_fn(bigendian=bigendian, ndr64=ndr64)
111 def ndr_unpack_out(object, data, bigendian=False, ndr64=False, allow_remaining=False):
112 """Unpack the output of an NDR function object.
114 :param cls: Class of the object to unpack
115 :param data: Buffer to unpack
116 :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
117 :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
118 :param allow_remaining: allows remaining data at the end (default=False)
119 :return: Unpacked object
121 ndr_unpack_out_fn = getattr(object, "__ndr_unpack_out__", None)
122 if ndr_unpack_out_fn is None:
123 raise TypeError("%r is not a NDR function object" % object)
124 ndr_unpack_out_fn(data, bigendian=bigendian, ndr64=ndr64,
125 allow_remaining=allow_remaining)
126 return object
129 def ndr_print_out(object):
130 ndr_print_out_fn = getattr(object, "__ndr_print_out__", None)
131 if ndr_print_out_fn is None:
132 raise TypeError("%r is not a NDR function object" % object)
133 return ndr_print_out_fn()