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