pool.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* 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. #ifndef OPENSSL_HEADER_POOL_H
  15. #define OPENSSL_HEADER_POOL_H
  16. #include <openssl/base.h>
  17. #include <openssl/stack.h>
  18. #if defined(__cplusplus)
  19. extern "C" {
  20. #endif
  21. // Buffers and buffer pools.
  22. //
  23. // |CRYPTO_BUFFER|s are simply reference-counted blobs. A |CRYPTO_BUFFER_POOL|
  24. // is an intern table for |CRYPTO_BUFFER|s. This allows for a single copy of a
  25. // given blob to be kept in memory and referenced from multiple places.
  26. DEFINE_STACK_OF(CRYPTO_BUFFER)
  27. // CRYPTO_BUFFER_POOL_new returns a freshly allocated |CRYPTO_BUFFER_POOL| or
  28. // NULL on error.
  29. OPENSSL_EXPORT CRYPTO_BUFFER_POOL* CRYPTO_BUFFER_POOL_new(void);
  30. // CRYPTO_BUFFER_POOL_free frees |pool|, which must be empty.
  31. OPENSSL_EXPORT void CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL *pool);
  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. // CRYPTO_BUFFER_alloc creates an unpooled |CRYPTO_BUFFER| of the given size and
  40. // writes the underlying data pointer to |*out_data|. It returns NULL on error.
  41. //
  42. // After calling this function, |len| bytes of contents must be written to
  43. // |out_data| before passing the returned pointer to any other BoringSSL
  44. // functions. Once initialized, the |CRYPTO_BUFFER| should be treated as
  45. // immutable.
  46. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_alloc(uint8_t **out_data,
  47. size_t len);
  48. // CRYPTO_BUFFER_new_from_CBS acts the same as |CRYPTO_BUFFER_new|.
  49. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_CBS(
  50. const CBS *cbs, CRYPTO_BUFFER_POOL *pool);
  51. // CRYPTO_BUFFER_new_from_static_data_unsafe behaves like |CRYPTO_BUFFER_new|
  52. // but does not copy |data|. |data| must be immutable and last for the lifetime
  53. // of the address space.
  54. OPENSSL_EXPORT CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_static_data_unsafe(
  55. const uint8_t *data, size_t len, CRYPTO_BUFFER_POOL *pool);
  56. // CRYPTO_BUFFER_free decrements the reference count of |buf|. If there are no
  57. // other references, or if the only remaining reference is from a pool, then
  58. // |buf| will be freed.
  59. OPENSSL_EXPORT void CRYPTO_BUFFER_free(CRYPTO_BUFFER *buf);
  60. // CRYPTO_BUFFER_up_ref increments the reference count of |buf| and returns
  61. // one.
  62. OPENSSL_EXPORT int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf);
  63. // CRYPTO_BUFFER_data returns a pointer to the data contained in |buf|.
  64. OPENSSL_EXPORT const uint8_t *CRYPTO_BUFFER_data(const CRYPTO_BUFFER *buf);
  65. // CRYPTO_BUFFER_len returns the length, in bytes, of the data contained in
  66. // |buf|.
  67. OPENSSL_EXPORT size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER *buf);
  68. // CRYPTO_BUFFER_init_CBS initialises |out| to point at the data from |buf|.
  69. OPENSSL_EXPORT void CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER *buf, CBS *out);
  70. #if defined(__cplusplus)
  71. } // extern C
  72. extern "C++" {
  73. BSSL_NAMESPACE_BEGIN
  74. BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER_POOL, CRYPTO_BUFFER_POOL_free)
  75. BORINGSSL_MAKE_DELETER(CRYPTO_BUFFER, CRYPTO_BUFFER_free)
  76. BORINGSSL_MAKE_UP_REF(CRYPTO_BUFFER, CRYPTO_BUFFER_up_ref)
  77. BSSL_NAMESPACE_END
  78. } // extern C++
  79. #endif
  80. #endif // OPENSSL_HEADER_POOL_H