bf_readbuff.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * This is a read only BIO filter that can be used to add BIO_tell() and
  11. * BIO_seek() support to source/sink BIO's (such as a file BIO that uses stdin).
  12. * It does this by caching ALL data read from the BIO source/sink into a
  13. * resizable memory buffer.
  14. */
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include "bio_local.h"
  18. #include "internal/cryptlib.h"
  19. #define DEFAULT_BUFFER_SIZE 4096
  20. static int readbuffer_write(BIO *h, const char *buf, int num);
  21. static int readbuffer_read(BIO *h, char *buf, int size);
  22. static int readbuffer_puts(BIO *h, const char *str);
  23. static int readbuffer_gets(BIO *h, char *str, int size);
  24. static long readbuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  25. static int readbuffer_new(BIO *h);
  26. static int readbuffer_free(BIO *data);
  27. static long readbuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  28. static const BIO_METHOD methods_readbuffer = {
  29. BIO_TYPE_BUFFER,
  30. "readbuffer",
  31. bwrite_conv,
  32. readbuffer_write,
  33. bread_conv,
  34. readbuffer_read,
  35. readbuffer_puts,
  36. readbuffer_gets,
  37. readbuffer_ctrl,
  38. readbuffer_new,
  39. readbuffer_free,
  40. readbuffer_callback_ctrl,
  41. };
  42. const BIO_METHOD *BIO_f_readbuffer(void)
  43. {
  44. return &methods_readbuffer;
  45. }
  46. static int readbuffer_new(BIO *bi)
  47. {
  48. BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  49. if (ctx == NULL)
  50. return 0;
  51. ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
  52. ctx->ibuf = OPENSSL_zalloc(DEFAULT_BUFFER_SIZE);
  53. if (ctx->ibuf == NULL) {
  54. OPENSSL_free(ctx);
  55. return 0;
  56. }
  57. bi->init = 1;
  58. bi->ptr = (char *)ctx;
  59. bi->flags = 0;
  60. return 1;
  61. }
  62. static int readbuffer_free(BIO *a)
  63. {
  64. BIO_F_BUFFER_CTX *b;
  65. if (a == NULL)
  66. return 0;
  67. b = (BIO_F_BUFFER_CTX *)a->ptr;
  68. OPENSSL_free(b->ibuf);
  69. OPENSSL_free(a->ptr);
  70. a->ptr = NULL;
  71. a->init = 0;
  72. a->flags = 0;
  73. return 1;
  74. }
  75. static int readbuffer_resize(BIO_F_BUFFER_CTX *ctx, int sz)
  76. {
  77. char *tmp;
  78. /* Figure out how many blocks are required */
  79. sz += (ctx->ibuf_off + DEFAULT_BUFFER_SIZE - 1);
  80. sz = DEFAULT_BUFFER_SIZE * (sz / DEFAULT_BUFFER_SIZE);
  81. /* Resize if the buffer is not big enough */
  82. if (sz > ctx->ibuf_size) {
  83. tmp = OPENSSL_realloc(ctx->ibuf, sz);
  84. if (tmp == NULL)
  85. return 0;
  86. ctx->ibuf = tmp;
  87. ctx->ibuf_size = sz;
  88. }
  89. return 1;
  90. }
  91. static int readbuffer_read(BIO *b, char *out, int outl)
  92. {
  93. int i, num = 0;
  94. BIO_F_BUFFER_CTX *ctx;
  95. if (out == NULL || outl == 0)
  96. return 0;
  97. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  98. if ((ctx == NULL) || (b->next_bio == NULL))
  99. return 0;
  100. BIO_clear_retry_flags(b);
  101. for (;;) {
  102. i = ctx->ibuf_len;
  103. /* If there is something in the buffer just read it. */
  104. if (i != 0) {
  105. if (i > outl)
  106. i = outl;
  107. memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
  108. ctx->ibuf_off += i;
  109. ctx->ibuf_len -= i;
  110. num += i;
  111. /* Exit if we have read the bytes required out of the buffer */
  112. if (outl == i)
  113. return num;
  114. outl -= i;
  115. out += i;
  116. }
  117. /* Only gets here if the buffer has been consumed */
  118. if (!readbuffer_resize(ctx, outl))
  119. return 0;
  120. /* Do some buffering by reading from the next bio */
  121. i = BIO_read(b->next_bio, ctx->ibuf + ctx->ibuf_off, outl);
  122. if (i <= 0) {
  123. BIO_copy_next_retry(b);
  124. if (i < 0)
  125. return ((num > 0) ? num : i);
  126. else
  127. return num; /* i == 0 */
  128. }
  129. ctx->ibuf_len = i;
  130. }
  131. }
  132. static int readbuffer_write(BIO *b, const char *in, int inl)
  133. {
  134. return 0;
  135. }
  136. static int readbuffer_puts(BIO *b, const char *str)
  137. {
  138. return 0;
  139. }
  140. static long readbuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  141. {
  142. BIO_F_BUFFER_CTX *ctx;
  143. long ret = 1, sz;
  144. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  145. switch (cmd) {
  146. case BIO_CTRL_EOF:
  147. if (ctx->ibuf_len > 0)
  148. return 0;
  149. if (b->next_bio == NULL)
  150. return 1;
  151. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  152. break;
  153. case BIO_C_FILE_SEEK:
  154. case BIO_CTRL_RESET:
  155. sz = ctx->ibuf_off + ctx->ibuf_len;
  156. /* Assume it can only seek backwards */
  157. if (num < 0 || num > sz)
  158. return 0;
  159. ctx->ibuf_off = num;
  160. ctx->ibuf_len = sz - num;
  161. break;
  162. case BIO_C_FILE_TELL:
  163. case BIO_CTRL_INFO:
  164. ret = (long)ctx->ibuf_off;
  165. break;
  166. case BIO_CTRL_PENDING:
  167. ret = (long)ctx->ibuf_len;
  168. if (ret == 0) {
  169. if (b->next_bio == NULL)
  170. return 0;
  171. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  172. }
  173. break;
  174. case BIO_CTRL_DUP:
  175. case BIO_CTRL_FLUSH:
  176. ret = 1;
  177. break;
  178. default:
  179. ret = 0;
  180. break;
  181. }
  182. return ret;
  183. }
  184. static long readbuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  185. {
  186. if (b->next_bio == NULL)
  187. return 0;
  188. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  189. }
  190. static int readbuffer_gets(BIO *b, char *buf, int size)
  191. {
  192. BIO_F_BUFFER_CTX *ctx;
  193. int num = 0, num_chars, found_newline;
  194. char *p;
  195. int i, j;
  196. if (size == 0)
  197. return 0;
  198. --size; /* the passed in size includes the terminator - so remove it here */
  199. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  200. BIO_clear_retry_flags(b);
  201. /* If data is already buffered then use this first */
  202. if (ctx->ibuf_len > 0) {
  203. p = ctx->ibuf + ctx->ibuf_off;
  204. found_newline = 0;
  205. for (num_chars = 0;
  206. (num_chars < ctx->ibuf_len) && (num_chars < size);
  207. num_chars++) {
  208. *buf++ = p[num_chars];
  209. if (p[num_chars] == '\n') {
  210. found_newline = 1;
  211. num_chars++;
  212. break;
  213. }
  214. }
  215. num += num_chars;
  216. size -= num_chars;
  217. ctx->ibuf_len -= num_chars;
  218. ctx->ibuf_off += num_chars;
  219. if (found_newline || size == 0) {
  220. *buf = '\0';
  221. return num;
  222. }
  223. }
  224. /*
  225. * If there is no buffered data left then read any remaining data from the
  226. * next bio.
  227. */
  228. /* Resize if we have to */
  229. if (!readbuffer_resize(ctx, 1 + size))
  230. return 0;
  231. /*
  232. * Read more data from the next bio using BIO_read_ex:
  233. * Note we cannot use BIO_gets() here as it does not work on a
  234. * binary stream that contains 0x00. (Since strlen() will stop at
  235. * any 0x00 not at the last read '\n' in a FILE bio).
  236. * Also note that some applications open and close the file bio
  237. * multiple times and need to read the next available block when using
  238. * stdin - so we need to READ one byte at a time!
  239. */
  240. p = ctx->ibuf + ctx->ibuf_off;
  241. for (i = 0; i < size; ++i) {
  242. j = BIO_read(b->next_bio, p, 1);
  243. if (j <= 0) {
  244. BIO_copy_next_retry(b);
  245. *buf = '\0';
  246. return num > 0 ? num : j;
  247. }
  248. *buf++ = *p;
  249. num++;
  250. ctx->ibuf_off++;
  251. if (*p == '\n')
  252. break;
  253. ++p;
  254. }
  255. *buf = '\0';
  256. return num;
  257. }