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: buffer.cxx,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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
38 const static UINT32 UP_LIMIT
=0xFFFFFF00L
;
40 // Der SbiBuffer wird in Inkrements von mindestens 16 Bytes erweitert.
41 // Dies ist notwendig, da viele Klassen von einer Pufferlaenge
42 // von x*16 Bytes ausgehen.
44 SbiBuffer::SbiBuffer( SbiParser
* p
, short n
)
47 n
= ( (n
+ 15 ) / 16 ) * 16;
56 SbiBuffer::~SbiBuffer()
61 // Rausreichen des Puffers
62 // Dies fuehrt zur Loeschung des Puffers!
64 char* SbiBuffer::GetBuffer()
72 // Test, ob der Puffer n Bytes aufnehmen kann.
73 // Im Zweifelsfall wird er vergroessert
75 BOOL
SbiBuffer::Check( USHORT n
)
78 if( ( static_cast<UINT32
>( nOff
)+ n
) > static_cast<UINT32
>( nSize
) )
83 while( nn
< n
) nn
= nn
+ nInc
;
85 if( ( static_cast<UINT32
>( nSize
) + nn
) > UP_LIMIT
) p
= NULL
;
86 else p
= new char [nSize
+ nn
];
89 pParser
->Error( SbERR_PROG_TOO_LARGE
);
91 delete[] pBuf
; pBuf
= NULL
;
96 if( nSize
) memcpy( p
, pBuf
, nSize
);
106 // Angleich des Puffers auf die uebergebene Byte-Grenze
108 void SbiBuffer::Align( INT32 n
)
111 UINT32 nn
=( ( nOff
+ n
) / n
) * n
;
115 if( Check( static_cast<USHORT
>(nn
) ) )
117 memset( pCur
, 0, nn
);
125 // Patch einer Location
127 void SbiBuffer::Patch( UINT32 off
, UINT32 val
)
129 if( ( off
+ sizeof( UINT32
) ) < nOff
)
131 UINT16 val1
= static_cast<UINT16
>( val
& 0xFFFF );
132 UINT16 val2
= static_cast<UINT16
>( val
>> 16 );
133 BYTE
* p
= (BYTE
*) pBuf
+ off
;
134 *p
++ = (char) ( val1
& 0xFF );
135 *p
++ = (char) ( val1
>> 8 );
136 *p
++ = (char) ( val2
& 0xFF );
137 *p
= (char) ( val2
>> 8 );
141 // Forward References auf Labels und Prozeduren
142 // bauen eine Kette auf. Der Anfang der Kette ist beim uebergebenen
143 // Parameter, das Ende der Kette ist 0.
145 void SbiBuffer::Chain( UINT32 off
)
151 UINT32 val1
= (nOff
& 0xFFFF);
152 UINT32 val2
= (nOff
>> 16);
155 ip
= (BYTE
*) pBuf
+ i
;
157 i
= *pTmp
++; i
|= *pTmp
++ << 8; i
|= *pTmp
++ << 16; i
|= *pTmp
++ << 24;
161 pParser
->Error( SbERR_INTERNAL_ERROR
, "BACKCHAIN" );
164 *ip
++ = (char) ( val1
& 0xFF );
165 *ip
++ = (char) ( val1
>> 8 );
166 *ip
++ = (char) ( val2
& 0xFF );
167 *ip
= (char) ( val2
>> 8 );
172 BOOL
SbiBuffer::operator +=( INT8 n
)
176 *pCur
++ = (char) n
; nOff
++; return TRUE
;
180 BOOL
SbiBuffer::operator +=( UINT8 n
)
184 *pCur
++ = (char) n
; nOff
++; return TRUE
;
188 BOOL
SbiBuffer::operator +=( INT16 n
)
192 *pCur
++ = (char) ( n
& 0xFF );
193 *pCur
++ = (char) ( n
>> 8 );
194 nOff
+= 2; return TRUE
;
198 BOOL
SbiBuffer::operator +=( UINT16 n
)
202 *pCur
++ = (char) ( n
& 0xFF );
203 *pCur
++ = (char) ( n
>> 8 );
204 nOff
+= 2; return TRUE
;
208 BOOL
SbiBuffer::operator +=( UINT32 n
)
212 UINT16 n1
= static_cast<UINT16
>( n
& 0xFFFF );
213 UINT16 n2
= static_cast<UINT16
>( n
>> 16 );
214 if ( operator +=( n1
) && operator +=( n2
) )
221 BOOL
SbiBuffer::operator +=( INT32 n
)
223 return operator +=( (UINT32
) n
);
227 BOOL
SbiBuffer::operator +=( const String
& n
)
229 USHORT l
= n
.Len() + 1;
232 ByteString
aByteStr( n
, gsl_getSystemTextEncoding() );
233 memcpy( pCur
, aByteStr
.GetBuffer(), l
);
241 BOOL
SbiBuffer::Add( const void* p
, USHORT len
)
245 memcpy( pCur
, p
, len
);