FileStream.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
  57. static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
  58. static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
  59. static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
  60. static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
  61. static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
  62. static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode)
  63. {
  64. FILE* file = NULL;
  65. const char* mode_fopen = NULL;
  66. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  67. mode_fopen = "rb";
  68. else
  69. if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  70. mode_fopen = "r+b";
  71. else
  72. if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  73. mode_fopen = "wb";
  74. if ((filename != NULL) && (mode_fopen != NULL))
  75. file = FOPEN_FUNC((const char*)filename, mode_fopen);
  76. return file;
  77. }
  78. static uLong ZCALLBACK fread_file_func(voidpf opaque, voidpf stream, void* buf, uLong size)
  79. {
  80. uLong ret;
  81. ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
  82. return ret;
  83. }
  84. static uLong ZCALLBACK fwrite_file_func(voidpf opaque, voidpf stream, const void* buf, uLong size)
  85. {
  86. uLong ret;
  87. ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
  88. return ret;
  89. }
  90. static ZPOS64_T ZCALLBACK ftell64_file_func(voidpf opaque, voidpf stream)
  91. {
  92. ZPOS64_T ret;
  93. ret = FTELLO_FUNC((FILE *)stream);
  94. return ret;
  95. }
  96. static long ZCALLBACK fseek64_file_func(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
  97. {
  98. int fseek_origin = 0;
  99. long ret;
  100. switch (origin)
  101. {
  102. case ZLIB_FILEFUNC_SEEK_CUR:
  103. fseek_origin = SEEK_CUR;
  104. break;
  105. case ZLIB_FILEFUNC_SEEK_END:
  106. fseek_origin = SEEK_END;
  107. break;
  108. case ZLIB_FILEFUNC_SEEK_SET:
  109. fseek_origin = SEEK_SET;
  110. break;
  111. default: return -1;
  112. }
  113. ret = 0;
  114. if (FSEEKO_FUNC((FILE *)stream, offset, fseek_origin) != 0)
  115. ret = -1;
  116. return ret;
  117. }
  118. static int ZCALLBACK fclose_file_func(voidpf opaque, voidpf stream)
  119. {
  120. int ret;
  121. ret = fclose((FILE *)stream);
  122. return ret;
  123. }
  124. static int ZCALLBACK ferror_file_func(voidpf opaque, voidpf stream)
  125. {
  126. int ret;
  127. ret = ferror((FILE *)stream);
  128. return ret;
  129. }
  130. ///end of ioapi.c
  131. //extern MINIZIP_EXPORT void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def);
  132. #ifdef VCMI_WINDOWS
  133. #ifndef _CRT_SECURE_NO_WARNINGS
  134. #define _CRT_SECURE_NO_WARNINGS
  135. #endif
  136. #include <cwchar>
  137. #define CHAR_LITERAL(s) L##s
  138. using CharType = wchar_t;
  139. #else
  140. #define CHAR_LITERAL(s) s
  141. using CharType = char;
  142. #endif
  143. inline FILE* do_open(const CharType* name, const CharType* mode)
  144. {
  145. #ifdef VCMI_WINDOWS
  146. return _wfopen(name, mode);
  147. #else
  148. return std::fopen(name, mode);
  149. #endif
  150. }
  151. #define GETFILE static_cast<std::FILE*>(filePtr)
  152. voidpf ZCALLBACK MinizipOpenFunc(voidpf opaque, const void* filename, int mode)
  153. {
  154. const CharType* mode_fopen = [mode]() -> const CharType*
  155. {
  156. if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
  157. return CHAR_LITERAL("rb");
  158. else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
  159. return CHAR_LITERAL("r+b");
  160. else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
  161. return CHAR_LITERAL("wb");
  162. return nullptr;
  163. }();
  164. if (filename != nullptr && mode_fopen != nullptr)
  165. return do_open(static_cast<const CharType*>(filename), mode_fopen);
  166. else
  167. return nullptr;
  168. }
  169. void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def)
  170. {
  171. pzlib_filefunc_def->zopen64_file = fopen64_file_func;
  172. pzlib_filefunc_def->zread_file = fread_file_func;
  173. pzlib_filefunc_def->zwrite_file = fwrite_file_func;
  174. pzlib_filefunc_def->ztell64_file = ftell64_file_func;
  175. pzlib_filefunc_def->zseek64_file = fseek64_file_func;
  176. pzlib_filefunc_def->zclose_file = fclose_file_func;
  177. pzlib_filefunc_def->zerror_file = ferror_file_func;
  178. pzlib_filefunc_def->opaque = NULL;
  179. }
  180. zlib_filefunc64_def* FileStream::GetMinizipFilefunc()
  181. {
  182. static zlib_filefunc64_def MinizipFilefunc;
  183. static bool initialized = false;
  184. if (!initialized)
  185. {
  186. fill_fopen64_filefunc(&MinizipFilefunc);
  187. MinizipFilefunc.zopen64_file = &MinizipOpenFunc;
  188. initialized = true;
  189. }
  190. return &MinizipFilefunc;
  191. }
  192. template struct boost::iostreams::stream<FileBuf>;
  193. /*static*/
  194. bool FileStream::CreateFile(const boost::filesystem::path& filename)
  195. {
  196. FILE* f = do_open(filename.c_str(), CHAR_LITERAL("wb"));
  197. bool result = (f != nullptr);
  198. if(result)
  199. fclose(f);
  200. return result;
  201. }
  202. FileBuf::FileBuf(const boost::filesystem::path& filename, std::ios_base::openmode mode)
  203. {
  204. auto openmode = [mode]() -> std::basic_string<CharType>
  205. {
  206. using namespace std;
  207. switch (mode & (~ios_base::ate & ~ios_base::binary))
  208. {
  209. case (ios_base::in):
  210. return CHAR_LITERAL("r");
  211. case (ios_base::out):
  212. case (ios_base::out | ios_base::trunc):
  213. return CHAR_LITERAL("w");
  214. case (ios_base::app):
  215. case (ios_base::out | ios_base::app):
  216. return CHAR_LITERAL("a");
  217. case (ios_base::out | ios_base::in):
  218. return CHAR_LITERAL("r+");
  219. case (ios_base::out | ios_base::in | ios_base::trunc):
  220. return CHAR_LITERAL("w+");
  221. case (ios_base::out | ios_base::in | ios_base::app):
  222. case (ios_base::in | ios_base::app):
  223. return CHAR_LITERAL("a+");
  224. default:
  225. throw std::ios_base::failure("invalid open mode");
  226. }
  227. }();
  228. if (mode & std::ios_base::binary)
  229. openmode += CHAR_LITERAL('b');
  230. filePtr = do_open(filename.c_str(), openmode.c_str());
  231. if (filePtr == nullptr)
  232. throw std::ios_base::failure("could not open file");
  233. if (mode & std::ios_base::ate) {
  234. if (std::fseek(GETFILE, 0, SEEK_END)) {
  235. fclose(GETFILE);
  236. throw std::ios_base::failure("could not open file");
  237. }
  238. }
  239. }
  240. void FileBuf::close()
  241. {
  242. std::fclose(GETFILE);
  243. }
  244. std::streamsize FileBuf::read(char* s, std::streamsize n)
  245. {
  246. return static_cast<std::streamsize>(std::fread(s, 1, n, GETFILE));
  247. }
  248. std::streamsize FileBuf::write(const char* s, std::streamsize n)
  249. {
  250. return static_cast<std::streamsize>(std::fwrite(s, 1, n, GETFILE));
  251. }
  252. std::streamoff FileBuf::seek(std::streamoff off, std::ios_base::seekdir way)
  253. {
  254. const auto src = [way]() -> int
  255. {
  256. switch(way)
  257. {
  258. case std::ios_base::beg:
  259. return SEEK_SET;
  260. case std::ios_base::cur:
  261. return SEEK_CUR;
  262. case std::ios_base::end:
  263. return SEEK_END;
  264. default:
  265. throw std::ios_base::failure("bad seek direction");
  266. }
  267. }();
  268. if(std::fseek(GETFILE, off, src))
  269. throw std::ios_base::failure("bad seek offset");
  270. return static_cast<std::streamsize>(std::ftell(GETFILE));
  271. }