xref: /AOO41X/main/oox/inc/oox/xls/biffcodec.hxx (revision e35081216278e1848f1c12af2e117a766f306f4b)
1*e3508121SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e3508121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e3508121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e3508121SAndrew Rist  * distributed with this work for additional information
6*e3508121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e3508121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e3508121SAndrew Rist  * "License"); you may not use this file except in compliance
9*e3508121SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*e3508121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*e3508121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e3508121SAndrew Rist  * software distributed under the License is distributed on an
15*e3508121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e3508121SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e3508121SAndrew Rist  * specific language governing permissions and limitations
18*e3508121SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*e3508121SAndrew Rist  *************************************************************/
21*e3508121SAndrew Rist 
22*e3508121SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef OOX_XLS_BIFFCODEC_HXX
25cdf0e10cSrcweir #define OOX_XLS_BIFFCODEC_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vector>
28cdf0e10cSrcweir #include <comphelper/docpasswordhelper.hxx>
29cdf0e10cSrcweir #include "oox/core/binarycodec.hxx"
30cdf0e10cSrcweir #include "oox/xls/workbookhelper.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace oox {
33cdf0e10cSrcweir namespace xls {
34cdf0e10cSrcweir 
35cdf0e10cSrcweir // ============================================================================
36cdf0e10cSrcweir 
37cdf0e10cSrcweir const sal_Int64 BIFF_RCF_BLOCKSIZE          = 1024;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir // ============================================================================
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /** Base class for BIFF stream decoders. */
42cdf0e10cSrcweir class BiffDecoderBase : public ::comphelper::IDocPasswordVerifier
43cdf0e10cSrcweir {
44cdf0e10cSrcweir public:
45cdf0e10cSrcweir     explicit            BiffDecoderBase();
46cdf0e10cSrcweir     virtual             ~BiffDecoderBase();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     /** Derived classes return a clone of the decoder for usage in new streams. */
clone()49cdf0e10cSrcweir     inline BiffDecoderBase* clone() { return implClone(); }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     /** Implementation of the ::comphelper::IDocPasswordVerifier interface. */
52cdf0e10cSrcweir     virtual ::comphelper::DocPasswordVerifierResult verifyPassword( const ::rtl::OUString& rPassword, ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& o_rEncryptionData );
53cdf0e10cSrcweir     virtual ::comphelper::DocPasswordVerifierResult verifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& o_rEncryptionData );
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /** Returns true, if the decoder has been initialized correctly. */
isValid() const56cdf0e10cSrcweir     inline bool         isValid() const { return mbValid; }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     /** Decodes nBytes bytes and writes encrypted data into the buffer pnDestData. */
59cdf0e10cSrcweir     void                decode(
60cdf0e10cSrcweir                             sal_uInt8* pnDestData,
61cdf0e10cSrcweir                             const sal_uInt8* pnSrcData,
62cdf0e10cSrcweir                             sal_Int64 nStreamPos,
63cdf0e10cSrcweir                             sal_uInt16 nBytes );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir private:
66cdf0e10cSrcweir     /** Derived classes return a clone of the decoder for usage in new streams. */
67cdf0e10cSrcweir     virtual BiffDecoderBase* implClone() = 0;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     /** Derived classes implement password verification and initialization of
70cdf0e10cSrcweir         the decoder. */
71cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > implVerifyPassword( const ::rtl::OUString& rPassword ) = 0;
72cdf0e10cSrcweir     virtual bool implVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData ) = 0;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     /** Implementation of decryption of a memory block. */
75cdf0e10cSrcweir     virtual void        implDecode(
76cdf0e10cSrcweir                             sal_uInt8* pnDestData,
77cdf0e10cSrcweir                             const sal_uInt8* pnSrcData,
78cdf0e10cSrcweir                             sal_Int64 nStreamPos,
79cdf0e10cSrcweir                             sal_uInt16 nBytes ) = 0;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir private:
82cdf0e10cSrcweir     bool                mbValid;        /// True = decoder is correctly initialized.
83cdf0e10cSrcweir };
84cdf0e10cSrcweir 
85cdf0e10cSrcweir typedef ::boost::shared_ptr< BiffDecoderBase > BiffDecoderRef;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir // ============================================================================
88cdf0e10cSrcweir 
89cdf0e10cSrcweir /** Decodes BIFF stream contents that are encoded using the old XOR algorithm. */
90cdf0e10cSrcweir class BiffDecoder_XOR : public BiffDecoderBase
91cdf0e10cSrcweir {
92cdf0e10cSrcweir public:
93cdf0e10cSrcweir     explicit            BiffDecoder_XOR( sal_uInt16 nKey, sal_uInt16 nHash );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir private:
96cdf0e10cSrcweir     /** Copy constructor for cloning. */
97cdf0e10cSrcweir                         BiffDecoder_XOR( const BiffDecoder_XOR& rDecoder );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     /** Returns a clone of the decoder for usage in new streams. */
100cdf0e10cSrcweir     virtual BiffDecoder_XOR* implClone();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     /** Implements password verification and initialization of the decoder. */
103cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > implVerifyPassword( const ::rtl::OUString& rPassword );
104cdf0e10cSrcweir     virtual bool implVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     /** Implementation of decryption of a memory block. */
108cdf0e10cSrcweir     virtual void        implDecode(
109cdf0e10cSrcweir                             sal_uInt8* pnDestData,
110cdf0e10cSrcweir                             const sal_uInt8* pnSrcData,
111cdf0e10cSrcweir                             sal_Int64 nStreamPos,
112cdf0e10cSrcweir                             sal_uInt16 nBytes );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir private:
115cdf0e10cSrcweir     ::oox::core::BinaryCodec_XOR maCodec;   /// Cipher algorithm implementation.
116cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > maEncryptionData;
117cdf0e10cSrcweir     sal_uInt16          mnKey;
118cdf0e10cSrcweir     sal_uInt16          mnHash;
119cdf0e10cSrcweir };
120cdf0e10cSrcweir 
121cdf0e10cSrcweir // ============================================================================
122cdf0e10cSrcweir 
123cdf0e10cSrcweir /** Decodes BIFF stream contents that are encoded using the RC4 algorithm. */
124cdf0e10cSrcweir class BiffDecoder_RCF : public BiffDecoderBase
125cdf0e10cSrcweir {
126cdf0e10cSrcweir public:
127cdf0e10cSrcweir     explicit            BiffDecoder_RCF(
128cdf0e10cSrcweir                             sal_uInt8 pnSalt[ 16 ],
129cdf0e10cSrcweir                             sal_uInt8 pnVerifier[ 16 ],
130cdf0e10cSrcweir                             sal_uInt8 pnVerifierHash[ 16 ] );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir private:
133cdf0e10cSrcweir     /** Copy constructor for cloning. */
134cdf0e10cSrcweir                         BiffDecoder_RCF( const BiffDecoder_RCF& rDecoder );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     /** Returns a clone of the decoder for usage in new streams. */
137cdf0e10cSrcweir     virtual BiffDecoder_RCF* implClone();
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /** Implements password verification and initialization of the decoder. */
140cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > implVerifyPassword( const ::rtl::OUString& rPassword );
141cdf0e10cSrcweir     virtual bool implVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     /** Implementation of decryption of a memory block. */
144cdf0e10cSrcweir     virtual void        implDecode(
145cdf0e10cSrcweir                             sal_uInt8* pnDestData,
146cdf0e10cSrcweir                             const sal_uInt8* pnSrcData,
147cdf0e10cSrcweir                             sal_Int64 nStreamPos,
148cdf0e10cSrcweir                             sal_uInt16 nBytes );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir private:
151cdf0e10cSrcweir     ::oox::core::BinaryCodec_RCF maCodec;   /// Cipher algorithm implementation.
152cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > maEncryptionData;
153cdf0e10cSrcweir     ::std::vector< sal_uInt8 > maSalt;
154cdf0e10cSrcweir     ::std::vector< sal_uInt8 > maVerifier;
155cdf0e10cSrcweir     ::std::vector< sal_uInt8 > maVerifierHash;
156cdf0e10cSrcweir };
157cdf0e10cSrcweir 
158cdf0e10cSrcweir // ============================================================================
159cdf0e10cSrcweir 
160cdf0e10cSrcweir /** Helper for BIFF stream codecs. Holds the used codec object. */
161cdf0e10cSrcweir class BiffCodecHelper : public WorkbookHelper
162cdf0e10cSrcweir {
163cdf0e10cSrcweir public:
164cdf0e10cSrcweir     explicit            BiffCodecHelper( const WorkbookHelper& rHelper );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     /** Implementation helper, reads the FILEPASS and returns a decoder object. */
167cdf0e10cSrcweir     static BiffDecoderRef implReadFilePass( BiffInputStream& rStrm, BiffType eBiff );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     /** Imports the FILEPASS record, asks for a password and sets a decoder at the stream. */
170cdf0e10cSrcweir     bool                importFilePass( BiffInputStream& rStrm );
171cdf0e10cSrcweir     /** Clones the contained decoder object if existing and sets it at the passed stream. */
172cdf0e10cSrcweir     void                cloneDecoder( BiffInputStream& rStrm );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir private:
175cdf0e10cSrcweir     BiffDecoderRef      mxDecoder;          /// The decoder for import filter.
176cdf0e10cSrcweir };
177cdf0e10cSrcweir 
178cdf0e10cSrcweir // ============================================================================
179cdf0e10cSrcweir 
180cdf0e10cSrcweir } // namespace xls
181cdf0e10cSrcweir } // namespace oox
182cdf0e10cSrcweir 
183cdf0e10cSrcweir #endif
184