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/XInputStream.hpp>
14 #include <com/sun/star/io/XOutputStream.hpp>
15 #include <com/sun/star/io/XStream.hpp>
16 #include <com/sun/star/io/XSeekable.hpp>
18 #include <oox/helper/binaryinputstream.hxx>
19 #include <oox/helper/binaryoutputstream.hxx>
20 #include <oox/ole/olestorage.hxx>
25 using namespace css::io
;
26 using namespace css::uno
;
28 DocumentEncryption::DocumentEncryption(Reference
<XStream
> const & xDocumentStream
,
29 oox::ole::OleStorage
& rOleStorage
,
30 const OUString
& rPassword
)
31 : mxDocumentStream(xDocumentStream
)
32 , mrOleStorage(rOleStorage
)
33 , maPassword(rPassword
)
36 bool DocumentEncryption::encrypt()
38 Reference
<XInputStream
> xInputStream (mxDocumentStream
->getInputStream(), UNO_SET_THROW
);
39 Reference
<XSeekable
> xSeekable(xInputStream
, UNO_QUERY
);
44 sal_uInt32 aLength
= xSeekable
->getLength();
46 if (!mrOleStorage
.isStorage())
49 Reference
<XOutputStream
> xEncryptionInfo(mrOleStorage
.openOutputStream("EncryptionInfo"), UNO_SET_THROW
);
50 BinaryXOutputStream
aEncryptionInfoBinaryOutputStream(xEncryptionInfo
, false);
52 mEngine
.writeEncryptionInfo(maPassword
, aEncryptionInfoBinaryOutputStream
);
54 aEncryptionInfoBinaryOutputStream
.close();
55 xEncryptionInfo
->flush();
56 xEncryptionInfo
->closeOutput();
58 Reference
<XOutputStream
> xEncryptedPackage(mrOleStorage
.openOutputStream("EncryptedPackage"), UNO_SET_THROW
);
59 BinaryXOutputStream
aEncryptedPackageStream(xEncryptedPackage
, false);
61 BinaryXInputStream
aDocumentInputStream(xInputStream
, false);
62 aDocumentInputStream
.seekToStart();
64 aEncryptedPackageStream
.WriteUInt32(aLength
); // size
65 aEncryptedPackageStream
.WriteUInt32(0U); // reserved
67 mEngine
.encrypt(aDocumentInputStream
, aEncryptedPackageStream
);
69 aEncryptedPackageStream
.close();
70 aDocumentInputStream
.close();
72 xEncryptedPackage
->flush();
73 xEncryptedPackage
->closeOutput();
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */