bss_mem.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* crypto/bio/bss_mem.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 mem_write(BIO *h, const char *buf, int num);
  63. static int mem_read(BIO *h, char *buf, int size);
  64. static int mem_puts(BIO *h, const char *str);
  65. static int mem_gets(BIO *h, char *str, int size);
  66. static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  67. static int mem_new(BIO *h);
  68. static int mem_free(BIO *data);
  69. static BIO_METHOD mem_method = {
  70. BIO_TYPE_MEM,
  71. "memory buffer",
  72. mem_write,
  73. mem_read,
  74. mem_puts,
  75. mem_gets,
  76. mem_ctrl,
  77. mem_new,
  78. mem_free,
  79. NULL,
  80. };
  81. /*
  82. * bio->num is used to hold the value to return on 'empty', if it is 0,
  83. * should_retry is not set
  84. */
  85. BIO_METHOD *BIO_s_mem(void)
  86. {
  87. return (&mem_method);
  88. }
  89. BIO *BIO_new_mem_buf(const void *buf, int len)
  90. {
  91. BIO *ret;
  92. BUF_MEM *b;
  93. size_t sz;
  94. if (!buf) {
  95. BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
  96. return NULL;
  97. }
  98. sz = (len < 0) ? strlen(buf) : (size_t)len;
  99. if (!(ret = BIO_new(BIO_s_mem())))
  100. return NULL;
  101. b = (BUF_MEM *)ret->ptr;
  102. /* Cast away const and trust in the MEM_RDONLY flag. */
  103. b->data = (void *)buf;
  104. b->length = sz;
  105. b->max = sz;
  106. ret->flags |= BIO_FLAGS_MEM_RDONLY;
  107. /* Since this is static data retrying wont help */
  108. ret->num = 0;
  109. return ret;
  110. }
  111. static int mem_new(BIO *bi)
  112. {
  113. BUF_MEM *b;
  114. if ((b = BUF_MEM_new()) == NULL)
  115. return (0);
  116. bi->shutdown = 1;
  117. bi->init = 1;
  118. bi->num = -1;
  119. bi->ptr = (char *)b;
  120. return (1);
  121. }
  122. static int mem_free(BIO *a)
  123. {
  124. if (a == NULL)
  125. return (0);
  126. if (a->shutdown) {
  127. if ((a->init) && (a->ptr != NULL)) {
  128. BUF_MEM *b;
  129. b = (BUF_MEM *)a->ptr;
  130. if (a->flags & BIO_FLAGS_MEM_RDONLY)
  131. b->data = NULL;
  132. BUF_MEM_free(b);
  133. a->ptr = NULL;
  134. }
  135. }
  136. return (1);
  137. }
  138. static int mem_read(BIO *b, char *out, int outl)
  139. {
  140. int ret = -1;
  141. BUF_MEM *bm;
  142. bm = (BUF_MEM *)b->ptr;
  143. BIO_clear_retry_flags(b);
  144. ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
  145. if ((out != NULL) && (ret > 0)) {
  146. memcpy(out, bm->data, ret);
  147. bm->length -= ret;
  148. if (b->flags & BIO_FLAGS_MEM_RDONLY)
  149. bm->data += ret;
  150. else {
  151. memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
  152. }
  153. } else if (bm->length == 0) {
  154. ret = b->num;
  155. if (ret != 0)
  156. BIO_set_retry_read(b);
  157. }
  158. return (ret);
  159. }
  160. static int mem_write(BIO *b, const char *in, int inl)
  161. {
  162. int ret = -1;
  163. int blen;
  164. BUF_MEM *bm;
  165. bm = (BUF_MEM *)b->ptr;
  166. if (in == NULL) {
  167. BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
  168. goto end;
  169. }
  170. if (b->flags & BIO_FLAGS_MEM_RDONLY) {
  171. BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
  172. goto end;
  173. }
  174. BIO_clear_retry_flags(b);
  175. blen = bm->length;
  176. if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
  177. goto end;
  178. memcpy(&(bm->data[blen]), in, inl);
  179. ret = inl;
  180. end:
  181. return (ret);
  182. }
  183. static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
  184. {
  185. long ret = 1;
  186. char **pptr;
  187. BUF_MEM *bm = (BUF_MEM *)b->ptr;
  188. switch (cmd) {
  189. case BIO_CTRL_RESET:
  190. if (bm->data != NULL) {
  191. /* For read only case reset to the start again */
  192. if (b->flags & BIO_FLAGS_MEM_RDONLY) {
  193. bm->data -= bm->max - bm->length;
  194. bm->length = bm->max;
  195. } else {
  196. memset(bm->data, 0, bm->max);
  197. bm->length = 0;
  198. }
  199. }
  200. break;
  201. case BIO_CTRL_EOF:
  202. ret = (long)(bm->length == 0);
  203. break;
  204. case BIO_C_SET_BUF_MEM_EOF_RETURN:
  205. b->num = (int)num;
  206. break;
  207. case BIO_CTRL_INFO:
  208. ret = (long)bm->length;
  209. if (ptr != NULL) {
  210. pptr = (char **)ptr;
  211. *pptr = (char *)&(bm->data[0]);
  212. }
  213. break;
  214. case BIO_C_SET_BUF_MEM:
  215. mem_free(b);
  216. b->shutdown = (int)num;
  217. b->ptr = ptr;
  218. break;
  219. case BIO_C_GET_BUF_MEM_PTR:
  220. if (ptr != NULL) {
  221. pptr = (char **)ptr;
  222. *pptr = (char *)bm;
  223. }
  224. break;
  225. case BIO_CTRL_GET_CLOSE:
  226. ret = (long)b->shutdown;
  227. break;
  228. case BIO_CTRL_SET_CLOSE:
  229. b->shutdown = (int)num;
  230. break;
  231. case BIO_CTRL_WPENDING:
  232. ret = 0L;
  233. break;
  234. case BIO_CTRL_PENDING:
  235. ret = (long)bm->length;
  236. break;
  237. case BIO_CTRL_DUP:
  238. case BIO_CTRL_FLUSH:
  239. ret = 1;
  240. break;
  241. case BIO_CTRL_PUSH:
  242. case BIO_CTRL_POP:
  243. default:
  244. ret = 0;
  245. break;
  246. }
  247. return (ret);
  248. }
  249. static int mem_gets(BIO *bp, char *buf, int size)
  250. {
  251. int i, j;
  252. int ret = -1;
  253. char *p;
  254. BUF_MEM *bm = (BUF_MEM *)bp->ptr;
  255. BIO_clear_retry_flags(bp);
  256. j = bm->length;
  257. if ((size - 1) < j)
  258. j = size - 1;
  259. if (j <= 0) {
  260. *buf = '\0';
  261. return 0;
  262. }
  263. p = bm->data;
  264. for (i = 0; i < j; i++) {
  265. if (p[i] == '\n') {
  266. i++;
  267. break;
  268. }
  269. }
  270. /*
  271. * i is now the max num of bytes to copy, either j or up to
  272. * and including the first newline
  273. */
  274. i = mem_read(bp, buf, i);
  275. if (i > 0)
  276. buf[i] = '\0';
  277. ret = i;
  278. return (ret);
  279. }
  280. static int mem_puts(BIO *bp, const char *str)
  281. {
  282. int n, ret;
  283. n = strlen(str);
  284. ret = mem_write(bp, str, n);
  285. /* memory semantics is that it will always work */
  286. return (ret);
  287. }