Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuichiro Nakada committed Nov 27, 2023
0 parents commit d589b04
Show file tree
Hide file tree
Showing 159 changed files with 34,926 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 7z/7z.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __7Z_H
#define __7Z_H

bool compress_deflate_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned num_passes, unsigned num_fast_bytes) throw ();
bool decompress_deflate_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned out_size) throw ();
bool compress_rfc1950_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned num_passes, unsigned num_fast_bytes) throw ();

bool compress_lzma_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned algo, unsigned dictionary_size, unsigned num_fast_bytes) throw ();
bool decompress_lzma_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned out_size) throw ();

#endif

92 changes: 92 additions & 0 deletions 7z/7zdeflate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "7z.h"

#include "DeflateEncoder.h"
#include "DeflateDecoder.h"

#include "zlib.h"

bool compress_deflate_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned num_passes, unsigned num_fast_bytes) throw ()
{
try {
NDeflate::NEncoder::CCoder cc;

if (cc.SetEncoderNumPasses(num_passes) != S_OK)
return false;

if (cc.SetEncoderNumFastBytes(num_fast_bytes) != S_OK)
return false;

ISequentialInStream in(reinterpret_cast<const char*>(in_data), in_size);
ISequentialOutStream out(reinterpret_cast<char*>(out_data), out_size);

UINT64 in_size_l = in_size;

if (cc.Code(&in, &out, &in_size_l) != S_OK)
return false;

out_size = out.size_get();

if (out.overflow_get())
return false;

return true;
} catch (...) {
return false;
}
}

bool decompress_deflate_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned out_size) throw () {
try {
NDeflate::NDecoder::CCoder cc;

ISequentialInStream in(reinterpret_cast<const char*>(in_data), in_size);
ISequentialOutStream out(reinterpret_cast<char*>(out_data), out_size);

UINT64 in_size_l = in_size;
UINT64 out_size_l = out_size;

if (cc.Code(&in, &out, &in_size_l, &out_size_l) != S_OK)
return false;

if (out.size_get() != out_size || out.overflow_get())
return false;

return true;
} catch (...) {
return false;
}
}

bool compress_rfc1950_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned num_passes, unsigned num_fast_bytes) throw ()
{
if (out_size < 6)
return false;

// 8 - deflate
// 7 - 32k window
// 3 - max compression
unsigned header = (8 << 8) | (7 << 12) | (3 << 6);

header += 31 - (header % 31);

out_data[0] = (header >> 8) & 0xFF;
out_data[1] = header & 0xFF;
out_data += 2;

unsigned size = out_size - 6;
if (!compress_deflate_7z(in_data, in_size, out_data, size, num_passes, num_fast_bytes)) {
return false;
}
out_data += size;

unsigned adler = adler32(adler32(0,0,0), in_data, in_size);

out_data[0] = (adler >> 24) & 0xFF;
out_data[1] = (adler >> 16) & 0xFF;
out_data[2] = (adler >> 8) & 0xFF;
out_data[3] = adler & 0xFF;

out_size = size + 6;

return true;
}
68 changes: 68 additions & 0 deletions 7z/7zlzma.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "7z.h"

#include "LZMAEncoder.h"
#include "LZMADecoder.h"

bool compress_lzma_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned& out_size, unsigned algo, unsigned dictionary_size, unsigned num_fast_bytes) throw () {
try {
NCompress::NLZMA::CEncoder cc;

// reduce the dictionary size if the file is small
while (dictionary_size > 8 && dictionary_size / 2 >= out_size)
dictionary_size /= 2;

if (cc.SetDictionarySize(dictionary_size) != S_OK)
return false;

if (cc.SetEncoderNumFastBytes(num_fast_bytes) != S_OK)
return false;

if (cc.SetEncoderAlgorithm(algo) != S_OK)
return false;

ISequentialInStream in(reinterpret_cast<const char*>(in_data), in_size);
ISequentialOutStream out(reinterpret_cast<char*>(out_data), out_size);

UINT64 in_size_l = in_size;

if (cc.WriteCoderProperties(&out) != S_OK)
return false;

if (cc.Code(&in, &out, &in_size_l) != S_OK)
return false;

out_size = out.size_get();

if (out.overflow_get())
return false;

return true;
} catch (...) {
return false;
}
}

