| 1 |
// Copyright (c) 2016, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_POOL_H |
| 5 |
#define OPENSSL_HEADER_POOL_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#include <openssl/stack.h> |
| 10 |
|
| 11 |
#if defined(__cplusplus) |
| 12 |
extern "C" { |
| 13 |
#endif |
| 14 |
|
| 15 |
|
| 16 |
// Buffers and buffer pools. |
| 17 |
// |
| 18 |
// |CRYPTO_BUFFER|s are simply reference-counted blobs. A |CRYPTO_BUFFER_POOL| |
| 19 |
// is an intern table for |CRYPTO_BUFFER|s. This allows for a single copy of a |
| 20 |
// given blob to be kept in memory and referenced from multiple places. |
| 21 |
|
| 22 |
|
| 23 |
DEFINE_STACK_OF(CRYPTO_BUFFER) |
| 24 |
|
| 25 |
// CRYPTO_BUFFER_POOL_new returns a freshly allocated |CRYPTO_BUFFER_POOL| or |
| 26 |
// NULL on error. |
| 27 |
OPENSSL_EXPORT CRYPTO_BUFFER_POOL* CRYPTO_BUFFER_POOL_new(void); |
| 28 |
|
| 29 |
// CRYPTO_BUFFER_POOL_free frees |pool|, which must be empty. |
| 30 |
OPENSSL_EXPORT void CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL *pool); |
| 31 |
|
| 32 |
// CRYPTO_BUFFER_new returns a |CRYPTO_BUFFER| containing a copy of |data|, or |
| 33 |
// else NULL on error. If |pool| is not NULL then the returned value may be a |
| 34 |
// reference to a previously existing |CRYPTO_BUFFER| that contained the same |
| 35 |
// data. Otherwise, the returned, fresh |CRYPTO_BUFFER| will be added to the |
| 36 |
// pool. |
| 37 |
OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new(const uint8_t *data, size_t len, |
| 38 |
CRYPTO_BUFFER_POOL *pool); |
| 39 |
|
| 40 |
// CRYPTO_BUFFER_alloc creates an unpooled |CRYPTO_BUFFER| of the given size and |
| 41 |
// writes the underlying data pointer to |*out_data|. It returns NULL on error. |
| 42 |
// |
| 43 |
// After calling this function, |len| bytes of contents must be written to |
| 44 |
// |out_data| before passing the returned pointer to any other BoringSSL |
| 45 |
// functions. Once initialized, the |CRYPTO_BUFFER| should be treated as |
| 46 |
// immutable. |
| 47 |
OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_alloc(uint8_t **out_data, |
| 48 |
size_t len); |
| 49 |
|
| 50 |
// CRYPTO_BUFFER_new_from_CBS acts the same as |CRYPTO_BUFFER_new|. |
| 51 |
OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_CBS( |
| 52 |
const CBS *cbs, CRYPTO_BUFFER_POOL *pool); |
| 53 |
|
| 54 |
// CRYPTO_BUFFER_new_from_static_data_unsafe behaves like |CRYPTO_BUFFER_new| |
| 55 |
// but does not copy |data|. |data| must be immutable and last for the lifetime |
| 56 |
// of the address space. |
| 57 |
OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_static_data_unsafe( |
| 58 |
const uint8_t *data, size_t len, CRYPTO_BUFFER_POOL *pool); |
| 59 |
|
| 60 |
// CRYPTO_BUFFER_free decrements the reference count of |buf|. If there are no |
| 61 |
// other references, or if the only remaining reference is from a pool, then |
| 62 |
// |buf| will be freed. |
| 63 |
OPENSSL_EXPORT void CRYPTO_BUFFER_free(CRYPTO_BUFFER *buf); |
| 64 |
|
| 65 |
// CRYPTO_BUFFER_up_ref increments the reference count of |buf| and returns |
| 66 |
// one. |
| 67 |
OPENSSL_EXPORT int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf); |
| 68 |
|
| 69 |
// CRYPTO_BUFFER_data returns a pointer to the data contained in |buf|. |
| 70 |
OPENSSL_EXPORT const uint8_t *CRYPTO_BUFFER_data(const CRYPTO_BUFFER *buf); |
| 71 |
|
| 72 |
// CRYPTO_BUFFER_len returns the length, in bytes, of the data contained in |
| 73 |
// |buf|. |
| 74 |
OPENSSL_EXPORT size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER *buf); |
| 75 |
|
| 76 |
// CRYPTO_BUFFER_init_CBS initialises |out| to point at the data from |buf|. |
| 77 |
OPENSSL_EXPORT void CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER *buf, CBS *out); |
| 78 |
|
| 79 |
|
| 80 |
#if defined(__cplusplus) |
| 81 |
} // extern C |
| 82 |
|
| 83 |
extern "C++" { |
| 84 |
|
| 85 |
BSSL_NAMESPACE_BEGIN |
| 86 |
|
| 87 |
BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_free) |
| 88 |
BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER, CRYPTO_BUFFER_free) |
| 89 |
BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER, CRYPTO_BUFFER_up_ref) |
| 90 |
|
| 91 |
BSSL_NAMESPACE_END |
| 92 |
|
| 93 |
} // extern C++ |
| 94 |
|
| 95 |
#endif |
| 96 |
|
| 97 |
#endif // OPENSSL_HEADER_POOL_H |