stream-open.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Copyright (C) 2004-2024 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #define _LARGEFILE_SOURCE
  23. #ifndef _FILE_OFFSET_BITS
  24. #define _FILE_OFFSET_BITS 64
  25. #endif
  26. #include "mupdf/fitz.h"
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #ifdef _WIN32
  31. #include <windows.h>
  32. #include <wchar.h>
  33. #else
  34. #include <unistd.h>
  35. #endif
  36. int
  37. fz_file_exists(fz_context *ctx, const char *path)
  38. {
  39. FILE *file;
  40. #ifdef _WIN32
  41. file = fz_fopen_utf8(path, "rb");
  42. #else
  43. file = fopen(path, "rb");
  44. #endif
  45. if (file)
  46. fclose(file);
  47. return !!file;
  48. }
  49. fz_stream *
  50. fz_new_stream(fz_context *ctx, void *state, fz_stream_next_fn *next, fz_stream_drop_fn *drop)
  51. {
  52. fz_stream *stm = NULL;
  53. fz_try(ctx)
  54. {
  55. stm = fz_malloc_struct(ctx, fz_stream);
  56. }
  57. fz_catch(ctx)
  58. {
  59. if (drop)
  60. drop(ctx, state);
  61. fz_rethrow(ctx);
  62. }
  63. stm->refs = 1;
  64. stm->error = 0;
  65. stm->eof = 0;
  66. stm->pos = 0;
  67. stm->bits = 0;
  68. stm->avail = 0;
  69. stm->rp = NULL;
  70. stm->wp = NULL;
  71. stm->state = state;
  72. stm->next = next;
  73. stm->drop = drop;
  74. stm->seek = NULL;
  75. return stm;
  76. }
  77. fz_stream *
  78. fz_keep_stream(fz_context *ctx, fz_stream *stm)
  79. {
  80. return fz_keep_imp(ctx, stm, &stm->refs);
  81. }
  82. void
  83. fz_drop_stream(fz_context *ctx, fz_stream *stm)
  84. {
  85. if (fz_drop_imp(ctx, stm, &stm->refs))
  86. {
  87. if (stm->drop)
  88. stm->drop(ctx, stm->state);
  89. fz_free(ctx, stm);
  90. }
  91. }
  92. /* File stream */
  93. // TODO: WIN32: HANDLE CreateFileW(), etc.
  94. // TODO: POSIX: int creat(), read(), write(), lseeko, etc.
  95. typedef struct
  96. {
  97. FILE *file;
  98. char *filename;
  99. #ifdef _WIN32
  100. wchar_t *filename_w;
  101. #endif
  102. int del_on_drop;
  103. unsigned char buffer[4096];
  104. } fz_file_stream;
  105. static int next_file(fz_context *ctx, fz_stream *stm, size_t n)
  106. {
  107. fz_file_stream *state = stm->state;
  108. /* n is only a hint, that we can safely ignore */
  109. n = fread(state->buffer, 1, sizeof(state->buffer), state->file);
  110. if (n < sizeof(state->buffer) && ferror(state->file))
  111. fz_throw(ctx, FZ_ERROR_SYSTEM, "read error: %s", strerror(errno));
  112. stm->rp = state->buffer;
  113. stm->wp = state->buffer + n;
  114. stm->pos += (int64_t)n;
  115. if (n == 0)
  116. return EOF;
  117. return *stm->rp++;
  118. }
  119. static void seek_file(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
  120. {
  121. fz_file_stream *state = stm->state;
  122. #ifdef _WIN32
  123. int64_t n = _fseeki64(state->file, offset, whence);
  124. #else
  125. int64_t n = fseeko(state->file, offset, whence);
  126. #endif
  127. if (n < 0)
  128. fz_throw(ctx, FZ_ERROR_SYSTEM, "cannot seek: %s", strerror(errno));
  129. #ifdef _WIN32
  130. stm->pos = _ftelli64(state->file);
  131. #else
  132. stm->pos = ftello(state->file);
  133. #endif
  134. stm->rp = state->buffer;
  135. stm->wp = state->buffer;
  136. }
  137. static void drop_file(fz_context *ctx, void *state_)
  138. {
  139. fz_file_stream *state = state_;
  140. if (state->filename && state->del_on_drop)
  141. {
  142. #ifdef _WIN32
  143. if (state->filename_w)
  144. _wunlink(state->filename_w);
  145. else
  146. #endif
  147. unlink(state->filename);
  148. }
  149. #ifdef _WIN32
  150. fz_free(ctx, state->filename_w);
  151. #endif
  152. fz_free(ctx, state->filename);
  153. fz_free(ctx, state);
  154. }
  155. static void close_and_drop_file(fz_context *ctx, void *state_)
  156. {
  157. fz_file_stream *state = state_;
  158. if (state)
  159. {
  160. int n = fclose(state->file);
  161. if (n < 0)
  162. fz_warn(ctx, "close error: %s", strerror(errno));
  163. drop_file(ctx, state_);
  164. }
  165. }
  166. static fz_stream *
  167. fz_open_file_ptr(fz_context *ctx, FILE *file, const char *name, int wide, int del_on_drop)
  168. {
  169. fz_stream *stm;
  170. fz_file_stream *state = NULL;
  171. fz_var(state);
  172. #ifndef _WIN32
  173. assert(!wide);
  174. #endif
  175. fz_try(ctx)
  176. {
  177. state = fz_malloc_struct(ctx, fz_file_stream);
  178. state->file = file;
  179. #ifdef _WIN32
  180. if (wide)
  181. {
  182. size_t z = wcslen((const wchar_t *)name)+1;
  183. state->filename_w = fz_malloc(ctx, z*2);
  184. memcpy(state->filename_w, name, z*2);
  185. state->filename = fz_utf8_from_wchar(ctx, (const wchar_t *)name);
  186. }
  187. else
  188. #endif
  189. state->filename = fz_strdup(ctx, name);
  190. state->del_on_drop = del_on_drop;
  191. stm = fz_new_stream(ctx, state, next_file, close_and_drop_file);
  192. stm->seek = seek_file;
  193. }
  194. fz_catch(ctx)
  195. {
  196. if (state == NULL && del_on_drop)
  197. {
  198. fclose(file);
  199. #ifdef _WIN32
  200. if (wide)
  201. _wunlink((const wchar_t *)name);
  202. else
  203. #endif
  204. unlink(name);
  205. }
  206. else
  207. close_and_drop_file(ctx, state);
  208. fz_rethrow(ctx);
  209. }
  210. return stm;
  211. }
  212. fz_stream *fz_open_file_ptr_no_close(fz_context *ctx, FILE *file)
  213. {
  214. fz_stream *stm;
  215. fz_file_stream *state = fz_malloc_struct(ctx, fz_file_stream);
  216. state->file = file;
  217. /* We don't own the file ptr. Ensure we don't close it */
  218. stm = fz_new_stream(ctx, state, next_file, drop_file);
  219. stm->seek = seek_file;
  220. return stm;
  221. }
  222. fz_stream *
  223. fz_open_file(fz_context *ctx, const char *name)
  224. {
  225. FILE *file;
  226. #ifdef _WIN32
  227. file = fz_fopen_utf8(name, "rb");
  228. #else
  229. file = fopen(name, "rb");
  230. #endif
  231. if (file == NULL)
  232. fz_throw(ctx, FZ_ERROR_SYSTEM, "cannot open %s: %s", name, strerror(errno));
  233. return fz_open_file_ptr(ctx, file, name, 0, 0);
  234. }
  235. fz_stream *
  236. fz_open_file_autodelete(fz_context *ctx, const char *name)
  237. {
  238. FILE *file;
  239. #ifdef _WIN32
  240. file = fz_fopen_utf8(name, "rb");
  241. #else
  242. file = fopen(name, "rb");
  243. #endif
  244. if (file == NULL)
  245. fz_throw(ctx, FZ_ERROR_SYSTEM, "cannot open %s: %s", name, strerror(errno));
  246. return fz_open_file_ptr(ctx, file, name, 0, 1);
  247. }
  248. fz_stream *
  249. fz_try_open_file(fz_context *ctx, const char *name)
  250. {
  251. FILE *file;
  252. #ifdef _WIN32
  253. file = fz_fopen_utf8(name, "rb");
  254. #else
  255. file = fopen(name, "rb");
  256. #endif
  257. if (file == NULL)
  258. return NULL;
  259. return fz_open_file_ptr(ctx, file, name, 0, 0);
  260. }
  261. #ifdef _WIN32
  262. fz_stream *
  263. fz_open_file_w(fz_context *ctx, const wchar_t *name)
  264. {
  265. FILE *file = _wfopen(name, L"rb");
  266. if (file == NULL)
  267. fz_throw(ctx, FZ_ERROR_SYSTEM, "cannot open file %ls: %s", name, strerror(errno));
  268. return fz_open_file_ptr(ctx, file, (const char *)name, 1, 0);
  269. }
  270. #endif
  271. const char *
  272. fz_stream_filename(fz_context *ctx, fz_stream *stm)
  273. {
  274. if (!stm || stm->next != next_file)
  275. return NULL;
  276. return ((fz_file_stream *)stm->state)->filename;
  277. }
  278. /* Memory stream */
  279. static int next_buffer(fz_context *ctx, fz_stream *stm, size_t max)
  280. {
  281. return EOF;
  282. }
  283. static void seek_buffer(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
  284. {
  285. int64_t pos = stm->pos - (stm->wp - stm->rp);
  286. /* Convert to absolute pos */
  287. if (whence == 1)
  288. {
  289. offset += pos; /* Was relative to current pos */
  290. }
  291. else if (whence == 2)
  292. {
  293. offset += stm->pos; /* Was relative to end */
  294. }
  295. if (offset < 0)
  296. offset = 0;
  297. if (offset > stm->pos)
  298. offset = stm->pos;
  299. stm->rp += (int)(offset - pos);
  300. }
  301. static void drop_buffer(fz_context *ctx, void *state_)
  302. {
  303. fz_buffer *state = (fz_buffer *)state_;
  304. fz_drop_buffer(ctx, state);
  305. }
  306. fz_stream *
  307. fz_open_buffer(fz_context *ctx, fz_buffer *buf)
  308. {
  309. fz_stream *stm;
  310. if (buf == NULL)
  311. return NULL;
  312. fz_keep_buffer(ctx, buf);
  313. stm = fz_new_stream(ctx, buf, next_buffer, drop_buffer);
  314. stm->seek = seek_buffer;
  315. stm->rp = buf->data;
  316. stm->wp = buf->data + buf->len;
  317. stm->pos = (int64_t)buf->len;
  318. return stm;
  319. }
  320. fz_stream *
  321. fz_open_memory(fz_context *ctx, const unsigned char *data, size_t len)
  322. {
  323. fz_stream *stm;
  324. stm = fz_new_stream(ctx, NULL, next_buffer, NULL);
  325. stm->seek = seek_buffer;
  326. stm->rp = (unsigned char *)data;
  327. stm->wp = (unsigned char *)data + len;
  328. stm->pos = (int64_t)len;
  329. return stm;
  330. }