OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_CRYPTO_SCOPED_CAPI_TYPES_H_ | 5 #ifndef BASE_CRYPTO_SCOPED_CAPI_TYPES_H_ |
6 #define BASE_CRYPTO_SCOPED_CAPI_TYPES_H_ | 6 #define BASE_CRYPTO_SCOPED_CAPI_TYPES_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <wincrypt.h> | 9 #include <wincrypt.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 // Simple destructor for the Free family of CryptoAPI functions, such as | 17 // Simple destructor for the Free family of CryptoAPI functions, such as |
18 // CryptDestroyHash, which take only a single argument to release. | 18 // CertFreeCertificateChain, which take a single argument to release and |
| 19 // return VOID |
| 20 template <typename CAPIHandle, VOID (WINAPI *Destroyer)(CAPIHandle)> |
| 21 struct CAPIDestroyerVOID { |
| 22 void operator()(CAPIHandle handle) const { |
| 23 if (handle) Destroyer(handle); |
| 24 } |
| 25 }; |
| 26 |
| 27 // Simple destructor for the Free family of CryptoAPI functions, such as |
| 28 // CryptDestroyHash, which take only a single argument to release and return |
| 29 // BOOL |
19 template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle)> | 30 template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle)> |
20 struct CAPIDestroyer { | 31 struct CAPIDestroyerBOOL { |
21 void operator()(CAPIHandle handle) const { | 32 void operator()(CAPIHandle handle) const { |
22 if (handle) { | 33 if (handle) { |
23 BOOL ok = Destroyer(handle); | 34 BOOL ok = Destroyer(handle); |
24 DCHECK(ok); | 35 DCHECK(ok); |
25 } | 36 } |
26 } | 37 } |
27 }; | 38 }; |
28 | 39 |
29 // Destructor for the Close/Release family of CryptoAPI functions, which take | 40 // Destructor for the Close/Release family of CryptoAPI functions, which take |
30 // a second DWORD parameter indicating flags to use when closing or releasing. | 41 // a second DWORD parameter indicating flags to use when closing or releasing. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 template<class CH, typename FP> inline | 117 template<class CH, typename FP> inline |
107 bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) { | 118 bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) { |
108 return h != b.get(); | 119 return h != b.get(); |
109 } | 120 } |
110 | 121 |
111 typedef ScopedCAPIHandle< | 122 typedef ScopedCAPIHandle< |
112 HCRYPTPROV, | 123 HCRYPTPROV, |
113 CAPIDestroyerWithFlags<HCRYPTPROV, | 124 CAPIDestroyerWithFlags<HCRYPTPROV, |
114 CryptReleaseContext, 0> > ScopedHCRYPTPROV; | 125 CryptReleaseContext, 0> > ScopedHCRYPTPROV; |
115 | 126 |
116 typedef ScopedCAPIHandle< | 127 typedef ScopedCAPIHandle<HCRYPTKEY, |
117 HCRYPTKEY, CAPIDestroyer<HCRYPTKEY, CryptDestroyKey> > ScopedHCRYPTKEY; | 128 CAPIDestroyerBOOL<HCRYPTKEY, |
| 129 CryptDestroyKey> > ScopedHCRYPTKEY; |
118 | 130 |
119 typedef ScopedCAPIHandle< | 131 typedef ScopedCAPIHandle<HCRYPTHASH, |
120 HCRYPTHASH, CAPIDestroyer<HCRYPTHASH, CryptDestroyHash> > ScopedHCRYPTHASH; | 132 CAPIDestroyerBOOL<HCRYPTHASH, |
| 133 CryptDestroyHash> > ScopedHCRYPTHASH; |
121 | 134 |
122 } // namespace base | 135 } // namespace base |
123 | 136 |
124 #endif // BASE_CRYPTO_SCOPED_CAPI_TYPES_H_ | 137 #endif // BASE_CRYPTO_SCOPED_CAPI_TYPES_H_ |
OLD | NEW |