OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CRYPTO_OPENSSL_UTIL_H_ | 5 #ifndef CRYPTO_OPENSSL_UTIL_H_ |
6 #define CRYPTO_OPENSSL_UTIL_H_ | 6 #define CRYPTO_OPENSSL_UTIL_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "crypto/crypto_export.h" | 10 #include "crypto/crypto_export.h" |
11 | 11 |
12 namespace crypto { | 12 namespace crypto { |
13 | 13 |
14 // A helper class that takes care of destroying OpenSSL objects when they go out | |
15 // of scope. | |
16 template <typename T, void (*destructor)(T*)> | |
17 class ScopedOpenSSL { | |
18 public: | |
19 ScopedOpenSSL() : ptr_(NULL) { } | |
20 explicit ScopedOpenSSL(T* ptr) : ptr_(ptr) { } | |
21 ~ScopedOpenSSL() { | |
22 reset(NULL); | |
23 } | |
24 | |
25 T* get() const { return ptr_; } | |
26 T* release() { | |
27 T* ptr = ptr_; | |
28 ptr_ = NULL; | |
29 return ptr; | |
30 } | |
31 void reset(T* ptr) { | |
32 if (ptr != ptr_) { | |
33 if (ptr_) (*destructor)(ptr_); | |
34 ptr_ = ptr; | |
35 } | |
36 } | |
37 | |
38 private: | |
39 T* ptr_; | |
40 | |
41 DISALLOW_COPY_AND_ASSIGN(ScopedOpenSSL); | |
42 }; | |
43 | |
44 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's | 14 // Provides a buffer of at least MIN_SIZE bytes, for use when calling OpenSSL's |
45 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those | 15 // SHA256, HMAC, etc functions, adapting the buffer sizing rules to meet those |
46 // of the our base wrapper APIs. | 16 // of the our base wrapper APIs. |
47 // This allows the library to write directly to the caller's buffer if it is of | 17 // This allows the library to write directly to the caller's buffer if it is of |
48 // sufficient size, but if not it will write to temporary |min_sized_buffer_| | 18 // sufficient size, but if not it will write to temporary |min_sized_buffer_| |
49 // of required size and then its content is automatically copied out on | 19 // of required size and then its content is automatically copied out on |
50 // destruction, with truncation as appropriate. | 20 // destruction, with truncation as appropriate. |
51 template<int MIN_SIZE> | 21 template<int MIN_SIZE> |
52 class ScopedOpenSSLSafeSizeBuffer { | 22 class ScopedOpenSSLSafeSizeBuffer { |
53 public: | 23 public: |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 81 |
112 private: | 82 private: |
113 const tracked_objects::Location location_; | 83 const tracked_objects::Location location_; |
114 | 84 |
115 DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer); | 85 DISALLOW_IMPLICIT_CONSTRUCTORS(OpenSSLErrStackTracer); |
116 }; | 86 }; |
117 | 87 |
118 } // namespace crypto | 88 } // namespace crypto |
119 | 89 |
120 #endif // CRYPTO_OPENSSL_UTIL_H_ | 90 #endif // CRYPTO_OPENSSL_UTIL_H_ |
OLD | NEW |