CMS 3D CMS Logo

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