blob: c61a4babfffb9666c5012a8b636b48a3331deed1 [file] [log] [blame]
Adam Langley9ef99d52016-10-25 17:33:49 -07001/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#ifndef OPENSSL_HEADER_POOL_H
16#define OPENSSL_HEADER_POOL_H
17
18#include <openssl/base.h>
19
David Benjamin01f8a8c2017-04-15 18:12:55 -040020#include <openssl/stack.h>
21
Adam Langley9ef99d52016-10-25 17:33:49 -070022#if defined(__cplusplus)
23extern "C" {
24#endif
25
26
David Benjamin4512b792017-08-18 19:21:50 -040027// Buffers and buffer pools.
28//
29// |CRYPTO_BUFFER|s are simply reference-counted blobs. A |CRYPTO_BUFFER_POOL|
30// is an intern table for |CRYPTO_BUFFER|s. This allows for a single copy of a
31// given blob to be kept in memory and referenced from multiple places.
Adam Langley9ef99d52016-10-25 17:33:49 -070032
33
David Benjamin01f8a8c2017-04-15 18:12:55 -040034DEFINE_STACK_OF(CRYPTO_BUFFER)
35
David Benjamin4512b792017-08-18 19:21:50 -040036// CRYPTO_BUFFER_POOL_new returns a freshly allocated |CRYPTO_BUFFER_POOL| or
37// NULL on error.
Adam Langley9ef99d52016-10-25 17:33:49 -070038OPENSSL_EXPORT CRYPTO_BUFFER_POOL* CRYPTO_BUFFER_POOL_new(void);
39
David Benjamin4512b792017-08-18 19:21:50 -040040// CRYPTO_BUFFER_POOL_free frees |pool|, which must be empty.
Adam Langley9ef99d52016-10-25 17:33:49 -070041OPENSSL_EXPORT void CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL *pool);
42
David Benjamin4512b792017-08-18 19:21:50 -040043// CRYPTO_BUFFER_new returns a |CRYPTO_BUFFER| containing a copy of |data|, or
44// else NULL on error. If |pool| is not NULL then the returned value may be a
45// reference to a previously existing |CRYPTO_BUFFER| that contained the same
46// data. Otherwise, the returned, fresh |CRYPTO_BUFFER| will be added to the
47// pool.
Adam Langley9ef99d52016-10-25 17:33:49 -070048OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new(const uint8_t *data, size_t len,
49 CRYPTO_BUFFER_POOL *pool);
50
Adam Langley0080d832018-06-07 16:39:49 -070051// CRYPTO_BUFFER_alloc creates an unpooled |CRYPTO_BUFFER| of the given size and
52// writes the underlying data pointer to |*out_data|. It returns NULL on error.
53//
54// After calling this function, |len| bytes of contents must be written to
55// |out_data| before passing the returned pointer to any other BoringSSL
56// functions. Once initialized, the |CRYPTO_BUFFER| should be treated as
57// immutable.
58OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_alloc(uint8_t **out_data,
59 size_t len);
60
David Benjamin4512b792017-08-18 19:21:50 -040061// CRYPTO_BUFFER_new_from_CBS acts the same as |CRYPTO_BUFFER_new|.
Adam Langley9ef99d52016-10-25 17:33:49 -070062OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_CBS(
David Benjamin7cac8fa2021-10-20 17:17:57 -040063 const CBS *cbs, CRYPTO_BUFFER_POOL *pool);
64
65// CRYPTO_BUFFER_new_from_static_data_unsafe behaves like |CRYPTO_BUFFER_new|
66// but does not copy |data|. |data| must be immutable and last for the lifetime
67// of the address space.
68OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_static_data_unsafe(
69 const uint8_t *data, size_t len, CRYPTO_BUFFER_POOL *pool);
Adam Langley9ef99d52016-10-25 17:33:49 -070070
David Benjamin4512b792017-08-18 19:21:50 -040071// CRYPTO_BUFFER_free decrements the reference count of |buf|. If there are no
72// other references, or if the only remaining reference is from a pool, then
73// |buf| will be freed.
Adam Langley9ef99d52016-10-25 17:33:49 -070074OPENSSL_EXPORT void CRYPTO_BUFFER_free(CRYPTO_BUFFER *buf);
75
David Benjamin4512b792017-08-18 19:21:50 -040076// CRYPTO_BUFFER_up_ref increments the reference count of |buf| and returns
77// one.
Adam Langley9ef99d52016-10-25 17:33:49 -070078OPENSSL_EXPORT int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf);
79
David Benjamin4512b792017-08-18 19:21:50 -040080// CRYPTO_BUFFER_data returns a pointer to the data contained in |buf|.
Adam Langley9ef99d52016-10-25 17:33:49 -070081OPENSSL_EXPORT const uint8_t *CRYPTO_BUFFER_data(const CRYPTO_BUFFER *buf);
82
David Benjamin4512b792017-08-18 19:21:50 -040083// CRYPTO_BUFFER_len returns the length, in bytes, of the data contained in
84// |buf|.
David Benjamind547f552016-10-28 08:55:59 -040085OPENSSL_EXPORT size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER *buf);
Adam Langley9ef99d52016-10-25 17:33:49 -070086
David Benjamin4512b792017-08-18 19:21:50 -040087// CRYPTO_BUFFER_init_CBS initialises |out| to point at the data from |buf|.
Adam Langley9ef99d52016-10-25 17:33:49 -070088OPENSSL_EXPORT void CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER *buf, CBS *out);
89
90
91#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -040092} // extern C
Adam Langley9ef99d52016-10-25 17:33:49 -070093
94extern "C++" {
95
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070096BSSL_NAMESPACE_BEGIN
Adam Langley9ef99d52016-10-25 17:33:49 -070097
98BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_free)
99BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER, CRYPTO_BUFFER_free)
David Benjamin2908dd12018-06-29 17:46:42 -0400100BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER, CRYPTO_BUFFER_up_ref)
Adam Langley9ef99d52016-10-25 17:33:49 -0700101
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700102BSSL_NAMESPACE_END
Adam Langley9ef99d52016-10-25 17:33:49 -0700103
David Benjamin4512b792017-08-18 19:21:50 -0400104} // extern C++
Adam Langley9ef99d52016-10-25 17:33:49 -0700105
106#endif
107
108#endif // OPENSSL_HEADER_POOL_H