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 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <pk11pub.h>
22 : #include "digestcontext.hxx"
23 :
24 : using namespace ::com::sun::star;
25 :
26 87 : ODigestContext::~ODigestContext()
27 : {
28 29 : if ( m_pContext )
29 : {
30 0 : PK11_DestroyContext( m_pContext, PR_TRUE );
31 0 : m_pContext = NULL;
32 : }
33 58 : }
34 :
35 29 : void SAL_CALL ODigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData )
36 : throw (lang::DisposedException, uno::RuntimeException, std::exception)
37 : {
38 29 : ::osl::MutexGuard aGuard( m_aMutex );
39 :
40 29 : if ( m_bBroken )
41 0 : throw uno::RuntimeException();
42 :
43 29 : if ( m_bDisposed )
44 0 : throw lang::DisposedException();
45 :
46 29 : if ( !m_b1KData || m_nDigested < 1024 )
47 : {
48 29 : uno::Sequence< sal_Int8 > aToDigest = aData;
49 29 : if ( m_b1KData && m_nDigested + aData.getLength() > 1024 )
50 0 : aToDigest.realloc( 1024 - m_nDigested );
51 :
52 29 : if ( PK11_DigestOp( m_pContext, reinterpret_cast< const unsigned char* >( aToDigest.getConstArray() ), aToDigest.getLength() ) != SECSuccess )
53 : {
54 0 : PK11_DestroyContext( m_pContext, PR_TRUE );
55 0 : m_pContext = NULL;
56 0 : m_bBroken = true;
57 0 : throw uno::RuntimeException();
58 : }
59 :
60 29 : m_nDigested += aToDigest.getLength();
61 29 : }
62 29 : }
63 :
64 29 : uno::Sequence< ::sal_Int8 > SAL_CALL ODigestContext::finalizeDigestAndDispose()
65 : throw (lang::DisposedException, uno::RuntimeException, std::exception)
66 : {
67 29 : ::osl::MutexGuard aGuard( m_aMutex );
68 :
69 29 : if ( m_bBroken )
70 0 : throw uno::RuntimeException();
71 :
72 29 : if ( m_bDisposed )
73 0 : throw lang::DisposedException();
74 :
75 29 : uno::Sequence< sal_Int8 > aResult( m_nDigestLength );
76 29 : unsigned int nResultLen = 0;
77 29 : if ( PK11_DigestFinal( m_pContext, reinterpret_cast< unsigned char* >( aResult.getArray() ), &nResultLen, aResult.getLength() ) != SECSuccess )
78 : {
79 0 : PK11_DestroyContext( m_pContext, PR_TRUE );
80 0 : m_pContext = NULL;
81 0 : m_bBroken = true;
82 0 : throw uno::RuntimeException();
83 : }
84 :
85 29 : PK11_DestroyContext( m_pContext, PR_TRUE );
86 29 : m_pContext = NULL;
87 29 : m_bDisposed = true;
88 :
89 29 : aResult.realloc( nResultLen );
90 29 : return aResult;
91 : }
92 :
93 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|