xref: /AOO41X/main/oox/inc/oox/xls/biffcodec.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef OOX_XLS_BIFFCODEC_HXX
29 #define OOX_XLS_BIFFCODEC_HXX
30 
31 #include <vector>
32 #include <comphelper/docpasswordhelper.hxx>
33 #include "oox/core/binarycodec.hxx"
34 #include "oox/xls/workbookhelper.hxx"
35 
36 namespace oox {
37 namespace xls {
38 
39 // ============================================================================
40 
41 const sal_Int64 BIFF_RCF_BLOCKSIZE          = 1024;
42 
43 // ============================================================================
44 
45 /** Base class for BIFF stream decoders. */
46 class BiffDecoderBase : public ::comphelper::IDocPasswordVerifier
47 {
48 public:
49     explicit            BiffDecoderBase();
50     virtual             ~BiffDecoderBase();
51 
52     /** Derived classes return a clone of the decoder for usage in new streams. */
53     inline BiffDecoderBase* clone() { return implClone(); }
54 
55     /** Implementation of the ::comphelper::IDocPasswordVerifier interface. */
56     virtual ::comphelper::DocPasswordVerifierResult verifyPassword( const ::rtl::OUString& rPassword, ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& o_rEncryptionData );
57     virtual ::comphelper::DocPasswordVerifierResult verifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& o_rEncryptionData );
58 
59     /** Returns true, if the decoder has been initialized correctly. */
60     inline bool         isValid() const { return mbValid; }
61 
62     /** Decodes nBytes bytes and writes encrypted data into the buffer pnDestData. */
63     void                decode(
64                             sal_uInt8* pnDestData,
65                             const sal_uInt8* pnSrcData,
66                             sal_Int64 nStreamPos,
67                             sal_uInt16 nBytes );
68 
69 private:
70     /** Derived classes return a clone of the decoder for usage in new streams. */
71     virtual BiffDecoderBase* implClone() = 0;
72 
73     /** Derived classes implement password verification and initialization of
74         the decoder. */
75     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > implVerifyPassword( const ::rtl::OUString& rPassword ) = 0;
76     virtual bool implVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData ) = 0;
77 
78     /** Implementation of decryption of a memory block. */
79     virtual void        implDecode(
80                             sal_uInt8* pnDestData,
81                             const sal_uInt8* pnSrcData,
82                             sal_Int64 nStreamPos,
83                             sal_uInt16 nBytes ) = 0;
84 
85 private:
86     bool                mbValid;        /// True = decoder is correctly initialized.
87 };
88 
89 typedef ::boost::shared_ptr< BiffDecoderBase > BiffDecoderRef;
90 
91 // ============================================================================
92 
93 /** Decodes BIFF stream contents that are encoded using the old XOR algorithm. */
94 class BiffDecoder_XOR : public BiffDecoderBase
95 {
96 public:
97     explicit            BiffDecoder_XOR( sal_uInt16 nKey, sal_uInt16 nHash );
98 
99 private:
100     /** Copy constructor for cloning. */
101                         BiffDecoder_XOR( const BiffDecoder_XOR& rDecoder );
102 
103     /** Returns a clone of the decoder for usage in new streams. */
104     virtual BiffDecoder_XOR* implClone();
105 
106     /** Implements password verification and initialization of the decoder. */
107     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > implVerifyPassword( const ::rtl::OUString& rPassword );
108     virtual bool implVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData );
109 
110 
111     /** Implementation of decryption of a memory block. */
112     virtual void        implDecode(
113                             sal_uInt8* pnDestData,
114                             const sal_uInt8* pnSrcData,
115                             sal_Int64 nStreamPos,
116                             sal_uInt16 nBytes );
117 
118 private:
119     ::oox::core::BinaryCodec_XOR maCodec;   /// Cipher algorithm implementation.
120     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > maEncryptionData;
121     sal_uInt16          mnKey;
122     sal_uInt16          mnHash;
123 };
124 
125 // ============================================================================
126 
127 /** Decodes BIFF stream contents that are encoded using the RC4 algorithm. */
128 class BiffDecoder_RCF : public BiffDecoderBase
129 {
130 public:
131     explicit            BiffDecoder_RCF(
132                             sal_uInt8 pnSalt[ 16 ],
133                             sal_uInt8 pnVerifier[ 16 ],
134                             sal_uInt8 pnVerifierHash[ 16 ] );
135 
136 private:
137     /** Copy constructor for cloning. */
138                         BiffDecoder_RCF( const BiffDecoder_RCF& rDecoder );
139 
140     /** Returns a clone of the decoder for usage in new streams. */
141     virtual BiffDecoder_RCF* implClone();
142 
143     /** Implements password verification and initialization of the decoder. */
144     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > implVerifyPassword( const ::rtl::OUString& rPassword );
145     virtual bool implVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData );
146 
147     /** Implementation of decryption of a memory block. */
148     virtual void        implDecode(
149                             sal_uInt8* pnDestData,
150                             const sal_uInt8* pnSrcData,
151                             sal_Int64 nStreamPos,
152                             sal_uInt16 nBytes );
153 
154 private:
155     ::oox::core::BinaryCodec_RCF maCodec;   /// Cipher algorithm implementation.
156     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > maEncryptionData;
157     ::std::vector< sal_uInt8 > maSalt;
158     ::std::vector< sal_uInt8 > maVerifier;
159     ::std::vector< sal_uInt8 > maVerifierHash;
160 };
161 
162 // ============================================================================
163 
164 /** Helper for BIFF stream codecs. Holds the used codec object. */
165 class BiffCodecHelper : public WorkbookHelper
166 {
167 public:
168     explicit            BiffCodecHelper( const WorkbookHelper& rHelper );
169 
170     /** Implementation helper, reads the FILEPASS and returns a decoder object. */
171     static BiffDecoderRef implReadFilePass( BiffInputStream& rStrm, BiffType eBiff );
172 
173     /** Imports the FILEPASS record, asks for a password and sets a decoder at the stream. */
174     bool                importFilePass( BiffInputStream& rStrm );
175     /** Clones the contained decoder object if existing and sets it at the passed stream. */
176     void                cloneDecoder( BiffInputStream& rStrm );
177 
178 private:
179     BiffDecoderRef      mxDecoder;          /// The decoder for import filter.
180 };
181 
182 // ============================================================================
183 
184 } // namespace xls
185 } // namespace oox
186 
187 #endif
188