1*e1f63238SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*e1f63238SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*e1f63238SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*e1f63238SAndrew Rist * distributed with this work for additional information
6*e1f63238SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*e1f63238SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*e1f63238SAndrew Rist * "License"); you may not use this file except in compliance
9*e1f63238SAndrew Rist * with the License. You may obtain a copy of the License at
10*e1f63238SAndrew Rist *
11*e1f63238SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*e1f63238SAndrew Rist *
13*e1f63238SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*e1f63238SAndrew Rist * software distributed under the License is distributed on an
15*e1f63238SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e1f63238SAndrew Rist * KIND, either express or implied. See the License for the
17*e1f63238SAndrew Rist * specific language governing permissions and limitations
18*e1f63238SAndrew Rist * under the License.
19*e1f63238SAndrew Rist *
20*e1f63238SAndrew Rist *************************************************************/
21*e1f63238SAndrew Rist
22*e1f63238SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basic.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "sbcomp.hxx"
28cdf0e10cSrcweir #include "buffer.hxx"
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir
31cdf0e10cSrcweir const static sal_uInt32 UP_LIMIT=0xFFFFFF00L;
32cdf0e10cSrcweir
33cdf0e10cSrcweir // Der SbiBuffer wird in Inkrements von mindestens 16 Bytes erweitert.
34cdf0e10cSrcweir // Dies ist notwendig, da viele Klassen von einer Pufferlaenge
35cdf0e10cSrcweir // von x*16 Bytes ausgehen.
36cdf0e10cSrcweir
SbiBuffer(SbiParser * p,short n)37cdf0e10cSrcweir SbiBuffer::SbiBuffer( SbiParser* p, short n )
38cdf0e10cSrcweir {
39cdf0e10cSrcweir pParser = p;
40cdf0e10cSrcweir n = ( (n + 15 ) / 16 ) * 16;
41cdf0e10cSrcweir if( !n ) n = 16;
42cdf0e10cSrcweir pBuf = NULL;
43cdf0e10cSrcweir pCur = NULL;
44cdf0e10cSrcweir nInc = n;
45cdf0e10cSrcweir nSize =
46cdf0e10cSrcweir nOff = 0;
47cdf0e10cSrcweir }
48cdf0e10cSrcweir
~SbiBuffer()49cdf0e10cSrcweir SbiBuffer::~SbiBuffer()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir delete[] pBuf;
52cdf0e10cSrcweir }
53cdf0e10cSrcweir
54cdf0e10cSrcweir // Rausreichen des Puffers
55cdf0e10cSrcweir // Dies fuehrt zur Loeschung des Puffers!
56cdf0e10cSrcweir
GetBuffer()57cdf0e10cSrcweir char* SbiBuffer::GetBuffer()
58cdf0e10cSrcweir {
59cdf0e10cSrcweir char* p = pBuf;
60cdf0e10cSrcweir pBuf = NULL;
61cdf0e10cSrcweir pCur = NULL;
62cdf0e10cSrcweir return p;
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
65cdf0e10cSrcweir // Test, ob der Puffer n Bytes aufnehmen kann.
66cdf0e10cSrcweir // Im Zweifelsfall wird er vergroessert
67cdf0e10cSrcweir
Check(sal_uInt16 n)68cdf0e10cSrcweir sal_Bool SbiBuffer::Check( sal_uInt16 n )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir if( !n ) return sal_True;
71cdf0e10cSrcweir if( ( static_cast<sal_uInt32>( nOff )+ n ) > static_cast<sal_uInt32>( nSize ) )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir if( nInc == 0 )
74cdf0e10cSrcweir return sal_False;
75cdf0e10cSrcweir sal_uInt16 nn = 0;
76cdf0e10cSrcweir while( nn < n ) nn = nn + nInc;
77cdf0e10cSrcweir char* p;
78cdf0e10cSrcweir if( ( static_cast<sal_uInt32>( nSize ) + nn ) > UP_LIMIT ) p = NULL;
79cdf0e10cSrcweir else p = new char [nSize + nn];
80cdf0e10cSrcweir if( !p )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir pParser->Error( SbERR_PROG_TOO_LARGE );
83cdf0e10cSrcweir nInc = 0;
84cdf0e10cSrcweir delete[] pBuf; pBuf = NULL;
85cdf0e10cSrcweir return sal_False;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir else
88cdf0e10cSrcweir {
89cdf0e10cSrcweir if( nSize ) memcpy( p, pBuf, nSize );
90cdf0e10cSrcweir delete[] pBuf;
91cdf0e10cSrcweir pBuf = p;
92cdf0e10cSrcweir pCur = pBuf + nOff;
93cdf0e10cSrcweir nSize = nSize + nn;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir }
96cdf0e10cSrcweir return sal_True;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir
99cdf0e10cSrcweir // Angleich des Puffers auf die uebergebene Byte-Grenze
100cdf0e10cSrcweir
Align(sal_Int32 n)101cdf0e10cSrcweir void SbiBuffer::Align( sal_Int32 n )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir if( nOff % n ) {
104cdf0e10cSrcweir sal_uInt32 nn =( ( nOff + n ) / n ) * n;
105cdf0e10cSrcweir if( nn <= UP_LIMIT )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir nn = nn - nOff;
108cdf0e10cSrcweir if( Check( static_cast<sal_uInt16>(nn) ) )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir memset( pCur, 0, nn );
111cdf0e10cSrcweir pCur += nn;
112cdf0e10cSrcweir nOff = nOff + nn;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir }
115cdf0e10cSrcweir }
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir // Patch einer Location
119cdf0e10cSrcweir
Patch(sal_uInt32 off,sal_uInt32 val)120cdf0e10cSrcweir void SbiBuffer::Patch( sal_uInt32 off, sal_uInt32 val )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir if( ( off + sizeof( sal_uInt32 ) ) < nOff )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir sal_uInt16 val1 = static_cast<sal_uInt16>( val & 0xFFFF );
125cdf0e10cSrcweir sal_uInt16 val2 = static_cast<sal_uInt16>( val >> 16 );
126cdf0e10cSrcweir sal_uInt8* p = (sal_uInt8*) pBuf + off;
127cdf0e10cSrcweir *p++ = (char) ( val1 & 0xFF );
128cdf0e10cSrcweir *p++ = (char) ( val1 >> 8 );
129cdf0e10cSrcweir *p++ = (char) ( val2 & 0xFF );
130cdf0e10cSrcweir *p = (char) ( val2 >> 8 );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir }
133cdf0e10cSrcweir
134cdf0e10cSrcweir // Forward References auf Labels und Prozeduren
135cdf0e10cSrcweir // bauen eine Kette auf. Der Anfang der Kette ist beim uebergebenen
136cdf0e10cSrcweir // Parameter, das Ende der Kette ist 0.
137cdf0e10cSrcweir
Chain(sal_uInt32 off)138cdf0e10cSrcweir void SbiBuffer::Chain( sal_uInt32 off )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir if( off && pBuf )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir sal_uInt8 *ip;
143cdf0e10cSrcweir sal_uInt32 i = off;
144cdf0e10cSrcweir sal_uInt32 val1 = (nOff & 0xFFFF);
145cdf0e10cSrcweir sal_uInt32 val2 = (nOff >> 16);
146cdf0e10cSrcweir do
147cdf0e10cSrcweir {
148cdf0e10cSrcweir ip = (sal_uInt8*) pBuf + i;
149cdf0e10cSrcweir sal_uInt8* pTmp = ip;
150cdf0e10cSrcweir i = *pTmp++; i |= *pTmp++ << 8; i |= *pTmp++ << 16; i |= *pTmp++ << 24;
151cdf0e10cSrcweir
152cdf0e10cSrcweir if( i >= nOff )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir pParser->Error( SbERR_INTERNAL_ERROR, "BACKCHAIN" );
155cdf0e10cSrcweir break;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir *ip++ = (char) ( val1 & 0xFF );
158cdf0e10cSrcweir *ip++ = (char) ( val1 >> 8 );
159cdf0e10cSrcweir *ip++ = (char) ( val2 & 0xFF );
160cdf0e10cSrcweir *ip = (char) ( val2 >> 8 );
161cdf0e10cSrcweir } while( i );
162cdf0e10cSrcweir }
163cdf0e10cSrcweir }
164cdf0e10cSrcweir
operator +=(sal_Int8 n)165cdf0e10cSrcweir sal_Bool SbiBuffer::operator +=( sal_Int8 n )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir if( Check( 1 ) )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir *pCur++ = (char) n; nOff++; return sal_True;
170cdf0e10cSrcweir } else return sal_False;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir
operator +=(sal_uInt8 n)173cdf0e10cSrcweir sal_Bool SbiBuffer::operator +=( sal_uInt8 n )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir if( Check( 1 ) )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir *pCur++ = (char) n; nOff++; return sal_True;
178cdf0e10cSrcweir } else return sal_False;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir
operator +=(sal_Int16 n)181cdf0e10cSrcweir sal_Bool SbiBuffer::operator +=( sal_Int16 n )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir if( Check( 2 ) )
184cdf0e10cSrcweir {
185cdf0e10cSrcweir *pCur++ = (char) ( n & 0xFF );
186cdf0e10cSrcweir *pCur++ = (char) ( n >> 8 );
187cdf0e10cSrcweir nOff += 2; return sal_True;
188cdf0e10cSrcweir } else return sal_False;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir
operator +=(sal_uInt16 n)191cdf0e10cSrcweir sal_Bool SbiBuffer::operator +=( sal_uInt16 n )
192cdf0e10cSrcweir {
193cdf0e10cSrcweir if( Check( 2 ) )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir *pCur++ = (char) ( n & 0xFF );
196cdf0e10cSrcweir *pCur++ = (char) ( n >> 8 );
197cdf0e10cSrcweir nOff += 2; return sal_True;
198cdf0e10cSrcweir } else return sal_False;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir
operator +=(sal_uInt32 n)201cdf0e10cSrcweir sal_Bool SbiBuffer::operator +=( sal_uInt32 n )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir if( Check( 4 ) )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir sal_uInt16 n1 = static_cast<sal_uInt16>( n & 0xFFFF );
206cdf0e10cSrcweir sal_uInt16 n2 = static_cast<sal_uInt16>( n >> 16 );
207cdf0e10cSrcweir if ( operator +=( n1 ) && operator +=( n2 ) )
208cdf0e10cSrcweir return sal_True;
209cdf0e10cSrcweir return sal_True;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir return sal_False;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir
operator +=(sal_Int32 n)214cdf0e10cSrcweir sal_Bool SbiBuffer::operator +=( sal_Int32 n )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir return operator +=( (sal_uInt32) n );
217cdf0e10cSrcweir }
218cdf0e10cSrcweir
219cdf0e10cSrcweir
operator +=(const String & n)220cdf0e10cSrcweir sal_Bool SbiBuffer::operator +=( const String& n )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir sal_uInt16 l = n.Len() + 1;
223cdf0e10cSrcweir if( Check( l ) )
224cdf0e10cSrcweir {
225cdf0e10cSrcweir ByteString aByteStr( n, gsl_getSystemTextEncoding() );
226cdf0e10cSrcweir memcpy( pCur, aByteStr.GetBuffer(), l );
227cdf0e10cSrcweir pCur += l;
228cdf0e10cSrcweir nOff = nOff + l;
229cdf0e10cSrcweir return sal_True;
230cdf0e10cSrcweir }
231cdf0e10cSrcweir else return sal_False;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir
Add(const void * p,sal_uInt16 len)234cdf0e10cSrcweir sal_Bool SbiBuffer::Add( const void* p, sal_uInt16 len )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir if( Check( len ) )
237cdf0e10cSrcweir {
238cdf0e10cSrcweir memcpy( pCur, p, len );
239cdf0e10cSrcweir pCur += len;
240cdf0e10cSrcweir nOff = nOff + len;
241cdf0e10cSrcweir return sal_True;
242cdf0e10cSrcweir } else return sal_False;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir
245cdf0e10cSrcweir
246cdf0e10cSrcweir
247