4.61 beta

This commit is contained in:
Igor Pavlov
2008-11-23 00:00:00 +00:00
committed by Kornel Lesiński
parent c10e6b16f6
commit b717a4dbfe
80 changed files with 1605 additions and 1312 deletions

View File

@@ -1,34 +1,19 @@
/* 7zMain.c - Test application for 7z Decoder
2008-08-17
Igor Pavlov
Public domain */
2008-11-23 : Igor Pavlov : Public domain */
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#define USE_WINDOWS_FUNCTIONS
#endif
#ifdef USE_WINDOWS_FUNCTIONS
#include <windows.h>
#endif
#include "7zIn.h"
#include "7zExtract.h"
#include "7zAlloc.h"
#include "../../7zCrc.h"
#include "../../7zFile.h"
#include "../../7zVersion.h"
#include "7zAlloc.h"
#include "7zExtract.h"
#include "7zIn.h"
#ifdef USE_WINDOWS_FUNCTIONS
typedef HANDLE MY_FILE_HANDLE;
#else
typedef FILE *MY_FILE_HANDLE;
#endif
void ConvertNumberToString(CFileSize value, char *s)
static void ConvertNumberToString(UInt64 value, char *s)
{
char temp[32];
int pos = 0;
@@ -48,7 +33,7 @@ void ConvertNumberToString(CFileSize value, char *s)
#define PERIOD_100 (PERIOD_4 * 25 - 1)
#define PERIOD_400 (PERIOD_100 * 4 + 1)
void ConvertFileTimeToString(CNtfsFileTime *ft, char *s)
static void ConvertFileTimeToString(CNtfsFileTime *ft, char *s)
{
unsigned year, mon, day, hour, min, sec;
UInt64 v64 = ft->Low | ((UInt64)ft->High << 32);
@@ -99,140 +84,6 @@ void ConvertFileTimeToString(CNtfsFileTime *ft, char *s)
sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d", year, mon, day, hour, min, sec);
}
#ifdef USE_WINDOWS_FUNCTIONS
/*
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).
*/
#define kChunkSizeMax (1 << 24)
#endif
size_t MyReadFile(MY_FILE_HANDLE file, void *data, size_t size)
{
if (size == 0)
return 0;
#ifdef USE_WINDOWS_FUNCTIONS
{
size_t processedSize = 0;
do
{
DWORD curSize = (size > kChunkSizeMax) ? kChunkSizeMax : (DWORD)size;
DWORD processedLoc = 0;
BOOL res = ReadFile(file, data, curSize, &processedLoc, NULL);
data = (void *)((unsigned char *)data + processedLoc);
size -= processedLoc;
processedSize += processedLoc;
if (!res || processedLoc == 0)
break;
}
while (size > 0);
return processedSize;
}
#else
return fread(data, 1, size, file);
#endif
}
size_t MyWriteFile(MY_FILE_HANDLE file, void *data, size_t size)
{
if (size == 0)
return 0;
#ifdef USE_WINDOWS_FUNCTIONS
{
size_t processedSize = 0;
do
{
DWORD curSize = (size > kChunkSizeMax) ? kChunkSizeMax : (DWORD)size;
DWORD processedLoc = 0;
BOOL res = WriteFile(file, data, curSize, &processedLoc, NULL);
data = (void *)((unsigned char *)data + processedLoc);
size -= processedLoc;
processedSize += processedLoc;
if (!res)
break;
}
while (size > 0);
return processedSize;
}
#else
return fwrite(data, 1, size, file);
#endif
}
int MyCloseFile(MY_FILE_HANDLE file)
{
#ifdef USE_WINDOWS_FUNCTIONS
return (CloseHandle(file) != FALSE) ? 0 : 1;
#else
return fclose(file);
#endif
}
typedef struct _CFileInStream
{
ISzInStream InStream;
MY_FILE_HANDLE File;
} CFileInStream;
#define kBufferSize (1 << 12)
Byte g_Buffer[kBufferSize];
SRes SzFileReadImp(void *object, void **buffer, size_t *size)
{
CFileInStream *s = (CFileInStream *)object;
if (*size > kBufferSize)
*size = kBufferSize;
*size = MyReadFile(s->File, g_Buffer, *size);
*buffer = g_Buffer;
return SZ_OK;
}
SRes SzFileSeekImp(void *object, CFileSize pos, ESzSeek origin)
{
CFileInStream *s = (CFileInStream *)object;
#ifdef USE_WINDOWS_FUNCTIONS
{
LARGE_INTEGER value;
DWORD moveMethod;
value.LowPart = (DWORD)pos;
value.HighPart = (LONG)((UInt64)pos >> 32);
#ifdef _SZ_FILE_SIZE_32
/* VC 6.0 has bug with >> 32 shifts. */
value.HighPart = 0;
#endif
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 SZ_ERROR_PARAM;
}
value.LowPart = SetFilePointer(s->File, value.LowPart, &value.HighPart, moveMethod);
if (value.LowPart == 0xFFFFFFFF)
if (GetLastError() != NO_ERROR)
return SZ_ERROR_FAIL;
return SZ_OK;
}
#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 SZ_ERROR_PARAM;
}
res = fseek(s->File, (long)pos, moveMethod );
return (res == 0) ? SZ_OK : SZ_ERROR_FAIL;
#endif
}
void PrintError(char *sz)
{
printf("\nERROR: %s\n", sz);
@@ -241,12 +92,13 @@ void PrintError(char *sz)
int MY_CDECL main(int numargs, char *args[])
{
CFileInStream archiveStream;
CLookToRead lookStream;
CSzArEx db;
SRes res;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
printf("\n7z ANSI-C Decoder 4.59 Copyright (c) 1999-2008 Igor Pavlov 2008-07-09\n");
printf("\n7z ANSI-C Decoder " MY_VERSION_COPYRIGHT_DATE "\n");
if (numargs == 1)
{
printf(
@@ -263,22 +115,18 @@ int MY_CDECL main(int numargs, char *args[])
return 1;
}
archiveStream.File =
#ifdef USE_WINDOWS_FUNCTIONS
CreateFileA(args[2], GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (archiveStream.File == INVALID_HANDLE_VALUE)
#else
archiveStream.File = fopen(args[2], "rb");
if (archiveStream.File == 0)
#endif
if (InFile_Open(&archiveStream.file, args[2]))
{
PrintError("can not open input file");
return 1;
}
archiveStream.InStream.Read = SzFileReadImp;
archiveStream.InStream.Seek = SzFileSeekImp;
FileInStream_CreateVTable(&archiveStream);
LookToRead_CreateVTable(&lookStream, False);
lookStream.realStream = &archiveStream.s;
LookToRead_Init(&lookStream);
allocImp.Alloc = SzAlloc;
allocImp.Free = SzFree;
@@ -289,19 +137,14 @@ int MY_CDECL main(int numargs, char *args[])
CrcGenerateTable();
SzArEx_Init(&db);
res = SzArEx_Open(&db, &archiveStream.InStream, &allocImp, &allocTempImp);
res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
if (res == SZ_OK)
{
char *command = args[1];
int listCommand = 0;
int testCommand = 0;
int extractCommand = 0;
if (strcmp(command, "l") == 0)
listCommand = 1;
if (strcmp(command, "t") == 0)
testCommand = 1;
else if (strcmp(command, "e") == 0)
extractCommand = 1;
int listCommand = 0, testCommand = 0, extractCommand = 0;
if (strcmp(command, "l") == 0) listCommand = 1;
else if (strcmp(command, "t") == 0) testCommand = 1;
else if (strcmp(command, "e") == 0) extractCommand = 1;
if (listCommand)
{
@@ -316,7 +159,7 @@ int MY_CDECL main(int numargs, char *args[])
else
strcpy(t, " ");
printf("%10s %s %s\n", s, t, f->Name);
printf("%s %10s %s\n", t, s, f->Name);
}
}
else if (testCommand || extractCommand)
@@ -349,7 +192,7 @@ int MY_CDECL main(int numargs, char *args[])
printf("\n");
continue;
}
res = SzAr_Extract(&db, &archiveStream.InStream, i,
res = SzAr_Extract(&db, &lookStream.s, i,
&blockIndex, &outBuffer, &outBufferSize,
&offset, &outSizeProcessed,
&allocImp, &allocTempImp);
@@ -357,7 +200,7 @@ int MY_CDECL main(int numargs, char *args[])
break;
if (!testCommand)
{
MY_FILE_HANDLE outputHandle;
CSzFile outFile;
size_t processedSize;
char *fileName = f->Name;
size_t nameLen = strlen(f->Name);
@@ -368,28 +211,21 @@ int MY_CDECL main(int numargs, char *args[])
break;
}
outputHandle =
#ifdef USE_WINDOWS_FUNCTIONS
CreateFileA(fileName, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (outputHandle == INVALID_HANDLE_VALUE)
#else
fopen(fileName, "wb+");
if (outputHandle == 0)
#endif
if (OutFile_Open(&outFile, fileName))
{
PrintError("can not open output file");
res = SZ_ERROR_FAIL;
break;
}
processedSize = MyWriteFile(outputHandle, outBuffer + offset, outSizeProcessed);
if (processedSize != outSizeProcessed)
processedSize = outSizeProcessed;
if (File_Write(&outFile, outBuffer + offset, &processedSize) != 0 ||
processedSize != outSizeProcessed)
{
PrintError("can not write output file");
res = SZ_ERROR_FAIL;
break;
}
if (MyCloseFile(outputHandle))
if (File_Close(&outFile))
{
PrintError("can not close output file");
res = SZ_ERROR_FAIL;
@@ -408,7 +244,7 @@ int MY_CDECL main(int numargs, char *args[])
}
SzArEx_Free(&db, &allocImp);
MyCloseFile(archiveStream.File);
File_Close(&archiveStream.file);
if (res == SZ_OK)
{
printf("\nEverything is Ok\n");