2
0

bf_lbuf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright 1995-2020 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. /* TODO: Convert to new style write function */
  29. bwrite_conv,
  30. linebuffer_write,
  31. /* TODO: Convert to new style read function */
  32. bread_conv,
  33. linebuffer_read,
  34. linebuffer_puts,
  35. linebuffer_gets,
  36. linebuffer_ctrl,
  37. linebuffer_new,
  38. linebuffer_free,
  39. linebuffer_callback_ctrl,
  40. };
  41. const BIO_METHOD *BIO_f_linebuffer(void)
  42. {
  43. return &methods_linebuffer;
  44. }
  45. typedef struct bio_linebuffer_ctx_struct {
  46. char *obuf; /* the output char array */
  47. int obuf_size; /* how big is the output buffer */
  48. int obuf_len; /* how many bytes are in it */
  49. } BIO_LINEBUFFER_CTX;
  50. static int linebuffer_new(BIO *bi)
  51. {
  52. BIO_LINEBUFFER_CTX *ctx;
  53. if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) {
  54. BIOerr(BIO_F_LINEBUFFER_NEW, ERR_R_MALLOC_FAILURE);
  55. return 0;
  56. }
  57. ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
  58. if (ctx->obuf == NULL) {
  59. BIOerr(BIO_F_LINEBUFFER_NEW, ERR_R_MALLOC_FAILURE);
  60. OPENSSL_free(ctx);
  61. return 0;
  62. }
  63. ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
  64. ctx->obuf_len = 0;
  65. bi->init = 1;
  66. bi->ptr = (char *)ctx;
  67. bi->flags = 0;
  68. return 1;
  69. }
  70. static int linebuffer_free(BIO *a)
  71. {
  72. BIO_LINEBUFFER_CTX *b;
  73. if (a == NULL)
  74. return 0;
  75. b = (BIO_LINEBUFFER_CTX *)a->ptr;
  76. OPENSSL_free(b->obuf);
  77. OPENSSL_free(a->ptr);
  78. a->ptr = NULL;
  79. a->init = 0;
  80. a->flags = 0;
  81. return 1;
  82. }
  83. static int linebuffer_read(BIO *b, char *out, int outl)
  84. {
  85. int ret = 0;
  86. if (out == NULL)
  87. return 0;
  88. if (b->next_bio == NULL)
  89. return 0;
  90. ret = BIO_read(b->next_bio, out, outl);
  91. BIO_clear_retry_flags(b);
  92. BIO_copy_next_retry(b);
  93. return ret;
  94. }
  95. static int linebuffer_write(BIO *b, const char *in, int inl)
  96. {
  97. int i, num = 0, foundnl;
  98. BIO_LINEBUFFER_CTX *ctx;
  99. if ((in == NULL) || (inl <= 0))
  100. return 0;
  101. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  102. if ((ctx == NULL) || (b->next_bio == NULL))
  103. return 0;
  104. BIO_clear_retry_flags(b);
  105. do {
  106. const char *p;
  107. char c;
  108. for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) ;
  109. if (c == '\n') {
  110. p++;
  111. foundnl = 1;
  112. } else
  113. foundnl = 0;
  114. /*
  115. * If a NL was found and we already have text in the save buffer,
  116. * concatenate them and write
  117. */
  118. while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
  119. && ctx->obuf_len > 0) {
  120. int orig_olen = ctx->obuf_len;
  121. i = ctx->obuf_size - ctx->obuf_len;
  122. if (p - in > 0) {
  123. if (i >= p - in) {
  124. memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
  125. ctx->obuf_len += p - in;
  126. inl -= p - in;
  127. num += p - in;
  128. in = p;
  129. } else {
  130. memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
  131. ctx->obuf_len += i;
  132. inl -= i;
  133. in += i;
  134. num += i;
  135. }
  136. }
  137. i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  138. if (i <= 0) {
  139. ctx->obuf_len = orig_olen;
  140. BIO_copy_next_retry(b);
  141. if (i < 0)
  142. return ((num > 0) ? num : i);
  143. if (i == 0)
  144. return num;
  145. }
  146. if (i < ctx->obuf_len)
  147. memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
  148. ctx->obuf_len -= i;
  149. }
  150. /*
  151. * Now that the save buffer is emptied, let's write the input buffer
  152. * if a NL was found and there is anything to write.
  153. */
  154. if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
  155. i = BIO_write(b->next_bio, in, p - in);
  156. if (i <= 0) {
  157. BIO_copy_next_retry(b);
  158. if (i < 0)
  159. return ((num > 0) ? num : i);
  160. if (i == 0)
  161. return num;
  162. }
  163. num += i;
  164. in += i;
  165. inl -= i;
  166. }
  167. }
  168. while (foundnl && inl > 0);
  169. /*
  170. * We've written as much as we can. The rest of the input buffer, if
  171. * any, is text that doesn't and with a NL and therefore needs to be
  172. * saved for the next trip.
  173. */
  174. if (inl > 0) {
  175. memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
  176. ctx->obuf_len += inl;
  177. num += inl;
  178. }
  179. return num;
  180. }
  181. static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  182. {
  183. BIO *dbio;
  184. BIO_LINEBUFFER_CTX *ctx;
  185. long ret = 1;
  186. char *p;
  187. int r;
  188. int obs;
  189. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  190. switch (cmd) {
  191. case BIO_CTRL_RESET:
  192. ctx->obuf_len = 0;
  193. if (b->next_bio == NULL)
  194. return 0;
  195. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  196. break;
  197. case BIO_CTRL_INFO:
  198. ret = (long)ctx->obuf_len;
  199. break;
  200. case BIO_CTRL_WPENDING:
  201. ret = (long)ctx->obuf_len;
  202. if (ret == 0) {
  203. if (b->next_bio == NULL)
  204. return 0;
  205. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  206. }
  207. break;
  208. case BIO_C_SET_BUFF_SIZE:
  209. obs = (int)num;
  210. p = ctx->obuf;
  211. if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
  212. p = OPENSSL_malloc((int)num);
  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. BIOerr(BIO_F_LINEBUFFER_CTRL, 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. }