2014-09-02 14:13:04 +00:00
|
|
|
/* 7zFile.c -- File IO
|
2014-10-16 17:03:28 +00:00
|
|
|
2009-11-24 : Igor Pavlov : Public domain */
|
2014-09-02 14:13:04 +00:00
|
|
|
|
2014-10-20 02:28:20 +00:00
|
|
|
#include <stdint.h>
|
2014-09-02 14:13:04 +00:00
|
|
|
#include "7zFile.h"
|
|
|
|
|
|
|
|
#ifndef USE_WINDOWS_FILE
|
|
|
|
|
|
|
|
#ifndef UNDER_CE
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
ReadFile and WriteFile functions in Windows have BUG:
|
|
|
|
If you Read or Write 64MB or more (probably min_failure_size = 64MB - 32KB + 1)
|
|
|
|
from/to Network file, it returns ERROR_NO_SYSTEM_RESOURCES
|
|
|
|
(Insufficient system resources exist to complete the requested service).
|
|
|
|
Probably in some version of Windows there are problems with other sizes:
|
|
|
|
for 32 MB (maybe also for 16 MB).
|
|
|
|
And message can be "Network connection was lost"
|
2014-10-16 17:03:28 +00:00
|
|
|
*/
|
2014-09-02 14:13:04 +00:00
|
|
|
|
|
|
|
#define kChunkSizeMax (1 << 22)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void File_Construct(CSzFile *p)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
p->handle = INVALID_HANDLE_VALUE;
|
|
|
|
#else
|
|
|
|
p->file = NULL;
|
|
|
|
#endif
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE)
|
|
|
|
static WRes File_Open(CSzFile *p, const char *name, int writeMode)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
p->handle = CreateFileA(name,
|
|
|
|
writeMode ? GENERIC_WRITE : GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, NULL,
|
|
|
|
writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
|
|
|
|
#else
|
|
|
|
p->file = fopen(name, writeMode ? "wb+" : "rb");
|
|
|
|
return (p->file != 0) ? 0 :
|
|
|
|
#ifdef UNDER_CE
|
|
|
|
2; /* ENOENT */
|
|
|
|
#else
|
|
|
|
errno;
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WRes InFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 0); }
|
|
|
|
WRes OutFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 1); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
static WRes File_OpenW(CSzFile *p, const WCHAR *name, int writeMode)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
p->handle = CreateFileW(name,
|
|
|
|
writeMode ? GENERIC_WRITE : GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, NULL,
|
|
|
|
writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
WRes InFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 0); }
|
|
|
|
WRes OutFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 1); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
WRes File_Close(CSzFile *p)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
if (p->handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if (!CloseHandle(p->handle))
|
|
|
|
return GetLastError();
|
|
|
|
p->handle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (p->file != NULL)
|
|
|
|
{
|
|
|
|
int res = fclose(p->file);
|
|
|
|
if (res != 0)
|
|
|
|
return res;
|
|
|
|
p->file = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WRes File_Read(CSzFile *p, void *data, size_t *size)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
size_t originalSize = *size;
|
|
|
|
if (originalSize == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
|
|
|
|
DWORD processed = 0;
|
|
|
|
BOOL res = ReadFile(p->handle, data, curSize, &processed, NULL);
|
2014-10-22 02:28:26 +00:00
|
|
|
data = (void *)((uint8_t*)data + processed);
|
2014-10-16 17:03:28 +00:00
|
|
|
originalSize -= processed;
|
|
|
|
*size += processed;
|
|
|
|
if (!res)
|
|
|
|
return GetLastError();
|
|
|
|
if (processed == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (originalSize > 0);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
*size = fread(data, 1, originalSize, p->file);
|
|
|
|
if (*size == originalSize)
|
|
|
|
return 0;
|
|
|
|
return ferror(p->file);
|
|
|
|
|
|
|
|
#endif
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WRes File_Write(CSzFile *p, const void *data, size_t *size)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
size_t originalSize = *size;
|
|
|
|
if (originalSize == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
|
|
|
|
DWORD processed = 0;
|
|
|
|
BOOL res = WriteFile(p->handle, data, curSize, &processed, NULL);
|
2014-10-22 02:28:26 +00:00
|
|
|
data = (void *)((uint8_t*)data + processed);
|
2014-10-16 17:03:28 +00:00
|
|
|
originalSize -= processed;
|
|
|
|
*size += processed;
|
|
|
|
if (!res)
|
|
|
|
return GetLastError();
|
|
|
|
if (processed == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (originalSize > 0);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
*size = fwrite(data, 1, originalSize, p->file);
|
|
|
|
if (*size == originalSize)
|
|
|
|
return 0;
|
|
|
|
return ferror(p->file);
|
|
|
|
|
|
|
|
#endif
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 02:28:20 +00:00
|
|
|
WRes File_Seek(CSzFile *p, int64_t *pos, ESzSeek origin)
|
2014-09-02 14:13:04 +00:00
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
|
|
|
|
LARGE_INTEGER value;
|
|
|
|
DWORD moveMethod;
|
|
|
|
value.LowPart = (DWORD)*pos;
|
2014-10-20 02:28:20 +00:00
|
|
|
value.HighPart = (LONG)((uint64_t)*pos >> 16 >> 16); /* for case when uint64_t is 32-bit only */
|
2014-10-16 17:03:28 +00:00
|
|
|
switch (origin)
|
|
|
|
{
|
|
|
|
case SZ_SEEK_SET: moveMethod = FILE_BEGIN; break;
|
|
|
|
case SZ_SEEK_CUR: moveMethod = FILE_CURRENT; break;
|
|
|
|
case SZ_SEEK_END: moveMethod = FILE_END; break;
|
|
|
|
default: return ERROR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
value.LowPart = SetFilePointer(p->handle, value.LowPart, &value.HighPart, moveMethod);
|
|
|
|
if (value.LowPart == 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
WRes res = GetLastError();
|
|
|
|
if (res != NO_ERROR)
|
|
|
|
return res;
|
|
|
|
}
|
2014-10-20 02:28:20 +00:00
|
|
|
*pos = ((int64_t)value.HighPart << 32) | value.LowPart;
|
2014-10-16 17:03:28 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int moveMethod;
|
|
|
|
int res;
|
|
|
|
switch (origin)
|
|
|
|
{
|
|
|
|
case SZ_SEEK_SET: moveMethod = SEEK_SET; break;
|
|
|
|
case SZ_SEEK_CUR: moveMethod = SEEK_CUR; break;
|
|
|
|
case SZ_SEEK_END: moveMethod = SEEK_END; break;
|
|
|
|
default: return 1;
|
|
|
|
}
|
|
|
|
res = fseek(p->file, (long)*pos, moveMethod);
|
|
|
|
*pos = ftell(p->file);
|
|
|
|
return res;
|
|
|
|
|
|
|
|
#endif
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 02:28:20 +00:00
|
|
|
WRes File_GetLength(CSzFile *p, uint64_t *length)
|
2014-09-02 14:13:04 +00:00
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
#ifdef USE_WINDOWS_FILE
|
|
|
|
|
|
|
|
DWORD sizeHigh;
|
|
|
|
DWORD sizeLow = GetFileSize(p->handle, &sizeHigh);
|
|
|
|
if (sizeLow == 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
DWORD res = GetLastError();
|
|
|
|
if (res != NO_ERROR)
|
|
|
|
return res;
|
|
|
|
}
|
2014-10-20 02:28:20 +00:00
|
|
|
*length = (((uint64_t)sizeHigh) << 32) + sizeLow;
|
2014-10-16 17:03:28 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
long pos = ftell(p->file);
|
|
|
|
int res = fseek(p->file, 0, SEEK_END);
|
|
|
|
*length = ftell(p->file);
|
|
|
|
fseek(p->file, pos, SEEK_SET);
|
|
|
|
return res;
|
|
|
|
|
|
|
|
#endif
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------- FileSeqInStream ---------- */
|
|
|
|
|
|
|
|
static SRes FileSeqInStream_Read(void *pp, void *buf, size_t *size)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
CFileSeqInStream *p = (CFileSeqInStream *)pp;
|
|
|
|
return File_Read(&p->file, buf, size) == 0 ? SZ_OK : SZ_ERROR_READ;
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
p->s.Read = FileSeqInStream_Read;
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------- FileInStream ---------- */
|
|
|
|
|
|
|
|
static SRes FileInStream_Read(void *pp, void *buf, size_t *size)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
CFileInStream *p = (CFileInStream *)pp;
|
|
|
|
return (File_Read(&p->file, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ;
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 02:28:20 +00:00
|
|
|
static SRes FileInStream_Seek(void *pp, int64_t *pos, ESzSeek origin)
|
2014-09-02 14:13:04 +00:00
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
CFileInStream *p = (CFileInStream *)pp;
|
|
|
|
return File_Seek(&p->file, pos, origin);
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileInStream_CreateVTable(CFileInStream *p)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
p->s.Read = FileInStream_Read;
|
|
|
|
p->s.Seek = FileInStream_Seek;
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------- FileOutStream ---------- */
|
|
|
|
|
|
|
|
static size_t FileOutStream_Write(void *pp, const void *data, size_t size)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
CFileOutStream *p = (CFileOutStream *)pp;
|
|
|
|
File_Write(&p->file, data, &size);
|
|
|
|
return size;
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileOutStream_CreateVTable(CFileOutStream *p)
|
|
|
|
{
|
2014-10-16 17:03:28 +00:00
|
|
|
p->s.Write = FileOutStream_Write;
|
2014-09-02 14:13:04 +00:00
|
|
|
}
|