bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / crypto / DocumentEncryption.cxx
blob8cb62c23f006c7d98b0e4290ac3728299186ac38
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
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>
22 namespace oox {
23 namespace core {
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);
41 if (!xSeekable.is())
42 return false;
44 sal_uInt32 aLength = xSeekable->getLength(); // check length of the stream
45 xSeekable->seek(0); // seek to begin of the document stream
47 if (!mrOleStorage.isStorage())
48 return false;
50 mEngine.setupEncryption(maPassword);
52 Reference<XOutputStream> xOutputStream(mrOleStorage.openOutputStream("EncryptedPackage"), UNO_SET_THROW);
54 mEngine.encrypt(xInputStream, xOutputStream, aLength);
56 xOutputStream->flush();
57 xOutputStream->closeOutput();
59 Reference<XOutputStream> xEncryptionInfo(mrOleStorage.openOutputStream("EncryptionInfo"), UNO_SET_THROW);
60 BinaryXOutputStream aEncryptionInfoBinaryOutputStream(xEncryptionInfo, false);
62 mEngine.writeEncryptionInfo(aEncryptionInfoBinaryOutputStream);
64 aEncryptionInfoBinaryOutputStream.close();
65 xEncryptionInfo->flush();
66 xEncryptionInfo->closeOutput();
68 return true;
71 } // namespace core
72 } // namespace oox
74 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */