7zFile.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* 7zFile.c -- File IO
  2. 2008-11-22 : Igor Pavlov : Public domain */
  3. #include "7zFile.h"
  4. #ifndef USE_WINDOWS_FILE
  5. #include <errno.h>
  6. #endif
  7. #ifdef USE_WINDOWS_FILE
  8. /*
  9. ReadFile and WriteFile functions in Windows have BUG:
  10. If you Read or Write 64MB or more (probably min_failure_size = 64MB - 32KB + 1)
  11. from/to Network file, it returns ERROR_NO_SYSTEM_RESOURCES
  12. (Insufficient system resources exist to complete the requested service).
  13. Probably in some version of Windows there are problems with other sizes:
  14. for 32 MB (maybe also for 16 MB).
  15. And message can be "Network connection was lost"
  16. */
  17. #define kChunkSizeMax (1 << 22)
  18. #endif
  19. void File_Construct(CSzFile *p)
  20. {
  21. #ifdef USE_WINDOWS_FILE
  22. p->handle = INVALID_HANDLE_VALUE;
  23. #else
  24. p->file = NULL;
  25. #endif
  26. }
  27. static WRes File_Open(CSzFile *p, const char *name, int writeMode)
  28. {
  29. #ifdef USE_WINDOWS_FILE
  30. p->handle = CreateFileA(name,
  31. writeMode ? GENERIC_WRITE : GENERIC_READ,
  32. FILE_SHARE_READ, NULL,
  33. writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
  34. FILE_ATTRIBUTE_NORMAL, NULL);
  35. return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
  36. #else
  37. p->file = fopen(name, writeMode ? "wb+" : "rb");
  38. return (p->file != 0) ? 0 : errno;
  39. #endif
  40. }
  41. WRes InFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 0); }
  42. WRes OutFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 1); }
  43. WRes File_Close(CSzFile *p)
  44. {
  45. #ifdef USE_WINDOWS_FILE
  46. if (p->handle != INVALID_HANDLE_VALUE)
  47. {
  48. if (!CloseHandle(p->handle))
  49. return GetLastError();
  50. p->handle = INVALID_HANDLE_VALUE;
  51. }
  52. #else
  53. if (p->file != NULL)
  54. {
  55. int res = fclose(p->file);
  56. if (res != 0)
  57. return res;
  58. p->file = NULL;
  59. }
  60. #endif
  61. return 0;
  62. }
  63. WRes File_Read(CSzFile *p, void *data, size_t *size)
  64. {
  65. size_t originalSize = *size;
  66. if (originalSize == 0)
  67. return 0;
  68. #ifdef USE_WINDOWS_FILE
  69. *size = 0;
  70. do
  71. {
  72. DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
  73. DWORD processed = 0;
  74. BOOL res = ReadFile(p->handle, data, curSize, &processed, NULL);
  75. data = (void *)((Byte *)data + processed);
  76. originalSize -= processed;
  77. *size += processed;
  78. if (!res)
  79. return GetLastError();
  80. if (processed == 0)
  81. break;
  82. }
  83. while (originalSize > 0);
  84. return 0;
  85. #else
  86. *size = fread(data, 1, originalSize, p->file);
  87. if (*size == originalSize)
  88. return 0;
  89. return ferror(p->file);
  90. #endif
  91. }
  92. WRes File_Write(CSzFile *p, const void *data, size_t *size)
  93. {
  94. size_t originalSize = *size;
  95. if (originalSize == 0)
  96. return 0;
  97. #ifdef USE_WINDOWS_FILE
  98. *size = 0;
  99. do
  100. {
  101. DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
  102. DWORD processed = 0;
  103. BOOL res = WriteFile(p->handle, data, curSize, &processed, NULL);
  104. data = (void *)((Byte *)data + processed);
  105. originalSize -= processed;
  106. *size += processed;
  107. if (!res)
  108. return GetLastError();
  109. if (processed == 0)
  110. break;
  111. }
  112. while (originalSize > 0);
  113. return 0;
  114. #else
  115. *size = fwrite(data, 1, originalSize, p->file);
  116. if (*size == originalSize)
  117. return 0;
  118. return ferror(p->file);
  119. #endif
  120. }
  121. WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin)
  122. {
  123. #ifdef USE_WINDOWS_FILE
  124. LARGE_INTEGER value;
  125. DWORD moveMethod;
  126. value.LowPart = (DWORD)*pos;
  127. value.HighPart = (LONG)((UInt64)*pos >> 16 >> 16); /* for case when UInt64 is 32-bit only */
  128. switch (origin)
  129. {
  130. case SZ_SEEK_SET: moveMethod = FILE_BEGIN; break;
  131. case SZ_SEEK_CUR: moveMethod = FILE_CURRENT; break;
  132. case SZ_SEEK_END: moveMethod = FILE_END; break;
  133. default: return ERROR_INVALID_PARAMETER;
  134. }
  135. value.LowPart = SetFilePointer(p->handle, value.LowPart, &value.HighPart, moveMethod);
  136. if (value.LowPart == 0xFFFFFFFF)
  137. {
  138. WRes res = GetLastError();
  139. if (res != NO_ERROR)
  140. return res;
  141. }
  142. *pos = ((Int64)value.HighPart << 32) | value.LowPart;
  143. return 0;
  144. #else
  145. int moveMethod;
  146. int res;
  147. switch (origin)
  148. {
  149. case SZ_SEEK_SET: moveMethod = SEEK_SET; break;
  150. case SZ_SEEK_CUR: moveMethod = SEEK_CUR; break;
  151. case SZ_SEEK_END: moveMethod = SEEK_END; break;
  152. default: return 1;
  153. }
  154. res = fseek(p->file, (long)*pos, moveMethod);
  155. *pos = ftell(p->file);
  156. return res;
  157. #endif
  158. }
  159. WRes File_GetLength(CSzFile *p, UInt64 *length)
  160. {
  161. #ifdef USE_WINDOWS_FILE
  162. DWORD sizeHigh;
  163. DWORD sizeLow = GetFileSize(p->handle, &sizeHigh);
  164. if (sizeLow == 0xFFFFFFFF)
  165. {
  166. DWORD res = GetLastError();
  167. if (res != NO_ERROR)
  168. return res;
  169. }
  170. *length = (((UInt64)sizeHigh) << 32) + sizeLow;
  171. return 0;
  172. #else
  173. long pos = ftell(p->file);
  174. int res = fseek(p->file, 0, SEEK_END);
  175. *length = ftell(p->file);
  176. fseek(p->file, pos, SEEK_SET);
  177. return res;
  178. #endif
  179. }
  180. /* ---------- FileSeqInStream ---------- */
  181. static SRes FileSeqInStream_Read(void *pp, void *buf, size_t *size)
  182. {
  183. CFileSeqInStream *p = (CFileSeqInStream *)pp;
  184. return File_Read(&p->file, buf, size) == 0 ? SZ_OK : SZ_ERROR_READ;
  185. }
  186. void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
  187. {
  188. p->s.Read = FileSeqInStream_Read;
  189. }
  190. /* ---------- FileInStream ---------- */
  191. static SRes FileInStream_Read(void *pp, void *buf, size_t *size)
  192. {
  193. CFileInStream *p = (CFileInStream *)pp;
  194. return (File_Read(&p->file, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ;
  195. }
  196. static SRes FileInStream_Seek(void *pp, Int64 *pos, ESzSeek origin)
  197. {
  198. CFileInStream *p = (CFileInStream *)pp;
  199. return File_Seek(&p->file, pos, origin);
  200. }
  201. void FileInStream_CreateVTable(CFileInStream *p)
  202. {
  203. p->s.Read = FileInStream_Read;
  204. p->s.Seek = FileInStream_Seek;
  205. }
  206. /* ---------- FileOutStream ---------- */
  207. static size_t FileOutStream_Write(void *pp, const void *data, size_t size)
  208. {
  209. CFileOutStream *p = (CFileOutStream *)pp;
  210. File_Write(&p->file, data, &size);
  211. return size;
  212. }
  213. void FileOutStream_CreateVTable(CFileOutStream *p)
  214. {
  215. p->s.Write = FileOutStream_Write;
  216. }