bf_lbuf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* crypto/bio/bf_buff.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #include "cryptlib.h"
  61. #include <openssl/bio.h>
  62. #include <openssl/evp.h>
  63. static int linebuffer_write(BIO *h, const char *buf, int num);
  64. static int linebuffer_read(BIO *h, char *buf, int size);
  65. static int linebuffer_puts(BIO *h, const char *str);
  66. static int linebuffer_gets(BIO *h, char *str, int size);
  67. static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  68. static int linebuffer_new(BIO *h);
  69. static int linebuffer_free(BIO *data);
  70. static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  71. /* A 10k maximum should be enough for most purposes */
  72. #define DEFAULT_LINEBUFFER_SIZE 1024*10
  73. /* #define DEBUG */
  74. static BIO_METHOD methods_linebuffer = {
  75. BIO_TYPE_LINEBUFFER,
  76. "linebuffer",
  77. linebuffer_write,
  78. linebuffer_read,
  79. linebuffer_puts,
  80. linebuffer_gets,
  81. linebuffer_ctrl,
  82. linebuffer_new,
  83. linebuffer_free,
  84. linebuffer_callback_ctrl,
  85. };
  86. BIO_METHOD *BIO_f_linebuffer(void)
  87. {
  88. return (&methods_linebuffer);
  89. }
  90. typedef struct bio_linebuffer_ctx_struct {
  91. char *obuf; /* the output char array */
  92. int obuf_size; /* how big is the output buffer */
  93. int obuf_len; /* how many bytes are in it */
  94. } BIO_LINEBUFFER_CTX;
  95. static int linebuffer_new(BIO *bi)
  96. {
  97. BIO_LINEBUFFER_CTX *ctx;
  98. ctx = (BIO_LINEBUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_LINEBUFFER_CTX));
  99. if (ctx == NULL)
  100. return (0);
  101. ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
  102. if (ctx->obuf == NULL) {
  103. OPENSSL_free(ctx);
  104. return (0);
  105. }
  106. ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
  107. ctx->obuf_len = 0;
  108. bi->init = 1;
  109. bi->ptr = (char *)ctx;
  110. bi->flags = 0;
  111. return (1);
  112. }
  113. static int linebuffer_free(BIO *a)
  114. {
  115. BIO_LINEBUFFER_CTX *b;
  116. if (a == NULL)
  117. return (0);
  118. b = (BIO_LINEBUFFER_CTX *)a->ptr;
  119. if (b->obuf != NULL)
  120. OPENSSL_free(b->obuf);
  121. OPENSSL_free(a->ptr);
  122. a->ptr = NULL;
  123. a->init = 0;
  124. a->flags = 0;
  125. return (1);
  126. }
  127. static int linebuffer_read(BIO *b, char *out, int outl)
  128. {
  129. int ret = 0;
  130. if (out == NULL)
  131. return (0);
  132. if (b->next_bio == NULL)
  133. return (0);
  134. ret = BIO_read(b->next_bio, out, outl);
  135. BIO_clear_retry_flags(b);
  136. BIO_copy_next_retry(b);
  137. return (ret);
  138. }
  139. static int linebuffer_write(BIO *b, const char *in, int inl)
  140. {
  141. int i, num = 0, foundnl;
  142. BIO_LINEBUFFER_CTX *ctx;
  143. if ((in == NULL) || (inl <= 0))
  144. return (0);
  145. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  146. if ((ctx == NULL) || (b->next_bio == NULL))
  147. return (0);
  148. BIO_clear_retry_flags(b);
  149. do {
  150. const char *p;
  151. for (p = in; p < in + inl && *p != '\n'; p++) ;
  152. if (*p == '\n') {
  153. p++;
  154. foundnl = 1;
  155. } else
  156. foundnl = 0;
  157. /*
  158. * If a NL was found and we already have text in the save buffer,
  159. * concatenate them and write
  160. */
  161. while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
  162. && ctx->obuf_len > 0) {
  163. int orig_olen = ctx->obuf_len;
  164. i = ctx->obuf_size - ctx->obuf_len;
  165. if (p - in > 0) {
  166. if (i >= p - in) {
  167. memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
  168. ctx->obuf_len += p - in;
  169. inl -= p - in;
  170. num += p - in;
  171. in = p;
  172. } else {
  173. memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
  174. ctx->obuf_len += i;
  175. inl -= i;
  176. in += i;
  177. num += i;
  178. }
  179. }
  180. #if 0
  181. BIO_write(b->next_bio, "<*<", 3);
  182. #endif
  183. i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  184. if (i <= 0) {
  185. ctx->obuf_len = orig_olen;
  186. BIO_copy_next_retry(b);
  187. #if 0
  188. BIO_write(b->next_bio, ">*>", 3);
  189. #endif
  190. if (i < 0)
  191. return ((num > 0) ? num : i);
  192. if (i == 0)
  193. return (num);
  194. }
  195. #if 0
  196. BIO_write(b->next_bio, ">*>", 3);
  197. #endif
  198. if (i < ctx->obuf_len)
  199. memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
  200. ctx->obuf_len -= i;
  201. }
  202. /*
  203. * Now that the save buffer is emptied, let's write the input buffer
  204. * if a NL was found and there is anything to write.
  205. */
  206. if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
  207. #if 0
  208. BIO_write(b->next_bio, "<*<", 3);
  209. #endif
  210. i = BIO_write(b->next_bio, in, p - in);
  211. if (i <= 0) {
  212. BIO_copy_next_retry(b);
  213. #if 0
  214. BIO_write(b->next_bio, ">*>", 3);
  215. #endif
  216. if (i < 0)
  217. return ((num > 0) ? num : i);
  218. if (i == 0)
  219. return (num);
  220. }
  221. #if 0
  222. BIO_write(b->next_bio, ">*>", 3);
  223. #endif
  224. num += i;
  225. in += i;
  226. inl -= i;
  227. }
  228. }
  229. while (foundnl && inl > 0);
  230. /*
  231. * We've written as much as we can. The rest of the input buffer, if
  232. * any, is text that doesn't and with a NL and therefore needs to be
  233. * saved for the next trip.
  234. */
  235. if (inl > 0) {
  236. memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
  237. ctx->obuf_len += inl;
  238. num += inl;
  239. }
  240. return num;
  241. }
  242. static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  243. {
  244. BIO *dbio;
  245. BIO_LINEBUFFER_CTX *ctx;
  246. long ret = 1;
  247. char *p;
  248. int r;
  249. int obs;
  250. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  251. switch (cmd) {
  252. case BIO_CTRL_RESET:
  253. ctx->obuf_len = 0;
  254. if (b->next_bio == NULL)
  255. return (0);
  256. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  257. break;
  258. case BIO_CTRL_INFO:
  259. ret = (long)ctx->obuf_len;
  260. break;
  261. case BIO_CTRL_WPENDING:
  262. ret = (long)ctx->obuf_len;
  263. if (ret == 0) {
  264. if (b->next_bio == NULL)
  265. return (0);
  266. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  267. }
  268. break;
  269. case BIO_C_SET_BUFF_SIZE:
  270. obs = (int)num;
  271. p = ctx->obuf;
  272. if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
  273. p = (char *)OPENSSL_malloc((int)num);
  274. if (p == NULL)
  275. goto malloc_error;
  276. }
  277. if (ctx->obuf != p) {
  278. if (ctx->obuf_len > obs) {
  279. ctx->obuf_len = obs;
  280. }
  281. memcpy(p, ctx->obuf, ctx->obuf_len);
  282. OPENSSL_free(ctx->obuf);
  283. ctx->obuf = p;
  284. ctx->obuf_size = obs;
  285. }
  286. break;
  287. case BIO_C_DO_STATE_MACHINE:
  288. if (b->next_bio == NULL)
  289. return (0);
  290. BIO_clear_retry_flags(b);
  291. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  292. BIO_copy_next_retry(b);
  293. break;
  294. case BIO_CTRL_FLUSH:
  295. if (b->next_bio == NULL)
  296. return (0);
  297. if (ctx->obuf_len <= 0) {
  298. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  299. break;
  300. }
  301. for (;;) {
  302. BIO_clear_retry_flags(b);
  303. if (ctx->obuf_len > 0) {
  304. r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  305. #if 0
  306. fprintf(stderr, "FLUSH %3d -> %3d\n", ctx->obuf_len, r);
  307. #endif
  308. BIO_copy_next_retry(b);
  309. if (r <= 0)
  310. return ((long)r);
  311. if (r < ctx->obuf_len)
  312. memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
  313. ctx->obuf_len -= r;
  314. } else {
  315. ctx->obuf_len = 0;
  316. ret = 1;
  317. break;
  318. }
  319. }
  320. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  321. break;
  322. case BIO_CTRL_DUP:
  323. dbio = (BIO *)ptr;
  324. if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
  325. ret = 0;
  326. break;
  327. default:
  328. if (b->next_bio == NULL)
  329. return (0);
  330. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  331. break;
  332. }
  333. return (ret);
  334. malloc_error:
  335. BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
  336. return (0);
  337. }
  338. static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  339. {
  340. long ret = 1;
  341. if (b->next_bio == NULL)
  342. return (0);
  343. switch (cmd) {
  344. default:
  345. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  346. break;
  347. }
  348. return (ret);
  349. }
  350. static int linebuffer_gets(BIO *b, char *buf, int size)
  351. {
  352. if (b->next_bio == NULL)
  353. return (0);
  354. return (BIO_gets(b->next_bio, buf, size));
  355. }
  356. static int linebuffer_puts(BIO *b, const char *str)
  357. {
  358. return (linebuffer_write(b, str, strlen(str)));
  359. }