blob: 0e4bdd5c4d7a1cd77780228b68f1d76234ae4ebc [file] [log] [blame]
Steven Valdez909b19f2016-11-21 15:35:44 -05001/* 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
Robert Sloan8ff03552017-06-14 12:40:58 -070020#include <openssl/stack.h>
21
Steven Valdez909b19f2016-11-21 15:35:44 -050022#if defined(__cplusplus)
23extern "C" {
24#endif
25
26
Robert Sloan8f860b12017-08-28 07:37:06 -070027// 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.
Steven Valdez909b19f2016-11-21 15:35:44 -050032
33
Robert Sloan8ff03552017-06-14 12:40:58 -070034DEFINE_STACK_OF(CRYPTO_BUFFER)
35
Robert Sloan8f860b12017-08-28 07:37:06 -070036// CRYPTO_BUFFER_POOL_new returns a freshly allocated |CRYPTO_BUFFER_POOL| or
37// NULL on error.
Steven Valdez909b19f2016-11-21 15:35:44 -050038OPENSSL_EXPORT CRYPTO_BUFFER_POOL* CRYPTO_BUFFER_POOL_new(void);
39
Robert Sloan8f860b12017-08-28 07:37:06 -070040// CRYPTO_BUFFER_POOL_free frees |pool|, which must be empty.
Steven Valdez909b19f2016-11-21 15:35:44 -050041OPENSSL_EXPORT void CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL *pool);
42
Robert Sloan8f860b12017-08-28 07:37:06 -070043// 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.
Steven Valdez909b19f2016-11-21 15:35:44 -050048OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new(const uint8_t *data, size_t len,
49 CRYPTO_BUFFER_POOL *pool);
50
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010051// 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
Robert Sloan8f860b12017-08-28 07:37:06 -070061// CRYPTO_BUFFER_new_from_CBS acts the same as |CRYPTO_BUFFER_new|.
Steven Valdez909b19f2016-11-21 15:35:44 -050062OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_CBS(
63 CBS *cbs, CRYPTO_BUFFER_POOL *pool);
64
Robert Sloan8f860b12017-08-28 07:37:06 -070065// CRYPTO_BUFFER_free decrements the reference count of |buf|. If there are no
66// other references, or if the only remaining reference is from a pool, then
67// |buf| will be freed.
Steven Valdez909b19f2016-11-21 15:35:44 -050068OPENSSL_EXPORT void CRYPTO_BUFFER_free(CRYPTO_BUFFER *buf);
69
Robert Sloan8f860b12017-08-28 07:37:06 -070070// CRYPTO_BUFFER_up_ref increments the reference count of |buf| and returns
71// one.
Steven Valdez909b19f2016-11-21 15:35:44 -050072OPENSSL_EXPORT int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf);
73
Robert Sloan8f860b12017-08-28 07:37:06 -070074// CRYPTO_BUFFER_data returns a pointer to the data contained in |buf|.
Steven Valdez909b19f2016-11-21 15:35:44 -050075OPENSSL_EXPORT const uint8_t *CRYPTO_BUFFER_data(const CRYPTO_BUFFER *buf);
76
Robert Sloan8f860b12017-08-28 07:37:06 -070077// CRYPTO_BUFFER_len returns the length, in bytes, of the data contained in
78// |buf|.
Steven Valdez909b19f2016-11-21 15:35:44 -050079OPENSSL_EXPORT size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER *buf);
80
Robert Sloan8f860b12017-08-28 07:37:06 -070081// CRYPTO_BUFFER_init_CBS initialises |out| to point at the data from |buf|.
Steven Valdez909b19f2016-11-21 15:35:44 -050082OPENSSL_EXPORT void CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER *buf, CBS *out);
83
84
85#if defined(__cplusplus)
Robert Sloan8f860b12017-08-28 07:37:06 -070086} // extern C
Steven Valdez909b19f2016-11-21 15:35:44 -050087
88extern "C++" {
89
Robert Sloan726e9d12018-09-11 11:45:04 -070090BSSL_NAMESPACE_BEGIN
Steven Valdez909b19f2016-11-21 15:35:44 -050091
92BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_free)
93BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER, CRYPTO_BUFFER_free)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010094BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER, CRYPTO_BUFFER_up_ref)
Steven Valdez909b19f2016-11-21 15:35:44 -050095
Robert Sloan726e9d12018-09-11 11:45:04 -070096BSSL_NAMESPACE_END
Steven Valdez909b19f2016-11-21 15:35:44 -050097
Robert Sloan8f860b12017-08-28 07:37:06 -070098} // extern C++
Steven Valdez909b19f2016-11-21 15:35:44 -050099
100#endif
101
102#endif // OPENSSL_HEADER_POOL_H