bool decompress_lzma_7z(const unsigned char* in_data, unsigned in_size, unsigned char* out_data, unsigned out_size) throw () {
try {
NCompress::NLZMA::CDecoder cc;

ISequentialInStream in(reinterpret_cast<const char*>(in_data), in_size);
ISequentialOutStream out(reinterpret_cast<char*>(out_data), out_size);

UINT64 in_size_l = in_size;
UINT64 out_size_l = out_size;

if (cc.ReadCoderProperties(&in) != S_OK)
return false;

if (cc.Code(&in, &out, &in_size_l, &out_size_l) != S_OK)
return false;

if (out.size_get() != out_size || out.overflow_get())
return false;

return true;
} catch (...) {
return false;
}
}
21 changes: 21 additions & 0 deletions 7z/AriBitCoder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "AriBitCoder.h"
#include "AriPrice.h"

#include <cmath>

namespace NCompression {
namespace NArithmetic {

static const double kDummyMultMid = (1.0 / kBitPrice) / 2;

CPriceTables::CPriceTables()
{
double aLn2 = log(2);
double aLnAll = log(kBitModelTotal >> kNumMoveReducingBits);
for(UINT32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
m_StatePrices[i] = UINT32((fabs(aLnAll - log(i)) / aLn2 + kDummyMultMid) * kBitPrice);
}

CPriceTables g_PriceTables;

}}
101 changes: 101 additions & 0 deletions 7z/AriBitCoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifndef __COMPRESSION_BITCODER_H
#define __COMPRESSION_BITCODER_H

#include "RangeCoder.h"

namespace NCompression {
namespace NArithmetic {

const int kNumBitModelTotalBits = 11;
const UINT32 kBitModelTotal = (1 << kNumBitModelTotalBits);

const int kNumMoveReducingBits = 2;


class CPriceTables
{
public:
UINT32 m_StatePrices[kBitModelTotal >> kNumMoveReducingBits];
CPriceTables();
};

extern CPriceTables g_PriceTables;


/////////////////////////////
// CBitModel

template <int aNumMoveBits>
class CBitModel
{
public:
UINT32 m_Probability;
void UpdateModel(UINT32 aSymbol)
{
/*
m_Probability -= (m_Probability + ((aSymbol - 1) & ((1 << aNumMoveBits) - 1))) >> aNumMoveBits;
m_Probability += (1 - aSymbol) << (kNumBitModelTotalBits - aNumMoveBits);
*/
if (aSymbol == 0)
m_Probability += (kBitModelTotal - m_Probability) >> aNumMoveBits;
else
m_Probability -= (m_Probability) >> aNumMoveBits;
}
public:
void Init() { m_Probability = kBitModelTotal / 2; }
};

template <int aNumMoveBits>
class CBitEncoder: public CBitModel<aNumMoveBits>
{
public:
void Encode(CRangeEncoder *aRangeEncoder, UINT32 aSymbol)
{
aRangeEncoder->EncodeBit(CBitModel<aNumMoveBits>::m_Probability, kNumBitModelTotalBits, aSymbol);
CBitModel<aNumMoveBits>::UpdateModel(aSymbol);
}
UINT32 GetPrice(UINT32 aSymbol) const
{
return g_PriceTables.m_StatePrices[
(((CBitModel<aNumMoveBits>::m_Probability - aSymbol) ^ ((-(int)aSymbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
}
};


template <int aNumMoveBits>
class CBitDecoder: public CBitModel<aNumMoveBits>
{
public:
UINT32 Decode(CRangeDecoder *aRangeDecoder)
{
UINT32 aNewBound = (aRangeDecoder->m_Range >> kNumBitModelTotalBits) * CBitModel<aNumMoveBits>::m_Probability;
if (aRangeDecoder->m_Code < aNewBound)
{
aRangeDecoder->m_Range = aNewBound;
CBitModel<aNumMoveBits>::m_Probability += (kBitModelTotal - CBitModel<aNumMoveBits>::m_Probability) >> aNumMoveBits;
if (aRangeDecoder->m_Range < kTopValue)
{
aRangeDecoder->m_Code = (aRangeDecoder->m_Code << 8) | aRangeDecoder->m_Stream.ReadByte();
aRangeDecoder->m_Range <<= 8;
}
return 0;
}
else
{
aRangeDecoder->m_Range -= aNewBound;
aRangeDecoder->m_Code -= aNewBound;
CBitModel<aNumMoveBits>::m_Probability -= (CBitModel<aNumMoveBits>::m_Probability) >> aNumMoveBits;
if (aRangeDecoder->m_Range < kTopValue)
{
aRangeDecoder->m_Code = (aRangeDecoder->m_Code << 8) | aRangeDecoder->m_Stream.ReadByte();
aRangeDecoder->m_Range <<= 8;
}
return 1;
}
}
};

}}


#endif
17 changes: 17 additions & 0 deletions 7z/AriConst.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef __ARICONST_H
#define __ARICONST_H

#include "AriBitCoder.h"

typedef NCompression::NArithmetic::CRangeEncoder CMyRangeEncoder;
typedef NCompression::NArithmetic::CRangeDecoder CMyRangeDecoder;

template <int aNumMoveBits> class CMyBitEncoder:
public NCompression::NArithmetic::CBitEncoder<aNumMoveBits> {};
template <int aNumMoveBits> class CMyBitDecoder:
public NCompression::NArithmetic::CBitDecoder<aNumMoveBits> {};

#endif



12 changes: 12 additions & 0 deletions 7z/AriPrice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __COMPRESSION_ARIPRICE_H
#define __COMPRESSION_ARIPRICE_H

namespace NCompression {
namespace NArithmetic {

const UINT32 kNumBitPriceShiftBits = 6;
const UINT32 kBitPrice = 1 << kNumBitPriceShiftBits;

}}

#endif
64 changes: 64 additions & 0 deletions 7z/BinTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "Portable.h"
#include "WindowIn.h"

namespace BT_NAMESPACE {

typedef UINT32 CIndex;
const UINT32 kMaxValForNormalize = (UINT32(1) << 31) - 1;

struct CPair
{
CIndex Left;
CIndex Right;
};

class CInTree: public NStream::NWindow::CIn
{
UINT32 m_HistorySize;
UINT32 m_MatchMaxLen;

CIndex *m_Hash;

#ifdef HASH_ARRAY_2
CIndex *m_Hash2;
#ifdef HASH_ARRAY_3
CIndex *m_Hash3;
#endif
#endif

CPair *m_Son;
CPair *m_Base;

UINT32 m_CutValue;

void NormalizeLinks(CIndex *anArray, UINT32 aNumItems, UINT32 aSubValue);
void Normalize();
void FreeMemory();

protected:
virtual void AfterMoveBlock();
public:
CInTree();
~CInTree();
HRESULT Create(UINT32 aSizeHistory, UINT32 aKeepAddBufferBefore, UINT32 aMatchMaxLen,
UINT32 aKeepAddBufferAfter, UINT32 _dwSizeReserv = (1<<17));
HRESULT Init(ISequentialInStream *aStream);
void SetCutValue(UINT32 aCutValue) { m_CutValue = aCutValue; }
UINT32 GetLongestMatch(UINT32 *aDistances);
void DummyLongestMatch();
HRESULT MovePos()
{
RETURN_IF_NOT_S_OK(CIn::MovePos());
if (m_Pos == kMaxValForNormalize)
Normalize();
return S_OK;
}
void ReduceOffsets(UINT32 aSubValue)
{
CIn::ReduceOffsets(aSubValue);
m_Son += aSubValue;
}
};

}

Loading

0 comments on commit d589b04

Please sign in to comment.