LibreOffice Module sc (master)  1
xestream.hxx
Go to the documentation of this file.
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 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_XESTREAM_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_XESTREAM_HXX
22 
23 #include <map>
24 #include <stack>
25 #include <rtl/strbuf.hxx>
26 
28 #include <sax/fshelper.hxx>
29 #include <tools/stream.hxx>
30 #include <formula/errorcodes.hxx>
31 #include "ftools.hxx"
32 #include <types.hxx>
33 
35 #include <vector>
36 
37 namespace com::sun::star::beans { struct NamedValue; }
38 
39 /* ============================================================================
40 Output stream class for Excel export
41 - CONTINUE record handling
42 ============================================================================ */
43 
44 class XclExpString;
45 class XclExpRoot;
47 typedef std::shared_ptr< XclExpBiff8Encrypter > XclExpEncrypterRef;
48 
72 {
73 public:
79  SvStream& rOutStrm,
80  const XclExpRoot& rRoot,
81  sal_uInt16 nMaxRecSize = 0 );
82 
83  ~XclExpStream();
84 
86  const XclExpRoot& GetRoot() const { return mrRoot; }
87 
89  void StartRecord( sal_uInt16 nRecId, std::size_t nRecSize );
91  void EndRecord();
92 
94  sal_uInt16 GetRawRecPos() const { return mnCurrSize; }
95 
97  void SetSliceSize( sal_uInt16 nSize );
98 
99  XclExpStream& operator<<( sal_Int8 nValue );
100  XclExpStream& operator<<( sal_uInt8 nValue );
101  XclExpStream& operator<<( sal_Int16 nValue );
102  XclExpStream& operator<<( sal_uInt16 nValue );
103  XclExpStream& operator<<( sal_Int32 nValue );
104  XclExpStream& operator<<( sal_uInt32 nValue );
105  XclExpStream& operator<<( float fValue );
106  XclExpStream& operator<<( double fValue );
107 
109  std::size_t Write( const void* pData, std::size_t nBytes );
111  void WriteZeroBytes( std::size_t nBytes );
112 
113  void WriteZeroBytesToRecord( std::size_t nBytes );
114 
117  void CopyFromStream( SvStream& rInStrm, sal_uInt64 nBytes = STREAM_SEEK_TO_END );
118 
119  // *** unicode string export is realized with helper class XclExpString ***
120  // (slice length setting has no effect here -> disabled automatically)
121 
123  void WriteUnicodeBuffer( const ScfUInt16Vec& rBuffer, sal_uInt8 nFlags );
124 
125  // *** write 8-bit-strings ***
126  // (slice length setting has no effect here -> disabled automatically)
127 
129  void WriteByteString( const OString& rString );
130 
132  void WriteCharBuffer( const ScfUInt8Vec& rBuffer );
133 
134  // *** SvStream access ***
135 
137  void SetSvStreamPos(sal_uInt64 nPos);
139  sal_uInt64 GetSvStreamPos() const { return mrStrm.Tell(); }
140 
141  void SetEncrypter( XclExpEncrypterRef const & xEncrypter );
142 
143  bool HasValidEncrypter() const;
144 
145  void EnableEncryption( bool bEnable = true );
146 
147  void DisableEncryption();
148 
149 private:
151  void InitRecord( sal_uInt16 nRecId );
153  void UpdateRecSize();
155  void UpdateSizeVars( std::size_t nSize );
157  void StartContinue();
159  void PrepareWrite( sal_uInt16 nSize );
162  sal_uInt16 PrepareWrite();
163 
165  void WriteRawZeroBytes( std::size_t nBytes );
166 
167 private:
170 
173 
174  // length data
175  sal_uInt16 mnMaxRecSize;
176  sal_uInt16 mnMaxContSize;
177  sal_uInt16 mnCurrMaxSize;
178  sal_uInt16 mnMaxSliceSize;
179  sal_uInt16 mnHeaderSize;
180  sal_uInt16 mnCurrSize;
181  sal_uInt16 mnSliceSize;
182  std::size_t mnPredictSize;
183 
184  // stream position data
185  std::size_t mnLastSizePos;
186  bool mbInRec;
187 };
188 
190 {
191 public:
192  explicit XclExpBiff8Encrypter( const XclExpRoot& rRoot );
194 
195  bool IsValid() const { return mbValid; }
196 
197  void GetSaltDigest( sal_uInt8 pnSaltDigest[16] ) const;
198  void GetSalt( sal_uInt8 pnSalt[16] ) const;
199  void GetDocId( sal_uInt8 pnDocId[16] ) const;
200 
201  void Encrypt( SvStream& rStrm, sal_uInt8 nData );
202  void Encrypt( SvStream& rStrm, sal_uInt16 nData );
203  void Encrypt( SvStream& rStrm, sal_uInt32 nData );
204 
205  void Encrypt( SvStream& rStrm, sal_Int8 nData );
206  void Encrypt( SvStream& rStrm, sal_Int16 nData );
207  void Encrypt( SvStream& rStrm, sal_Int32 nData );
208 
209  void Encrypt( SvStream& rStrm, float fValue );
210  void Encrypt( SvStream& rStrm, double fValue );
211 
212  void EncryptBytes( SvStream& rStrm, ::std::vector<sal_uInt8>& aBytes );
213 
214 private:
215  void Init( const css::uno::Sequence< css::beans::NamedValue >& aEncryptionData );
216 
217  static sal_uInt32 GetBlockPos( std::size_t nStrmPos );
218  static sal_uInt16 GetOffsetInBlock( std::size_t nStrmPos );
219 
220 private:
225 
226  sal_uInt64 mnOldPos;
227  bool mbValid;
228 };
229 
230 // `s.GetChar(0) != 0` needed because some strings on export only contain NULL.
231 #define XESTRING_TO_PSZ(s) \
232  (s.Len() && s.GetChar( 0 ) != 0 ? XclXmlUtils::ToOString( s ).getStr() : nullptr)
233 
234 class ScAddress;
235 class ScDocShell;
236 class ScFormulaCell;
237 class ScRange;
238 class ScRangeList;
239 class ScTokenArray;
240 struct XclAddress;
241 struct XclFontData;
242 class XclRangeList;
243 namespace sc { class CompileFormulaContext; }
244 
246 {
247 public:
248  XclXmlUtils() = delete;
249  ~XclXmlUtils() = delete;
250  XclXmlUtils(const XclXmlUtils&) = delete;
251  XclXmlUtils& operator=(const XclXmlUtils&) = delete;
252 
253  static void GetFormulaTypeAndValue( ScFormulaCell& rCell, const char*& sType, OUString& rValue);
254  static OUString GetStreamName( const char* sStreamDir, const char* sStream, sal_Int32 nId );
255 
256  static OString ToOString( const Color& rColor );
257  static OString ToOString( const ScfUInt16Vec& rBuffer );
258  static OStringBuffer& ToOString( OStringBuffer& s, const ScAddress& rRange );
259  static OString ToOString( const ScDocument& rDoc, const ScRange& rRange, bool bFullAddressNotation = false );
260  static OString ToOString( const ScDocument& rDoc, const ScRangeList& rRangeList );
261  static OStringBuffer& ToOString( OStringBuffer& s, const XclAddress& rAddress );
262  static OString ToOString( const XclExpString& s );
263  static OString ToOString( const ScDocument& rDoc, const XclRangeList& rRangeList );
264 
265  static OUString ToOUString( const char* s );
266  static OUString ToOUString( const ScfUInt16Vec& rBuffer, sal_Int32 nStart = 0, sal_Int32 nLength = -1 );
267  static OUString ToOUString( sc::CompileFormulaContext& rCtx, const ScAddress& rAddress,
268  const ScTokenArray* pTokenArray, FormulaError nErrCode = FormulaError::NONE );
269  static OUString ToOUString( const XclExpString& s );
270 
271  template <class T>
272  static sax_fastparser::FSHelperPtr WriteElement(sax_fastparser::FSHelperPtr pStream, sal_Int32 nElement, const T& value)
273  {
274  pStream->startElement(nElement);
275  pStream->write(value);
276  pStream->endElement(nElement);
277 
278  return pStream;
279  }
280  static sax_fastparser::FSHelperPtr WriteFontData( sax_fastparser::FSHelperPtr pStream, const XclFontData& rFontData, sal_Int32 nNameId );
281 };
282 
284 {
285 public:
286  XclExpXmlStream( const css::uno::Reference< css::uno::XComponentContext >& rCC, bool bExportVBA, bool bExportTemplate );
287  virtual ~XclExpXmlStream() override;
288 
290  const XclExpRoot& GetRoot() const { return *mpRoot; }
291 
293  void PushStream( sax_fastparser::FSHelperPtr const & aStream );
294  void PopStream();
295 
296  sax_fastparser::FSHelperPtr GetStreamForPath( const OUString& rPath );
297 
298  template <typename Str, typename... Args>
299  void WriteAttributes(sal_Int32 nAttribute, Str&& value, Args&&... rest)
300  {
301  WriteAttribute(nAttribute, std::forward<Str>(value));
302  if constexpr(sizeof...(rest) > 0)
303  {
304  // coverity[stray_semicolon : FALSE] - coverity parse error
305  WriteAttributes(std::forward<Args>(rest)...);
306  }
307  }
308 
310  const OUString& sFullStream,
311  const OUString& sRelativeStream,
312  const css::uno::Reference< css::io::XOutputStream >& xParentRelation,
313  const char* sContentType,
314  const char* sRelationshipType,
315  OUString* pRelationshipId = nullptr );
316 
317  // ignore
318  virtual bool exportDocument() override;
319 
320  // only needed for import; ignore
321  virtual bool importDocument() throw() override;
322  virtual oox::vml::Drawing* getVmlDrawing() override;
323  virtual const oox::drawingml::Theme* getCurrentTheme() const override;
324  virtual oox::drawingml::table::TableStyleListPtr getTableStyles() override;
325  virtual oox::drawingml::chart::ChartConverter* getChartConverter() override;
326 
327 private:
328  virtual ::oox::ole::VbaProject* implCreateVbaProject() const override;
329  virtual OUString SAL_CALL getImplementationName() override;
331  void WriteAttribute(sal_Int32 nAttr, const OUString& sVal);
332  void WriteAttribute(sal_Int32 nAttr, const OString& sVal)
333  {
334  WriteAttribute(nAttr, OStringToOUString(sVal, RTL_TEXTENCODING_UTF8));
335  }
336  void WriteAttribute(sal_Int32 nAttr, const char* sVal)
337  {
338  if (sVal)
339  WriteAttribute(nAttr, OUString(sVal, strlen(sVal), RTL_TEXTENCODING_UTF8));
340  }
341 
342  void validateTabNames(std::vector<OUString>& aOriginalTabNames);
343  void restoreTabNames(const std::vector<OUString>& aOriginalTabNames);
344  void renameTab(SCTAB aTab, OUString aNewName);
345 
346  typedef std::map< OUString,
347  std::pair< OUString,
349 
351  std::stack< sax_fastparser::FSHelperPtr > maStreams;
352  XclExpXmlPathToStateMap maOpenedStreamMap;
353 
356 };
357 
358 #endif
359 
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual const oox::drawingml::Theme * getCurrentTheme() const override
Definition: xestream.cxx:973
void WriteAttributes(sal_Int32 nAttribute, Str &&value, Args &&...rest)
Definition: xestream.hxx:299
This class stores an unformatted or formatted string for Excel export.
Definition: xestring.hxx:43
void GetSaltDigest(sal_uInt8 pnSaltDigest[16]) const
Definition: xestream.cxx:472
virtual oox::vml::Drawing * getVmlDrawing() override
Definition: xestream.cxx:968
void StartContinue()
Writes CONTINUE header, internal setup.
Definition: xestream.cxx:410
::std::vector< sal_uInt8 > ScfUInt8Vec
Definition: ftools.hxx:251
std::size_t Write(const void *pData, std::size_t nBytes)
Writes nBytes bytes from memory.
Definition: xestream.cxx:215
static void GetFormulaTypeAndValue(ScFormulaCell &rCell, const char *&sType, OUString &rValue)
Definition: xestream.cxx:657
static OString ToOString(const Color &rColor)
Definition: xestream.cxx:698
char const sStream[]
sal_uInt16 mnCurrSize
Record size written in last record header.
Definition: xestream.hxx:180
void SetEncrypter(XclExpEncrypterRef const &xEncrypter)
Definition: xestream.cxx:348
const XclExpRoot & GetRoot() const
Returns the filter root data.
Definition: xestream.hxx:290
static sax_fastparser::FSHelperPtr WriteElement(sax_fastparser::FSHelperPtr pStream, sal_Int32 nElement, const T &value)
Definition: xestream.hxx:272
This class is used to export Excel record streams.
Definition: xestream.hxx:71
sal_uInt16 GetRawRecPos() const
Returns the position inside of current record (starts by 0 in every CONTINUE).
Definition: xestream.hxx:94
signed char sal_Int8
sal_uInt16 mnMaxContSize
Maximum size of record content.
Definition: xestream.hxx:176
static OUString ToOUString(const char *s)
Definition: xestream.cxx:783
virtual OUString SAL_CALL getImplementationName() override
Definition: xestream.cxx:1131
std::shared_ptr< TableStyleList > TableStyleListPtr
sal_uInt16 mnCurrMaxSize
Maximum size of CONTINUE content.
Definition: xestream.hxx:177
virtual bool exportDocument() override
Definition: xestream.cxx:1001
bool mbInRec
Stream position of size field in current header.
Definition: xestream.hxx:186
This struct helps reading and writing Excel fonts.
Definition: xlstyle.hxx:286
bool HasValidEncrypter() const
Definition: xestream.cxx:353
sal_Int16 nId
std::shared_ptr< XclExpBiff8Encrypter > XclExpEncrypterRef
Definition: xestream.hxx:46
void EncryptBytes(SvStream &rStrm,::std::vector< sal_uInt8 > &aBytes)
Definition: xestream.cxx:579
::msfilter::MSCodec_Std97 maCodec
Definition: xestream.hxx:221
sal_uInt16 mnMaxSliceSize
Current maximum, either mnMaxRecSize or mnMaxContSize.
Definition: xestream.hxx:178
XclExpXmlStream(const css::uno::Reference< css::uno::XComponentContext > &rCC, bool bExportVBA, bool bExportTemplate)
Definition: xestream.cxx:897
sal_uInt16 mnSliceSize
Count of bytes already written in current record.
Definition: xestream.hxx:181
virtual ~XclExpXmlStream() override
Definition: xestream.cxx:905
void Encrypt(SvStream &rStrm, sal_uInt8 nData)
Definition: xestream.cxx:490
sax_fastparser::FSHelperPtr GetStreamForPath(const OUString &rPath)
Definition: xestream.cxx:927
css::uno::Any const & rValue
const XclExpRoot * mpRoot
Definition: xestream.hxx:350
Access to global data from other classes.
Definition: xeroot.hxx:113
virtual bool importDocument() override
Definition: xestream.cxx:963
SvStream & mrStrm
Definition: xestream.hxx:168
~XclXmlUtils()=delete
void PushStream(sax_fastparser::FSHelperPtr const &aStream)
Definition: xestream.cxx:916
static sax_fastparser::FSHelperPtr WriteFontData(sax_fastparser::FSHelperPtr pStream, const XclFontData &rFontData, sal_Int32 nNameId)
Definition: xestream.cxx:866
void validateTabNames(std::vector< OUString > &aOriginalTabNames)
Definition: xestream.cxx:1136
ScDocShell * getDocShell()
Definition: xestream.cxx:989
const XclExpRoot & mrRoot
Reference to the system output stream.
Definition: xestream.hxx:169
sal_uInt16 mnHeaderSize
Maximum size of data slices (parts that cannot be split).
Definition: xestream.hxx:179
XclXmlUtils & operator=(const XclXmlUtils &)=delete
sax_fastparser::FSHelperPtr & GetCurrentStream()
Definition: xestream.cxx:910
void PopStream()
Definition: xestream.cxx:921
void UpdateRecSize()
Rewrites correct record length, if different from calculated.
Definition: xestream.cxx:387
void SetSliceSize(sal_uInt16 nSize)
Sets data slice length.
Definition: xestream.cxx:129
void Init(const css::uno::Sequence< css::beans::NamedValue > &aEncryptionData)
Definition: xestream.cxx:544
XclExpEncrypterRef mxEncrypter
Definition: xestream.hxx:172
static sal_uInt32 GetBlockPos(std::size_t nStrmPos)
Definition: xestream.cxx:569
sal_uInt16 mnMaxRecSize
Definition: xestream.hxx:175
void restoreTabNames(const std::vector< OUString > &aOriginalTabNames)
Definition: xestream.cxx:1212
std::size_t mnPredictSize
Count of bytes already written in current slice.
Definition: xestream.hxx:182
OptionalString sType
sal_uInt64 GetSvStreamPos() const
Returns the absolute position of the system stream.
Definition: xestream.hxx:139
XclExpXmlPathToStateMap maOpenedStreamMap
Definition: xestream.hxx:352
virtual oox::drawingml::chart::ChartConverter * getChartConverter() override
Definition: xestream.cxx:983
sal_uInt8 mpnSaltDigest[16]
Definition: xestream.hxx:224
A 2D cell range address list with Excel column and row indexes.
Definition: xladdress.hxx:101
void EndRecord()
Checks and corrects real record length.
Definition: xestream.cxx:120
sal_uInt8 mpnDocId[16]
Crypto algorithm implementation.
Definition: xestream.hxx:222
virtual ::oox::ole::VbaProject * implCreateVbaProject() const override
Definition: xestream.cxx:1126
void GetDocId(sal_uInt8 pnDocId[16]) const
Definition: xestream.cxx:484
A 2D cell address struct with Excel column and row indexes.
Definition: xladdress.hxx:30
XclExpStream & operator<<(sal_Int8 nValue)
Definition: xestream.cxx:135
void CopyFromStream(SvStream &rInStrm, sal_uInt64 nBytes=STREAM_SEEK_TO_END)
Copies nBytes bytes from current position of the stream rInStrm.
Definition: xestream.cxx:283
std::shared_ptr< FastSerializerHelper > FSHelperPtr
void WriteAttribute(sal_Int32 nAttr, const OUString &sVal)
Definition: xestream.cxx:934
static sal_uInt16 GetOffsetInBlock(std::size_t nStrmPos)
Definition: xestream.cxx:574
void WriteByteString(const OString &rString)
Writes string length field and OString buffer.
Definition: xestream.cxx:328
FormulaError
XclXmlUtils()=delete
void SetSvStreamPos(sal_uInt64 nPos)
Sets position of system stream (only allowed outside of records).
Definition: xestream.cxx:368
static OUString GetStreamName(const char *sStreamDir, const char *sStream, sal_Int32 nId)
Definition: xestream.cxx:683
std::stack< sax_fastparser::FSHelperPtr > maStreams
Definition: xestream.hxx:351
void WriteZeroBytesToRecord(std::size_t nBytes)
Definition: xestream.cxx:273
void GetSalt(sal_uInt8 pnSalt[16]) const
Definition: xestream.cxx:478
void InitRecord(sal_uInt16 nRecId)
Writes header data, internal setup.
Definition: xestream.cxx:376
::std::vector< sal_uInt16 > ScfUInt16Vec
Definition: ftools.hxx:253
XclExpStream(SvStream &rOutStrm, const XclExpRoot &rRoot, sal_uInt16 nMaxRecSize=0)
Constructs the Excel record export stream.
Definition: xestream.cxx:84
unsigned char sal_uInt8
virtual oox::drawingml::table::TableStyleListPtr getTableStyles() override
Definition: xestream.cxx:978
std::map< OUString, std::pair< OUString, sax_fastparser::FSHelperPtr > > XclExpXmlPathToStateMap
Definition: xestream.hxx:348
std::size_t mnLastSizePos
Predicted size received from calling function.
Definition: xestream.hxx:185
void WriteRawZeroBytes(std::size_t nBytes)
Writes a raw sequence of zero bytes.
Definition: xestream.cxx:444
void WriteAttribute(sal_Int32 nAttr, const char *sVal)
Definition: xestream.hxx:336
void UpdateSizeVars(std::size_t nSize)
Recalculates mnCurrSize and mnSliceSize.
Definition: xestream.cxx:396
bool mbExportTemplate
Definition: xestream.hxx:355
sal_uInt64 Tell() const
void WriteCharBuffer(const ScfUInt8Vec &rBuffer)
Writes 8-bit character buffer.
Definition: xestream.cxx:342
Any value
void DisableEncryption()
Definition: xestream.cxx:363
bool mbValid
Last known stream position.
Definition: xestream.hxx:227
bool IsValid() const
Definition: xestream.hxx:195
void renameTab(SCTAB aTab, OUString aNewName)
Definition: xestream.cxx:1231
void StartRecord(sal_uInt16 nRecId, std::size_t nRecSize)
Starts a new record: writes header data, stores calculated record size.
Definition: xestream.cxx:108
sal_uInt64 mnOldPos
Definition: xestream.hxx:226
void EnableEncryption(bool bEnable=true)
Definition: xestream.cxx:358
sal_Int32 nLength
const XclExpRoot & GetRoot() const
Returns the filter root data.
Definition: xestream.hxx:86
sal_uInt8 mpnSalt[16]
Definition: xestream.hxx:223
void WriteUnicodeBuffer(const ScfUInt16Vec &rBuffer, sal_uInt8 nFlags)
Writes Unicode buffer as 8/16 bit, repeats nFlags at start of a CONTINUE record.
Definition: xestream.cxx:305
sax_fastparser::FSHelperPtr CreateOutputStream(const OUString &sFullStream, const OUString &sRelativeStream, const css::uno::Reference< css::io::XOutputStream > &xParentRelation, const char *sContentType, const char *sRelationshipType, OUString *pRelationshipId=nullptr)
Definition: xestream.cxx:939
XclExpBiff8Encrypter(const XclExpRoot &rRoot)
Definition: xestream.cxx:457
bool mbUseEncrypter
Filter root data.
Definition: xestream.hxx:171
sal_Int16 SCTAB
Definition: types.hxx:23
void WriteZeroBytes(std::size_t nBytes)
Writes a sequence of nBytes zero bytes (respects slice setting).
Definition: xestream.cxx:256
sal_uInt16 PrepareWrite()
Creates CONTINUE record at end of record.
Definition: xestream.cxx:429