2
0

bss_mem.c 7.6 KB

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