bf_lbuf.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 1995-2023 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. #include <stdio.h>
  10. #include <errno.h>
  11. #include "bio_local.h"
  12. #include "internal/cryptlib.h"
  13. #include <openssl/evp.h>
  14. static int linebuffer_write(BIO *h, const char *buf, int num);
  15. static int linebuffer_read(BIO *h, char *buf, int size);
  16. static int linebuffer_puts(BIO *h, const char *str);
  17. static int linebuffer_gets(BIO *h, char *str, int size);
  18. static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  19. static int linebuffer_new(BIO *h);
  20. static int linebuffer_free(BIO *data);
  21. static long linebuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  22. /* A 10k maximum should be enough for most purposes */
  23. #define DEFAULT_LINEBUFFER_SIZE 1024*10
  24. /* #define DEBUG */
  25. static const BIO_METHOD methods_linebuffer = {
  26. BIO_TYPE_LINEBUFFER,
  27. "linebuffer",
  28. bwrite_conv,
  29. linebuffer_write,
  30. bread_conv,
  31. linebuffer_read,
  32. linebuffer_puts,
  33. linebuffer_gets,
  34. linebuffer_ctrl,
  35. linebuffer_new,
  36. linebuffer_free,
  37. linebuffer_callback_ctrl,
  38. };
  39. const BIO_METHOD *BIO_f_linebuffer(void)
  40. {
  41. return &methods_linebuffer;
  42. }
  43. typedef struct bio_linebuffer_ctx_struct {
  44. char *obuf; /* the output char array */
  45. int obuf_size; /* how big is the output buffer */
  46. int obuf_len; /* how many bytes are in it */
  47. } BIO_LINEBUFFER_CTX;
  48. static int linebuffer_new(BIO *bi)
  49. {
  50. BIO_LINEBUFFER_CTX *ctx;
  51. if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL)
  52. return 0;
  53. ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
  54. if (ctx->obuf == NULL) {
  55. OPENSSL_free(ctx);
  56. return 0;
  57. }
  58. ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
  59. ctx->obuf_len = 0;
  60. bi->init = 1;
  61. bi->ptr = (char *)ctx;
  62. bi->flags = 0;
  63. return 1;
  64. }
  65. static int linebuffer_free(BIO *a)
  66. {
  67. BIO_LINEBUFFER_CTX *b;
  68. if (a == NULL)
  69. return 0;
  70. b = (BIO_LINEBUFFER_CTX *)a->ptr;
  71. OPENSSL_free(b->obuf);
  72. OPENSSL_free(a->ptr);
  73. a->ptr = NULL;
  74. a->init = 0;
  75. a->flags = 0;
  76. return 1;
  77. }
  78. static int linebuffer_read(BIO *b, char *out, int outl)
  79. {
  80. int ret = 0;
  81. if (out == NULL)
  82. return 0;
  83. if (b->next_bio == NULL)
  84. return 0;
  85. ret = BIO_read(b->next_bio, out, outl);
  86. BIO_clear_retry_flags(b);
  87. BIO_copy_next_retry(b);
  88. return ret;
  89. }
  90. static int linebuffer_write(BIO *b, const char *in, int inl)
  91. {
  92. int i, num = 0, foundnl;
  93. BIO_LINEBUFFER_CTX *ctx;
  94. if ((in == NULL) || (inl <= 0))
  95. return 0;
  96. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  97. if ((ctx == NULL) || (b->next_bio == NULL))
  98. return 0;
  99. BIO_clear_retry_flags(b);
  100. do {
  101. const char *p;
  102. char c;
  103. for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) ;
  104. if (c == '\n') {
  105. p++;
  106. foundnl = 1;
  107. } else
  108. foundnl = 0;
  109. /*
  110. * If a NL was found and we already have text in the save buffer,
  111. * concatenate them and write
  112. */
  113. while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
  114. && ctx->obuf_len > 0) {
  115. int orig_olen = ctx->obuf_len;
  116. i = ctx->obuf_size - ctx->obuf_len;
  117. if (p - in > 0) {
  118. if (i >= p - in) {
  119. memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
  120. ctx->obuf_len += p - in;
  121. inl -= p - in;
  122. num += p - in;
  123. in = p;
  124. } else {
  125. memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
  126. ctx->obuf_len += i;
  127. inl -= i;
  128. in += i;
  129. num += i;
  130. }
  131. }
  132. i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  133. if (i <= 0) {
  134. ctx->obuf_len = orig_olen;
  135. BIO_copy_next_retry(b);
  136. if (i < 0)
  137. return ((num > 0) ? num : i);
  138. if (i == 0)
  139. return num;
  140. }
  141. if (i < ctx->obuf_len)
  142. memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
  143. ctx->obuf_len -= i;
  144. }
  145. /*
  146. * Now that the save buffer is emptied, let's write the input buffer
  147. * if a NL was found and there is anything to write.
  148. */
  149. if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
  150. i = BIO_write(b->next_bio, in, p - in);
  151. if (i <= 0) {
  152. BIO_copy_next_retry(b);
  153. if (i < 0)
  154. return ((num > 0) ? num : i);
  155. if (i == 0)
  156. return num;
  157. }
  158. num += i;
  159. in += i;
  160. inl -= i;
  161. }
  162. }
  163. while (foundnl && inl > 0);
  164. /*
  165. * We've written as much as we can. The rest of the input buffer, if
  166. * any, is text that doesn't and with a NL and therefore needs to be
  167. * saved for the next trip.
  168. */
  169. if (inl > 0) {
  170. memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
  171. ctx->obuf_len += inl;
  172. num += inl;
  173. }
  174. return num;
  175. }
  176. static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  177. {
  178. BIO *dbio;
  179. BIO_LINEBUFFER_CTX *ctx;
  180. long ret = 1;
  181. char *p;
  182. int r;
  183. int obs;
  184. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  185. switch (cmd) {
  186. case BIO_CTRL_RESET:
  187. ctx->obuf_len = 0;
  188. if (b->next_bio == NULL)
  189. return 0;
  190. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  191. break;
  192. case BIO_CTRL_INFO:
  193. ret = (long)ctx->obuf_len;
  194. break;
  195. case BIO_CTRL_WPENDING:
  196. ret = (long)ctx->obuf_len;
  197. if (ret == 0) {
  198. if (b->next_bio == NULL)
  199. return 0;
  200. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  201. }
  202. break;
  203. case BIO_C_SET_BUFF_SIZE:
  204. if (num > INT_MAX)
  205. return 0;
  206. obs = (int)num;
  207. p = ctx->obuf;
  208. if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
  209. p = OPENSSL_malloc((size_t)obs);
  210. if (p == NULL)
  211. return 0;
  212. }
  213. if (ctx->obuf != p) {
  214. if (ctx->obuf_len > obs) {
  215. ctx->obuf_len = obs;
  216. }
  217. memcpy(p, ctx->obuf, ctx->obuf_len);
  218. OPENSSL_free(ctx->obuf);
  219. ctx->obuf = p;
  220. ctx->obuf_size = obs;
  221. }
  222. break;
  223. case BIO_C_DO_STATE_MACHINE:
  224. if (b->next_bio == NULL)
  225. return 0;
  226. BIO_clear_retry_flags(b);
  227. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  228. BIO_copy_next_retry(b);
  229. break;
  230. case BIO_CTRL_FLUSH:
  231. if (b->next_bio == NULL)
  232. return 0;
  233. if (ctx->obuf_len <= 0) {
  234. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  235. BIO_copy_next_retry(b);
  236. break;
  237. }
  238. for (;;) {
  239. BIO_clear_retry_flags(b);
  240. if (ctx->obuf_len > 0) {
  241. r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  242. BIO_copy_next_retry(b);
  243. if (r <= 0)
  244. return (long)r;
  245. if (r < ctx->obuf_len)
  246. memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
  247. ctx->obuf_len -= r;
  248. } else {
  249. ctx->obuf_len = 0;
  250. break;
  251. }
  252. }
  253. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  254. BIO_copy_next_retry(b);
  255. break;
  256. case BIO_CTRL_DUP:
  257. dbio = (BIO *)ptr;
  258. if (BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0)
  259. ret = 0;
  260. break;
  261. default:
  262. if (b->next_bio == NULL)
  263. return 0;
  264. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  265. break;
  266. }
  267. return ret;
  268. }
  269. static long linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  270. {
  271. if (b->next_bio == NULL)
  272. return 0;
  273. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  274. }
  275. static int linebuffer_gets(BIO *b, char *buf, int size)
  276. {
  277. if (b->next_bio == NULL)
  278. return 0;
  279. return BIO_gets(b->next_bio, buf, size);
  280. }
  281. static int linebuffer_puts(BIO *b, const char *str)
  282. {
  283. return linebuffer_write(b, str, strlen(str));
  284. }