1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <algorithm>
#include <string.h>
#include <sal/log.hxx>
#include <osl/file.hxx>
#include <unotools/tempfile.hxx>
#include <set>
#include "sot/stg.hxx"
#include "stgelem.hxx"
#include "stgcache.hxx"
#include "stgstrms.hxx"
#include "stgdir.hxx"
#include "stgio.hxx"
#include <memory>
///////////////////////////// class StgFAT
// The FAT class performs FAT operations on an underlying storage stream.
// This stream is either the master FAT stream (m == true ) or a normal
// storage stream, which then holds the FAT for small data allocations.
StgFAT::StgFAT( StgStrm& r, bool m ) : m_rStrm( r )
{
m_bPhys = m;
m_nPageSize = m_rStrm.GetIo().GetPhysPageSize();
m_nEntries = m_nPageSize >> 2;
m_nOffset = 0;
m_nMaxPage = 0;
m_nLimit = 0;
}
// Retrieve the physical page for a given byte offset.
rtl::Reference< StgPage > StgFAT::GetPhysPage( sal_Int32 nByteOff )
{
rtl::Reference< StgPage > pPg;
// Position within the underlying stream
// use the Pos2Page() method of the stream
if( m_rStrm.Pos2Page( nByteOff ) )
{
m_nOffset = m_rStrm.GetOffset();
sal_Int32 nPhysPage = m_rStrm.GetPage();
// get the physical page (must be present)
pPg = m_rStrm.GetIo().Get( nPhysPage, true );
}
return pPg;
}
// Get the follow page for a certain FAT page.
sal_Int32 StgFAT::GetNextPage( sal_Int32 nPg )
{
if( nPg >= 0 )
{
rtl::Reference< StgPage > pPg = GetPhysPage( nPg << 2 );
nPg = pPg.is() ? StgCache::GetFromPage( pPg, m_nOffset >> 2 ) : STG_EOF;
}
return nPg;
}
// Find the best fit block for the given size. Return
// the starting block and its size or STG_EOF and 0.
// nLastPage is a stopper which tells the current
// underlying stream size. It is treated as a recommendation
// to abort the search to inhibit excessive file growth.
sal_Int32 StgFAT::FindBlock( sal_Int32& nPgs )
{
sal_Int32 nMinStart = STG_EOF, nMinLen = 0;
sal_Int32 nMaxStart = STG_EOF, nMaxLen = 0x7FFFFFFFL;
sal_Int32 nTmpStart = STG_EOF, nTmpLen = 0;
sal_Int32 nPages = m_rStrm.GetSize() >> 2;
bool bFound = false;
rtl::Reference< StgPage > pPg;
short nEntry = 0;
for( sal_Int32 i = 0; i < nPages; i++, nEntry++ )
{
if( !( nEntry % m_nEntries ) )
{
// load the next page for that stream
nEntry = 0;
pPg = GetPhysPage( i << 2 );
if( !pPg.is() )
return STG_EOF;
}
sal_Int32 nCur = StgCache::GetFromPage( pPg, nEntry );
if( nCur == STG_FREE )
{
// count the size of this area
if( nTmpLen )
nTmpLen++;
else
{
nTmpStart = i;
nTmpLen = 1;
}
if( nTmpLen == nPgs
// If we already did find a block, stop when reaching the limit
|| ( bFound && ( nEntry >= m_nLimit ) ) )
break;
}
else if( nTmpLen )
{
if( nTmpLen > nPgs && nTmpLen < nMaxLen )
{
// block > requested size
nMaxLen = nTmpLen;
nMaxStart = nTmpStart;
bFound = true;
}
else if( nTmpLen >= nMinLen )
{
// block < requested size
nMinLen = nTmpLen;
nMinStart = nTmpStart;
bFound = true;
if( nTmpLen == nPgs )
break;
}
nTmpStart = STG_EOF;
nTmpLen = 0;
}
}
// Determine which block to use.
if( nTmpLen )
{
if( nTmpLen > nPgs && nTmpLen < nMaxLen )
{
// block > requested size
nMaxLen = nTmpLen;
nMaxStart = nTmpStart;
}
else if( nTmpLen >= nMinLen )
{
// block < requested size
nMinLen = nTmpLen;
nMinStart = nTmpStart;
}
}
if( nMinStart != STG_EOF && nMaxStart != STG_EOF )
{
// two areas found; return the best fit area
sal_Int32 nMinDiff = nPgs - nMinLen;
sal_Int32 nMaxDiff = nMaxLen - nPgs;
if( nMinDiff > nMaxDiff )
nMinStart = STG_EOF;
}
if( nMinStart != STG_EOF )
{
nPgs = nMinLen; return nMinStart;
}
else
{
return nMaxStart;
}
}
// Set up the consecutive chain for a given block.
bool StgFAT::MakeChain( sal_Int32 nStart, sal_Int32 nPgs )
{
sal_Int32 nPos = nStart << 2;
rtl::Reference< StgPage > pPg = GetPhysPage( nPos );
if( !pPg.is() || !nPgs )
return false;
while( --nPgs )
{
if( m_nOffset >= m_nPageSize )
{
pPg = GetPhysPage( nPos );
if( !pPg.is() )
return false;
}
m_rStrm.GetIo().SetToPage( pPg, m_nOffset >> 2, ++nStart );
m_nOffset += 4;
nPos += 4;
}
if( m_nOffset >= m_nPageSize )
{
pPg = GetPhysPage( nPos );
if( !pPg.is() )
return false;
}
m_rStrm.GetIo().SetToPage( pPg, m_nOffset >> 2, STG_EOF );
return true;
}
// Allocate a block of data from the given page number on.
// It the page number is != STG_EOF, chain the block.
sal_Int32 StgFAT::AllocPages( sal_Int32 nBgn, sal_Int32 nPgs )
{
sal_Int32 nOrig = nBgn;
sal_Int32 nLast = nBgn;
sal_Int32 nBegin = STG_EOF;
sal_Int32 nAlloc;
sal_Int32 nPages = m_rStrm.GetSize() >> 2;
short nPasses = 0;
// allow for two passes
while( nPasses < 2 )
{
// try to satisfy the request from the pool of free pages
while( nPgs )
{
nAlloc = nPgs;
nBegin = FindBlock( nAlloc );
// no more blocks left in present alloc chain
if( nBegin == STG_EOF )
break;
if( ( nBegin + nAlloc ) > m_nMaxPage )
m_nMaxPage = nBegin + nAlloc;
if( !MakeChain( nBegin, nAlloc ) )
return STG_EOF;
if( nOrig == STG_EOF )
nOrig = nBegin;
else
{
// Patch the chain
rtl::Reference< StgPage > pPg = GetPhysPage( nLast << 2 );
if( !pPg.is() )
return STG_EOF;
m_rStrm.GetIo().SetToPage( pPg, m_nOffset >> 2, nBegin );
}
nLast = nBegin + nAlloc - 1;
nPgs -= nAlloc;
}
if( nPgs && !nPasses )
{
// we need new, fresh space, so allocate and retry
if( !m_rStrm.SetSize( ( nPages + nPgs ) << 2 ) )
return STG_EOF;
if( !m_bPhys && !InitNew( nPages ) )
return 0;
// FIXME: this was originally "FALSE", whether or not that
// makes sense (or should be STG_EOF instead, say?)
nPages = m_rStrm.GetSize() >> 2;
nPasses++;
}
else
break;
}
// now we should have a chain for the complete block
if( nBegin == STG_EOF || nPgs )
{
m_rStrm.GetIo().SetError( SVSTREAM_FILEFORMAT_ERROR );
return STG_EOF; // bad structure
}
return nOrig;
}
// Initialize newly allocated pages for a standard FAT stream
// It can be assumed that the stream size is always on
// a page boundary
bool StgFAT::InitNew( sal_Int32 nPage1 )
{
sal_Int32 n = ( ( m_rStrm.GetSize() >> 2 ) - nPage1 ) / m_nEntries;
if ( n > 0 )
{
while( n-- )
{
rtl::Reference< StgPage > pPg;
// Position within the underlying stream
// use the Pos2Page() method of the stream
m_rStrm.Pos2Page( nPage1 << 2 );
// Initialize the page
pPg = m_rStrm.GetIo().Copy( m_rStrm.GetPage() );
if ( !pPg.is() )
return false;
for( short i = 0; i < m_nEntries; i++ )
m_rStrm.GetIo().SetToPage( pPg, i, STG_FREE );
nPage1++;
}
}
return true;
}
// Release a chain
bool StgFAT::FreePages( sal_Int32 nStart, bool bAll )
{
while( nStart >= 0 )
{
rtl::Reference< StgPage > pPg = GetPhysPage( nStart << 2 );
if( !pPg.is() )
return false;
nStart = StgCache::GetFromPage( pPg, m_nOffset >> 2 );
// The first released page is either set to EOF or FREE
m_rStrm.GetIo().SetToPage( pPg, m_nOffset >> 2, bAll ? STG_FREE : STG_EOF );
bAll = true;
}
return true;
}
///////////////////////////// class StgStrm
// The base stream class provides basic functionality for seeking
// and accessing the data on a physical basis. It uses the built-in
// FAT class for the page allocations.
StgStrm::StgStrm( StgIo& r ) : m_rIo( r )
{
m_pFat = nullptr;
m_nStart = m_nPage = STG_EOF;
m_nOffset = 0;
m_pEntry = nullptr;
m_nPos = m_nSize = 0;
m_nPageSize = m_rIo.GetPhysPageSize();
}
StgStrm::~StgStrm()
{
delete m_pFat;
}
// Attach the stream to the given entry.
void StgStrm::SetEntry( StgDirEntry& r )
{
r.m_aEntry.SetLeaf( STG_DATA, m_nStart );
r.m_aEntry.SetSize( m_nSize );
m_pEntry = &r;
r.SetDirty();
}
/*
* The page chain, is basically a singly linked list of slots each
* point to the next page. Instead of traversing the file structure
* for this each time build a simple flat in-memory vector list
* of pages.
*/
void StgStrm::scanBuildPageChainCache(sal_Int32 *pOptionalCalcSize)
{
if (m_nSize > 0)
m_aPagesCache.reserve(m_nSize/m_nPageSize);
bool bError = false;
sal_Int32 nBgn = m_nStart;
sal_Int32 nOptSize = 0;
// Track already scanned PageNumbers here and use them to
// see if an already counted page is re-visited
std::set< sal_Int32 > nUsedPageNumbers;
while( nBgn >= 0 && !bError )
{
if( nBgn >= 0 )
m_aPagesCache.push_back(nBgn);
nBgn = m_pFat->GetNextPage( nBgn );
//returned second is false if it already exists
if (!nUsedPageNumbers.insert(nBgn).second)
{
SAL_WARN ("sot", "Error: page number " << nBgn << " already in chain for stream");
bError = true;
}
nOptSize += m_nPageSize;
}
if (bError)
{
SAL_WARN("sot", "returning wrong format error");
if (pOptionalCalcSize)
m_rIo.SetError( ERRCODE_IO_WRONGFORMAT );
m_aPagesCache.clear();
}
if (pOptionalCalcSize)
*pOptionalCalcSize = nOptSize;
}
// Compute page number and offset for the given byte position.
// If the position is behind the size, set the stream right
// behind the EOF.
bool StgStrm::Pos2Page( sal_Int32 nBytePos )
{
if ( !m_pFat )
return false;
// Values < 0 seek to the end
if( nBytePos < 0 || nBytePos >= m_nSize )
nBytePos = m_nSize;
// Adjust the position back to offset 0
m_nPos -= m_nOffset;
sal_Int32 nMask = ~( m_nPageSize - 1 );
sal_Int32 nOld = m_nPos & nMask;
sal_Int32 nNew = nBytePos & nMask;
m_nOffset = (short) ( nBytePos & ~nMask );
m_nPos = nBytePos;
if( nOld == nNew )
return true;
// See fdo#47644 for a .doc with a vast amount of pages where seeking around the
// document takes a colossal amount of time
// Please Note: we build the pagescache incrementally as we go if necessary,
// so that a corrupted FAT doesn't poison the stream state for earlier reads
size_t nIdx = nNew / m_nPageSize;
if( nIdx >= m_aPagesCache.size() )
{
// Extend the FAT cache ! ...
size_t nToAdd = nIdx + 1;
if (m_aPagesCache.empty())
m_aPagesCache.push_back( m_nStart );
nToAdd -= m_aPagesCache.size();
sal_Int32 nBgn = m_aPagesCache.back();
// Start adding pages while we can
while( nToAdd > 0 && nBgn >= 0 )
{
nBgn = m_pFat->GetNextPage( nBgn );
if( nBgn >= 0 )
{
m_aPagesCache.push_back( nBgn );
nToAdd--;
}
}
}
if ( nIdx > m_aPagesCache.size() )
{
SAL_WARN("sot", "seek to index " << nIdx <<
" beyond page cache size " << m_aPagesCache.size());
// fdo#84229 - handle seek to end and back as eg. XclImpStream expects
m_nPage = STG_EOF;
m_nOffset = 0;
// Intriguingly in the past we didn't reset nPos to match the real
// length of the stream thus:
// nIdx = m_aPagesCache.size();
// nPos = nPageSize * nIdx;
// so retain this behavior for now.
return false;
}
// special case: seek to 1st byte of new, unallocated page
// (in case the file size is a multiple of the page size)
if( nBytePos == m_nSize && !m_nOffset && nIdx > 0 && nIdx == m_aPagesCache.size() )
{
nIdx--;
m_nOffset = m_nPageSize;
}
else if ( nIdx == m_aPagesCache.size() )
{
m_nPage = STG_EOF;
return false;
}
m_nPage = m_aPagesCache[ nIdx ];
return m_nPage >= 0;
}
// Copy an entire stream. Both streams are allocated in the FAT.
// The target stream is this stream.
bool StgStrm::Copy( sal_Int32 nFrom, sal_Int32 nBytes )
{
if ( !m_pFat )
return false;
m_aPagesCache.clear();
sal_Int32 nTo = m_nStart;
sal_Int32 nPgs = ( nBytes + m_nPageSize - 1 ) / m_nPageSize;
while( nPgs-- )
{
if( nTo < 0 )
{
m_rIo.SetError( SVSTREAM_FILEFORMAT_ERROR );
return false;
}
m_rIo.Copy( nTo, nFrom );
if( nFrom >= 0 )
{
nFrom = m_pFat->GetNextPage( nFrom );
if( nFrom < 0 )
{
m_rIo.SetError( SVSTREAM_FILEFORMAT_ERROR );
return false;
}
}
nTo = m_pFat->GetNextPage( nTo );
}
return true;
}
bool StgStrm::SetSize( sal_Int32 nBytes )
{
if ( nBytes < 0 || !m_pFat )
return false;
m_aPagesCache.clear();
// round up to page size
sal_Int32 nOld = ( ( m_nSize + m_nPageSize - 1 ) / m_nPageSize ) * m_nPageSize;
sal_Int32 nNew = ( ( nBytes + m_nPageSize - 1 ) / m_nPageSize ) * m_nPageSize;
if( nNew > nOld )
{
if( !Pos2Page( m_nSize ) )
return false;
sal_Int32 nBgn = m_pFat->AllocPages( m_nPage, ( nNew - nOld ) / m_nPageSize );
if( nBgn == STG_EOF )
return false;
if( m_nStart == STG_EOF )
m_nStart = m_nPage = nBgn;
}
else if( nNew < nOld )
{
bool bAll = ( nBytes == 0 );
if( !Pos2Page( nBytes ) || !m_pFat->FreePages( m_nPage, bAll ) )
return false;
if( bAll )
m_nStart = m_nPage = STG_EOF;
}
if( m_pEntry )
{
// change the dir entry?
if( !m_nSize || !nBytes )
m_pEntry->m_aEntry.SetLeaf( STG_DATA, m_nStart );
m_pEntry->m_aEntry.SetSize( nBytes );
m_pEntry->SetDirty();
}
m_nSize = nBytes;
m_pFat->SetLimit( GetPages() );
return true;
}
// Return the # of allocated pages
//////////////////////////// class StgFATStrm
// The FAT stream class provides physical access to the master FAT.
// Since this access is implemented as a StgStrm, we can use the
// FAT allocator.
StgFATStrm::StgFATStrm( StgIo& r ) : StgStrm( r )
{
m_pFat = new StgFAT( *this, true );
m_nSize = m_rIo.m_aHdr.GetFATSize() * m_nPageSize;
}
bool StgFATStrm::Pos2Page( sal_Int32 nBytePos )
{
// Values < 0 seek to the end
if( nBytePos < 0 || nBytePos >= m_nSize )
nBytePos = m_nSize ? m_nSize - 1 : 0;
m_nPage = nBytePos / m_nPageSize;
m_nOffset = (short) ( nBytePos % m_nPageSize );
m_nPos = nBytePos;
m_nPage = GetPage( (short) m_nPage, false );
return m_nPage >= 0;
}
// Get the page number entry for the given page offset.
sal_Int32 StgFATStrm::GetPage( short nOff, bool bMake, sal_uInt16 *pnMasterAlloc )
{
OSL_ENSURE( nOff >= 0, "The offset may not be negative!" );
if( pnMasterAlloc ) *pnMasterAlloc = 0;
if( nOff < StgHeader::GetFAT1Size() )
return m_rIo.m_aHdr.GetFATPage( nOff );
sal_Int32 nMaxPage = m_nSize >> 2;
nOff = nOff - StgHeader::GetFAT1Size();
// Anzahl der Masterpages, durch die wir iterieren muessen
sal_uInt16 nMasterCount = ( m_nPageSize >> 2 ) - 1;
sal_uInt16 nBlocks = nOff / nMasterCount;
// Offset in letzter Masterpage
nOff = nOff % nMasterCount;
rtl::Reference< StgPage > pOldPage;
rtl::Reference< StgPage > pMaster;
sal_Int32 nFAT = m_rIo.m_aHdr.GetFATChain();
for( sal_uInt16 nCount = 0; nCount <= nBlocks; nCount++ )
{
if( nFAT == STG_EOF || nFAT == STG_FREE )
{
if( bMake )
{
m_aPagesCache.clear();
// create a new master page
nFAT = nMaxPage++;
pMaster = m_rIo.Copy( nFAT );
if ( pMaster.is() )
{
for( short k = 0; k < (short)( m_nPageSize >> 2 ); k++ )
m_rIo.SetToPage( pMaster, k, STG_FREE );
// Verkettung herstellen
if( !pOldPage.is() )
m_rIo.m_aHdr.SetFATChain( nFAT );
else
m_rIo.SetToPage( pOldPage, nMasterCount, nFAT );
if( nMaxPage >= m_rIo.GetPhysPages() )
if( !m_rIo.SetSize( nMaxPage ) )
return STG_EOF;
// mark the page as used
// Platz fuer Masterpage schaffen
if( !pnMasterAlloc ) // Selbst Platz schaffen
{
if( !Pos2Page( nFAT << 2 ) )
return STG_EOF;
rtl::Reference< StgPage > pPg = m_rIo.Get( m_nPage, true );
if( !pPg.is() )
return STG_EOF;
m_rIo.SetToPage( pPg, m_nOffset >> 2, STG_MASTER );
}
else
(*pnMasterAlloc)++;
m_rIo.m_aHdr.SetMasters( nCount + 1 );
pOldPage = pMaster;
}
}
}
else
{
pMaster = m_rIo.Get( nFAT, true );
if ( pMaster.is() )
{
nFAT = StgCache::GetFromPage( pMaster, nMasterCount );
pOldPage = pMaster;
}
}
}
if( pMaster.is() )
return StgCache::GetFromPage( pMaster, nOff );
m_rIo.SetError( SVSTREAM_GENERALERROR );
return STG_EOF;
}
// Set the page number entry for the given page offset.
bool StgFATStrm::SetPage( short nOff, sal_Int32 nNewPage )
{
OSL_ENSURE( nOff >= 0, "The offset may not be negative!" );
m_aPagesCache.clear();
bool bRes = true;
if( nOff < StgHeader::GetFAT1Size() )
m_rIo.m_aHdr.SetFATPage( nOff, nNewPage );
else
{
nOff = nOff - StgHeader::GetFAT1Size();
// Anzahl der Masterpages, durch die wir iterieren muessen
sal_uInt16 nMasterCount = ( m_nPageSize >> 2 ) - 1;
sal_uInt16 nBlocks = nOff / nMasterCount;
// Offset in letzter Masterpage
nOff = nOff % nMasterCount;
rtl::Reference< StgPage > pMaster;
sal_Int32 nFAT = m_rIo.m_aHdr.GetFATChain();
for( sal_uInt16 nCount = 0; nCount <= nBlocks; nCount++ )
{
if( nFAT == STG_EOF || nFAT == STG_FREE )
{
pMaster = nullptr;
break;
}
pMaster = m_rIo.Get( nFAT, true );
if ( pMaster.is() )
nFAT = StgCache::GetFromPage( pMaster, nMasterCount );
}
if( pMaster.is() )
m_rIo.SetToPage( pMaster, nOff, nNewPage );
else
{
m_rIo.SetError( SVSTREAM_GENERALERROR );
bRes = false;
}
}
// lock the page against access
if( bRes )
{
Pos2Page( nNewPage << 2 );
rtl::Reference< StgPage > pPg = m_rIo.Get( m_nPage, true );
if( pPg.is() )
m_rIo.SetToPage( pPg, m_nOffset >> 2, STG_FAT );
else
bRes = false;
}
return bRes;
}
bool StgFATStrm::SetSize( sal_Int32 nBytes )
{
if ( nBytes < 0 )
return false;
m_aPagesCache.clear();
// Set the number of entries to a multiple of the page size
short nOld = (short) ( ( m_nSize + ( m_nPageSize - 1 ) ) / m_nPageSize );
short nNew = (short) (
( nBytes + ( m_nPageSize - 1 ) ) / m_nPageSize ) ;
if( nNew < nOld )
{
// release master pages
for( short i = nNew; i < nOld; i++ )
SetPage( i, STG_FREE );
}
else
{
while( nOld < nNew )
{
// allocate master pages
// find a free master page slot
sal_Int32 nPg = 0;
sal_uInt16 nMasterAlloc = 0;
nPg = GetPage( nOld, true, &nMasterAlloc );
if( nPg == STG_EOF )
return false;
// 4 Bytes have been used for Allocation of each MegaMasterPage
nBytes += nMasterAlloc << 2;
// find a free page using the FAT allocator
sal_Int32 n = 1;
OSL_ENSURE( m_pFat, "The pointer is always initializer here!" );
sal_Int32 nNewPage = m_pFat->FindBlock( n );
if( nNewPage == STG_EOF )
{
// no free pages found; create a new page
// Since all pages are allocated, extend
// the file size for the next page!
nNewPage = m_nSize >> 2;
// if a MegaMasterPage was created avoid taking
// the same Page
nNewPage += nMasterAlloc;
// adjust the file size if necessary
if( nNewPage >= m_rIo.GetPhysPages() )
if( !m_rIo.SetSize( nNewPage + 1 ) )
return false;
}
// Set up the page with empty entries
rtl::Reference< StgPage > pPg = m_rIo.Copy( nNewPage );
if ( !pPg.is() )
return false;
for( short j = 0; j < (short)( m_nPageSize >> 2 ); j++ )
m_rIo.SetToPage( pPg, j, STG_FREE );
// store the page number into the master FAT
// Set the size before so the correct FAT can be found
m_nSize = ( nOld + 1 ) * m_nPageSize;
SetPage( nOld, nNewPage );
// MegaMasterPages were created, mark it them as used
sal_uInt32 nMax = m_rIo.m_aHdr.GetMasters( );
sal_uInt32 nFAT = m_rIo.m_aHdr.GetFATChain();
if( nMasterAlloc )
for( sal_uInt32 nCount = 0; nCount < nMax; nCount++ )
{
if( !Pos2Page( nFAT << 2 ) )
return false;
if( nMax - nCount <= nMasterAlloc )
{
rtl::Reference< StgPage > piPg = m_rIo.Get( m_nPage, true );
if( !piPg.is() )
return false;
m_rIo.SetToPage( piPg, m_nOffset >> 2, STG_MASTER );
}
rtl::Reference< StgPage > pPage = m_rIo.Get( nFAT, true );
if( !pPage.is() ) return false;
nFAT = StgCache::GetFromPage( pPage, (m_nPageSize >> 2 ) - 1 );
}
nOld++;
// We have used up 4 bytes for the STG_FAT entry
nBytes += 4;
nNew = (short) (
( nBytes + ( m_nPageSize - 1 ) ) / m_nPageSize );
}
}
m_nSize = nNew * m_nPageSize;
m_rIo.m_aHdr.SetFATSize( nNew );
return true;
}
/////////////////////////// class StgDataStrm
// This class is a normal physical stream which can be initialized
// either with an existing dir entry or an existing FAT chain.
// The stream has a size increment which normally is 1, but which can be
// set to any value is you want the size to be incremented by certain values.
StgDataStrm::StgDataStrm( StgIo& r, sal_Int32 nBgn, sal_Int32 nLen ) : StgStrm( r )
{
Init( nBgn, nLen );
}
StgDataStrm::StgDataStrm( StgIo& r, StgDirEntry& p ) : StgStrm( r )
{
m_pEntry = &p;
Init( p.m_aEntry.GetLeaf( STG_DATA ),
p.m_aEntry.GetSize() );
}
void StgDataStrm::Init( sal_Int32 nBgn, sal_Int32 nLen )
{
if ( m_rIo.m_pFAT )
m_pFat = new StgFAT( *m_rIo.m_pFAT, true );
OSL_ENSURE( m_pFat, "The pointer should not be empty!" );
m_nStart = m_nPage = nBgn;
m_nSize = nLen;
m_nIncr = 1;
m_nOffset = 0;
if( nLen < 0 && m_pFat )
{
// determine the actual size of the stream by scanning
// the FAT chain and counting the # of pages allocated
scanBuildPageChainCache( &m_nSize );
}
}
// Set the size of a physical stream.
bool StgDataStrm::SetSize( sal_Int32 nBytes )
{
if ( !m_pFat )
return false;
nBytes = ( ( nBytes + m_nIncr - 1 ) / m_nIncr ) * m_nIncr;
sal_Int32 nOldSz = m_nSize;
if( ( nOldSz != nBytes ) )
{
if( !StgStrm::SetSize( nBytes ) )
return false;
sal_Int32 nMaxPage = m_pFat->GetMaxPage();
if( nMaxPage > m_rIo.GetPhysPages() )
if( !m_rIo.SetSize( nMaxPage ) )
return false;
// If we only allocated one page or less, create this
// page in the cache for faster throughput. The current
// position is the former EOF point.
if( ( m_nSize - 1 ) / m_nPageSize - ( nOldSz - 1 ) / m_nPageSize == 1 )
{
Pos2Page( nBytes );
if( m_nPage >= 0 )
m_rIo.Copy( m_nPage );
}
}
return true;
}
// Get the address of the data byte at a specified offset.
// If bForce = true, a read of non-existent data causes
// a read fault.
void* StgDataStrm::GetPtr( sal_Int32 Pos, bool bDirty )
{
if( Pos2Page( Pos ) )
{
rtl::Reference< StgPage > pPg = m_rIo.Get( m_nPage, true/*bForce*/ );
if (pPg.is() && m_nOffset < pPg->GetSize())
{
if( bDirty )
m_rIo.SetDirty( pPg );
return static_cast<sal_uInt8 *>(pPg->GetData()) + m_nOffset;
}
}
return nullptr;
}
// This could easily be adapted to a better algorithm by determining
// the amount of consecutable blocks before doing a read. The result
// is the number of bytes read. No error is generated on EOF.
sal_Int32 StgDataStrm::Read( void* pBuf, sal_Int32 n )
{
if ( n < 0 )
return 0;
if( ( m_nPos + n ) > m_nSize )
n = m_nSize - m_nPos;
sal_Int32 nDone = 0;
while( n )
{
short nBytes = m_nPageSize - m_nOffset;
rtl::Reference< StgPage > pPg;
if( (sal_Int32) nBytes > n )
nBytes = (short) n;
if( nBytes )
{
short nRes;
void *p = static_cast<sal_uInt8 *>(pBuf) + nDone;
if( nBytes == m_nPageSize )
{
pPg = m_rIo.Find( m_nPage );
if( pPg.is() )
{
// data is present, so use the cached data
memcpy( p, pPg->GetData(), nBytes );
nRes = nBytes;
}
else
// do a direct (unbuffered) read
nRes = (short) m_rIo.Read( m_nPage, p ) * m_nPageSize;
}
else
{
// partial block read through the cache.
pPg = m_rIo.Get( m_nPage, false );
if( !pPg.is() )
break;
memcpy( p, static_cast<sal_uInt8*>(pPg->GetData()) + m_nOffset, nBytes );
nRes = nBytes;
}
nDone += nRes;
m_nPos += nRes;
n -= nRes;
m_nOffset = m_nOffset + nRes;
if( nRes != nBytes )
break; // read error or EOF
}
// Switch to next page if necessary
if( m_nOffset >= m_nPageSize && !Pos2Page( m_nPos ) )
break;
}
return nDone;
}
sal_Int32 StgDataStrm::Write( const void* pBuf, sal_Int32 n )
{
if ( n < 0 )
return 0;
sal_Int32 nDone = 0;
if( ( m_nPos + n ) > m_nSize )
{
sal_Int32 nOld = m_nPos;
if( !SetSize( m_nPos + n ) )
return 0;
Pos2Page( nOld );
}
while( n )
{
short nBytes = m_nPageSize - m_nOffset;
rtl::Reference< StgPage > pPg;
if( (sal_Int32) nBytes > n )
nBytes = (short) n;
if( nBytes )
{
short nRes;
const void *p = static_cast<const sal_uInt8 *>(pBuf) + nDone;
if( nBytes == m_nPageSize )
{
pPg = m_rIo.Find( m_nPage );
if( pPg.is() )
{
// data is present, so use the cached data
memcpy( pPg->GetData(), p, nBytes );
m_rIo.SetDirty( pPg );
nRes = nBytes;
}
else
// do a direct (unbuffered) write
nRes = (short) m_rIo.Write( m_nPage, const_cast<void*>(p) ) * m_nPageSize;
}
else
{
// partial block read through the cache.
pPg = m_rIo.Get( m_nPage, false );
if( !pPg.is() )
break;
memcpy( static_cast<sal_uInt8*>(pPg->GetData()) + m_nOffset, p, nBytes );
m_rIo.SetDirty( pPg );
nRes = nBytes;
}
nDone += nRes;
m_nPos += nRes;
n -= nRes;
m_nOffset = m_nOffset + nRes;
if( nRes != nBytes )
break; // read error
}
// Switch to next page if necessary
if( m_nOffset >= m_nPageSize && !Pos2Page( m_nPos ) )
break;
}
return nDone;
}
//////////////////////////// class StgSmallStream
// The small stream class provides access to streams with a size < 4096 bytes.
// This stream is a StgStream containing small pages. The FAT for this stream
// is also a StgStream. The start of the FAT is in the header at DataRootPage,
// the stream itself is pointed to by the root entry (it holds start & size).
StgSmallStrm::StgSmallStrm( StgIo& r, sal_Int32 nBgn ) : StgStrm( r )
{
Init( nBgn, 0 );
}
StgSmallStrm::StgSmallStrm( StgIo& r, StgDirEntry& p ) : StgStrm( r )
{
m_pEntry = &p;
Init( p.m_aEntry.GetLeaf( STG_DATA ),
p.m_aEntry.GetSize() );
}
void StgSmallStrm::Init( sal_Int32 nBgn, sal_Int32 nLen )
{
if ( m_rIo.m_pDataFAT )
m_pFat = new StgFAT( *m_rIo.m_pDataFAT, false );
m_pData = m_rIo.m_pDataStrm;
OSL_ENSURE( m_pFat && m_pData, "The pointers should not be empty!" );
m_nPageSize = m_rIo.GetDataPageSize();
m_nStart =
m_nPage = nBgn;
m_nSize = nLen;
}
// This could easily be adapted to a better algorithm by determining
// the amount of consecutable blocks before doing a read. The result
// is the number of bytes read. No error is generated on EOF.
sal_Int32 StgSmallStrm::Read( void* pBuf, sal_Int32 n )
{
// We can safely assume that reads are not huge, since the
// small stream is likely to be < 64 KBytes.
if( ( m_nPos + n ) > m_nSize )
n = m_nSize - m_nPos;
sal_Int32 nDone = 0;
while( n )
{
short nBytes = m_nPageSize - m_nOffset;
if( (sal_Int32) nBytes > n )
nBytes = (short) n;
if( nBytes )
{
if( !m_pData || !m_pData->Pos2Page( m_nPage * m_nPageSize + m_nOffset ) )
break;
// all reading through the stream
short nRes = (short) m_pData->Read( static_cast<sal_uInt8*>(pBuf) + nDone, nBytes );
nDone += nRes;
m_nPos += nRes;
n -= nRes;
m_nOffset = m_nOffset + nRes;
// read problem?
if( nRes != nBytes )
break;
}
// Switch to next page if necessary
if( m_nOffset >= m_nPageSize && !Pos2Page( m_nPos ) )
break;
}
return nDone;
}
sal_Int32 StgSmallStrm::Write( const void* pBuf, sal_Int32 n )
{
// you can safely assume that reads are not huge, since the
// small stream is likely to be < 64 KBytes.
sal_Int32 nDone = 0;
if( ( m_nPos + n ) > m_nSize )
{
sal_Int32 nOld = m_nPos;
if( !SetSize( m_nPos + n ) )
return 0;
Pos2Page( nOld );
}
while( n )
{
short nBytes = m_nPageSize - m_nOffset;
if( (sal_Int32) nBytes > n )
nBytes = (short) n;
if( nBytes )
{
// all writing goes through the stream
sal_Int32 nDataPos = m_nPage * m_nPageSize + m_nOffset;
if ( !m_pData
|| ( m_pData->GetSize() < ( nDataPos + nBytes )
&& !m_pData->SetSize( nDataPos + nBytes ) ) )
break;
if( !m_pData->Pos2Page( nDataPos ) )
break;
short nRes = (short) m_pData->Write( static_cast<sal_uInt8 const *>(pBuf) + nDone, nBytes );
nDone += nRes;
m_nPos += nRes;
n -= nRes;
m_nOffset = m_nOffset + nRes;
// write problem?
if( nRes != nBytes )
break;
}
// Switch to next page if necessary
if( m_nOffset >= m_nPageSize && !Pos2Page( m_nPos ) )
break;
}
return nDone;
}
/////////////////////////// class StgTmpStrm
// The temporary stream uses a memory stream if < 32K, otherwise a
// temporary file.
#define THRESHOLD 32768L
StgTmpStrm::StgTmpStrm( sal_uInt64 nInitSize )
: SvMemoryStream( nInitSize > THRESHOLD
? 16
: ( nInitSize ? nInitSize : 16 ), 4096 )
{
m_pStrm = nullptr;
// this calls FlushData, so all members should be set by this time
SetBufferSize( 0 );
if( nInitSize > THRESHOLD )
SetSize( nInitSize );
}
bool StgTmpStrm::Copy( StgTmpStrm& rSrc )
{
sal_uInt64 n = rSrc.GetSize();
const sal_uInt64 nCur = rSrc.Tell();
SetSize( n );
if( GetError() == SVSTREAM_OK )
{
std::unique_ptr<sal_uInt8[]> p(new sal_uInt8[ 4096 ]);
rSrc.Seek( 0L );
Seek( 0L );
while( n )
{
const sal_uInt64 nn = std::min<sal_uInt64>(n, 4096);
if( rSrc.Read( p.get(), nn ) != nn )
break;
if( Write( p.get(), nn ) != nn )
break;
n -= nn;
}
p.reset();
rSrc.Seek( nCur );
Seek( nCur );
return n == 0;
}
else
return false;
}
StgTmpStrm::~StgTmpStrm()
{
if( m_pStrm )
{
m_pStrm->Close();
osl::File::remove( m_aName );
delete m_pStrm;
}
}
sal_uInt64 StgTmpStrm::GetSize() const
{
sal_uInt64 n;
if( m_pStrm )
{
sal_uInt64 old = m_pStrm->Tell();
n = m_pStrm->Seek( STREAM_SEEK_TO_END );
m_pStrm->Seek( old );
}
else
n = nEndOfData;
return n;
}
void StgTmpStrm::SetSize(sal_uInt64 n)
{
if( m_pStrm )
m_pStrm->SetStreamSize( n );
else
{
if( n > THRESHOLD )
{
m_aName = utl::TempFile(nullptr, false).GetURL();
SvFileStream* s = new SvFileStream( m_aName, STREAM_READWRITE );
const sal_uInt64 nCur = Tell();
sal_uInt64 i = nEndOfData;
std::unique_ptr<sal_uInt8[]> p(new sal_uInt8[ 4096 ]);
if( i )
{
Seek( 0L );
while( i )
{
const sal_uInt64 nb = std::min<sal_uInt64>(i, 4096);
if( Read( p.get(), nb ) == nb
&& s->Write( p.get(), nb ) == nb )
i -= nb;
else
break;
}
}
if( !i && n > nEndOfData )
{
// We have to write one byte at the end of the file
// if the file is bigger than the memstream to see
// if it fits on disk
s->Seek(nEndOfData);
memset(p.get(), 0x00, 4096);
i = n - nEndOfData;
while (i)
{
const sal_uInt64 nb = std::min<sal_uInt64>(i, 4096);
if (s->Write(p.get(), nb) == nb)
i -= nb;
else
break; // error
}
s->Flush();
if( s->GetError() != SVSTREAM_OK )
i = 1;
}
Seek( nCur );
s->Seek( nCur );
if( i )
{
SetError( s->GetError() );
delete s;
return;
}
m_pStrm = s;
// Shrink the memory to 16 bytes, which seems to be the minimum
ReAllocateMemory( - ( (long) nEndOfData - 16 ) );
}
else
{
if( n > nEndOfData )
{
SvMemoryStream::SetSize(n);
}
else
nEndOfData = n;
}
}
}
sal_Size StgTmpStrm::GetData( void* pData, sal_Size n )
{
if( m_pStrm )
{
n = m_pStrm->Read( pData, n );
SetError( m_pStrm->GetError() );
return n;
}
else
return SvMemoryStream::GetData( pData, n );
}
sal_Size StgTmpStrm::PutData( const void* pData, sal_Size n )
{
sal_uInt32 nCur = Tell();
sal_uInt32 nNew = nCur + n;
if( nNew > THRESHOLD && !m_pStrm )
{
SetSize( nNew );
if( GetError() != SVSTREAM_OK )
return 0;
}
if( m_pStrm )
{
nNew = m_pStrm->Write( pData, n );
SetError( m_pStrm->GetError() );
}
else
nNew = SvMemoryStream::PutData( pData, n );
return nNew;
}
sal_uInt64 StgTmpStrm::SeekPos(sal_uInt64 n)
{
// check if a truncated STREAM_SEEK_TO_END was passed
assert(n != SAL_MAX_UINT32);
if( n == STREAM_SEEK_TO_END )
n = GetSize();
if( n && n > THRESHOLD && !m_pStrm )
{
SetSize( n );
if( GetError() != SVSTREAM_OK )
return Tell();
else
return n;
}
else if( m_pStrm )
{
n = m_pStrm->Seek( n );
SetError( m_pStrm->GetError() );
return n;
}
else
return SvMemoryStream::SeekPos( n );
}
void StgTmpStrm::FlushData()
{
if( m_pStrm )
{
m_pStrm->Flush();
SetError( m_pStrm->GetError() );
}
else
SvMemoryStream::FlushData();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|