1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
11 #include "oox/crypto/DocumentEncryption.hxx"
13 #include <com/sun/star/io/XSeekable.hpp>
15 #include "oox/helper/binaryinputstream.hxx"
16 #include "oox/helper/binaryoutputstream.hxx"
21 using namespace css::beans
;
22 using namespace css::io
;
23 using namespace css::lang
;
24 using namespace css::uno
;
28 DocumentEncryption::DocumentEncryption(Reference
< XStream
> xDocumentStream
, oox::ole::OleStorage
& rOleStorage
, const OUString
& aPassword
) :
29 mxDocumentStream(xDocumentStream
),
30 mrOleStorage(rOleStorage
),
34 bool DocumentEncryption::encrypt()
36 Reference
< XInputStream
> xInputStream ( mxDocumentStream
->getInputStream(), UNO_SET_THROW
);
37 Reference
< XSeekable
> xSeekable( xInputStream
, UNO_QUERY
);
42 sal_uInt32 aLength
= xSeekable
->getLength();
44 if (!mrOleStorage
.isStorage())
47 Reference
< XOutputStream
> xEncryptionInfo( mrOleStorage
.openOutputStream( "EncryptionInfo" ), UNO_SET_THROW
);
48 BinaryXOutputStream
aEncryptionInfoBinaryOutputStream( xEncryptionInfo
, false );
50 mEngine
.writeEncryptionInfo(maPassword
, aEncryptionInfoBinaryOutputStream
);
52 aEncryptionInfoBinaryOutputStream
.close();
53 xEncryptionInfo
->flush();
54 xEncryptionInfo
->closeOutput();
56 Reference
< XOutputStream
> xEncryptedPackage( mrOleStorage
.openOutputStream( "EncryptedPackage" ), UNO_SET_THROW
);
57 BinaryXOutputStream
aEncryptedPackageStream( xEncryptedPackage
, false );
59 BinaryXInputStream
aDocumentInputStream( xInputStream
, false );
60 aDocumentInputStream
.seekToStart();
62 aEncryptedPackageStream
.WriteUInt32( aLength
); // size
63 aEncryptedPackageStream
.WriteUInt32( 0U ); // reserved
65 mEngine
.encrypt(aDocumentInputStream
, aEncryptedPackageStream
);
67 aEncryptedPackageStream
.close();
68 aDocumentInputStream
.close();
70 xEncryptedPackage
->flush();
71 xEncryptedPackage
->closeOutput();
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */