From b8b5019c2c57bfa6c5161650e67661e9861fcacb Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Thu, 23 Aug 2018 14:59:30 +0900 Subject: [PATCH] Apply patch for packaging: liblzma-tool liblzma-tool will replace lib7zip Change-Id: Ic9f2edcf286c570f6426abd55785e59a0a8d69e2 Signed-off-by: Sunmin Lee --- C/7zDec.c | 2 +- C/7zDecomp.c | 464 ++++++++++++++++++++++++++++++++++++++++++++++++ C/Lzma2Enc.c | 4 +- C/LzmaEnc.c | 5 +- CMakeLists.txt | 59 ++++++ liblzma-tool.pc.in | 13 ++ local_build.sh | 5 + packaging/lzma-sdk.spec | 51 ++++++ 8 files changed, 599 insertions(+), 4 deletions(-) create mode 100755 C/7zDecomp.c create mode 100755 CMakeLists.txt create mode 100644 liblzma-tool.pc.in create mode 100755 local_build.sh create mode 100644 packaging/lzma-sdk.spec diff --git a/C/7zDec.c b/C/7zDec.c index 3386807..a79ed5b 100755 --- a/C/7zDec.c +++ b/C/7zDec.c @@ -3,7 +3,7 @@ #include -/* #define _7ZIP_PPMD_SUPPPORT */ +#define _7ZIP_PPMD_SUPPPORT #include "7z.h" diff --git a/C/7zDecomp.c b/C/7zDecomp.c new file mode 100755 index 0000000..2136e6d --- /dev/null +++ b/C/7zDecomp.c @@ -0,0 +1,464 @@ +/* 7zMain.c - Test application for 7z Decoder +2010-10-28 : Igor Pavlov : Public domain */ + +#include +#include + +#include "7z.h" +#include "7zAlloc.h" +#include "7zCrc.h" +#include "7zFile.h" +#include "7zVersion.h" + +#ifndef USE_WINDOWS_FILE +/* for mkdir */ +#ifdef _WIN32 +#include +#else +#include +#include +#endif +#endif + +static ISzAlloc g_Alloc = { SzAlloc, SzFree }; + +static int Buf_EnsureSize(CBuf *dest, size_t size) +{ + if (dest->size >= size) + return 1; + Buf_Free(dest, &g_Alloc); + return Buf_Create(dest, size, &g_Alloc); +} + +#ifndef _WIN32 + +static Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + +static Bool Utf16_To_Utf8(Byte *dest, size_t *destLen, const UInt16 *src, size_t srcLen) +{ + size_t destPos = 0, srcPos = 0; + for (;;) + { + unsigned numAdds; + UInt32 value; + if (srcPos == srcLen) + { + *destLen = destPos; + return True; + } + value = src[srcPos++]; + if (value < 0x80) + { + if (dest) + dest[destPos] = (char)value; + destPos++; + continue; + } + if (value >= 0xD800 && value < 0xE000) + { + UInt32 c2; + if (value >= 0xDC00 || srcPos == srcLen) + break; + c2 = src[srcPos++]; + if (c2 < 0xDC00 || c2 >= 0xE000) + break; + value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000; + } + for (numAdds = 1; numAdds < 5; numAdds++) + if (value < (((UInt32)1) << (numAdds * 5 + 6))) + break; + if (dest) + dest[destPos] = (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds))); + destPos++; + do + { + numAdds--; + if (dest) + dest[destPos] = (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F)); + destPos++; + } + while (numAdds != 0); + } + *destLen = destPos; + return False; +} + +static SRes Utf16_To_Utf8Buf(CBuf *dest, const UInt16 *src, size_t srcLen) +{ + size_t destLen = 0; + Bool res; + Utf16_To_Utf8(NULL, &destLen, src, srcLen); + destLen += 1; + if (!Buf_EnsureSize(dest, destLen)) + return SZ_ERROR_MEM; + res = Utf16_To_Utf8(dest->data, &destLen, src, srcLen); + dest->data[destLen] = 0; + return res ? SZ_OK : SZ_ERROR_FAIL; +} +#endif + +static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s, int fileMode) +{ + int len = 0; + for (len = 0; s[len] != '\0'; len++); + + #ifdef _WIN32 + { + int size = len * 3 + 100; + if (!Buf_EnsureSize(buf, size)) + return SZ_ERROR_MEM; + { + char defaultChar = '_'; + BOOL defUsed; + int numChars = WideCharToMultiByte(fileMode ? + ( + #ifdef UNDER_CE + CP_ACP + #else + AreFileApisANSI() ? CP_ACP : CP_OEMCP + #endif + ) : CP_OEMCP, + 0, s, len, (char *)buf->data, size, &defaultChar, &defUsed); + if (numChars == 0 || numChars >= size) + return SZ_ERROR_FAIL; + buf->data[numChars] = 0; + return SZ_OK; + } + } + #else + fileMode = fileMode; + return Utf16_To_Utf8Buf(buf, s, len); + #endif +} + +static WRes MyCreateDir(const UInt16 *name) +{ + #ifdef USE_WINDOWS_FILE + + return CreateDirectoryW(name, NULL) ? 0 : GetLastError(); + + #else + + CBuf buf; + WRes res; + Buf_Init(&buf); + RINOK(Utf16_To_Char(&buf, name, 1)); + + res = + #ifdef _WIN32 + _mkdir((const char *)buf.data) + #else + mkdir((const char *)buf.data, 0777) + #endif + == 0 ? 0 : errno; + Buf_Free(&buf, &g_Alloc); + return res; + + #endif +} + +static WRes OutFile_OpenUtf16(CSzFile *p, const UInt16 *name) +{ + #ifdef USE_WINDOWS_FILE + return OutFile_OpenW(p, name); + #else + CBuf buf; + WRes res; + Buf_Init(&buf); + RINOK(Utf16_To_Char(&buf, name, 1)); + res = OutFile_Open(p, (const char *)buf.data); + Buf_Free(&buf, &g_Alloc); + return res; + #endif +} + +static SRes PrintString(const UInt16 *s) +{ + CBuf buf; + SRes res; + Buf_Init(&buf); + res = Utf16_To_Char(&buf, s, 0); + if (res != SZ_OK) + printf("[%s]\n",(const char *)buf.data); + Buf_Free(&buf, &g_Alloc); + return res; +} + +static void UInt64ToStr(UInt64 value, char *s) +{ + char temp[32]; + int pos = 0; + do + { + temp[pos++] = (char)('0' + (unsigned)(value % 10)); + value /= 10; + } + while (value != 0); + do + *s++ = temp[--pos]; + while (pos); + *s = '\0'; +} + +static char *UIntToStr(char *s, unsigned value, int numDigits) +{ + char temp[16]; + int pos = 0; + do + temp[pos++] = (char)('0' + (value % 10)); + while (value /= 10); + for (numDigits -= pos; numDigits > 0; numDigits--) + *s++ = '0'; + do + *s++ = temp[--pos]; + while (pos); + *s = '\0'; + return s; +} + +#define PERIOD_4 (4 * 365 + 1) +#define PERIOD_100 (PERIOD_4 * 25 - 1) +#define PERIOD_400 (PERIOD_100 * 4 + 1) + +static void ConvertFileTimeToString(const CNtfsFileTime *ft, char *s) +{ + unsigned year, mon, day, hour, min, sec; + UInt64 v64 = (ft->Low | ((UInt64)ft->High << 32)) / 10000000; + Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + unsigned t; + UInt32 v; + sec = (unsigned)(v64 % 60); v64 /= 60; + min = (unsigned)(v64 % 60); v64 /= 60; + hour = (unsigned)(v64 % 24); v64 /= 24; + + v = (UInt32)v64; + + year = (unsigned)(1601 + v / PERIOD_400 * 400); + v %= PERIOD_400; + + t = v / PERIOD_100; if (t == 4) t = 3; year += t * 100; v -= t * PERIOD_100; + t = v / PERIOD_4; if (t == 25) t = 24; year += t * 4; v -= t * PERIOD_4; + t = v / 365; if (t == 4) t = 3; year += t; v -= t * 365; + + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + ms[1] = 29; + for (mon = 1; mon <= 12; mon++) + { + unsigned s = ms[mon - 1]; + if (v < s) + break; + v -= s; + } + day = (unsigned)v + 1; + s = UIntToStr(s, year, 4); *s++ = '-'; + s = UIntToStr(s, mon, 2); *s++ = '-'; + s = UIntToStr(s, day, 2); *s++ = ' '; + s = UIntToStr(s, hour, 2); *s++ = ':'; + s = UIntToStr(s, min, 2); *s++ = ':'; + s = UIntToStr(s, sec, 2); +} + +void PrintError(char *sz) +{ + printf("\nERROR: %s\n", sz); +} + +static void GetAttribString(UInt32 Bool, Byte isDir, char *s) +{ + s[0] = '\0'; +} + +int _7zdecompress(char *path) +{ + CFileInStream archiveStream; + CLookToRead lookStream; + CSzArEx db; + SRes res; + ISzAlloc allocImp; + ISzAlloc allocTempImp; + UInt16 *temp = NULL; + size_t tempSize = 0; + + + allocImp.Alloc = SzAlloc; + allocImp.Free = SzFree; + + allocTempImp.Alloc = SzAllocTemp; + allocTempImp.Free = SzFreeTemp; + + if (InFile_Open(&archiveStream.file, path)) + { + PrintError("can not open input file"); + return 1; + } + + FileInStream_CreateVTable(&archiveStream); + LookToRead_CreateVTable(&lookStream, False); + + lookStream.realStream = &archiveStream.s; + LookToRead_Init(&lookStream); + + CrcGenerateTable(); + + SzArEx_Init(&db); + res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp); + if (res == SZ_OK) + { + int listCommand = 0, testCommand = 0, fullPaths = 0; + + fullPaths = 1; + if (res == SZ_OK) + { + UInt32 i; + + /* + if you need cache, use these 3 variables. + if you use external function, you can make these variable as static. + */ + UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */ + Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */ + size_t outBufferSize = 0; /* it can have any value before first call (if outBuffer = 0) */ + + for (i = 0; i < db.db.NumFiles; i++) + { + size_t offset = 0; + size_t outSizeProcessed = 0; + const CSzFileItem *f = db.db.Files + i; + size_t len; + if (listCommand == 0 && f->IsDir && !fullPaths) + continue; + len = SzArEx_GetFileNameUtf16(&db, i, NULL); + + if (len > tempSize) + { + SzFree(NULL, temp); + tempSize = len; + temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0])); + if (temp == 0) + { + res = SZ_ERROR_MEM; + break; + } + } + + SzArEx_GetFileNameUtf16(&db, i, temp); + if (listCommand) + { + char attr[8], s[32], t[32]; + + GetAttribString(f->AttribDefined ? f->Attrib : 0, f->IsDir, attr); + + UInt64ToStr(f->Size, s); + if (f->MTimeDefined) + ConvertFileTimeToString(&f->MTime, t); + else + { + size_t j; + for (j = 0; j < 19; j++) + t[j] = ' '; + t[j] = '\0'; + } + + printf("%s %s %10s ", t, attr, s); + res = PrintString(temp); + if (res != SZ_OK) + break; + if (f->IsDir) + printf("/"); + printf("\n"); + continue; + } + fputs(testCommand ? + "Testing ": + "Extracting ", + stdout); + res = PrintString(temp); + if (res != SZ_OK) + break; + if (f->IsDir) + printf("/"); + else + { + res = SzArEx_Extract(&db, &lookStream.s, i, + &blockIndex, &outBuffer, &outBufferSize, + &offset, &outSizeProcessed, + &allocImp, &allocTempImp); + if (res != SZ_OK) + break; + } + if (!testCommand) + { + CSzFile outFile; + size_t processedSize; + size_t j; + UInt16 *name = (UInt16 *)temp; + const UInt16 *destPath = (const UInt16 *)name; + for (j = 0; name[j] != 0; j++) + if (name[j] == '/') + { + if (fullPaths) + { + name[j] = 0; + MyCreateDir(name); + name[j] = CHAR_PATH_SEPARATOR; + } + else + destPath = name + j + 1; + } + PrintString(destPath); + if (f->IsDir) + { + MyCreateDir(destPath); + printf("\n"); + continue; + } + else if (OutFile_OpenUtf16(&outFile, destPath)) + { + PrintError("can not open output file"); + PrintString(destPath); + res = SZ_ERROR_FAIL; + break; + } + processedSize = outSizeProcessed; + if (File_Write(&outFile, outBuffer + offset, &processedSize) != 0 || processedSize != outSizeProcessed) + { + PrintError("can not write output file"); + File_Close(&outFile);//wgid: 27067 + res = SZ_ERROR_FAIL; + break; + } + if (File_Close(&outFile)) + { + PrintError("can not close output file"); + res = SZ_ERROR_FAIL; + break; + } + #ifdef USE_WINDOWS_FILE + if (f->AttribDefined) + SetFileAttributesW(destPath, f->Attrib); + #endif + } + printf("\n"); + } + IAlloc_Free(&allocImp, outBuffer); + } + } + SzArEx_Free(&db, &allocImp); + SzFree(NULL, temp); + + File_Close(&archiveStream.file); + if (res == SZ_OK) + { + printf("\nEverything is Ok\n"); + return 0; + } + if (res == SZ_ERROR_UNSUPPORTED) + PrintError("decoder doesn't support this archive"); + else if (res == SZ_ERROR_MEM) + PrintError("can not allocate memory"); + else if (res == SZ_ERROR_CRC) + PrintError("CRC error"); + else + printf("\nERROR #%d\n", res); + return 1; +} diff --git a/C/Lzma2Enc.c b/C/Lzma2Enc.c index 35f6ed7..e48aad3 100755 --- a/C/Lzma2Enc.c +++ b/C/Lzma2Enc.c @@ -4,11 +4,11 @@ /* #include */ #include -/* #define _7ZIP_ST */ +#define _7ZIP_ST #include "Lzma2Enc.h" -#ifndef _7ZIP_ST +#if 0//ndef _7ZIP_ST #include "MtCoder.h" #else #define NUM_MT_CODER_THREADS_MAX 1 diff --git a/C/LzmaEnc.c b/C/LzmaEnc.c index 9e6dbdb..a9e3f09 100755 --- a/C/LzmaEnc.c +++ b/C/LzmaEnc.c @@ -3,6 +3,7 @@ #include +#define _7ZIP_ST /* #define SHOW_STAT */ /* #define SHOW_STAT2 */ @@ -1897,11 +1898,13 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) { UInt32 beforeSize = kNumOpts; + #ifndef _7ZIP_ST Bool btMode; + #endif if (!RangeEnc_Alloc(&p->rc, alloc)) return SZ_ERROR_MEM; - btMode = (p->matchFinderBase.btMode != 0); #ifndef _7ZIP_ST + btMode = (p->matchFinderBase.btMode != 0); p->mtMode = (p->multiThread && !p->fastMode && btMode); #endif diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..1a0d39c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,59 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(lzma-tool C) + +SET(LIBLZMA_SRCS + C/7zAlloc.c + C/7zBuf2.c + C/7zBuf.c + C/7zCrc.c + C/7zCrcOpt.c + C/7zDec.c + C/7zFile.c + C/7zIn.c + C/7zStream.c + C/7zDecomp.c + C/Alloc.c + C/Bcj2.c + C/Bra86.c + C/Bra.c + C/CpuArch.c + C/LzFind.c + C/Lzma2Dec.c + C/Lzma2Enc.c + C/LzmaDec.c + C/LzmaEnc.c + C/Ppmd7.c + C/Ppmd7Dec.c +) + +SET(HEADERS + C/Types.h + C/Alloc.h + C/7zFile.h + C/7zVersion.h + C/LzmaDec.h + C/LzmaEnc.h +) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "${PREFIX}/bin") +SET(INCLUDEDIR "${PREFIX}/include/${PROJECT_NAME}") +SET(LIBVERSION 0.0.0) + +SET(LIBNAME "lib${PROJECT_NAME}") +CONFIGURE_FILE(${LIBNAME}.pc.in ${LIBNAME}.pc @ONLY) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${LIBNAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/C) +INSTALL(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME}) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") +ADD_LIBRARY(${PROJECT_NAME}-object OBJECT ${LIBLZMA_SRCS}) + +ADD_LIBRARY(${PROJECT_NAME} SHARED $) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${LIBVERSION}) +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}) + +ADD_LIBRARY(${PROJECT_NAME}-static STATIC $) +SET_TARGET_PROPERTIES(${PROJECT_NAME}-static PROPERTIES VERSION ${LIBVERSION}) +INSTALL(TARGETS ${PROJECT_NAME}-static DESTINATION ${LIB_INSTALL_DIR}) diff --git a/liblzma-tool.pc.in b/liblzma-tool.pc.in new file mode 100644 index 0000000..553c406 --- /dev/null +++ b/liblzma-tool.pc.in @@ -0,0 +1,13 @@ +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=@EXEC_PREFIX@ +libdir=@LIB_INSTALL_DIR@ +includedir=@INCLUDEDIR@ + +Name: lzma-tool +Description: LZMA tool library +Version: @VERSION@ +Requires: +Libs: -L${libdir} -llzma-tool +Cflags: -I${includedir} diff --git a/local_build.sh b/local_build.sh new file mode 100755 index 0000000..055e1b4 --- /dev/null +++ b/local_build.sh @@ -0,0 +1,5 @@ +mkdir -p local_build +cd local_build +cmake -DLIB_INSTALL_DIR="/usr/local/lib" .. +make install +cd .. diff --git a/packaging/lzma-sdk.spec b/packaging/lzma-sdk.spec new file mode 100644 index 0000000..e59ceb5 --- /dev/null +++ b/packaging/lzma-sdk.spec @@ -0,0 +1,51 @@ +Name: lzma-sdk +Url: https://www.7-zip.org/sdk.html +Summary: The SDK for LZMA Compression +Version: 9.20 +Release: 1 +Group: System +License: PD +Source0: %{name}-%{version}.tar.gz + +BuildRequires: cmake + +%define keepstatic 1 + +%description +LZMA SDK provides the documentation, samples, header files, libraries, +and tools you need to develop applications that use LZMA compression + +%package -n liblzma-tool +Summary: LZMA tool library package + +%description -n liblzma-tool +LZMA tool shared library + +%package -n liblzma-tool-devel +Summary: LZMA tool library for (devel) + +%description -n liblzma-tool-devel +LZMA tool library package for (devel) + +%prep +%setup -q + +%build +%cmake . \ + -DCMAKE_INSTALL_PREFIX=%{_prefix} +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + +%files -n liblzma-tool +%defattr(-,root,root,-) +%{_libdir}/liblzma-tool.so.* + +%files -n liblzma-tool-devel +%defattr(-,root,root,-) +%{_includedir}/lzma-tool/*.h +%{_libdir}/liblzma-tool.so +%{_libdir}/liblzma-tool-static.a +%{_libdir}/pkgconfig/liblzma-tool.pc -- 2.7.4