FileIO.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SoftEther VPN Source Code
  2. // Mayaqua Kernel
  3. //
  4. // SoftEther VPN Server, Client and Bridge are free software under GPLv2.
  5. //
  6. // Copyright (c) 2012-2014 Daiyuu Nobori.
  7. // Copyright (c) 2012-2014 SoftEther VPN Project, University of Tsukuba, Japan.
  8. // Copyright (c) 2012-2014 SoftEther Corporation.
  9. //
  10. // All Rights Reserved.
  11. //
  12. // http://www.softether.org/
  13. //
  14. // Author: Daiyuu Nobori
  15. // Comments: Tetsuo Sugiyama, Ph.D.
  16. //
  17. //
  18. // This program is free software; you can redistribute it and/or
  19. // modify it under the terms of the GNU General Public License
  20. // version 2 as published by the Free Software Foundation.
  21. //
  22. // This program is distributed in the hope that it will be useful,
  23. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. // GNU General Public License for more details.
  26. //
  27. // You should have received a copy of the GNU General Public License version 2
  28. // along with this program; if not, write to the Free Software
  29. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. //
  31. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  34. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  35. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  36. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  37. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. //
  39. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  40. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  41. //
  42. //
  43. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  44. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  45. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  46. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  47. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  48. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  49. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  50. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  51. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  52. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  53. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  54. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  55. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  56. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  57. //
  58. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  59. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  60. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  61. // COUNTRIES MIGHT BE RESTRICTED.
  62. //
  63. //
  64. // DEAR SECURITY EXPERTS
  65. // ---------------------
  66. //
  67. // If you find a bug or a security vulnerability please kindly inform us
  68. // about the problem immediately so that we can fix the security problem
  69. // to protect a lot of users around the world as soon as possible.
  70. //
  71. // Our e-mail address for security reports is:
  72. // softether-vpn-security [at] softether.org
  73. //
  74. // Please note that the above e-mail address is not a technical support
  75. // inquiry address. If you need technical assistance, please visit
  76. // http://www.softether.org/ and ask your question on the users forum.
  77. //
  78. // Thank you for your cooperation.
  79. // FileIO.h
  80. // Header of FileIO.c
  81. #ifndef FILEIO_H
  82. #define FILEIO_H
  83. // Constant
  84. #define HAMCORE_DIR_NAME "hamcore"
  85. #define HAMCORE_FILE_NAME "hamcore.se2"
  86. #define HAMCORE_FILE_NAME_2 "_hamcore.se2"
  87. #define HAMCORE_TEXT_NAME "hamcore.txt"
  88. #define HAMCORE_HEADER_DATA "HamCore"
  89. #define HAMCORE_HEADER_SIZE 7
  90. #define HAMCORE_CACHE_EXPIRES (5 * 60 * 1000)
  91. // IO structure
  92. struct IO
  93. {
  94. char Name[MAX_SIZE];
  95. wchar_t NameW[MAX_SIZE];
  96. void *pData;
  97. bool WriteMode;
  98. bool HamMode;
  99. BUF *HamBuf;
  100. UINT64 SetUpdateTime, SetCreateTime;
  101. UINT64 GetUpdateTime, GetCreateTime, GetAccessTime;
  102. };
  103. // HC structure
  104. typedef struct HC
  105. {
  106. char *FileName; // File name
  107. UINT Size; // File size
  108. UINT SizeCompressed; // Compressed file size
  109. UINT Offset; // Offset
  110. void *Buffer; // Buffer
  111. UINT64 LastAccess; // Access Date
  112. } HC;
  113. // DIRENT structure
  114. struct DIRENT
  115. {
  116. bool Folder; // Folder
  117. char *FileName; // File name (ANSI)
  118. wchar_t *FileNameW; // File name (Unicode)
  119. UINT64 FileSize; // File size
  120. UINT64 CreateDate; // Creation Date
  121. UINT64 UpdateDate; // Updating date
  122. };
  123. // DIRLIST structure
  124. struct DIRLIST
  125. {
  126. UINT NumFiles; // Number of files
  127. struct DIRENT **File; // File array
  128. };
  129. // ZIP related structure
  130. #ifdef OS_WIN32
  131. #pragma pack(push, 1)
  132. #endif // OS_WIN32
  133. struct ZIP_DATA_HEADER
  134. {
  135. UINT Signature;
  136. USHORT NeedVer;
  137. USHORT Option;
  138. USHORT CompType;
  139. USHORT FileTime;
  140. USHORT FileDate;
  141. UINT Crc32;
  142. UINT CompSize;
  143. UINT UncompSize;
  144. USHORT FileNameLen;
  145. USHORT ExtraLen;
  146. } GCC_PACKED;
  147. struct ZIP_DATA_FOOTER
  148. {
  149. UINT Signature;
  150. UINT Crc32;
  151. UINT CompSize;
  152. UINT UncompSize;
  153. } GCC_PACKED;
  154. struct ZIP_DIR_HEADER
  155. {
  156. UINT Signature;
  157. USHORT MadeVer;
  158. USHORT NeedVer;
  159. USHORT Option;
  160. USHORT CompType;
  161. USHORT FileTime;
  162. USHORT FileDate;
  163. UINT Crc32;
  164. UINT CompSize;
  165. UINT UncompSize;
  166. USHORT FileNameLen;
  167. USHORT ExtraLen;
  168. USHORT CommentLen;
  169. USHORT DiskNum;
  170. USHORT InAttr;
  171. UINT OutAttr;
  172. UINT HeaderPos;
  173. } GCC_PACKED;
  174. struct ZIP_END_HEADER
  175. {
  176. UINT Signature;
  177. USHORT DiskNum;
  178. USHORT StartDiskNum;
  179. USHORT DiskDirEntry;
  180. USHORT DirEntry;
  181. UINT DirSize;
  182. UINT StartPos;
  183. USHORT CommentLen;
  184. } GCC_PACKED;
  185. #define ZIP_SIGNATURE 0x04034B50
  186. #define ZIP_SIGNATURE_END 0x06054B50
  187. #define ZIP_VERSION 10
  188. #define ZIP_VERSION_WITH_COMPRESS 20
  189. #ifdef OS_WIN32
  190. #pragma pack(pop)
  191. #endif // OS_WIN32
  192. struct ZIP_FILE
  193. {
  194. char Name[MAX_PATH];
  195. UINT Size;
  196. UINT64 DateTime;
  197. UINT Attributes;
  198. UINT CurrentSize;
  199. UINT CompressSize;
  200. UINT Crc32;
  201. UINT HeaderPos;
  202. };
  203. struct ZIP_PACKER
  204. {
  205. FIFO *Fifo;
  206. LIST *FileList;
  207. ZIP_FILE *CurrentFile;
  208. };
  209. struct ENUM_DIR_WITH_SUB_DATA
  210. {
  211. LIST *FileList;
  212. };
  213. void InitCrc32();
  214. UINT Crc32(void *buf, UINT pos, UINT len);
  215. UINT Crc32First(void *buf, UINT pos, UINT len);
  216. UINT Crc32Next(void *buf, UINT pos, UINT len, UINT last_crc32);
  217. UINT Crc32Finish(UINT last_crc32);
  218. void WriteZipDataHeader(ZIP_FILE *f, ZIP_DATA_HEADER *h, bool write_sizes);
  219. void WriteZipDataFooter(ZIP_FILE *f, ZIP_DATA_FOOTER *h);
  220. ZIP_PACKER *NewZipPacker();
  221. void FreeZipPacker(ZIP_PACKER *p);
  222. void ZipAddFileSimple(ZIP_PACKER *p, char *name, UINT64 dt, UINT attribute, void *data, UINT size);
  223. bool ZipAddRealFileW(ZIP_PACKER *p, char *name, UINT64 dt, UINT attribute, wchar_t *srcname);
  224. bool ZipAddRealFile(ZIP_PACKER *p, char *name, UINT64 dt, UINT attribute, char *srcname);
  225. void ZipAddFileStart(ZIP_PACKER *p, char *name, UINT size, UINT64 dt, UINT attribute);
  226. UINT ZipAddFileData(ZIP_PACKER *p, void *data, UINT pos, UINT len);
  227. void ZipAddFileFooter(ZIP_PACKER *p);
  228. FIFO *ZipFinish(ZIP_PACKER *p);
  229. bool ZipWriteW(ZIP_PACKER *p, wchar_t *name);
  230. bool DeleteDirInner(char *name);
  231. bool DeleteDirInnerW(wchar_t *name);
  232. bool DeleteDir(char *name);
  233. bool DeleteDirW(wchar_t *name);
  234. bool MakeDirInner(char *name);
  235. bool MakeDirInnerW(wchar_t *name);
  236. bool MakeDir(char *name);
  237. bool MakeDirW(wchar_t *name);
  238. bool MakeDirEx(char *name);
  239. bool MakeDirExW(wchar_t *name);
  240. bool FileDeleteInner(char *name);
  241. bool FileDeleteInnerW(wchar_t *name);
  242. bool FileDelete(char *name);
  243. bool FileDeleteW(wchar_t *name);
  244. bool FileSeek(IO *o, UINT mode, int offset);
  245. UINT FileSize(IO *o);
  246. UINT64 FileSize64(IO *o);
  247. UINT FileSizeEx(char *name);
  248. UINT FileSizeExW(wchar_t *name);
  249. bool FileRead(IO *o, void *buf, UINT size);
  250. bool FileWrite(IO *o, void *buf, UINT size);
  251. void FileFlush(IO *o);
  252. void FileClose(IO *o);
  253. void FileCloseEx(IO *o, bool no_flush);
  254. void FileCloseAndDelete(IO *o);
  255. IO *FileCreateInner(char *name);
  256. IO *FileCreateInnerW(wchar_t *name);
  257. IO *FileCreate(char *name);
  258. IO *FileCreateW(wchar_t *name);
  259. bool FileWriteAll(char *name, void *data, UINT size);
  260. bool FileWriteAllW(wchar_t *name, void *data, UINT size);
  261. IO *FileOpenInner(char *name, bool write_mode, bool read_lock);
  262. IO *FileOpenInnerW(wchar_t *name, bool write_mode, bool read_lock);
  263. IO *FileOpen(char *name, bool write_mode);
  264. IO *FileOpenW(wchar_t *name, bool write_mode);
  265. IO *FileOpenEx(char *name, bool write_mode, bool read_lock);
  266. IO *FileOpenExW(wchar_t *name, bool write_mode, bool read_lock);
  267. void ConvertPath(char *path);
  268. void ConvertPathW(wchar_t *path);
  269. bool FileRenameInner(char *old_name, char *new_name);
  270. bool FileRenameInnerW(wchar_t *old_name, wchar_t *new_name);
  271. bool FileRename(char *old_name, char *new_name);
  272. bool FileRenameW(wchar_t *old_name, wchar_t *new_name);
  273. void NormalizePath(char *dst, UINT size, char *src);
  274. void NormalizePathW(wchar_t *dst, UINT size, wchar_t *src);
  275. bool GetRelativePathW(wchar_t *dst, UINT size, wchar_t *fullpath, wchar_t *basepath);
  276. bool GetRelativePath(char *dst, UINT size, char *fullpath, char *basepath);
  277. TOKEN_LIST *ParseSplitedPath(char *path);
  278. UNI_TOKEN_LIST *ParseSplitedPathW(wchar_t *path);
  279. char *GetCurrentPathEnvStr();
  280. bool IsFileExistsInner(char *name);
  281. bool IsFileExistsInnerW(wchar_t *name);
  282. bool IsFileExists(char *name);
  283. bool IsFileExistsW(wchar_t *name);
  284. void InnerFilePath(char *dst, UINT size, char *src);
  285. void InnerFilePathW(wchar_t *dst, UINT size, wchar_t *src);
  286. void ConbinePath(char *dst, UINT size, char *dirname, char *filename);
  287. void ConbinePathW(wchar_t *dst, UINT size, wchar_t *dirname, wchar_t *filename);
  288. void CombinePath(char *dst, UINT size, char *dirname, char *filename);
  289. void CombinePathW(wchar_t *dst, UINT size, wchar_t *dirname, wchar_t *filename);
  290. void GetDirNameFromFilePath(char *dst, UINT size, char *filepath);
  291. void GetDirNameFromFilePathW(wchar_t *dst, UINT size, wchar_t *filepath);
  292. void GetFileNameFromFilePath(char *dst, UINT size, char *filepath);
  293. void GetFileNameFromFilePathW(wchar_t *dst, UINT size, wchar_t *filepath);
  294. void MakeSafeFileName(char *dst, UINT size, char *src);
  295. void MakeSafeFileNameW(wchar_t *dst, UINT size, wchar_t *src);
  296. void InitGetExeName(char *arg);
  297. void UnixGetExeNameW(wchar_t *name, UINT size, wchar_t *arg);
  298. void GetExeName(char *name, UINT size);
  299. void GetExeNameW(wchar_t *name, UINT size);
  300. void GetExeDir(char *name, UINT size);
  301. void GetExeDirW(wchar_t *name, UINT size);
  302. void BuildHamcore(char *dst_filename, char *src_dir, bool unix_only);
  303. int CompareHamcore(void *p1, void *p2);
  304. void InitHamcore();
  305. void FreeHamcore();
  306. BUF *ReadHamcore(char *name);
  307. BUF *ReadHamcoreW(wchar_t *filename);
  308. void SafeFileName(char *name);
  309. void SafeFileNameW(wchar_t *name);
  310. void UniSafeFileName(wchar_t *name);
  311. DIRLIST *EnumDir(char *dirname);
  312. DIRLIST *EnumDirW(wchar_t *dirname);
  313. DIRLIST *EnumDirEx(char *dirname, COMPARE *compare);
  314. DIRLIST *EnumDirExW(wchar_t *dirname, COMPARE *compare);
  315. UNI_TOKEN_LIST *EnumDirWithSubDirsW(wchar_t *dirname);
  316. TOKEN_LIST *EnumDirWithSubDirs(char *dirname);
  317. void EnumDirWithSubDirsMain(ENUM_DIR_WITH_SUB_DATA *d, wchar_t *dirname);
  318. void FreeDir(DIRLIST *d);
  319. int CompareDirListByName(void *p1, void *p2);
  320. bool GetDiskFree(char *path, UINT64 *free_size, UINT64 *used_size, UINT64 *total_size);
  321. bool GetDiskFreeW(wchar_t *path, UINT64 *free_size, UINT64 *used_size, UINT64 *total_size);
  322. void ConvertSafeFileName(char *dst, UINT size, char *src);
  323. void ConvertSafeFileNameW(wchar_t *dst, UINT size, wchar_t *src);
  324. bool FileReplaceRename(char *old_name, char *new_name);
  325. bool FileReplaceRenameW(wchar_t *old_name, wchar_t *new_name);
  326. bool IsFile(char *name);
  327. bool IsFileW(wchar_t *name);
  328. void GetCurrentDirW(wchar_t *name, UINT size);
  329. void GetCurrentDir(char *name, UINT size);
  330. bool SaveFileW(wchar_t *name, void *data, UINT size);
  331. bool SaveFile(char *name, void *data, UINT size);
  332. bool IsFileWriteLockedW(wchar_t *name);
  333. bool IsFileWriteLocked(char *name);
  334. bool IsInLines(BUF *buf, char *str, bool instr);
  335. bool IsInLinesFile(wchar_t *filename, char *str, bool instr);
  336. #endif // FILEIO_H
  337. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  338. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  339. // Join us: http://www.tsukuba.ac.jp/english/admission/