bf_buff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. static int buffer_write(BIO *h, const char *buf, int num);
  63. static int buffer_read(BIO *h, char *buf, int size);
  64. static int buffer_puts(BIO *h, const char *str);
  65. static int buffer_gets(BIO *h, char *str, int size);
  66. static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  67. static int buffer_new(BIO *h);
  68. static int buffer_free(BIO *data);
  69. static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  70. #define DEFAULT_BUFFER_SIZE 4096
  71. static BIO_METHOD methods_buffer = {
  72. BIO_TYPE_BUFFER,
  73. "buffer",
  74. buffer_write,
  75. buffer_read,
  76. buffer_puts,
  77. buffer_gets,
  78. buffer_ctrl,
  79. buffer_new,
  80. buffer_free,
  81. buffer_callback_ctrl,
  82. };
  83. BIO_METHOD *BIO_f_buffer(void)
  84. {
  85. return (&methods_buffer);
  86. }
  87. static int buffer_new(BIO *bi)
  88. {
  89. BIO_F_BUFFER_CTX *ctx;
  90. ctx = (BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
  91. if (ctx == NULL)
  92. return (0);
  93. ctx->ibuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  94. if (ctx->ibuf == NULL) {
  95. OPENSSL_free(ctx);
  96. return (0);
  97. }
  98. ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  99. if (ctx->obuf == NULL) {
  100. OPENSSL_free(ctx->ibuf);
  101. OPENSSL_free(ctx);
  102. return (0);
  103. }
  104. ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
  105. ctx->obuf_size = DEFAULT_BUFFER_SIZE;
  106. ctx->ibuf_len = 0;
  107. ctx->ibuf_off = 0;
  108. ctx->obuf_len = 0;
  109. ctx->obuf_off = 0;
  110. bi->init = 1;
  111. bi->ptr = (char *)ctx;
  112. bi->flags = 0;
  113. return (1);
  114. }
  115. static int buffer_free(BIO *a)
  116. {
  117. BIO_F_BUFFER_CTX *b;
  118. if (a == NULL)
  119. return (0);
  120. b = (BIO_F_BUFFER_CTX *)a->ptr;
  121. if (b->ibuf != NULL)
  122. OPENSSL_free(b->ibuf);
  123. if (b->obuf != NULL)
  124. OPENSSL_free(b->obuf);
  125. OPENSSL_free(a->ptr);
  126. a->ptr = NULL;
  127. a->init = 0;
  128. a->flags = 0;
  129. return (1);
  130. }
  131. static int buffer_read(BIO *b, char *out, int outl)
  132. {
  133. int i, num = 0;
  134. BIO_F_BUFFER_CTX *ctx;
  135. if (out == NULL)
  136. return (0);
  137. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  138. if ((ctx == NULL) || (b->next_bio == NULL))
  139. return (0);
  140. num = 0;
  141. BIO_clear_retry_flags(b);
  142. start:
  143. i = ctx->ibuf_len;
  144. /* If there is stuff left over, grab it */
  145. if (i != 0) {
  146. if (i > outl)
  147. i = outl;
  148. memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
  149. ctx->ibuf_off += i;
  150. ctx->ibuf_len -= i;
  151. num += i;
  152. if (outl == i)
  153. return (num);
  154. outl -= i;
  155. out += i;
  156. }
  157. /*
  158. * We may have done a partial read. try to do more. We have nothing in
  159. * the buffer. If we get an error and have read some data, just return it
  160. * and let them retry to get the error again. copy direct to parent
  161. * address space
  162. */
  163. if (outl > ctx->ibuf_size) {
  164. for (;;) {
  165. i = BIO_read(b->next_bio, out, outl);
  166. if (i <= 0) {
  167. BIO_copy_next_retry(b);
  168. if (i < 0)
  169. return ((num > 0) ? num : i);
  170. if (i == 0)
  171. return (num);
  172. }
  173. num += i;
  174. if (outl == i)
  175. return (num);
  176. out += i;
  177. outl -= i;
  178. }
  179. }
  180. /* else */
  181. /* we are going to be doing some buffering */
  182. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  183. if (i <= 0) {
  184. BIO_copy_next_retry(b);
  185. if (i < 0)
  186. return ((num > 0) ? num : i);
  187. if (i == 0)
  188. return (num);
  189. }
  190. ctx->ibuf_off = 0;
  191. ctx->ibuf_len = i;
  192. /* Lets re-read using ourselves :-) */
  193. goto start;
  194. }
  195. static int buffer_write(BIO *b, const char *in, int inl)
  196. {
  197. int i, num = 0;
  198. BIO_F_BUFFER_CTX *ctx;
  199. if ((in == NULL) || (inl <= 0))
  200. return (0);
  201. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  202. if ((ctx == NULL) || (b->next_bio == NULL))
  203. return (0);
  204. BIO_clear_retry_flags(b);
  205. start:
  206. i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
  207. /* add to buffer and return */
  208. if (i >= inl) {
  209. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
  210. ctx->obuf_len += inl;
  211. return (num + inl);
  212. }
  213. /* else */
  214. /* stuff already in buffer, so add to it first, then flush */
  215. if (ctx->obuf_len != 0) {
  216. if (i > 0) { /* lets fill it up if we can */
  217. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
  218. in += i;
  219. inl -= i;
  220. num += i;
  221. ctx->obuf_len += i;
  222. }
  223. /* we now have a full buffer needing flushing */
  224. for (;;) {
  225. i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
  226. ctx->obuf_len);
  227. if (i <= 0) {
  228. BIO_copy_next_retry(b);
  229. if (i < 0)
  230. return ((num > 0) ? num : i);
  231. if (i == 0)
  232. return (num);
  233. }
  234. ctx->obuf_off += i;
  235. ctx->obuf_len -= i;
  236. if (ctx->obuf_len == 0)
  237. break;
  238. }
  239. }
  240. /*
  241. * we only get here if the buffer has been flushed and we still have
  242. * stuff to write
  243. */
  244. ctx->obuf_off = 0;
  245. /* we now have inl bytes to write */
  246. while (inl >= ctx->obuf_size) {
  247. i = BIO_write(b->next_bio, in, inl);
  248. if (i <= 0) {
  249. BIO_copy_next_retry(b);
  250. if (i < 0)
  251. return ((num > 0) ? num : i);
  252. if (i == 0)
  253. return (num);
  254. }
  255. num += i;
  256. in += i;
  257. inl -= i;
  258. if (inl == 0)
  259. return (num);
  260. }
  261. /*
  262. * copy the rest into the buffer since we have only a small amount left
  263. */
  264. goto start;
  265. }
  266. static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  267. {
  268. BIO *dbio;
  269. BIO_F_BUFFER_CTX *ctx;
  270. long ret = 1;
  271. char *p1, *p2;
  272. int r, i, *ip;
  273. int ibs, obs;
  274. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  275. switch (cmd) {
  276. case BIO_CTRL_RESET:
  277. ctx->ibuf_off = 0;
  278. ctx->ibuf_len = 0;
  279. ctx->obuf_off = 0;
  280. ctx->obuf_len = 0;
  281. if (b->next_bio == NULL)
  282. return (0);
  283. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  284. break;
  285. case BIO_CTRL_INFO:
  286. ret = (long)ctx->obuf_len;
  287. break;
  288. case BIO_C_GET_BUFF_NUM_LINES:
  289. ret = 0;
  290. p1 = ctx->ibuf;
  291. for (i = 0; i < ctx->ibuf_len; i++) {
  292. if (p1[ctx->ibuf_off + i] == '\n')
  293. ret++;
  294. }
  295. break;
  296. case BIO_CTRL_WPENDING:
  297. ret = (long)ctx->obuf_len;
  298. if (ret == 0) {
  299. if (b->next_bio == NULL)
  300. return (0);
  301. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  302. }
  303. break;
  304. case BIO_CTRL_PENDING:
  305. ret = (long)ctx->ibuf_len;
  306. if (ret == 0) {
  307. if (b->next_bio == NULL)
  308. return (0);
  309. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  310. }
  311. break;
  312. case BIO_C_SET_BUFF_READ_DATA:
  313. if (num > ctx->ibuf_size) {
  314. p1 = OPENSSL_malloc((int)num);
  315. if (p1 == NULL)
  316. goto malloc_error;
  317. if (ctx->ibuf != NULL)
  318. OPENSSL_free(ctx->ibuf);
  319. ctx->ibuf = p1;
  320. }
  321. ctx->ibuf_off = 0;
  322. ctx->ibuf_len = (int)num;
  323. memcpy(ctx->ibuf, ptr, (int)num);
  324. ret = 1;
  325. break;
  326. case BIO_C_SET_BUFF_SIZE:
  327. if (ptr != NULL) {
  328. ip = (int *)ptr;
  329. if (*ip == 0) {
  330. ibs = (int)num;
  331. obs = ctx->obuf_size;
  332. } else { /* if (*ip == 1) */
  333. ibs = ctx->ibuf_size;
  334. obs = (int)num;
  335. }
  336. } else {
  337. ibs = (int)num;
  338. obs = (int)num;
  339. }
  340. p1 = ctx->ibuf;
  341. p2 = ctx->obuf;
  342. if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
  343. p1 = (char *)OPENSSL_malloc((int)num);
  344. if (p1 == NULL)
  345. goto malloc_error;
  346. }
  347. if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
  348. p2 = (char *)OPENSSL_malloc((int)num);
  349. if (p2 == NULL) {
  350. if (p1 != ctx->ibuf)
  351. OPENSSL_free(p1);
  352. goto malloc_error;
  353. }
  354. }
  355. if (ctx->ibuf != p1) {
  356. OPENSSL_free(ctx->ibuf);
  357. ctx->ibuf = p1;
  358. ctx->ibuf_off = 0;
  359. ctx->ibuf_len = 0;
  360. ctx->ibuf_size = ibs;
  361. }
  362. if (ctx->obuf != p2) {
  363. OPENSSL_free(ctx->obuf);
  364. ctx->obuf = p2;
  365. ctx->obuf_off = 0;
  366. ctx->obuf_len = 0;
  367. ctx->obuf_size = obs;
  368. }
  369. break;
  370. case BIO_C_DO_STATE_MACHINE:
  371. if (b->next_bio == NULL)
  372. return (0);
  373. BIO_clear_retry_flags(b);
  374. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  375. BIO_copy_next_retry(b);
  376. break;
  377. case BIO_CTRL_FLUSH:
  378. if (b->next_bio == NULL)
  379. return (0);
  380. if (ctx->obuf_len <= 0) {
  381. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  382. break;
  383. }
  384. for (;;) {
  385. BIO_clear_retry_flags(b);
  386. if (ctx->obuf_len > 0) {
  387. r = BIO_write(b->next_bio,
  388. &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
  389. #if 0
  390. fprintf(stderr, "FLUSH [%3d] %3d -> %3d\n", ctx->obuf_off,
  391. ctx->obuf_len, r);
  392. #endif
  393. BIO_copy_next_retry(b);
  394. if (r <= 0)
  395. return ((long)r);
  396. ctx->obuf_off += r;
  397. ctx->obuf_len -= r;
  398. } else {
  399. ctx->obuf_len = 0;
  400. ctx->obuf_off = 0;
  401. ret = 1;
  402. break;
  403. }
  404. }
  405. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  406. break;
  407. case BIO_CTRL_DUP:
  408. dbio = (BIO *)ptr;
  409. if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
  410. !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
  411. ret = 0;
  412. break;
  413. default:
  414. if (b->next_bio == NULL)
  415. return (0);
  416. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  417. break;
  418. }
  419. return (ret);
  420. malloc_error:
  421. BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
  422. return (0);
  423. }
  424. static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  425. {
  426. long ret = 1;
  427. if (b->next_bio == NULL)
  428. return (0);
  429. switch (cmd) {
  430. default:
  431. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  432. break;
  433. }
  434. return (ret);
  435. }
  436. static int buffer_gets(BIO *b, char *buf, int size)
  437. {
  438. BIO_F_BUFFER_CTX *ctx;
  439. int num = 0, i, flag;
  440. char *p;
  441. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  442. size--; /* reserve space for a '\0' */
  443. BIO_clear_retry_flags(b);
  444. for (;;) {
  445. if (ctx->ibuf_len > 0) {
  446. p = &(ctx->ibuf[ctx->ibuf_off]);
  447. flag = 0;
  448. for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
  449. *(buf++) = p[i];
  450. if (p[i] == '\n') {
  451. flag = 1;
  452. i++;
  453. break;
  454. }
  455. }
  456. num += i;
  457. size -= i;
  458. ctx->ibuf_len -= i;
  459. ctx->ibuf_off += i;
  460. if (flag || size == 0) {
  461. *buf = '\0';
  462. return (num);
  463. }
  464. } else { /* read another chunk */
  465. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  466. if (i <= 0) {
  467. BIO_copy_next_retry(b);
  468. *buf = '\0';
  469. if (i < 0)
  470. return ((num > 0) ? num : i);
  471. if (i == 0)
  472. return (num);
  473. }
  474. ctx->ibuf_len = i;
  475. ctx->ibuf_off = 0;
  476. }
  477. }
  478. }
  479. static int buffer_puts(BIO *b, const char *str)
  480. {
  481. return (buffer_write(b, str, strlen(str)));
  482. }