00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef SC_XESTREAM_HXX
00031 #define SC_XESTREAM_HXX
00032
00033 #ifdef XLSX_GUARD
00034 #error Should not include this from sc/source/filter/xlsx code
00035 #endif
00036
00037 #include <map>
00038 #include <stack>
00039 #include <string>
00040
00041 #include <oox/core/xmlfilterbase.hxx>
00042 #include <sax/fshelper.hxx>
00043
00044 #include "xlstream.hxx"
00045 #include "xestring.hxx"
00046
00047 #include <svx/mscodec.hxx>
00048 #include <vector>
00049
00050
00051
00052
00053
00054
00055
00056 class XclExpRoot;
00057 class XclExpBiff8Encrypter;
00058 typedef ScfRef< XclExpBiff8Encrypter > XclExpEncrypterRef;
00059
00082 class XclExpStream
00083 {
00084 public:
00089 XclExpStream(
00090 SvStream& rOutStrm,
00091 const XclExpRoot& rRoot,
00092 sal_uInt16 nMaxRecSize = 0 );
00093
00094 ~XclExpStream();
00095
00097 inline const XclExpRoot& GetRoot() const { return mrRoot; }
00098
00100 void StartRecord( sal_uInt16 nRecId, sal_Size nRecSize );
00102 void EndRecord();
00103
00105 inline sal_uInt16 GetRawRecPos() const { return mnCurrSize; }
00106
00108 inline sal_uInt16 GetMaxRecSize() const { return mnMaxRecSize; }
00110 inline void SetMaxRecSize( sal_uInt16 nMax ) { mnCurrMaxSize = nMax; }
00112 inline void SetMaxContSize( sal_uInt16 nMax ) { mnMaxContSize = nMax; }
00113
00115 void SetSliceSize( sal_uInt16 nSize );
00116
00117 XclExpStream& operator<<( sal_Int8 nValue );
00118 XclExpStream& operator<<( sal_uInt8 nValue );
00119 XclExpStream& operator<<( sal_Int16 nValue );
00120 XclExpStream& operator<<( sal_uInt16 nValue );
00121 XclExpStream& operator<<( sal_Int32 nValue );
00122 XclExpStream& operator<<( sal_uInt32 nValue );
00123 XclExpStream& operator<<( float fValue );
00124 XclExpStream& operator<<( double fValue );
00125
00127 sal_Size Write( const void* pData, sal_Size nBytes );
00129 void WriteZeroBytes( sal_Size nBytes );
00130
00131 void WriteZeroBytesToRecord( sal_Size nBytes );
00132
00135 sal_Size CopyFromStream( SvStream& rInStrm, sal_Size nBytes = STREAM_SEEK_TO_END );
00136
00137
00138
00139
00140
00141
00142
00144 void WriteUnicodeBuffer( const ScfUInt16Vec& rBuffer, sal_uInt8 nFlags );
00145
00146
00147
00148
00149
00150
00151
00152
00153
00155 void WriteByteString(
00156 const ByteString& rString,
00157 sal_uInt16 nMaxLen = 0x00FF,
00158 bool b16BitCount = false );
00159
00161 void WriteCharBuffer( const ScfUInt8Vec& rBuffer );
00162
00163
00164
00166 sal_Size SetSvStreamPos( sal_Size nPos );
00168 inline sal_Size GetSvStreamPos() const { return mrStrm.Tell(); }
00169
00170 void SetEncrypter( XclExpEncrypterRef xEncrypter );
00171
00172 bool HasValidEncrypter() const;
00173
00174 void EnableEncryption( bool bEnable = true );
00175
00176 void DisableEncryption();
00177
00178 private:
00180 void InitRecord( sal_uInt16 nRecId );
00182 void UpdateRecSize();
00184 void UpdateSizeVars( sal_Size nSize );
00186 void StartContinue();
00188 void PrepareWrite( sal_uInt16 nSize );
00191 sal_uInt16 PrepareWrite();
00192
00194 void WriteRawZeroBytes( sal_Size nBytes );
00195
00196 private:
00197 SvStream& mrStrm;
00198 const XclExpRoot& mrRoot;
00199
00200 bool mbUseEncrypter;
00201 XclExpEncrypterRef mxEncrypter;
00202
00203
00204 sal_uInt16 mnMaxRecSize;
00205 sal_uInt16 mnMaxContSize;
00206 sal_uInt16 mnCurrMaxSize;
00207 sal_uInt16 mnMaxSliceSize;
00208 sal_uInt16 mnHeaderSize;
00209 sal_uInt16 mnCurrSize;
00210 sal_uInt16 mnSliceSize;
00211 sal_Size mnPredictSize;
00212
00213
00214 sal_Size mnLastSizePos;
00215 bool mbInRec;
00216 };
00217
00218
00219
00220 class XclExpBiff8Encrypter
00221 {
00222 public:
00223 explicit XclExpBiff8Encrypter( const XclExpRoot& rRoot, const sal_uInt8 nDocId[16],
00224 const sal_uInt8 nSalt[16] );
00225 ~XclExpBiff8Encrypter();
00226
00227 bool IsValid() const;
00228
00229 void GetSaltDigest( sal_uInt8 nSaltDigest[16] ) const;
00230
00231 void Encrypt( SvStream& rStrm, sal_uInt8 nData );
00232 void Encrypt( SvStream& rStrm, sal_uInt16 nData );
00233 void Encrypt( SvStream& rStrm, sal_uInt32 nData );
00234
00235 void Encrypt( SvStream& rStrm, sal_Int8 nData );
00236 void Encrypt( SvStream& rStrm, sal_Int16 nData );
00237 void Encrypt( SvStream& rStrm, sal_Int32 nData );
00238
00239 void Encrypt( SvStream& rStrm, float fValue );
00240 void Encrypt( SvStream& rStrm, double fValue );
00241
00242 void EncryptBytes( SvStream& rStrm, ::std::vector<sal_uInt8>& aBytes );
00243
00244 private:
00245 void Init( const String& aPass, const sal_uInt8 nDocId[16],
00246 const sal_uInt8 nSalt[16] );
00247
00248 sal_uInt32 GetBlockPos( sal_Size nStrmPos ) const;
00249 sal_uInt16 GetOffsetInBlock( sal_Size nStrmPos ) const;
00250
00251 private:
00252 ::svx::MSCodec_Std97 maCodec;
00253 sal_uInt16 mnPassw[16];
00254 sal_uInt8 mnDocId[16];
00255 sal_uInt8 mnSaltDigest[16];
00256
00257 const XclExpRoot& mrRoot;
00258 sal_Size mnOldPos;
00259 bool mbValid;
00260 };
00261
00262
00263
00264
00265
00266
00267
00268 #define XESTRING_TO_PSZ(s) \
00269 (s.Len() && s.GetChar( 0 ) != 0 ? XclXmlUtils::ToOString( s ).getStr() : NULL)
00270
00271 class ScAddress;
00272 class ScDocument;
00273 class ScRange;
00274 class ScRangeList;
00275 class ScTokenArray;
00276 struct XclAddress;
00277 struct XclFontData;
00278 class XclRangeList;
00279
00280 class XclXmlUtils
00281 {
00282 XclXmlUtils();
00283 ~XclXmlUtils();
00284 XclXmlUtils(const XclXmlUtils&);
00285 XclXmlUtils& operator=(const XclXmlUtils&);
00286 public:
00287 static ::rtl::OUString GetStreamName( const char* sStreamDir, const char* sStream, sal_Int32 nId );
00288
00289 static ::rtl::OString ToOString( const Color& rColor );
00290 static ::rtl::OString ToOString( const ::rtl::OUString& s );
00291 static ::rtl::OString ToOString( const ScfUInt16Vec& rBuffer );
00292 static ::rtl::OString ToOString( const String& s );
00293 static ::rtl::OString ToOString( const ScAddress& rRange );
00294 static ::rtl::OString ToOString( const ScRange& rRange );
00295 static ::rtl::OString ToOString( const ScRangeList& rRangeList );
00296 static ::rtl::OString ToOString( const XclAddress& rAddress );
00297 static ::rtl::OString ToOString( const XclExpString& s );
00298 static ::rtl::OString ToOString( const XclRangeList& rRangeList );
00299
00300 static ::rtl::OUString ToOUString( const char* s );
00301 static ::rtl::OUString ToOUString( const ScfUInt16Vec& rBuffer, sal_Int32 nStart = 0, sal_Int32 nLength = -1 );
00302 static ::rtl::OUString ToOUString( const String& s );
00303 static ::rtl::OUString ToOUString( ScDocument& rDocument, const ScAddress& rAddress, ScTokenArray* pTokenArray );
00304 static ::rtl::OUString ToOUString( const XclExpString& s );
00305 static const char* ToPsz( bool b );
00306 };
00307
00308 class XclExpXmlStream : public oox::core::XmlFilterBase
00309 {
00310 public:
00311 XclExpXmlStream( const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >& rSMgr, SvStream& rStrm, const XclExpRoot& rRoot );
00312 virtual ~XclExpXmlStream();
00313
00315 inline const XclExpRoot& GetRoot() const { return mrRoot; }
00316
00317 sax_fastparser::FSHelperPtr& GetCurrentStream();
00318 void PushStream( sax_fastparser::FSHelperPtr aStream );
00319 void PopStream();
00320
00321 ::rtl::OUString GetIdForPath( const ::rtl::OUString& rPath );
00322 sax_fastparser::FSHelperPtr GetStreamForPath( const ::rtl::OUString& rPath );
00323
00324 sax_fastparser::FSHelperPtr& WriteAttributes( sal_Int32 nAttribute, ... );
00325 sax_fastparser::FSHelperPtr& WriteFontData( const XclFontData& rFontData, sal_Int32 nNameId );
00326
00327 sax_fastparser::FSHelperPtr CreateOutputStream (
00328 const ::rtl::OUString& sFullStream,
00329 const ::rtl::OUString& sRelativeStream,
00330 const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xParentRelation,
00331 const char* sContentType,
00332 const char* sRelationshipType,
00333 ::rtl::OUString* pRelationshipId = NULL );
00334
00335
00336 virtual bool exportDocument() throw();
00337
00338
00339 virtual bool importDocument() throw();
00340 virtual oox::vml::Drawing* getVmlDrawing();
00341 virtual const oox::drawingml::Theme* getCurrentTheme() const;
00342 virtual const oox::drawingml::table::TableStyleListPtr getTableStyles();
00343 virtual oox::drawingml::chart::ChartConverter* getChartConverter();
00344
00345 void Trace( const char* format, ...);
00346 private:
00347 virtual ::rtl::OUString implGetImplementationName() const;
00348
00349 typedef std::map< ::rtl::OUString,
00350 std::pair< ::rtl::OUString,
00351 sax_fastparser::FSHelperPtr > > XclExpXmlPathToStateMap;
00352
00353 const XclExpRoot& mrRoot;
00354 std::stack< sax_fastparser::FSHelperPtr > maStreams;
00355 XclExpXmlPathToStateMap maOpenedStreamMap;
00356 };
00357
00358 #endif
00359