1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * either of the following licenses
6 * - GNU Lesser General Public License Version 2.1
7 * - Sun Industry Standards Source License Version 1.1
9 * Sun Microsystems Inc., October, 2000
11 * GNU Lesser General Public License Version 2.1
12 * =============================================
13 * Copyright 2000 by Sun Microsystems, Inc.
14 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License version 2.1, as published by the Free Software Foundation.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 * Sun Industry Standards Source License Version 1.1
32 * =================================================
33 * The contents of this file are subject to the Sun Industry Standards
34 * Source License Version 1.1 (the "License"); You may not use this file
35 * except in compliance with the License. You may obtain a copy of the
36 * License at http://www.openoffice.org/license.html.
38 * Software provided under this License is provided on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
40 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
41 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
42 * See the License for the specific provisions governing your rights and
43 * obligations concerning the Software.
45 * The Initial Developer of the Original Code is: IBM Corporation
47 * Copyright: 2008 by IBM Corporation
49 * All Rights Reserved.
51 * Contributor(s): _______________________________________
54 ************************************************************************/
60 #include "lwpsvstream.hxx"
62 #define BEN_CC __stdcall
64 #define BEN_EXPORT __declspec(dllexport)
73 // removed for WordPro filter, 2005-01-27
74 //#include <scerrors.hxx> // error code defined for SC
75 //#include <filter.hxx> // error code defined for SC filters
76 #include <tools/stream.hxx> // SvStream definition
77 #include <sot/storage.hxx> // SotStorageStream definition, add by 10/24/2005
79 namespace OpenStormBento
82 #define BEN_CURR_MAJOR_VERSION 2
83 #define BEN_CURR_MINOR_VERSION 0
84 #define BEN_MAGIC_BYTES_SIZE 8
85 #define BEN_LABEL_SIZE 24
87 #define BEN_MAGIC_BYTES "\xA4""CM""\xA5""Hdr""\xD7"
88 //For Ole2DirectoryStruct, Add by 10/24/2005
89 #define BEN_STGTY_STORAGE 1
90 #define BEN_STGTY_STREAM 2
91 #define ASWENTRY_SIZE 204
96 UINT8 n4
, n5
, n6
, n7
, n8
, n9
, n10
, n11
;
98 class AswEntry
//total length: 204
100 UINT16 nName
[ 68 ]; //Name of IStorage or IStream referenced by this entry, length = 136
104 ClsId aClsId
; //CLSID from OLE 2 IStorage::SetClass call
105 UINT32 nStatebits
; //State bits from OLE 2 IStorage::SetStateBits call
106 UINT32 nType
; // STGTY_STORAGE: 1 or STGTY_STREAM:2,
107 UINT32 nObjectIDRef
; //Persistent Bento reference to Bento object for this IStorage or IStream
110 UINT32 nReserved
[2]; //skip 16 char
113 void Init(); // initialize the data
114 void SetName( const String
& ); // store a name (ASCII, up to 32 chars)
115 void GetName( String
& rName
) const;
116 void Load( const void* );
118 UINT32
GetType() const { return nType
; }
119 void SetType( UINT32 t
) { nType
= t
;}
120 const ClsId
& GetClassId() const { return aClsId
;}
121 void SetClassId( const ClsId
& );
122 void SetObjectID(UINT32 id
) { nObjectIDRef
= id
;}
123 UINT32
GetObjectID() const { return nObjectIDRef
;}
130 BenErr_NameConflict
= 1,
131 BenErr_DuplicateObjectID
= 2,
132 BenErr_UnknownBentoFormatVersion
= 3,
133 BenErr_NamedObjectError
= 4,
134 BenErr_NamedObjectIllegalValue
= 5,
135 BenErr_InvalidTOC
= 6,
136 BenErr_64BitOffsetNotSupported
= 7,
137 BenErr_ReadPastEndOfTOC
= 8,
138 BenErr_ContainerWithNoObjects
= 9,
139 BenErr_ObjectWithNoProperties
= 10,
140 BenErr_PropertyWithNoValues
= 11,
141 BenErr_IllegalInMemoryTOC
= 12,
142 BenErr_PropertyAlreadyExists
= 13,
143 BenErr_UnexpectedEndOfFile
= 14,
144 BenErr_InvalidWriteOffset
= 15,
145 BenErr_InvalidImmediateWrite
= 16,
146 BenErr_TOCSeedError
= 17,
147 BenErr_ReadPastEndOfContainer
= 18,
148 BenErr_DuplicateName
= 19,
149 BenErr_BadReferencedList
= 20,
150 BenErr_IllegalContinuedImmediate
= 21,
151 BenErr_NotBentoContainer
= 22,
152 BenErr_PropertyWithMoreThanOneValue
= 23
154 // IMPORTANT - UtStream errors (UtErr_....) are also valid Bento
155 // errors. They have codes of 100 and higher. When, say, a Bento
156 // container open fails due to an access violation (somebody else, say,
157 // has it open exclusive), will get a UtErr code. If define own
158 // subclass of UtStream (custom handler), can define own error codes--
159 // those should start at 200
162 * These two functions are useless in SODC
164 inline UtError BenToUtError(BenError Err)
166 if (Err == 0 || Err >= 100)
167 return (UtError) Err;
168 else return UtErr_Fail;
171 inline BenError UtToBenError(UtError Err)
172 { UT_ASSERT(Err == 0 || Err >= 100); return (BenError) Err; }
174 BenSeek_FromStart = 1,
175 BenSeek_FromCurr = 2,
180 UtDefClassP(LtcBenContainer
);
181 UtDefClassP(CBenIDListElmt
);
182 UtDefClassP(CBenObject
);
183 UtDefClassP(CBenProperty
);
184 UtDefClassP(CBenReference
);
185 UtDefClassP(CBenValue
);
186 UtDefClassP(CBenValueSegment
);
187 UtDefClassP(CBenNamedObjectListElmt
);
188 UtDefClassP(CBenNamedObject
);
189 UtDefClassP(CBenPropertyName
);
190 UtDefClassP(CBenTypeName
);
192 typedef unsigned char BenByte
;
193 typedef unsigned short BenWord
;
194 typedef unsigned long BenDWord
;
195 typedef void * BenDataPtr
;
196 typedef const void * BenConstDataPtr
;
198 typedef unsigned long BenContainerPos
;
199 typedef unsigned long BenObjectID
;
200 typedef unsigned long BenGeneration
;
202 ULONG
BenOpenContainer(LwpSvStream
* pStream
, pLtcBenContainer
* ppContainer
);
204 class CBenIDListElmt
: public CUtListElmt
206 public: // Internal methods
207 CBenIDListElmt(BenObjectID ID
, pCBenIDListElmt pPrev
) : CUtListElmt(pPrev
)
209 CBenIDListElmt(BenObjectID ID
) { cID
= ID
; }
210 BenObjectID
GetID() { return cID
; }
216 class CBenNamedObjectListElmt
: public CUtListElmt
219 // added by to remove warning 2004-06-24
220 CBenNamedObjectListElmt(pCBenNamedObjectListElmt pPrev
) : CUtListElmt(pPrev
)
221 { cpNamedObject
= NULL
; }
222 void SetNamedObject(pCBenNamedObject pObj
)
224 cpNamedObject
= pObj
;
227 CBenNamedObjectListElmt(pCBenNamedObject pNamedObject
,
228 pCBenNamedObjectListElmt pPrev
) : CUtListElmt(pPrev
)
229 { cpNamedObject
= pNamedObject
; }
230 pCBenNamedObject
GetNamedObject() { return cpNamedObject
; }
233 pCBenNamedObject cpNamedObject
;
236 class LtcUtBenValueStream
: public SvStream
239 LtcUtBenValueStream(pCBenValue pValue
);
240 ~LtcUtBenValueStream();
242 public: // Overridden methods
243 #if 0 // Deleted by 2004-06-16
244 UtError
Open(UtBool
/* OpenNew */, UtStrmOpenFlags
/* Flags */);
246 UtError
Seek(long Offset
, UtSeekMode Mode
);
247 UtError
GetPosition(unsigned long * pPosition
);
248 UtError
GetSize(unsigned long * pSize
);
249 UtError
Read(UtStrmDataPtr pBuffer
, unsigned long MaxSize
,
250 unsigned long * pAmtRead
);
251 UtError
Write(UtConstStrmDataPtr pBuffer
, unsigned long Size
,
252 unsigned long * pAmtWritten
);
257 CBenValue
* GetValue(){ return cpValue
; };
258 ULONG
GetSize() { return m_ulValueLength
; };
259 protected: // Overridden methods
261 virtual ULONG
GetData( void* pData
, ULONG nSize
);
262 virtual ULONG
PutData( const void* pData
, ULONG nSize
);
263 virtual ULONG
SeekPos( ULONG nPos
);
264 virtual void SetSize( ULONG nSize
);
265 virtual void FlushData();
267 #if 0// Deleted by 2004-06-16
268 UtError
TruncateSize(unsigned long Size
);
272 unsigned long cCurrentPosition
;
274 ULONG m_ulValueLength
; // Added by , sum of length of all sub-valuesegments
275 // void GetAmountLeft(ULONG * pAmtLeft); useless in SODC
278 class LtcBenContainer
282 void Release(); // Deletes container object--last call
284 BenError
RegisterTypeName(const char * sTypeName
,
285 pCBenTypeName
* ppTypeName
);
286 BenError
RegisterPropertyName(const char * sPropertyName
,
287 pCBenPropertyName
* ppPropertyName
);
288 // Pass NULL to begin iteration. Done when returns NULL.
289 // Objects are returned in order of increasing ID
290 pCBenObject
GetNextObject(pCBenObject pCurrObject
);
291 pCBenObject
FindObject(BenObjectID ObjectID
);
292 pCBenObject
FindNextObjectWithProperty(pCBenObject pCurrObject
,
293 BenObjectID PropertyID
);
294 BenError BEN_EXPORT
NewObject(pCBenObject
* ppBenObject
);
296 public: // Internal methods
297 LtcBenContainer(LwpSvStream
* pStream
);
300 BenError
Read(BenDataPtr pBuffer
, unsigned long MaxSize
,
301 unsigned long * pAmtRead
);
302 BenError
ReadKnownSize(BenDataPtr pBuffer
, unsigned long Amt
);
303 BenError
SeekToPosition(BenContainerPos Pos
);
304 BenError
SeekFromEnd(long Offset
);
306 BenError
GetPosition(BenContainerPos
* pPosition
);
308 BenObjectID
GetNextAvailObjectID() { return cNextAvailObjectID
; }
309 void SetNextAvailObjectID(BenObjectID ID
) { cNextAvailObjectID
= ID
; }
310 pCUtList
GetObjects() { return &cObjects
; }
311 pCUtList
GetNamedObjects() { return &cNamedObjects
; }
313 // Added by 2004-06-10
314 LtcUtBenValueStream
* FindNextValueStreamWithPropertyName(const char * sPropertyName
, LtcUtBenValueStream
* pCurrentValueStream
);
315 LtcUtBenValueStream
* FindValueStreamWithPropertyName(const char * sPropertyName
);
316 LtcUtBenValueStream
* FindObjectValueStreamWithObjectIDAndProperty(BenObjectID ObjectID
, const char * sPropertyName
);
317 BenError
CreateGraphicStreams(std::vector
<SvStream
*> * pStreamVector
) ;
318 BenError
CreateGraphicStream(SvStream
* &pStream
, const char *pObjectName
);
320 SotStorageStreamRef
ConvertAswStorageToOLE2Stream(const char * sObjectName
);
321 LtcUtBenValueStream
* FindOLEStorageStreamWithObjectName(const char * sObjectName
, AswEntry
& rDirStruct
);
322 void ReadAswEntry(SvStream
* pStream
, AswEntry
& rEntry
);
324 BenError
GetSize(ULONG
* pLength
);
325 LwpSvStream
* GetStream()
330 CUtOwningList cObjects
;
331 CUtList cNamedObjects
;
332 LwpSvStream
* cpStream
;
333 ULONG m_ulLength
; // Added for TOCRead.cpp
334 BenObjectID cNextAvailObjectID
; // for new object
337 class CBenObject
: public CBenIDListElmt
340 pCBenProperty
GetNextProperty(pCBenProperty pCurrProperty
);
341 pCBenProperty
UseProperty(BenObjectID PropertyID
);
342 void DeleteProperty(pCBenProperty pProperty
);
343 pCBenValue
UseValue(BenObjectID PropertyID
);
344 pCBenValue
UseSingleValue();
345 // Inefficient to use this method if will use property multiple times--
346 // instead register property and call UseProperty with property ID
347 pCBenValue
UseValueWithPropertyName(const char * sPropertyName
);
348 virtual UtBool
IsNamedObject();
349 pLtcBenContainer
GetContainer() { return cpContainer
; }
350 BenObjectID
GetObjectID() { return GetID(); }
351 BenError BEN_EXPORT
NewValue(BenObjectID PropertyID
, BenObjectID TypeID
,
352 pCBenValue
* ppValue
);
353 public: // Internal methods
354 CBenObject(pLtcBenContainer pContainer
, BenObjectID ObjectID
,
355 pCBenIDListElmt pPrev
) : CBenIDListElmt(ObjectID
, pPrev
)
356 { cpContainer
= pContainer
; }
357 pCUtList
GetProperties() { return &cProperties
; }
360 pLtcBenContainer cpContainer
;
361 CUtOwningList cProperties
;
364 class CBenValue
: public CBenIDListElmt
367 unsigned long GetValueSize();
368 BenError
ReadValueData(BenDataPtr pBuffer
,
369 unsigned long Offset
, unsigned long MaxSize
, unsigned long * pAmtRead
);
370 BenError
ReadValueDataKnownSize(BenDataPtr pBuffer
,
371 unsigned long Offset
, unsigned long Amt
);
373 BenError BEN_EXPORT
WriteValueData(BenConstDataPtr pBuffer
,
374 unsigned long Offset
, unsigned long Size
);
375 BenError BEN_EXPORT
WriteValueData(BenConstDataPtr pBuffer
,
376 unsigned long Offset
, unsigned long Size
, unsigned long * pAmtWritten
);
377 BenError BEN_EXPORT
WriteImmediateValueData(BenConstDataPtr pBuffer
,
378 unsigned short Size
);
379 BenError BEN_EXPORT
TruncateValueSize(unsigned long NewSize
);
380 BenError BEN_EXPORT
NewReference(BenObjectID ReferencedObjectID
,
381 pCBenReference pReference
);
382 BenObjectID BEN_EXPORT
GetReferencedObject(pCBenReference pReference
);
385 pCBenProperty BEN_EXPORT
GetProperty() { return cpProperty
; }
387 public: // Internal methods
388 // added by to remove WARNING 2004-06-24
389 CBenValue(BenObjectID TypeID
):CBenIDListElmt(TypeID
)
392 cpReferencedList
= NULL
;
395 void SetProperty(pCBenProperty pProperty
)
397 cpProperty
= pProperty
;
400 CBenValue(pCBenProperty pProperty
, BenObjectID TypeID
) :
401 CBenIDListElmt(TypeID
)
403 cpProperty
= pProperty
;
404 cpReferencedList
= NULL
;
406 BenObjectID
GetTypeNameID() { return GetID(); }
407 inline pCBenValueSegment
GetNextValueSegment(pCBenValueSegment
409 inline pLtcBenContainer
GetContainer();
410 pCUtList
GetValueSegments() { return &cValueSegments
; }
411 BenObjectID
GetReferencedListID();
412 // Currently, no generation support
413 BenGeneration
GetGeneration() { return 1; }
416 pCBenProperty cpProperty
;
417 CUtOwningList cValueSegments
;
418 pCBenValue cpReferencedList
;
419 unsigned long cReferencedObjectsSize
;
422 class CBenProperty
: public CBenIDListElmt
425 // In the Apple Bento, a property can have multiple values, each of a
426 // different type. But we never use it that way, so in this code a
427 // property has exactly one value
429 pCBenValue
UseValue() { return &cValue
; }
430 pCBenObject
GetBenObject() { return cpObject
; }
431 pLtcBenContainer
GetContainer() { return GetBenObject()->GetContainer(); }
433 public: // Internal methods
435 CBenProperty(pCBenObject pObject
, BenObjectID PropertyID
,
436 BenObjectID TypeID
, pCBenIDListElmt pPrevProperty
) :
437 CBenIDListElmt(PropertyID
, pPrevProperty
), cValue(this, TypeID
)
438 { cpObject
= pObject
; }
440 // changed by to remove WARNING here 2004-06-24
441 CBenProperty(pCBenObject pObject
, BenObjectID PropertyID
,
442 BenObjectID TypeID
, pCBenIDListElmt pPrevProperty
) :
443 CBenIDListElmt(PropertyID
, pPrevProperty
), cValue(TypeID
)
446 cValue
.SetProperty(this);
449 BenObjectID
GetPropertyNameID() { return GetID(); }
452 pCBenObject cpObject
;
456 // In our implementation, reference is always just the object ID for the
457 // object referenced (but you shouldn't assume that)
458 #define BEN_REFERENCE_SIZE 4
463 BenByte
* GetData() { return cData
; }
466 BenByte cData
[BEN_REFERENCE_SIZE
];
469 class CBenValueSegment
: public CUtListElmt
471 public: // Internal methods
472 CBenValueSegment(pCBenValue pValue
, BenContainerPos Pos
,
473 unsigned long Size
) : CUtListElmt(pValue
->GetValueSegments())
474 { cpValue
= pValue
; cImmediate
= UT_FALSE
; cPos
= Pos
;
476 CBenValueSegment(pCBenValue pValue
, BenConstDataPtr pImmData
,
477 unsigned short Size
) : CUtListElmt(pValue
->GetValueSegments())
478 { cpValue
= pValue
; cImmediate
= UT_TRUE
;
479 UtHugeMemcpy(cImmData
, pImmData
, Size
); cSize
= Size
; }
480 CBenValueSegment(BenContainerPos Pos
, unsigned long Size
)
481 { cpValue
= NULL
; cImmediate
= UT_FALSE
; cPos
= Pos
;
483 CBenValueSegment(BenConstDataPtr pImmData
, unsigned short Size
)
484 { cpValue
= NULL
; cImmediate
= UT_TRUE
;
485 UtHugeMemcpy(cImmData
, pImmData
, Size
); cSize
= Size
; }
488 return cpValue
== NULL
|| cpValue
->GetValueSegments()->GetLast() ==
491 UtBool
IsImmediate() { return cImmediate
; }
492 BenContainerPos
GetPosition() { return cPos
; }
493 unsigned long GetSize() { return cSize
; }
494 void SetSize(unsigned long Size
) { cSize
= Size
; }
495 BenByte
* GetImmediateData() { return cImmData
; }
502 BenContainerPos cPos
;
508 inline pCBenValueSegment
CBenValue::GetNextValueSegment(pCBenValueSegment
510 { return (pCBenValueSegment
) cValueSegments
.GetNextOrNULL(pCurrValueSegment
); }
512 inline pLtcBenContainer
CBenValue::GetContainer()
513 { return GetProperty()->GetContainer(); }
515 class CBenNamedObject
: public CBenObject
518 virtual UtBool
IsNamedObject();
519 virtual UtBool
IsPropertyName();
520 virtual UtBool
IsTypeName();
522 public: // Internal methods
523 CBenNamedObject(pLtcBenContainer pContainer
, BenObjectID ObjectID
,
524 pCBenObject pPrevObject
, const char * sName
,
525 pCBenNamedObjectListElmt pPrevNamedObjectListElmt
);
526 const char * GetName() { return csName
.data(); }
529 const char * GetNameCStr() { return csName
.c_str(); }
532 void SetPosition(BenContainerPos Pos
) { cPos
= Pos
; }
533 BenContainerPos
GetPosition(void) { return cPos
; }
534 size_t GetLength(void) { return csName
.length()+ 1; }
535 pCBenNamedObjectListElmt
GetNameListElmt() { return &cNameListElmt
; }
539 CBenNamedObjectListElmt cNameListElmt
;
540 BenContainerPos cPos
;
543 class CBenPropertyName
: public CBenNamedObject
545 public: // Internal methods
546 CBenPropertyName(pLtcBenContainer pContainer
, BenObjectID ObjectID
,
547 pCBenObject pPrevObject
, const char * sName
,
548 pCBenNamedObjectListElmt pPrevNamedObjectListElmt
) :
549 CBenNamedObject(pContainer
, ObjectID
, pPrevObject
, sName
,
550 pPrevNamedObjectListElmt
) { ; }
551 virtual UtBool
IsPropertyName();
554 class CBenTypeName
: public CBenNamedObject
556 public: // Internal methods
557 CBenTypeName(pLtcBenContainer pContainer
, BenObjectID ObjectID
,
558 pCBenObject pPrevObject
, const char * sName
,
559 pCBenNamedObjectListElmt pPrevNamedObjectListElmt
) :
560 CBenNamedObject(pContainer
, ObjectID
, pPrevObject
, sName
,
561 pPrevNamedObjectListElmt
) { ; }
562 virtual UtBool
IsTypeName();
566 } // end namespace OpenStormBento