Update ooo320-m1
[ooovba.git] / bridges / source / remote / urp / urp_unmarshal.hxx
blob954e13160e804365ce1192bba03ae8b248fb3b11
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: urp_unmarshal.hxx,v $
10 * $Revision: 1.9 $
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_
33 #include <stack>
34 #include <vector>
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>
41 #endif
42 #include <com/sun/star/uno/Type.hxx>
43 #include "urp_bridgeimpl.hxx"
45 typedef struct _uno_Environment uno_Environment;
46 struct remote_Interface;
48 namespace bridges_urp
51 extern char g_bSystemIsLittleEndian;
52 class ThreadId;
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 );
59 struct UnpackItem
61 void * pDest;
62 typelib_TypeDescription * pType;
63 bool bMustBeConstructed;
65 UnpackItem()
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;
74 class Unmarshal
76 public:
77 Unmarshal(
78 struct urp_BridgeImpl *,
79 uno_Environment *pEnvRemote,
80 remote_createStubFunc callback );
81 ~Unmarshal();
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()
103 { return m_base; }
104 inline sal_Bool isSystemLittleEndian()
105 { return g_bSystemIsLittleEndian; }
107 private:
108 inline sal_Bool checkOverflow( sal_Int32 nNextMem );
110 UnpackItemStack m_aItemsToUnpack;
111 TypeDescVector m_aTypesToRelease;
113 sal_Int32 m_nBufferSize;
114 sal_Int8 *m_base;
115 sal_Int8 *m_pos;
116 sal_Int32 m_nLength;
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.
128 sal_Int8 * base =
129 (sal_Int8*)rtl_reallocateMemory (m_base, sal_Size(nSize));
130 if (base != 0)
132 m_base = base;
133 m_nLength = m_nBufferSize = nSize;
136 else
138 // adjust buffer length, only.
139 m_nLength = nSize;
142 // reset buffer position, and leave.
143 m_pos = m_base;
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;
151 if( bOverflow )
152 m_pBridgeImpl->addError( "message too short" );
153 return bOverflow;
157 inline sal_Bool Unmarshal::unpackInt8( void *pDest )
159 sal_Bool bReturn = ! checkOverflow( 1 );
160 if( bReturn )
162 *((sal_Int8*)pDest ) = *m_pos;
163 m_pos++;
165 else
167 *((sal_Int8*)pDest ) = 0;
169 return bReturn;
172 inline sal_Bool Unmarshal::unpackInt32( void *pDest )
174 sal_uInt32 *p = ( sal_uInt32 * ) pDest;
175 sal_Bool bReturn = ! checkOverflow(4);
176 if( bReturn )
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];
185 else
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];
192 m_pos += 4;
194 else
196 *p = 0;
198 return bReturn;
201 inline sal_Bool Unmarshal::unpackInt16( void *pDest )
203 sal_uInt16 *p = ( sal_uInt16 * ) pDest;
205 sal_Bool bReturn = ! checkOverflow( 2 );
206 if( bReturn )
208 if( isSystemLittleEndian() )
210 ((sal_Int8*) p )[1] = m_pos[0];
211 ((sal_Int8*) p )[0] = m_pos[1];
213 else
215 ((sal_Int8*) p )[1] = m_pos[1];
216 ((sal_Int8*) p )[0] = m_pos[0];
218 m_pos ++;
219 m_pos ++;
221 else
223 *p = 0;
225 return bReturn;
228 inline sal_Bool Unmarshal::unpackString( void *pDest )
230 sal_Int32 nLength;
231 sal_Bool bReturn = unpackCompressedSize( &nLength );
233 bReturn = bReturn && ! checkOverflow( nLength );
234 if( bReturn )
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 );
239 m_pos += nLength;
241 else
243 *(rtl_uString ** ) pDest = 0;
244 rtl_uString_new( (rtl_uString **) pDest );
246 return bReturn;
249 inline sal_Bool Unmarshal::unpackCompressedSize( sal_Int32 *pData )
251 sal_uInt8 n8Size;
252 sal_Bool bReturn = unpackInt8( &n8Size );
253 if( bReturn )
255 if( n8Size == 0xff )
257 unpackInt32( pData );
259 else
261 *pData = (sal_Int32 ) n8Size;
264 return bReturn;
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 );
279 return bReturn;
283 #endif