bss_file.c 11 KB

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