| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <tools/stream.hxx> |
| 21 |
#include <tools/tenccvt.hxx> |
| 22 |
#include <osl/thread.h> |
| 23 |
#include <o3tl/safeint.hxx> |
| 24 |
#include <sal/log.hxx> |
| 25 |
#include <basic/sbx.hxx> |
| 26 |
#include <filefmt.hxx> |
| 27 |
#include <sb.hxx> |
| 28 |
#include <sbxprop.hxx> |
| 29 |
#include <string.h> |
| 30 |
#include <image.hxx> |
| 31 |
#include <codegen.hxx> |
| 32 |
#include <memory> |
| 33 |
#include <string_view> |
| 34 |
#include <algorithm> |
| 35 |
|
| 36 |
SbiImage::SbiImage() |
| 37 |
: bError(false) |
| 38 |
, nFlags(SbiImageFlags::NONE) |
| 39 |
, nStringSize(0) |
| 40 |
, nDimBase(0) |
| 41 |
, eCharSet(osl_getThreadTextEncoding()) |
| 42 |
, nStringIdx(0) |
| 43 |
, nStringOff(0) |
| 44 |
, bInit(false) |
| 45 |
, bFirstInit(true) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
SbiImage::~SbiImage() |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
void SbiImage::Clear() |
| 54 |
{ |
| 55 |
mvStringOffsets.clear(); |
| 56 |
pStrings.reset(); |
| 57 |
aCode.clear(); |
| 58 |
aLegacyPCode.clear(); |
| 59 |
nFlags = SbiImageFlags::NONE; |
| 60 |
nStringSize= 0; |
| 61 |
eCharSet = osl_getThreadTextEncoding(); |
| 62 |
nDimBase = 0; |
| 63 |
bError = false; |
| 64 |
} |
| 65 |
|
| 66 |
// Open Record |
| 67 |
static sal_uInt64 SbiOpenRecord( SvStream& r, FileOffset nSignature, sal_uInt16 nElem ) |
| 68 |
{ |
| 69 |
sal_uInt64 nPos = r.Tell(); |
| 70 |
r.WriteUInt16( static_cast<sal_uInt16>( nSignature ) ) |
| 71 |
.WriteInt32( 0 ).WriteUInt16( nElem ); |
| 72 |
return nPos; |
| 73 |
} |
| 74 |
|
| 75 |
// Close Record |
| 76 |
static void SbiCloseRecord( SvStream& r, sal_uInt64 nOff ) |
| 77 |
{ |
| 78 |
sal_uInt64 nPos = r.Tell(); |
| 79 |
r.Seek( nOff + 2 ); |
| 80 |
r.WriteInt32(nPos - nOff - 8 ); |
| 81 |
r.Seek( nPos ); |
| 82 |
} |
| 83 |
|
| 84 |
constexpr sal_uInt32 nUnicodeDataMagicNumber = 0x556E6920; // "Uni " BE |
| 85 |
|
| 86 |
static bool GetToUnicodePoolData(SvStream& r, sal_uInt64 nLen, sal_uInt64 nNext) |
| 87 |
{ |
| 88 |
const auto nPos = r.Tell(); |
| 89 |
// Check space for legacy data, magic number and Unicode data |
| 90 |
bool bResult = nPos + nLen + sizeof(sal_uInt32) + nLen * sizeof(sal_Unicode) <= nNext; |
| 91 |
if (bResult) |
| 92 |
{ |
| 93 |
r.SeekRel(nLen); // Skip legacy data |
| 94 |
sal_uInt32 nMagic = 0; |
| 95 |
r.ReadUInt32(nMagic); |
| 96 |
if (nMagic != nUnicodeDataMagicNumber) |
| 97 |
{ |
| 98 |
r.Seek(nPos); // return |
| 99 |
bResult = false; |
| 100 |
} |
| 101 |
} |
| 102 |
return bResult; |
| 103 |
} |
| 104 |
|
| 105 |
bool SbiImage::Load( SvStream& r, sal_uInt32& nVersion ) |
| 106 |
{ |
| 107 |
|
| 108 |
sal_uInt16 nSign, nCount; |
| 109 |
sal_uInt32 nLen; |
| 110 |
|
| 111 |
Clear(); |
| 112 |
// Read Master-Record |
| 113 |
r.ReadUInt16( nSign ).ReadUInt32( nLen ).ReadUInt16( nCount ); |
| 114 |
sal_uInt64 nLast = r.Tell() + nLen; |
| 115 |
bool bBadVer = false; |
| 116 |
if( nSign == static_cast<sal_uInt16>( FileOffset::Module ) ) |
| 117 |
{ |
| 118 |
sal_uInt32 nCharSet; // System charset |
| 119 |
sal_uInt32 lDimBase; |
| 120 |
sal_uInt16 nTmpFlags; |
| 121 |
sal_uInt16 nReserved1; |
| 122 |
sal_uInt32 nReserved2; |
| 123 |
sal_uInt32 nReserved3; |
| 124 |
r.ReadUInt32( nVersion ).ReadUInt32( nCharSet ).ReadUInt32( lDimBase ) |
| 125 |
.ReadUInt16( nTmpFlags ).ReadUInt16( nReserved1 ).ReadUInt32( nReserved2 ).ReadUInt32( nReserved3 ); |
| 126 |
nFlags = static_cast<SbiImageFlags>(nTmpFlags); |
| 127 |
eCharSet = nCharSet; |
| 128 |
eCharSet = GetSOLoadTextEncoding( eCharSet ); |
| 129 |
bBadVer = ( nVersion > B_IMG_VERSION_13 ); |
| 130 |
nDimBase = static_cast<sal_uInt16>(lDimBase); |
| 131 |
} |
| 132 |
|
| 133 |
bool bLegacy = ( nVersion < B_IMG_VERSION_12 ); |
| 134 |
|
| 135 |
sal_uInt64 nNext; |
| 136 |
while( ( nNext = r.Tell() ) < nLast ) |
| 137 |
{ |
| 138 |
|
| 139 |
r.ReadUInt16( nSign ).ReadUInt32( nLen ).ReadUInt16( nCount ); |
| 140 |
nNext += nLen + 8; |
| 141 |
if( r.GetError() == ERRCODE_NONE ) |
| 142 |
{ |
| 143 |
switch( static_cast<FileOffset>( nSign ) ) |
| 144 |
{ |
| 145 |
case FileOffset::Name: |
| 146 |
aName = r.ReadUniOrByteString(eCharSet); |
| 147 |
break; |
| 148 |
case FileOffset::Comment: |
| 149 |
aComment = r.ReadUniOrByteString(eCharSet ); |
| 150 |
break; |
| 151 |
case FileOffset::Source: |
| 152 |
{ |
| 153 |
aOUSource = r.ReadUniOrByteString(eCharSet); |
| 154 |
break; |
| 155 |
} |
| 156 |
case FileOffset::ExtSource: |
| 157 |
{ |
| 158 |
//assuming an empty string with just the lead 32bit/16bit len indicator |
| 159 |
const size_t nMinStringSize = (eCharSet == RTL_TEXTENCODING_UNICODE) ? 4 : 2; |
| 160 |
const sal_uInt64 nMaxStrings = r.remainingSize() / nMinStringSize; |
| 161 |
if (nCount > nMaxStrings) |
| 162 |
{ |
| 163 |
SAL_WARN("basic", "Parsing error: " << nMaxStrings << |
| 164 |
" max possible entries, but " << nCount << " claimed, truncating"); |
| 165 |
nCount = nMaxStrings; |
| 166 |
} |
| 167 |
for( sal_uInt16 j = 0; j < nCount; ++j) |
| 168 |
{ |
| 169 |
aOUSource += r.ReadUniOrByteString(eCharSet); |
| 170 |
} |
| 171 |
break; |
| 172 |
} |
| 173 |
case FileOffset::PCode: |
| 174 |
if( bBadVer ) break; |
| 175 |
aCode.resize(nLen); |
| 176 |
r.ReadBytes(aCode.data(), aCode.size()); |
| 177 |
if ( bLegacy ) |
| 178 |
{ |
| 179 |
aLegacyPCode = std::move(aCode); |
| 180 |
|
| 181 |
PCodeBuffConvertor<sal_uInt16, sal_uInt32> aLegacyToNew(aLegacyPCode.data(), |
| 182 |
aLegacyPCode.size()); |
| 183 |
aLegacyToNew.convert(); |
| 184 |
aCode = aLegacyToNew.GetBuffer(); |
| 185 |
// we don't release the legacy buffer |
| 186 |
// right now, that's because the module |
| 187 |
// needs it to fix up the method |
| 188 |
// nStart members. When that is done |
| 189 |
// the module can release the buffer |
| 190 |
// or it can wait until this routine |
| 191 |
// is called again or when this class // destructs all of which will trigger |
| 192 |
// release of the buffer. |
| 193 |
} |
| 194 |
break; |
| 195 |
case FileOffset::Publics: |
| 196 |
case FileOffset::PoolDir: |
| 197 |
case FileOffset::SymPool: |
| 198 |
case FileOffset::LineRanges: |
| 199 |
break; |
| 200 |
case FileOffset::StringPool: |
| 201 |
{ |
| 202 |
// the data layout is: nCount of 32-bit offsets into both legacy 1-byte char stream |
| 203 |
// and resulting char buffer (1:1 correspondence assumed; 16 of 32 bits used); |
| 204 |
// 32-bit length N of following 1-byte char stream (16 bits used); N bytes of 1-byte |
| 205 |
// char stream; then optional magic number and stream of N sal_Unicode characters. |
| 206 |
|
| 207 |
if( bBadVer ) break; |
| 208 |
//assuming an empty string with just the lead 32bit len indicator |
| 209 |
const sal_uInt64 nMinStringSize = 4; |
| 210 |
const sal_uInt64 nMaxStrings = r.remainingSize() / nMinStringSize; |
| 211 |
if (nCount > nMaxStrings) |
| 212 |
{ |
| 213 |
SAL_WARN("basic", "Parsing error: " << nMaxStrings << |
| 214 |
" max possible entries, but " << nCount << " claimed, truncating"); |
| 215 |
nCount = nMaxStrings; |
| 216 |
} |
| 217 |
MakeStrings( nCount ); |
| 218 |
for (size_t i = 0; i < mvStringOffsets.size() && r.good(); i++) |
| 219 |
{ |
| 220 |
sal_uInt32 nOff; |
| 221 |
r.ReadUInt32( nOff ); |
| 222 |
mvStringOffsets[ i ] = static_cast<sal_uInt16>(nOff); |
| 223 |
} |
| 224 |
r.ReadUInt32( nLen ); |
| 225 |
if (r.good()) |
| 226 |
{ |
| 227 |
pStrings.reset(new sal_Unicode[ nLen ]); |
| 228 |
nStringSize = static_cast<sal_uInt16>(nLen); |
| 229 |
|
| 230 |
if (GetToUnicodePoolData(r, nLen, nNext)) |
| 231 |
{ |
| 232 |
OUString s = read_uInt16s_ToOUString(r, nLen); |
| 233 |
std::copy_n(s.getStr(), s.getLength(), pStrings.get()); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
std::unique_ptr<char[]> pByteStrings(new char[nLen]); |
| 238 |
r.ReadBytes(pByteStrings.get(), nLen); |
| 239 |
for (size_t j = 0; j < mvStringOffsets.size(); j++) |
| 240 |
{ |
| 241 |
sal_uInt16 nOff2 = static_cast<sal_uInt16>(mvStringOffsets[j]); |
| 242 |
OUString aStr(pByteStrings.get() + nOff2, strlen(pByteStrings.get() + nOff2), eCharSet); |
| 243 |
std::copy_n(aStr.getStr(), aStr.getLength() + 1, |
| 244 |
pStrings.get() + nOff2); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
break; |
| 249 |
} |
| 250 |
case FileOffset::UserTypes: |
| 251 |
{ |
| 252 |
//assuming an empty string with just the lead 32bit/16bit len indicator |
| 253 |
const size_t nMinStringSize = (eCharSet == RTL_TEXTENCODING_UNICODE) ? 4 : 2; |
| 254 |
const sal_uInt64 nMinRecordSize = nMinStringSize + sizeof(sal_Int16); |
| 255 |
const sal_uInt64 nMaxRecords = r.remainingSize() / nMinRecordSize; |
| 256 |
if (nCount > nMaxRecords) |
| 257 |
{ |
| 258 |
SAL_WARN("basic", "Parsing error: " << nMaxRecords << |
| 259 |
" max possible entries, but " << nCount << " claimed, truncating"); |
| 260 |
nCount = nMaxRecords; |
| 261 |
} |
| 262 |
|
| 263 |
// User defined types; ref.: SbiParser::DefType |
| 264 |
for (sal_uInt16 i = 0; i < nCount; i++) |
| 265 |
{ |
| 266 |
OUString aTypeName = r.ReadUniOrByteString(eCharSet); |
| 267 |
|
| 268 |
sal_uInt16 nTypeMembers; |
| 269 |
r.ReadUInt16(nTypeMembers); |
| 270 |
|
| 271 |
const sal_uInt64 nMaxTypeMembers = r.remainingSize() / 8; |
| 272 |
if (nTypeMembers > nMaxTypeMembers) |
| 273 |
{ |
| 274 |
SAL_WARN("basic", "Parsing error: " << nMaxTypeMembers << |
| 275 |
" max possible entries, but " << nTypeMembers << " claimed, truncating"); |
| 276 |
nTypeMembers = nMaxTypeMembers; |
| 277 |
} |
| 278 |
|
| 279 |
SbxObject *pType = new SbxObject(aTypeName); |
| 280 |
SbxArray *pTypeMembers = pType->GetProperties(); |
| 281 |
|
| 282 |
for (sal_uInt16 j = 0; j < nTypeMembers; j++) |
| 283 |
{ |
| 284 |
OUString aMemberName = r.ReadUniOrByteString(eCharSet); |
| 285 |
|
| 286 |
sal_Int16 aIntMemberType; |
| 287 |
r.ReadInt16(aIntMemberType); |
| 288 |
SbxDataType aMemberType = static_cast< SbxDataType > ( aIntMemberType ); |
| 289 |
|
| 290 |
SbxProperty *pTypeElem = new SbxProperty( aMemberName, aMemberType ); |
| 291 |
|
| 292 |
sal_uInt32 aIntFlag; |
| 293 |
r.ReadUInt32(aIntFlag); |
| 294 |
SbxFlagBits nElemFlags = static_cast< SbxFlagBits > ( aIntFlag ); |
| 295 |
|
| 296 |
pTypeElem->SetFlags(nElemFlags); |
| 297 |
|
| 298 |
sal_Int16 hasObject; |
| 299 |
r.ReadInt16(hasObject); |
| 300 |
|
| 301 |
if (hasObject == 1) |
| 302 |
{ |
| 303 |
if(aMemberType == SbxOBJECT) |
| 304 |
{ |
| 305 |
// nested user defined types |
| 306 |
// declared before use, so it is ok to reference it by name on load |
| 307 |
OUString aNestedTypeName = r.ReadUniOrByteString(eCharSet); |
| 308 |
SbxObject* pNestedTypeObj = static_cast< SbxObject* >( rTypes->Find( aNestedTypeName, SbxClassType::Object ) ); |
| 309 |
if (pNestedTypeObj) |
| 310 |
{ |
| 311 |
SbxObjectRef pCloneObj = cloneTypeObjectImpl( *pNestedTypeObj ); |
| 312 |
pTypeElem->PutObject( pCloneObj.get() ); |
| 313 |
} |
| 314 |
} |
| 315 |
else |
| 316 |
{ |
| 317 |
// an array |
| 318 |
SbxDimArray* pArray = new SbxDimArray( |
| 319 |
static_cast<SbxDataType>(aMemberType & 0x0FFF)); |
| 320 |
|
| 321 |
sal_Int16 isFixedSize; |
| 322 |
r.ReadInt16(isFixedSize); |
| 323 |
if (isFixedSize == 1) |
| 324 |
pArray->setHasFixedSize( true ); |
| 325 |
|
| 326 |
sal_Int32 nDims; |
| 327 |
r.ReadInt32(nDims); |
| 328 |
for (sal_Int32 d = 0; d < nDims; d++) |
| 329 |
{ |
| 330 |
sal_Int32 lBound; |
| 331 |
sal_Int32 uBound; |
| 332 |
r.ReadInt32(lBound).ReadInt32(uBound); |
| 333 |
pArray->unoAddDim(lBound, uBound); |
| 334 |
} |
| 335 |
|
| 336 |
const SbxFlagBits nSavFlags = pTypeElem->GetFlags(); |
| 337 |
// need to reset the FIXED flag |
| 338 |
// when calling PutObject ( because the type will not match Object ) |
| 339 |
pTypeElem->ResetFlag(SbxFlagBits::Fixed); |
| 340 |
pTypeElem->PutObject( pArray ); |
| 341 |
pTypeElem->SetFlags(nSavFlags); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
pTypeMembers->Insert(pTypeElem, pTypeMembers->Count()); |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
pType->Remove( u"Name"_ustr, SbxClassType::DontCare ); |
| 350 |
pType->Remove( u"Parent"_ustr, SbxClassType::DontCare ); |
| 351 |
|
| 352 |
AddType(pType); |
| 353 |
} |
| 354 |
break; |
| 355 |
} |
| 356 |
case FileOffset::ModEnd: |
| 357 |
goto done; |
| 358 |
default: |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
else |
| 363 |
{ |
| 364 |
break; |
| 365 |
} |
| 366 |
r.Seek( nNext ); |
| 367 |
} |
| 368 |
done: |
| 369 |
r.Seek( nLast ); |
| 370 |
if (!r.good()) |
| 371 |
{ |
| 372 |
bError = true; |
| 373 |
} |
| 374 |
return !bError; |
| 375 |
} |
| 376 |
|
| 377 |
bool SbiImage::Save( SvStream& r, sal_uInt32 nVer ) |
| 378 |
{ |
| 379 |
// First of all the header |
| 380 |
sal_uInt64 nStart = SbiOpenRecord( r, FileOffset::Module, 1 ); |
| 381 |
sal_uInt64 nPos; |
| 382 |
|
| 383 |
eCharSet = GetSOStoreTextEncoding( eCharSet ); |
| 384 |
r .WriteInt32( nVer ) |
| 385 |
.WriteInt32( eCharSet ) |
| 386 |
.WriteInt32( nDimBase ) |
| 387 |
.WriteInt16( static_cast<sal_uInt16>(nFlags) ) |
| 388 |
.WriteInt16( 0 ) |
| 389 |
.WriteInt32( 0 ) |
| 390 |
.WriteInt32( 0 ); |
| 391 |
|
| 392 |
// Name? |
| 393 |
if (!aName.isEmpty() && r.good()) |
| 394 |
{ |
| 395 |
nPos = SbiOpenRecord( r, FileOffset::Name, 1 ); |
| 396 |
r.WriteUniOrByteString( aName, eCharSet ); |
| 397 |
SbiCloseRecord( r, nPos ); |
| 398 |
} |
| 399 |
// Comment? |
| 400 |
if (!aComment.isEmpty() && r.good()) |
| 401 |
{ |
| 402 |
nPos = SbiOpenRecord( r, FileOffset::Comment, 1 ); |
| 403 |
r.WriteUniOrByteString( aComment, eCharSet ); |
| 404 |
SbiCloseRecord( r, nPos ); |
| 405 |
} |
| 406 |
// Source? |
| 407 |
if (!aOUSource.isEmpty() && r.good()) |
| 408 |
{ |
| 409 |
nPos = SbiOpenRecord( r, FileOffset::Source, 1 ); |
| 410 |
r.WriteUniOrByteString( aOUSource, eCharSet ); |
| 411 |
SbiCloseRecord( r, nPos ); |
| 412 |
} |
| 413 |
// Binary data? |
| 414 |
if (aCode.size() && r.good()) |
| 415 |
{ |
| 416 |
nPos = SbiOpenRecord( r, FileOffset::PCode, 1 ); |
| 417 |
r.WriteBytes(aCode.data(), aCode.size()); |
| 418 |
SbiCloseRecord( r, nPos ); |
| 419 |
} |
| 420 |
// String-Pool? |
| 421 |
if( !mvStringOffsets.empty() ) |
| 422 |
{ |
| 423 |
nPos = SbiOpenRecord( r, FileOffset::StringPool, mvStringOffsets.size() ); |
| 424 |
// For every String: |
| 425 |
// sal_uInt32 Offset of the Strings in the Stringblock |
| 426 |
for (size_t i = 0; i < mvStringOffsets.size() && r.good(); i++) |
| 427 |
{ |
| 428 |
r.WriteUInt32( mvStringOffsets[ i ] ); |
| 429 |
} |
| 430 |
// Then the String-Block |
| 431 |
std::unique_ptr<char[]> pByteStrings(new char[ nStringSize ]); |
| 432 |
for( size_t i = 0; i < mvStringOffsets.size(); i++ ) |
| 433 |
{ |
| 434 |
sal_uInt16 nOff = static_cast<sal_uInt16>(mvStringOffsets[ i ]); |
| 435 |
OString aStr(OUStringToOString(std::u16string_view(pStrings.get() + nOff), eCharSet)); |
| 436 |
std::copy_n(aStr.getStr(), aStr.getLength() + 1, pByteStrings.get() + nOff); |
| 437 |
} |
| 438 |
r.WriteUInt32( nStringSize ); |
| 439 |
r.WriteBytes(pByteStrings.get(), nStringSize); |
| 440 |
pByteStrings.reset(); |
| 441 |
|
| 442 |
// Now write magic number and store the same data in UTF-16; this is backward compatible: |
| 443 |
// old readers will not read this data after having read legacy data, and will proceed |
| 444 |
// straight to the end of the record. So no version restriction here. |
| 445 |
r.WriteUInt32(nUnicodeDataMagicNumber); |
| 446 |
r.WriteUnicodeOrByteText(std::u16string_view(pStrings.get(), nStringSize), |
| 447 |
RTL_TEXTENCODING_UNICODE); |
| 448 |
|
| 449 |
SbiCloseRecord( r, nPos ); |
| 450 |
} |
| 451 |
// User defined types |
| 452 |
if ( rTypes.is() ) |
| 453 |
{ |
| 454 |
sal_uInt32 nTypes = rTypes->Count(); |
| 455 |
assert(nTypes <= std::numeric_limits<sal_uInt16>::max()); |
| 456 |
if (nTypes > 0 ) |
| 457 |
{ |
| 458 |
nPos = SbiOpenRecord( r, FileOffset::UserTypes, sal::static_int_cast<sal_uInt16>(nTypes) ); |
| 459 |
|
| 460 |
for (sal_uInt32 i = 0; i < nTypes; i++) |
| 461 |
{ |
| 462 |
SbxObject* pType = static_cast<SbxObject*>(rTypes->Get(i)); |
| 463 |
OUString aTypeName = pType->GetClassName(); |
| 464 |
|
| 465 |
r.WriteUniOrByteString( aTypeName, eCharSet ); |
| 466 |
|
| 467 |
SbxArray *pTypeMembers = pType->GetProperties(); |
| 468 |
sal_uInt32 nTypeMembers = pTypeMembers->Count(); |
| 469 |
assert(nTypeMembers <= std::numeric_limits<sal_uInt16>::max()); |
| 470 |
|
| 471 |
r.WriteInt16(sal::static_int_cast<sal_uInt16>(nTypeMembers)); |
| 472 |
|
| 473 |
for (sal_uInt32 j = 0; j < nTypeMembers; j++) |
| 474 |
{ |
| 475 |
|
| 476 |
SbxProperty* pTypeElem = static_cast<SbxProperty*>(pTypeMembers->Get(j)); |
| 477 |
|
| 478 |
const OUString& aElemName = pTypeElem->GetName(); |
| 479 |
r.WriteUniOrByteString( aElemName, eCharSet ); |
| 480 |
|
| 481 |
SbxDataType dataType = pTypeElem->GetType(); |
| 482 |
r.WriteInt16(dataType); |
| 483 |
|
| 484 |
SbxFlagBits nElemFlags = pTypeElem->GetFlags(); |
| 485 |
r.WriteUInt32(static_cast< sal_uInt32 > (nElemFlags) ); |
| 486 |
|
| 487 |
SbxBase* pElemObject = pTypeElem->GetObject(); |
| 488 |
|
| 489 |
if (pElemObject) |
| 490 |
{ |
| 491 |
r.WriteInt16(1); // has elem Object |
| 492 |
|
| 493 |
if( dataType == SbxOBJECT ) |
| 494 |
{ |
| 495 |
// nested user defined types |
| 496 |
// declared before use, so it is ok to reference it by name on load |
| 497 |
SbxObject* pNestedType = static_cast< SbxObject* > ( pElemObject ); |
| 498 |
r.WriteUniOrByteString( pNestedType->GetClassName(), eCharSet ); |
| 499 |
} |
| 500 |
else |
| 501 |
{ |
| 502 |
// an array |
| 503 |
SbxDimArray* pArray = static_cast< SbxDimArray* > ( pElemObject ); |
| 504 |
|
| 505 |
bool bFixedSize = pArray->hasFixedSize(); |
| 506 |
if (bFixedSize) |
| 507 |
r.WriteInt16(1); |
| 508 |
else |
| 509 |
r.WriteInt16(0); |
| 510 |
|
| 511 |
sal_Int32 nDims = pArray->GetDims(); |
| 512 |
r.WriteInt32(nDims); |
| 513 |
|
| 514 |
for (sal_Int32 d = 1; d <= nDims; d++) |
| 515 |
{ |
| 516 |
sal_Int32 lBound; |
| 517 |
sal_Int32 uBound; |
| 518 |
pArray->GetDim(d, lBound, uBound); |
| 519 |
r.WriteInt32(lBound).WriteInt32(uBound); |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
else |
| 524 |
r.WriteInt16(0); // no elem Object |
| 525 |
|
| 526 |
} |
| 527 |
} |
| 528 |
SbiCloseRecord( r, nPos ); |
| 529 |
} |
| 530 |
} |
| 531 |
// Set overall length |
| 532 |
SbiCloseRecord( r, nStart ); |
| 533 |
if (!r.good()) |
| 534 |
{ |
| 535 |
bError = true; |
| 536 |
} |
| 537 |
return !bError; |
| 538 |
} |
| 539 |
|
| 540 |
void SbiImage::MakeStrings( short nSize ) |
| 541 |
{ |
| 542 |
nStringIdx = 0; |
| 543 |
nStringOff = 0; |
| 544 |
nStringSize = 1024; |
| 545 |
pStrings.reset( new sal_Unicode[ nStringSize ]); |
| 546 |
mvStringOffsets.resize(nSize); |
| 547 |
if (nSize != 0) { |
| 548 |
memset( mvStringOffsets.data(), 0, nSize * sizeof( sal_uInt32 ) ); |
| 549 |
} |
| 550 |
memset( pStrings.get(), 0, nStringSize * sizeof( sal_Unicode ) ); |
| 551 |
} |
| 552 |
|
| 553 |
// Add a string to StringPool. The String buffer is dynamically |
| 554 |
// growing in 1K-Steps |
| 555 |
void SbiImage::AddString( const OUString& r ) |
| 556 |
{ |
| 557 |
if( nStringIdx >= mvStringOffsets.size() ) |
| 558 |
{ |
| 559 |
bError = true; |
| 560 |
} |
| 561 |
if( bError ) |
| 562 |
return; |
| 563 |
|
| 564 |
sal_Int32 len = r.getLength() + 1; |
| 565 |
sal_uInt32 needed = nStringOff + len; |
| 566 |
if( needed > 0xFFFFFF00 ) |
| 567 |
{ |
| 568 |
bError = true; // out of mem! |
| 569 |
} |
| 570 |
else if( needed > nStringSize ) |
| 571 |
{ |
| 572 |
sal_uInt32 nNewLen = needed + 1024; |
| 573 |
nNewLen &= 0xFFFFFC00; // trim to 1K border |
| 574 |
std::unique_ptr<sal_Unicode[]> p(new sal_Unicode[nNewLen]); |
| 575 |
std::copy_n(pStrings.get(), nStringSize, p.get()); |
| 576 |
pStrings = std::move(p); |
| 577 |
nStringSize = sal::static_int_cast< sal_uInt16 >(nNewLen); |
| 578 |
} |
| 579 |
if( !bError ) |
| 580 |
{ |
| 581 |
mvStringOffsets[ nStringIdx++ ] = nStringOff; |
| 582 |
std::copy_n(r.getStr(), len, pStrings.get() + nStringOff); |
| 583 |
nStringOff = nStringOff + len; |
| 584 |
// Last String? The update the size of the buffer |
| 585 |
if( nStringIdx >= mvStringOffsets.size() ) |
| 586 |
{ |
| 587 |
nStringSize = nStringOff; |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
// Add code block |
| 593 |
// The block was fetched by the compiler from class SbBuffer and |
| 594 |
// is already created with new. Additionally it contains all Integers |
| 595 |
// in Big Endian format, so can be directly read/written. |
| 596 |
void SbiImage::AddCode(std::vector<sal_uInt8>&& v) |
| 597 |
{ |
| 598 |
aCode = std::move(v); |
| 599 |
} |
| 600 |
|
| 601 |
// Add user type |
| 602 |
void SbiImage::AddType(SbxObject const * pObject) |
| 603 |
{ |
| 604 |
if( !rTypes.is() ) |
| 605 |
{ |
| 606 |
rTypes = new SbxArray; |
| 607 |
} |
| 608 |
SbxObject *pCopyObject = new SbxObject(*pObject); |
| 609 |
rTypes->Insert(pCopyObject, rTypes->Count()); |
| 610 |
} |
| 611 |
|
| 612 |
void SbiImage::AddEnum(SbxObject* pObject) // Register enum type |
| 613 |
{ |
| 614 |
if( !rEnums.is() ) |
| 615 |
{ |
| 616 |
rEnums = new SbxArray; |
| 617 |
} |
| 618 |
rEnums->Insert(pObject, rEnums->Count()); |
| 619 |
} |
| 620 |
|
| 621 |
// Note: IDs start with 1 |
| 622 |
OUString SbiImage::GetString(sal_uInt32 nId, SbxDataType* peType) const |
| 623 |
{ |
| 624 |
if (nId < 1 || nId > mvStringOffsets.size()) |
| 625 |
{ |
| 626 |
if (peType) |
| 627 |
*peType = SbxEMPTY; |
| 628 |
return {}; |
| 629 |
} |
| 630 |
|
| 631 |
sal_uInt32 nOff = mvStringOffsets[ nId - 1 ]; |
| 632 |
sal_Unicode* pStr = pStrings.get() + nOff; |
| 633 |
|
| 634 |
sal_uInt32 nNextOff = (nId < mvStringOffsets.size()) ? mvStringOffsets[ nId ] : nStringSize; |
| 635 |
sal_uInt32 nLen = nNextOff - nOff - 1; |
| 636 |
OUString aOUStr(pStr); |
| 637 |
// #i42467: Special treatment for vbNullChar |
| 638 |
if (aOUStr.isEmpty() && nLen == 1) |
| 639 |
{ |
| 640 |
if (peType) |
| 641 |
*peType = SbxSTRING; |
| 642 |
return OUString(u'\0'); |
| 643 |
} |
| 644 |
|
| 645 |
// tdf#143707 - check if the data type character was added after the string termination |
| 646 |
// symbol. It was added in basic/source/comp/symtbl.cxx. |
| 647 |
if (peType) |
| 648 |
{ |
| 649 |
*peType = SbxSTRING; |
| 650 |
if (o3tl::make_unsigned(aOUStr.getLength()) < nLen) |
| 651 |
{ |
| 652 |
const sal_Unicode pTypeChar = *(pStr + aOUStr.getLength() + 1); |
| 653 |
switch (pTypeChar) |
| 654 |
{ |
| 655 |
// See GetSuffixType in basic/source/comp/scanner.cxx for type characters |
| 656 |
case '%': *peType = SbxINTEGER; break; |
| 657 |
case '&': *peType = SbxLONG; break; |
| 658 |
case '!': *peType = SbxSINGLE; break; |
| 659 |
case '#': *peType = SbxDOUBLE; break; |
| 660 |
case '@': *peType = SbxCURRENCY; break; |
| 661 |
// tdf#142460 - properly handle boolean values in string pool |
| 662 |
case 'b': *peType = SbxBOOL; break; // Not in GetSuffixType |
| 663 |
// tdf#168569 - support date values in string pool |
| 664 |
case 'd': *peType = SbxDATE; break; // Not in GetSuffixType |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
return aOUStr; |
| 669 |
} |
| 670 |
|
| 671 |
const SbxObject* SbiImage::FindType (const OUString& aTypeName) const |
| 672 |
{ |
| 673 |
return rTypes.is() ? static_cast<SbxObject*>(rTypes->Find(aTypeName,SbxClassType::Object)) : nullptr; |
| 674 |
} |
| 675 |
|
| 676 |
sal_uInt16 SbiImage::CalcLegacyOffset( sal_Int32 nOffset ) |
| 677 |
{ |
| 678 |
return SbiCodeGen::calcLegacyOffSet(aCode.data(), nOffset); |
| 679 |
} |
| 680 |
|
| 681 |
sal_uInt32 SbiImage::CalcNewOffset( sal_Int16 nOffset ) |
| 682 |
{ |
| 683 |
return SbiCodeGen::calcNewOffSet(aLegacyPCode.data(), nOffset); |
| 684 |
} |
| 685 |
|
| 686 |
void SbiImage::ReleaseLegacyBuffer() |
| 687 |
{ |
| 688 |
aLegacyPCode.clear(); |
| 689 |
} |
| 690 |
|
| 691 |
bool SbiImage::ExceedsLegacyLimits() |
| 692 |
{ |
| 693 |
return (nStringSize > 0xFF00) || (CalcLegacyOffset(aCode.size()) > 0xFF00); |
| 694 |
} |
| 695 |
|
| 696 |
bool SbiImage::ExceedsImgVersion12Limits() |
| 697 |
{ |
| 698 |
const sal_Int16 nMax = std::numeric_limits<sal_Int16>::max(); |
| 699 |
return nStringSize >= nMax || CalcLegacyOffset(aCode.size()) >= nMax; |
| 700 |
} |
| 701 |
|
| 702 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |