2
0

bss_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #if defined(__linux) || defined(__sun) || defined(__hpux)
  66. /* Following definition aliases fopen to fopen64 on above mentioned
  67. * platforms. This makes it possible to open and sequentially access
  68. * files larger than 2GB from 32-bit application. It does not allow to
  69. * traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
  70. * 32-bit platform permits that, not with fseek/ftell. Not to mention
  71. * that breaking 2GB limit for seeking would require surgery to *our*
  72. * API. But sequential access suffices for practical cases when you
  73. * can run into large files, such as fingerprinting, so we can let API
  74. * alone. For reference, the list of 32-bit platforms which allow for
  75. * sequential access of large files without extra "magic" comprise *BSD,
  76. * Darwin, IRIX...
  77. */
  78. #ifndef _FILE_OFFSET_BITS
  79. #define _FILE_OFFSET_BITS 64
  80. #endif
  81. #endif
  82. #include <stdio.h>
  83. #include <errno.h>
  84. #include "cryptlib.h"
  85. #include "bio_lcl.h"
  86. #include <openssl/err.h>
  87. #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
  88. #include <nwfileio.h>
  89. #endif
  90. #if !defined(OPENSSL_NO_STDIO)
  91. static int MS_CALLBACK file_write(BIO *h, const char *buf, int num);
  92. static int MS_CALLBACK file_read(BIO *h, char *buf, int size);
  93. static int MS_CALLBACK file_puts(BIO *h, const char *str);
  94. static int MS_CALLBACK file_gets(BIO *h, char *str, int size);
  95. static long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  96. static int MS_CALLBACK file_new(BIO *h);
  97. static int MS_CALLBACK file_free(BIO *data);
  98. static BIO_METHOD methods_filep=
  99. {
  100. BIO_TYPE_FILE,
  101. "FILE pointer",
  102. file_write,
  103. file_read,
  104. file_puts,
  105. file_gets,
  106. file_ctrl,
  107. file_new,
  108. file_free,
  109. NULL,
  110. };
  111. BIO *BIO_new_file(const char *filename, const char *mode)
  112. {
  113. BIO *ret;
  114. FILE *file;
  115. if ((file=fopen(filename,mode)) == NULL)
  116. {
  117. SYSerr(SYS_F_FOPEN,get_last_sys_error());
  118. ERR_add_error_data(5,"fopen('",filename,"','",mode,"')");
  119. if (errno == ENOENT)
  120. BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE);
  121. else
  122. BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
  123. return(NULL);
  124. }
  125. if ((ret=BIO_new(BIO_s_file())) == NULL)
  126. {
  127. fclose(file);
  128. return(NULL);
  129. }
  130. BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
  131. BIO_set_fp(ret,file,BIO_CLOSE);
  132. return(ret);
  133. }
  134. BIO *BIO_new_fp(FILE *stream, int close_flag)
  135. {
  136. BIO *ret;
  137. if ((ret=BIO_new(BIO_s_file())) == NULL)
  138. return(NULL);
  139. BIO_set_flags(ret,BIO_FLAGS_UPLINK); /* redundant, left for documentation puposes */
  140. BIO_set_fp(ret,stream,close_flag);
  141. return(ret);
  142. }
  143. BIO_METHOD *BIO_s_file(void)
  144. {
  145. return(&methods_filep);
  146. }
  147. static int MS_CALLBACK file_new(BIO *bi)
  148. {
  149. bi->init=0;
  150. bi->num=0;
  151. bi->ptr=NULL;
  152. bi->flags=BIO_FLAGS_UPLINK; /* default to UPLINK */
  153. return(1);
  154. }
  155. static int MS_CALLBACK file_free(BIO *a)
  156. {
  157. if (a == NULL) return(0);
  158. if (a->shutdown)
  159. {
  160. if ((a->init) && (a->ptr != NULL))
  161. {
  162. if (a->flags&BIO_FLAGS_UPLINK)
  163. UP_fclose (a->ptr);
  164. else
  165. fclose (a->ptr);
  166. a->ptr=NULL;
  167. a->flags=BIO_FLAGS_UPLINK;
  168. }
  169. a->init=0;
  170. }
  171. return(1);
  172. }
  173. static int MS_CALLBACK file_read(BIO *b, char *out, int outl)
  174. {
  175. int ret=0;
  176. if (b->init && (out != NULL))
  177. {
  178. if (b->flags&BIO_FLAGS_UPLINK)
  179. ret=UP_fread(out,1,(int)outl,b->ptr);
  180. else
  181. ret=fread(out,1,(int)outl,(FILE *)b->ptr);
  182. if(ret == 0 && (b->flags&BIO_FLAGS_UPLINK)?UP_ferror((FILE *)b->ptr):ferror((FILE *)b->ptr))
  183. {
  184. SYSerr(SYS_F_FREAD,get_last_sys_error());
  185. BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB);
  186. ret=-1;
  187. }
  188. }
  189. return(ret);
  190. }
  191. static int MS_CALLBACK file_write(BIO *b, const char *in, int inl)
  192. {
  193. int ret=0;
  194. if (b->init && (in != NULL))
  195. {
  196. if (b->flags&BIO_FLAGS_UPLINK)
  197. ret=UP_fwrite(in,(int)inl,1,b->ptr);
  198. else
  199. ret=fwrite(in,(int)inl,1,(FILE *)b->ptr);
  200. if (ret)
  201. ret=inl;
  202. /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */
  203. /* according to Tim Hudson <tjh@cryptsoft.com>, the commented
  204. * out version above can cause 'inl' write calls under
  205. * some stupid stdio implementations (VMS) */
  206. }
  207. return(ret);
  208. }
  209. static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr)
  210. {
  211. long ret=1;
  212. FILE *fp=(FILE *)b->ptr;
  213. FILE **fpp;
  214. char p[4];
  215. switch (cmd)
  216. {
  217. case BIO_C_FILE_SEEK:
  218. case BIO_CTRL_RESET:
  219. if (b->flags&BIO_FLAGS_UPLINK)
  220. ret=(long)UP_fseek(b->ptr,num,0);
  221. else
  222. ret=(long)fseek(fp,num,0);
  223. break;
  224. case BIO_CTRL_EOF:
  225. if (b->flags&BIO_FLAGS_UPLINK)
  226. ret=(long)UP_feof(fp);
  227. else
  228. ret=(long)feof(fp);
  229. break;
  230. case BIO_C_FILE_TELL:
  231. case BIO_CTRL_INFO:
  232. if (b->flags&BIO_FLAGS_UPLINK)
  233. ret=UP_ftell(b->ptr);
  234. else
  235. ret=ftell(fp);
  236. break;
  237. case BIO_C_SET_FILE_PTR:
  238. file_free(b);
  239. b->shutdown=(int)num&BIO_CLOSE;
  240. b->ptr=ptr;
  241. b->init=1;
  242. #if BIO_FLAGS_UPLINK!=0
  243. #if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES)
  244. #define _IOB_ENTRIES 20
  245. #endif
  246. #if defined(_IOB_ENTRIES)
  247. /* Safety net to catch purely internal BIO_set_fp calls */
  248. if ((size_t)ptr >= (size_t)stdin &&
  249. (size_t)ptr < (size_t)(stdin+_IOB_ENTRIES))
  250. BIO_clear_flags(b,BIO_FLAGS_UPLINK);
  251. #endif
  252. #endif
  253. #ifdef UP_fsetmode
  254. if (b->flags&BIO_FLAGS_UPLINK)
  255. UP_fsetmode(b->ptr,num&BIO_FP_TEXT?'t':'b');
  256. else
  257. #endif
  258. {
  259. #if defined(OPENSSL_SYS_WINDOWS)
  260. int fd = fileno((FILE*)ptr);
  261. if (num & BIO_FP_TEXT)
  262. _setmode(fd,_O_TEXT);
  263. else
  264. _setmode(fd,_O_BINARY);
  265. #elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB)
  266. int fd = fileno((FILE*)ptr);
  267. /* Under CLib there are differences in file modes */
  268. if (num & BIO_FP_TEXT)
  269. setmode(fd,O_TEXT);
  270. else
  271. setmode(fd,O_BINARY);
  272. #elif defined(OPENSSL_SYS_MSDOS)
  273. int fd = fileno((FILE*)ptr);
  274. /* Set correct text/binary mode */
  275. if (num & BIO_FP_TEXT)
  276. _setmode(fd,_O_TEXT);
  277. /* Dangerous to set stdin/stdout to raw (unless redirected) */
  278. else
  279. {
  280. if (fd == STDIN_FILENO || fd == STDOUT_FILENO)
  281. {
  282. if (isatty(fd) <= 0)
  283. _setmode(fd,_O_BINARY);
  284. }
  285. else
  286. _setmode(fd,_O_BINARY);
  287. }
  288. #elif defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
  289. int fd = fileno((FILE*)ptr);
  290. if (num & BIO_FP_TEXT)
  291. setmode(fd, O_TEXT);
  292. else
  293. setmode(fd, O_BINARY);
  294. #endif
  295. }
  296. break;
  297. case BIO_C_SET_FILENAME:
  298. file_free(b);
  299. b->shutdown=(int)num&BIO_CLOSE;
  300. if (num & BIO_FP_APPEND)
  301. {
  302. if (num & BIO_FP_READ)
  303. BUF_strlcpy(p,"a+",sizeof p);
  304. else BUF_strlcpy(p,"a",sizeof p);
  305. }
  306. else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
  307. BUF_strlcpy(p,"r+",sizeof p);
  308. else if (num & BIO_FP_WRITE)
  309. BUF_strlcpy(p,"w",sizeof p);
  310. else if (num & BIO_FP_READ)
  311. BUF_strlcpy(p,"r",sizeof p);
  312. else
  313. {
  314. BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE);
  315. ret=0;
  316. break;
  317. }
  318. #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN)
  319. if (!(num & BIO_FP_TEXT))
  320. strcat(p,"b");
  321. else
  322. strcat(p,"t");
  323. #endif
  324. #if defined(OPENSSL_SYS_NETWARE)
  325. if (!(num & BIO_FP_TEXT))
  326. strcat(p,"b");
  327. else
  328. strcat(p,"t");
  329. #endif
  330. fp=fopen(ptr,p);
  331. if (fp == NULL)
  332. {
  333. SYSerr(SYS_F_FOPEN,get_last_sys_error());
  334. ERR_add_error_data(5,"fopen('",ptr,"','",p,"')");
  335. BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB);
  336. ret=0;
  337. break;
  338. }
  339. b->ptr=fp;
  340. b->init=1;
  341. BIO_clear_flags(b,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */
  342. break;
  343. case BIO_C_GET_FILE_PTR:
  344. /* the ptr parameter is actually a FILE ** in this case. */
  345. if (ptr != NULL)
  346. {
  347. fpp=(FILE **)ptr;
  348. *fpp=(FILE *)b->ptr;
  349. }
  350. break;
  351. case BIO_CTRL_GET_CLOSE:
  352. ret=(long)b->shutdown;
  353. break;
  354. case BIO_CTRL_SET_CLOSE:
  355. b->shutdown=(int)num;
  356. break;
  357. case BIO_CTRL_FLUSH:
  358. if (b->flags&BIO_FLAGS_UPLINK)
  359. UP_fflush(b->ptr);
  360. else
  361. fflush((FILE *)b->ptr);
  362. break;
  363. case BIO_CTRL_DUP:
  364. ret=1;
  365. break;
  366. case BIO_CTRL_WPENDING:
  367. case BIO_CTRL_PENDING:
  368. case BIO_CTRL_PUSH:
  369. case BIO_CTRL_POP:
  370. default:
  371. ret=0;
  372. break;
  373. }
  374. return(ret);
  375. }
  376. static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size)
  377. {
  378. int ret=0;
  379. buf[0]='\0';
  380. if (bp->flags&BIO_FLAGS_UPLINK)
  381. UP_fgets(buf,size,bp->ptr);
  382. else
  383. fgets(buf,size,(FILE *)bp->ptr);
  384. if (buf[0] != '\0')
  385. ret=strlen(buf);
  386. return(ret);
  387. }
  388. static int MS_CALLBACK file_puts(BIO *bp, const char *str)
  389. {
  390. int n,ret;
  391. n=strlen(str);
  392. ret=file_write(bp,str,n);
  393. return(ret);
  394. }
  395. #endif /* OPENSSL_NO_STDIO */
  396. #endif /* HEADER_BSS_FILE_C */