io.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * \brief IO Chaining
  19. * \defgroup jose_io IO
  20. * @{
  21. */
  22. #pragma once
  23. #include "cfg.h"
  24. #include <stdbool.h>
  25. #include <stdio.h>
  26. #ifdef DOXYGEN
  27. /**
  28. * Defines a jose_io_t which calls jose_io_decref() at end of scope.
  29. *
  30. * For example:
  31. *
  32. * void foo() {
  33. * uint8_t *buf = NULL;
  34. * size_t len = 0;
  35. * jose_io_auto_t *io = jose_io_malloc(NULL, &buf, &len);
  36. * // jose_io_decref() implicitly called
  37. * }
  38. */
  39. typedef jose_io_t jose_io_auto_t;
  40. /**
  41. * The interface for chained IO.
  42. *
  43. * \see jose_io_malloc()
  44. * \see jose_io_buffer()
  45. * \see jose_io_file()
  46. * \see jose_io_multiplex()
  47. * \see jose_b64_enc_io()
  48. * \see jose_b64_dec_io()
  49. * \see jose_jws_sig_io()
  50. * \see jose_jws_ver_io()
  51. * \see jose_jwe_dec_io()
  52. * \see jose_jwe_dec_cek_io()
  53. * \see jose_jwe_enc_io()
  54. * \see jose_jwe_enc_cek_io()
  55. */
  56. typedef struct {
  57. /**
  58. * Pushes data into the IO chain.
  59. *
  60. * \param io The jose_io_t entity you are using.
  61. * \param in The input buffer.
  62. * \param len The length of the data in the input buffer.
  63. * \return Returns true if all data was consumed, otherwise false.
  64. */
  65. bool (*feed)(jose_io_t *io, const void *in, size_t len);
  66. /**
  67. * Completes the IO chain.
  68. *
  69. * Any data stored in internal buffers will be flushed.
  70. *
  71. * \param io The jose_io_t entity you are using.
  72. * \return Returns true if flushing was successful, otherwise false.
  73. */
  74. bool (*done)(jose_io_t *io);
  75. } jose_io_t;
  76. #else
  77. #define jose_io_auto_t jose_io_t __attribute__((cleanup(jose_io_auto)))
  78. typedef struct jose_io jose_io_t;
  79. struct jose_io {
  80. size_t refs;
  81. bool (*feed)(jose_io_t *io, const void *in, size_t len);
  82. bool (*done)(jose_io_t *io);
  83. void (*free)(jose_io_t *io); /* Don't call this. Use jose_io_decref(). */
  84. };
  85. #endif
  86. void
  87. jose_io_auto(jose_io_t **io);
  88. /**
  89. * Increases the reference count of an IO object.
  90. *
  91. * This function always succeeds.
  92. *
  93. * \param io The jose_io_t entity you are using.
  94. * \return The value of \p io (for convenience).
  95. */
  96. jose_io_t *
  97. jose_io_incref(jose_io_t *io);
  98. /**
  99. * Decreases the reference count of an IO object.
  100. *
  101. * When the reference count reaches zero, io->free() is called.
  102. *
  103. * \param io The jose_io_t entity you are using.
  104. */
  105. void
  106. jose_io_decref(jose_io_t *io);
  107. /**
  108. * Creates a new IO object which collects data into a dynamic buffer.
  109. *
  110. * The dynamic buffer is allocated into the \p buf pointer you provided and
  111. * the length of the buffer is stored in \p len. The pointer referenced by
  112. * \p buf must remain valid for the entire duration of the returned IO object.
  113. *
  114. * The default behavior is for the IO object to zero and free the buffer when
  115. * it is freed. This means that, by default, you own the buffer pointer but
  116. * the buffer itself is owned by the IO object. You can, however, steal the
  117. * buffer by setting the buffer pointer to NULL.
  118. *
  119. * \see jose_io_malloc_steal()
  120. * \param cfg The configuration context (optional).
  121. * \param buf A buffer pointer pointer.
  122. * \param len A pointer to the length of the buffer.
  123. * \return The new IO object or NULL on error.
  124. */
  125. jose_io_t *
  126. jose_io_malloc(jose_cfg_t *cfg, void **buf, size_t *len);
  127. /**
  128. * Steals the buffer created by the jose_io_malloc() IO object.
  129. *
  130. * This convenience function simply returns the value of \p *buf and then sets
  131. * \p *buf to NULL.
  132. *
  133. * \see jose_io_malloc()
  134. * \param buf A pointer to the buffer pointer.
  135. * \return The value of \p *buf before it is set to NULL.
  136. */
  137. void *
  138. jose_io_malloc_steal(void **buf);
  139. /**
  140. * Creates a new IO object which collects data into a static buffer.
  141. *
  142. * The size of \p buf MUST be specified in the variable pointed to by \p len.
  143. * This will be the maximum data written. However, after the function returns,
  144. * the variable pointed to by \p len will contain the current length of data in
  145. * the buffer.
  146. *
  147. * Unlike jose_io_malloc(), you own the buffer and it is not zeroed or freed
  148. * when the IO object is freed.
  149. *
  150. * \param cfg The configuration context (optional).
  151. * \param buf A buffer pointer.
  152. * \param len A pointer to the length of the buffer.
  153. * \return The new IO object or NULL on error.
  154. */
  155. jose_io_t *
  156. jose_io_buffer(jose_cfg_t *cfg, void *buf, size_t *len);
  157. /**
  158. * Creates a new IO object which writes data into a FILE.
  159. *
  160. * This function DOES NOT take ownership of the FILE. You are still responsible
  161. * for calling fclose() at the appropriate time.
  162. *
  163. * \param cfg The configuration context (optional).
  164. * \param file The output file which MUST be opened for writing or appending.
  165. * \return The new IO object or NULL on error.
  166. */
  167. jose_io_t *
  168. jose_io_file(jose_cfg_t *cfg, FILE *file);
  169. /**
  170. * Creates a new IO object which multiplexes data into multiple IO objects.
  171. *
  172. * If \p all is true, the success of all \p nexts is required. Otherwise,
  173. * all but one of the \p nexts can fail before the error is propagated upward.
  174. *
  175. * \param cfg The configuration context (optional).
  176. * \param nexts A NULL-terminated array of IO object pointers.
  177. * \param all Whether or not the success of all \p nexts is required.
  178. * \return The new IO object or NULL on error.
  179. */
  180. jose_io_t *
  181. jose_io_multiplex(jose_cfg_t *cfg, jose_io_t **nexts, bool all);
  182. /** @} */