cid#1606759 Data race condition
[LibreOffice.git] / lotuswordpro / source / filter / bencont.cxx
blob29153ee0a828a502141e37d757504016c7b18b47
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
56 #include "first.hxx"
57 #include "tocread.hxx"
58 #include <algorithm>
59 #include <osl/diagnose.h>
60 #include <rtl/string.hxx>
62 namespace OpenStormBento
65 // String definitions
66 const char gsBenMagicBytes[] = BEN_MAGIC_BYTES;
68 /**
69 * New bento container from file stream
70 * @param pointer to length of bento file
71 * @param pointer to pointer of Bento Container object
72 * @return error code
74 BenError BenOpenContainer(LwpSvStream * pStream, std::unique_ptr<LtcBenContainer>* ppContainer)
76 *ppContainer = nullptr;
78 if (nullptr == pStream)
80 return BenErr_ContainerWithNoObjects;
83 std::unique_ptr<LtcBenContainer> pContainer(new LtcBenContainer(pStream));
84 if (pContainer->Open() != BenErr_OK) // delete two inputs
86 return BenErr_InvalidTOC;
89 *ppContainer = std::move(pContainer);
90 return BenErr_OK;
93 LtcBenContainer::~LtcBenContainer()
97 BenError
98 LtcBenContainer::Open() // delete two inputs
100 BenError Err;
101 CBenTOCReader TOCReader(this);
102 if ((Err = TOCReader.ReadLabelAndTOC()) != BenErr_OK)
104 return Err;
106 return BenErr_OK;
109 void
110 LtcBenContainer::RegisterPropertyName(const char * sPropertyName,
111 CBenPropertyName ** ppPropertyName)
113 CUtListElmt * pPrevNamedObjectListElmt;
114 CBenNamedObject * pNamedObject = FindNamedObject(&cNamedObjects,
115 sPropertyName, &pPrevNamedObjectListElmt);
117 if (pNamedObject != nullptr)
119 if (! pNamedObject->IsPropertyName())
120 return;
121 else *ppPropertyName = static_cast<CBenPropertyName *>(pNamedObject);
123 else
125 CUtListElmt * pPrevObject;
126 if (FindID(&cObjects, cNextAvailObjectID, &pPrevObject) != nullptr)
127 return;
129 *ppPropertyName = new CBenPropertyName(this, cNextAvailObjectID,
130 pPrevObject, sPropertyName, pPrevNamedObjectListElmt);
131 ++cNextAvailObjectID;
135 CBenObject *
136 LtcBenContainer::GetNextObject(CBenObject const * pCurrObject)
138 return static_cast<CBenObject *>(cObjects.GetNextOrNULL(pCurrObject));
141 CBenObject *
142 LtcBenContainer::FindNextObjectWithProperty(CBenObject * pCurrObject,
143 BenObjectID PropertyID)
145 while ((pCurrObject = GetNextObject(pCurrObject)) != nullptr)
146 if (pCurrObject->UseProperty(PropertyID) != nullptr)
147 return pCurrObject;
149 return nullptr;
153 * Construction
154 * @param Bento file stream pointer
155 * @return
157 LtcBenContainer::LtcBenContainer(LwpSvStream * pStream)
158 : cNextAvailObjectID(0)
160 cpStream = pStream;
161 pStream->Seek(STREAM_SEEK_TO_END);
162 m_ulLength = pStream->Tell();
163 pStream->Seek(STREAM_SEEK_TO_BEGIN);
167 * Read buffer for bento file with specified buffer
168 * @param buffer pointer
169 * @param buffer size
170 * @param number of bytes read
172 void LtcBenContainer::Read(void * pBuffer, size_t MaxSize,
173 size_t* pAmtRead)
175 *pAmtRead = cpStream->Read(pBuffer, MaxSize);
178 * Read buffer from bento file with specified size
179 * @param buffer pointer
180 * @param number of bytes to be read
181 * @return BenError
183 BenError LtcBenContainer::ReadKnownSize(void * pBuffer, size_t Amt)
185 size_t ulLength = cpStream->Read(pBuffer, Amt);
186 if (ulLength == Amt)
188 return BenErr_OK;
190 return BenErr_ReadPastEndOfContainer;
193 * Seek to position from the beginning of the bento file
194 * @param position in container file from beginning
196 void LtcBenContainer::SeekToPosition(BenContainerPos Pos)
198 cpStream->Seek(Pos);
201 * Seek to position compare to end of bento file
202 * @param position in container file from end
204 void LtcBenContainer::SeekFromEnd(tools::Long Offset)
206 cpStream->Seek(STREAM_SEEK_TO_END);
207 cpStream->SeekRel(Offset);
210 * Find the next value stream with property name
211 * @param string of property name
212 * @return next value stream pointer with the property names
214 LtcUtBenValueStream * LtcBenContainer::FindNextValueStreamWithPropertyName(const char * sPropertyName)
216 CBenPropertyName * pPropertyName(nullptr);
217 RegisterPropertyName(sPropertyName, &pPropertyName); // Get property name object
219 if (nullptr == pPropertyName)
220 return nullptr; // Property not exist
222 // Get current object
223 CBenObject * pObj =FindNextObjectWithProperty(nullptr, pPropertyName->GetID()); // Get next object with same property name
224 if (nullptr == pObj)
225 return nullptr;
227 CBenValue * pValue;
228 LtcUtBenValueStream * pValueStream;
230 pValue = pObj->UseValue(pPropertyName->GetID());
232 pValueStream = new LtcUtBenValueStream(pValue);
234 return pValueStream;
238 * Find the unique value stream with property name
239 * @param string of property name
240 * @return the only value stream pointer with the property names
242 LtcUtBenValueStream * LtcBenContainer::FindValueStreamWithPropertyName(const char * sPropertyName)
244 return FindNextValueStreamWithPropertyName(sPropertyName);
247 namespace
249 void readDataInBlocks(SvStream& rSt, sal_uInt64 nDLen, std::vector<sal_uInt8>& rData)
251 //read data in blocks as it's more likely large values are simply broken
252 //and we'll run out of data before we need to realloc
253 for (sal_uInt64 i = 0; i < nDLen; i+= SAL_MAX_UINT16)
255 size_t nOldSize = rData.size();
256 size_t nBlock = std::min<size_t>(SAL_MAX_UINT16, nDLen - nOldSize);
257 rData.resize(nOldSize + nBlock);
258 size_t nReadBlock = rSt.ReadBytes(rData.data() + nOldSize, nBlock);
259 if (nBlock != nReadBlock)
261 rData.resize(nOldSize + nReadBlock);
262 break;
269 * Find hazily according to object ID
270 * @param pObjectname - format as "GrXX,XXXXXXXX" wherein XX is high part of object ID, and XXXXXXXX is low part
272 std::vector<sal_uInt8> LtcBenContainer::GetGraphicData(const char *pObjectName)
274 std::vector<sal_uInt8> aData;
275 if (!pObjectName)
277 return aData;
279 // construct the string of property name
280 OString sSName=OString::Concat(pObjectName) + "-S";
281 OString sDName=OString::Concat(pObjectName) + "-D";
283 /* traverse the found properties and construct the stream vectors */
284 // get S&D's stream and merge them together
285 std::unique_ptr<SvStream> xS(FindValueStreamWithPropertyName(sSName.getStr()));
286 std::unique_ptr<SvStream> xD(FindValueStreamWithPropertyName(sDName.getStr()));
288 sal_uInt64 nDLen = 0;
289 if (xD)
291 nDLen = xD->TellEnd();
293 sal_uInt64 nSLen = 0;
294 if (xS)
296 nSLen = xS->TellEnd();
299 sal_uInt64 nLen = nDLen + nSLen;
300 OSL_ENSURE(nLen > 0, "expected a non-0 length");
301 // the 'D' stream is NULL or it has invalid length
302 if (nLen <= 0)
304 return aData;
307 if (xD)
309 readDataInBlocks(*xD, nDLen, aData);
310 xD.reset();
312 if (xS)
314 readDataInBlocks(*xS, nSLen, aData);
315 xS.reset();
318 return aData;
321 sal_uInt64 LtcBenContainer::remainingSize() const
323 return m_ulLength - cpStream->Tell();
326 }// end namespace OpenStormBento
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */