Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / source / core / tool / rechead.cxx
blobabd44d04194a8e1338002569f660b8d4c38b2ce3
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 <rechead.hxx>
21 #include <scerrors.hxx>
23 #include <osl/diagnose.h>
25 ScMultipleReadHeader::ScMultipleReadHeader(SvStream& rNewStream) :
26 rStream( rNewStream )
28 sal_uInt32 nDataSize;
29 rStream.ReadUInt32( nDataSize );
30 sal_uInt64 nDataPos = rStream.Tell();
31 nTotalEnd = nDataPos + nDataSize;
32 nEntryEnd = nTotalEnd;
34 rStream.SeekRel(nDataSize);
35 sal_uInt16 nID;
36 rStream.ReadUInt16( nID );
37 if (nID != SCID_SIZES)
39 OSL_FAIL("SCID_SIZES not found");
40 if ( rStream.GetError() == ERRCODE_NONE )
41 rStream.SetError( SVSTREAM_FILEFORMAT_ERROR );
43 // everything to 0, so BytesLeft() aborts at least
44 pBuf = nullptr; pMemStream.reset();
45 nEntryEnd = nDataPos;
47 else
49 sal_uInt32 nSizeTableLen;
50 rStream.ReadUInt32( nSizeTableLen );
51 pBuf.reset( new sal_uInt8[nSizeTableLen] );
52 rStream.ReadBytes( pBuf.get(), nSizeTableLen );
53 pMemStream.reset(new SvMemoryStream( pBuf.get(), nSizeTableLen, StreamMode::READ ));
56 nEndPos = rStream.Tell();
57 rStream.Seek( nDataPos );
60 ScMultipleReadHeader::~ScMultipleReadHeader()
62 if ( pMemStream && pMemStream->Tell() != pMemStream->GetEndOfData() )
64 OSL_FAIL( "Sizes not fully read" );
65 if ( rStream.GetError() == ERRCODE_NONE )
66 rStream.SetError( SCWARN_IMPORT_INFOLOST );
68 pMemStream.reset();
70 rStream.Seek(nEndPos);
73 void ScMultipleReadHeader::EndEntry()
75 sal_uInt64 nPos = rStream.Tell();
76 OSL_ENSURE( nPos <= nEntryEnd, "read too much" );
77 if ( nPos != nEntryEnd )
79 if ( rStream.GetError() == ERRCODE_NONE )
80 rStream.SetError( SCWARN_IMPORT_INFOLOST );
81 rStream.Seek( nEntryEnd ); // ignore the rest
84 nEntryEnd = nTotalEnd; // all remaining, if no StartEntry follows
87 void ScMultipleReadHeader::StartEntry()
89 sal_uInt64 nPos = rStream.Tell();
90 sal_uInt32 nEntrySize;
91 (*pMemStream).ReadUInt32( nEntrySize );
93 nEntryEnd = nPos + nEntrySize;
94 OSL_ENSURE( nEntryEnd <= nTotalEnd, "read too many entries" );
97 sal_uInt64 ScMultipleReadHeader::BytesLeft() const
99 sal_uInt64 nReadEnd = rStream.Tell();
100 if (nReadEnd <= nEntryEnd)
101 return nEntryEnd-nReadEnd;
103 OSL_FAIL("ScMultipleReadHeader::BytesLeft: Error");
104 return 0;
107 ScMultipleWriteHeader::ScMultipleWriteHeader(SvStream& rNewStream) :
108 rStream( rNewStream ),
109 aMemStream( 4096, 4096 )
111 nDataSize = 0;
112 rStream.WriteUInt32( nDataSize );
114 nDataPos = rStream.Tell();
115 nEntryStart = nDataPos;
118 ScMultipleWriteHeader::~ScMultipleWriteHeader()
120 sal_uInt64 nDataEnd = rStream.Tell();
122 rStream.WriteUInt16( SCID_SIZES );
123 rStream.WriteUInt32( aMemStream.Tell() );
124 rStream.WriteBytes( aMemStream.GetData(), aMemStream.Tell() );
126 if ( nDataEnd - nDataPos != nDataSize ) // matched default ?
128 nDataSize = nDataEnd - nDataPos;
129 sal_uInt64 nPos = rStream.Tell();
130 rStream.Seek(nDataPos-sizeof(sal_uInt32));
131 rStream.WriteUInt32( nDataSize ); // record size at the beginning
132 rStream.Seek(nPos);
136 void ScMultipleWriteHeader::EndEntry()
138 sal_uInt64 nPos = rStream.Tell();
139 aMemStream.WriteUInt32( nPos - nEntryStart );
142 void ScMultipleWriteHeader::StartEntry()
144 sal_uInt64 nPos = rStream.Tell();
145 nEntryStart = nPos;
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */