bump product version to 4.1.6.2
[LibreOffice.git] / lotuswordpro / source / filter / tocread.cxx
blob2a6f289ed793eec69552869d648fa56faff82e6e
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 "assert.h"
58 namespace OpenStormBento
61 BenError
62 CBenTOCReader::ReadLabelAndTOC()
64 BenError Err;
66 unsigned long TOCOffset;
67 if ((Err = ReadLabel(&TOCOffset, &cTOCSize)) != BenErr_OK)
68 return Err;
70 if ((Err = cpContainer->SeekToPosition(TOCOffset)) != BenErr_OK)
71 return Err;
73 cpTOC = new BenByte[cTOCSize];
74 if ((Err = cpContainer->ReadKnownSize(cpTOC, cTOCSize)) != BenErr_OK)
75 return Err;
77 if ((Err = ReadTOC()) != BenErr_OK)
78 return Err;
80 return BenErr_OK;
83 BenError
84 CBenTOCReader::ReadLabel(unsigned long * pTOCOffset, unsigned long * pTOCSize)
86 // If seek fails, then probably because stream is smaller than
87 // BEN_LABEL_SIZE and thus can't be Bento container
88 BenError Err;
89 if ((Err = cpContainer->SeekFromEnd(-BEN_LABEL_SIZE)) != BenErr_OK)
90 return BenErr_NotBentoContainer;
92 BenByte Label[BEN_LABEL_SIZE];
93 if ((Err = cpContainer->ReadKnownSize(Label, BEN_LABEL_SIZE)) != BenErr_OK)
94 return Err;
96 if (memcmp(Label, gsBenMagicBytes, BEN_MAGIC_BYTES_SIZE) != 0)
97 if ((Err = SearchForLabel(Label)) != BenErr_OK)
98 return Err;
100 BenByte * pCurrLabel = Label + BEN_MAGIC_BYTES_SIZE;
102 #ifndef NDEBUG
103 BenWord Flags =
104 #endif
105 UtGetIntelWord(pCurrLabel); pCurrLabel += 2; // Flags
106 // Newer files are 0x0101--indicates if big or little endian. Older
107 // files are 0x0 for flags
108 assert(Flags == 0x0101 || Flags == 0x0);
110 cBlockSize = UtGetIntelWord(pCurrLabel) * 1024; pCurrLabel += 2;
112 // Check major version
113 if (UtGetIntelWord(pCurrLabel) != BEN_CURR_MAJOR_VERSION)
114 return BenErr_UnknownBentoFormatVersion;
115 pCurrLabel += 2;
117 UtGetIntelWord(pCurrLabel); pCurrLabel += 2; // Minor version
119 *pTOCOffset = UtGetIntelDWord(pCurrLabel); pCurrLabel += 4;
120 *pTOCSize = UtGetIntelDWord(pCurrLabel);
122 assert(pCurrLabel + 4 == Label + BEN_LABEL_SIZE);
124 return BenErr_OK;
127 #define LABEL_READ_BUFFER_SIZE 500
128 #define MAX_SEARCH_AMOUNT 1024 * 1024
130 BenError
131 CBenTOCReader::SearchForLabel(BenByte * pLabel)
133 BenError Err;
135 sal_uLong Length;
136 if ((Err = cpContainer->GetSize(&Length)) != BenErr_OK)
137 return Err;
139 // Always ready to check for MagicBytes from
140 // CurrOffset - BEN_MAGIC_BYTES_SIZE to CurrOffset - 1
141 unsigned long CurrOffset = Length - BEN_LABEL_SIZE + BEN_MAGIC_BYTES_SIZE -
144 char Buffer[LABEL_READ_BUFFER_SIZE];
146 unsigned long BufferStartOffset = Length; // Init to big value
148 while (CurrOffset >= BEN_MAGIC_BYTES_SIZE)
150 // Don't search backwards more than 1 meg
151 if (Length - CurrOffset > MAX_SEARCH_AMOUNT)
152 break;
155 // If before beginning of buffer
156 if (CurrOffset - BEN_MAGIC_BYTES_SIZE < BufferStartOffset)
158 unsigned long UsedBufferSize;
159 if (CurrOffset < LABEL_READ_BUFFER_SIZE)
160 UsedBufferSize = CurrOffset;
161 else UsedBufferSize = LABEL_READ_BUFFER_SIZE;
163 if ((Err = cpContainer->SeekToPosition(CurrOffset - UsedBufferSize))
164 != BenErr_OK)
165 return Err;
167 if ((Err = cpContainer->ReadKnownSize(Buffer, UsedBufferSize)) !=
168 BenErr_OK)
169 return Err;
171 BufferStartOffset = CurrOffset - UsedBufferSize;
174 if (memcmp(Buffer + (CurrOffset - BEN_MAGIC_BYTES_SIZE -
175 BufferStartOffset), gsBenMagicBytes, BEN_MAGIC_BYTES_SIZE) == 0)
177 if ((Err = cpContainer->SeekToPosition(CurrOffset -
178 BEN_MAGIC_BYTES_SIZE)) != BenErr_OK)
179 return Err;
181 return cpContainer->ReadKnownSize(pLabel, BEN_LABEL_SIZE);
184 --CurrOffset;
187 return BenErr_NotBentoContainer; // Didn't find magic bytes
190 BenError
191 CBenTOCReader::ReadTOC()
193 BenError Err;
194 BenByte LookAhead = GetCode();
195 BenGeneration Generation = 0;
197 // Read in all objects
198 while (LookAhead == BEN_NEW_OBJECT)
200 BenObjectID ObjectID;
201 if ((Err = GetDWord(&ObjectID)) != BenErr_OK)
202 return Err;
203 pCBenObject pObject = NULL;
205 // Read in all properties for object
208 BenObjectID PropertyID;
210 if ((Err = GetDWord(&PropertyID)) != BenErr_OK)
211 return Err;
212 pCBenProperty pProperty = NULL;
214 // Read in all values for property
217 BenObjectID ReferencedListID = 0;
219 BenObjectID TypeID;
220 if ((Err = GetDWord(&TypeID)) != BenErr_OK)
221 return Err;
222 LookAhead = GetCode();
224 if (LookAhead == BEN_EXPLICIT_GEN)
226 if ((Err = GetDWord(&Generation)) != BenErr_OK)
227 return Err;
228 LookAhead = GetCode();
231 if (LookAhead == BEN_REFERENCE_LIST_ID)
233 if ((Err = GetDWord(&ReferencedListID)) != BenErr_OK)
234 return Err;
235 LookAhead = GetCode();
238 if (PropertyID == BEN_PROPID_GLOBAL_PROPERTY_NAME ||
239 PropertyID == BEN_PROPID_GLOBAL_TYPE_NAME)
241 // Read property or type name
243 if (pObject != NULL || TypeID != BEN_TYPEID_7_BIT_ASCII ||
244 LookAhead != BEN_OFFSET4_LEN4)
245 return BenErr_NamedObjectError;
247 BenContainerPos Pos;
248 unsigned long Length;
250 if ((Err = GetDWord(&Pos)) != BenErr_OK)
251 return Err;
252 if ((Err = GetDWord(&Length)) != BenErr_OK)
253 return Err;
254 LookAhead = GetCode();
256 if ((Err = cpContainer->SeekToPosition(Pos)) != BenErr_OK)
257 return Err;
259 #define STACK_BUFFER_SIZE 256
260 char sStackBuffer[STACK_BUFFER_SIZE];
261 char * sAllocBuffer;
262 char * sBuffer;
263 if (Length > STACK_BUFFER_SIZE)
265 sBuffer = new char[Length];
266 sAllocBuffer = sBuffer;
268 else
270 sBuffer = sStackBuffer;
271 sAllocBuffer = NULL;
274 if ((Err = cpContainer->ReadKnownSize(sBuffer, Length)) !=
275 BenErr_OK)
277 delete[] sAllocBuffer;
278 return Err;
281 pCBenNamedObjectListElmt pPrevNamedObjectListElmt;
282 if (FindNamedObject(cpContainer->GetNamedObjects(),
283 sBuffer, &pPrevNamedObjectListElmt) != NULL)
285 delete[] sAllocBuffer;
286 return BenErr_DuplicateName;
289 pCBenObject pPrevObject = (pCBenObject) cpContainer->
290 GetObjects()->GetLast();
292 if (PropertyID == BEN_PROPID_GLOBAL_PROPERTY_NAME)
293 pObject = new CBenPropertyName(cpContainer, ObjectID,
294 pPrevObject, sBuffer, pPrevNamedObjectListElmt);
295 else pObject = new CBenTypeName(cpContainer, ObjectID,
296 pPrevObject, sBuffer, pPrevNamedObjectListElmt);
298 delete[] sAllocBuffer;
300 else if (PropertyID == BEN_PROPID_OBJ_REFERENCES)
302 // Don't need to read in references object--we assume
303 // that all references use object ID as key
304 if ((Err = ReadSegments(NULL, &LookAhead)) != BenErr_OK)
305 return Err;
307 else if (ObjectID == BEN_OBJID_TOC)
309 if (PropertyID == BEN_PROPID_TOC_SEED)
311 if (TypeID != BEN_TYPEID_TOC_TYPE ||
312 LookAhead != BEN_IMMEDIATE4)
313 return BenErr_TOCSeedError;
315 BenDWord Data;
316 if ((Err = GetDWord(&Data)) != BenErr_OK)
317 return Err;
318 LookAhead = GetCode();
320 cpContainer->SetNextAvailObjectID(Data);
322 else
324 // Ignore the other BEN_OBJID_TOC properties
325 if ((Err = ReadSegments(NULL, &LookAhead)) != BenErr_OK)
326 return Err;
329 else
331 if (pProperty != NULL)
332 return BenErr_PropertyWithMoreThanOneValue;
334 if (pObject == NULL)
335 pObject = new CBenObject(cpContainer, ObjectID,
336 (pCBenObject) cpContainer->GetObjects()->GetLast());
338 pProperty = new CBenProperty(pObject, PropertyID, TypeID,
339 (pCBenProperty) pObject->GetProperties()->GetLast());
341 if ((Err = ReadSegments(pProperty->UseValue(),
342 &LookAhead)) != BenErr_OK)
343 return Err;
345 } while (LookAhead == BEN_NEW_TYPE);
346 } while (LookAhead == BEN_NEW_PROPERTY);
349 if (LookAhead == BEN_READ_PAST_END_OF_TOC)
350 return BenErr_OK;
351 else return BenErr_InvalidTOC;
354 BenError
355 CBenTOCReader::ReadSegments(pCBenValue pValue, BenByte * pLookAhead)
357 BenError Err;
359 while (*pLookAhead >= BEN_SEGMENT_CODE_START &&
360 *pLookAhead <= BEN_SEGMENT_CODE_END)
362 if ((Err = ReadSegment(pValue, pLookAhead)) !=
363 BenErr_OK)
364 return Err;
367 return BenErr_OK;
370 BenError
371 CBenTOCReader::ReadSegment(pCBenValue pValue, BenByte * pLookAhead)
373 BenError Err;
375 UtBool Immediate = UT_FALSE;
376 UtBool EightByteOffset = UT_FALSE;
377 unsigned long Offset(0), Length(0);
379 switch (*pLookAhead)
381 case BEN_CONT_OFFSET4_LEN4:
382 case BEN_OFFSET4_LEN4:
383 if ((Err = GetDWord(&Offset)) != BenErr_OK)
384 return Err;
385 if ((Err = GetDWord(&Length)) != BenErr_OK)
386 return Err;
387 break;
389 case BEN_IMMEDIATE0:
390 Length = 0; Immediate = UT_TRUE;
391 break;
393 case BEN_IMMEDIATE1:
394 Length = 1; Immediate = UT_TRUE;
395 break;
397 case BEN_IMMEDIATE2:
398 Length = 2; Immediate = UT_TRUE;
399 break;
401 case BEN_IMMEDIATE3:
402 Length = 3; Immediate = UT_TRUE;
403 break;
405 case BEN_CONT_IMMEDIATE4:
406 case BEN_IMMEDIATE4:
407 Length = 4; Immediate = UT_TRUE;
408 break;
410 case BEN_CONT_OFFSET8_LEN4:
411 case BEN_OFFSET8_LEN4:
412 EightByteOffset = UT_TRUE;
413 break;
415 default:
416 return BenErr_OK;
419 BenByte ImmData[4];
420 if (Immediate && Length != 0)
421 if ((Err = GetData(ImmData, 4)) != BenErr_OK)
422 return Err;
424 *pLookAhead = GetCode();
426 if (EightByteOffset)
427 return BenErr_64BitOffsetNotSupported;
429 if (pValue != NULL)
431 if (! Immediate)
432 new CBenValueSegment(pValue, Offset, Length);
433 else if (Length != 0)
435 assert(Length <= 4);
436 new CBenValueSegment(pValue, ImmData, (unsigned short) Length);
440 return BenErr_OK;
443 UtBool
444 CBenTOCReader::CanGetData(unsigned long Amt)
446 return cCurr + Amt <= cTOCSize;
449 BenError
450 CBenTOCReader::GetByte(BenByte * pByte)
452 if (! CanGetData(1))
453 return BenErr_ReadPastEndOfTOC;
455 *pByte = UtGetIntelByte(cpTOC + cCurr);
456 ++cCurr;
457 return BenErr_OK;
460 BenError
461 CBenTOCReader::GetDWord(BenDWord * pDWord)
463 if (! CanGetData(4))
464 return BenErr_ReadPastEndOfTOC;
466 *pDWord = UtGetIntelDWord(cpTOC + cCurr);
467 cCurr += 4;
468 return BenErr_OK;
471 BenByte
472 CBenTOCReader::GetCode()
474 BenByte Code;
477 if (GetByte(&Code) != BenErr_OK)
478 return BEN_READ_PAST_END_OF_TOC;
480 if (Code == BEN_END_OF_BUFFER)
481 // Advance to next block
482 cCurr = cBlockSize * ((cCurr + (cBlockSize - 1)) /
483 cBlockSize);
485 while (Code == BEN_NOOP || Code == BEN_END_OF_BUFFER);
486 return Code;
489 BenError
490 CBenTOCReader::GetData(BenDataPtr pBuffer, unsigned long Amt)
492 if (! CanGetData(Amt))
493 return BenErr_ReadPastEndOfTOC;
495 UtHugeMemcpy(pBuffer, cpTOC + cCurr, Amt);
496 cCurr += Amt;
497 return BenErr_OK;
499 }//end OpenStormBento namespace
501 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */