1 /* Ppmd8.h -- PPMdI codec 2 2011-01-27 : Igor Pavlov : Public domain 3 This code is based on: 4 PPMd var.I (2002): Dmitry Shkarin : Public domain 5 Carryless rangecoder (1999): Dmitry Subbotin : Public domain */ 6 7 #ifndef ARCHIVE_PPMD8_PRIVATE_H_INCLUDED 8 #define ARCHIVE_PPMD8_PRIVATE_H_INCLUDED 9 10 #include "archive_ppmd_private.h" 11 12 #define PPMD8_MIN_ORDER 2 13 #define PPMD8_MAX_ORDER 16 14 15 struct CPpmd8_Context_; 16 17 typedef 18 #ifdef PPMD_32BIT 19 struct CPpmd8_Context_ * 20 #else 21 UInt32 22 #endif 23 CPpmd8_Context_Ref; 24 25 #pragma pack(push, 1) 26 27 typedef struct CPpmd8_Context_ 28 { 29 Byte NumStats; 30 Byte Flags; 31 UInt16 SummFreq; 32 CPpmd_State_Ref Stats; 33 CPpmd8_Context_Ref Suffix; 34 } CPpmd8_Context; 35 36 #pragma pack(pop) 37 38 #define Ppmd8Context_OneState(p) ((CPpmd_State *)&(p)->SummFreq) 39 40 /* The BUG in Shkarin's code for FREEZE mode was fixed, but that fixed 41 code is not compatible with original code for some files compressed 42 in FREEZE mode. So we disable FREEZE mode support. */ 43 44 enum 45 { 46 PPMD8_RESTORE_METHOD_RESTART, 47 PPMD8_RESTORE_METHOD_CUT_OFF 48 #ifdef PPMD8_FREEZE_SUPPORT 49 , PPMD8_RESTORE_METHOD_FREEZE 50 #endif 51 }; 52 53 typedef struct 54 { 55 CPpmd8_Context *MinContext, *MaxContext; 56 CPpmd_State *FoundState; 57 unsigned OrderFall, InitEsc, PrevSuccess, MaxOrder; 58 Int32 RunLength, InitRL; /* must be 32-bit at least */ 59 60 UInt32 Size; 61 UInt32 GlueCount; 62 Byte *Base, *LoUnit, *HiUnit, *Text, *UnitsStart; 63 UInt32 AlignOffset; 64 unsigned RestoreMethod; 65 66 /* Range Coder */ 67 UInt32 Range; 68 UInt32 Code; 69 UInt32 Low; 70 union 71 { 72 IByteIn *In; 73 IByteOut *Out; 74 } Stream; 75 76 Byte Indx2Units[PPMD_NUM_INDEXES]; 77 Byte Units2Indx[128]; 78 CPpmd_Void_Ref FreeList[PPMD_NUM_INDEXES]; 79 UInt32 Stamps[PPMD_NUM_INDEXES]; 80 81 Byte NS2BSIndx[256], NS2Indx[260]; 82 CPpmd_See DummySee, See[24][32]; 83 UInt16 BinSumm[25][64]; 84 } CPpmd8; 85 86 void Ppmd8_Construct(CPpmd8 *p); 87 Bool Ppmd8_Alloc(CPpmd8 *p, UInt32 size); 88 void Ppmd8_Free(CPpmd8 *p); 89 void Ppmd8_Init(CPpmd8 *p, unsigned maxOrder, unsigned restoreMethod); 90 #define Ppmd8_WasAllocated(p) ((p)->Base != NULL) 91 92 93 /* ---------- Internal Functions ---------- */ 94 95 extern const Byte PPMD8_kExpEscape[16]; 96 97 #ifdef PPMD_32BIT 98 #define Ppmd8_GetPtr(p, ptr) (ptr) 99 #define Ppmd8_GetContext(p, ptr) (ptr) 100 #define Ppmd8_GetStats(p, ctx) ((ctx)->Stats) 101 #else 102 #define Ppmd8_GetPtr(p, offs) ((void *)((p)->Base + (offs))) 103 #define Ppmd8_GetContext(p, offs) ((CPpmd8_Context *)Ppmd8_GetPtr((p), (offs))) 104 #define Ppmd8_GetStats(p, ctx) ((CPpmd_State *)Ppmd8_GetPtr((p), ((ctx)->Stats))) 105 #endif 106 107 void Ppmd8_Update1(CPpmd8 *p); 108 void Ppmd8_Update1_0(CPpmd8 *p); 109 void Ppmd8_Update2(CPpmd8 *p); 110 void Ppmd8_UpdateBin(CPpmd8 *p); 111 112 #define Ppmd8_GetBinSumm(p) \ 113 &p->BinSumm[p->NS2Indx[Ppmd8Context_OneState(p->MinContext)->Freq - 1]][ \ 114 p->NS2BSIndx[Ppmd8_GetContext(p, p->MinContext->Suffix)->NumStats] + \ 115 p->PrevSuccess + p->MinContext->Flags + ((p->RunLength >> 26) & 0x20)] 116 117 CPpmd_See *Ppmd8_MakeEscFreq(CPpmd8 *p, unsigned numMasked, UInt32 *scale); 118 119 120 /* ---------- Decode ---------- */ 121 122 Bool Ppmd8_RangeDec_Init(CPpmd8 *p); 123 #define Ppmd8_RangeDec_IsFinishedOK(p) ((p)->Code == 0) 124 int Ppmd8_DecodeSymbol(CPpmd8 *p); /* returns: -1 as EndMarker, -2 as DataError */ 125 126 /* ---------- Encode ---------- */ 127 128 #define Ppmd8_RangeEnc_Init(p) { (p)->Low = 0; (p)->Range = 0xFFFFFFFF; } 129 void Ppmd8_RangeEnc_FlushData(CPpmd8 *p); 130 void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol); /* symbol = -1 means EndMarker */ 131 132 typedef struct 133 { 134 /* Base Functions */ 135 void (*Ppmd8_Construct)(CPpmd8 *p); 136 Bool (*Ppmd8_Alloc)(CPpmd8 *p, UInt32 size); 137 void (*Ppmd8_Free)(CPpmd8 *p); 138 void (*Ppmd8_Init)(CPpmd8 *p, unsigned max_order, unsigned restore_method); 139 #define Ppmd7_WasAllocated(p) ((p)->Base != NULL) 140 141 /* Decode Functions */ 142 int (*Ppmd8_RangeDec_Init)(CPpmd8 *p); 143 int (*Ppmd8_DecodeSymbol)(CPpmd8 *p); 144 } IPpmd8; 145 146 extern const IPpmd8 __archive_ppmd8_functions; 147 148 #endif 149