Projects
Eulaceura:Mainline
unizip
_service:obs_scm:add-lzma-sdk-and-uadk-include-...
Sign Up
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:obs_scm:add-lzma-sdk-and-uadk-include-file.patch of Package unizip
From 4f1cab99673dc0410bbbb6fc233018ed884b1a54 Mon Sep 17 00:00:00 2001 From: huangduirong <huangduirong@huawei.com> Date: Mon, 11 Dec 2023 23:26:43 +0800 Subject: [PATCH] add lzma-sdk and uadk include file --- src/lzma-sdk/C/7z.h | 204 ++++++++++ src/lzma-sdk/C/7zAlloc.h | 19 + src/lzma-sdk/C/7zBuf.h | 35 ++ src/lzma-sdk/C/7zCrc.h | 25 ++ src/lzma-sdk/C/7zFile.h | 91 +++++ src/lzma-sdk/C/7zTypes.h | 529 +++++++++++++++++++++++++ src/lzma-sdk/C/7zVersion.h | 27 ++ src/lzma-sdk/C/Aes.h | 60 +++ src/lzma-sdk/C/Alloc.h | 58 +++ src/lzma-sdk/C/Bcj2.h | 146 +++++++ src/lzma-sdk/C/Bra.h | 64 +++ src/lzma-sdk/C/Compiler.h | 43 ++ src/lzma-sdk/C/CpuArch.h | 442 +++++++++++++++++++++ src/lzma-sdk/C/Delta.h | 19 + src/lzma-sdk/C/DllSecur.h | 20 + src/lzma-sdk/C/LzFind.h | 136 +++++++ src/lzma-sdk/C/LzFindMt.h | 109 +++++ src/lzma-sdk/C/LzHash.h | 34 ++ src/lzma-sdk/C/Lzma2Dec.h | 120 ++++++ src/lzma-sdk/C/Lzma2DecMt.h | 79 ++++ src/lzma-sdk/C/Lzma2Enc.h | 55 +++ src/lzma-sdk/C/Lzma86.h | 111 ++++++ src/lzma-sdk/C/LzmaDec.h | 236 +++++++++++ src/lzma-sdk/C/LzmaEnc.h | 78 ++++ src/lzma-sdk/C/LzmaLib.h | 138 +++++++ src/lzma-sdk/C/MtCoder.h | 141 +++++++ src/lzma-sdk/C/MtDec.h | 202 ++++++++++ src/lzma-sdk/C/Ppmd.h | 167 ++++++++ src/lzma-sdk/C/Ppmd7.h | 181 +++++++++ src/lzma-sdk/C/Precomp.h | 10 + src/lzma-sdk/C/RotateDefs.h | 30 ++ src/lzma-sdk/C/Sha256.h | 76 ++++ src/lzma-sdk/C/Sort.h | 18 + src/lzma-sdk/C/Threads.h | 232 +++++++++++ src/lzma-sdk/C/Util/7z/Precomp.h | 10 + src/lzma-sdk/C/Util/SfxSetup/Precomp.h | 10 + src/lzma-sdk/C/Xz.h | 517 ++++++++++++++++++++++++ src/lzma-sdk/C/XzCrc64.h | 26 ++ src/lzma-sdk/C/XzEnc.h | 60 +++ src/uadk/v1/uacce.h | 86 ++++ src/uadk/v1/wd.h | 214 ++++++++++ src/uadk/v1/wd_adapter.h | 75 ++++ src/uadk/v1/wd_aead.h | 213 ++++++++++ src/uadk/v1/wd_bmm.h | 50 +++ src/uadk/v1/wd_cipher.h | 166 ++++++++ src/uadk/v1/wd_comp.h | 256 ++++++++++++ src/uadk/v1/wd_dh.h | 91 +++++ src/uadk/v1/wd_digest.h | 168 ++++++++ src/uadk/v1/wd_ecc.h | 273 +++++++++++++ src/uadk/v1/wd_rng.h | 76 ++++ src/uadk/v1/wd_rsa.h | 148 +++++++ src/uadk/v1/wd_sgl.h | 74 ++++ src/uadk/v1/wd_util.h | 393 ++++++++++++++++++ 53 files changed, 6841 insertions(+) create mode 100644 src/lzma-sdk/C/7z.h create mode 100644 src/lzma-sdk/C/7zAlloc.h create mode 100644 src/lzma-sdk/C/7zBuf.h create mode 100644 src/lzma-sdk/C/7zCrc.h create mode 100644 src/lzma-sdk/C/7zFile.h create mode 100644 src/lzma-sdk/C/7zTypes.h create mode 100644 src/lzma-sdk/C/7zVersion.h create mode 100644 src/lzma-sdk/C/Aes.h create mode 100644 src/lzma-sdk/C/Alloc.h create mode 100644 src/lzma-sdk/C/Bcj2.h create mode 100644 src/lzma-sdk/C/Bra.h create mode 100644 src/lzma-sdk/C/Compiler.h create mode 100644 src/lzma-sdk/C/CpuArch.h create mode 100644 src/lzma-sdk/C/Delta.h create mode 100644 src/lzma-sdk/C/DllSecur.h create mode 100644 src/lzma-sdk/C/LzFind.h create mode 100644 src/lzma-sdk/C/LzFindMt.h create mode 100644 src/lzma-sdk/C/LzHash.h create mode 100644 src/lzma-sdk/C/Lzma2Dec.h create mode 100644 src/lzma-sdk/C/Lzma2DecMt.h create mode 100644 src/lzma-sdk/C/Lzma2Enc.h create mode 100644 src/lzma-sdk/C/Lzma86.h create mode 100644 src/lzma-sdk/C/LzmaDec.h create mode 100644 src/lzma-sdk/C/LzmaEnc.h create mode 100644 src/lzma-sdk/C/LzmaLib.h create mode 100644 src/lzma-sdk/C/MtCoder.h create mode 100644 src/lzma-sdk/C/MtDec.h create mode 100644 src/lzma-sdk/C/Ppmd.h create mode 100644 src/lzma-sdk/C/Ppmd7.h create mode 100644 src/lzma-sdk/C/Precomp.h create mode 100644 src/lzma-sdk/C/RotateDefs.h create mode 100644 src/lzma-sdk/C/Sha256.h create mode 100644 src/lzma-sdk/C/Sort.h create mode 100644 src/lzma-sdk/C/Threads.h create mode 100644 src/lzma-sdk/C/Util/7z/Precomp.h create mode 100644 src/lzma-sdk/C/Util/SfxSetup/Precomp.h create mode 100644 src/lzma-sdk/C/Xz.h create mode 100644 src/lzma-sdk/C/XzCrc64.h create mode 100644 src/lzma-sdk/C/XzEnc.h create mode 100644 src/uadk/v1/uacce.h create mode 100644 src/uadk/v1/wd.h create mode 100644 src/uadk/v1/wd_adapter.h create mode 100644 src/uadk/v1/wd_aead.h create mode 100644 src/uadk/v1/wd_bmm.h create mode 100644 src/uadk/v1/wd_cipher.h create mode 100644 src/uadk/v1/wd_comp.h create mode 100644 src/uadk/v1/wd_dh.h create mode 100644 src/uadk/v1/wd_digest.h create mode 100644 src/uadk/v1/wd_ecc.h create mode 100644 src/uadk/v1/wd_rng.h create mode 100644 src/uadk/v1/wd_rsa.h create mode 100644 src/uadk/v1/wd_sgl.h create mode 100644 src/uadk/v1/wd_util.h diff --git a/src/lzma-sdk/C/7z.h b/src/lzma-sdk/C/7z.h new file mode 100644 index 0000000..969523c --- /dev/null +++ b/src/lzma-sdk/C/7z.h @@ -0,0 +1,204 @@ +/* 7z.h -- 7z interface +2018-07-02 : Igor Pavlov : Public domain */ + +#ifndef __7Z_H +#define __7Z_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define k7zStartHeaderSize 0x20 +#define k7zSignatureSize 6 + +extern const Byte k7zSignature[k7zSignatureSize]; + +typedef struct +{ + const Byte *Data; + size_t Size; +} CSzData; + +/* CSzCoderInfo & CSzFolder support only default methods */ + +typedef struct +{ + size_t PropsOffset; + UInt32 MethodID; + Byte NumStreams; + Byte PropsSize; +} CSzCoderInfo; + +typedef struct +{ + UInt32 InIndex; + UInt32 OutIndex; +} CSzBond; + +#define SZ_NUM_CODERS_IN_FOLDER_MAX 4 +#define SZ_NUM_BONDS_IN_FOLDER_MAX 3 +#define SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX 4 + +typedef struct +{ + UInt32 NumCoders; + UInt32 NumBonds; + UInt32 NumPackStreams; + UInt32 UnpackStream; + UInt32 PackStreams[SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX]; + CSzBond Bonds[SZ_NUM_BONDS_IN_FOLDER_MAX]; + CSzCoderInfo Coders[SZ_NUM_CODERS_IN_FOLDER_MAX]; +} CSzFolder; + + +SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd); + +typedef struct +{ + UInt32 Low; + UInt32 High; +} CNtfsFileTime; + +typedef struct +{ + Byte *Defs; /* MSB 0 bit numbering */ + UInt32 *Vals; +} CSzBitUi32s; + +typedef struct +{ + Byte *Defs; /* MSB 0 bit numbering */ + // UInt64 *Vals; + CNtfsFileTime *Vals; +} CSzBitUi64s; + +#define SzBitArray_Check(p, i) (((p)[(i) >> 3] & (0x80 >> ((i) & 7))) != 0) + +#define SzBitWithVals_Check(p, i) ((p)->Defs && ((p)->Defs[(i) >> 3] & (0x80 >> ((i) & 7))) != 0) + +typedef struct +{ + UInt32 NumPackStreams; + UInt32 NumFolders; + + UInt64 *PackPositions; // NumPackStreams + 1 + CSzBitUi32s FolderCRCs; // NumFolders + + size_t *FoCodersOffsets; // NumFolders + 1 + UInt32 *FoStartPackStreamIndex; // NumFolders + 1 + UInt32 *FoToCoderUnpackSizes; // NumFolders + 1 + Byte *FoToMainUnpackSizeIndex; // NumFolders + UInt64 *CoderUnpackSizes; // for all coders in all folders + + Byte *CodersData; + + UInt64 RangeLimit; +} CSzAr; + +UInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex); + +SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex, + ILookInStream *stream, UInt64 startPos, + Byte *outBuffer, size_t outSize, + ISzAllocPtr allocMain); + +typedef struct +{ + CSzAr db; + + UInt64 startPosAfterHeader; + UInt64 dataPos; + + UInt32 NumFiles; + + UInt64 *UnpackPositions; // NumFiles + 1 + // Byte *IsEmptyFiles; + Byte *IsDirs; + CSzBitUi32s CRCs; + + CSzBitUi32s Attribs; + // CSzBitUi32s Parents; + CSzBitUi64s MTime; + CSzBitUi64s CTime; + + UInt32 *FolderToFile; // NumFolders + 1 + UInt32 *FileToFolder; // NumFiles + + size_t *FileNameOffsets; /* in 2-byte steps */ + Byte *FileNames; /* UTF-16-LE */ +} CSzArEx; + +#define SzArEx_IsDir(p, i) (SzBitArray_Check((p)->IsDirs, i)) + +#define SzArEx_GetFileSize(p, i) ((p)->UnpackPositions[(i) + 1] - (p)->UnpackPositions[i]) + +void SzArEx_Init(CSzArEx *p); +void SzArEx_Free(CSzArEx *p, ISzAllocPtr alloc); +UInt64 SzArEx_GetFolderStreamPos(const CSzArEx *p, UInt32 folderIndex, UInt32 indexInFolder); +int SzArEx_GetFolderFullPackSize(const CSzArEx *p, UInt32 folderIndex, UInt64 *resSize); + +/* +if dest == NULL, the return value specifies the required size of the buffer, + in 16-bit characters, including the null-terminating character. +if dest != NULL, the return value specifies the number of 16-bit characters that + are written to the dest, including the null-terminating character. */ + +size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest); + +/* +size_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex); +UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16 *dest); +*/ + + + +/* + SzArEx_Extract extracts file from archive + + *outBuffer must be 0 before first call for each new archive. + + Extracting cache: + If you need to decompress more than one file, you can send + these values from previous call: + *blockIndex, + *outBuffer, + *outBufferSize + You can consider "*outBuffer" as cache of solid block. If your archive is solid, + it will increase decompression speed. + + If you use external function, you can declare these 3 cache variables + (blockIndex, outBuffer, outBufferSize) as static in that external function. + + Free *outBuffer and set *outBuffer to 0, if you want to flush cache. +*/ + +SRes SzArEx_Extract( + const CSzArEx *db, + ILookInStream *inStream, + UInt32 fileIndex, /* index of file */ + UInt32 *blockIndex, /* index of solid block */ + Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */ + size_t *outBufferSize, /* buffer size for output buffer */ + size_t *offset, /* offset of stream for required file in *outBuffer */ + size_t *outSizeProcessed, /* size of file in *outBuffer */ + ISzAllocPtr allocMain, + ISzAllocPtr allocTemp); + + +/* +SzArEx_Open Errors: +SZ_ERROR_NO_ARCHIVE +SZ_ERROR_ARCHIVE +SZ_ERROR_UNSUPPORTED +SZ_ERROR_MEM +SZ_ERROR_CRC +SZ_ERROR_INPUT_EOF +SZ_ERROR_FAIL +*/ + +SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream, + ISzAllocPtr allocMain, ISzAllocPtr allocTemp); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/7zAlloc.h b/src/lzma-sdk/C/7zAlloc.h new file mode 100644 index 0000000..c0f89d7 --- /dev/null +++ b/src/lzma-sdk/C/7zAlloc.h @@ -0,0 +1,19 @@ +/* 7zAlloc.h -- Allocation functions +2017-04-03 : Igor Pavlov : Public domain */ + +#ifndef __7Z_ALLOC_H +#define __7Z_ALLOC_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +void *SzAlloc(ISzAllocPtr p, size_t size); +void SzFree(ISzAllocPtr p, void *address); + +void *SzAllocTemp(ISzAllocPtr p, size_t size); +void SzFreeTemp(ISzAllocPtr p, void *address); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/7zBuf.h b/src/lzma-sdk/C/7zBuf.h new file mode 100644 index 0000000..5942d6e --- /dev/null +++ b/src/lzma-sdk/C/7zBuf.h @@ -0,0 +1,35 @@ +/* 7zBuf.h -- Byte Buffer +2017-04-03 : Igor Pavlov : Public domain */ + +#ifndef __7Z_BUF_H +#define __7Z_BUF_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +typedef struct +{ + Byte *data; + size_t size; +} CBuf; + +void Buf_Init(CBuf *p); +int Buf_Create(CBuf *p, size_t size, ISzAllocPtr alloc); +void Buf_Free(CBuf *p, ISzAllocPtr alloc); + +typedef struct +{ + Byte *data; + size_t size; + size_t pos; +} CDynBuf; + +void DynBuf_Construct(CDynBuf *p); +void DynBuf_SeekToBeg(CDynBuf *p); +int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAllocPtr alloc); +void DynBuf_Free(CDynBuf *p, ISzAllocPtr alloc); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/7zCrc.h b/src/lzma-sdk/C/7zCrc.h new file mode 100644 index 0000000..3b04594 --- /dev/null +++ b/src/lzma-sdk/C/7zCrc.h @@ -0,0 +1,25 @@ +/* 7zCrc.h -- CRC32 calculation +2013-01-18 : Igor Pavlov : Public domain */ + +#ifndef __7Z_CRC_H +#define __7Z_CRC_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +extern UInt32 g_CrcTable[]; + +/* Call CrcGenerateTable one time before other CRC functions */ +void MY_FAST_CALL CrcGenerateTable(void); + +#define CRC_INIT_VAL 0xFFFFFFFF +#define CRC_GET_DIGEST(crc) ((crc) ^ CRC_INIT_VAL) +#define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) + +UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size); +UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/7zFile.h b/src/lzma-sdk/C/7zFile.h new file mode 100644 index 0000000..c7a30fc --- /dev/null +++ b/src/lzma-sdk/C/7zFile.h @@ -0,0 +1,91 @@ +/* 7zFile.h -- File IO +2021-02-15 : Igor Pavlov : Public domain */ + +#ifndef __7Z_FILE_H +#define __7Z_FILE_H + +#ifdef _WIN32 +#define USE_WINDOWS_FILE +// #include <windows.h> +#endif + +#ifdef USE_WINDOWS_FILE +#include <windows.h> +#else +// note: USE_FOPEN mode is limited to 32-bit file size +// #define USE_FOPEN +// #include <stdio.h> +#endif + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +/* ---------- File ---------- */ + +typedef struct +{ + #ifdef USE_WINDOWS_FILE + HANDLE handle; + #elif defined(USE_FOPEN) + FILE *file; + #else + int fd; + #endif +} CSzFile; + +void File_Construct(CSzFile *p); +#if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE) +WRes InFile_Open(CSzFile *p, const char *name); +WRes OutFile_Open(CSzFile *p, const char *name); +#endif +#ifdef USE_WINDOWS_FILE +WRes InFile_OpenW(CSzFile *p, const WCHAR *name); +WRes OutFile_OpenW(CSzFile *p, const WCHAR *name); +#endif +WRes File_Close(CSzFile *p); + +/* reads max(*size, remain file's size) bytes */ +WRes File_Read(CSzFile *p, void *data, size_t *size); + +/* writes *size bytes */ +WRes File_Write(CSzFile *p, const void *data, size_t *size); + +WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin); +WRes File_GetLength(CSzFile *p, UInt64 *length); + + +/* ---------- FileInStream ---------- */ + +typedef struct +{ + ISeqInStream vt; + CSzFile file; + WRes wres; +} CFileSeqInStream; + +void FileSeqInStream_CreateVTable(CFileSeqInStream *p); + + +typedef struct +{ + ISeekInStream vt; + CSzFile file; + WRes wres; +} CFileInStream; + +void FileInStream_CreateVTable(CFileInStream *p); + + +typedef struct +{ + ISeqOutStream vt; + CSzFile file; + WRes wres; +} CFileOutStream; + +void FileOutStream_CreateVTable(CFileOutStream *p); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/7zTypes.h b/src/lzma-sdk/C/7zTypes.h new file mode 100644 index 0000000..763d3a9 --- /dev/null +++ b/src/lzma-sdk/C/7zTypes.h @@ -0,0 +1,529 @@ +/* 7zTypes.h -- Basic types +2022-04-01 : Igor Pavlov : Public domain */ + +#ifndef __7Z_TYPES_H +#define __7Z_TYPES_H + +#ifdef _WIN32 +/* #include <windows.h> */ +#else +#include <errno.h> +#endif + +#include <stddef.h> + +#ifndef EXTERN_C_BEGIN +#ifdef __cplusplus +#define EXTERN_C_BEGIN extern "C" { +#define EXTERN_C_END } +#else +#define EXTERN_C_BEGIN +#define EXTERN_C_END +#endif +#endif + +EXTERN_C_BEGIN + +#define SZ_OK 0 + +#define SZ_ERROR_DATA 1 +#define SZ_ERROR_MEM 2 +#define SZ_ERROR_CRC 3 +#define SZ_ERROR_UNSUPPORTED 4 +#define SZ_ERROR_PARAM 5 +#define SZ_ERROR_INPUT_EOF 6 +#define SZ_ERROR_OUTPUT_EOF 7 +#define SZ_ERROR_READ 8 +#define SZ_ERROR_WRITE 9 +#define SZ_ERROR_PROGRESS 10 +#define SZ_ERROR_FAIL 11 +#define SZ_ERROR_THREAD 12 + +#define SZ_ERROR_ARCHIVE 16 +#define SZ_ERROR_NO_ARCHIVE 17 + +typedef int SRes; + + +#ifdef _MSC_VER + #if _MSC_VER > 1200 + #define MY_ALIGN(n) __declspec(align(n)) + #else + #define MY_ALIGN(n) + #endif +#else + #define MY_ALIGN(n) __attribute__ ((aligned(n))) +#endif + + +#ifdef _WIN32 + +/* typedef DWORD WRes; */ +typedef unsigned WRes; +#define MY_SRes_HRESULT_FROM_WRes(x) HRESULT_FROM_WIN32(x) + +// #define MY_HRES_ERROR__INTERNAL_ERROR MY_SRes_HRESULT_FROM_WRes(ERROR_INTERNAL_ERROR) + +#else // _WIN32 + +// #define ENV_HAVE_LSTAT +typedef int WRes; + +// (FACILITY_ERRNO = 0x800) is 7zip's FACILITY constant to represent (errno) errors in HRESULT +#define MY__FACILITY_ERRNO 0x800 +#define MY__FACILITY_WIN32 7 +#define MY__FACILITY__WRes MY__FACILITY_ERRNO + +#define MY_HRESULT_FROM_errno_CONST_ERROR(x) ((HRESULT)( \ + ( (HRESULT)(x) & 0x0000FFFF) \ + | (MY__FACILITY__WRes << 16) \ + | (HRESULT)0x80000000 )) + +#define MY_SRes_HRESULT_FROM_WRes(x) \ + ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : MY_HRESULT_FROM_errno_CONST_ERROR(x)) + +// we call macro HRESULT_FROM_WIN32 for system errors (WRes) that are (errno) +#define HRESULT_FROM_WIN32(x) MY_SRes_HRESULT_FROM_WRes(x) + +/* +#define ERROR_FILE_NOT_FOUND 2L +#define ERROR_ACCESS_DENIED 5L +#define ERROR_NO_MORE_FILES 18L +#define ERROR_LOCK_VIOLATION 33L +#define ERROR_FILE_EXISTS 80L +#define ERROR_DISK_FULL 112L +#define ERROR_NEGATIVE_SEEK 131L +#define ERROR_ALREADY_EXISTS 183L +#define ERROR_DIRECTORY 267L +#define ERROR_TOO_MANY_POSTS 298L + +#define ERROR_INTERNAL_ERROR 1359L +#define ERROR_INVALID_REPARSE_DATA 4392L +#define ERROR_REPARSE_TAG_INVALID 4393L +#define ERROR_REPARSE_TAG_MISMATCH 4394L +*/ + +// we use errno equivalents for some WIN32 errors: + +#define ERROR_INVALID_PARAMETER EINVAL +#define ERROR_INVALID_FUNCTION EINVAL +#define ERROR_ALREADY_EXISTS EEXIST +#define ERROR_FILE_EXISTS EEXIST +#define ERROR_PATH_NOT_FOUND ENOENT +#define ERROR_FILE_NOT_FOUND ENOENT +#define ERROR_DISK_FULL ENOSPC +// #define ERROR_INVALID_HANDLE EBADF + +// we use FACILITY_WIN32 for errors that has no errno equivalent +// Too many posts were made to a semaphore. +#define ERROR_TOO_MANY_POSTS ((HRESULT)0x8007012AL) +#define ERROR_INVALID_REPARSE_DATA ((HRESULT)0x80071128L) +#define ERROR_REPARSE_TAG_INVALID ((HRESULT)0x80071129L) + +// if (MY__FACILITY__WRes != FACILITY_WIN32), +// we use FACILITY_WIN32 for COM errors: +#define E_OUTOFMEMORY ((HRESULT)0x8007000EL) +#define E_INVALIDARG ((HRESULT)0x80070057L) +#define MY__E_ERROR_NEGATIVE_SEEK ((HRESULT)0x80070083L) + +/* +// we can use FACILITY_ERRNO for some COM errors, that have errno equivalents: +#define E_OUTOFMEMORY MY_HRESULT_FROM_errno_CONST_ERROR(ENOMEM) +#define E_INVALIDARG MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL) +#define MY__E_ERROR_NEGATIVE_SEEK MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL) +*/ + +#define TEXT(quote) quote + +#define FILE_ATTRIBUTE_READONLY 0x0001 +#define FILE_ATTRIBUTE_HIDDEN 0x0002 +#define FILE_ATTRIBUTE_SYSTEM 0x0004 +#define FILE_ATTRIBUTE_DIRECTORY 0x0010 +#define FILE_ATTRIBUTE_ARCHIVE 0x0020 +#define FILE_ATTRIBUTE_DEVICE 0x0040 +#define FILE_ATTRIBUTE_NORMAL 0x0080 +#define FILE_ATTRIBUTE_TEMPORARY 0x0100 +#define FILE_ATTRIBUTE_SPARSE_FILE 0x0200 +#define FILE_ATTRIBUTE_REPARSE_POINT 0x0400 +#define FILE_ATTRIBUTE_COMPRESSED 0x0800 +#define FILE_ATTRIBUTE_OFFLINE 0x1000 +#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x2000 +#define FILE_ATTRIBUTE_ENCRYPTED 0x4000 + +#define FILE_ATTRIBUTE_UNIX_EXTENSION 0x8000 /* trick for Unix */ + +#endif + + +#ifndef RINOK +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } +#endif + +#ifndef RINOK_WRes +#define RINOK_WRes(x) { WRes __result__ = (x); if (__result__ != 0) return __result__; } +#endif + +typedef unsigned char Byte; +typedef short Int16; +typedef unsigned short UInt16; + +#ifdef _LZMA_UINT32_IS_ULONG +typedef long Int32; +typedef unsigned long UInt32; +#else +typedef int Int32; +typedef unsigned int UInt32; +#endif + + +#ifndef _WIN32 + +typedef int INT; +typedef Int32 INT32; +typedef unsigned int UINT; +typedef UInt32 UINT32; +typedef INT32 LONG; // LONG, ULONG and DWORD must be 32-bit for _WIN32 compatibility +typedef UINT32 ULONG; + +#undef DWORD +typedef UINT32 DWORD; + +#define VOID void + +#define HRESULT LONG + +typedef void *LPVOID; +// typedef void VOID; +// typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR; +// gcc / clang on Unix : sizeof(long==sizeof(void*) in 32 or 64 bits) +typedef long INT_PTR; +typedef unsigned long UINT_PTR; +typedef long LONG_PTR; +typedef unsigned long DWORD_PTR; + +typedef size_t SIZE_T; + +#endif // _WIN32 + + +#define MY_HRES_ERROR__INTERNAL_ERROR ((HRESULT)0x8007054FL) + + +#ifdef _SZ_NO_INT_64 + +/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers. + NOTES: Some code will work incorrectly in that case! */ + +typedef long Int64; +typedef unsigned long UInt64; + +#else + +#if defined(_MSC_VER) || defined(__BORLANDC__) +typedef __int64 Int64; +typedef unsigned __int64 UInt64; +#define UINT64_CONST(n) n +#else +typedef long long int Int64; +typedef unsigned long long int UInt64; +#define UINT64_CONST(n) n ## ULL +#endif + +#endif + +#ifdef _LZMA_NO_SYSTEM_SIZE_T +typedef UInt32 SizeT; +#else +typedef size_t SizeT; +#endif + +typedef int BoolInt; +/* typedef BoolInt Bool; */ +#define True 1 +#define False 0 + + +#ifdef _WIN32 +#define MY_STD_CALL __stdcall +#else +#define MY_STD_CALL +#endif + +#ifdef _MSC_VER + +#if _MSC_VER >= 1300 +#define MY_NO_INLINE __declspec(noinline) +#else +#define MY_NO_INLINE +#endif + +#define MY_FORCE_INLINE __forceinline + +#define MY_CDECL __cdecl +#define MY_FAST_CALL __fastcall + +#else // _MSC_VER + +#if (defined(__GNUC__) && (__GNUC__ >= 4)) \ + || (defined(__clang__) && (__clang_major__ >= 4)) \ + || defined(__INTEL_COMPILER) \ + || defined(__xlC__) +#define MY_NO_INLINE __attribute__((noinline)) +// #define MY_FORCE_INLINE __attribute__((always_inline)) inline +#else +#define MY_NO_INLINE +#endif + +#define MY_FORCE_INLINE + + +#define MY_CDECL + +#if defined(_M_IX86) \ + || defined(__i386__) +// #define MY_FAST_CALL __attribute__((fastcall)) +// #define MY_FAST_CALL __attribute__((cdecl)) +#define MY_FAST_CALL +#elif defined(MY_CPU_AMD64) +// #define MY_FAST_CALL __attribute__((ms_abi)) +#define MY_FAST_CALL +#else +#define MY_FAST_CALL +#endif + +#endif // _MSC_VER + + +/* The following interfaces use first parameter as pointer to structure */ + +typedef struct IByteIn IByteIn; +struct IByteIn +{ + Byte (*Read)(const IByteIn *p); /* reads one byte, returns 0 in case of EOF or error */ +}; +#define IByteIn_Read(p) (p)->Read(p) + + +typedef struct IByteOut IByteOut; +struct IByteOut +{ + void (*Write)(const IByteOut *p, Byte b); +}; +#define IByteOut_Write(p, b) (p)->Write(p, b) + + +typedef struct ISeqInStream ISeqInStream; +struct ISeqInStream +{ + SRes (*Read)(const ISeqInStream *p, void *buf, size_t *size); + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. + (output(*size) < input(*size)) is allowed */ +}; +#define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size) + +/* it can return SZ_ERROR_INPUT_EOF */ +SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size); +SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType); +SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf); + + +typedef struct ISeqOutStream ISeqOutStream; +struct ISeqOutStream +{ + size_t (*Write)(const ISeqOutStream *p, const void *buf, size_t size); + /* Returns: result - the number of actually written bytes. + (result < size) means error */ +}; +#define ISeqOutStream_Write(p, buf, size) (p)->Write(p, buf, size) + +typedef enum +{ + SZ_SEEK_SET = 0, + SZ_SEEK_CUR = 1, + SZ_SEEK_END = 2 +} ESzSeek; + + +typedef struct ISeekInStream ISeekInStream; +struct ISeekInStream +{ + SRes (*Read)(const ISeekInStream *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ + SRes (*Seek)(const ISeekInStream *p, Int64 *pos, ESzSeek origin); +}; +#define ISeekInStream_Read(p, buf, size) (p)->Read(p, buf, size) +#define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) + + +typedef struct ILookInStream ILookInStream; +struct ILookInStream +{ + SRes (*Look)(const ILookInStream *p, const void **buf, size_t *size); + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. + (output(*size) > input(*size)) is not allowed + (output(*size) < input(*size)) is allowed */ + SRes (*Skip)(const ILookInStream *p, size_t offset); + /* offset must be <= output(*size) of Look */ + + SRes (*Read)(const ILookInStream *p, void *buf, size_t *size); + /* reads directly (without buffer). It's same as ISeqInStream::Read */ + SRes (*Seek)(const ILookInStream *p, Int64 *pos, ESzSeek origin); +}; + +#define ILookInStream_Look(p, buf, size) (p)->Look(p, buf, size) +#define ILookInStream_Skip(p, offset) (p)->Skip(p, offset) +#define ILookInStream_Read(p, buf, size) (p)->Read(p, buf, size) +#define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) + + +SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size); +SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset); + +/* reads via ILookInStream::Read */ +SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType); +SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size); + + + +typedef struct +{ + ILookInStream vt; + const ISeekInStream *realStream; + + size_t pos; + size_t size; /* it's data size */ + + /* the following variables must be set outside */ + Byte *buf; + size_t bufSize; +} CLookToRead2; + +void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead); + +#define LookToRead2_Init(p) { (p)->pos = (p)->size = 0; } + + +typedef struct +{ + ISeqInStream vt; + const ILookInStream *realStream; +} CSecToLook; + +void SecToLook_CreateVTable(CSecToLook *p); + + + +typedef struct +{ + ISeqInStream vt; + const ILookInStream *realStream; +} CSecToRead; + +void SecToRead_CreateVTable(CSecToRead *p); + + +typedef struct ICompressProgress ICompressProgress; + +struct ICompressProgress +{ + SRes (*Progress)(const ICompressProgress *p, UInt64 inSize, UInt64 outSize); + /* Returns: result. (result != SZ_OK) means break. + Value (UInt64)(Int64)-1 for size means unknown value. */ +}; +#define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize) + + + +typedef struct ISzAlloc ISzAlloc; +typedef const ISzAlloc * ISzAllocPtr; + +struct ISzAlloc +{ + void *(*Alloc)(ISzAllocPtr p, size_t size); + void (*Free)(ISzAllocPtr p, void *address); /* address can be 0 */ +}; + +#define ISzAlloc_Alloc(p, size) (p)->Alloc(p, size) +#define ISzAlloc_Free(p, a) (p)->Free(p, a) + +/* deprecated */ +#define IAlloc_Alloc(p, size) ISzAlloc_Alloc(p, size) +#define IAlloc_Free(p, a) ISzAlloc_Free(p, a) + + + + + +#ifndef MY_offsetof + #ifdef offsetof + #define MY_offsetof(type, m) offsetof(type, m) + /* + #define MY_offsetof(type, m) FIELD_OFFSET(type, m) + */ + #else + #define MY_offsetof(type, m) ((size_t)&(((type *)0)->m)) + #endif +#endif + + + +#ifndef MY_container_of + +/* +#define MY_container_of(ptr, type, m) container_of(ptr, type, m) +#define MY_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m) +#define MY_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m))) +#define MY_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m)))) +*/ + +/* + GCC shows warning: "perhaps the 'offsetof' macro was used incorrectly" + GCC 3.4.4 : classes with constructor + GCC 4.8.1 : classes with non-public variable members" +*/ + +#define MY_container_of(ptr, type, m) ((type *)(void *)((char *)(void *)(1 ? (ptr) : &((type *)0)->m) - MY_offsetof(type, m))) + +#endif + +#define CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(void *)(ptr)) + +/* +#define CONTAINER_FROM_VTBL(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) +*/ +#define CONTAINER_FROM_VTBL(ptr, type, m) MY_container_of(ptr, type, m) + +#define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) +/* +#define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL(ptr, type, m) +*/ + + +#define MY_memset_0_ARRAY(a) memset((a), 0, sizeof(a)) + +#ifdef _WIN32 + +#define CHAR_PATH_SEPARATOR '\\' +#define WCHAR_PATH_SEPARATOR L'\\' +#define STRING_PATH_SEPARATOR "\\" +#define WSTRING_PATH_SEPARATOR L"\\" + +#else + +#define CHAR_PATH_SEPARATOR '/' +#define WCHAR_PATH_SEPARATOR L'/' +#define STRING_PATH_SEPARATOR "/" +#define WSTRING_PATH_SEPARATOR L"/" + +#endif + +#define k_PropVar_TimePrec_0 0 +#define k_PropVar_TimePrec_Unix 1 +#define k_PropVar_TimePrec_DOS 2 +#define k_PropVar_TimePrec_HighPrec 3 +#define k_PropVar_TimePrec_Base 16 +#define k_PropVar_TimePrec_100ns (k_PropVar_TimePrec_Base + 7) +#define k_PropVar_TimePrec_1ns (k_PropVar_TimePrec_Base + 9) + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/7zVersion.h b/src/lzma-sdk/C/7zVersion.h new file mode 100644 index 0000000..345b5b5 --- /dev/null +++ b/src/lzma-sdk/C/7zVersion.h @@ -0,0 +1,27 @@ +#define MY_VER_MAJOR 22 +#define MY_VER_MINOR 00 +#define MY_VER_BUILD 0 +#define MY_VERSION_NUMBERS "22.00" +#define MY_VERSION MY_VERSION_NUMBERS + +#ifdef MY_CPU_NAME + #define MY_VERSION_CPU MY_VERSION " (" MY_CPU_NAME ")" +#else + #define MY_VERSION_CPU MY_VERSION +#endif + +#define MY_DATE "2022-06-15" +#undef MY_COPYRIGHT +#undef MY_VERSION_COPYRIGHT_DATE +#define MY_AUTHOR_NAME "Igor Pavlov" +#define MY_COPYRIGHT_PD "Igor Pavlov : Public domain" +#define MY_COPYRIGHT_CR "Copyright (c) 1999-2022 Igor Pavlov" + +#ifdef USE_COPYRIGHT_CR + #define MY_COPYRIGHT MY_COPYRIGHT_CR +#else + #define MY_COPYRIGHT MY_COPYRIGHT_PD +#endif + +#define MY_COPYRIGHT_DATE MY_COPYRIGHT " : " MY_DATE +#define MY_VERSION_COPYRIGHT_DATE MY_VERSION_CPU " : " MY_COPYRIGHT " : " MY_DATE diff --git a/src/lzma-sdk/C/Aes.h b/src/lzma-sdk/C/Aes.h new file mode 100644 index 0000000..602e25e --- /dev/null +++ b/src/lzma-sdk/C/Aes.h @@ -0,0 +1,60 @@ +/* Aes.h -- AES encryption / decryption +2018-04-28 : Igor Pavlov : Public domain */ + +#ifndef __AES_H +#define __AES_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define AES_BLOCK_SIZE 16 + +/* Call AesGenTables one time before other AES functions */ +void AesGenTables(void); + +/* UInt32 pointers must be 16-byte aligned */ + +/* 16-byte (4 * 32-bit words) blocks: 1 (IV) + 1 (keyMode) + 15 (AES-256 roundKeys) */ +#define AES_NUM_IVMRK_WORDS ((1 + 1 + 15) * 4) + +/* aes - 16-byte aligned pointer to keyMode+roundKeys sequence */ +/* keySize = 16 or 24 or 32 (bytes) */ +typedef void (MY_FAST_CALL *AES_SET_KEY_FUNC)(UInt32 *aes, const Byte *key, unsigned keySize); +void MY_FAST_CALL Aes_SetKey_Enc(UInt32 *aes, const Byte *key, unsigned keySize); +void MY_FAST_CALL Aes_SetKey_Dec(UInt32 *aes, const Byte *key, unsigned keySize); + +/* ivAes - 16-byte aligned pointer to iv+keyMode+roundKeys sequence: UInt32[AES_NUM_IVMRK_WORDS] */ +void AesCbc_Init(UInt32 *ivAes, const Byte *iv); /* iv size is AES_BLOCK_SIZE */ + +/* data - 16-byte aligned pointer to data */ +/* numBlocks - the number of 16-byte blocks in data array */ +typedef void (MY_FAST_CALL *AES_CODE_FUNC)(UInt32 *ivAes, Byte *data, size_t numBlocks); + +extern AES_CODE_FUNC g_AesCbc_Decode; +#ifndef _SFX +extern AES_CODE_FUNC g_AesCbc_Encode; +extern AES_CODE_FUNC g_AesCtr_Code; +#define k_Aes_SupportedFunctions_HW (1 << 2) +#define k_Aes_SupportedFunctions_HW_256 (1 << 3) +extern UInt32 g_Aes_SupportedFunctions_Flags; +#endif + + +#define DECLARE__AES_CODE_FUNC(funcName) \ + void MY_FAST_CALL funcName(UInt32 *ivAes, Byte *data, size_t numBlocks); + +DECLARE__AES_CODE_FUNC (AesCbc_Encode) +DECLARE__AES_CODE_FUNC (AesCbc_Decode) +DECLARE__AES_CODE_FUNC (AesCtr_Code) + +DECLARE__AES_CODE_FUNC (AesCbc_Encode_HW) +DECLARE__AES_CODE_FUNC (AesCbc_Decode_HW) +DECLARE__AES_CODE_FUNC (AesCtr_Code_HW) + +DECLARE__AES_CODE_FUNC (AesCbc_Decode_HW_256) +DECLARE__AES_CODE_FUNC (AesCtr_Code_HW_256) + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Alloc.h b/src/lzma-sdk/C/Alloc.h new file mode 100644 index 0000000..59de107 --- /dev/null +++ b/src/lzma-sdk/C/Alloc.h @@ -0,0 +1,58 @@ +/* Alloc.h -- Memory allocation functions +2021-07-13 : Igor Pavlov : Public domain */ + +#ifndef __COMMON_ALLOC_H +#define __COMMON_ALLOC_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +void *MyAlloc(size_t size); +void MyFree(void *address); + +#ifdef _WIN32 + +void SetLargePageSize(void); + +void *MidAlloc(size_t size); +void MidFree(void *address); +void *BigAlloc(size_t size); +void BigFree(void *address); + +#else + +#define MidAlloc(size) MyAlloc(size) +#define MidFree(address) MyFree(address) +#define BigAlloc(size) MyAlloc(size) +#define BigFree(address) MyFree(address) + +#endif + +extern const ISzAlloc g_Alloc; + +#ifdef _WIN32 +extern const ISzAlloc g_BigAlloc; +extern const ISzAlloc g_MidAlloc; +#else +#define g_BigAlloc g_AlignedAlloc +#define g_MidAlloc g_AlignedAlloc +#endif + +extern const ISzAlloc g_AlignedAlloc; + + +typedef struct +{ + ISzAlloc vt; + ISzAllocPtr baseAlloc; + unsigned numAlignBits; /* ((1 << numAlignBits) >= sizeof(void *)) */ + size_t offset; /* (offset == (k * sizeof(void *)) && offset < (1 << numAlignBits) */ +} CAlignOffsetAlloc; + +void AlignOffsetAlloc_CreateVTable(CAlignOffsetAlloc *p); + + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Bcj2.h b/src/lzma-sdk/C/Bcj2.h new file mode 100644 index 0000000..68893d2 --- /dev/null +++ b/src/lzma-sdk/C/Bcj2.h @@ -0,0 +1,146 @@ +/* Bcj2.h -- BCJ2 Converter for x86 code +2014-11-10 : Igor Pavlov : Public domain */ + +#ifndef __BCJ2_H +#define __BCJ2_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define BCJ2_NUM_STREAMS 4 + +enum +{ + BCJ2_STREAM_MAIN, + BCJ2_STREAM_CALL, + BCJ2_STREAM_JUMP, + BCJ2_STREAM_RC +}; + +enum +{ + BCJ2_DEC_STATE_ORIG_0 = BCJ2_NUM_STREAMS, + BCJ2_DEC_STATE_ORIG_1, + BCJ2_DEC_STATE_ORIG_2, + BCJ2_DEC_STATE_ORIG_3, + + BCJ2_DEC_STATE_ORIG, + BCJ2_DEC_STATE_OK +}; + +enum +{ + BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS, + BCJ2_ENC_STATE_OK +}; + + +#define BCJ2_IS_32BIT_STREAM(s) ((s) == BCJ2_STREAM_CALL || (s) == BCJ2_STREAM_JUMP) + +/* +CBcj2Dec / CBcj2Enc +bufs sizes: + BUF_SIZE(n) = lims[n] - bufs[n] +bufs sizes for BCJ2_STREAM_CALL and BCJ2_STREAM_JUMP must be mutliply of 4: + (BUF_SIZE(BCJ2_STREAM_CALL) & 3) == 0 + (BUF_SIZE(BCJ2_STREAM_JUMP) & 3) == 0 +*/ + +/* +CBcj2Dec: +dest is allowed to overlap with bufs[BCJ2_STREAM_MAIN], with the following conditions: + bufs[BCJ2_STREAM_MAIN] >= dest && + bufs[BCJ2_STREAM_MAIN] - dest >= tempReserv + + BUF_SIZE(BCJ2_STREAM_CALL) + + BUF_SIZE(BCJ2_STREAM_JUMP) + tempReserv = 0 : for first call of Bcj2Dec_Decode + tempReserv = 4 : for any other calls of Bcj2Dec_Decode + overlap with offset = 1 is not allowed +*/ + +typedef struct +{ + const Byte *bufs[BCJ2_NUM_STREAMS]; + const Byte *lims[BCJ2_NUM_STREAMS]; + Byte *dest; + const Byte *destLim; + + unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */ + + UInt32 ip; + Byte temp[4]; + UInt32 range; + UInt32 code; + UInt16 probs[2 + 256]; +} CBcj2Dec; + +void Bcj2Dec_Init(CBcj2Dec *p); + +/* Returns: SZ_OK or SZ_ERROR_DATA */ +SRes Bcj2Dec_Decode(CBcj2Dec *p); + +#define Bcj2Dec_IsFinished(_p_) ((_p_)->code == 0) + + + +typedef enum +{ + BCJ2_ENC_FINISH_MODE_CONTINUE, + BCJ2_ENC_FINISH_MODE_END_BLOCK, + BCJ2_ENC_FINISH_MODE_END_STREAM +} EBcj2Enc_FinishMode; + +typedef struct +{ + Byte *bufs[BCJ2_NUM_STREAMS]; + const Byte *lims[BCJ2_NUM_STREAMS]; + const Byte *src; + const Byte *srcLim; + + unsigned state; + EBcj2Enc_FinishMode finishMode; + + Byte prevByte; + + Byte cache; + UInt32 range; + UInt64 low; + UInt64 cacheSize; + + UInt32 ip; + + /* 32-bit ralative offset in JUMP/CALL commands is + - (mod 4 GB) in 32-bit mode + - signed Int32 in 64-bit mode + We use (mod 4 GB) check for fileSize. + Use fileSize up to 2 GB, if you want to support 32-bit and 64-bit code conversion. */ + UInt32 fileIp; + UInt32 fileSize; /* (fileSize <= ((UInt32)1 << 31)), 0 means no_limit */ + UInt32 relatLimit; /* (relatLimit <= ((UInt32)1 << 31)), 0 means desable_conversion */ + + UInt32 tempTarget; + unsigned tempPos; + Byte temp[4 * 2]; + + unsigned flushPos; + + UInt16 probs[2 + 256]; +} CBcj2Enc; + +void Bcj2Enc_Init(CBcj2Enc *p); +void Bcj2Enc_Encode(CBcj2Enc *p); + +#define Bcj2Enc_Get_InputData_Size(p) ((SizeT)((p)->srcLim - (p)->src) + (p)->tempPos) +#define Bcj2Enc_IsFinished(p) ((p)->flushPos == 5) + + +#define BCJ2_RELAT_LIMIT_NUM_BITS 26 +#define BCJ2_RELAT_LIMIT ((UInt32)1 << BCJ2_RELAT_LIMIT_NUM_BITS) + +/* limit for CBcj2Enc::fileSize variable */ +#define BCJ2_FileSize_MAX ((UInt32)1 << 31) + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Bra.h b/src/lzma-sdk/C/Bra.h new file mode 100644 index 0000000..aba8dce --- /dev/null +++ b/src/lzma-sdk/C/Bra.h @@ -0,0 +1,64 @@ +/* Bra.h -- Branch converters for executables +2013-01-18 : Igor Pavlov : Public domain */ + +#ifndef __BRA_H +#define __BRA_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +/* +These functions convert relative addresses to absolute addresses +in CALL instructions to increase the compression ratio. + + In: + data - data buffer + size - size of data + ip - current virtual Instruction Pinter (IP) value + state - state variable for x86 converter + encoding - 0 (for decoding), 1 (for encoding) + + Out: + state - state variable for x86 converter + + Returns: + The number of processed bytes. If you call these functions with multiple calls, + you must start next call with first byte after block of processed bytes. + + Type Endian Alignment LookAhead + + x86 little 1 4 + ARMT little 2 2 + ARM little 4 0 + PPC big 4 0 + SPARC big 4 0 + IA64 little 16 0 + + size must be >= Alignment + LookAhead, if it's not last block. + If (size < Alignment + LookAhead), converter returns 0. + + Example: + + UInt32 ip = 0; + for () + { + ; size must be >= Alignment + LookAhead, if it's not last block + SizeT processed = Convert(data, size, ip, 1); + data += processed; + size -= processed; + ip += processed; + } +*/ + +#define x86_Convert_Init(state) { state = 0; } +SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding); +SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Compiler.h b/src/lzma-sdk/C/Compiler.h new file mode 100644 index 0000000..eba3742 --- /dev/null +++ b/src/lzma-sdk/C/Compiler.h @@ -0,0 +1,43 @@ +/* Compiler.h +2021-01-05 : Igor Pavlov : Public domain */ + +#ifndef __7Z_COMPILER_H +#define __7Z_COMPILER_H + + #ifdef __clang__ + #pragma clang diagnostic ignored "-Wunused-private-field" + #endif + +#ifdef _MSC_VER + + #ifdef UNDER_CE + #define RPC_NO_WINDOWS_H + /* #pragma warning(disable : 4115) // '_RPC_ASYNC_STATE' : named type definition in parentheses */ + #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union + #pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int + #endif + + #if _MSC_VER >= 1300 + #pragma warning(disable : 4996) // This function or variable may be unsafe + #else + #pragma warning(disable : 4511) // copy constructor could not be generated + #pragma warning(disable : 4512) // assignment operator could not be generated + #pragma warning(disable : 4514) // unreferenced inline function has been removed + #pragma warning(disable : 4702) // unreachable code + #pragma warning(disable : 4710) // not inlined + #pragma warning(disable : 4714) // function marked as __forceinline not inlined + #pragma warning(disable : 4786) // identifier was truncated to '255' characters in the debug information + #endif + + #ifdef __clang__ + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + #pragma clang diagnostic ignored "-Wmicrosoft-exception-spec" + // #pragma clang diagnostic ignored "-Wreserved-id-macro" + #endif + +#endif + +#define UNUSED_VAR(x) (void)x; +/* #define UNUSED_VAR(x) x=x; */ + +#endif diff --git a/src/lzma-sdk/C/CpuArch.h b/src/lzma-sdk/C/CpuArch.h new file mode 100644 index 0000000..b300a5a --- /dev/null +++ b/src/lzma-sdk/C/CpuArch.h @@ -0,0 +1,442 @@ +/* CpuArch.h -- CPU specific code +2021-07-13 : Igor Pavlov : Public domain */ + +#ifndef __CPU_ARCH_H +#define __CPU_ARCH_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +/* +MY_CPU_LE means that CPU is LITTLE ENDIAN. +MY_CPU_BE means that CPU is BIG ENDIAN. +If MY_CPU_LE and MY_CPU_BE are not defined, we don't know about ENDIANNESS of platform. + +MY_CPU_LE_UNALIGN means that CPU is LITTLE ENDIAN and CPU supports unaligned memory accesses. + +MY_CPU_64BIT means that processor can work with 64-bit registers. + MY_CPU_64BIT can be used to select fast code branch + MY_CPU_64BIT doesn't mean that (sizeof(void *) == 8) +*/ + +#if defined(_M_X64) \ + || defined(_M_AMD64) \ + || defined(__x86_64__) \ + || defined(__AMD64__) \ + || defined(__amd64__) + #define MY_CPU_AMD64 + #ifdef __ILP32__ + #define MY_CPU_NAME "x32" + #define MY_CPU_SIZEOF_POINTER 4 + #else + #define MY_CPU_NAME "x64" + #define MY_CPU_SIZEOF_POINTER 8 + #endif + #define MY_CPU_64BIT +#endif + + +#if defined(_M_IX86) \ + || defined(__i386__) + #define MY_CPU_X86 + #define MY_CPU_NAME "x86" + /* #define MY_CPU_32BIT */ + #define MY_CPU_SIZEOF_POINTER 4 +#endif + + +#if defined(_M_ARM64) \ + || defined(__AARCH64EL__) \ + || defined(__AARCH64EB__) \ + || defined(__aarch64__) + #define MY_CPU_ARM64 + #define MY_CPU_NAME "arm64" + #define MY_CPU_64BIT +#endif + + +#if defined(_M_ARM) \ + || defined(_M_ARM_NT) \ + || defined(_M_ARMT) \ + || defined(__arm__) \ + || defined(__thumb__) \ + || defined(__ARMEL__) \ + || defined(__ARMEB__) \ + || defined(__THUMBEL__) \ + || defined(__THUMBEB__) + #define MY_CPU_ARM + + #if defined(__thumb__) || defined(__THUMBEL__) || defined(_M_ARMT) + #define MY_CPU_NAME "armt" + #else + #define MY_CPU_NAME "arm" + #endif + /* #define MY_CPU_32BIT */ + #define MY_CPU_SIZEOF_POINTER 4 +#endif + + +#if defined(_M_IA64) \ + || defined(__ia64__) + #define MY_CPU_IA64 + #define MY_CPU_NAME "ia64" + #define MY_CPU_64BIT +#endif + + +#if defined(__mips64) \ + || defined(__mips64__) \ + || (defined(__mips) && (__mips == 64 || __mips == 4 || __mips == 3)) + #define MY_CPU_NAME "mips64" + #define MY_CPU_64BIT +#elif defined(__mips__) + #define MY_CPU_NAME "mips" + /* #define MY_CPU_32BIT */ +#endif + + +#if defined(__ppc64__) \ + || defined(__powerpc64__) \ + || defined(__ppc__) \ + || defined(__powerpc__) \ + || defined(__PPC__) \ + || defined(_POWER) + +#if defined(__ppc64__) \ + || defined(__powerpc64__) \ + || defined(_LP64) \ + || defined(__64BIT__) + #ifdef __ILP32__ + #define MY_CPU_NAME "ppc64-32" + #define MY_CPU_SIZEOF_POINTER 4 + #else + #define MY_CPU_NAME "ppc64" + #define MY_CPU_SIZEOF_POINTER 8 + #endif + #define MY_CPU_64BIT +#else + #define MY_CPU_NAME "ppc" + #define MY_CPU_SIZEOF_POINTER 4 + /* #define MY_CPU_32BIT */ +#endif +#endif + + +#if defined(__sparc64__) + #define MY_CPU_NAME "sparc64" + #define MY_CPU_64BIT +#elif defined(__sparc__) + #define MY_CPU_NAME "sparc" + /* #define MY_CPU_32BIT */ +#endif + + +#if defined(MY_CPU_X86) || defined(MY_CPU_AMD64) +#define MY_CPU_X86_OR_AMD64 +#endif + +#if defined(MY_CPU_ARM) || defined(MY_CPU_ARM64) +#define MY_CPU_ARM_OR_ARM64 +#endif + + +#ifdef _WIN32 + + #ifdef MY_CPU_ARM + #define MY_CPU_ARM_LE + #endif + + #ifdef MY_CPU_ARM64 + #define MY_CPU_ARM64_LE + #endif + + #ifdef _M_IA64 + #define MY_CPU_IA64_LE + #endif + +#endif + + +#if defined(MY_CPU_X86_OR_AMD64) \ + || defined(MY_CPU_ARM_LE) \ + || defined(MY_CPU_ARM64_LE) \ + || defined(MY_CPU_IA64_LE) \ + || defined(__LITTLE_ENDIAN__) \ + || defined(__ARMEL__) \ + || defined(__THUMBEL__) \ + || defined(__AARCH64EL__) \ + || defined(__MIPSEL__) \ + || defined(__MIPSEL) \ + || defined(_MIPSEL) \ + || defined(__BFIN__) \ + || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) + #define MY_CPU_LE +#endif + +#if defined(__BIG_ENDIAN__) \ + || defined(__ARMEB__) \ + || defined(__THUMBEB__) \ + || defined(__AARCH64EB__) \ + || defined(__MIPSEB__) \ + || defined(__MIPSEB) \ + || defined(_MIPSEB) \ + || defined(__m68k__) \ + || defined(__s390__) \ + || defined(__s390x__) \ + || defined(__zarch__) \ + || (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) + #define MY_CPU_BE +#endif + + +#if defined(MY_CPU_LE) && defined(MY_CPU_BE) + #error Stop_Compiling_Bad_Endian +#endif + + +#if defined(MY_CPU_32BIT) && defined(MY_CPU_64BIT) + #error Stop_Compiling_Bad_32_64_BIT +#endif + +#ifdef __SIZEOF_POINTER__ + #ifdef MY_CPU_SIZEOF_POINTER + #if MY_CPU_SIZEOF_POINTER != __SIZEOF_POINTER__ + #error Stop_Compiling_Bad_MY_CPU_PTR_SIZE + #endif + #else + #define MY_CPU_SIZEOF_POINTER __SIZEOF_POINTER__ + #endif +#endif + +#if defined(MY_CPU_SIZEOF_POINTER) && (MY_CPU_SIZEOF_POINTER == 4) +#if defined (_LP64) + #error Stop_Compiling_Bad_MY_CPU_PTR_SIZE +#endif +#endif + +#ifdef _MSC_VER + #if _MSC_VER >= 1300 + #define MY_CPU_pragma_pack_push_1 __pragma(pack(push, 1)) + #define MY_CPU_pragma_pop __pragma(pack(pop)) + #else + #define MY_CPU_pragma_pack_push_1 + #define MY_CPU_pragma_pop + #endif +#else + #ifdef __xlC__ + #define MY_CPU_pragma_pack_push_1 _Pragma("pack(1)") + #define MY_CPU_pragma_pop _Pragma("pack()") + #else + #define MY_CPU_pragma_pack_push_1 _Pragma("pack(push, 1)") + #define MY_CPU_pragma_pop _Pragma("pack(pop)") + #endif +#endif + + +#ifndef MY_CPU_NAME + #ifdef MY_CPU_LE + #define MY_CPU_NAME "LE" + #elif defined(MY_CPU_BE) + #define MY_CPU_NAME "BE" + #else + /* + #define MY_CPU_NAME "" + */ + #endif +#endif + + + + + +#ifdef MY_CPU_LE + #if defined(MY_CPU_X86_OR_AMD64) \ + || defined(MY_CPU_ARM64) + #define MY_CPU_LE_UNALIGN + #define MY_CPU_LE_UNALIGN_64 + #elif defined(__ARM_FEATURE_UNALIGNED) + /* gcc9 for 32-bit arm can use LDRD instruction that requires 32-bit alignment. + So we can't use unaligned 64-bit operations. */ + #define MY_CPU_LE_UNALIGN + #endif +#endif + + +#ifdef MY_CPU_LE_UNALIGN + +#define GetUi16(p) (*(const UInt16 *)(const void *)(p)) +#define GetUi32(p) (*(const UInt32 *)(const void *)(p)) +#ifdef MY_CPU_LE_UNALIGN_64 +#define GetUi64(p) (*(const UInt64 *)(const void *)(p)) +#endif + +#define SetUi16(p, v) { *(UInt16 *)(void *)(p) = (v); } +#define SetUi32(p, v) { *(UInt32 *)(void *)(p) = (v); } +#ifdef MY_CPU_LE_UNALIGN_64 +#define SetUi64(p, v) { *(UInt64 *)(void *)(p) = (v); } +#endif + +#else + +#define GetUi16(p) ( (UInt16) ( \ + ((const Byte *)(p))[0] | \ + ((UInt16)((const Byte *)(p))[1] << 8) )) + +#define GetUi32(p) ( \ + ((const Byte *)(p))[0] | \ + ((UInt32)((const Byte *)(p))[1] << 8) | \ + ((UInt32)((const Byte *)(p))[2] << 16) | \ + ((UInt32)((const Byte *)(p))[3] << 24)) + +#define SetUi16(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \ + _ppp_[0] = (Byte)_vvv_; \ + _ppp_[1] = (Byte)(_vvv_ >> 8); } + +#define SetUi32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \ + _ppp_[0] = (Byte)_vvv_; \ + _ppp_[1] = (Byte)(_vvv_ >> 8); \ + _ppp_[2] = (Byte)(_vvv_ >> 16); \ + _ppp_[3] = (Byte)(_vvv_ >> 24); } + +#endif + + +#ifndef MY_CPU_LE_UNALIGN_64 + +#define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32)) + +#define SetUi64(p, v) { Byte *_ppp2_ = (Byte *)(p); UInt64 _vvv2_ = (v); \ + SetUi32(_ppp2_ , (UInt32)_vvv2_); \ + SetUi32(_ppp2_ + 4, (UInt32)(_vvv2_ >> 32)); } + +#endif + + + + +#ifdef __has_builtin + #define MY__has_builtin(x) __has_builtin(x) +#else + #define MY__has_builtin(x) 0 +#endif + +#if defined(MY_CPU_LE_UNALIGN) && /* defined(_WIN64) && */ defined(_MSC_VER) && (_MSC_VER >= 1300) + +/* Note: we use bswap instruction, that is unsupported in 386 cpu */ + +#include <stdlib.h> + +#pragma intrinsic(_byteswap_ushort) +#pragma intrinsic(_byteswap_ulong) +#pragma intrinsic(_byteswap_uint64) + +/* #define GetBe16(p) _byteswap_ushort(*(const UInt16 *)(const Byte *)(p)) */ +#define GetBe32(p) _byteswap_ulong (*(const UInt32 *)(const void *)(p)) +#define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const void *)(p)) + +#define SetBe32(p, v) (*(UInt32 *)(void *)(p)) = _byteswap_ulong(v) + +#elif defined(MY_CPU_LE_UNALIGN) && ( \ + (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \ + || (defined(__clang__) && MY__has_builtin(__builtin_bswap16)) ) + +/* #define GetBe16(p) __builtin_bswap16(*(const UInt16 *)(const void *)(p)) */ +#define GetBe32(p) __builtin_bswap32(*(const UInt32 *)(const void *)(p)) +#define GetBe64(p) __builtin_bswap64(*(const UInt64 *)(const void *)(p)) + +#define SetBe32(p, v) (*(UInt32 *)(void *)(p)) = __builtin_bswap32(v) + +#else + +#define GetBe32(p) ( \ + ((UInt32)((const Byte *)(p))[0] << 24) | \ + ((UInt32)((const Byte *)(p))[1] << 16) | \ + ((UInt32)((const Byte *)(p))[2] << 8) | \ + ((const Byte *)(p))[3] ) + +#define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4)) + +#define SetBe32(p, v) { Byte *_ppp_ = (Byte *)(p); UInt32 _vvv_ = (v); \ + _ppp_[0] = (Byte)(_vvv_ >> 24); \ + _ppp_[1] = (Byte)(_vvv_ >> 16); \ + _ppp_[2] = (Byte)(_vvv_ >> 8); \ + _ppp_[3] = (Byte)_vvv_; } + +#endif + + +#ifndef GetBe16 + +#define GetBe16(p) ( (UInt16) ( \ + ((UInt16)((const Byte *)(p))[0] << 8) | \ + ((const Byte *)(p))[1] )) + +#endif + + + +#ifdef MY_CPU_X86_OR_AMD64 + +typedef struct +{ + UInt32 maxFunc; + UInt32 vendor[3]; + UInt32 ver; + UInt32 b; + UInt32 c; + UInt32 d; +} Cx86cpuid; + +enum +{ + CPU_FIRM_INTEL, + CPU_FIRM_AMD, + CPU_FIRM_VIA +}; + +void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d); + +BoolInt x86cpuid_CheckAndRead(Cx86cpuid *p); +int x86cpuid_GetFirm(const Cx86cpuid *p); + +#define x86cpuid_GetFamily(ver) (((ver >> 16) & 0xFF0) | ((ver >> 8) & 0xF)) +#define x86cpuid_GetModel(ver) (((ver >> 12) & 0xF0) | ((ver >> 4) & 0xF)) +#define x86cpuid_GetStepping(ver) (ver & 0xF) + +BoolInt CPU_Is_InOrder(void); + +BoolInt CPU_IsSupported_AES(void); +BoolInt CPU_IsSupported_AVX2(void); +BoolInt CPU_IsSupported_VAES_AVX2(void); +BoolInt CPU_IsSupported_SSSE3(void); +BoolInt CPU_IsSupported_SSE41(void); +BoolInt CPU_IsSupported_SHA(void); +BoolInt CPU_IsSupported_PageGB(void); + +#elif defined(MY_CPU_ARM_OR_ARM64) + +BoolInt CPU_IsSupported_CRC32(void); +BoolInt CPU_IsSupported_NEON(void); + +#if defined(_WIN32) +BoolInt CPU_IsSupported_CRYPTO(void); +#define CPU_IsSupported_SHA1 CPU_IsSupported_CRYPTO +#define CPU_IsSupported_SHA2 CPU_IsSupported_CRYPTO +#define CPU_IsSupported_AES CPU_IsSupported_CRYPTO +#else +BoolInt CPU_IsSupported_SHA1(void); +BoolInt CPU_IsSupported_SHA2(void); +BoolInt CPU_IsSupported_AES(void); +#endif + +#endif + +#if defined(__APPLE__) +int My_sysctlbyname_Get(const char *name, void *buf, size_t *bufSize); +int My_sysctlbyname_Get_UInt32(const char *name, UInt32 *val); +#endif + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Delta.h b/src/lzma-sdk/C/Delta.h new file mode 100644 index 0000000..e59d5a2 --- /dev/null +++ b/src/lzma-sdk/C/Delta.h @@ -0,0 +1,19 @@ +/* Delta.h -- Delta converter +2013-01-18 : Igor Pavlov : Public domain */ + +#ifndef __DELTA_H +#define __DELTA_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define DELTA_STATE_SIZE 256 + +void Delta_Init(Byte *state); +void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size); +void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/DllSecur.h b/src/lzma-sdk/C/DllSecur.h new file mode 100644 index 0000000..0fd8070 --- /dev/null +++ b/src/lzma-sdk/C/DllSecur.h @@ -0,0 +1,20 @@ +/* DllSecur.h -- DLL loading for security +2018-02-19 : Igor Pavlov : Public domain */ + +#ifndef __DLL_SECUR_H +#define __DLL_SECUR_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#ifdef _WIN32 + +void My_SetDefaultDllDirectories(void); +void LoadSecurityDlls(void); + +#endif + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/LzFind.h b/src/lzma-sdk/C/LzFind.h new file mode 100644 index 0000000..8f9fade --- /dev/null +++ b/src/lzma-sdk/C/LzFind.h @@ -0,0 +1,136 @@ +/* LzFind.h -- Match finder for LZ algorithms +2021-07-13 : Igor Pavlov : Public domain */ + +#ifndef __LZ_FIND_H +#define __LZ_FIND_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +typedef UInt32 CLzRef; + +typedef struct _CMatchFinder +{ + Byte *buffer; + UInt32 pos; + UInt32 posLimit; + UInt32 streamPos; /* wrap over Zero is allowed (streamPos < pos). Use (UInt32)(streamPos - pos) */ + UInt32 lenLimit; + + UInt32 cyclicBufferPos; + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ + + Byte streamEndWasReached; + Byte btMode; + Byte bigHash; + Byte directInput; + + UInt32 matchMaxLen; + CLzRef *hash; + CLzRef *son; + UInt32 hashMask; + UInt32 cutValue; + + Byte *bufferBase; + ISeqInStream *stream; + + UInt32 blockSize; + UInt32 keepSizeBefore; + UInt32 keepSizeAfter; + + UInt32 numHashBytes; + size_t directInputRem; + UInt32 historySize; + UInt32 fixedHashSize; + UInt32 hashSizeSum; + SRes result; + UInt32 crc[256]; + size_t numRefs; + + UInt64 expectedDataSize; +} CMatchFinder; + +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((const Byte *)(p)->buffer) + +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((UInt32)((p)->streamPos - (p)->pos)) + +/* +#define Inline_MatchFinder_IsFinishedOK(p) \ + ((p)->streamEndWasReached \ + && (p)->streamPos == (p)->pos \ + && (!(p)->directInput || (p)->directInputRem == 0)) +*/ + +int MatchFinder_NeedMove(CMatchFinder *p); +/* Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p); */ +void MatchFinder_MoveBlock(CMatchFinder *p); +void MatchFinder_ReadIfRequired(CMatchFinder *p); + +void MatchFinder_Construct(CMatchFinder *p); + +/* Conditions: + historySize <= 3 GB + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB +*/ +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, + ISzAllocPtr alloc); +void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc); +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems); +// void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue); + +/* +#define Inline_MatchFinder_InitPos(p, val) \ + (p)->pos = (val); \ + (p)->streamPos = (val); +*/ + +#define Inline_MatchFinder_ReduceOffsets(p, subValue) \ + (p)->pos -= (subValue); \ + (p)->streamPos -= (subValue); + + +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son, + size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, + UInt32 *distances, UInt32 maxLen); + +/* +Conditions: + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func. + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function +*/ + +typedef void (*Mf_Init_Func)(void *object); +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object); +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object); +typedef UInt32 * (*Mf_GetMatches_Func)(void *object, UInt32 *distances); +typedef void (*Mf_Skip_Func)(void *object, UInt32); + +typedef struct _IMatchFinder +{ + Mf_Init_Func Init; + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos; + Mf_GetMatches_Func GetMatches; + Mf_Skip_Func Skip; +} IMatchFinder2; + +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder2 *vTable); + +void MatchFinder_Init_LowHash(CMatchFinder *p); +void MatchFinder_Init_HighHash(CMatchFinder *p); +void MatchFinder_Init_4(CMatchFinder *p); +void MatchFinder_Init(CMatchFinder *p); + +UInt32* Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); +UInt32* Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); + +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); + +void LzFindPrepare(void); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/LzFindMt.h b/src/lzma-sdk/C/LzFindMt.h new file mode 100644 index 0000000..ee9a1b6 --- /dev/null +++ b/src/lzma-sdk/C/LzFindMt.h @@ -0,0 +1,109 @@ +/* LzFindMt.h -- multithreaded Match finder for LZ algorithms +2021-07-12 : Igor Pavlov : Public domain */ + +#ifndef __LZ_FIND_MT_H +#define __LZ_FIND_MT_H + +#include "LzFind.h" +#include "Threads.h" + +EXTERN_C_BEGIN + +typedef struct _CMtSync +{ + UInt32 numProcessedBlocks; + CThread thread; + UInt64 affinity; + + BoolInt wasCreated; + BoolInt needStart; + BoolInt csWasInitialized; + BoolInt csWasEntered; + + BoolInt exit; + BoolInt stopWriting; + + CAutoResetEvent canStart; + CAutoResetEvent wasStopped; + CSemaphore freeSemaphore; + CSemaphore filledSemaphore; + CCriticalSection cs; + // UInt32 numBlocks_Sent; +} CMtSync; + +typedef UInt32 * (*Mf_Mix_Matches)(void *p, UInt32 matchMinPos, UInt32 *distances); + +/* kMtCacheLineDummy must be >= size_of_CPU_cache_line */ +#define kMtCacheLineDummy 128 + +typedef void (*Mf_GetHeads)(const Byte *buffer, UInt32 pos, + UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc); + +typedef struct _CMatchFinderMt +{ + /* LZ */ + const Byte *pointerToCurPos; + UInt32 *btBuf; + const UInt32 *btBufPos; + const UInt32 *btBufPosLimit; + UInt32 lzPos; + UInt32 btNumAvailBytes; + + UInt32 *hash; + UInt32 fixedHashSize; + // UInt32 hash4Mask; + UInt32 historySize; + const UInt32 *crc; + + Mf_Mix_Matches MixMatchesFunc; + UInt32 failure_LZ_BT; // failure in BT transfered to LZ + // UInt32 failure_LZ_LZ; // failure in LZ tables + UInt32 failureBuf[1]; + // UInt32 crc[256]; + + /* LZ + BT */ + CMtSync btSync; + Byte btDummy[kMtCacheLineDummy]; + + /* BT */ + UInt32 *hashBuf; + UInt32 hashBufPos; + UInt32 hashBufPosLimit; + UInt32 hashNumAvail; + UInt32 failure_BT; + + + CLzRef *son; + UInt32 matchMaxLen; + UInt32 numHashBytes; + UInt32 pos; + const Byte *buffer; + UInt32 cyclicBufferPos; + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ + UInt32 cutValue; + + /* BT + Hash */ + CMtSync hashSync; + /* Byte hashDummy[kMtCacheLineDummy]; */ + + /* Hash */ + Mf_GetHeads GetHeadsFunc; + CMatchFinder *MatchFinder; + // CMatchFinder MatchFinder; +} CMatchFinderMt; + +// only for Mt part +void MatchFinderMt_Construct(CMatchFinderMt *p); +void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAllocPtr alloc); + +SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore, + UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAllocPtr alloc); +void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder2 *vTable); + +/* call MatchFinderMt_InitMt() before IMatchFinder::Init() */ +SRes MatchFinderMt_InitMt(CMatchFinderMt *p); +void MatchFinderMt_ReleaseStream(CMatchFinderMt *p); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/LzHash.h b/src/lzma-sdk/C/LzHash.h new file mode 100644 index 0000000..a682f83 --- /dev/null +++ b/src/lzma-sdk/C/LzHash.h @@ -0,0 +1,34 @@ +/* LzHash.h -- HASH functions for LZ algorithms +2019-10-30 : Igor Pavlov : Public domain */ + +#ifndef __LZ_HASH_H +#define __LZ_HASH_H + +/* + (kHash2Size >= (1 << 8)) : Required + (kHash3Size >= (1 << 16)) : Required +*/ + +#define kHash2Size (1 << 10) +#define kHash3Size (1 << 16) +// #define kHash4Size (1 << 20) + +#define kFix3HashSize (kHash2Size) +#define kFix4HashSize (kHash2Size + kHash3Size) +// #define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size) + +/* + We use up to 3 crc values for hash: + crc0 + crc1 << Shift_1 + crc2 << Shift_2 + (Shift_1 = 5) and (Shift_2 = 10) is good tradeoff. + Small values for Shift are not good for collision rate. + Big value for Shift_2 increases the minimum size + of hash table, that will be slow for small files. +*/ + +#define kLzHash_CrcShift_1 5 +#define kLzHash_CrcShift_2 10 + +#endif diff --git a/src/lzma-sdk/C/Lzma2Dec.h b/src/lzma-sdk/C/Lzma2Dec.h new file mode 100644 index 0000000..da50387 --- /dev/null +++ b/src/lzma-sdk/C/Lzma2Dec.h @@ -0,0 +1,120 @@ +/* Lzma2Dec.h -- LZMA2 Decoder +2018-02-19 : Igor Pavlov : Public domain */ + +#ifndef __LZMA2_DEC_H +#define __LZMA2_DEC_H + +#include "LzmaDec.h" + +EXTERN_C_BEGIN + +/* ---------- State Interface ---------- */ + +typedef struct +{ + unsigned state; + Byte control; + Byte needInitLevel; + Byte isExtraMode; + Byte _pad_; + UInt32 packSize; + UInt32 unpackSize; + CLzmaDec decoder; +} CLzma2Dec; + +#define Lzma2Dec_Construct(p) LzmaDec_Construct(&(p)->decoder) +#define Lzma2Dec_FreeProbs(p, alloc) LzmaDec_FreeProbs(&(p)->decoder, alloc) +#define Lzma2Dec_Free(p, alloc) LzmaDec_Free(&(p)->decoder, alloc) + +SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc); +SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc); +void Lzma2Dec_Init(CLzma2Dec *p); + +/* +finishMode: + It has meaning only if the decoding reaches output limit (*destLen or dicLimit). + LZMA_FINISH_ANY - use smallest number of input bytes + LZMA_FINISH_END - read EndOfStream marker after decoding + +Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + LZMA_STATUS_NEEDS_MORE_INPUT + SZ_ERROR_DATA - Data error +*/ + +SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit, + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); + +SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); + + +/* ---------- LZMA2 block and chunk parsing ---------- */ + +/* +Lzma2Dec_Parse() parses compressed data stream up to next independent block or next chunk data. +It can return LZMA_STATUS_* code or LZMA2_PARSE_STATUS_* code: + - LZMA2_PARSE_STATUS_NEW_BLOCK - there is new block, and 1 additional byte (control byte of next block header) was read from input. + - LZMA2_PARSE_STATUS_NEW_CHUNK - there is new chunk, and only lzma2 header of new chunk was read. + CLzma2Dec::unpackSize contains unpack size of that chunk +*/ + +typedef enum +{ +/* + LZMA_STATUS_NOT_SPECIFIED // data error + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED // + LZMA_STATUS_NEEDS_MORE_INPUT + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK // unused +*/ + LZMA2_PARSE_STATUS_NEW_BLOCK = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK + 1, + LZMA2_PARSE_STATUS_NEW_CHUNK +} ELzma2ParseStatus; + +ELzma2ParseStatus Lzma2Dec_Parse(CLzma2Dec *p, + SizeT outSize, // output size + const Byte *src, SizeT *srcLen, + int checkFinishBlock // set (checkFinishBlock = 1), if it must read full input data, if decoder.dicPos reaches blockMax position. + ); + +/* +LZMA2 parser doesn't decode LZMA chunks, so we must read + full input LZMA chunk to decode some part of LZMA chunk. + +Lzma2Dec_GetUnpackExtra() returns the value that shows + max possible number of output bytes that can be output by decoder + at current input positon. +*/ + +#define Lzma2Dec_GetUnpackExtra(p) ((p)->isExtraMode ? (p)->unpackSize : 0); + + +/* ---------- One Call Interface ---------- */ + +/* +finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + LZMA_FINISH_ANY - use smallest number of input bytes + LZMA_FINISH_END - read EndOfStream marker after decoding + +Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + SZ_ERROR_DATA - Data error + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - Unsupported properties + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). +*/ + +SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, + Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAllocPtr alloc); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Lzma2DecMt.h b/src/lzma-sdk/C/Lzma2DecMt.h new file mode 100644 index 0000000..96f89a3 --- /dev/null +++ b/src/lzma-sdk/C/Lzma2DecMt.h @@ -0,0 +1,79 @@ +/* Lzma2DecMt.h -- LZMA2 Decoder Multi-thread +2018-02-17 : Igor Pavlov : Public domain */ + +#ifndef __LZMA2_DEC_MT_H +#define __LZMA2_DEC_MT_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +typedef struct +{ + size_t inBufSize_ST; + size_t outStep_ST; + + #ifndef _7ZIP_ST + unsigned numThreads; + size_t inBufSize_MT; + size_t outBlockMax; + size_t inBlockMax; + #endif +} CLzma2DecMtProps; + +/* init to single-thread mode */ +void Lzma2DecMtProps_Init(CLzma2DecMtProps *p); + + +/* ---------- CLzma2DecMtHandle Interface ---------- */ + +/* Lzma2DecMt_ * functions can return the following exit codes: +SRes: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater in props + SZ_ERROR_WRITE - ISeqOutStream write callback error + // SZ_ERROR_OUTPUT_EOF - output buffer overflow - version with (Byte *) output + SZ_ERROR_PROGRESS - some break from progress callback + SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) +*/ + +typedef void * CLzma2DecMtHandle; + +CLzma2DecMtHandle Lzma2DecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid); +void Lzma2DecMt_Destroy(CLzma2DecMtHandle p); + +SRes Lzma2DecMt_Decode(CLzma2DecMtHandle p, + Byte prop, + const CLzma2DecMtProps *props, + ISeqOutStream *outStream, + const UInt64 *outDataSize, // NULL means undefined + int finishMode, // 0 - partial unpacking is allowed, 1 - if lzma2 stream must be finished + // Byte *outBuf, size_t *outBufSize, + ISeqInStream *inStream, + // const Byte *inData, size_t inDataSize, + + // out variables: + UInt64 *inProcessed, + int *isMT, /* out: (*isMT == 0), if single thread decoding was used */ + + // UInt64 *outProcessed, + ICompressProgress *progress); + + +/* ---------- Read from CLzma2DecMtHandle Interface ---------- */ + +SRes Lzma2DecMt_Init(CLzma2DecMtHandle pp, + Byte prop, + const CLzma2DecMtProps *props, + const UInt64 *outDataSize, int finishMode, + ISeqInStream *inStream); + +SRes Lzma2DecMt_Read(CLzma2DecMtHandle pp, + Byte *data, size_t *outSize, + UInt64 *inStreamProcessed); + + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Lzma2Enc.h b/src/lzma-sdk/C/Lzma2Enc.h new file mode 100644 index 0000000..65f2dd1 --- /dev/null +++ b/src/lzma-sdk/C/Lzma2Enc.h @@ -0,0 +1,55 @@ +/* Lzma2Enc.h -- LZMA2 Encoder +2017-07-27 : Igor Pavlov : Public domain */ + +#ifndef __LZMA2_ENC_H +#define __LZMA2_ENC_H + +#include "LzmaEnc.h" + +EXTERN_C_BEGIN + +#define LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO 0 +#define LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID ((UInt64)(Int64)-1) + +typedef struct +{ + CLzmaEncProps lzmaProps; + UInt64 blockSize; + int numBlockThreads_Reduced; + int numBlockThreads_Max; + int numTotalThreads; +} CLzma2EncProps; + +void Lzma2EncProps_Init(CLzma2EncProps *p); +void Lzma2EncProps_Normalize(CLzma2EncProps *p); + +/* ---------- CLzmaEnc2Handle Interface ---------- */ + +/* Lzma2Enc_* functions can return the following exit codes: +SRes: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater in props + SZ_ERROR_WRITE - ISeqOutStream write callback error + SZ_ERROR_OUTPUT_EOF - output buffer overflow - version with (Byte *) output + SZ_ERROR_PROGRESS - some break from progress callback + SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) +*/ + +typedef void * CLzma2EncHandle; + +CLzma2EncHandle Lzma2Enc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig); +void Lzma2Enc_Destroy(CLzma2EncHandle p); +SRes Lzma2Enc_SetProps(CLzma2EncHandle p, const CLzma2EncProps *props); +void Lzma2Enc_SetDataSize(CLzma2EncHandle p, UInt64 expectedDataSiize); +Byte Lzma2Enc_WriteProperties(CLzma2EncHandle p); +SRes Lzma2Enc_Encode2(CLzma2EncHandle p, + ISeqOutStream *outStream, + Byte *outBuf, size_t *outBufSize, + ISeqInStream *inStream, + const Byte *inData, size_t inDataSize, + ICompressProgress *progress); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Lzma86.h b/src/lzma-sdk/C/Lzma86.h new file mode 100644 index 0000000..83057e5 --- /dev/null +++ b/src/lzma-sdk/C/Lzma86.h @@ -0,0 +1,111 @@ +/* Lzma86.h -- LZMA + x86 (BCJ) Filter +2013-01-18 : Igor Pavlov : Public domain */ + +#ifndef __LZMA86_H +#define __LZMA86_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define LZMA86_SIZE_OFFSET (1 + 5) +#define LZMA86_HEADER_SIZE (LZMA86_SIZE_OFFSET + 8) + +/* +It's an example for LZMA + x86 Filter use. +You can use .lzma86 extension, if you write that stream to file. +.lzma86 header adds one additional byte to standard .lzma header. +.lzma86 header (14 bytes): + Offset Size Description + 0 1 = 0 - no filter, pure LZMA + = 1 - x86 filter + LZMA + 1 1 lc, lp and pb in encoded form + 2 4 dictSize (little endian) + 6 8 uncompressed size (little endian) + + +Lzma86_Encode +------------- +level - compression level: 0 <= level <= 9, the default value for "level" is 5. + +dictSize - The dictionary size in bytes. The maximum value is + 128 MB = (1 << 27) bytes for 32-bit version + 1 GB = (1 << 30) bytes for 64-bit version + The default value is 16 MB = (1 << 24) bytes, for level = 5. + It's recommended to use the dictionary that is larger than 4 KB and + that can be calculated as (1 << N) or (3 << N) sizes. + For better compression ratio dictSize must be >= inSize. + +filterMode: + SZ_FILTER_NO - no Filter + SZ_FILTER_YES - x86 Filter + SZ_FILTER_AUTO - it tries both alternatives to select best. + Encoder will use 2 or 3 passes: + 2 passes when FILTER_NO provides better compression. + 3 passes when FILTER_YES provides better compression. + +Lzma86Encode allocates Data with MyAlloc functions. +RAM Requirements for compressing: + RamSize = dictionarySize * 11.5 + 6MB + FilterBlockSize + filterMode FilterBlockSize + SZ_FILTER_NO 0 + SZ_FILTER_YES inSize + SZ_FILTER_AUTO inSize + + +Return code: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater + SZ_ERROR_OUTPUT_EOF - output buffer overflow + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) +*/ + +enum ESzFilterMode +{ + SZ_FILTER_NO, + SZ_FILTER_YES, + SZ_FILTER_AUTO +}; + +SRes Lzma86_Encode(Byte *dest, size_t *destLen, const Byte *src, size_t srcLen, + int level, UInt32 dictSize, int filterMode); + + +/* +Lzma86_GetUnpackSize: + In: + src - input data + srcLen - input data size + Out: + unpackSize - size of uncompressed stream + Return code: + SZ_OK - OK + SZ_ERROR_INPUT_EOF - Error in headers +*/ + +SRes Lzma86_GetUnpackSize(const Byte *src, SizeT srcLen, UInt64 *unpackSize); + +/* +Lzma86_Decode: + In: + dest - output data + destLen - output data size + src - input data + srcLen - input data size + Out: + destLen - processed output size + srcLen - processed input size + Return code: + SZ_OK - OK + SZ_ERROR_DATA - Data error + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - unsupported file + SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer +*/ + +SRes Lzma86_Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/LzmaDec.h b/src/lzma-sdk/C/LzmaDec.h new file mode 100644 index 0000000..6194b7d --- /dev/null +++ b/src/lzma-sdk/C/LzmaDec.h @@ -0,0 +1,236 @@ +/* LzmaDec.h -- LZMA Decoder +2020-03-19 : Igor Pavlov : Public domain */ + +#ifndef __LZMA_DEC_H +#define __LZMA_DEC_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +/* #define _LZMA_PROB32 */ +/* _LZMA_PROB32 can increase the speed on some CPUs, + but memory usage for CLzmaDec::probs will be doubled in that case */ + +typedef +#ifdef _LZMA_PROB32 + UInt32 +#else + UInt16 +#endif + CLzmaProb; + + +/* ---------- LZMA Properties ---------- */ + +#define LZMA_PROPS_SIZE 5 + +typedef struct _CLzmaProps +{ + Byte lc; + Byte lp; + Byte pb; + Byte _pad_; + UInt32 dicSize; +} CLzmaProps; + +/* LzmaProps_Decode - decodes properties +Returns: + SZ_OK + SZ_ERROR_UNSUPPORTED - Unsupported properties +*/ + +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size); + + +/* ---------- LZMA Decoder state ---------- */ + +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ + +#define LZMA_REQUIRED_INPUT_MAX 20 + +typedef struct +{ + /* Don't change this structure. ASM code can use it. */ + CLzmaProps prop; + CLzmaProb *probs; + CLzmaProb *probs_1664; + Byte *dic; + SizeT dicBufSize; + SizeT dicPos; + const Byte *buf; + UInt32 range; + UInt32 code; + UInt32 processedPos; + UInt32 checkDicSize; + UInt32 reps[4]; + UInt32 state; + UInt32 remainLen; + + UInt32 numProbs; + unsigned tempBufSize; + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; +} CLzmaDec; + +#define LzmaDec_Construct(p) { (p)->dic = NULL; (p)->probs = NULL; } + +void LzmaDec_Init(CLzmaDec *p); + +/* There are two types of LZMA streams: + - Stream with end mark. That end mark adds about 6 bytes to compressed size. + - Stream without end mark. You must know exact uncompressed size to decompress such stream. */ + +typedef enum +{ + LZMA_FINISH_ANY, /* finish at any point */ + LZMA_FINISH_END /* block must be finished at the end */ +} ELzmaFinishMode; + +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! + + You must use LZMA_FINISH_END, when you know that current output buffer + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. + + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK, + and output value of destLen will be less than output buffer size limit. + You can check status result also. + + You can use multiple checks to test data integrity after full decompression: + 1) Check Result and "status" variable. + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. + You must use correct finish mode in that case. */ + +typedef enum +{ + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */ + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */ +} ELzmaStatus; + +/* ELzmaStatus is used only as output value for function call */ + + +/* ---------- Interfaces ---------- */ + +/* There are 3 levels of interfaces: + 1) Dictionary Interface + 2) Buffer Interface + 3) One Call Interface + You can select any of these interfaces, but don't mix functions from different + groups for same object. */ + + +/* There are two variants to allocate state for Dictionary Interface: + 1) LzmaDec_Allocate / LzmaDec_Free + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs + You can use variant 2, if you set dictionary buffer manually. + For Buffer Interface you must always use variant 1. + +LzmaDec_Allocate* can return: + SZ_OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - Unsupported properties +*/ + +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc); +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc); + +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc); +void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc); + +/* ---------- Dictionary Interface ---------- */ + +/* You can use it, if you want to eliminate the overhead for data copying from + dictionary to some other external buffer. + You must work with CLzmaDec variables directly in this interface. + + STEPS: + LzmaDec_Construct() + LzmaDec_Allocate() + for (each new stream) + { + LzmaDec_Init() + while (it needs more decompression) + { + LzmaDec_DecodeToDic() + use data from CLzmaDec::dic and update CLzmaDec::dicPos + } + } + LzmaDec_Free() +*/ + +/* LzmaDec_DecodeToDic + + The decoding to internal dictionary buffer (CLzmaDec::dic). + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!! + +finishMode: + It has meaning only if the decoding reaches output limit (dicLimit). + LZMA_FINISH_ANY - Decode just dicLimit bytes. + LZMA_FINISH_END - Stream must be finished after dicLimit. + +Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + LZMA_STATUS_NEEDS_MORE_INPUT + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK + SZ_ERROR_DATA - Data error + SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure +*/ + +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); + + +/* ---------- Buffer Interface ---------- */ + +/* It's zlib-like interface. + See LzmaDec_DecodeToDic description for information about STEPS and return results, + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need + to work with CLzmaDec variables manually. + +finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + LZMA_FINISH_ANY - Decode just destLen bytes. + LZMA_FINISH_END - Stream must be finished after (*destLen). +*/ + +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); + + +/* ---------- One Call Interface ---------- */ + +/* LzmaDecode + +finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + LZMA_FINISH_ANY - Decode just destLen bytes. + LZMA_FINISH_END - Stream must be finished after (*destLen). + +Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK + SZ_ERROR_DATA - Data error + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - Unsupported properties + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). + SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure +*/ + +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, + ELzmaStatus *status, ISzAllocPtr alloc); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/LzmaEnc.h b/src/lzma-sdk/C/LzmaEnc.h new file mode 100644 index 0000000..26757ba --- /dev/null +++ b/src/lzma-sdk/C/LzmaEnc.h @@ -0,0 +1,78 @@ +/* LzmaEnc.h -- LZMA Encoder +2019-10-30 : Igor Pavlov : Public domain */ + +#ifndef __LZMA_ENC_H +#define __LZMA_ENC_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define LZMA_PROPS_SIZE 5 + +typedef struct _CLzmaEncProps +{ + int level; /* 0 <= level <= 9 */ + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version + (1 << 12) <= dictSize <= (3 << 29) for 64-bit version + default = (1 << 24) */ + int lc; /* 0 <= lc <= 8, default = 3 */ + int lp; /* 0 <= lp <= 4, default = 0 */ + int pb; /* 0 <= pb <= 4, default = 2 */ + int algo; /* 0 - fast, 1 - normal, default = 1 */ + int fb; /* 5 <= fb <= 273, default = 32 */ + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */ + int numHashBytes; /* 2, 3 or 4, default = 4 */ + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */ + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */ + int numThreads; /* 1 or 2, default = 2 */ + + UInt64 reduceSize; /* estimated size of data that will be compressed. default = (UInt64)(Int64)-1. + Encoder uses this value to reduce dictionary size */ + + UInt64 affinity; +} CLzmaEncProps; + +void LzmaEncProps_Init(CLzmaEncProps *p); +void LzmaEncProps_Normalize(CLzmaEncProps *p); +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2); + + +/* ---------- CLzmaEncHandle Interface ---------- */ + +/* LzmaEnc* functions can return the following exit codes: +SRes: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater in props + SZ_ERROR_WRITE - ISeqOutStream write callback error + SZ_ERROR_OUTPUT_EOF - output buffer overflow - version with (Byte *) output + SZ_ERROR_PROGRESS - some break from progress callback + SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) +*/ + +typedef void * CLzmaEncHandle; + +CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc); +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig); + +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props); +void LzmaEnc_SetDataSize(CLzmaEncHandle p, UInt64 expectedDataSiize); +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size); +unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle p); + +SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, + ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, + int writeEndMark, ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); + + +/* ---------- One Call Interface ---------- */ + +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, + ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/LzmaLib.h b/src/lzma-sdk/C/LzmaLib.h new file mode 100644 index 0000000..4103e22 --- /dev/null +++ b/src/lzma-sdk/C/LzmaLib.h @@ -0,0 +1,138 @@ +/* LzmaLib.h -- LZMA library interface +2021-04-03 : Igor Pavlov : Public domain */ + +#ifndef __LZMA_LIB_H +#define __LZMA_LIB_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define MY_STDAPI int MY_STD_CALL + +#define LZMA_PROPS_SIZE 5 + +/* +RAM requirements for LZMA: + for compression: (dictSize * 11.5 + 6 MB) + state_size + for decompression: dictSize + state_size + state_size = (4 + (1.5 << (lc + lp))) KB + by default (lc=3, lp=0), state_size = 16 KB. + +LZMA properties (5 bytes) format + Offset Size Description + 0 1 lc, lp and pb in encoded form. + 1 4 dictSize (little endian). +*/ + +/* +LzmaCompress +------------ + +outPropsSize - + In: the pointer to the size of outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. + Out: the pointer to the size of written properties in outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. + + LZMA Encoder will use defult values for any parameter, if it is + -1 for any from: level, loc, lp, pb, fb, numThreads + 0 for dictSize + +level - compression level: 0 <= level <= 9; + + level dictSize algo fb + 0: 64 KB 0 32 + 1: 256 KB 0 32 + 2: 1 MB 0 32 + 3: 4 MB 0 32 + 4: 16 MB 0 32 + 5: 16 MB 1 32 + 6: 32 MB 1 32 + 7: 32 MB 1 64 + 8: 64 MB 1 64 + 9: 64 MB 1 64 + + The default value for "level" is 5. + + algo = 0 means fast method + algo = 1 means normal method + +dictSize - The dictionary size in bytes. The maximum value is + 128 MB = (1 << 27) bytes for 32-bit version + 1 GB = (1 << 30) bytes for 64-bit version + The default value is 16 MB = (1 << 24) bytes. + It's recommended to use the dictionary that is larger than 4 KB and + that can be calculated as (1 << N) or (3 << N) sizes. + +lc - The number of literal context bits (high bits of previous literal). + It can be in the range from 0 to 8. The default value is 3. + Sometimes lc=4 gives the gain for big files. + +lp - The number of literal pos bits (low bits of current position for literals). + It can be in the range from 0 to 4. The default value is 0. + The lp switch is intended for periodical data when the period is equal to 2^lp. + For example, for 32-bit (4 bytes) periodical data you can use lp=2. Often it's + better to set lc=0, if you change lp switch. + +pb - The number of pos bits (low bits of current position). + It can be in the range from 0 to 4. The default value is 2. + The pb switch is intended for periodical data when the period is equal 2^pb. + +fb - Word size (the number of fast bytes). + It can be in the range from 5 to 273. The default value is 32. + Usually, a big number gives a little bit better compression ratio and + slower compression process. + +numThreads - The number of thereads. 1 or 2. The default value is 2. + Fast mode (algo = 0) can use only 1 thread. + +In: + dest - output data buffer + destLen - output data buffer size + src - input data + srcLen - input data size +Out: + destLen - processed output size +Returns: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater + SZ_ERROR_OUTPUT_EOF - output buffer overflow + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) +*/ + +MY_STDAPI LzmaCompress(unsigned char *dest, size_t *destLen, const unsigned char *src, size_t srcLen, + unsigned char *outProps, size_t *outPropsSize, /* *outPropsSize must be = 5 */ + int level, /* 0 <= level <= 9, default = 5 */ + unsigned dictSize, /* default = (1 << 24) */ + int lc, /* 0 <= lc <= 8, default = 3 */ + int lp, /* 0 <= lp <= 4, default = 0 */ + int pb, /* 0 <= pb <= 4, default = 2 */ + int fb, /* 5 <= fb <= 273, default = 32 */ + int numThreads /* 1 or 2, default = 2 */ + ); + +/* +LzmaUncompress +-------------- +In: + dest - output data buffer + destLen - output data buffer size + src - input data + srcLen - input data size +Out: + destLen - processed output size + srcLen - processed input size +Returns: + SZ_OK - OK + SZ_ERROR_DATA - Data error + SZ_ERROR_MEM - Memory allocation arror + SZ_ERROR_UNSUPPORTED - Unsupported properties + SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src) +*/ + +MY_STDAPI LzmaUncompress(unsigned char *dest, size_t *destLen, const unsigned char *src, SizeT *srcLen, + const unsigned char *props, size_t propsSize); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/MtCoder.h b/src/lzma-sdk/C/MtCoder.h new file mode 100644 index 0000000..603329d --- /dev/null +++ b/src/lzma-sdk/C/MtCoder.h @@ -0,0 +1,141 @@ +/* MtCoder.h -- Multi-thread Coder +2018-07-04 : Igor Pavlov : Public domain */ + +#ifndef __MT_CODER_H +#define __MT_CODER_H + +#include "MtDec.h" + +EXTERN_C_BEGIN + +/* + if ( defined MTCODER__USE_WRITE_THREAD) : main thread writes all data blocks to output stream + if (not defined MTCODER__USE_WRITE_THREAD) : any coder thread can write data blocks to output stream +*/ +/* #define MTCODER__USE_WRITE_THREAD */ + +#ifndef _7ZIP_ST + #define MTCODER__GET_NUM_BLOCKS_FROM_THREADS(numThreads) ((numThreads) + (numThreads) / 8 + 1) + #define MTCODER__THREADS_MAX 64 + #define MTCODER__BLOCKS_MAX (MTCODER__GET_NUM_BLOCKS_FROM_THREADS(MTCODER__THREADS_MAX) + 3) +#else + #define MTCODER__THREADS_MAX 1 + #define MTCODER__BLOCKS_MAX 1 +#endif + + +#ifndef _7ZIP_ST + + +typedef struct +{ + ICompressProgress vt; + CMtProgress *mtProgress; + UInt64 inSize; + UInt64 outSize; +} CMtProgressThunk; + +void MtProgressThunk_CreateVTable(CMtProgressThunk *p); + +#define MtProgressThunk_Init(p) { (p)->inSize = 0; (p)->outSize = 0; } + + +struct _CMtCoder; + + +typedef struct +{ + struct _CMtCoder *mtCoder; + unsigned index; + int stop; + Byte *inBuf; + + CAutoResetEvent startEvent; + CThread thread; +} CMtCoderThread; + + +typedef struct +{ + SRes (*Code)(void *p, unsigned coderIndex, unsigned outBufIndex, + const Byte *src, size_t srcSize, int finished); + SRes (*Write)(void *p, unsigned outBufIndex); +} IMtCoderCallback2; + + +typedef struct +{ + SRes res; + unsigned bufIndex; + BoolInt finished; +} CMtCoderBlock; + + +typedef struct _CMtCoder +{ + /* input variables */ + + size_t blockSize; /* size of input block */ + unsigned numThreadsMax; + UInt64 expectedDataSize; + + ISeqInStream *inStream; + const Byte *inData; + size_t inDataSize; + + ICompressProgress *progress; + ISzAllocPtr allocBig; + + IMtCoderCallback2 *mtCallback; + void *mtCallbackObject; + + + /* internal variables */ + + size_t allocatedBufsSize; + + CAutoResetEvent readEvent; + CSemaphore blocksSemaphore; + + BoolInt stopReading; + SRes readRes; + + #ifdef MTCODER__USE_WRITE_THREAD + CAutoResetEvent writeEvents[MTCODER__BLOCKS_MAX]; + #else + CAutoResetEvent finishedEvent; + SRes writeRes; + unsigned writeIndex; + Byte ReadyBlocks[MTCODER__BLOCKS_MAX]; + LONG numFinishedThreads; + #endif + + unsigned numStartedThreadsLimit; + unsigned numStartedThreads; + + unsigned numBlocksMax; + unsigned blockIndex; + UInt64 readProcessed; + + CCriticalSection cs; + + unsigned freeBlockHead; + unsigned freeBlockList[MTCODER__BLOCKS_MAX]; + + CMtProgress mtProgress; + CMtCoderBlock blocks[MTCODER__BLOCKS_MAX]; + CMtCoderThread threads[MTCODER__THREADS_MAX]; +} CMtCoder; + + +void MtCoder_Construct(CMtCoder *p); +void MtCoder_Destruct(CMtCoder *p); +SRes MtCoder_Code(CMtCoder *p); + + +#endif + + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/MtDec.h b/src/lzma-sdk/C/MtDec.h new file mode 100644 index 0000000..7a30b6a --- /dev/null +++ b/src/lzma-sdk/C/MtDec.h @@ -0,0 +1,202 @@ +/* MtDec.h -- Multi-thread Decoder +2020-03-05 : Igor Pavlov : Public domain */ + +#ifndef __MT_DEC_H +#define __MT_DEC_H + +#include "7zTypes.h" + +#ifndef _7ZIP_ST +#include "Threads.h" +#endif + +EXTERN_C_BEGIN + +#ifndef _7ZIP_ST + +#ifndef _7ZIP_ST + #define MTDEC__THREADS_MAX 32 +#else + #define MTDEC__THREADS_MAX 1 +#endif + + +typedef struct +{ + ICompressProgress *progress; + SRes res; + UInt64 totalInSize; + UInt64 totalOutSize; + CCriticalSection cs; +} CMtProgress; + +void MtProgress_Init(CMtProgress *p, ICompressProgress *progress); +SRes MtProgress_Progress_ST(CMtProgress *p); +SRes MtProgress_ProgressAdd(CMtProgress *p, UInt64 inSize, UInt64 outSize); +SRes MtProgress_GetError(CMtProgress *p); +void MtProgress_SetError(CMtProgress *p, SRes res); + +struct _CMtDec; + +typedef struct +{ + struct _CMtDec *mtDec; + unsigned index; + void *inBuf; + + size_t inDataSize_Start; // size of input data in start block + UInt64 inDataSize; // total size of input data in all blocks + + CThread thread; + CAutoResetEvent canRead; + CAutoResetEvent canWrite; + void *allocaPtr; +} CMtDecThread; + +void MtDecThread_FreeInBufs(CMtDecThread *t); + + +typedef enum +{ + MTDEC_PARSE_CONTINUE, // continue this block with more input data + MTDEC_PARSE_OVERFLOW, // MT buffers overflow, need switch to single-thread + MTDEC_PARSE_NEW, // new block + MTDEC_PARSE_END // end of block threading. But we still can return to threading after Write(&needContinue) +} EMtDecParseState; + +typedef struct +{ + // in + int startCall; + const Byte *src; + size_t srcSize; + // in : (srcSize == 0) is allowed + // out : it's allowed to return less that actually was used ? + int srcFinished; + + // out + EMtDecParseState state; + BoolInt canCreateNewThread; + UInt64 outPos; // check it (size_t) +} CMtDecCallbackInfo; + + +typedef struct +{ + void (*Parse)(void *p, unsigned coderIndex, CMtDecCallbackInfo *ci); + + // PreCode() and Code(): + // (SRes_return_result != SZ_OK) means stop decoding, no need another blocks + SRes (*PreCode)(void *p, unsigned coderIndex); + SRes (*Code)(void *p, unsigned coderIndex, + const Byte *src, size_t srcSize, int srcFinished, + UInt64 *inCodePos, UInt64 *outCodePos, int *stop); + // stop - means stop another Code calls + + + /* Write() must be called, if Parse() was called + set (needWrite) if + { + && (was not interrupted by progress) + && (was not interrupted in previous block) + } + + out: + if (*needContinue), decoder still need to continue decoding with new iteration, + even after MTDEC_PARSE_END + if (*canRecode), we didn't flush current block data, so we still can decode current block later. + */ + SRes (*Write)(void *p, unsigned coderIndex, + BoolInt needWriteToStream, + const Byte *src, size_t srcSize, BoolInt isCross, + // int srcFinished, + BoolInt *needContinue, + BoolInt *canRecode); + +} IMtDecCallback2; + + + +typedef struct _CMtDec +{ + /* input variables */ + + size_t inBufSize; /* size of input block */ + unsigned numThreadsMax; + // size_t inBlockMax; + unsigned numThreadsMax_2; + + ISeqInStream *inStream; + // const Byte *inData; + // size_t inDataSize; + + ICompressProgress *progress; + ISzAllocPtr alloc; + + IMtDecCallback2 *mtCallback; + void *mtCallbackObject; + + + /* internal variables */ + + size_t allocatedBufsSize; + + BoolInt exitThread; + WRes exitThreadWRes; + + UInt64 blockIndex; + BoolInt isAllocError; + BoolInt overflow; + SRes threadingErrorSRes; + + BoolInt needContinue; + + // CAutoResetEvent finishedEvent; + + SRes readRes; + SRes codeRes; + + BoolInt wasInterrupted; + + unsigned numStartedThreads_Limit; + unsigned numStartedThreads; + + Byte *crossBlock; + size_t crossStart; + size_t crossEnd; + UInt64 readProcessed; + BoolInt readWasFinished; + UInt64 inProcessed; + + unsigned filledThreadStart; + unsigned numFilledThreads; + + #ifndef _7ZIP_ST + BoolInt needInterrupt; + UInt64 interruptIndex; + CMtProgress mtProgress; + CMtDecThread threads[MTDEC__THREADS_MAX]; + #endif +} CMtDec; + + +void MtDec_Construct(CMtDec *p); +void MtDec_Destruct(CMtDec *p); + +/* +MtDec_Code() returns: + SZ_OK - in most cases + MY_SRes_HRESULT_FROM_WRes(WRes_error) - in case of unexpected error in threading function +*/ + +SRes MtDec_Code(CMtDec *p); +Byte *MtDec_GetCrossBuff(CMtDec *p); + +int MtDec_PrepareRead(CMtDec *p); +const Byte *MtDec_Read(CMtDec *p, size_t *inLim); + +#endif + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Ppmd.h b/src/lzma-sdk/C/Ppmd.h new file mode 100644 index 0000000..ee93ece --- /dev/null +++ b/src/lzma-sdk/C/Ppmd.h @@ -0,0 +1,167 @@ +/* Ppmd.h -- PPMD codec common code +2021-04-13 : Igor Pavlov : Public domain +This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */ + +#ifndef __PPMD_H +#define __PPMD_H + +#include "CpuArch.h" + +EXTERN_C_BEGIN + +#if defined(MY_CPU_SIZEOF_POINTER) && (MY_CPU_SIZEOF_POINTER == 4) +/* + PPMD code always uses 32-bit internal fields in PPMD structures to store internal references in main block. + if (PPMD_32BIT is defined), the PPMD code stores internal pointers to 32-bit reference fields. + if (PPMD_32BIT is NOT defined), the PPMD code stores internal UInt32 offsets to reference fields. + if (pointer size is 64-bit), then (PPMD_32BIT) mode is not allowed, + if (pointer size is 32-bit), then (PPMD_32BIT) mode is optional, + and it's allowed to disable PPMD_32BIT mode even if pointer is 32-bit. + PPMD code works slightly faster in (PPMD_32BIT) mode. +*/ + #define PPMD_32BIT +#endif + +#define PPMD_INT_BITS 7 +#define PPMD_PERIOD_BITS 7 +#define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS)) + +#define PPMD_GET_MEAN_SPEC(summ, shift, round) (((summ) + (1 << ((shift) - (round)))) >> (shift)) +#define PPMD_GET_MEAN(summ) PPMD_GET_MEAN_SPEC((summ), PPMD_PERIOD_BITS, 2) +#define PPMD_UPDATE_PROB_0(prob) ((prob) + (1 << PPMD_INT_BITS) - PPMD_GET_MEAN(prob)) +#define PPMD_UPDATE_PROB_1(prob) ((prob) - PPMD_GET_MEAN(prob)) + +#define PPMD_N1 4 +#define PPMD_N2 4 +#define PPMD_N3 4 +#define PPMD_N4 ((128 + 3 - 1 * PPMD_N1 - 2 * PPMD_N2 - 3 * PPMD_N3) / 4) +#define PPMD_NUM_INDEXES (PPMD_N1 + PPMD_N2 + PPMD_N3 + PPMD_N4) + +MY_CPU_pragma_pack_push_1 +/* Most compilers works OK here even without #pragma pack(push, 1), but some GCC compilers need it. */ + +/* SEE-contexts for PPM-contexts with masked symbols */ +typedef struct +{ + UInt16 Summ; /* Freq */ + Byte Shift; /* Speed of Freq change; low Shift is for fast change */ + Byte Count; /* Count to next change of Shift */ +} CPpmd_See; + +#define Ppmd_See_Update(p) if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \ + { (p)->Summ = (UInt16)((p)->Summ << 1); (p)->Count = (Byte)(3 << (p)->Shift++); } + + +typedef struct +{ + Byte Symbol; + Byte Freq; + UInt16 Successor_0; + UInt16 Successor_1; +} CPpmd_State; + +typedef struct CPpmd_State2_ +{ + Byte Symbol; + Byte Freq; +} CPpmd_State2; + +typedef struct CPpmd_State4_ +{ + UInt16 Successor_0; + UInt16 Successor_1; +} CPpmd_State4; + +MY_CPU_pragma_pop + +/* + PPMD code can write full CPpmd_State structure data to CPpmd*_Context + at (byte offset = 2) instead of some fields of original CPpmd*_Context structure. + + If we use pointers to different types, but that point to shared + memory space, we can have aliasing problem (strict aliasing). + + XLC compiler in -O2 mode can change the order of memory write instructions + in relation to read instructions, if we have use pointers to different types. + + To solve that aliasing problem we use combined CPpmd*_Context structure + with unions that contain the fields from both structures: + the original CPpmd*_Context and CPpmd_State. + So we can access the fields from both structures via one pointer, + and the compiler doesn't change the order of write instructions + in relation to read instructions. + + If we don't use memory write instructions to shared memory in + some local code, and we use only reading instructions (read only), + then probably it's safe to use pointers to different types for reading. +*/ + + + +#ifdef PPMD_32BIT + + #define Ppmd_Ref_Type(type) type * + #define Ppmd_GetRef(p, ptr) (ptr) + #define Ppmd_GetPtr(p, ptr) (ptr) + #define Ppmd_GetPtr_Type(p, ptr, note_type) (ptr) + +#else + + #define Ppmd_Ref_Type(type) UInt32 + #define Ppmd_GetRef(p, ptr) ((UInt32)((Byte *)(ptr) - (p)->Base)) + #define Ppmd_GetPtr(p, offs) ((void *)((p)->Base + (offs))) + #define Ppmd_GetPtr_Type(p, offs, type) ((type *)Ppmd_GetPtr(p, offs)) + +#endif // PPMD_32BIT + + +typedef Ppmd_Ref_Type(CPpmd_State) CPpmd_State_Ref; +typedef Ppmd_Ref_Type(void) CPpmd_Void_Ref; +typedef Ppmd_Ref_Type(Byte) CPpmd_Byte_Ref; + + +/* +#ifdef MY_CPU_LE_UNALIGN +// the unaligned 32-bit access latency can be too large, if the data is not in L1 cache. +#define Ppmd_GET_SUCCESSOR(p) ((CPpmd_Void_Ref)*(const UInt32 *)(const void *)&(p)->Successor_0) +#define Ppmd_SET_SUCCESSOR(p, v) *(UInt32 *)(void *)(void *)&(p)->Successor_0 = (UInt32)(v) + +#else +*/ + +/* + We can write 16-bit halves to 32-bit (Successor) field in any selected order. + But the native order is more consistent way. + So we use the native order, if LE/BE order can be detected here at compile time. +*/ + +#ifdef MY_CPU_BE + + #define Ppmd_GET_SUCCESSOR(p) \ + ( (CPpmd_Void_Ref) (((UInt32)(p)->Successor_0 << 16) | (p)->Successor_1) ) + + #define Ppmd_SET_SUCCESSOR(p, v) { \ + (p)->Successor_0 = (UInt16)(((UInt32)(v) >> 16) /* & 0xFFFF */); \ + (p)->Successor_1 = (UInt16)((UInt32)(v) /* & 0xFFFF */); } + +#else + + #define Ppmd_GET_SUCCESSOR(p) \ + ( (CPpmd_Void_Ref) ((p)->Successor_0 | ((UInt32)(p)->Successor_1 << 16)) ) + + #define Ppmd_SET_SUCCESSOR(p, v) { \ + (p)->Successor_0 = (UInt16)((UInt32)(v) /* & 0xFFFF */); \ + (p)->Successor_1 = (UInt16)(((UInt32)(v) >> 16) /* & 0xFFFF */); } + +#endif + +// #endif + + +#define PPMD_SetAllBitsIn256Bytes(p) \ + { size_t z; for (z = 0; z < 256 / sizeof(p[0]); z += 8) { \ + p[z+7] = p[z+6] = p[z+5] = p[z+4] = p[z+3] = p[z+2] = p[z+1] = p[z+0] = ~(size_t)0; }} + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Ppmd7.h b/src/lzma-sdk/C/Ppmd7.h new file mode 100644 index 0000000..297e35f --- /dev/null +++ b/src/lzma-sdk/C/Ppmd7.h @@ -0,0 +1,181 @@ +/* Ppmd7.h -- Ppmd7 (PPMdH) compression codec +2021-04-13 : Igor Pavlov : Public domain +This code is based on: + PPMd var.H (2001): Dmitry Shkarin : Public domain */ + + +#ifndef __PPMD7_H +#define __PPMD7_H + +#include "Ppmd.h" + +EXTERN_C_BEGIN + +#define PPMD7_MIN_ORDER 2 +#define PPMD7_MAX_ORDER 64 + +#define PPMD7_MIN_MEM_SIZE (1 << 11) +#define PPMD7_MAX_MEM_SIZE (0xFFFFFFFF - 12 * 3) + +struct CPpmd7_Context_; + +typedef Ppmd_Ref_Type(struct CPpmd7_Context_) CPpmd7_Context_Ref; + +// MY_CPU_pragma_pack_push_1 + +typedef struct CPpmd7_Context_ +{ + UInt16 NumStats; + + + union + { + UInt16 SummFreq; + CPpmd_State2 State2; + } Union2; + + union + { + CPpmd_State_Ref Stats; + CPpmd_State4 State4; + } Union4; + + CPpmd7_Context_Ref Suffix; +} CPpmd7_Context; + +// MY_CPU_pragma_pop + +#define Ppmd7Context_OneState(p) ((CPpmd_State *)&(p)->Union2) + + + + +typedef struct +{ + UInt32 Range; + UInt32 Code; + UInt32 Low; + IByteIn *Stream; +} CPpmd7_RangeDec; + + +typedef struct +{ + UInt32 Range; + Byte Cache; + // Byte _dummy_[3]; + UInt64 Low; + UInt64 CacheSize; + IByteOut *Stream; +} CPpmd7z_RangeEnc; + + +typedef struct +{ + CPpmd7_Context *MinContext, *MaxContext; + CPpmd_State *FoundState; + unsigned OrderFall, InitEsc, PrevSuccess, MaxOrder, HiBitsFlag; + Int32 RunLength, InitRL; /* must be 32-bit at least */ + + UInt32 Size; + UInt32 GlueCount; + UInt32 AlignOffset; + Byte *Base, *LoUnit, *HiUnit, *Text, *UnitsStart; + + + + + union + { + CPpmd7_RangeDec dec; + CPpmd7z_RangeEnc enc; + } rc; + + Byte Indx2Units[PPMD_NUM_INDEXES + 2]; // +2 for alignment + Byte Units2Indx[128]; + CPpmd_Void_Ref FreeList[PPMD_NUM_INDEXES]; + + Byte NS2BSIndx[256], NS2Indx[256]; + Byte ExpEscape[16]; + CPpmd_See DummySee, See[25][16]; + UInt16 BinSumm[128][64]; + // int LastSymbol; +} CPpmd7; + + +void Ppmd7_Construct(CPpmd7 *p); +BoolInt Ppmd7_Alloc(CPpmd7 *p, UInt32 size, ISzAllocPtr alloc); +void Ppmd7_Free(CPpmd7 *p, ISzAllocPtr alloc); +void Ppmd7_Init(CPpmd7 *p, unsigned maxOrder); +#define Ppmd7_WasAllocated(p) ((p)->Base != NULL) + + +/* ---------- Internal Functions ---------- */ + +#define Ppmd7_GetPtr(p, ptr) Ppmd_GetPtr(p, ptr) +#define Ppmd7_GetContext(p, ptr) Ppmd_GetPtr_Type(p, ptr, CPpmd7_Context) +#define Ppmd7_GetStats(p, ctx) Ppmd_GetPtr_Type(p, (ctx)->Union4.Stats, CPpmd_State) + +void Ppmd7_Update1(CPpmd7 *p); +void Ppmd7_Update1_0(CPpmd7 *p); +void Ppmd7_Update2(CPpmd7 *p); + +#define PPMD7_HiBitsFlag_3(sym) ((((unsigned)sym + 0xC0) >> (8 - 3)) & (1 << 3)) +#define PPMD7_HiBitsFlag_4(sym) ((((unsigned)sym + 0xC0) >> (8 - 4)) & (1 << 4)) +// #define PPMD7_HiBitsFlag_3(sym) ((sym) < 0x40 ? 0 : (1 << 3)) +// #define PPMD7_HiBitsFlag_4(sym) ((sym) < 0x40 ? 0 : (1 << 4)) + +#define Ppmd7_GetBinSumm(p) \ + &p->BinSumm[(size_t)(unsigned)Ppmd7Context_OneState(p->MinContext)->Freq - 1] \ + [ p->PrevSuccess + ((p->RunLength >> 26) & 0x20) \ + + p->NS2BSIndx[(size_t)Ppmd7_GetContext(p, p->MinContext->Suffix)->NumStats - 1] \ + + PPMD7_HiBitsFlag_4(Ppmd7Context_OneState(p->MinContext)->Symbol) \ + + (p->HiBitsFlag = PPMD7_HiBitsFlag_3(p->FoundState->Symbol)) ] + +CPpmd_See *Ppmd7_MakeEscFreq(CPpmd7 *p, unsigned numMasked, UInt32 *scale); + + +/* +We support two versions of Ppmd7 (PPMdH) methods that use same CPpmd7 structure: + 1) Ppmd7a_*: original PPMdH + 2) Ppmd7z_*: modified PPMdH with 7z Range Coder +Ppmd7_*: the structures and functions that are common for both versions of PPMd7 (PPMdH) +*/ + +/* ---------- Decode ---------- */ + +#define PPMD7_SYM_END (-1) +#define PPMD7_SYM_ERROR (-2) + +/* +You must set (CPpmd7::rc.dec.Stream) before Ppmd7*_RangeDec_Init() + +Ppmd7*_DecodeSymbol() +out: + >= 0 : decoded byte + -1 : PPMD7_SYM_END : End of payload marker + -2 : PPMD7_SYM_ERROR : Data error +*/ + +/* Ppmd7a_* : original PPMdH */ +BoolInt Ppmd7a_RangeDec_Init(CPpmd7_RangeDec *p); +#define Ppmd7a_RangeDec_IsFinishedOK(p) ((p)->Code == 0) +int Ppmd7a_DecodeSymbol(CPpmd7 *p); + +/* Ppmd7z_* : modified PPMdH with 7z Range Coder */ +BoolInt Ppmd7z_RangeDec_Init(CPpmd7_RangeDec *p); +#define Ppmd7z_RangeDec_IsFinishedOK(p) ((p)->Code == 0) +int Ppmd7z_DecodeSymbol(CPpmd7 *p); +// Byte *Ppmd7z_DecodeSymbols(CPpmd7 *p, Byte *buf, const Byte *lim); + + +/* ---------- Encode ---------- */ + +void Ppmd7z_Init_RangeEnc(CPpmd7 *p); +void Ppmd7z_Flush_RangeEnc(CPpmd7 *p); +// void Ppmd7z_EncodeSymbol(CPpmd7 *p, int symbol); +void Ppmd7z_EncodeSymbols(CPpmd7 *p, const Byte *buf, const Byte *lim); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Precomp.h b/src/lzma-sdk/C/Precomp.h new file mode 100644 index 0000000..edb5814 --- /dev/null +++ b/src/lzma-sdk/C/Precomp.h @@ -0,0 +1,10 @@ +/* Precomp.h -- StdAfx +2013-11-12 : Igor Pavlov : Public domain */ + +#ifndef __7Z_PRECOMP_H +#define __7Z_PRECOMP_H + +#include "Compiler.h" +/* #include "7zTypes.h" */ + +#endif diff --git a/src/lzma-sdk/C/RotateDefs.h b/src/lzma-sdk/C/RotateDefs.h new file mode 100644 index 0000000..6c790e7 --- /dev/null +++ b/src/lzma-sdk/C/RotateDefs.h @@ -0,0 +1,30 @@ +/* RotateDefs.h -- Rotate functions +2015-03-25 : Igor Pavlov : Public domain */ + +#ifndef __ROTATE_DEFS_H +#define __ROTATE_DEFS_H + +#ifdef _MSC_VER + +#include <stdlib.h> + +/* don't use _rotl with MINGW. It can insert slow call to function. */ + +/* #if (_MSC_VER >= 1200) */ +#pragma intrinsic(_rotl) +#pragma intrinsic(_rotr) +/* #endif */ + +#define rotlFixed(x, n) _rotl((x), (n)) +#define rotrFixed(x, n) _rotr((x), (n)) + +#else + +/* new compilers can translate these macros to fast commands. */ + +#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) + +#endif + +#endif diff --git a/src/lzma-sdk/C/Sha256.h b/src/lzma-sdk/C/Sha256.h new file mode 100644 index 0000000..f529339 --- /dev/null +++ b/src/lzma-sdk/C/Sha256.h @@ -0,0 +1,76 @@ +/* Sha256.h -- SHA-256 Hash +2021-01-01 : Igor Pavlov : Public domain */ + +#ifndef __7Z_SHA256_H +#define __7Z_SHA256_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#define SHA256_NUM_BLOCK_WORDS 16 +#define SHA256_NUM_DIGEST_WORDS 8 + +#define SHA256_BLOCK_SIZE (SHA256_NUM_BLOCK_WORDS * 4) +#define SHA256_DIGEST_SIZE (SHA256_NUM_DIGEST_WORDS * 4) + +typedef void (MY_FAST_CALL *SHA256_FUNC_UPDATE_BLOCKS)(UInt32 state[8], const Byte *data, size_t numBlocks); + +/* + if (the system supports different SHA256 code implementations) + { + (CSha256::func_UpdateBlocks) will be used + (CSha256::func_UpdateBlocks) can be set by + Sha256_Init() - to default (fastest) + Sha256_SetFunction() - to any algo + } + else + { + (CSha256::func_UpdateBlocks) is ignored. + } +*/ + +typedef struct +{ + SHA256_FUNC_UPDATE_BLOCKS func_UpdateBlocks; + UInt64 count; + UInt64 __pad_2[2]; + UInt32 state[SHA256_NUM_DIGEST_WORDS]; + + Byte buffer[SHA256_BLOCK_SIZE]; +} CSha256; + + +#define SHA256_ALGO_DEFAULT 0 +#define SHA256_ALGO_SW 1 +#define SHA256_ALGO_HW 2 + +/* +Sha256_SetFunction() +return: + 0 - (algo) value is not supported, and func_UpdateBlocks was not changed + 1 - func_UpdateBlocks was set according (algo) value. +*/ + +BoolInt Sha256_SetFunction(CSha256 *p, unsigned algo); + +void Sha256_InitState(CSha256 *p); +void Sha256_Init(CSha256 *p); +void Sha256_Update(CSha256 *p, const Byte *data, size_t size); +void Sha256_Final(CSha256 *p, Byte *digest); + + + + +// void MY_FAST_CALL Sha256_UpdateBlocks(UInt32 state[8], const Byte *data, size_t numBlocks); + +/* +call Sha256Prepare() once at program start. +It prepares all supported implementations, and detects the fastest implementation. +*/ + +void Sha256Prepare(void); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Sort.h b/src/lzma-sdk/C/Sort.h new file mode 100644 index 0000000..7209d78 --- /dev/null +++ b/src/lzma-sdk/C/Sort.h @@ -0,0 +1,18 @@ +/* Sort.h -- Sort functions +2014-04-05 : Igor Pavlov : Public domain */ + +#ifndef __7Z_SORT_H +#define __7Z_SORT_H + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +void HeapSort(UInt32 *p, size_t size); +void HeapSort64(UInt64 *p, size_t size); + +/* void HeapSortRef(UInt32 *p, UInt32 *vals, size_t size); */ + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Threads.h b/src/lzma-sdk/C/Threads.h new file mode 100644 index 0000000..e9493af --- /dev/null +++ b/src/lzma-sdk/C/Threads.h @@ -0,0 +1,232 @@ +/* Threads.h -- multithreading library +2021-12-21 : Igor Pavlov : Public domain */ + +#ifndef __7Z_THREADS_H +#define __7Z_THREADS_H + +#ifdef _WIN32 +#include <Windows.h> +#else + +#if defined(__linux__) +#if !defined(__APPLE__) && !defined(_AIX) && !defined(__ANDROID__) +#ifndef _7ZIP_AFFINITY_DISABLE +#define _7ZIP_AFFINITY_SUPPORTED +// #pragma message(" ==== _7ZIP_AFFINITY_SUPPORTED") +// #define _GNU_SOURCE +#endif +#endif +#endif + +#include <pthread.h> + +#endif + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +#ifdef _WIN32 + +WRes HandlePtr_Close(HANDLE *h); +WRes Handle_WaitObject(HANDLE h); + +typedef HANDLE CThread; + +#define Thread_Construct(p) { *(p) = NULL; } +#define Thread_WasCreated(p) (*(p) != NULL) +#define Thread_Close(p) HandlePtr_Close(p) +// #define Thread_Wait(p) Handle_WaitObject(*(p)) + +#ifdef UNDER_CE + // if (USE_THREADS_CreateThread is defined), we use _beginthreadex() + // if (USE_THREADS_CreateThread is not definned), we use CreateThread() + #define USE_THREADS_CreateThread +#endif + +typedef + #ifdef USE_THREADS_CreateThread + DWORD + #else + unsigned + #endif + THREAD_FUNC_RET_TYPE; + +typedef DWORD_PTR CAffinityMask; +typedef DWORD_PTR CCpuSet; + +#define CpuSet_Zero(p) { *(p) = 0; } +#define CpuSet_Set(p, cpu) { *(p) |= ((DWORD_PTR)1 << (cpu)); } + +#else // _WIN32 + +typedef struct _CThread +{ + pthread_t _tid; + int _created; +} CThread; + +#define Thread_Construct(p) { (p)->_tid = 0; (p)->_created = 0; } +#define Thread_WasCreated(p) ((p)->_created != 0) +WRes Thread_Close(CThread *p); +// #define Thread_Wait Thread_Wait_Close + +typedef void * THREAD_FUNC_RET_TYPE; + +typedef UInt64 CAffinityMask; + +#ifdef _7ZIP_AFFINITY_SUPPORTED + +typedef cpu_set_t CCpuSet; +#define CpuSet_Zero(p) CPU_ZERO(p) +#define CpuSet_Set(p, cpu) CPU_SET(cpu, p) +#define CpuSet_IsSet(p, cpu) CPU_ISSET(cpu, p) + +#else + +typedef UInt64 CCpuSet; +#define CpuSet_Zero(p) { *(p) = 0; } +#define CpuSet_Set(p, cpu) { *(p) |= ((UInt64)1 << (cpu)); } +#define CpuSet_IsSet(p, cpu) ((*(p) & ((UInt64)1 << (cpu))) != 0) + +#endif + + +#endif // _WIN32 + + +#define THREAD_FUNC_CALL_TYPE MY_STD_CALL + +#if defined(_WIN32) && defined(__GNUC__) +/* GCC compiler for x86 32-bit uses the rule: + the stack is 16-byte aligned before CALL instruction for function calling. + But only root function main() contains instructions that + set 16-byte alignment for stack pointer. And another functions + just keep alignment, if it was set in some parent function. + + The problem: + if we create new thread in MinGW (GCC) 32-bit x86 via _beginthreadex() or CreateThread(), + the root function of thread doesn't set 16-byte alignment. + And stack frames in all child functions also will be unaligned in that case. + + Here we set (force_align_arg_pointer) attribute for root function of new thread. + Do we need (force_align_arg_pointer) also for another systems? */ + + #define THREAD_FUNC_ATTRIB_ALIGN_ARG __attribute__((force_align_arg_pointer)) + // #define THREAD_FUNC_ATTRIB_ALIGN_ARG // for debug : bad alignment in SSE functions +#else + #define THREAD_FUNC_ATTRIB_ALIGN_ARG +#endif + +#define THREAD_FUNC_DECL THREAD_FUNC_ATTRIB_ALIGN_ARG THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE + +typedef THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE * THREAD_FUNC_TYPE)(void *); +WRes Thread_Create(CThread *p, THREAD_FUNC_TYPE func, LPVOID param); +WRes Thread_Create_With_Affinity(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, CAffinityMask affinity); +WRes Thread_Wait_Close(CThread *p); + +#ifdef _WIN32 +#define Thread_Create_With_CpuSet(p, func, param, cs) \ + Thread_Create_With_Affinity(p, func, param, *cs) +#else +WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, const CCpuSet *cpuSet); +#endif + + +#ifdef _WIN32 + +typedef HANDLE CEvent; +typedef CEvent CAutoResetEvent; +typedef CEvent CManualResetEvent; +#define Event_Construct(p) *(p) = NULL +#define Event_IsCreated(p) (*(p) != NULL) +#define Event_Close(p) HandlePtr_Close(p) +#define Event_Wait(p) Handle_WaitObject(*(p)) +WRes Event_Set(CEvent *p); +WRes Event_Reset(CEvent *p); +WRes ManualResetEvent_Create(CManualResetEvent *p, int signaled); +WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p); +WRes AutoResetEvent_Create(CAutoResetEvent *p, int signaled); +WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p); + +typedef HANDLE CSemaphore; +#define Semaphore_Construct(p) *(p) = NULL +#define Semaphore_IsCreated(p) (*(p) != NULL) +#define Semaphore_Close(p) HandlePtr_Close(p) +#define Semaphore_Wait(p) Handle_WaitObject(*(p)) +WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount); +WRes Semaphore_OptCreateInit(CSemaphore *p, UInt32 initCount, UInt32 maxCount); +WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num); +WRes Semaphore_Release1(CSemaphore *p); + +typedef CRITICAL_SECTION CCriticalSection; +WRes CriticalSection_Init(CCriticalSection *p); +#define CriticalSection_Delete(p) DeleteCriticalSection(p) +#define CriticalSection_Enter(p) EnterCriticalSection(p) +#define CriticalSection_Leave(p) LeaveCriticalSection(p) + + +#else // _WIN32 + +typedef struct _CEvent +{ + int _created; + int _manual_reset; + int _state; + pthread_mutex_t _mutex; + pthread_cond_t _cond; +} CEvent; + +typedef CEvent CAutoResetEvent; +typedef CEvent CManualResetEvent; + +#define Event_Construct(p) (p)->_created = 0 +#define Event_IsCreated(p) ((p)->_created) + +WRes ManualResetEvent_Create(CManualResetEvent *p, int signaled); +WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p); +WRes AutoResetEvent_Create(CAutoResetEvent *p, int signaled); +WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p); +WRes Event_Set(CEvent *p); +WRes Event_Reset(CEvent *p); +WRes Event_Wait(CEvent *p); +WRes Event_Close(CEvent *p); + + +typedef struct _CSemaphore +{ + int _created; + UInt32 _count; + UInt32 _maxCount; + pthread_mutex_t _mutex; + pthread_cond_t _cond; +} CSemaphore; + +#define Semaphore_Construct(p) (p)->_created = 0 +#define Semaphore_IsCreated(p) ((p)->_created) + +WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount); +WRes Semaphore_OptCreateInit(CSemaphore *p, UInt32 initCount, UInt32 maxCount); +WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num); +#define Semaphore_Release1(p) Semaphore_ReleaseN(p, 1) +WRes Semaphore_Wait(CSemaphore *p); +WRes Semaphore_Close(CSemaphore *p); + + +typedef struct _CCriticalSection +{ + pthread_mutex_t _mutex; +} CCriticalSection; + +WRes CriticalSection_Init(CCriticalSection *p); +void CriticalSection_Delete(CCriticalSection *cs); +void CriticalSection_Enter(CCriticalSection *cs); +void CriticalSection_Leave(CCriticalSection *cs); + +LONG InterlockedIncrement(LONG volatile *addend); + +#endif // _WIN32 + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/Util/7z/Precomp.h b/src/lzma-sdk/C/Util/7z/Precomp.h new file mode 100644 index 0000000..9f398d0 --- /dev/null +++ b/src/lzma-sdk/C/Util/7z/Precomp.h @@ -0,0 +1,10 @@ +/* Precomp.h -- StdAfx +2013-06-16 : Igor Pavlov : Public domain */ + +#ifndef __7Z_PRECOMP_H +#define __7Z_PRECOMP_H + +#include "../../Compiler.h" +#include "../../7zTypes.h" + +#endif diff --git a/src/lzma-sdk/C/Util/SfxSetup/Precomp.h b/src/lzma-sdk/C/Util/SfxSetup/Precomp.h new file mode 100644 index 0000000..9f398d0 --- /dev/null +++ b/src/lzma-sdk/C/Util/SfxSetup/Precomp.h @@ -0,0 +1,10 @@ +/* Precomp.h -- StdAfx +2013-06-16 : Igor Pavlov : Public domain */ + +#ifndef __7Z_PRECOMP_H +#define __7Z_PRECOMP_H + +#include "../../Compiler.h" +#include "../../7zTypes.h" + +#endif diff --git a/src/lzma-sdk/C/Xz.h b/src/lzma-sdk/C/Xz.h new file mode 100644 index 0000000..cf9458e --- /dev/null +++ b/src/lzma-sdk/C/Xz.h @@ -0,0 +1,517 @@ +/* Xz.h - Xz interface +2021-04-01 : Igor Pavlov : Public domain */ + +#ifndef __XZ_H +#define __XZ_H + +#include "Sha256.h" + +EXTERN_C_BEGIN + +#define XZ_ID_Subblock 1 +#define XZ_ID_Delta 3 +#define XZ_ID_X86 4 +#define XZ_ID_PPC 5 +#define XZ_ID_IA64 6 +#define XZ_ID_ARM 7 +#define XZ_ID_ARMT 8 +#define XZ_ID_SPARC 9 +#define XZ_ID_LZMA2 0x21 + +unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value); +unsigned Xz_WriteVarInt(Byte *buf, UInt64 v); + +/* ---------- xz block ---------- */ + +#define XZ_BLOCK_HEADER_SIZE_MAX 1024 + +#define XZ_NUM_FILTERS_MAX 4 +#define XZ_BF_NUM_FILTERS_MASK 3 +#define XZ_BF_PACK_SIZE (1 << 6) +#define XZ_BF_UNPACK_SIZE (1 << 7) + +#define XZ_FILTER_PROPS_SIZE_MAX 20 + +typedef struct +{ + UInt64 id; + UInt32 propsSize; + Byte props[XZ_FILTER_PROPS_SIZE_MAX]; +} CXzFilter; + +typedef struct +{ + UInt64 packSize; + UInt64 unpackSize; + Byte flags; + CXzFilter filters[XZ_NUM_FILTERS_MAX]; +} CXzBlock; + +#define XzBlock_GetNumFilters(p) (((unsigned)(p)->flags & XZ_BF_NUM_FILTERS_MASK) + 1) +#define XzBlock_HasPackSize(p) (((p)->flags & XZ_BF_PACK_SIZE) != 0) +#define XzBlock_HasUnpackSize(p) (((p)->flags & XZ_BF_UNPACK_SIZE) != 0) +#define XzBlock_HasUnsupportedFlags(p) (((p)->flags & ~(XZ_BF_NUM_FILTERS_MASK | XZ_BF_PACK_SIZE | XZ_BF_UNPACK_SIZE)) != 0) + +SRes XzBlock_Parse(CXzBlock *p, const Byte *header); +SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, BoolInt *isIndex, UInt32 *headerSizeRes); + +/* ---------- xz stream ---------- */ + +#define XZ_SIG_SIZE 6 +#define XZ_FOOTER_SIG_SIZE 2 + +extern const Byte XZ_SIG[XZ_SIG_SIZE]; + +/* +extern const Byte XZ_FOOTER_SIG[XZ_FOOTER_SIG_SIZE]; +*/ + +#define XZ_FOOTER_SIG_0 'Y' +#define XZ_FOOTER_SIG_1 'Z' + +#define XZ_STREAM_FLAGS_SIZE 2 +#define XZ_STREAM_CRC_SIZE 4 + +#define XZ_STREAM_HEADER_SIZE (XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE + XZ_STREAM_CRC_SIZE) +#define XZ_STREAM_FOOTER_SIZE (XZ_FOOTER_SIG_SIZE + XZ_STREAM_FLAGS_SIZE + XZ_STREAM_CRC_SIZE + 4) + +#define XZ_CHECK_MASK 0xF +#define XZ_CHECK_NO 0 +#define XZ_CHECK_CRC32 1 +#define XZ_CHECK_CRC64 4 +#define XZ_CHECK_SHA256 10 + +typedef struct +{ + unsigned mode; + UInt32 crc; + UInt64 crc64; + CSha256 sha; +} CXzCheck; + +void XzCheck_Init(CXzCheck *p, unsigned mode); +void XzCheck_Update(CXzCheck *p, const void *data, size_t size); +int XzCheck_Final(CXzCheck *p, Byte *digest); + +typedef UInt16 CXzStreamFlags; + +#define XzFlags_IsSupported(f) ((f) <= XZ_CHECK_MASK) +#define XzFlags_GetCheckType(f) ((f) & XZ_CHECK_MASK) +#define XzFlags_HasDataCrc32(f) (Xz_GetCheckType(f) == XZ_CHECK_CRC32) +unsigned XzFlags_GetCheckSize(CXzStreamFlags f); + +SRes Xz_ParseHeader(CXzStreamFlags *p, const Byte *buf); +SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream); + +typedef struct +{ + UInt64 unpackSize; + UInt64 totalSize; +} CXzBlockSizes; + +typedef struct +{ + CXzStreamFlags flags; + size_t numBlocks; + CXzBlockSizes *blocks; + UInt64 startOffset; +} CXzStream; + +void Xz_Construct(CXzStream *p); +void Xz_Free(CXzStream *p, ISzAllocPtr alloc); + +#define XZ_SIZE_OVERFLOW ((UInt64)(Int64)-1) + +UInt64 Xz_GetUnpackSize(const CXzStream *p); +UInt64 Xz_GetPackSize(const CXzStream *p); + +typedef struct +{ + size_t num; + size_t numAllocated; + CXzStream *streams; +} CXzs; + +void Xzs_Construct(CXzs *p); +void Xzs_Free(CXzs *p, ISzAllocPtr alloc); +SRes Xzs_ReadBackward(CXzs *p, ILookInStream *inStream, Int64 *startOffset, ICompressProgress *progress, ISzAllocPtr alloc); + +UInt64 Xzs_GetNumBlocks(const CXzs *p); +UInt64 Xzs_GetUnpackSize(const CXzs *p); + + +// ECoderStatus values are identical to ELzmaStatus values of LZMA2 decoder + +typedef enum +{ + CODER_STATUS_NOT_SPECIFIED, /* use main error code instead */ + CODER_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ + CODER_STATUS_NOT_FINISHED, /* stream was not finished */ + CODER_STATUS_NEEDS_MORE_INPUT /* you must provide more input bytes */ +} ECoderStatus; + + +// ECoderFinishMode values are identical to ELzmaFinishMode + +typedef enum +{ + CODER_FINISH_ANY, /* finish at any point */ + CODER_FINISH_END /* block must be finished at the end */ +} ECoderFinishMode; + + +typedef struct _IStateCoder +{ + void *p; + void (*Free)(void *p, ISzAllocPtr alloc); + SRes (*SetProps)(void *p, const Byte *props, size_t propSize, ISzAllocPtr alloc); + void (*Init)(void *p); + SRes (*Code2)(void *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, + int srcWasFinished, ECoderFinishMode finishMode, + // int *wasFinished, + ECoderStatus *status); + SizeT (*Filter)(void *p, Byte *data, SizeT size); +} IStateCoder; + + + +#define MIXCODER_NUM_FILTERS_MAX 4 + +typedef struct +{ + ISzAllocPtr alloc; + Byte *buf; + unsigned numCoders; + + Byte *outBuf; + size_t outBufSize; + size_t outWritten; // is equal to lzmaDecoder.dicPos (in outBuf mode) + BoolInt wasFinished; + SRes res; + ECoderStatus status; + // BoolInt SingleBufMode; + + int finished[MIXCODER_NUM_FILTERS_MAX - 1]; + size_t pos[MIXCODER_NUM_FILTERS_MAX - 1]; + size_t size[MIXCODER_NUM_FILTERS_MAX - 1]; + UInt64 ids[MIXCODER_NUM_FILTERS_MAX]; + SRes results[MIXCODER_NUM_FILTERS_MAX]; + IStateCoder coders[MIXCODER_NUM_FILTERS_MAX]; +} CMixCoder; + + +typedef enum +{ + XZ_STATE_STREAM_HEADER, + XZ_STATE_STREAM_INDEX, + XZ_STATE_STREAM_INDEX_CRC, + XZ_STATE_STREAM_FOOTER, + XZ_STATE_STREAM_PADDING, + XZ_STATE_BLOCK_HEADER, + XZ_STATE_BLOCK, + XZ_STATE_BLOCK_FOOTER +} EXzState; + + +typedef struct +{ + EXzState state; + UInt32 pos; + unsigned alignPos; + unsigned indexPreSize; + + CXzStreamFlags streamFlags; + + UInt32 blockHeaderSize; + UInt64 packSize; + UInt64 unpackSize; + + UInt64 numBlocks; // number of finished blocks in current stream + UInt64 indexSize; + UInt64 indexPos; + UInt64 padSize; + + UInt64 numStartedStreams; + UInt64 numFinishedStreams; + UInt64 numTotalBlocks; + + UInt32 crc; + CMixCoder decoder; + CXzBlock block; + CXzCheck check; + CSha256 sha; + + BoolInt parseMode; + BoolInt headerParsedOk; + BoolInt decodeToStreamSignature; + unsigned decodeOnlyOneBlock; + + Byte *outBuf; + size_t outBufSize; + size_t outDataWritten; // the size of data in (outBuf) that were fully unpacked + + Byte shaDigest[SHA256_DIGEST_SIZE]; + Byte buf[XZ_BLOCK_HEADER_SIZE_MAX]; +} CXzUnpacker; + +/* alloc : aligned for cache line allocation is better */ +void XzUnpacker_Construct(CXzUnpacker *p, ISzAllocPtr alloc); +void XzUnpacker_Init(CXzUnpacker *p); +void XzUnpacker_SetOutBuf(CXzUnpacker *p, Byte *outBuf, size_t outBufSize); +void XzUnpacker_Free(CXzUnpacker *p); + +/* + XzUnpacker + The sequence for decoding functions: + { + XzUnpacker_Construct() + [Decoding_Calls] + XzUnpacker_Free() + } + + [Decoding_Calls] + + There are 3 types of interfaces for [Decoding_Calls] calls: + + Interface-1 : Partial output buffers: + { + XzUnpacker_Init() + for() + { + XzUnpacker_Code(); + } + XzUnpacker_IsStreamWasFinished() + } + + Interface-2 : Direct output buffer: + Use it, if you know exact size of decoded data, and you need + whole xz unpacked data in one output buffer. + xz unpacker doesn't allocate additional buffer for lzma2 dictionary in that mode. + { + XzUnpacker_Init() + XzUnpacker_SetOutBufMode(); // to set output buffer and size + for() + { + XzUnpacker_Code(); // (dest = NULL) in XzUnpacker_Code() + } + XzUnpacker_IsStreamWasFinished() + } + + Interface-3 : Direct output buffer : One call full decoding + It unpacks whole input buffer to output buffer in one call. + It uses Interface-2 internally. + { + XzUnpacker_CodeFull() + XzUnpacker_IsStreamWasFinished() + } +*/ + +/* +finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + CODER_FINISH_ANY - use smallest number of input bytes + CODER_FINISH_END - read EndOfStream marker after decoding + +Returns: + SZ_OK + status: + CODER_STATUS_NOT_FINISHED, + CODER_STATUS_NEEDS_MORE_INPUT - the decoder can return it in two cases: + 1) it needs more input data to finish current xz stream + 2) xz stream was finished successfully. But the decoder supports multiple + concatented xz streams. So it expects more input data for new xz streams. + Call XzUnpacker_IsStreamWasFinished() to check that latest xz stream was finished successfully. + + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_DATA - Data error + SZ_ERROR_UNSUPPORTED - Unsupported method or method properties + SZ_ERROR_CRC - CRC error + // SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). + + SZ_ERROR_NO_ARCHIVE - the error with xz Stream Header with one of the following reasons: + - xz Stream Signature failure + - CRC32 of xz Stream Header is failed + - The size of Stream padding is not multiple of four bytes. + It's possible to get that error, if xz stream was finished and the stream + contains some another data. In that case you can call XzUnpacker_GetExtraSize() + function to get real size of xz stream. +*/ + + +SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen, + const Byte *src, SizeT *srcLen, int srcFinished, + ECoderFinishMode finishMode, ECoderStatus *status); + +SRes XzUnpacker_CodeFull(CXzUnpacker *p, Byte *dest, SizeT *destLen, + const Byte *src, SizeT *srcLen, + ECoderFinishMode finishMode, ECoderStatus *status); + +/* +If you decode full xz stream(s), then you can call XzUnpacker_IsStreamWasFinished() +after successful XzUnpacker_CodeFull() or after last call of XzUnpacker_Code(). +*/ + +BoolInt XzUnpacker_IsStreamWasFinished(const CXzUnpacker *p); + +/* +XzUnpacker_GetExtraSize() returns then number of unconfirmed bytes, + if it's in (XZ_STATE_STREAM_HEADER) state or in (XZ_STATE_STREAM_PADDING) state. +These bytes can be some data after xz archive, or +it can be start of new xz stream. + +Call XzUnpacker_GetExtraSize() after XzUnpacker_Code() function to detect real size of +xz stream in two cases, if XzUnpacker_Code() returns: + res == SZ_OK && status == CODER_STATUS_NEEDS_MORE_INPUT + res == SZ_ERROR_NO_ARCHIVE +*/ + +UInt64 XzUnpacker_GetExtraSize(const CXzUnpacker *p); + + +/* + for random block decoding: + XzUnpacker_Init(); + set CXzUnpacker::streamFlags + XzUnpacker_PrepareToRandomBlockDecoding() + loop + { + XzUnpacker_Code() + XzUnpacker_IsBlockFinished() + } +*/ + +void XzUnpacker_PrepareToRandomBlockDecoding(CXzUnpacker *p); +BoolInt XzUnpacker_IsBlockFinished(const CXzUnpacker *p); + +#define XzUnpacker_GetPackSizeForIndex(p) ((p)->packSize + (p)->blockHeaderSize + XzFlags_GetCheckSize((p)->streamFlags)) + + + + + + +/* ---- Single-Thread and Multi-Thread xz Decoding with Input/Output Streams ---- */ + +/* + if (CXzDecMtProps::numThreads > 1), the decoder can try to use + Multi-Threading. The decoder analyses xz block header, and if + there are pack size and unpack size values stored in xz block header, + the decoder reads compressed data of block to internal buffers, + and then it can start parallel decoding, if there are another blocks. + The decoder can switch back to Single-Thread decoding after some conditions. + + The sequence of calls for xz decoding with in/out Streams: + { + XzDecMt_Create() + XzDecMtProps_Init(XzDecMtProps) to set default values of properties + // then you can change some XzDecMtProps parameters with required values + // here you can set the number of threads and (memUseMax) - the maximum + Memory usage for multithreading decoding. + for() + { + XzDecMt_Decode() // one call per one file + } + XzDecMt_Destroy() + } +*/ + + +typedef struct +{ + size_t inBufSize_ST; // size of input buffer for Single-Thread decoding + size_t outStep_ST; // size of output buffer for Single-Thread decoding + BoolInt ignoreErrors; // if set to 1, the decoder can ignore some errors and it skips broken parts of data. + + #ifndef _7ZIP_ST + unsigned numThreads; // the number of threads for Multi-Thread decoding. if (umThreads == 1) it will use Single-thread decoding + size_t inBufSize_MT; // size of small input data buffers for Multi-Thread decoding. Big number of such small buffers can be created + size_t memUseMax; // the limit of total memory usage for Multi-Thread decoding. + // it's recommended to set (memUseMax) manually to value that is smaller of total size of RAM in computer. + #endif +} CXzDecMtProps; + +void XzDecMtProps_Init(CXzDecMtProps *p); + + +typedef void * CXzDecMtHandle; + +/* + alloc : XzDecMt uses CAlignOffsetAlloc internally for addresses allocated by (alloc). + allocMid : for big allocations, aligned allocation is better +*/ + +CXzDecMtHandle XzDecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid); +void XzDecMt_Destroy(CXzDecMtHandle p); + + +typedef struct +{ + Byte UnpackSize_Defined; + Byte NumStreams_Defined; + Byte NumBlocks_Defined; + + Byte DataAfterEnd; // there are some additional data after good xz streams, and that data is not new xz stream. + Byte DecodingTruncated; // Decoding was Truncated, we need only partial output data + + UInt64 InSize; // pack size processed. That value doesn't include the data after + // end of xz stream, if that data was not correct + UInt64 OutSize; + + UInt64 NumStreams; + UInt64 NumBlocks; + + SRes DecodeRes; // the error code of xz streams data decoding + SRes ReadRes; // error code from ISeqInStream:Read() + SRes ProgressRes; // error code from ICompressProgress:Progress() + + SRes CombinedRes; // Combined result error code that shows main rusult + // = S_OK, if there is no error. + // but check also (DataAfterEnd) that can show additional minor errors. + + SRes CombinedRes_Type; // = SZ_ERROR_READ, if error from ISeqInStream + // = SZ_ERROR_PROGRESS, if error from ICompressProgress + // = SZ_ERROR_WRITE, if error from ISeqOutStream + // = SZ_ERROR_* codes for decoding +} CXzStatInfo; + +void XzStatInfo_Clear(CXzStatInfo *p); + +/* + +XzDecMt_Decode() +SRes: it's combined decoding result. It also is equal to stat->CombinedRes. + + SZ_OK - no error + check also output value in (stat->DataAfterEnd) + that can show additional possible error + + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_NO_ARCHIVE - is not xz archive + SZ_ERROR_ARCHIVE - Headers error + SZ_ERROR_DATA - Data Error + SZ_ERROR_UNSUPPORTED - Unsupported method or method properties + SZ_ERROR_CRC - CRC Error + SZ_ERROR_INPUT_EOF - it needs more input data + SZ_ERROR_WRITE - ISeqOutStream error + (SZ_ERROR_READ) - ISeqInStream errors + (SZ_ERROR_PROGRESS) - ICompressProgress errors + // SZ_ERROR_THREAD - error in multi-threading functions + MY_SRes_HRESULT_FROM_WRes(WRes_error) - error in multi-threading function +*/ + +SRes XzDecMt_Decode(CXzDecMtHandle p, + const CXzDecMtProps *props, + const UInt64 *outDataSize, // NULL means undefined + int finishMode, // 0 - partial unpacking is allowed, 1 - xz stream(s) must be finished + ISeqOutStream *outStream, + // Byte *outBuf, size_t *outBufSize, + ISeqInStream *inStream, + // const Byte *inData, size_t inDataSize, + CXzStatInfo *stat, // out: decoding results and statistics + int *isMT, // out: 0 means that ST (Single-Thread) version was used + // 1 means that MT (Multi-Thread) version was used + ICompressProgress *progress); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/XzCrc64.h b/src/lzma-sdk/C/XzCrc64.h new file mode 100644 index 0000000..71b10d5 --- /dev/null +++ b/src/lzma-sdk/C/XzCrc64.h @@ -0,0 +1,26 @@ +/* XzCrc64.h -- CRC64 calculation +2013-01-18 : Igor Pavlov : Public domain */ + +#ifndef __XZ_CRC64_H +#define __XZ_CRC64_H + +#include <stddef.h> + +#include "7zTypes.h" + +EXTERN_C_BEGIN + +extern UInt64 g_Crc64Table[]; + +void MY_FAST_CALL Crc64GenerateTable(void); + +#define CRC64_INIT_VAL UINT64_CONST(0xFFFFFFFFFFFFFFFF) +#define CRC64_GET_DIGEST(crc) ((crc) ^ CRC64_INIT_VAL) +#define CRC64_UPDATE_BYTE(crc, b) (g_Crc64Table[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) + +UInt64 MY_FAST_CALL Crc64Update(UInt64 crc, const void *data, size_t size); +UInt64 MY_FAST_CALL Crc64Calc(const void *data, size_t size); + +EXTERN_C_END + +#endif diff --git a/src/lzma-sdk/C/XzEnc.h b/src/lzma-sdk/C/XzEnc.h new file mode 100644 index 0000000..529ac3f --- /dev/null +++ b/src/lzma-sdk/C/XzEnc.h @@ -0,0 +1,60 @@ +/* XzEnc.h -- Xz Encode +2017-06-27 : Igor Pavlov : Public domain */ + +#ifndef __XZ_ENC_H +#define __XZ_ENC_H + +#include "Lzma2Enc.h" + +#include "Xz.h" + +EXTERN_C_BEGIN + + +#define XZ_PROPS__BLOCK_SIZE__AUTO LZMA2_ENC_PROPS__BLOCK_SIZE__AUTO +#define XZ_PROPS__BLOCK_SIZE__SOLID LZMA2_ENC_PROPS__BLOCK_SIZE__SOLID + + +typedef struct +{ + UInt32 id; + UInt32 delta; + UInt32 ip; + int ipDefined; +} CXzFilterProps; + +void XzFilterProps_Init(CXzFilterProps *p); + + +typedef struct +{ + CLzma2EncProps lzma2Props; + CXzFilterProps filterProps; + unsigned checkId; + UInt64 blockSize; + int numBlockThreads_Reduced; + int numBlockThreads_Max; + int numTotalThreads; + int forceWriteSizesInHeader; + UInt64 reduceSize; +} CXzProps; + +void XzProps_Init(CXzProps *p); + + +typedef void * CXzEncHandle; + +CXzEncHandle XzEnc_Create(ISzAllocPtr alloc, ISzAllocPtr allocBig); +void XzEnc_Destroy(CXzEncHandle p); +SRes XzEnc_SetProps(CXzEncHandle p, const CXzProps *props); +void XzEnc_SetDataSize(CXzEncHandle p, UInt64 expectedDataSiize); +SRes XzEnc_Encode(CXzEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress); + +SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream, + const CXzProps *props, ICompressProgress *progress); + +SRes Xz_EncodeEmpty(ISeqOutStream *outStream); + +EXTERN_C_END + +#endif diff --git a/src/uadk/v1/uacce.h b/src/uadk/v1/uacce.h new file mode 100644 index 0000000..d45062e --- /dev/null +++ b/src/uadk/v1/uacce.h @@ -0,0 +1,86 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _UAPI_WD_UACCE_H +#define _UAPI_WD_UACCE_H + +#include <linux/types.h> +#include <linux/ioctl.h> + +#define WD_UACCE_CLASS_NAME "uacce" + +/** + * WD_UACCE Device Attributes: + * + * NOIOMMU: the device has no IOMMU support + * can do ssva, but no map to the dev + * IOMMU: the device has IOMMU support + * PASID: the device has IOMMU which support PASID setting + * can do ssva, mapped to dev per process + * FAULT_FROM_DEV: the device has IOMMU which can do page fault request + * no need for ssva, should be used with PASID + * KMAP_DUS: map the Device user-shared space to kernel + * DRVMAP_DUS: Driver self-maintain its DUS + * SVA: full function device + * SHARE_DOMAIN: no PASID, can do ssva only for one process and the kernel + */ +#define WD_UACCE_DEV_SVA (1<<0) +#define WD_UACCE_DEV_NOIOMMU (1<<1) +#define WD_UACCE_DEV_PASID (1<<2) +#define WD_UACCE_DEV_IOMMU (1<<7) + +/* uacce mode of the driver */ +#define WD_UACCE_MODE_NOWD_UACCE 0 /* don't use uacce */ +#define WD_UACCE_MODE_NOIOMMU 2 /* use uacce noiommu mode */ + +#define WD_UACCE_API_VER_NOIOMMU_SUBFIX "_noiommu" +#define WD_UACCE_QFR_NA ((unsigned long)-1) + +/** + * enum uacce_qfrt: queue file region type + * @WD_UACCE_QFRT_MMIO: device mmio region + * @WD_UACCE_QFRT_DUS: device user share region + * @WD_UACCE_QFRT_SS: static share memory(no-sva) + */ +enum uacce_qfrt { + WD_UACCE_QFRT_MMIO = 0, /* device mmio region */ + WD_UACCE_QFRT_DUS, /* device user share */ + WD_UACCE_QFRT_SS, /* static share memory */ + WD_UACCE_QFRT_MAX, +}; + +#define WD_UACCE_QFRT_INVALID WD_UACCE_QFRT_MAX + +/* Pass DMA SS region slice size by granularity 64KB */ +#define WD_UACCE_GRAN_SIZE 0x10000ull +#define WD_UACCE_GRAN_SHIFT 16 +#define WD_UACCE_GRAN_NUM_MASK 0xfffull + +/* + * WD_UACCE_CMD_START_Q: Start queue + */ +#define WD_UACCE_CMD_START_Q _IO('W', 0) + +/* + * WD_UACCE_CMD_PUT_Q: + * User actively stop queue and free queue resource immediately + * Optimization method since close fd may delay + */ +#define WD_UACCE_CMD_PUT_Q _IO('W', 1) +#define WD_UACCE_CMD_SHARE_SVAS _IO('W', 2) +#define WD_UACCE_CMD_GET_SS_DMA _IOR('W', 3, unsigned long) + +#endif diff --git a/src/uadk/v1/wd.h b/src/uadk/v1/wd.h new file mode 100644 index 0000000..1bd9669 --- /dev/null +++ b/src/uadk/v1/wd.h @@ -0,0 +1,214 @@ +/* + * Copyright 2018-2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WD_H +#define __WD_H + +#include <stdlib.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <stdbool.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdint.h> +#include <unistd.h> +#include <assert.h> +#include "uacce.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define SYS_VAL_SIZE 16 +#define PATH_STR_SIZE 256 +#define MAX_ATTR_STR_SIZE 256 +#define WD_NAME_SIZE 64 +#define WCRYPTO_MAX_BURST_NUM 16 + +/* WD error code */ +#define WD_SUCCESS 0 +#define WD_STREAM_END 1 +#define WD_STREAM_START 2 +#define WD_EIO EIO +#define WD_EAGAIN EAGAIN +#define WD_ENOMEM ENOMEM +#define WD_EACCESS EACCESS +#define WD_EBUSY EBUSY +#define WD_ENODEV ENODEV +#define WD_EINVAL EINVAL +#define WD_ETIMEDOUT ETIMEDOUT +#define WD_ADDR_ERR 61 +#define WD_HW_EACCESS 62 +#define WD_SGL_ERR 63 +#define WD_VERIFY_ERR 64 +#define WD_OUT_EPARA 66 +#define WD_IN_EPARA 67 +#define WD_ENOPROC 68 + +typedef void (*wcrypto_cb)(const void *msg, void *tag); + +typedef void (*wd_log)(const char *format, ...); + +struct wcrypto_cb_tag { + void *ctx; /* user: context or other user relatives */ + void *tag; /* to store user tag */ + int ctx_id; /* user id: context ID or other user identifier */ +}; + +struct wcrypto_paras { + /* + * 0--encipher/compress .etc, 1 ---decipher/decomp .etc + * it not been used for HiSilicon SEC currently. + */ + __u8 direction; + __u8 is_poll; + + /* to be extended */ +}; + +enum wd_buff_type { + WD_FLAT_BUF, + WD_SGL_BUF, +}; + +/* memory APIs for Algorithm Layer */ +typedef void *(*wd_alloc)(void *usr, size_t size); +typedef void (*wd_free)(void *usr, void *va); + + /* memory VA to DMA address map */ +typedef void *(*wd_map)(void *usr, void *va, size_t sz); +typedef void (*wd_unmap)(void *usr, void *va, void *dma, size_t sz); +typedef __u32 (*wd_bufsize)(void *usr); + +/* Memory from user, it is given at ctx creating. */ +struct wd_mm_br { + wd_alloc alloc; /* Memory allocation */ + wd_free free; /* Memory free */ + wd_map iova_map; /* get iova from user space VA */ + + /* destroy the mapping between the PA of VA and iova */ + wd_unmap iova_unmap; + void *usr; /* data for the above operations */ + wd_bufsize get_bufsize; /* optional */ +}; + +/* Warpdrive data buffer */ +struct wd_dtb { + char *data; /* data/buffer start address */ + __u32 dsize; /* data size */ + __u32 bsize; /* buffer size */ +}; + +enum wcrypto_type { + WCRYPTO_RSA, + WCRYPTO_DH, + WCRYPTO_CIPHER, + WCRYPTO_DIGEST, + WCRYPTO_COMP, + WCRYPTO_EC, + WCRYPTO_RNG, + WCRYPTO_ECDH, + WCRYPTO_X25519, + WCRYPTO_X448, + WCRYPTO_ECDSA, + WCRYPTO_SM2, + WCRYPTO_AEAD, + WCRYPTO_MAX_ALG +}; + +#ifndef true +#define true 1 +#endif + +#ifndef false +#define false 0 +#endif + +#ifndef WD_ERR +#ifndef WITH_LOG_FILE +extern wd_log log_out; + +#define __WD_FILENAME__ (strrchr(__FILE__, '/') ? \ + ((char *)((uintptr_t)strrchr(__FILE__, '/') + 1)) : __FILE__) + +#define WD_ERR(format, args...) \ + (log_out ? log_out("[%s, %d, %s]:"format, \ + __WD_FILENAME__, __LINE__, __func__, ##args) : \ + fprintf(stderr, format, ##args)) +#else +extern FILE *flog_fd; +#define WD_ERR(format, args...) do { \ + if (!flog_fd) \ + flog_fd = fopen(WITH_LOG_FILE, "a+"); \ + if (flog_fd) \ + fprintf(flog_fd, format, ##args); \ + else \ + fprintf(stderr, "log %s not exists!", \ + WITH_LOG_FILE); \ +} while (0) +#endif +#endif + +#define WD_CAPA_PRIV_DATA_SIZE 64 + +/* Capabilities */ +struct wd_capa { + /* Algorithm name */ + const char *alg; + /* throughput capability */ + int throughput; + /* latency capability */ + int latency; + /* other capabilities */ + __u32 flags; + + /* For algorithm parameters, now it is defined in extending notions */ + struct wcrypto_paras priv; +}; + +struct wd_queue { + struct wd_capa capa; + /* if denote dev name, get its Q */ + char dev_path[PATH_STR_SIZE]; + /* if denote dev node mask, get its Q */ + unsigned int node_mask; + /* queue private */ + void *qinfo; +}; + +int wd_request_queue(struct wd_queue *q); +void wd_release_queue(struct wd_queue *q); +int wd_send(struct wd_queue *q, void *req); +int wd_recv(struct wd_queue *q, void **resp); +int wd_wait(struct wd_queue *q, __u16 ms); +int wd_recv_sync(struct wd_queue *q, void **resp, __u16 ms); +void *wd_reserve_memory(struct wd_queue *q, size_t size); +int wd_share_reserved_memory(struct wd_queue *q, + struct wd_queue *target_q); +int wd_get_available_dev_num(const char *algorithm); +int wd_get_node_id(struct wd_queue *q); +void *wd_iova_map(struct wd_queue *q, void *va, size_t sz); +void wd_iova_unmap(struct wd_queue *q, void *va, void *dma, size_t sz); +void *wd_dma_to_va(struct wd_queue *q, void *dma); +int wd_register_log(wd_log log); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/uadk/v1/wd_adapter.h b/src/uadk/v1/wd_adapter.h new file mode 100644 index 0000000..bcceff2 --- /dev/null +++ b/src/uadk/v1/wd_adapter.h @@ -0,0 +1,75 @@ +/* + * Copyright 2018-2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* the common driver header define the unified interface for wd */ +#ifndef __WD_ADAPTER_H__ +#define __WD_ADAPTER_H__ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/mman.h> + +#include "wd.h" +#include "wd_sgl.h" + +/* Use to describe hardware SGL, different hardware has different SGL format */ +struct hw_sgl_info { + __u32 sgl_sz; + __u32 sgl_align_sz; + __u32 sge_sz; + __u32 sge_align_sz; +}; + +struct wd_drv_dio_if { + /* vendor tag which is used to select right vendor driver */ + char *hw_type; + /* user space WD queue initialize */ + int (*open)(struct wd_queue *q); + /* user space WD queue uninitialize */ + void (*close)(struct wd_queue *q); + /* Send WCRYPTO message to WD queue */ + int (*send)(struct wd_queue *q, void **req, __u32 num); + /* Receive WCRYPTO message from WD queue */ + int (*recv)(struct wd_queue *q, void **req, __u32 num); + + /* Get hardware sgl information from WD queue */ + int (*get_sgl_info)(struct wd_queue *q, struct hw_sgl_info *info); + /* Initialize hardware sgl from WD queue */ + int (*init_sgl)(struct wd_queue *q, void *pool, struct wd_sgl *sgl); + /* Uninitialize hardware sgl from WD queue */ + int (*uninit_sgl)(struct wd_queue *q, void *pool, struct wd_sgl *sgl); + /* Merge two hardware sgls to 'dst_sgl' from WD queue */ + int (*sgl_merge)(struct wd_queue *q, void *pool, + struct wd_sgl *dst_sgl, struct wd_sgl *src_sgl); +}; + +int drv_open(struct wd_queue *q); +void drv_close(struct wd_queue *q); +int drv_send(struct wd_queue *q, void **req, __u32 num); +int drv_recv(struct wd_queue *q, void **req, __u32 num); +void drv_flush(struct wd_queue *q); +void drv_free_slice(struct wd_queue *q); +void *drv_reserve_mem(struct wd_queue *q, size_t size); +void drv_unmap_reserve_mem(struct wd_queue *q, void *addr, size_t size); +int drv_get_sgl_info(struct wd_queue *q, struct hw_sgl_info *info); +int drv_init_sgl(struct wd_queue *q, void *pool, struct wd_sgl *sgl); +int drv_uninit_sgl(struct wd_queue *q, void *pool, struct wd_sgl *sgl); +int drv_sgl_merge(struct wd_queue *q, void *pool, + struct wd_sgl *dst_sgl, struct wd_sgl *src_sgl); + +#endif diff --git a/src/uadk/v1/wd_aead.h b/src/uadk/v1/wd_aead.h new file mode 100644 index 0000000..ae5697b --- /dev/null +++ b/src/uadk/v1/wd_aead.h @@ -0,0 +1,213 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WD_AEAD_H +#define __WD_AEAD_H + +#include <stdlib.h> +#include <errno.h> +#include <linux/types.h> +#include "wd.h" +#include "wd_cipher.h" +#include "wd_digest.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum wcrypto_aead_op_type { + WCRYPTO_CIPHER_ENCRYPTION_DIGEST, + WCRYPTO_CIPHER_DECRYPTION_DIGEST, + WCRYPTO_DIGEST_CIPHER_ENCRYPTION, + WCRYPTO_DIGEST_CIPHER_DECRYPTION, +}; + +enum wcrypto_aead_mac_len { + WCRYPTO_CCM_GCM_LEN = 16, + WCRYPTO_SM3_LEN = 32, + WCRYPTO_MD5_LEN = 16, + WCRYPTO_SHA1_LEN = 20, + WCRYPTO_SHA256_LEN = 32, + WCRYPTO_SHA224_LEN = 28, + WCRYPTO_SHA384_LEN = 48, + WCRYPTO_SHA512_LEN = 64, + WCRYPTO_SHA512_224_LEN = 28, + WCRYPTO_SHA512_256_LEN = 32 +}; + +/** + * different contexts for different users/threads + * @cb: call back functions of user + * @calg: cipher algorithm type; denoted by enum wcrypto_cipher_alg + * @cmode: cipher algorithm mode; denoted by enum wcrypto_cipher_mode + * @dalg: digest algorithm type; denoted by enum wcrypto_digest_alg + * @dmode: digest algorithm mode; denoted by enum wcrypto_digest_mode + * @br: memory from user, it is given at ctx creating + * @data_fmt: denoted by enum wcrypto_buff_type + */ +struct wcrypto_aead_ctx_setup { + wcrypto_cb cb; + enum wcrypto_cipher_alg calg; + enum wcrypto_cipher_mode cmode; + enum wcrypto_digest_alg dalg; + enum wcrypto_digest_mode dmode; + struct wd_mm_br br; + __u16 data_fmt; +}; + +/** + * operational data per I/O operation + * AEAD encryption input: assoc data || plaintext + * AEAD encryption output: assoc data || ciphertext || auth tag + * AEAD decryption input: assoc data || ciphertext || auth tag + * AEAD decryption output: assoc data || plaintext + * @op_type:aead operation type, denoted by enum wcrypto_aead_op_type + * @status:I/O operation return status + * @in: input data address + * @out:output data address + * @iv:initialization verctor data address + * @in_bytes: input data size + * @out_bytes:output data size + * @out_buf_bytes:output buffer size + * @iv_bytes:initialization verctor data size + * @assoc_size: aead associated data size + * @priv:reserved data field segment + */ +struct wcrypto_aead_op_data { + enum wcrypto_aead_op_type op_type; + int status; + void *in; + void *out; + void *iv; + __u32 in_bytes; + __u32 out_bytes; + __u32 out_buf_bytes; + __u16 iv_bytes; + __u16 assoc_size; + void *priv; +}; + +/* AEAD message format of Warpdrive */ +struct wcrypto_aead_msg { + __u8 alg_type:4; /* Denoted by enum wcrypto_type */ + __u8 op_type:4; /* Denoted by enum wcrypto_aead_op_type */ + __u8 calg:4; /* Denoted by enum wcrypto_cipher_type */ + __u8 cmode:4; /* Denoted by enum wcrypto_cipher_mode */ + __u8 dalg:4; /* Denoted by enum wcrypto_digest_type */ + __u8 dmode:4; /* Denoted by enum wcrypto_digest_mode */ + __u8 data_fmt; /* Data format, denoted by enum wcrypto_buff_type */ + __u8 result; /* Operation result, denoted by WD error code */ + + __u16 ckey_bytes; /* Key bytes */ + __u16 akey_bytes; /* Key bytes */ + __u16 assoc_bytes; /* Input associated data bytes */ + __u16 auth_bytes; /* Output authentication bytes */ + __u16 iv_bytes; /* IV bytes */ + __u32 in_bytes; /* Input data bytes */ + __u32 out_bytes; /* Output data bytes */ + + __u8 *ckey; /* Input key VA pointer, should be DMA buffer */ + __u8 *akey; /* Input authenticate key VA pointer, should be DMA buffer */ + __u8 *iv; /* Input IV VA pointer, should be DMA buffer */ + __u8 *aiv; /* Input auth IV VA pointer, should be DMA buffer */ + __u8 *in; /* Input data VA pointer, should be DMA buffer */ + __u8 *out; /* Output data VA pointer, should be DMA buffer */ + __u64 usr_data; /* user identifier: struct wcrypto_cb_tag */ +}; + +/** + * wcrypto_create_aead_ctx() - create a aead context on the wrapdrive queue. + * @q: wrapdrive queue, need requested by user. + * @setup:setup data of user + */ +void *wcrypto_create_aead_ctx(struct wd_queue *q, + struct wcrypto_aead_ctx_setup *setup); + +/** + * wcrypto_set_aead_ckey() - set cipher key to aead context. + * @ctx: aead context, created by wcrypto_create_aead_ctx. + * @key: cipher key addr + * @key_len: cipher key length + */ +int wcrypto_set_aead_ckey(void *ctx, __u8 *key, __u16 key_len); + +/** + * wcrypto_set_aead_akey() - set authenticate key to aead context. + * @ctx: aead context, created by wcrypto_create_aead_ctx. + * @key: authenticate key addr + * @key_len: authenticate key length + */ +int wcrypto_set_aead_akey(void *ctx, __u8 *key, __u16 key_len); + +/** + * wcrypto_aead_setauthsize() - set aead authsize to aead context. + * @ctx: aead context, created by wcrypto_create_aead_ctx. + * @authsize: aead authsize + */ +int wcrypto_aead_setauthsize(void *ctx, __u16 authsize); + +/** + * wcrypto_aead_getauthsize() - obtain maximum authentication data size + * @ctx: aead context, created by wcrypto_create_aead_ctx. + * Return: authentication data size / tag size in bytes + */ +int wcrypto_aead_getauthsize(void *ctx); + +/** + * wcrypto_aead_getmaxauthsize() - obtain maximum authentication data size + * @ctx: aead context, created by wcrypto_create_aead_ctx. + * Return: max authentication data size + */ +int wcrypto_aead_get_maxauthsize(void *ctx); + +/** + * wcrypto_do_aead() - syn/asynchronous aead operation + * @ctx: context of user, created by wcrypto_create_aead_ctx. + * @opdata: operational data + * @tag: asynchronous:user_tag; synchronous:NULL. + */ +int wcrypto_do_aead(void *ctx, struct wcrypto_aead_op_data *opdata, + void *tag); + +/** + * wcrypto_burst_aead() - (a)synchronous multiple aead operations + * @a_ctx: context of user, created by wcrypto_create_aead_ctx. + * @opdata: operational data + * @tag: asynchronous:user_tag; synchronous:NULL. + * @num: operations number per calling, maximum number is WCRYPTO_MAX_BURST_NUM. + */ +int wcrypto_burst_aead(void *a_ctx, struct wcrypto_aead_op_data **opdata, + void **tag, __u32 num); + +/** + * wcrypto_aead_poll() - poll operation for asynchronous operation + * @q:wrapdrive queue + * @num:how many respondences this poll has to get, 0 means get all finishings + */ +int wcrypto_aead_poll(struct wd_queue *q, unsigned int num); + +/** + * wcrypto_del_aead_ctx() - free aead context + * @ctx: the context to be free + */ +void wcrypto_del_aead_ctx(void *ctx); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/uadk/v1/wd_bmm.h b/src/uadk/v1/wd_bmm.h new file mode 100644 index 0000000..88a8f9d --- /dev/null +++ b/src/uadk/v1/wd_bmm.h @@ -0,0 +1,50 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _WD_BMM_H +#define _WD_BMM_H + +#include <asm/types.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Memory pool creating parameters */ +struct wd_blkpool_setup { + __u32 block_size; /* Block buffer size */ + __u32 block_num; /* Block buffer number */ + __u32 align_size; /* Block buffer starting address align size */ + struct wd_mm_br br; /* memory from user if don't use WD memory */ +}; + +void *wd_blkpool_create(struct wd_queue *q, + struct wd_blkpool_setup *setup); +void wd_blkpool_destroy(void *pool); +void *wd_alloc_blk(void *pool); +void wd_free_blk(void *pool, void *blk); +int wd_get_free_blk_num(void *pool, __u32 *free_num); +int wd_blk_alloc_failures(void *pool, __u32 *fail_num); +void *wd_blk_iova_map(void *pool, void *blk); +void wd_blk_iova_unmap(void *pool, void *blk_dma, void *blk); +__u32 wd_blksize(void *pool); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/uadk/v1/wd_cipher.h b/src/uadk/v1/wd_cipher.h new file mode 100644 index 0000000..591a590 --- /dev/null +++ b/src/uadk/v1/wd_cipher.h @@ -0,0 +1,166 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WD_CIPHER_H +#define __WD_CIPHER_H + +#include <stdlib.h> +#include <errno.h> +#include <linux/types.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum wcrypto_cipher_op_type { + WCRYPTO_CIPHER_ENCRYPTION, + WCRYPTO_CIPHER_DECRYPTION, +}; + +enum wcrypto_cipher_alg { + WCRYPTO_CIPHER_SM4, + WCRYPTO_CIPHER_AES, + WCRYPTO_CIPHER_DES, + WCRYPTO_CIPHER_3DES, +}; + +enum wcrypto_cipher_mode { + WCRYPTO_CIPHER_ECB, + WCRYPTO_CIPHER_CBC, + WCRYPTO_CIPHER_CTR, + WCRYPTO_CIPHER_XTS, + WCRYPTO_CIPHER_OFB, + WCRYPTO_CIPHER_CFB, + WCRYPTO_CIPHER_CCM, + WCRYPTO_CIPHER_GCM, +}; + +/** + * different contexts for different users/threads + * @cb: call back functions of user + * @alg: cipher algorithm type; denoted by enum wcrypto_cipher_alg + * @mode:cipher algorithm mode; denoted by enum wcrypto_cipher_mode + * @br: memory from user, it is given at ctx creating + * @data_fmt: data format, denoted by enum wcrypto_buff_type + */ +struct wcrypto_cipher_ctx_setup { + wcrypto_cb cb; + enum wcrypto_cipher_alg alg; + enum wcrypto_cipher_mode mode; + struct wd_mm_br br; + __u16 data_fmt; +}; + +/** + * operational data per I/O operation + * @op_type:cipher operation type, denoted by enum wcrypto_cipher_op_type + * @status:I/O operation return status + * @in: input data address + * @out:output data address + * @iv:initialization verctor data address + * @in_bytes: input data size + * @out_bytes:output data size + * @iv_bytes:initialization verctor data size + * @priv:reserved data field segment + */ +struct wcrypto_cipher_op_data { + enum wcrypto_cipher_op_type op_type; + int status; + void *in; + void *out; + void *iv; + __u32 in_bytes; + __u32 out_bytes; + __u32 iv_bytes; + void *priv; +}; + +/* Cipher message format of Warpdrive */ +struct wcrypto_cipher_msg { + __u8 alg_type:4; /* Denoted by enum wcrypto_type */ + __u8 alg:4; /* Denoted by enum wcrypto_cipher_alg */ + __u8 op_type:4; /* Denoted by enum wcrypto_cipher_op_type */ + __u8 mode:4; /* Denoted by enum wcrypto_cipher_mode */ + __u8 data_fmt; /* Data format, denoted by enum wcrypto_buff_type */ + __u8 result; /* Operation result, denoted by WD error code */ + + __u16 key_bytes; /* Key bytes */ + __u16 iv_bytes; /* IV bytes */ + __u32 in_bytes; /* Input data bytes */ + __u32 out_bytes; /* Output data bytes */ + + __u8 *key; /* Input key VA pointer, should be DMA buffer */ + __u8 *iv; /* Input IV VA pointer, should be DMA buffer */ + __u8 *in; /* Input data VA pointer, should be DMA buffer */ + __u8 *out; /* Output data VA pointer, should be DMA buffer */ + __u64 usr_data; /* user identifier: struct wcrypto_cb_tag */ +}; + +/** + * wcrypto_create_cipher_ctx() - create a cipher context on the wrapdrive queue. + * @q: wrapdrive queue, need requested by user. + * @setup:setup data of user + */ +void *wcrypto_create_cipher_ctx(struct wd_queue *q, + struct wcrypto_cipher_ctx_setup *setup); + +/** + * wcrypto_set_cipher_key() - set cipher key to cipher context. + * @ctx: cipher context, created by wcrypto_create_cipher_ctx. + * @key: cipher key addr + * @key_len: cipher key length + */ +int wcrypto_set_cipher_key(void *ctx, __u8 *key, __u16 key_len); + +/** + * wcrypto_do_cipher() - syn/asynchronous cipher operation + * @ctx: context of user, created by wcrypto_create_cipher_ctx. + * @opdata: operational data + * @tag: asynchronous:user_tag; synchronous:NULL. + */ +int wcrypto_do_cipher(void *ctx, struct wcrypto_cipher_op_data *opdata, + void *tag); + +/** + * wcrypto_cipher_poll() - poll operation for asynchronous operation + * @q:wrapdrive queue + * @num:how many respondences this poll has to get, 0 means get all finishings + */ +int wcrypto_cipher_poll(struct wd_queue *q, unsigned int num); + +/** + * wcrypto_del_cipher_ctx() - free cipher context + * @ctx: the context to be free + */ +void wcrypto_del_cipher_ctx(void *ctx); + +/** + * wcrypto_burst_cipher() - (a)synchronous multiple cipher operations + * @ctx: context of user, created by wcrypto_create_cipher_ctx. + * @c_opdata: operational data + * @tag: asynchronous:user_tag; synchronous:NULL. + * @num: operations number per calling, maximum number is WCRYPTO_MAX_BURST_NUM. + */ +int wcrypto_burst_cipher(void *ctx, struct wcrypto_cipher_op_data **c_opdata, + void **tag, __u32 num); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/uadk/v1/wd_comp.h b/src/uadk/v1/wd_comp.h new file mode 100644 index 0000000..580b968 --- /dev/null +++ b/src/uadk/v1/wd_comp.h @@ -0,0 +1,256 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WCRYPTO_COMP_H +#define __WCRYPTO_COMP_H + +#include <stdlib.h> +#include <errno.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned char __u8; +typedef unsigned int __u32; +typedef unsigned long long __u64; + +#define ZIP_LOG(format, args...) fprintf(stderr, format, ##args) + +#define MAX_CTX_RSV_SIZE 65536 + +/* now hw not support config */ +enum wcrypto_comp_level { + WCRYPTO_COMP_L1 = 1, /* Compression level 1 */ + WCRYPTO_COMP_L2, /* Compression level 2 */ + WCRYPTO_COMP_L3, /* Compression level 3 */ + WCRYPTO_COMP_L4, /* Compression level 4 */ + WCRYPTO_COMP_L5, /* Compression level 5 */ + WCRYPTO_COMP_L6, /* Compression level 6 */ + WCRYPTO_COMP_L7, /* Compression level 7 */ + WCRYPTO_COMP_L8, /* Compression level 8 */ + WCRYPTO_COMP_L9, /* Compression level 9 */ +}; + +/* now hw not support config */ +enum wcrypto_comp_win_type { + WCRYPTO_COMP_WS_4K, /* 4k bytes window size */ + WCRYPTO_COMP_WS_8K, /* 8k bytes window size */ + WCRYPTO_COMP_WS_16K, /* 16k bytes window size */ + WCRYPTO_COMP_WS_24K, /* 24k bytes window size */ + WCRYPTO_COMP_WS_32K, /* 32k bytes window size */ +}; + +/* Flush types */ +enum wcrypto_comp_flush_type { + WCRYPTO_INVALID_FLUSH, + + /* output as much data as we can to improve performance */ + WCRYPTO_NO_FLUSH, + + /* output as bytes aligning or some other conditions satisfied */ + WCRYPTO_SYNC_FLUSH, + + /* indicates the end of the file/data */ + WCRYPTO_FINISH, +}; + +enum wcrypto_comp_alg_type { + WCRYPTO_ZLIB, + WCRYPTO_GZIP, + WCRYPTO_RAW_DEFLATE, + WCRYPTO_LZ77_ZSTD, + WCRYPTO_COMP_MAX_ALG, +}; + +/* Operational types for COMP */ +enum wcrypto_comp_optype { + WCRYPTO_DEFLATE, + WCRYPTO_INFLATE, +}; + +enum wcrypto_op_result { + WCRYPTO_STATUS_NULL, + WCRYPTO_COMP_END, + WCRYPTO_DECOMP_END, + WCRYPTO_DECOMP_END_NOSPACE, + WCRYPTO_DECOMP_NO_CRC, + WCRYPTO_DECOMP_BLK_NOSTART, + WCRYPTO_SRC_DIF_ERR, + WCRYPTO_DST_DIF_ERR, + WCRYPTO_NEGTIVE_COMP_ERR, +}; + +enum wcrypto_comp_state { + WCRYPTO_COMP_STATELESS, + WCRYPTO_COMP_STATEFUL, +}; + +enum wcrypto_stream_status { + WCRYPTO_COMP_STREAM_OLD, + WCRYPTO_COMP_STREAM_NEW, /* indicates first packet */ +}; + +/** + * different contexts for different users/threads + * @cb: call back functions of user + * @alg_type:compressing algorithm type zlib/gzip + * @op_type:operational types deflate/inflate + * @stream_mode:stateless(block)/statefull + * @comp_lv: compressing level;now reserved + * @win_size: window size of algorithm; now reserved + * @data_fmt: buffer format + * @br: memory operations from user + */ +struct wcrypto_comp_ctx_setup { + wcrypto_cb cb; + __u8 alg_type; + __u8 op_type; + __u8 stream_mode; + __u8 comp_lv; + __u16 win_size; + __u16 data_fmt; + struct wd_mm_br br; +}; + +/** + * operational out data when use zstd_lz77 in sgl format + * @literal:literals address when use zstd in sgl format + * @lit_len:avail literals size for hw when use zstd in sgl format, + * and avail literals is always <= (src_len + ZSTD_LIT_RSV_SIZE); + * @sequence:sequence address when use zstd in sgl format + * @seq_len:avail literals size for hw when use zstd in sgl format + */ +struct wcrypto_zstd_out { + void *literal; + __u32 lit_sz; + void *sequence; + __u32 seq_sz; +}; + +/** + * operational data per I/O operation + * @alg_type:compressing algorithm type zlib/gzip + * @flush:input and output, denotes flush type or data status + * @stream_pos: denotes stream start + * @status:task status current time + * @in:input data address + * @out:output data address + * @in_len:input data size + * @avail_out:avail output size for hw + * @consumed:output, denotes how many bytes are consumed this time + * @produced:output, denotes how many bytes are produced this time + * @isize:gzip isize + * @checksum: protocol checksum + * @priv: private field for extend + */ +struct wcrypto_comp_op_data { + __u8 alg_type; + __u8 flush; + __u8 stream_pos; + __u8 status; + __u8 *in; + __u8 *out; + __u32 in_len; + __u32 avail_out; + __u32 consumed; + __u32 produced; + __u32 isize; + __u32 checksum; + void *priv; +}; + +struct wcrypto_comp_msg { + __u8 alg_type; /* Denoted by enum wcrypto_comp_alg_type */ + __u8 op_type; /* Denoted by enum wcrypto_comp_op_type */ + __u8 flush_type; /* Denoted by enum wcrypto_comp_flush_type */ + __u8 stream_mode;/* Denoted by enum wcrypto_comp_state */ + __u8 stream_pos; /* Denoted by enum wcrypto_stream_status */ + __u8 comp_lv; /* Denoted by enum wcrypto_comp_level */ + __u8 data_fmt; /* Data format, denoted by enum wd_buff_type */ + __u8 win_size; /* Denoted by enum wcrypto_comp_win_type */ + __u32 in_size; /* Input data bytes */ + __u32 avail_out; /* Output buffer size */ + __u32 in_cons; /* consumed bytes of input data */ + __u32 produced; /* produced bytes of current operation */ + __u8 *src; /* Input data VA, buf should be DMA-able. */ + __u8 *dst; /* Output data VA pointer */ + __u32 tag; /* User-defined request identifier */ + __u32 status; /* Denoted by error code and enum wcrypto_op_result */ + __u32 isize; /* Denoted by gzip isize */ + __u32 checksum; /* Denoted by zlib/gzip CRC */ + void *ctx_buf; /* Denoted HW ctx cache, for stream mode */ + __u64 udata; /* Input user tag, identify data of stream/user */ +}; + +/** + * The output format defined by uadk and drivers should fill the format + * @literals_start:address of the literals data output by the hardware + * @sequences_start:address of the sequences data output by the hardware + * @lit_num:the size of literals + * @seq_num:the size of sequences + * @lit_length_overflow_cnt:the count of the literal length overflow + * @lit_length_overflow_pos:the position of the literal length overflow + * @freq:address of the frequency about sequences members + * @blk_type:the previous block status, 0 means an uncompressed block, + * 1 means a RLE block and 2 means a compressed block. + */ +struct wcrypto_lz77_zstd_format { + void *literals_start; + void *sequences_start; + __u32 lit_num; + __u32 seq_num; + __u32 lit_length_overflow_cnt; + __u32 lit_length_overflow_pos; + void *freq; + __u32 blk_type; +}; + +/** + * wcrypto_create_comp_ctx() - create a compress context on the wrapdrive queue. + * @q: wrapdrive queue, need requested by user. + * @setup:setup data of user + */ +void *wcrypto_create_comp_ctx(struct wd_queue *q, + struct wcrypto_comp_ctx_setup *setup); + +/** + * wcrypto_do_comp() - syn/asynchronous compressing/decompressing operation + * @ctx: context of user + * @opdata: operational data + * @tag: asynchronous:user_tag; synchronous:NULL. + */ +int wcrypto_do_comp(void *ctx, struct wcrypto_comp_op_data *opdata, void *tag); + +/** + * wcrypto_comp_poll() - poll operation for asynchronous operation + * @q:wrapdrive queue + * @num:how many respondences this poll has to get, 0 means get all finishings + */ +int wcrypto_comp_poll(struct wd_queue *q, unsigned int num); + +/** + * wcrypto_del_comp_ctx() - free compress context + * @ctx: the context to be free + */ +void wcrypto_del_comp_ctx(void *ctx); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/uadk/v1/wd_dh.h b/src/uadk/v1/wd_dh.h new file mode 100644 index 0000000..e411830 --- /dev/null +++ b/src/uadk/v1/wd_dh.h @@ -0,0 +1,91 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WD_DH_H +#define __WD_DH_H + +#include <stdlib.h> +#include <errno.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum wcrypto_dh_op_type { + WCRYPTO_DH_INVALID, /* invalid DH operation */ + WCRYPTO_DH_PHASE1, /* Phase1 DH key generate */ + WCRYPTO_DH_PHASE2 /* Phase2 DH key compute */ +}; + +struct wcrypto_dh_ctx_setup { + wcrypto_cb cb; /* call back function from user */ + __u16 data_fmt; /* data format denoted by enum wd_buff_type */ + __u16 key_bits; /* DH key bites */ + bool is_g2; /* is g2 mode or not */ + struct wd_mm_br br; /* memory operations from user */ +}; + +struct wcrypto_dh_op_data { + void *x_p; /* x and p, should be DMA buffer */ + + /* it is g, but it is PV at phase 2, should be DMA buffer */ + void *pv; + + /* phase 1&&2 output, should be DMA buffer */ + void *pri; + __u16 pri_bytes; /* output bytes */ + + __u16 pbytes; /* p bytes */ + __u16 xbytes; /* x bytes */ + __u16 pvbytes; /* pv bytes */ + enum wcrypto_dh_op_type op_type; /* operational type */ + __u32 status; /* output status */ +}; + +struct wcrypto_dh_msg { + __u8 alg_type:3; /* Denoted by enum wcrypto_type */ + __u8 op_type:2; /* Denoted by enum wcrypto_dh_op_type */ + __u8 data_fmt:1; /* Data format, denoted by enum wd_buff_type */ + __u8 is_g2:2; /* g2 mode of phase 1 */ + __u8 result; /* Data format, denoted by WD error code */ + __u16 key_bytes; /* Key size */ + __u8 *x_p; /* This is Xa and p data in order. Should be DMA buffer */ + __u8 *g; /* This is PV also at phase 2. Should be DMA buffer */ + __u8 *out; /* Result address, should be DMA buffer */ + __u16 xbytes; /* parameter Xa size */ + __u16 pbytes; /* parameter p size */ + __u16 gbytes; /* parameter g size */ + __u16 out_bytes; /* output parameter size */ + __u64 usr_data; /* user identifier: struct wcrypto_cb_tag */ +}; + +void *wcrypto_create_dh_ctx(struct wd_queue *q, struct wcrypto_dh_ctx_setup *setup); +bool wcrypto_dh_is_g2(const void *ctx); +int wcrypto_dh_key_bits(const void *ctx); + +/* Asynchronous/sync mode APIs of DH */ +int wcrypto_do_dh(void *ctx, struct wcrypto_dh_op_data *opdata, void *tag); +int wcrypto_dh_poll(struct wd_queue *q, unsigned int num); +void wcrypto_del_dh_ctx(void *ctx); +int wcrypto_set_dh_g(void *ctx, struct wd_dtb *g); +void wcrypto_get_dh_g(void *ctx, struct wd_dtb **g); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/uadk/v1/wd_digest.h b/src/uadk/v1/wd_digest.h new file mode 100644 index 0000000..6ad4c85 --- /dev/null +++ b/src/uadk/v1/wd_digest.h @@ -0,0 +1,168 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WD_DIGEST_H +#define __WD_DIGEST_H + +#include <stdlib.h> +#include <errno.h> +#include <linux/types.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum wcrypto_digest_alg { + WCRYPTO_SM3, + WCRYPTO_MD5, + WCRYPTO_SHA1, + WCRYPTO_SHA256, + WCRYPTO_SHA224, + WCRYPTO_SHA384, + WCRYPTO_SHA512, + WCRYPTO_SHA512_224, + WCRYPTO_SHA512_256, + WCRYPTO_MAX_DIGEST_TYPE, +}; + +enum wd_digest_mac_len { + WCRYPTO_DIGEST_SM3_LEN = 32, + WCRYPTO_DIGEST_MD5_LEN = 16, + WCRYPTO_DIGEST_SHA1_LEN = 20, + WCRYPTO_DIGEST_SHA256_LEN = 32, + WCRYPTO_DIGEST_SHA224_LEN = 28, + WCRYPTO_DIGEST_SHA384_LEN = 48, + WCRYPTO_DIGEST_SHA512_LEN = 64, + WCRYPTO_DIGEST_SHA512_224_LEN = 28, + WCRYPTO_DIGEST_SHA512_256_LEN = 32 +}; + +enum wcrypto_digest_mode { + WCRYPTO_DIGEST_NORMAL, + WCRYPTO_DIGEST_HMAC, +}; + +/** + * different contexts for different users/threads + * @cb: call back functions of user + * @alg: digest algorithm type; denoted by enum wcrypto_digest_alg + * @mode:digest algorithm mode; denoted by enum wcrypto_digest_mode + * @br: memory from user, it is given at ctx creating + * @data_fmt: data format, denoted by enum wcrypto_buff_type + */ +struct wcrypto_digest_ctx_setup { + wcrypto_cb cb; + enum wcrypto_digest_alg alg; + enum wcrypto_digest_mode mode; + struct wd_mm_br br; + __u16 data_fmt; +}; + +/** + * operational data per I/O operation + * @in: input data address + * @out:output data address + * @in_bytes: input data size + * @out_bytes:output data size + * @priv:reserved data field segment + * @status:I/O operation return status + * @has_next: is there next data block + */ +struct wcrypto_digest_op_data { + void *in; + void *out; + __u32 in_bytes; + __u32 out_bytes; + void *priv; + int status; + bool has_next; +}; + +/* Digest message format of Warpdrive */ +struct wcrypto_digest_msg { + __u8 alg_type; /* Denoted by enum wcrypto_type */ + __u8 alg:4; /* Denoted by enum wcrypto_digest_alg */ + __u8 has_next:1; /* is there next block data */ + __u8 mode:3; /* Denoted by enum wcrypto_digest_mode */ + __u8 data_fmt; /* Data format, denoted by enum wcrypto_buff_type */ + __u8 result; /* Operation result, denoted by WD error code */ + __u16 key_bytes; /* Key bytes */ + __u16 iv_bytes; /* IV bytes */ + + __u8 *key; /* Input key VA pointer, should be DMA buffer */ + __u8 *iv; /* Input IV VA pointer, should be DMA buffer */ + __u8 *in; /* Input data VA pointer, should be DMA buffer */ + __u8 *out; /* Output data VA pointer, should be DMA buffer */ + __u32 in_bytes; /* Input data bytes */ + __u32 out_bytes; /* Output data bytes */ + __u64 usr_data; /* user identifier: struct wcrypto_cb_tag */ +}; + +/** + * wcrypto_create_digest_ctx() - create a digest context on the wrapdrive queue. + * @q: wrapdrive queue, need requested by user. + * @setup:setup data of user + */ +void *wcrypto_create_digest_ctx(struct wd_queue *q, + struct wcrypto_digest_ctx_setup *setup); + +/** + * wcrypto_set_digest_key() - set auth key to digest context. + * @ctx: digest context, created by wcrypto_create_digest_ctx. + * @key: auth key addr + * @key_len: auth key length + */ +int wcrypto_set_digest_key(void *ctx, __u8 *key, __u16 key_len); + +/** + * wcrypto_do_digest() - syn/asynchronous digest operation + * @ctx: context of user, created by wcrypto_create_digest_ctx. + * @opdata: operational data + * @tag: asynchronous:user_tag; synchronous:NULL. + */ +int wcrypto_do_digest(void *ctx, struct wcrypto_digest_op_data *opdata, + void *tag); + +/** + * wcrypto_digest_poll() - poll operation for asynchronous operation + * @q:wrapdrive queue + * @num:how many respondences this poll has to get, 0 means get all finishings + */ +int wcrypto_digest_poll(struct wd_queue *q, unsigned int num); + +/** + * wcrypto_del_digest_ctx() - free digest context + * @ctx: the context to be free + */ +void wcrypto_del_digest_ctx(void *ctx); + +/** + * wcrypto_burst_digest() - (a)synchronous multiple digest operations + * @d_ctx: context of user, created by wcrypto_create_digest_ctx. + * @opdata: operational data + * @tag: asynchronous:user_tag; synchronous:NULL. + * @num: operations number per calling, maximum number is WCRYPTO_MAX_BURST_NUM. + */ +int wcrypto_burst_digest(void *d_ctx, struct wcrypto_digest_op_data **opdata, + void **tag, __u32 num); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/uadk/v1/wd_ecc.h b/src/uadk/v1/wd_ecc.h new file mode 100644 index 0000000..84998ed --- /dev/null +++ b/src/uadk/v1/wd_ecc.h @@ -0,0 +1,273 @@ +/* + * Copyright 2020 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __WD_ECC_H +#define __WD_ECC_H + +#include <stdlib.h> +#include <errno.h> +#include <linux/types.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef int (*wcrypto_rand)(char *out, size_t out_len, void *usr); +typedef int (*wcrypto_hash)(const char *in, size_t in_len, + char *out, size_t out_len, void *usr); + +struct wcrypto_ecc_in; /* ecc input parameters */ +struct wcrypto_ecc_key; /* ecc key parameters */ +struct wcrypto_ecc_out; /* ecc output parameters */ + +struct wcrypto_ecc_point { + struct wd_dtb x; /* x affine coordinates */ + struct wd_dtb y; /* y affine coordinates */ +}; + +/* ECC operational types */ +enum wcrypto_ecc_op_type { + WCRYPTO_EC_OP_INVALID, /* invalid ecc operation */ + WCRYPTO_ECXDH_GEN_KEY, /* ECDH/X448/X25519 generate public key */ + WCRYPTO_ECXDH_COMPUTE_KEY, /* ECDH/X448/X25519 compute share key */ + WCRYPTO_ECDSA_SIGN, /* ECDSA sign */ + WCRYPTO_ECDSA_VERIFY, /* ECDSA verify */ + WCRYPTO_SM2_SIGN, /* SM2 sign */ + WCRYPTO_SM2_VERIFY, /* SM2 verify */ + WCRYPTO_SM2_ENCRYPT, /* SM2 encrypt */ + WCRYPTO_SM2_DECRYPT, /* SM2 decrypt */ + WCRYPTO_SM2_KG, /* SM2 key generate */ + WCRYPTO_EC_OP_MAX /* max operation */ +}; + +/* ECC operational types */ +enum wcrypto_ecc_curve_id { + WCRYPTO_SECP128R1 = 0x10, /* SECG 128 bit prime field */ + WCRYPTO_SECP192K1 = 0x11, /* SECG 192 bit prime field */ + WCRYPTO_SECP256K1 = 0x12, /* SECG 256 bit prime field */ + WCRYPTO_BRAINPOOLP320R1 = 0x13, /* RFC5639 320 bit prime field */ + WCRYPTO_BRAINPOOLP384R1 = 0x14, /* RFC5639 384 bit prime field */ + WCRYPTO_SECP521R1 = 0x15, /* NIST/SECG 521 bit prime field */ +}; + +/* ECC hash callback function types */ +enum wcrypto_ecc_hash_type { + WCRYPTO_HASH_SM3, + WCRYPTO_HASH_SHA1, + WCRYPTO_HASH_SHA224, + WCRYPTO_HASH_SHA256, + WCRYPTO_HASH_SHA384, + WCRYPTO_HASH_SHA512, + WCRYPTO_HASH_MD4, + WCRYPTO_HASH_MD5, + WCRYPTO_HASH_MAX +}; + +struct wcrypto_ecc_curve { + struct wd_dtb p; /* Prime field p */ + struct wd_dtb a; /* Elliptic curve equation a parameter */ + struct wd_dtb b; /* Elliptic curve equation b parameter */ + struct wcrypto_ecc_point g; /* Elliptic curve G point */ + struct wd_dtb n; /* Elliptic curve order */ +}; + +enum wcrypto_ecc_curve_cfg_type { + WCRYPTO_CV_CFG_ID, /* set curve parameter by denote curve ID */ + WCRYPTO_CV_CFG_PARAM /* set curve parameter by denote curve parameter */ +}; + +struct wcrypto_ecc_curve_cfg { + __u32 type; /* denoted by enum wcrypto_ecc_curve_cfg_type */ + union { + enum wcrypto_ecc_curve_id id; /* if WCRYPTO_CV_CFG_ID */ + struct wcrypto_ecc_curve *pparam; /* if WCRYPTO_CV_CFG_PARAM */ + } cfg; + __u8 resv[4]; /* reserve */ +}; + +struct wcrypto_rand_mt { + wcrypto_rand cb; /* rand callback */ + void *usr; /* user private parameter */ +}; + +struct wcrypto_hash_mt { + wcrypto_hash cb; /* hash callback */ + void *usr; /* user private parameter */ + __u8 type; /* hash type, denoted by enum wcrypto_ecc_hash_type */ + __u8 rsv[3]; /* reserve */ +}; + +/* ECC context setting up input parameters from user */ +struct wcrypto_ecc_ctx_setup { + wcrypto_cb cb; /* call back function from user */ + __u16 data_fmt; /* data format denoted by enum wd_buff_type */ + __u16 key_bits; /* ECC key bits */ + struct wcrypto_ecc_curve_cfg cv; /* curve config denoted by user */ + struct wd_mm_br br; /* memory operations from user */ + struct wcrypto_rand_mt rand; /* rand method from user */ + struct wcrypto_hash_mt hash; /* hash method from user */ +}; + +struct wcrypto_ecc_op_data { + enum wcrypto_ecc_op_type op_type; /* ecc operation type */ + int status; /* ecc operation status */ + void *in; /* ecc operation input address, should be DMA-able */ + void *out; /* ecc operation output address, should be DMA-able */ + __u32 in_bytes; /* ecc operation input bytes */ + __u32 out_bytes; /* ecc operation output bytes */ +}; + +/* ECC message format of Warpdrive */ +struct wcrypto_ecc_msg { + __u8 alg_type:4; /* Denoted by enum wcrypto_type */ + __u8 op_type:4; /* Denoted by enum wcrypto_ecc_op_type */ + __u8 curve_id:7; /* Ec curve denoted by enum wcrypto_ecc_curve_type */ + __u8 data_fmt:1; /* Data format, denoted by enum wd_buff_type */ + __u8 mtype; /* not used, reserve */ + __u8 result; /* alg op error code */ + __u16 key_bytes; /* key bytes */ + __u16 in_bytes; /* Input data bytes */ + __u16 out_bytes; /* Output data bytes */ + __u8 hash_type; /* hash method denoted by enum wcrypto_ecc_hash_type */ + __u8 *in; /* Input data VA, should be DMA buffer */ + __u8 *out; /* Output data VA, should be DMA buffer */ + __u8 *key; /* Input key VA, should be DMA buffer */ + /* + * Input user tag, used for identify data stream/user: + * struct wcrypto_cb_tag + */ + __u64 usr_data; +}; + +int wcrypto_get_ecc_key_bits(const void *ctx); +void *wcrypto_create_ecc_ctx(struct wd_queue *q, + struct wcrypto_ecc_ctx_setup *setup); +void wcrypto_del_ecc_ctx(void *ctx); +struct wcrypto_ecc_key *wcrypto_get_ecc_key(void *ctx); +int wcrypto_set_ecc_prikey(struct wcrypto_ecc_key *ecc_key, + struct wd_dtb *prikey); +int wcrypto_get_ecc_prikey(struct wcrypto_ecc_key *ecc_key, + struct wd_dtb **prikey); +int wcrypto_set_ecc_pubkey(struct wcrypto_ecc_key *ecc_key, + struct wcrypto_ecc_point *pubkey); +int wcrypto_get_ecc_pubkey(struct wcrypto_ecc_key *ecc_key, + struct wcrypto_ecc_point **pubkey); +void wcrypto_del_ecc_in(void *ctx, struct wcrypto_ecc_in *in); +void wcrypto_del_ecc_out(void *ctx, struct wcrypto_ecc_out *out); +void wcrypto_get_ecc_prikey_params(struct wcrypto_ecc_key *key, + struct wd_dtb **p, struct wd_dtb **a, + struct wd_dtb **b, struct wd_dtb **n, + struct wcrypto_ecc_point **g, + struct wd_dtb **d); +void wcrypto_get_ecc_pubkey_params(struct wcrypto_ecc_key *key, + struct wd_dtb **p, struct wd_dtb **a, + struct wd_dtb **b, struct wd_dtb **n, + struct wcrypto_ecc_point **g, + struct wcrypto_ecc_point **pub); + +/* APIs For ECDH */ +void wcrypto_get_ecxdh_in_params(struct wcrypto_ecc_in *in, + struct wcrypto_ecc_point **pbk); +void wcrypto_get_ecxdh_out_params(struct wcrypto_ecc_out *out, + struct wcrypto_ecc_point **key); +struct wcrypto_ecc_in *wcrypto_new_ecxdh_in(void *ctx, + struct wcrypto_ecc_point *in); +struct wcrypto_ecc_out *wcrypto_new_ecxdh_out(void *ctx); +/** + * This is a pair of asynchronous mode ECDH/ECDSA/SM2 API as tag is not NULL, + * or it is synchronous mode + */ +int wcrypto_do_ecxdh(void *ctx, struct wcrypto_ecc_op_data *opdata, void *tag); +int wcrypto_ecxdh_poll(struct wd_queue *q, unsigned int num); + + +/* APIs For ECDSA sign/verf */ +struct wcrypto_ecc_in *wcrypto_new_ecdsa_sign_in(void *ctx, + struct wd_dtb *dgst, + struct wd_dtb *k); +struct wcrypto_ecc_in *wcrypto_new_ecdsa_verf_in(void *ctx, + struct wd_dtb *dgst, + struct wd_dtb *r, + struct wd_dtb *s); +struct wcrypto_ecc_out *wcrypto_new_ecdsa_sign_out(void *ctx); +void wcrypto_get_ecdsa_sign_in_params(struct wcrypto_ecc_in *in, + struct wd_dtb **dgst, + struct wd_dtb **k); +void wcrypto_get_ecdsa_verf_in_params(struct wcrypto_ecc_in *in, + struct wd_dtb **dgst, + struct wd_dtb **r, + struct wd_dtb **s); +void wcrypto_get_ecdsa_sign_out_params(struct wcrypto_ecc_out *out, + struct wd_dtb **r, + struct wd_dtb **s); + +/** + * This is a pair of asynchronous mode ECDSA API as tag is not NULL, + * or it is synchronous mode + */ +int wcrypto_do_ecdsa(void *ctx, struct wcrypto_ecc_op_data *opdata, void *tag); +int wcrypto_ecdsa_poll(struct wd_queue *q, unsigned int num); + +/* APIs For SM2 sign/verf/kg */ +struct wcrypto_ecc_in *wcrypto_new_sm2_sign_in(void *ctx, + struct wd_dtb *e, + struct wd_dtb *k, + struct wd_dtb *id, + __u8 is_dgst); +struct wcrypto_ecc_in *wcrypto_new_sm2_verf_in(void *ctx, + struct wd_dtb *e, + struct wd_dtb *r, + struct wd_dtb *s, + struct wd_dtb *id, + __u8 is_dgst); +struct wcrypto_ecc_out *wcrypto_new_sm2_sign_out(void *ctx); + +void wcrypto_get_sm2_sign_out_params(struct wcrypto_ecc_out *out, + struct wd_dtb **r, + struct wd_dtb **s); + +struct wcrypto_ecc_in *wcrypto_new_sm2_enc_in(void *ctx, + struct wd_dtb *k, + struct wd_dtb *plaintext); +struct wcrypto_ecc_in *wcrypto_new_sm2_dec_in(void *ctx, + struct wcrypto_ecc_point *c1, + struct wd_dtb *c2, + struct wd_dtb *c3); +struct wcrypto_ecc_out *wcrypto_new_sm2_enc_out(void *ctx, __u32 plaintext_len); +struct wcrypto_ecc_out *wcrypto_new_sm2_dec_out(void *ctx, __u32 plaintext_len); +struct wcrypto_ecc_out *wcrypto_new_sm2_kg_out(void *ctx); +void wcrypto_get_sm2_kg_out_params(struct wcrypto_ecc_out *out, + struct wd_dtb **privkey, + struct wcrypto_ecc_point **pubkey); +void wcrypto_get_sm2_enc_out_params(struct wcrypto_ecc_out *out, + struct wcrypto_ecc_point **c1, + struct wd_dtb **c2, + struct wd_dtb **c3); +void wcrypto_get_sm2_dec_out_params(struct wcrypto_ecc_out *out, + struct wd_dtb **plaintext); + + +/** + * This is a pair of asynchronous mode SM2 API as tag is not NULL, + * or it is synchronous mode + */ +int wcrypto_do_sm2(void *ctx, struct wcrypto_ecc_op_data *opdata, void *tag); +int wcrypto_sm2_poll(struct wd_queue *q, unsigned int num); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/uadk/v1/wd_rng.h b/src/uadk/v1/wd_rng.h new file mode 100644 index 0000000..fcde26d --- /dev/null +++ b/src/uadk/v1/wd_rng.h @@ -0,0 +1,76 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WD_RNG_H +#define __WD_RNG_H + +#include "wd.h" +#include "wd_digest.h" +#include "wd_cipher.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct wcrypto_rng_ctx_setup { + wcrypto_cb cb; + __u16 data_fmt; /* Data format, denoted by enum wd_buff_type */ + enum wcrypto_type type; /* Please refer to the definition of enum */ + enum wcrypto_cipher_alg calg; /* DRBG cipher algorithm */ + enum wcrypto_cipher_mode cmode; /* DRBG cipher mode */ + enum wcrypto_digest_alg dalg; /* DRBG digest algorithm */ + enum wcrypto_digest_mode dmode; /* DRBG digest mode */ +}; + +struct wcrypto_rng_msg { + __u8 alg_type; /* Denoted by enum wcrypto_type */ + __u8 op_type; /* Denoted by enum wcrypto_rng_op_type */ + __u8 data_fmt; /* Data format, denoted by enum wd_buff_type */ + __u8 result; /* Data format, denoted by WD error code */ + __u8 *out; /* Result address */ + __u8 *in; /* Input address */ + __u32 out_bytes; /* output bytes */ + __u32 in_bytes; /* input bytes */ + __u64 usr_tag; /* user identifier */ +}; + +enum wcrypto_rng_op_type { + WCRYPTO_RNG_INVALID, /* Invalid RNG operational type */ + WCRYPTO_DRBG_RESEED, /* seed operation */ + WCRYPTO_DRBG_GEN, /* deterministic random number generation */ + WCRYPTO_TRNG_GEN, /* true random number generation */ +}; + +struct wcrypto_rng_op_data { + enum wcrypto_rng_op_type op_type; + __u32 status; /* Operation result status */ + void *in; /* input */ + void *out; /* output */ + __u32 in_bytes; /* input bytes */ + __u32 out_bytes; /* output bytes */ +}; + +void *wcrypto_create_rng_ctx(struct wd_queue *q, + struct wcrypto_rng_ctx_setup *setup); +void wcrypto_del_rng_ctx(void *ctx); +int wcrypto_do_rng(void *ctx, struct wcrypto_rng_op_data *opdata, void *tag); +int wcrypto_rng_poll(struct wd_queue *q, unsigned int num); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/uadk/v1/wd_rsa.h b/src/uadk/v1/wd_rsa.h new file mode 100644 index 0000000..480c088 --- /dev/null +++ b/src/uadk/v1/wd_rsa.h @@ -0,0 +1,148 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __WD_RSA_H +#define __WD_RSA_H + +#include <stdlib.h> +#include <errno.h> +#include <linux/types.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct wcrypto_rsa_kg_in; /* rsa key generation input parameters */ +struct wcrypto_rsa_kg_out; /* rsa key generation output parameters */ +struct wcrypto_rsa_pubkey; /* rsa public key */ +struct wcrypto_rsa_prikey; /* rsa private key */ + +/* RSA operational types */ +enum wcrypto_rsa_op_type { + WCRYPTO_RSA_INVALID, /* invalid rsa operation */ + WCRYPTO_RSA_SIGN, /* RSA sign */ + WCRYPTO_RSA_VERIFY, /* RSA verify */ + WCRYPTO_RSA_GENKEY, /* RSA key generation */ +}; + +/* RSA key types */ +enum wcrypto_rsa_key_type { + WCRYPTO_RSA_INVALID_KEY, /* invalid rsa key type */ + WCRYPTO_RSA_PUBKEY, /* rsa public key type */ + WCRYPTO_RSA_PRIKEY1, /* invalid rsa private common key type */ + WCRYPTO_RSA_PRIKEY2, /* invalid rsa private CRT key type */ +}; + +/* RSA context setting up input parameters from user */ +struct wcrypto_rsa_ctx_setup { + wcrypto_cb cb; /* call back function from user */ + __u16 data_fmt; /* data format denoted by enum wd_buff_type */ + __u16 key_bits; /* RSA key bits */ + bool is_crt; /* CRT mode or not */ + struct wd_mm_br br; /* memory operations from user */ +}; + +struct wcrypto_rsa_op_data { + enum wcrypto_rsa_op_type op_type; /* rsa operation type */ + int status; /* rsa operation status */ + void *in; /* rsa operation input address, should be DMA-able */ + void *out; /* rsa operation output address, should be DMA-able */ + __u32 in_bytes; /* rsa operation input bytes */ + __u32 out_bytes; /* rsa operation output bytes */ +}; + +/* RSA message format of Warpdrive */ +struct wcrypto_rsa_msg { + __u8 alg_type:3; /* Denoted by enum wcrypto_type */ + __u8 op_type:2; /* Denoted by enum wcrypto_rsa_op_type */ + __u8 key_type:2; /* Denoted by enum wcrypto_rsa_key_type */ + __u8 data_fmt:1; /* Data format, denoted by enum wd_buff_type */ + __u8 result; /* Data format, denoted by WD error code */ + __u16 in_bytes; /* Input data bytes */ + __u16 out_bytes; /* Output data bytes */ + __u16 key_bytes; /* Input key bytes */ + __u8 *in; /* Input data VA, buf should be DMA buffer. */ + __u8 *out; /* Output data VA pointer, should be DMA buffer */ + __u8 *key; /* Input key VA pointer, should be DMA buffer */ + + /* + * Input user tag, used for identify data stream/user: + * struct wcrypto_cb_tag + */ + __u64 usr_data; +}; + +bool wcrypto_rsa_is_crt(const void *ctx); +int wcrypto_rsa_key_bits(const void *ctx); +void *wcrypto_create_rsa_ctx(struct wd_queue *q, struct wcrypto_rsa_ctx_setup *setup); +void wcrypto_get_rsa_pubkey(void *ctx, struct wcrypto_rsa_pubkey **pubkey); +void wcrypto_get_rsa_prikey(void *ctx, struct wcrypto_rsa_prikey **prikey); +int wcrypto_set_rsa_pubkey_params(void *ctx, struct wd_dtb *e, struct wd_dtb *n); +void wcrypto_get_rsa_pubkey_params(struct wcrypto_rsa_pubkey *pbk, + struct wd_dtb **e, struct wd_dtb **n); +int wcrypto_set_rsa_prikey_params(void *ctx, struct wd_dtb *d, struct wd_dtb *n); +void wcrypto_get_rsa_prikey_params(struct wcrypto_rsa_prikey *pvk, struct wd_dtb **d, + struct wd_dtb **n); +int wcrypto_set_rsa_crt_prikey_params(void *ctx, struct wd_dtb *dq, + struct wd_dtb *dp, + struct wd_dtb *qinv, + struct wd_dtb *q, + struct wd_dtb *p); +void wcrypto_get_rsa_crt_prikey_params(struct wcrypto_rsa_prikey *pvk, + struct wd_dtb **dq, struct wd_dtb **dp, + struct wd_dtb **qinv, struct wd_dtb **q, + struct wd_dtb **p); + +/* APIs For RSA key generate */ +struct wcrypto_rsa_kg_in *wcrypto_new_kg_in(void *ctx, struct wd_dtb *e, + struct wd_dtb *p, struct wd_dtb *q); +void wcrypto_del_kg_in(void *ctx, struct wcrypto_rsa_kg_in *ki); +void wcrypto_get_rsa_kg_in_params(struct wcrypto_rsa_kg_in *kin, struct wd_dtb *e, + struct wd_dtb *q, struct wd_dtb *p); + +struct wcrypto_rsa_kg_out *wcrypto_new_kg_out(void *ctx); +void wcrypto_del_kg_out(void *ctx, struct wcrypto_rsa_kg_out *kout); +void wcrypto_get_rsa_kg_out_params(struct wcrypto_rsa_kg_out *kout, + struct wd_dtb *d, + struct wd_dtb *n); +void wcrypto_get_rsa_kg_out_crt_params(struct wcrypto_rsa_kg_out *kout, + struct wd_dtb *qinv, + struct wd_dtb *dq, struct wd_dtb *dp); + +int wcrypto_rsa_kg_in_data(struct wcrypto_rsa_kg_in *ki, char **data); +int wcrypto_rsa_kg_out_data(struct wcrypto_rsa_kg_out *ko, char **data); +void wcrypto_set_rsa_kg_out_crt_psz(struct wcrypto_rsa_kg_out *kout, + size_t qinv_sz, + size_t dq_sz, + size_t dp_sz); +void wcrypto_set_rsa_kg_out_psz(struct wcrypto_rsa_kg_out *kout, + size_t d_sz, + size_t n_sz); + +/** + * This is a pair of asynchronous mode RSA API as tag is not NULL, + * or it is synchronous mode + */ +int wcrypto_do_rsa(void *ctx, struct wcrypto_rsa_op_data *opdata, void *tag); +int wcrypto_rsa_poll(struct wd_queue *q, unsigned int num); +void wcrypto_del_rsa_ctx(void *ctx); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/uadk/v1/wd_sgl.h b/src/uadk/v1/wd_sgl.h new file mode 100644 index 0000000..e2e82f2 --- /dev/null +++ b/src/uadk/v1/wd_sgl.h @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _WD_SGL_H +#define _WD_SGL_H + +#include <stddef.h> +#include <asm/types.h> +#include "wd.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct wd_sgl; +struct wd_sglpool_setup { + /* Total number of SGEs with buffer slices */ + __u32 buf_num; + /* memory size of entry buffer */ + __u32 buf_size; + /* Fixed SGE number in the SGL of the pool */ + __u8 sge_num_in_sgl; + /* Initiated buf number in the SGL of the pool, changeable */ + __u8 buf_num_in_sgl; + /* Total number of sgl for entries and buffers */ + __u16 sgl_num; + /* SGE data buffer starting address align size */ + __u32 align_size; + /* memory from user if don't use WD memory */ + struct wd_mm_br br; +}; + +void *wd_sglpool_create(struct wd_queue *q, struct wd_sglpool_setup *setup); +void wd_sglpool_destroy(void *pool); +struct wd_sgl *wd_alloc_sgl(void *pool, __u32 size); +void wd_free_sgl(void *pool, struct wd_sgl *sgl); +int wd_sgl_merge(struct wd_sgl *dst_sgl, struct wd_sgl *src_sgl); +int wd_sgl_cp_to_pbuf(struct wd_sgl *sgl, size_t offset, void *pbuf, size_t size); +int wd_sgl_cp_from_pbuf(struct wd_sgl *sgl, size_t offset, void *pbuf, size_t size); +void *wd_sgl_iova_map(void *pool, struct wd_sgl *sgl, size_t sz); +void wd_sgl_iova_unmap(void *pool, void *sgl_iova, struct wd_sgl *sgl); + +void *wd_get_last_sge_buf(struct wd_sgl *sgl); +void *wd_get_first_sge_buf(struct wd_sgl *sgl); +int wd_get_sgl_sge_num(struct wd_sgl *sgl); +int wd_get_sgl_buf_num(struct wd_sgl *sgl); +void *wd_get_sge_buf(struct wd_sgl *sgl, __u32 num); +int wd_get_sgl_buf_sum(struct wd_sgl *sgl); +int wd_get_sgl_mem_size(struct wd_sgl *sgl, size_t *size); +int wd_get_free_sgl_num(void *pool, __u32 *free_sgl_num); +int wd_get_free_sgl_sge_num(struct wd_sgl *sgl, __u32 *free_sgl_sge_num); +int wd_get_free_buf_num(void *pool, __u32 *free_buf_num); +int wd_get_sgl_datalen(struct wd_sgl *sgl, __u32 *dtsize); +int wd_get_sge_datalen(struct wd_sgl *sgl, __u32 num, __u32 *dtsize); +int wd_get_sgl_bufsize(struct wd_sgl *sgl, __u32 *bufsz); + +#ifdef __cplusplus +} +#endif + +#endif /* _WD_SGL_H */ diff --git a/src/uadk/v1/wd_util.h b/src/uadk/v1/wd_util.h new file mode 100644 index 0000000..228493c --- /dev/null +++ b/src/uadk/v1/wd_util.h @@ -0,0 +1,393 @@ +/* + * Copyright 2019 Huawei Technologies Co.,Ltd.All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* the common driver header define the unified interface for wd */ +#ifndef __WD_UTIL_H__ +#define __WD_UTIL_H__ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/epoll.h> +#include <sys/eventfd.h> +#include <sys/queue.h> +#include <linux/types.h> + +#include "v1/wd.h" +#include "v1/wd_ecc.h" +#include "v1/wd_adapter.h" + +#define WD_CTX_MSG_NUM 64 +#define WD_HPRE_CTX_MSG_NUM 64 +#define WD_RNG_CTX_MSG_NUM 256 +#define WD_MAX_CTX_NUM 256 +#define BYTE_BITS 8 +#define BYTE_BITS_SHIFT 3 +#define CRT_PARAMS_SZ(key_size) ((5 * (key_size)) >> 1) +#define CRT_GEN_PARAMS_SZ(key_size) ((7 * (key_size)) >> 1) +#define GEN_PARAMS_SZ(key_size) ((key_size) << 1) +#define CRT_PARAM_SZ(key_size) ((key_size) >> 1) +#define GET_NEGATIVE(val) (0 - (val)) +#define XTS_MODE_KEY_SHIFT 1 +#define AES_KEYSIZE_128 16 +#define AES_KEYSIZE_192 24 +#define AES_KEYSIZE_256 32 +#define CTX_ID_MAX_NUM 64 +#define CTX_ID_MAX_NUM_BYTES 512 + +/* ECC */ +#define ECDH_IN_PARAM_NUM 2 +#define ECDH_OUT_PARAM_NUM 2 +#define ECC_SIGN_IN_PARAM_NUM 2 +#define ECC_SIGN_OUT_PARAM_NUM 2 +#define ECC_VERF_IN_PARAM_NUM 3 +#define ECC_PRIKEY_PARAM_NUM 7 +#define ECDH_HW_KEY_PARAM_NUM 5 +#define ECC_PUBKEY_PARAM_NUM 8 +#define SM2_KG_OUT_PARAM_NUM 3 +#define ECC_POINT_PARAM_NUM 2 + +#define ECDH_HW_KEY_SZ(hsz) ((hsz) * ECDH_HW_KEY_PARAM_NUM) +#define ECC_PRIKEY_SZ(hsz) ((hsz) * ECC_PRIKEY_PARAM_NUM) +#define ECC_PUBKEY_SZ(hsz) ((hsz) * ECC_PUBKEY_PARAM_NUM) +#define ECDH_OUT_PARAMS_SZ(hsz) ((hsz) * ECDH_OUT_PARAM_NUM) + +/* x25519/x448 */ +#define X_DH_OUT_PARAM_NUM 1 +#define X_DH_HW_KEY_PARAM_NUM 3 + +#define X_DH_OUT_PARAMS_SZ(hsz) ((hsz) * X_DH_OUT_PARAM_NUM) +#define X_DH_HW_KEY_SZ(hsz) ((hsz) * X_DH_HW_KEY_PARAM_NUM) +#define SM2_KG_OUT_PARAMS_SZ(hsz) ((hsz) * SM2_KG_OUT_PARAM_NUM) +#define BITS_TO_BYTES(bits) (((bits) + 7) >> 3) +#define ECC_SIGN_IN_PARAMS_SZ(hsz) ((hsz) * ECC_SIGN_IN_PARAM_NUM) +#define ECC_SIGN_OUT_PARAMS_SZ(hsz) ((hsz) * ECC_SIGN_OUT_PARAM_NUM) +#define ECC_VERF_IN_PARAMS_SZ(hsz) ((hsz) * ECC_VERF_IN_PARAM_NUM) +#define ECC_VERF_OUT_PARAMS_SZ 1 + +/* Required compiler attributes */ +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) + +struct wd_lock { + __u8 lock; +}; + +struct wd_ss_region { + void *va; + unsigned long long pa; + size_t size; + + TAILQ_ENTRY(wd_ss_region) next; +}; + +TAILQ_HEAD(wd_ss_region_list, wd_ss_region); + +struct q_info { + const char *hw_type; + int hw_type_id; + int ref; + void *priv; /* private data used by the drive layer */ + const void *dev_info; + void *ss_va; + int fd; + int iommu_type; + struct wd_ss_region_list ss_list; + struct wd_ss_region_list *head; + unsigned int dev_flags; + unsigned long ss_size; + enum wcrypto_type atype; + int ctx_num; + struct wd_mm_br br; + struct wcrypto_hash_mt hash; + unsigned long qfrs_offset[WD_UACCE_QFRT_MAX]; + struct wd_lock qlock; + __u8 ctx_id[WD_MAX_CTX_NUM]; +}; + +struct wd_cookie_pool { + void *cookies; + __u8 *cstatus; + __u32 cookies_num; + __u32 cookies_size; + __u32 cid; +}; + +struct wd_dif_gen { + __u32 page_layout_gen_type:4; + __u32 grd_gen_type:4; + __u32 ver_gen_type:4; + __u32 app_gen_type:4; + __u32 ref_gen_type:4; + __u32 page_layout_pad_type:2; + __u32 reserved:10; +}; + +struct wd_dif_verify { + __u16 page_layout_pad_type:2; + __u16 grd_verify_type:4; + __u16 ref_verify_type:4; + __u16 reserved:6; +}; + +struct wd_dif_ctrl { + struct wd_dif_gen gen; + struct wd_dif_verify verify; + __u8 dif_comp_ctrl; +}; + +struct wd_dif { + __u64 lba; + __u32 priv_info; + __u8 ver; + __u8 app; + struct wd_dif_ctrl ctrl; +}; + +struct wd_sec_udata { + __u32 src_offset; + __u32 dst_offset; + struct wd_dif dif; + __u16 block_size; + __u16 gran_num; + __u16 key_bytes; + __u8 *key; +}; + +/* Digest tag format of Warpdrive */ +struct wcrypto_digest_tag { + struct wcrypto_cb_tag wcrypto_tag; + __u64 long_data_len; + void *priv; +}; + +/* Cipher tag format of Warpdrive */ +struct wcrypto_cipher_tag { + struct wcrypto_cb_tag wcrypto_tag; + void *priv; +}; + +/* AEAD tag format of Warpdrive */ +struct wcrypto_aead_tag { + struct wcrypto_cb_tag wcrypto_tag; + void *priv; +}; + +/* EC tag format of Warpdrive */ +struct wcrypto_ec_tag { + struct wcrypto_cb_tag wcrypto_tag; + __u64 tbl_addr; + void *priv; +}; + +/* COMP tag format of Warpdrive */ +struct wcrypto_comp_tag { + struct wcrypto_cb_tag wcrypto_tag; + void *priv; +}; + +/* ecc */ +struct wcrypto_ecc_pubkey { + struct wd_dtb p; + struct wd_dtb a; + struct wd_dtb b; + struct wd_dtb n; + struct wcrypto_ecc_point g; + struct wcrypto_ecc_point pub; + __u32 size; + void *data; +}; + +struct wcrypto_ecc_prikey { + struct wd_dtb p; + struct wd_dtb a; + struct wd_dtb d; + struct wd_dtb b; + struct wd_dtb n; + struct wcrypto_ecc_point g; + __u32 size; + void *data; +}; + +struct wcrypto_ecc_key { + struct wcrypto_ecc_pubkey *pubkey; + struct wcrypto_ecc_prikey *prikey; + struct wcrypto_ecc_curve *cv; + struct wcrypto_ecc_point *pub; + struct wd_dtb *d; +}; + +struct wcrypto_ecc_dh_in { + struct wcrypto_ecc_point pbk; +}; + +struct wcrypto_ecc_sign_in { + struct wd_dtb dgst; /* hash message */ + struct wd_dtb k; /* random */ + struct wd_dtb plaintext; /* original text before hash */ + __u8 k_set; /* 1 - k parameter set 0 - not set */ + __u8 dgst_set; /* 1 - dgst parameter set 0 - not set */ +}; + +struct wcrypto_ecc_verf_in { + struct wd_dtb dgst; /* hash message */ + struct wd_dtb s; /* signature s parameter */ + struct wd_dtb r; /* signature r parameter */ + struct wd_dtb plaintext; /* original text before hash */ + __u8 dgst_set; /* 1 - dgst parameter set 0 - not set */ +}; + +struct wcrypto_ecc_dh_out { + struct wcrypto_ecc_point out; +}; + +struct wcrypto_ecc_sign_out { + struct wd_dtb r; /* signature r parameter */ + struct wd_dtb s; /* signature s parameter */ +}; + +struct wcrypto_sm2_enc_in { + struct wd_dtb k; /* random */ + struct wd_dtb plaintext; /* original text */ + __u8 k_set; /* 0 - not set 1 - set */ +}; + +struct wcrypto_sm2_enc_out { + struct wcrypto_ecc_point c1; + struct wd_dtb c2; + struct wd_dtb c3; +}; + +struct wcrypto_sm2_dec_in { + struct wcrypto_ecc_point c1; + struct wd_dtb c2; + struct wd_dtb c3; +}; + +struct wcrypto_sm2_kg_in { + struct wcrypto_ecc_point g; +}; + +struct wcrypto_sm2_dec_out { + struct wd_dtb plaintext; +}; + +struct wcrypto_sm2_kg_out { + struct wcrypto_ecc_point pub; + struct wd_dtb priv; +}; + +typedef union { + struct wcrypto_ecc_dh_in dh_in; + struct wcrypto_ecc_sign_in sin; + struct wcrypto_ecc_verf_in vin; + struct wcrypto_sm2_enc_in ein; + struct wcrypto_sm2_dec_in din; + struct wcrypto_sm2_kg_in kin; +} wcrypto_ecc_in_param; + +typedef union { + struct wcrypto_ecc_dh_out dh_out; + struct wcrypto_ecc_sign_out sout; + struct wcrypto_sm2_enc_out eout; + struct wcrypto_sm2_dec_out dout; + struct wcrypto_sm2_kg_out kout; +} wcrypto_ecc_out_param; + +struct wcrypto_ecc_in { + wcrypto_ecc_in_param param; + __u64 size; + char data[]; +}; + +struct wcrypto_ecc_out { + wcrypto_ecc_out_param param; + __u64 size; + char data[]; +}; + +#ifdef DEBUG_LOG +#define dbg(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__) +#else +#define dbg(msg, ...) +#endif + +#ifdef DEBUG +#define ASSERT(f) assert(f) +#else +#define ASSERT(f) +#endif + +#if defined(__AARCH64_CMODEL_SMALL__) && __AARCH64_CMODEL_SMALL__ +#define dsb(opt) { asm volatile("dsb " #opt : : : "memory"); } +#define rmb() dsb(ld) /* read fence */ +#define wmb() dsb(st) /* write fence */ +#define mb() dsb(sy) /* rw fence */ +#else +#define rmb() __sync_synchronize() /* read fence */ +#define wmb() __sync_synchronize() /* write fence */ +#define mb() __sync_synchronize() /* rw fence */ +#endif + +static inline void wd_reg_write(void *reg_addr, uint32_t value) +{ + *((uint32_t *)reg_addr) = value; + wmb(); /* load fence */ +} + +static inline uint32_t wd_reg_read(void *reg_addr) +{ + uint32_t temp; + + temp = *((uint32_t *)reg_addr); + rmb(); /* load fence */ + + return temp; +} + +void wd_spinlock(struct wd_lock *lock); +void wd_unspinlock(struct wd_lock *lock); +void *wd_drv_mmap_qfr(struct wd_queue *q, enum uacce_qfrt qfrt, size_t size); +void wd_drv_unmmap_qfr(struct wd_queue *q, void *addr, + enum uacce_qfrt qfrt, size_t size); +void *drv_iova_map(struct wd_queue *q, void *va, size_t sz); +void drv_iova_unmap(struct wd_queue *q, void *va, void *dma, size_t sz); +int wd_init_cookie_pool(struct wd_cookie_pool *pool, + __u32 cookies_size, __u32 cookies_num); +void wd_uninit_cookie_pool(struct wd_cookie_pool *pool); +int wd_alloc_id(__u8 *buf, __u32 size, __u32 *id, __u32 last_id, __u32 id_max); +void wd_free_id(__u8 *buf, __u32 size, __u32 id, __u32 id_max); +int wd_get_cookies(struct wd_cookie_pool *pool, void **cookies, __u32 num); +void wd_put_cookies(struct wd_cookie_pool *pool, void **cookies, __u32 num); +const char *wd_get_drv(struct wd_queue *q); +int wd_burst_send(struct wd_queue *q, void **req, __u32 num); +int wd_burst_recv(struct wd_queue *q, void **resp, __u32 num); + +void drv_set_sgl_sge_pri(struct wd_sgl *sgl, int num, void *priv); +void *drv_get_sgl_sge_pri(struct wd_sgl *sgl, int num); +void drv_set_sgl_pri(struct wd_sgl *sgl, void *priv); +void *drv_get_sgl_pri(struct wd_sgl *sgl); +struct wd_mm_br *drv_get_br(void *pool); +void wd_sgl_memset(struct wd_sgl *sgl, int ch); + +#endif -- 2.33.0
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2