nss: upgrade to release 3.73
[LibreOffice.git] / basic / source / comp / buffer.cxx
blobbbc3c351f5ae201fe2e6f4af41af730a263548fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <buffer.hxx>
21 #include <parser.hxx>
23 #include <basic/sberrors.hxx>
25 const sal_uInt32 UP_LIMIT=0xFFFFFF00;
27 // The SbiBuffer will be expanded in increments of at least 16 Bytes.
28 // This is necessary, because many classes emanate from a buffer length
29 // of x*16 Bytes.
31 SbiBuffer::SbiBuffer( SbiParser* p, short n )
33 pParser = p;
34 n = ( (n + 15 ) / 16 ) * 16;
35 if( !n ) n = 16;
36 pCur = nullptr;
37 nInc = n;
38 nSize =
39 nOff = 0;
42 SbiBuffer::~SbiBuffer()
46 // Reach out the buffer
47 // This lead to the deletion of the buffer!
49 char* SbiBuffer::GetBuffer()
51 char* p = pBuf.release();
52 pCur = nullptr;
53 return p;
56 // Test, if the buffer can contain n Bytes.
57 // In case of doubt it will be enlarged
59 bool SbiBuffer::Check( sal_Int32 n )
61 if( !n )
63 return true;
65 if( nOff + n > nSize )
67 if( nInc == 0 )
69 return false;
72 sal_Int32 nn = 0;
73 while( nn < n )
75 nn = nn + nInc;
77 char* p;
78 if( ( nSize + nn ) > UP_LIMIT )
80 p = nullptr;
82 else
84 p = new char [nSize + nn];
86 if( !p )
88 pParser->Error( ERRCODE_BASIC_PROG_TOO_LARGE );
89 nInc = 0;
90 pBuf.reset();
91 return false;
93 else
95 if( nSize ) memcpy( p, pBuf.get(), nSize );
96 pBuf.reset(p);
97 pCur = pBuf.get() + nOff;
98 nSize = nSize + nn;
101 return true;
104 // Patch of a Location
106 void SbiBuffer::Patch( sal_uInt32 off, sal_uInt32 val )
108 if( ( off + sizeof( sal_uInt32 ) ) < nOff )
110 sal_uInt16 val1 = static_cast<sal_uInt16>( val & 0xFFFF );
111 sal_uInt16 val2 = static_cast<sal_uInt16>( val >> 16 );
112 sal_uInt8* p = reinterpret_cast<sal_uInt8*>(pBuf.get()) + off;
113 *p++ = static_cast<char>( val1 & 0xFF );
114 *p++ = static_cast<char>( val1 >> 8 );
115 *p++ = static_cast<char>( val2 & 0xFF );
116 *p = static_cast<char>( val2 >> 8 );
120 // Forward References upon label and procedures
121 // establish a linkage. The beginning of the linkage is at the passed parameter,
122 // the end of the linkage is 0.
124 void SbiBuffer::Chain( sal_uInt32 off )
126 if( !(off && pBuf) )
127 return;
129 sal_uInt8 *ip;
130 sal_uInt32 i = off;
131 sal_uInt32 val1 = (nOff & 0xFFFF);
132 sal_uInt32 val2 = (nOff >> 16);
135 ip = reinterpret_cast<sal_uInt8*>(pBuf.get()) + i;
136 sal_uInt8* pTmp = ip;
137 i = *pTmp++; i |= *pTmp++ << 8; i |= *pTmp++ << 16; i |= *pTmp++ << 24;
139 if( i >= nOff )
141 pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, "BACKCHAIN" );
142 break;
144 *ip++ = static_cast<char>( val1 & 0xFF );
145 *ip++ = static_cast<char>( val1 >> 8 );
146 *ip++ = static_cast<char>( val2 & 0xFF );
147 *ip = static_cast<char>( val2 >> 8 );
148 } while( i );
151 void SbiBuffer::operator +=( sal_Int8 n )
153 if( Check( 1 ) )
155 *pCur++ = static_cast<char>(n);
156 nOff += 1;
160 bool SbiBuffer::operator +=( sal_uInt8 n )
162 if( Check( 1 ) )
164 *pCur++ = static_cast<char>(n);
165 nOff += 1;
166 return true;
168 else
170 return false;
174 void SbiBuffer::operator +=( sal_Int16 n )
176 if( Check( 2 ) )
178 *pCur++ = static_cast<char>( n & 0xFF );
179 *pCur++ = static_cast<char>( n >> 8 );
180 nOff += 2;
184 bool SbiBuffer::operator +=( sal_uInt16 n )
186 if( Check( 2 ) )
188 *pCur++ = static_cast<char>( n & 0xFF );
189 *pCur++ = static_cast<char>( n >> 8 );
190 nOff += 2;
191 return true;
193 else
195 return false;
199 bool SbiBuffer::operator +=( sal_uInt32 n )
201 if( Check( 4 ) )
203 sal_uInt16 n1 = static_cast<sal_uInt16>( n & 0xFFFF );
204 sal_uInt16 n2 = static_cast<sal_uInt16>( n >> 16 );
205 operator +=(n1) && operator +=(n2);
206 return true;
208 else
210 return false;
214 void SbiBuffer::operator +=( sal_Int32 n )
216 operator +=( static_cast<sal_uInt32>(n) );
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */