summaryrefslogtreecommitdiffstats
path: root/python/samba/ndr.py
blob: 4207ee2a3184e90a6eb53450b290f4d70bec9d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-

# Unix SMB/CIFS implementation.
# Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


"""Network Data Representation (NDR) marshalling and unmarshalling."""


def ndr_pack(object):
    """Pack a NDR object.

    :param object: Object to pack
    :return: String object with marshalled object.
    """
    ndr_pack = getattr(object, "__ndr_pack__", None)
    if ndr_pack is None:
        raise TypeError("%r is not a NDR object" % object)
    return ndr_pack()


def ndr_unpack(cls, data, allow_remaining=False):
    """NDR unpack an object.

    :param cls: Class of the object to unpack
    :param data: Buffer to unpack
    :param allow_remaining: allows remaining data at the end (default=False)
    :return: Unpacked object
    """
    object = cls()
    ndr_unpack = getattr(object, "__ndr_unpack__", None)
    if ndr_unpack is None:
        raise TypeError("%r is not a NDR object" % object)
    ndr_unpack(data, allow_remaining=allow_remaining)
    return object


def ndr_print(object):
    ndr_print = getattr(object, "__ndr_print__", None)
    if ndr_print is None:
        raise TypeError(f"{object} is not a NDR object")
    return ndr_print()


def ndr_deepcopy(object):
    """Create a deep copy of a NDR object, using pack/unpack

    :param object: Object to copy
    :return: The object copy
    """
    ndr_pack = getattr(object, "__ndr_pack__", None)
    if ndr_pack is None:
        raise TypeError("%r is not a NDR object" % object)
    data = ndr_pack()
    cls = type(object)
    copy = cls()
    ndr_unpack = getattr(copy, "__ndr_unpack__", None)
    if ndr_unpack is None:
        raise TypeError("%r is not a NDR object" % copy)
    ndr_unpack(data, allow_remaining=False)
    return copy


def ndr_pack_in(object, bigendian=False, ndr64=False):
    """Pack the input of an NDR function object.

    :param object: Object to pack
    :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
    :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
    :return: String object with marshalled object.
    """
    ndr_pack_in_fn = getattr(object, "__ndr_pack_in__", None)
    if ndr_pack_in_fn is None:
        raise TypeError("%r is not a NDR function object" % object)
    return ndr_pack_in_fn(bigendian=bigendian, ndr64=ndr64)


def ndr_unpack_in(object, data, bigendian=False, ndr64=False, allow_remaining=False):
    """Unpack the input of an NDR function object.

    :param cls: Class of the object to unpack
    :param data: Buffer to unpack
    :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
    :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
    :param allow_remaining: allows remaining data at the end (default=False)
    :return: Unpacked object
    """
    ndr_unpack_in_fn = getattr(object, "__ndr_unpack_in__", None)
    if ndr_unpack_in_fn is None:
        raise TypeError("%r is not a NDR function object" % object)
    ndr_unpack_in_fn(data, bigendian=bigendian, ndr64=ndr64,
                     allow_remaining=allow_remaining)
    return object


def ndr_print_in(object):
    ndr_print_in_fn = getattr(object, "__ndr_print_in__", None)
    if ndr_print_in_fn is None:
        raise TypeError("%r is not a NDR function object" % object)
    return ndr_print_in_fn()


def ndr_pack_out(object, bigendian=False, ndr64=False):
    """Pack the output of an NDR function object.

    :param object: Object to pack
    :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
    :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
    :return: String object with marshalled object.
    """
    ndr_pack_out_fn = getattr(object, "__ndr_pack_out__", None)
    if ndr_pack_out_fn is None:
        raise TypeError("%r is not a NDR function object" % object)
    return ndr_pack_out_fn(bigendian=bigendian, ndr64=ndr64)


def ndr_unpack_out(object, data, bigendian=False, ndr64=False, allow_remaining=False):
    """Unpack the output of an NDR function object.

    :param cls: Class of the object to unpack
    :param data: Buffer to unpack
    :param bigendian: use LIBNDR_FLAG_BIGENDIAN (default=False)
    :param ndr64: use LIBNDR_FLAG_NDR64 (default=False)
    :param allow_remaining: allows remaining data at the end (default=False)
    :return: Unpacked object
    """
    ndr_unpack_out_fn = getattr(object, "__ndr_unpack_out__", None)
    if ndr_unpack_out_fn is None:
        raise TypeError("%r is not a NDR function object" % object)
    ndr_unpack_out_fn(data, bigendian=bigendian, ndr64=ndr64,
                      allow_remaining=allow_remaining)
    return object


def ndr_print_out(object):
    ndr_print_out_fn = getattr(object, "__ndr_print_out__", None)
    if ndr_print_out_fn is None:
        raise TypeError("%r is not a NDR function object" % object)
    return ndr_print_out_fn()