bss_mem.c 8.5 KB

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