1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: urp_unmarshal.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _URP_UNMARSHAL_HXX_
31 #define _URP_UNMARSHAL_HXX_
35 #include <rtl/byteseq.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <bridges/remote/context.h>
39 #ifndef _BRIDGES_REMOTE_HELPER_HXX_
40 #include <bridges/remote/helper.hxx>
42 #include <com/sun/star/uno/Type.hxx>
43 #include "urp_bridgeimpl.hxx"
45 typedef struct _uno_Environment uno_Environment
;
46 struct remote_Interface
;
51 extern char g_bSystemIsLittleEndian
;
53 struct urp_BridgeImpl
;
54 void SAL_CALL
urp_releaseRemoteCallback(
55 remote_Interface
*pRemoteI
,rtl_uString
*pOid
,
56 typelib_TypeDescriptionReference
*pTypeRef
,
57 uno_Environment
*pEnvRemote
);
62 typelib_TypeDescription
* pType
;
63 bool bMustBeConstructed
;
66 : pDest( 0 ), pType( 0 ), bMustBeConstructed( false ) {}
67 UnpackItem( void * d
, typelib_TypeDescription
* t
, bool b
= false )
68 : pDest( d
), pType( t
), bMustBeConstructed( b
) {}
71 typedef std::stack
< UnpackItem
> UnpackItemStack
;
72 typedef std::vector
< typelib_TypeDescription
* > TypeDescVector
;
78 struct urp_BridgeImpl
*,
79 uno_Environment
*pEnvRemote
,
80 remote_createStubFunc callback
);
83 inline sal_Bool
finished()
84 { return m_base
+ m_nLength
== m_pos
; }
85 inline sal_uInt32
getPos()
86 { return (sal_uInt32
) (m_pos
- m_base
); }
88 inline sal_Bool
setSize( sal_Int32 nSize
);
90 sal_Bool
unpack( void *pDest
, typelib_TypeDescription
*pType
);
91 inline sal_Bool
unpackCompressedSize( sal_Int32
*pData
);
92 inline sal_Bool
unpackInt8( void *pDest
);
93 inline sal_Bool
unpackString( void *pDest
);
94 inline sal_Bool
unpackInt16( void *pDest
);
95 inline sal_Bool
unpackInt32( void *pDest
);
96 sal_Bool
unpackType( void *pDest
);
98 inline sal_Bool
unpackAny( void *pDest
);
99 sal_Bool
unpackOid( rtl_uString
**ppOid
);
100 sal_Bool
unpackTid( sal_Sequence
**ppThreadId
);
102 sal_Int8
*getBuffer()
104 inline sal_Bool
isSystemLittleEndian()
105 { return g_bSystemIsLittleEndian
; }
108 inline sal_Bool
checkOverflow( sal_Int32 nNextMem
);
110 UnpackItemStack m_aItemsToUnpack
;
111 TypeDescVector m_aTypesToRelease
;
113 sal_Int32 m_nBufferSize
;
118 remote_createStubFunc m_callback
;
119 uno_Environment
*m_pEnvRemote
;
120 urp_BridgeImpl
*m_pBridgeImpl
;
123 inline sal_Bool
Unmarshal::setSize( sal_Int32 nSize
)
125 if( nSize
> m_nBufferSize
)
127 // adjust buffer size and length.
129 (sal_Int8
*)rtl_reallocateMemory (m_base
, sal_Size(nSize
));
133 m_nLength
= m_nBufferSize
= nSize
;
138 // adjust buffer length, only.
142 // reset buffer position, and leave.
144 return (m_nLength
== nSize
);
147 inline sal_Bool
Unmarshal::checkOverflow( sal_Int32 nNextMem
)
149 sal_Bool bOverflow
= nNextMem
< 0 ||
150 (((sal_Int32
)( m_pos
- m_base
)) + nNextMem
) > m_nLength
;
152 m_pBridgeImpl
->addError( "message too short" );
157 inline sal_Bool
Unmarshal::unpackInt8( void *pDest
)
159 sal_Bool bReturn
= ! checkOverflow( 1 );
162 *((sal_Int8
*)pDest
) = *m_pos
;
167 *((sal_Int8
*)pDest
) = 0;
172 inline sal_Bool
Unmarshal::unpackInt32( void *pDest
)
174 sal_uInt32
*p
= ( sal_uInt32
* ) pDest
;
175 sal_Bool bReturn
= ! checkOverflow(4);
178 if( isSystemLittleEndian() )
180 ((sal_Int8
*) p
)[3] = m_pos
[0];
181 ((sal_Int8
*) p
)[2] = m_pos
[1];
182 ((sal_Int8
*) p
)[1] = m_pos
[2];
183 ((sal_Int8
*) p
)[0] = m_pos
[3];
187 ((sal_Int8
*) p
)[3] = m_pos
[3];
188 ((sal_Int8
*) p
)[2] = m_pos
[2];
189 ((sal_Int8
*) p
)[1] = m_pos
[1];
190 ((sal_Int8
*) p
)[0] = m_pos
[0];
201 inline sal_Bool
Unmarshal::unpackInt16( void *pDest
)
203 sal_uInt16
*p
= ( sal_uInt16
* ) pDest
;
205 sal_Bool bReturn
= ! checkOverflow( 2 );
208 if( isSystemLittleEndian() )
210 ((sal_Int8
*) p
)[1] = m_pos
[0];
211 ((sal_Int8
*) p
)[0] = m_pos
[1];
215 ((sal_Int8
*) p
)[1] = m_pos
[1];
216 ((sal_Int8
*) p
)[0] = m_pos
[0];
228 inline sal_Bool
Unmarshal::unpackString( void *pDest
)
231 sal_Bool bReturn
= unpackCompressedSize( &nLength
);
233 bReturn
= bReturn
&& ! checkOverflow( nLength
);
236 *(rtl_uString
**) pDest
= 0;
237 rtl_string2UString( (rtl_uString
**) pDest
, (const sal_Char
* )m_pos
, nLength
,
238 RTL_TEXTENCODING_UTF8
, OSTRING_TO_OUSTRING_CVTFLAGS
);
243 *(rtl_uString
** ) pDest
= 0;
244 rtl_uString_new( (rtl_uString
**) pDest
);
249 inline sal_Bool
Unmarshal::unpackCompressedSize( sal_Int32
*pData
)
252 sal_Bool bReturn
= unpackInt8( &n8Size
);
257 unpackInt32( pData
);
261 *pData
= (sal_Int32
) n8Size
;
267 inline sal_Bool
Unmarshal::unpackAny( void *pDest
)
269 typelib_TypeDescriptionReference
*pTypeRef
=
270 * typelib_static_type_getByTypeClass( typelib_TypeClass_ANY
);
272 typelib_TypeDescription
* pTD
= 0;
273 typelib_typedescriptionreference_getDescription( &pTD
, pTypeRef
);
275 sal_Bool bReturn
= unpack( pDest
, pTD
);
277 typelib_typedescription_release( pTD
);