bss_file.c 11 KB

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