tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / package / source / zipapi / blowfishcontext.cxx
blobf848f717c54fe6e4ce9d30dae15471924f992bae
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 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <rtl/cipher.h>
24 #include <rtl/ref.hxx>
26 #include "blowfishcontext.hxx"
28 using namespace ::com::sun::star;
30 // static
31 uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt )
33 ::rtl::Reference< BlowfishCFB8CipherContext > xResult = new BlowfishCFB8CipherContext();
34 xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream );
35 if ( !xResult->m_pCipher )
36 throw uno::RuntimeException(u"Can not create cipher!"_ustr );
38 if ( rtl_Cipher_E_None != rtl_cipher_init(
39 xResult->m_pCipher,
40 bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode,
41 reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ),
42 aDerivedKey.getLength(),
43 reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ),
44 aInitVector.getLength() ) )
46 throw uno::RuntimeException(u"Can not initialize cipher!"_ustr );
49 xResult->m_bEncrypt = bEncrypt;
51 return xResult;
54 BlowfishCFB8CipherContext::~BlowfishCFB8CipherContext()
56 if ( m_pCipher )
58 rtl_cipher_destroy ( m_pCipher );
59 m_pCipher = nullptr;
63 uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData )
65 std::scoped_lock aGuard( m_aMutex );
66 if ( !m_pCipher )
67 throw lang::DisposedException();
69 uno::Sequence< sal_Int8 > aResult( aData.getLength() );
70 rtlCipherError nError = rtl_Cipher_E_None;
72 if ( m_bEncrypt )
74 nError = rtl_cipher_encode( m_pCipher,
75 aData.getConstArray(),
76 aData.getLength(),
77 reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
78 aResult.getLength() );
80 else
82 nError = rtl_cipher_decode( m_pCipher,
83 aData.getConstArray(),
84 aData.getLength(),
85 reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
86 aResult.getLength() );
89 if ( rtl_Cipher_E_None != nError )
91 throw uno::RuntimeException(u"Can not decrypt/encrypt with cipher!"_ustr );
94 return aResult;
97 uno::Sequence< ::sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::finalizeCipherContextAndDispose()
99 std::scoped_lock aGuard( m_aMutex );
100 if ( !m_pCipher )
101 throw lang::DisposedException();
103 rtl_cipher_destroy ( m_pCipher );
104 m_pCipher = nullptr;
106 return uno::Sequence< sal_Int8 >();
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */