Line data Source code
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 : */
10 :
11 : #include "oox/crypto/DocumentEncryption.hxx"
12 :
13 : #include <com/sun/star/io/XSeekable.hpp>
14 :
15 : #include "oox/helper/binaryinputstream.hxx"
16 : #include "oox/helper/binaryoutputstream.hxx"
17 :
18 : namespace oox {
19 : namespace core {
20 :
21 : using namespace css::beans;
22 : using namespace css::io;
23 : using namespace css::lang;
24 : using namespace css::uno;
25 :
26 : using namespace std;
27 :
28 0 : DocumentEncryption::DocumentEncryption(Reference< XStream > xDocumentStream, oox::ole::OleStorage& rOleStorage, const OUString& aPassword) :
29 : mxDocumentStream(xDocumentStream),
30 : mrOleStorage(rOleStorage),
31 0 : maPassword(aPassword)
32 0 : {}
33 :
34 0 : bool DocumentEncryption::encrypt()
35 : {
36 0 : Reference< XInputStream > xInputStream ( mxDocumentStream->getInputStream(), UNO_SET_THROW );
37 0 : Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY );
38 :
39 0 : if (!xSeekable.is())
40 0 : return false;
41 :
42 0 : sal_uInt32 aLength = xSeekable->getLength();
43 :
44 0 : if (!mrOleStorage.isStorage())
45 0 : return false;
46 :
47 0 : Reference< XOutputStream > xEncryptionInfo( mrOleStorage.openOutputStream( "EncryptionInfo" ), UNO_SET_THROW );
48 0 : BinaryXOutputStream aEncryptionInfoBinaryOutputStream( xEncryptionInfo, false );
49 :
50 0 : mEngine.writeEncryptionInfo(maPassword, aEncryptionInfoBinaryOutputStream);
51 :
52 0 : aEncryptionInfoBinaryOutputStream.close();
53 0 : xEncryptionInfo->flush();
54 0 : xEncryptionInfo->closeOutput();
55 :
56 0 : Reference< XOutputStream > xEncryptedPackage( mrOleStorage.openOutputStream( "EncryptedPackage" ), UNO_SET_THROW );
57 0 : BinaryXOutputStream aEncryptedPackageStream( xEncryptedPackage, false );
58 :
59 0 : BinaryXInputStream aDocumentInputStream( xInputStream, false );
60 0 : aDocumentInputStream.seekToStart();
61 :
62 0 : aEncryptedPackageStream.writeValue<sal_uInt32>( aLength ); // size
63 0 : aEncryptedPackageStream.writeValue<sal_uInt32>( 0 ); // reserved
64 :
65 0 : mEngine.encrypt(aDocumentInputStream, aEncryptedPackageStream);
66 :
67 0 : aEncryptedPackageStream.close();
68 0 : aDocumentInputStream.close();
69 :
70 0 : xEncryptedPackage->flush();
71 0 : xEncryptedPackage->closeOutput();
72 :
73 0 : return true;
74 : }
75 :
76 : } // namespace core
77 0 : } // namespace oox
78 :
79 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|