bss_mem.c 7.7 KB

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