bss_file.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* crypto/bio/bss_file.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. /*
  59. * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout
  60. * with binary data (e.g. asn1parse -inform DER < xxx) under
  61. * Windows
  62. */
  63. #ifndef HEADER_BSS_FILE_C
  64. #define HEADER_BSS_FILE_C
  65. #include <stdio.h>
  66. #include <errno.h>
  67. #include "cryptlib.h"
  68. #include <openssl/bio.h>
  69. #include <openssl/err.h>
  70. #if !defined(OPENSSL_NO_STDIO)
  71. static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
  72. static int MS_CALLBACK file_read(BIO *h, char *buf, int size);
  73. static int MS_CALLBACK file_puts(BIO *h, const char *str);
  74. static int MS_CALLBACK file_gets(BIO *h, char *str, int size);
  75. static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  76. static int MS_CALLBACK file_new(BIO *h);
  77. static int MS_CALLBACK file_free(BIO *data);
  78. static BIO_METHOD methods_filep=
  79. {
  80. BIO_TYPE_FILE,
  81. "FILE pointer",
  82. file_write,
  83. file_read,
  84. file_puts,
  85. file_gets,
  86. file_ctrl,
  87. file_new,
  88. file_free,
  89. NULL,
  90. };
  91. BIO *BIO_new_file(const char *filename, const char *mode)
  92. {
  93. BIO *ret;
  94. FILE *file;
  95. if ((file=fopen(filename,mode)) == NULL)
  96. {
  97. SYSerr(SYS_F_FOPEN,get_last_sys_error());
  98. ERR_add_error_data(5,"fopen('",filename,"','",mode,"')");
  99. BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
  100. return(NULL);
  101. }
  102. if ((ret=BIO_new(BIO_s_file_internal())) == NULL)
  103. return(NULL);
  104. BIO_set_fp(ret,file,BIO_CLOSE);
  105. return(ret);
  106. }
  107. BIO *BIO_new_fp(FILE *stream, int close_flag)
  108. {
  109. BIO *ret;
  110. if ((ret=BIO_new(BIO_s_file())) == NULL)
  111. return(NULL);
  112. BIO_set_fp(ret,stream,close_flag);
  113. return(ret);
  114. }
  115. BIO_METHOD *BIO_s_file(void)
  116. {
  117. return(&methods_filep);
  118. }
  119. static int MS_CALLBACK file_new(BIO *bi)
  120. {
  121. bi->init=0;
  122. bi->num=0;
  123. bi->ptr=NULL;
  124. return(1);
  125. }
  126. static int MS_CALLBACK file_free(BIO *a)
  127. {
  128. if (a == NULL) return(0);
  129. if (a->shutdown)
  130. {
  131. if ((a->init) && (a->ptr != NULL))
  132. {
  133. fclose((FILE *)a->ptr);
  134. a->ptr=NULL;
  135. }
  136. a->init=0;
  137. }
  138. return(1);
  139. }
  140. static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
  141. {
  142. int ret=0;
  143. if (b->init && (out != NULL))
  144. {
  145. ret=fread(out,1,(int)outl,(FILE *)b->ptr);
  146. }
  147. return(ret);
  148. }
  149. static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
  150. {
  151. int ret=0;
  152. if (b->init && (in != NULL))
  153. {
  154. if (fwrite(in,(int)inl,1,(FILE *)b->ptr))
  155. ret=inl;
  156. /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
  157. /* according to Tim Hudson <tjh@cryptsoft.com>, the commented
  158. * out version above can cause 'inl' write calls under
  159. * some stupid stdio implementations (VMS) */
  160. }
  161. return(ret);
  162. }
  163. static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
  164. {
  165. long ret=1;
  166. FILE *fp=(FILE *)b->ptr;
  167. FILE **fpp;
  168. char p[4];
  169. switch (cmd)
  170. {
  171. case BIO_C_FILE_SEEK:
  172. case BIO_CTRL_RESET:
  173. ret=(long)fseek(fp,num,0);
  174. break;
  175. case BIO_CTRL_EOF:
  176. ret=(long)feof(fp);
  177. break;
  178. case BIO_C_FILE_TELL:
  179. case BIO_CTRL_INFO:
  180. ret=ftell(fp);
  181. break;
  182. case BIO_C_SET_FILE_PTR:
  183. file_free(b);
  184. b->shutdown=(int)num&BIO_CLOSE;
  185. b->ptr=(char *)ptr;
  186. b->init=1;
  187. #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
  188. /* Set correct text/binary mode */
  189. if (num & BIO_FP_TEXT)
  190. _setmode(fileno((FILE *)ptr),_O_TEXT);
  191. else
  192. _setmode(fileno((FILE *)ptr),_O_BINARY);
  193. #endif
  194. break;
  195. case BIO_C_SET_FILENAME:
  196. file_free(b);
  197. b->shutdown=(int)num&BIO_CLOSE;
  198. if (num & BIO_FP_APPEND)
  199. {
  200. if (num & BIO_FP_READ)
  201. strcpy(p,"a+");
  202. else strcpy(p,"a");
  203. }
  204. else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
  205. strcpy(p,"r+");
  206. else if (num & BIO_FP_WRITE)
  207. strcpy(p,"w");
  208. else if (num & BIO_FP_READ)
  209. strcpy(p,"r");
  210. else
  211. {
  212. BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE);
  213. ret=0;
  214. break;
  215. }
  216. #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS)
  217. if (!(num & BIO_FP_TEXT))
  218. strcat(p,"b");
  219. else
  220. strcat(p,"t");
  221. #endif
  222. fp=fopen(ptr,p);
  223. if (fp == NULL)
  224. {
  225. SYSerr(SYS_F_FOPEN,get_last_sys_error());
  226. ERR_add_error_data(5,"fopen('",ptr,"','",p,"')");
  227. BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB);
  228. ret=0;
  229. break;
  230. }
  231. b->ptr=(char *)fp;
  232. b->init=1;
  233. break;
  234. case BIO_C_GET_FILE_PTR:
  235. /* the ptr parameter is actually a FILE ** in this case. */
  236. if (ptr != NULL)
  237. {
  238. fpp=(FILE **)ptr;
  239. *fpp=(FILE *)b->ptr;
  240. }
  241. break;
  242. case BIO_CTRL_GET_CLOSE:
  243. ret=(long)b->shutdown;
  244. break;
  245. case BIO_CTRL_SET_CLOSE:
  246. b->shutdown=(int)num;
  247. break;
  248. case BIO_CTRL_FLUSH:
  249. fflush((FILE *)b->ptr);
  250. break;
  251. case BIO_CTRL_DUP:
  252. ret=1;
  253. break;
  254. case BIO_CTRL_WPENDING:
  255. case BIO_CTRL_PENDING:
  256. case BIO_CTRL_PUSH:
  257. case BIO_CTRL_POP:
  258. default:
  259. ret=0;
  260. break;
  261. }
  262. return(ret);
  263. }
  264. static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
  265. {
  266. int ret=0;
  267. buf[0]='\0';
  268. fgets(buf,size,(FILE *)bp->ptr);
  269. if (buf[0] != '\0')
  270. ret=strlen(buf);
  271. return(ret);
  272. }
  273. static int MS_CALLBACK file_puts(BIO *bp, const char *str)
  274. {
  275. int n,ret;
  276. n=strlen(str);
  277. ret=file_write(bp,str,n);
  278. return(ret);
  279. }
  280. #endif /* OPENSSL_NO_STDIO */
  281. #endif /* HEADER_BSS_FILE_C */