2
0

FileStream.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "StdInc.h"
  2. #include "FileStream.h"
  3. #ifdef USE_SYSTEM_MINIZIP
  4. #include <minizip/unzip.h>
  5. #else
  6. #include "../minizip/unzip.h"
  7. #endif
  8. #include <cstdio>
  9. ///copied from ioapi.c due to linker issues on MSVS
  10. #include "../minizip/ioapi.h"
  11. #if defined(__APPLE__) || defined(IOAPI_NO_64)
  12. // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
  13. #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
  14. #define FTELLO_FUNC(stream) ftello(stream)
  15. #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
  16. #else
  17. #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
  18. #define FTELLO_FUNC(stream) ftello64(stream)
  19. #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
  20. #endif
  21. voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
  22. {
  23. if (pfilefunc->zfile_func64.zopen64_file != NULL)
  24. return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
  25. else
  26. {
  27. return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
  28. }
  29. }
  30. long call_zseek64(const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, ZPOS64_T offset, int origin)
  31. {
  32. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  33. return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque, filestream, offset, origin);
  34. else
  35. {
  36. uLong offsetTruncated = (uLong)offset;
  37. if (offsetTruncated != offset)
  38. return -1;
  39. else
  40. return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque, filestream, offsetTruncated, origin);
  41. }
  42. }
  43. ZPOS64_T call_ztell64(const zlib_filefunc64_32_def* pfilefunc, voidpf filestream)
  44. {
  45. if (pfilefunc->zfile_func64.zseek64_file != NULL)
  46. return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque, filestream);
  47. else
  48. {
  49. uLong tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque, filestream);
  50. if ((tell_uLong) == MAXU32)
  51. return (ZPOS64_T)-1;
  52. else
  53. return tell_uLong;
  54. }
  55. }
  56. static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
  57. static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  58. static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
  59. static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
  60. static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  61. static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
  62. static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
  63. static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
  64. {
  65. FILE* file = NULL;
  66. const char* mode_fopen = NULL;
  67. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  68. mode_fopen = "rb";
  69. else
  70. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  71. mode_fopen = "r+b";
  72. else
  73. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  74. mode_fopen = "wb";
  75. if ((filename != NULL) && (mode_fopen != NULL))
  76. file = fopen(filename, mode_fopen);
  77. return file;
  78. }
  79. static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode)
  80. {
  81. FILE* file = NULL;
  82. const char* mode_fopen = NULL;
  83. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  84. mode_fopen = "rb";
  85. else
  86. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  87. mode_fopen = "r+b";
  88. else
  89. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  90. mode_fopen = "wb";
  91. if ((filename != NULL) && (mode_fopen != NULL))
  92. file = FOPEN_FUNC((const char*)filename, mode_fopen);
  93. return file;
  94. }
  95. static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uLong size)
  96. {
  97. uLong ret;
  98. ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
  99. return ret;
  100. }
  101. static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size)
  102. {
  103. uLong ret;
  104. ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
  105. return ret;
  106. }
  107. static long ZCALLBACK ftell_file_func(voidpf opaque, voidpf stream)
  108. {
  109. long ret;
  110. ret = ftell((FILE *)stream);
  111. return ret;
  112. }
  113. static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream)
  114. {
  115. ZPOS64_T ret;
  116. ret = FTELLO_FUNC((FILE *)stream);
  117. return ret;
  118. }
  119. static long ZCALLBACK fseek_file_func(voidpf opaque, voidpf stream, uLong offset, int origin)
  120. {
  121. int fseek_origin = 0;
  122. long ret;
  123. switch (origin)
  124. {
  125. case ZLIB_FILEFUNC_SEEK_CUR:
  126. fseek_origin = SEEK_CUR;
  127. break;
  128. case ZLIB_FILEFUNC_SEEK_END:
  129. fseek_origin = SEEK_END;
  130. break;
  131. case ZLIB_FILEFUNC_SEEK_SET:
  132. fseek_origin = SEEK_SET;
  133. break;
  134. default: return -1;
  135. }
  136. ret = 0;
  137. if (fseek((FILE *)stream, offset, fseek_origin) != 0)
  138. ret = -1;
  139. return ret;
  140. }
  141. static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  142. {
  143. int fseek_origin = 0;
  144. long ret;
  145. switch (origin)
  146. {
  147. case ZLIB_FILEFUNC_SEEK_CUR:
  148. fseek_origin = SEEK_CUR;
  149. break;
  150. case ZLIB_FILEFUNC_SEEK_END:
  151. fseek_origin = SEEK_END;
  152. break;
  153. case ZLIB_FILEFUNC_SEEK_SET:
  154. fseek_origin = SEEK_SET;
  155. break;
  156. default: return -1;
  157. }
  158. ret = 0;
  159. if (FSEEKO_FUNC((FILE *)stream, offset, fseek_origin) != 0)
  160. ret = -1;
  161. return ret;
  162. }
  163. static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream)
  164. {
  165. int ret;
  166. ret = fclose((FILE *)stream);
  167. return ret;
  168. }
  169. static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream)
  170. {
  171. int ret;
  172. ret = ferror((FILE *)stream);
  173. return ret;
  174. }
  175. ///end of ioapi.c
  176. //extern MINIZIP_EXPORT void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def);
  177. #ifdef VCMI_WINDOWS
  178. #ifndef _CRT_SECURE_NO_WARNINGS
  179. #define _CRT_SECURE_NO_WARNINGS
  180. #endif
  181. #include <cwchar>
  182. #define CHAR_LITERAL(s) L##s
  183. using CharType = wchar_t;
  184. #else
  185. #define CHAR_LITERAL(s) s
  186. using CharType = char;
  187. #endif
  188. inline FILE* do_open(const CharType* name, const CharType* mode)
  189. {
  190. #ifdef VCMI_WINDOWS
  191. return _wfopen(name, mode);
  192. #else
  193. return std::fopen(name, mode);
  194. #endif
  195. }
  196. #define GETFILE static_cast<std::FILE*>(filePtr)
  197. voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
  198. {
  199. const CharType* mode_fopen = [mode]() -> const CharType*
  200. {
  201. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  202. return CHAR_LITERAL("rb");
  203. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  204. return CHAR_LITERAL("r+b");
  205. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  206. return CHAR_LITERAL("wb");
  207. return nullptr;
  208. }();
  209. if (filename != nullptr && mode_fopen != nullptr)
  210. return do_open(static_cast<const CharType*>(filename), mode_fopen);
  211. else
  212. return nullptr;
  213. }
  214. void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def)
  215. {
  216. pzlib_filefunc_def->zopen64_file = fopen64_file_func;
  217. pzlib_filefunc_def->zread_file = fread_file_func;
  218. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  219. pzlib_filefunc_def->ztell64_file = ftell64_file_func;
  220. pzlib_filefunc_def->zseek64_file = fseek64_file_func;
  221. pzlib_filefunc_def->zclose_file = fclose_file_func;
  222. pzlib_filefunc_def->zerror_file = ferror_file_func;
  223. pzlib_filefunc_def->opaque = NULL;
  224. }
  225. zlib_filefunc64_def* FileStream::GetMinizipFilefunc()
  226. {
  227. static zlib_filefunc64_def MinizipFilefunc;
  228. static bool initialized = false;
  229. if (!initialized)
  230. {
  231. fill_fopen64_filefunc(&MinizipFilefunc);
  232. MinizipFilefunc.zopen64_file = &MinizipOpenFunc;
  233. initialized = true;
  234. }
  235. return &MinizipFilefunc;
  236. }
  237. template class boost::iostreams::stream<FileBuf>;
  238. /*static*/
  239. bool FileStream::CreateFile(const boost::filesystem::path& filename)
  240. {
  241. FILE* f = do_open(filename.c_str(), CHAR_LITERAL("wb"));
  242. bool result = (f != nullptr);
  243. fclose(f);
  244. return result;
  245. }
  246. FileBuf::FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode)
  247. {
  248. auto openmode = [mode]() -> std::basic_string<CharType>
  249. {
  250. using namespace std;
  251. switch (mode & (~ios_base::ate & ~ios_base::binary))
  252. {
  253. case (ios_base::in):
  254. return CHAR_LITERAL("r");
  255. case (ios_base::out):
  256. case (ios_base::out | ios_base::trunc):
  257. return CHAR_LITERAL("w");
  258. case (ios_base::app):
  259. case (ios_base::out | ios_base::app):
  260. return CHAR_LITERAL("a");
  261. case (ios_base::out | ios_base::in):
  262. return CHAR_LITERAL("r+");
  263. case (ios_base::out | ios_base::in | ios_base::trunc):
  264. return CHAR_LITERAL("w+");
  265. case (ios_base::out | ios_base::in | ios_base::app):
  266. case (ios_base::in | ios_base::app):
  267. return CHAR_LITERAL("a+");
  268. default:
  269. throw std::ios_base::failure("invalid open mode");
  270. }
  271. }();
  272. if (mode & std::ios_base::binary)
  273. openmode += CHAR_LITERAL('b');
  274. filePtr = do_open(filename.c_str(), openmode.c_str());
  275. if (filePtr == nullptr)
  276. throw std::ios_base::failure("could not open file");
  277. if (mode & std::ios_base::ate) {
  278. if (std::fseek(GETFILE, 0, SEEK_END)) {
  279. fclose(GETFILE);
  280. throw std::ios_base::failure("could not open file");
  281. }
  282. }
  283. }
  284. void FileBuf::close()
  285. {
  286. std::fclose(GETFILE);
  287. }
  288. std::streamsize FileBuf::read(char* s, std::streamsize n)
  289. {
  290. return static_cast<std::streamsize>(std::fread(s, 1, n, GETFILE));
  291. }
  292. std::streamsize FileBuf::write(const char* s, std::streamsize n)
  293. {
  294. return static_cast<std::streamsize>(std::fwrite(s, 1, n, GETFILE));
  295. }
  296. std::streamoff FileBuf::seek(std::streamoff off, std::ios_base::seekdir way)
  297. {
  298. const auto src = [way]() -> int
  299. {
  300. switch(way)
  301. {
  302. case std::ios_base::beg:
  303. return SEEK_SET;
  304. case std::ios_base::cur:
  305. return SEEK_CUR;
  306. case std::ios_base::end:
  307. return SEEK_END;
  308. default:
  309. throw std::ios_base::failure("bad seek direction");
  310. }
  311. }();
  312. if(std::fseek(GETFILE, off, src))
  313. throw std::ios_base::failure("bad seek offset");
  314. return static_cast<std::streamsize>(std::ftell(GETFILE));
  315. }