Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : // ============================================================================
21 : :
22 : : #ifndef SC_XESTREAM_HXX
23 : : #define SC_XESTREAM_HXX
24 : :
25 : : #include <com/sun/star/beans/NamedValue.hpp>
26 : :
27 : : #include <map>
28 : : #include <stack>
29 : : #include <string>
30 : :
31 : : #include <oox/core/xmlfilterbase.hxx>
32 : : #include <oox/token/tokens.hxx>
33 : : #include <sax/fshelper.hxx>
34 : :
35 : : #include "xlstream.hxx"
36 : : #include "xestring.hxx"
37 : :
38 : : #include <filter/msfilter/mscodec.hxx>
39 : : #include <vector>
40 : :
41 : : /* ============================================================================
42 : : Output stream class for Excel export
43 : : - CONTINUE record handling
44 : : ============================================================================ */
45 : :
46 : : class XclExpRoot;
47 : : class XclExpBiff8Encrypter;
48 : : typedef boost::shared_ptr< XclExpBiff8Encrypter > XclExpEncrypterRef;
49 : :
50 : : /** This class is used to export Excel record streams.
51 : : @descr An instance is constructed with an SvStream and the maximum size of Excel
52 : : record contents (in BIFF5: 2080 bytes, in BIFF8: 8224 bytes).
53 : :
54 : : To start writing a record call StartRecord(). Parameters are the record identifier
55 : : and any calculated record size. This is for optimizing the write process: if the real
56 : : written data has the same size as the calculated, the stream will not seek back and
57 : : update the record size field. But it is not mandatory to calculate a size. Each
58 : : record must be closed by calling EndRecord(). This will check (and update) the record
59 : : size field.
60 : :
61 : : If some data exceeds the record size limit, a CONTINUE record is started automatically
62 : : and the new data will be written to this record.
63 : :
64 : : If specific data pieces must not be splitted, use SetSliceLen(). For instance:
65 : : To write a sequence of 16-bit values, where 4 values form a unit and cannot be
66 : : split, call SetSliceLen( 8 ) first (4*2 bytes == 8).
67 : :
68 : : To write unicode character arrays, call WriteUnicodeBuffer(). It creates CONTINUE
69 : : records and repeats the unicode string flag byte automatically. This function is used
70 : : for instance from the class XclExpString which can write complete unicode strings.
71 : : */
72 : : class XclExpStream
73 : : {
74 : : public:
75 : : /** Constructs the Excel record export stream.
76 : : @param rOutStrm The system output stream to write to.
77 : : @param nMaxRecSize The maximum allowed size of record content (depending on BIFF type).
78 : : If 0 is passed, the record size will be set automatically, depending on the current BIFF type. */
79 : : XclExpStream(
80 : : SvStream& rOutStrm,
81 : : const XclExpRoot& rRoot,
82 : : sal_uInt16 nMaxRecSize = 0 );
83 : :
84 : : ~XclExpStream();
85 : :
86 : : /** Returns the filter root data. */
87 : 0 : inline const XclExpRoot& GetRoot() const { return mrRoot; }
88 : :
89 : : /** Starts a new record: writes header data, stores calculated record size. */
90 : : void StartRecord( sal_uInt16 nRecId, sal_Size nRecSize );
91 : : /** Checks and corrects real record length. Must be called everytime a record is finished. */
92 : : void EndRecord();
93 : :
94 : : /** Returns the position inside of current record (starts by 0 in every CONTINUE). */
95 : 0 : inline sal_uInt16 GetRawRecPos() const { return mnCurrSize; }
96 : :
97 : : /** Returns the maximum size of a record. */
98 : : inline sal_uInt16 GetMaxRecSize() const { return mnMaxRecSize; }
99 : : /** Sets maximum record size (valid only for current record). */
100 : : inline void SetMaxRecSize( sal_uInt16 nMax ) { mnCurrMaxSize = nMax; }
101 : : /** Sets maximum size of CONTINUE records (valid only for current record). */
102 : : inline void SetMaxContSize( sal_uInt16 nMax ) { mnMaxContSize = nMax; }
103 : :
104 : : /** Sets data slice length. 0 = no slices. */
105 : : void SetSliceSize( sal_uInt16 nSize );
106 : :
107 : : XclExpStream& operator<<( sal_Int8 nValue );
108 : : XclExpStream& operator<<( sal_uInt8 nValue );
109 : : XclExpStream& operator<<( sal_Int16 nValue );
110 : : XclExpStream& operator<<( sal_uInt16 nValue );
111 : : XclExpStream& operator<<( sal_Int32 nValue );
112 : : XclExpStream& operator<<( sal_uInt32 nValue );
113 : : XclExpStream& operator<<( float fValue );
114 : : XclExpStream& operator<<( double fValue );
115 : :
116 : : /** Writes nBytes bytes from memory. */
117 : : sal_Size Write( const void* pData, sal_Size nBytes );
118 : : /** Writes a sequence of nBytes zero bytes (respects slice setting). */
119 : : void WriteZeroBytes( sal_Size nBytes );
120 : :
121 : : void WriteZeroBytesToRecord( sal_Size nBytes );
122 : :
123 : : /** Copies nBytes bytes from current position of the stream rInStrm.
124 : : @descr Omitting the second parameter means: read to end of stream. */
125 : : sal_Size CopyFromStream( SvStream& rInStrm, sal_Size nBytes = STREAM_SEEK_TO_END );
126 : :
127 : : // *** unicode string export is realized with helper class XclExpString ***
128 : : // (slice length setting has no effect here -> disabled automatically)
129 : :
130 : : /** Writes Unicode buffer as 8/16 bit, repeats nFlags at start of a CONTINUE record. */
131 : : void WriteUnicodeBuffer( const ScfUInt16Vec& rBuffer, sal_uInt8 nFlags );
132 : :
133 : : // *** write 8-bit-strings ***
134 : : // (slice length setting has no effect here -> disabled automatically)
135 : :
136 : : /** Writes string length field and rtl::OString buffer. */
137 : : void WriteByteString(
138 : : const rtl::OString& rString,
139 : : sal_uInt16 nMaxLen = 0x00FF,
140 : : bool b16BitCount = false );
141 : :
142 : : /** Writes 8-bit character buffer. */
143 : : void WriteCharBuffer( const ScfUInt8Vec& rBuffer );
144 : :
145 : : // *** SvStream access ***
146 : :
147 : : /** Sets position of system stream (only allowed outside of records). */
148 : : sal_Size SetSvStreamPos( sal_Size nPos );
149 : : /** Returns the absolute position of the system stream. */
150 : 0 : inline sal_Size GetSvStreamPos() const { return mrStrm.Tell(); }
151 : :
152 : : void SetEncrypter( XclExpEncrypterRef xEncrypter );
153 : :
154 : : bool HasValidEncrypter() const;
155 : :
156 : : void EnableEncryption( bool bEnable = true );
157 : :
158 : : void DisableEncryption();
159 : :
160 : : private:
161 : : /** Writes header data, internal setup. */
162 : : void InitRecord( sal_uInt16 nRecId );
163 : : /** Rewrites correct record length, if different from calculated. */
164 : : void UpdateRecSize();
165 : : /** Recalculates mnCurrSize and mnSliceSize. */
166 : : void UpdateSizeVars( sal_Size nSize );
167 : : /** Writes CONTINUE header, internal setup. */
168 : : void StartContinue();
169 : : /** Refreshes counter vars, creates CONTINUE records. */
170 : : void PrepareWrite( sal_uInt16 nSize );
171 : : /** Creates CONTINUE record at end of record.
172 : : @return Maximum data block size remaining. */
173 : : sal_uInt16 PrepareWrite();
174 : :
175 : : /** Writes a raw sequence of zero bytes. */
176 : : void WriteRawZeroBytes( sal_Size nBytes );
177 : :
178 : : private:
179 : : SvStream& mrStrm; /// Reference to the system output stream.
180 : : const XclExpRoot& mrRoot; /// Filter root data.
181 : :
182 : : bool mbUseEncrypter;
183 : : XclExpEncrypterRef mxEncrypter;
184 : :
185 : : // length data
186 : : sal_uInt16 mnMaxRecSize; /// Maximum size of record content.
187 : : sal_uInt16 mnMaxContSize; /// Maximum size of CONTINUE content.
188 : : sal_uInt16 mnCurrMaxSize; /// Current maximum, either mnMaxRecSize or mnMaxContSize.
189 : : sal_uInt16 mnMaxSliceSize; /// Maximum size of data slices (parts that cannot be split).
190 : : sal_uInt16 mnHeaderSize; /// Record size written in last record header.
191 : : sal_uInt16 mnCurrSize; /// Count of bytes already written in current record.
192 : : sal_uInt16 mnSliceSize; /// Count of bytes already written in current slice.
193 : : sal_Size mnPredictSize; /// Predicted size received from calling function.
194 : :
195 : : // stream position data
196 : : sal_Size mnLastSizePos; /// Stream position of size field in current header.
197 : : bool mbInRec; /// true = currently writing inside of a record.
198 : : };
199 : :
200 : : // ============================================================================
201 : :
202 : : class XclExpBiff8Encrypter
203 : : {
204 : : public:
205 : : explicit XclExpBiff8Encrypter( const XclExpRoot& rRoot );
206 : : ~XclExpBiff8Encrypter();
207 : :
208 : : bool IsValid() const;
209 : :
210 : : void GetSaltDigest( sal_uInt8 pnSaltDigest[16] ) const;
211 : : void GetSalt( sal_uInt8 pnSalt[16] ) const;
212 : : void GetDocId( sal_uInt8 pnDocId[16] ) const;
213 : :
214 : : void Encrypt( SvStream& rStrm, sal_uInt8 nData );
215 : : void Encrypt( SvStream& rStrm, sal_uInt16 nData );
216 : : void Encrypt( SvStream& rStrm, sal_uInt32 nData );
217 : :
218 : : void Encrypt( SvStream& rStrm, sal_Int8 nData );
219 : : void Encrypt( SvStream& rStrm, sal_Int16 nData );
220 : : void Encrypt( SvStream& rStrm, sal_Int32 nData );
221 : :
222 : : void Encrypt( SvStream& rStrm, float fValue );
223 : : void Encrypt( SvStream& rStrm, double fValue );
224 : :
225 : : void EncryptBytes( SvStream& rStrm, ::std::vector<sal_uInt8>& aBytes );
226 : :
227 : : private:
228 : : void Init( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aEncryptionData );
229 : :
230 : : sal_uInt32 GetBlockPos( sal_Size nStrmPos ) const;
231 : : sal_uInt16 GetOffsetInBlock( sal_Size nStrmPos ) const;
232 : :
233 : : private:
234 : : ::msfilter::MSCodec_Std97 maCodec; /// Crypto algorithm implementation.
235 : : sal_uInt8 mpnDocId[16];
236 : : sal_uInt8 mpnSalt[16];
237 : : sal_uInt8 mpnSaltDigest[16];
238 : :
239 : : const XclExpRoot& mrRoot;
240 : : sal_Size mnOldPos; /// Last known stream position
241 : : bool mbValid;
242 : : };
243 : :
244 : : // ----------------------------------------------------------------------------
245 : :
246 : :
247 : : // ============================================================================
248 : :
249 : : // `s.GetChar(0) != 0` needed because some strings on export only contain NULL.
250 : : #define XESTRING_TO_PSZ(s) \
251 : : (s.Len() && s.GetChar( 0 ) != 0 ? XclXmlUtils::ToOString( s ).getStr() : NULL)
252 : :
253 : : class ScAddress;
254 : : class ScDocShell;
255 : : class ScDocument;
256 : : class ScFormulaCell;
257 : : class ScRange;
258 : : class ScRangeList;
259 : : class ScTokenArray;
260 : : struct XclAddress;
261 : : struct XclFontData;
262 : : struct XclRange;
263 : : class XclRangeList;
264 : :
265 : : class XclXmlUtils
266 : : {
267 : : XclXmlUtils();
268 : : ~XclXmlUtils();
269 : : XclXmlUtils(const XclXmlUtils&);
270 : : XclXmlUtils& operator=(const XclXmlUtils&);
271 : : public:
272 : : static void GetFormulaTypeAndValue( ScFormulaCell& rCell, const char*& sType, ::rtl::OUString& rValue);
273 : : static ::rtl::OUString GetStreamName( const char* sStreamDir, const char* sStream, sal_Int32 nId );
274 : :
275 : : static ::rtl::OString ToOString( const Color& rColor );
276 : : static ::rtl::OString ToOString( const ::rtl::OUString& s );
277 : : static ::rtl::OString ToOString( const ScfUInt16Vec& rBuffer );
278 : : static ::rtl::OString ToOString( const String& s );
279 : : static ::rtl::OString ToOString( const ScAddress& rRange );
280 : : static ::rtl::OString ToOString( const ScRange& rRange );
281 : : static ::rtl::OString ToOString( const ScRangeList& rRangeList );
282 : : static ::rtl::OString ToOString( const XclAddress& rAddress );
283 : : static ::rtl::OString ToOString( const XclExpString& s );
284 : : static ::rtl::OString ToOString( const XclRange& rRange );
285 : : static ::rtl::OString ToOString( const XclRangeList& rRangeList );
286 : :
287 : : static ::rtl::OUString ToOUString( const char* s );
288 : : static ::rtl::OUString ToOUString( const ScfUInt16Vec& rBuffer, sal_Int32 nStart = 0, sal_Int32 nLength = -1 );
289 : : static ::rtl::OUString ToOUString( const String& s );
290 : : static ::rtl::OUString ToOUString( ScDocument& rDocument, const ScAddress& rAddress, ScTokenArray* pTokenArray );
291 : : static ::rtl::OUString ToOUString( const XclExpString& s );
292 : : static const char* ToPsz( bool b );
293 : :
294 : : static sax_fastparser::FSHelperPtr WriteElement( sax_fastparser::FSHelperPtr pStream, sal_Int32 nElement, sal_Int32 nValue );
295 : : static sax_fastparser::FSHelperPtr WriteElement( sax_fastparser::FSHelperPtr pStream, sal_Int32 nElement, sal_Int64 nValue );
296 : : static sax_fastparser::FSHelperPtr WriteElement( sax_fastparser::FSHelperPtr pStream, sal_Int32 nElement, const char* sValue );
297 : : static sax_fastparser::FSHelperPtr WriteFontData( sax_fastparser::FSHelperPtr pStream, const XclFontData& rFontData, sal_Int32 nNameId );
298 : : };
299 : :
300 : : class XclExpXmlStream : public oox::core::XmlFilterBase
301 : : {
302 : : public:
303 : : XclExpXmlStream( const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& rCC );
304 : : virtual ~XclExpXmlStream();
305 : :
306 : : /** Returns the filter root data. */
307 : 0 : inline const XclExpRoot& GetRoot() const { return *mpRoot; }
308 : :
309 : : sax_fastparser::FSHelperPtr& GetCurrentStream();
310 : : void PushStream( sax_fastparser::FSHelperPtr aStream );
311 : : void PopStream();
312 : :
313 : : sax_fastparser::FSHelperPtr GetStreamForPath( const ::rtl::OUString& rPath );
314 : :
315 : : sax_fastparser::FSHelperPtr& WriteAttributes( sal_Int32 nAttribute, ... );
316 : :
317 : : sax_fastparser::FSHelperPtr CreateOutputStream (
318 : : const ::rtl::OUString& sFullStream,
319 : : const ::rtl::OUString& sRelativeStream,
320 : : const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xParentRelation,
321 : : const char* sContentType,
322 : : const char* sRelationshipType,
323 : : ::rtl::OUString* pRelationshipId = NULL );
324 : :
325 : : // ignore
326 : : virtual bool exportDocument() throw();
327 : :
328 : : // only needed for import; ignore
329 : : virtual bool importDocument() throw();
330 : : virtual oox::vml::Drawing* getVmlDrawing();
331 : : virtual const oox::drawingml::Theme* getCurrentTheme() const;
332 : : virtual const oox::drawingml::table::TableStyleListPtr getTableStyles();
333 : : virtual oox::drawingml::chart::ChartConverter* getChartConverter();
334 : :
335 : : private:
336 : : virtual ::oox::ole::VbaProject* implCreateVbaProject() const;
337 : : virtual ::rtl::OUString implGetImplementationName() const;
338 : : ScDocShell *getDocShell();
339 : :
340 : : typedef std::map< ::rtl::OUString,
341 : : std::pair< ::rtl::OUString,
342 : : sax_fastparser::FSHelperPtr > > XclExpXmlPathToStateMap;
343 : :
344 : : const XclExpRoot* mpRoot;
345 : : std::stack< sax_fastparser::FSHelperPtr > maStreams;
346 : : XclExpXmlPathToStateMap maOpenedStreamMap;
347 : : };
348 : :
349 : : #endif
350 : :
351 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|