file.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #if defined(__linux) || defined(__sun) || defined(__hpux)
  57. // Following definition aliases fopen to fopen64 on above mentioned
  58. // platforms. This makes it possible to open and sequentially access
  59. // files larger than 2GB from 32-bit application. It does not allow to
  60. // traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
  61. // 32-bit platform permits that, not with fseek/ftell. Not to mention
  62. // that breaking 2GB limit for seeking would require surgery to *our*
  63. // API. But sequential access suffices for practical cases when you
  64. // can run into large files, such as fingerprinting, so we can let API
  65. // alone. For reference, the list of 32-bit platforms which allow for
  66. // sequential access of large files without extra "magic" comprise *BSD,
  67. // Darwin, IRIX...
  68. #ifndef _FILE_OFFSET_BITS
  69. #define _FILE_OFFSET_BITS 64
  70. #endif
  71. #endif
  72. #include <openssl/bio.h>
  73. #include <errno.h>
  74. #include <stdio.h>
  75. #include <string.h>
  76. #include <openssl/buf.h>
  77. #include <openssl/err.h>
  78. #include <openssl/mem.h>
  79. #define BIO_FP_READ 0x02
  80. #define BIO_FP_WRITE 0x04
  81. #define BIO_FP_APPEND 0x08
  82. BIO *BIO_new_file(const char *filename, const char *mode) {
  83. BIO *ret;
  84. FILE *file;
  85. file = fopen(filename, mode);
  86. if (file == NULL) {
  87. OPENSSL_PUT_SYSTEM_ERROR();
  88. ERR_add_error_data(5, "fopen('", filename, "','", mode, "')");
  89. if (errno == ENOENT) {
  90. OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE);
  91. } else {
  92. OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB);
  93. }
  94. return NULL;
  95. }
  96. ret = BIO_new(BIO_s_file());
  97. if (ret == NULL) {
  98. fclose(file);
  99. return NULL;
  100. }
  101. BIO_set_fp(ret, file, BIO_CLOSE);
  102. return ret;
  103. }
  104. BIO *BIO_new_fp(FILE *stream, int close_flag) {
  105. BIO *ret = BIO_new(BIO_s_file());
  106. if (ret == NULL) {
  107. return NULL;
  108. }
  109. BIO_set_fp(ret, stream, close_flag);
  110. return ret;
  111. }
  112. static int file_new(BIO *bio) { return 1; }
  113. static int file_free(BIO *bio) {
  114. if (bio == NULL) {
  115. return 0;
  116. }
  117. if (!bio->shutdown) {
  118. return 1;
  119. }
  120. if (bio->init && bio->ptr != NULL) {
  121. fclose(bio->ptr);
  122. bio->ptr = NULL;
  123. }
  124. bio->init = 0;
  125. return 1;
  126. }
  127. static int file_read(BIO *b, char *out, int outl) {
  128. if (!b->init) {
  129. return 0;
  130. }
  131. size_t ret = fread(out, 1, outl, (FILE *)b->ptr);
  132. if (ret == 0 && ferror((FILE *)b->ptr)) {
  133. OPENSSL_PUT_SYSTEM_ERROR();
  134. OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
  135. return -1;
  136. }
  137. // fread reads at most |outl| bytes, so |ret| fits in an int.
  138. return (int)ret;
  139. }
  140. static int file_write(BIO *b, const char *in, int inl) {
  141. int ret = 0;
  142. if (!b->init) {
  143. return 0;
  144. }
  145. ret = fwrite(in, inl, 1, (FILE *)b->ptr);
  146. if (ret > 0) {
  147. ret = inl;
  148. }
  149. return ret;
  150. }
  151. static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
  152. long ret = 1;
  153. FILE *fp = (FILE *)b->ptr;
  154. FILE **fpp;
  155. char p[4];
  156. switch (cmd) {
  157. case BIO_CTRL_RESET:
  158. num = 0;
  159. OPENSSL_FALLTHROUGH;
  160. case BIO_C_FILE_SEEK:
  161. ret = (long)fseek(fp, num, 0);
  162. break;
  163. case BIO_CTRL_EOF:
  164. ret = (long)feof(fp);
  165. break;
  166. case BIO_C_FILE_TELL:
  167. case BIO_CTRL_INFO:
  168. ret = ftell(fp);
  169. break;
  170. case BIO_C_SET_FILE_PTR:
  171. file_free(b);
  172. b->shutdown = (int)num & BIO_CLOSE;
  173. b->ptr = ptr;
  174. b->init = 1;
  175. break;
  176. case BIO_C_SET_FILENAME:
  177. file_free(b);
  178. b->shutdown = (int)num & BIO_CLOSE;
  179. if (num & BIO_FP_APPEND) {
  180. if (num & BIO_FP_READ) {
  181. BUF_strlcpy(p, "a+", sizeof(p));
  182. } else {
  183. BUF_strlcpy(p, "a", sizeof(p));
  184. }
  185. } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
  186. BUF_strlcpy(p, "r+", sizeof(p));
  187. } else if (num & BIO_FP_WRITE) {
  188. BUF_strlcpy(p, "w", sizeof(p));
  189. } else if (num & BIO_FP_READ) {
  190. BUF_strlcpy(p, "r", sizeof(p));
  191. } else {
  192. OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE);
  193. ret = 0;
  194. break;
  195. }
  196. fp = fopen(ptr, p);
  197. if (fp == NULL) {
  198. OPENSSL_PUT_SYSTEM_ERROR();
  199. ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
  200. OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
  201. ret = 0;
  202. break;
  203. }
  204. b->ptr = fp;
  205. b->init = 1;
  206. break;
  207. case BIO_C_GET_FILE_PTR:
  208. // the ptr parameter is actually a FILE ** in this case.
  209. if (ptr != NULL) {
  210. fpp = (FILE **)ptr;
  211. *fpp = (FILE *)b->ptr;
  212. }
  213. break;
  214. case BIO_CTRL_GET_CLOSE:
  215. ret = (long)b->shutdown;
  216. break;
  217. case BIO_CTRL_SET_CLOSE:
  218. b->shutdown = (int)num;
  219. break;
  220. case BIO_CTRL_FLUSH:
  221. ret = 0 == fflush((FILE *)b->ptr);
  222. break;
  223. case BIO_CTRL_WPENDING:
  224. case BIO_CTRL_PENDING:
  225. default:
  226. ret = 0;
  227. break;
  228. }
  229. return ret;
  230. }
  231. static int file_gets(BIO *bp, char *buf, int size) {
  232. int ret = 0;
  233. if (size == 0) {
  234. return 0;
  235. }
  236. if (!fgets(buf, size, (FILE *)bp->ptr)) {
  237. buf[0] = 0;
  238. goto err;
  239. }
  240. ret = strlen(buf);
  241. err:
  242. return ret;
  243. }
  244. static const BIO_METHOD methods_filep = {
  245. BIO_TYPE_FILE, "FILE pointer",
  246. file_write, file_read,
  247. NULL /* puts */, file_gets,
  248. file_ctrl, file_new,
  249. file_free, NULL /* callback_ctrl */,
  250. };
  251. const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
  252. int BIO_get_fp(BIO *bio, FILE **out_file) {
  253. return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file);
  254. }
  255. int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
  256. return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file);
  257. }
  258. int BIO_read_filename(BIO *bio, const char *filename) {
  259. return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
  260. (char *)filename);
  261. }
  262. int BIO_write_filename(BIO *bio, const char *filename) {
  263. return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
  264. (char *)filename);
  265. }
  266. int BIO_append_filename(BIO *bio, const char *filename) {
  267. return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
  268. (char *)filename);
  269. }
  270. int BIO_rw_filename(BIO *bio, const char *filename) {
  271. return BIO_ctrl(bio, BIO_C_SET_FILENAME,
  272. BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename);
  273. }