2008-05-24 21:50:09 +04:00
# -*- coding: utf-8 -*-
2008-05-24 20:57:15 +04:00
# Unix SMB/CIFS implementation.
# Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
2010-11-28 16:09:30 +03:00
#
2008-05-24 20:57:15 +04:00
# 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.
2010-11-28 16:09:30 +03:00
#
2008-05-24 20:57:15 +04:00
# 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.
2010-11-28 16:09:30 +03:00
#
2008-05-24 20:57:15 +04:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2010-11-28 16:09:30 +03:00
""" Network Data Representation (NDR) marshalling and unmarshalling. """
2008-05-24 20:57:15 +04:00
def ndr_pack ( object ) :
2010-11-28 16:09:30 +03:00
""" Pack a NDR object.
2011-09-13 03:27:50 +04:00
2010-11-28 16:09:30 +03:00
: 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 ( )
2008-05-24 20:57:15 +04:00
2012-01-05 19:34:02 +04:00
def ndr_unpack ( cls , data , allow_remaining = False ) :
2010-11-28 16:09:30 +03:00
""" NDR unpack an object.
: param cls : Class of the object to unpack
: param data : Buffer to unpack
2012-01-05 19:34:02 +04:00
: param allow_remaining : allows remaining data at the end ( default = False )
2010-11-28 16:09:30 +03:00
: return : Unpacked object
"""
2008-05-24 20:57:15 +04:00
object = cls ( )
2016-09-13 06:51:42 +03:00
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 )
2008-05-24 20:57:15 +04:00
return object
2010-08-23 01:52:25 +04:00
2010-11-28 16:09:30 +03:00
2010-08-23 01:52:25 +04:00
def ndr_print ( object ) :
2016-09-13 06:51:42 +03:00
ndr_print = getattr ( object , " __ndr_print__ " , None )
if ndr_print is None :
raise TypeError ( " %r is not a NDR object " % object )
return ndr_print ( )
2016-09-13 06:51:42 +03:00
2018-07-30 09:20:39 +03:00
2016-09-13 06:51:42 +03:00
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 ,
2018-07-30 09:15:34 +03:00
allow_remaining = allow_remaining )
2016-09-13 06:51:42 +03:00
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 ,
2018-07-30 09:15:34 +03:00
allow_remaining = allow_remaining )
2016-09-13 06:51:42 +03:00
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 ( )