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 0 : ODigestContext::~ODigestContext()
27 : {
28 0 : if ( m_pContext )
29 : {
30 0 : PK11_DestroyContext( m_pContext, PR_TRUE );
31 0 : m_pContext = NULL;
32 : }
33 0 : }
34 :
35 0 : void SAL_CALL ODigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData )
36 : throw (lang::DisposedException, uno::RuntimeException, std::exception)
37 : {
38 0 : ::osl::MutexGuard aGuard( m_aMutex );
39 :
40 0 : if ( m_bBroken )
41 0 : throw uno::RuntimeException();
42 :
43 0 : if ( m_bDisposed )
44 0 : throw lang::DisposedException();
45 :
46 0 : if ( !m_b1KData || m_nDigested < 1024 )
47 : {
48 0 : uno::Sequence< sal_Int8 > aToDigest = aData;
49 0 : if ( m_b1KData && m_nDigested + aData.getLength() > 1024 )
50 0 : aToDigest.realloc( 1024 - m_nDigested );
51 :
52 0 : 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 0 : m_nDigested += aToDigest.getLength();
61 0 : }
62 0 : }
63 :
64 0 : uno::Sequence< ::sal_Int8 > SAL_CALL ODigestContext::finalizeDigestAndDispose()
65 : throw (lang::DisposedException, uno::RuntimeException, std::exception)
66 : {
67 0 : ::osl::MutexGuard aGuard( m_aMutex );
68 :
69 0 : if ( m_bBroken )
70 0 : throw uno::RuntimeException();
71 :
72 0 : if ( m_bDisposed )
73 0 : throw lang::DisposedException();
74 :
75 0 : uno::Sequence< sal_Int8 > aResult( m_nDigestLength );
76 0 : unsigned int nResultLen = 0;
77 0 : 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 0 : PK11_DestroyContext( m_pContext, PR_TRUE );
86 0 : m_pContext = NULL;
87 0 : m_bDisposed = true;
88 :
89 0 : aResult.realloc( nResultLen );
90 0 : return aResult;
91 : }
92 :
93 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|