1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_basic.hxx" 30 31 #include "sbcomp.hxx" 32 #include "buffer.hxx" 33 #include <string.h> 34 35 const static sal_uInt32 UP_LIMIT=0xFFFFFF00L; 36 37 // Der SbiBuffer wird in Inkrements von mindestens 16 Bytes erweitert. 38 // Dies ist notwendig, da viele Klassen von einer Pufferlaenge 39 // von x*16 Bytes ausgehen. 40 41 SbiBuffer::SbiBuffer( SbiParser* p, short n ) 42 { 43 pParser = p; 44 n = ( (n + 15 ) / 16 ) * 16; 45 if( !n ) n = 16; 46 pBuf = NULL; 47 pCur = NULL; 48 nInc = n; 49 nSize = 50 nOff = 0; 51 } 52 53 SbiBuffer::~SbiBuffer() 54 { 55 delete[] pBuf; 56 } 57 58 // Rausreichen des Puffers 59 // Dies fuehrt zur Loeschung des Puffers! 60 61 char* SbiBuffer::GetBuffer() 62 { 63 char* p = pBuf; 64 pBuf = NULL; 65 pCur = NULL; 66 return p; 67 } 68 69 // Test, ob der Puffer n Bytes aufnehmen kann. 70 // Im Zweifelsfall wird er vergroessert 71 72 sal_Bool SbiBuffer::Check( sal_uInt16 n ) 73 { 74 if( !n ) return sal_True; 75 if( ( static_cast<sal_uInt32>( nOff )+ n ) > static_cast<sal_uInt32>( nSize ) ) 76 { 77 if( nInc == 0 ) 78 return sal_False; 79 sal_uInt16 nn = 0; 80 while( nn < n ) nn = nn + nInc; 81 char* p; 82 if( ( static_cast<sal_uInt32>( nSize ) + nn ) > UP_LIMIT ) p = NULL; 83 else p = new char [nSize + nn]; 84 if( !p ) 85 { 86 pParser->Error( SbERR_PROG_TOO_LARGE ); 87 nInc = 0; 88 delete[] pBuf; pBuf = NULL; 89 return sal_False; 90 } 91 else 92 { 93 if( nSize ) memcpy( p, pBuf, nSize ); 94 delete[] pBuf; 95 pBuf = p; 96 pCur = pBuf + nOff; 97 nSize = nSize + nn; 98 } 99 } 100 return sal_True; 101 } 102 103 // Angleich des Puffers auf die uebergebene Byte-Grenze 104 105 void SbiBuffer::Align( sal_Int32 n ) 106 { 107 if( nOff % n ) { 108 sal_uInt32 nn =( ( nOff + n ) / n ) * n; 109 if( nn <= UP_LIMIT ) 110 { 111 nn = nn - nOff; 112 if( Check( static_cast<sal_uInt16>(nn) ) ) 113 { 114 memset( pCur, 0, nn ); 115 pCur += nn; 116 nOff = nOff + nn; 117 } 118 } 119 } 120 } 121 122 // Patch einer Location 123 124 void SbiBuffer::Patch( sal_uInt32 off, sal_uInt32 val ) 125 { 126 if( ( off + sizeof( sal_uInt32 ) ) < nOff ) 127 { 128 sal_uInt16 val1 = static_cast<sal_uInt16>( val & 0xFFFF ); 129 sal_uInt16 val2 = static_cast<sal_uInt16>( val >> 16 ); 130 sal_uInt8* p = (sal_uInt8*) pBuf + off; 131 *p++ = (char) ( val1 & 0xFF ); 132 *p++ = (char) ( val1 >> 8 ); 133 *p++ = (char) ( val2 & 0xFF ); 134 *p = (char) ( val2 >> 8 ); 135 } 136 } 137 138 // Forward References auf Labels und Prozeduren 139 // bauen eine Kette auf. Der Anfang der Kette ist beim uebergebenen 140 // Parameter, das Ende der Kette ist 0. 141 142 void SbiBuffer::Chain( sal_uInt32 off ) 143 { 144 if( off && pBuf ) 145 { 146 sal_uInt8 *ip; 147 sal_uInt32 i = off; 148 sal_uInt32 val1 = (nOff & 0xFFFF); 149 sal_uInt32 val2 = (nOff >> 16); 150 do 151 { 152 ip = (sal_uInt8*) pBuf + i; 153 sal_uInt8* pTmp = ip; 154 i = *pTmp++; i |= *pTmp++ << 8; i |= *pTmp++ << 16; i |= *pTmp++ << 24; 155 156 if( i >= nOff ) 157 { 158 pParser->Error( SbERR_INTERNAL_ERROR, "BACKCHAIN" ); 159 break; 160 } 161 *ip++ = (char) ( val1 & 0xFF ); 162 *ip++ = (char) ( val1 >> 8 ); 163 *ip++ = (char) ( val2 & 0xFF ); 164 *ip = (char) ( val2 >> 8 ); 165 } while( i ); 166 } 167 } 168 169 sal_Bool SbiBuffer::operator +=( sal_Int8 n ) 170 { 171 if( Check( 1 ) ) 172 { 173 *pCur++ = (char) n; nOff++; return sal_True; 174 } else return sal_False; 175 } 176 177 sal_Bool SbiBuffer::operator +=( sal_uInt8 n ) 178 { 179 if( Check( 1 ) ) 180 { 181 *pCur++ = (char) n; nOff++; return sal_True; 182 } else return sal_False; 183 } 184 185 sal_Bool SbiBuffer::operator +=( sal_Int16 n ) 186 { 187 if( Check( 2 ) ) 188 { 189 *pCur++ = (char) ( n & 0xFF ); 190 *pCur++ = (char) ( n >> 8 ); 191 nOff += 2; return sal_True; 192 } else return sal_False; 193 } 194 195 sal_Bool SbiBuffer::operator +=( sal_uInt16 n ) 196 { 197 if( Check( 2 ) ) 198 { 199 *pCur++ = (char) ( n & 0xFF ); 200 *pCur++ = (char) ( n >> 8 ); 201 nOff += 2; return sal_True; 202 } else return sal_False; 203 } 204 205 sal_Bool SbiBuffer::operator +=( sal_uInt32 n ) 206 { 207 if( Check( 4 ) ) 208 { 209 sal_uInt16 n1 = static_cast<sal_uInt16>( n & 0xFFFF ); 210 sal_uInt16 n2 = static_cast<sal_uInt16>( n >> 16 ); 211 if ( operator +=( n1 ) && operator +=( n2 ) ) 212 return sal_True; 213 return sal_True; 214 } 215 return sal_False; 216 } 217 218 sal_Bool SbiBuffer::operator +=( sal_Int32 n ) 219 { 220 return operator +=( (sal_uInt32) n ); 221 } 222 223 224 sal_Bool SbiBuffer::operator +=( const String& n ) 225 { 226 sal_uInt16 l = n.Len() + 1; 227 if( Check( l ) ) 228 { 229 ByteString aByteStr( n, gsl_getSystemTextEncoding() ); 230 memcpy( pCur, aByteStr.GetBuffer(), l ); 231 pCur += l; 232 nOff = nOff + l; 233 return sal_True; 234 } 235 else return sal_False; 236 } 237 238 sal_Bool SbiBuffer::Add( const void* p, sal_uInt16 len ) 239 { 240 if( Check( len ) ) 241 { 242 memcpy( pCur, p, len ); 243 pCur += len; 244 nOff = nOff + len; 245 return sal_True; 246 } else return sal_False; 247 } 248 249 250 251