bf_lbuf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright 1995-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. #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. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  53. return 0;
  54. }
  55. ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
  56. if (ctx->obuf == NULL) {
  57. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  58. OPENSSL_free(ctx);
  59. return 0;
  60. }
  61. ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
  62. ctx->obuf_len = 0;
  63. bi->init = 1;
  64. bi->ptr = (char *)ctx;
  65. bi->flags = 0;
  66. return 1;
  67. }
  68. static int linebuffer_free(BIO *a)
  69. {
  70. BIO_LINEBUFFER_CTX *b;
  71. if (a == NULL)
  72. return 0;
  73. b = (BIO_LINEBUFFER_CTX *)a->ptr;
  74. OPENSSL_free(b->obuf);
  75. OPENSSL_free(a->ptr);
  76. a->ptr = NULL;
  77. a->init = 0;
  78. a->flags = 0;
  79. return 1;
  80. }
  81. static int linebuffer_read(BIO *b, char *out, int outl)
  82. {
  83. int ret = 0;
  84. if (out == NULL)
  85. return 0;
  86. if (b->next_bio == NULL)
  87. return 0;
  88. ret = BIO_read(b->next_bio, out, outl);
  89. BIO_clear_retry_flags(b);
  90. BIO_copy_next_retry(b);
  91. return ret;
  92. }
  93. static int linebuffer_write(BIO *b, const char *in, int inl)
  94. {
  95. int i, num = 0, foundnl;
  96. BIO_LINEBUFFER_CTX *ctx;
  97. if ((in == NULL) || (inl <= 0))
  98. return 0;
  99. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  100. if ((ctx == NULL) || (b->next_bio == NULL))
  101. return 0;
  102. BIO_clear_retry_flags(b);
  103. do {
  104. const char *p;
  105. char c;
  106. for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) ;
  107. if (c == '\n') {
  108. p++;
  109. foundnl = 1;
  110. } else
  111. foundnl = 0;
  112. /*
  113. * If a NL was found and we already have text in the save buffer,
  114. * concatenate them and write
  115. */
  116. while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
  117. && ctx->obuf_len > 0) {
  118. int orig_olen = ctx->obuf_len;
  119. i = ctx->obuf_size - ctx->obuf_len;
  120. if (p - in > 0) {
  121. if (i >= p - in) {
  122. memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
  123. ctx->obuf_len += p - in;
  124. inl -= p - in;
  125. num += p - in;
  126. in = p;
  127. } else {
  128. memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
  129. ctx->obuf_len += i;
  130. inl -= i;
  131. in += i;
  132. num += i;
  133. }
  134. }
  135. i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  136. if (i <= 0) {
  137. ctx->obuf_len = orig_olen;
  138. BIO_copy_next_retry(b);
  139. if (i < 0)
  140. return ((num > 0) ? num : i);
  141. if (i == 0)
  142. return num;
  143. }
  144. if (i < ctx->obuf_len)
  145. memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
  146. ctx->obuf_len -= i;
  147. }
  148. /*
  149. * Now that the save buffer is emptied, let's write the input buffer
  150. * if a NL was found and there is anything to write.
  151. */
  152. if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
  153. i = BIO_write(b->next_bio, in, p - in);
  154. if (i <= 0) {
  155. BIO_copy_next_retry(b);
  156. if (i < 0)
  157. return ((num > 0) ? num : i);
  158. if (i == 0)
  159. return num;
  160. }
  161. num += i;
  162. in += i;
  163. inl -= i;
  164. }
  165. }
  166. while (foundnl && inl > 0);
  167. /*
  168. * We've written as much as we can. The rest of the input buffer, if
  169. * any, is text that doesn't and with a NL and therefore needs to be
  170. * saved for the next trip.
  171. */
  172. if (inl > 0) {
  173. memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
  174. ctx->obuf_len += inl;
  175. num += inl;
  176. }
  177. return num;
  178. }
  179. static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  180. {
  181. BIO *dbio;
  182. BIO_LINEBUFFER_CTX *ctx;
  183. long ret = 1;
  184. char *p;
  185. int r;
  186. int obs;
  187. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  188. switch (cmd) {
  189. case BIO_CTRL_RESET:
  190. ctx->obuf_len = 0;
  191. if (b->next_bio == NULL)
  192. return 0;
  193. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  194. break;
  195. case BIO_CTRL_INFO:
  196. ret = (long)ctx->obuf_len;
  197. break;
  198. case BIO_CTRL_WPENDING:
  199. ret = (long)ctx->obuf_len;
  200. if (ret == 0) {
  201. if (b->next_bio == NULL)
  202. return 0;
  203. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  204. }
  205. break;
  206. case BIO_C_SET_BUFF_SIZE:
  207. if (num > INT_MAX)
  208. return 0;
  209. obs = (int)num;
  210. p = ctx->obuf;
  211. if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
  212. p = OPENSSL_malloc((size_t)obs);
  213. if (p == NULL)
  214. goto malloc_error;
  215. }
  216. if (ctx->obuf != p) {
  217. if (ctx->obuf_len > obs) {
  218. ctx->obuf_len = obs;
  219. }
  220. memcpy(p, ctx->obuf, ctx->obuf_len);
  221. OPENSSL_free(ctx->obuf);
  222. ctx->obuf = p;
  223. ctx->obuf_size = obs;
  224. }
  225. break;
  226. case BIO_C_DO_STATE_MACHINE:
  227. if (b->next_bio == NULL)
  228. return 0;
  229. BIO_clear_retry_flags(b);
  230. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  231. BIO_copy_next_retry(b);
  232. break;
  233. case BIO_CTRL_FLUSH:
  234. if (b->next_bio == NULL)
  235. return 0;
  236. if (ctx->obuf_len <= 0) {
  237. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  238. break;
  239. }
  240. for (;;) {
  241. BIO_clear_retry_flags(b);
  242. if (ctx->obuf_len > 0) {
  243. r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  244. BIO_copy_next_retry(b);
  245. if (r <= 0)
  246. return (long)r;
  247. if (r < ctx->obuf_len)
  248. memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
  249. ctx->obuf_len -= r;
  250. } else {
  251. ctx->obuf_len = 0;
  252. break;
  253. }
  254. }
  255. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  256. break;
  257. case BIO_CTRL_DUP:
  258. dbio = (BIO *)ptr;
  259. if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
  260. ret = 0;
  261. break;
  262. default:
  263. if (b->next_bio == NULL)
  264. return 0;
  265. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  266. break;
  267. }
  268. return ret;
  269. malloc_error:
  270. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  271. return 0;
  272. }
  273. static long linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  274. {
  275. if (b->next_bio == NULL)
  276. return 0;
  277. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  278. }
  279. static int linebuffer_gets(BIO *b, char *buf, int size)
  280. {
  281. if (b->next_bio == NULL)
  282. return 0;
  283. return BIO_gets(b->next_bio, buf, size);
  284. }
  285. static int linebuffer_puts(BIO *b, const char *str)
  286. {
  287. return linebuffer_write(b, str, strlen(str));
  288. }