1 /* Ppmd8.h -- Ppmd8 (PPMdI) compression codec 2 2023-04-02 : 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 ZIP7_INC_PPMD8_H 8 #define ZIP7_INC_PPMD8_H 9 10 #include "Ppmd.h" 11 12 EXTERN_C_BEGIN 13 14 #define PPMD8_MIN_ORDER 2 15 #define PPMD8_MAX_ORDER 16 16 17 18 19 20 struct CPpmd8_Context_; 21 22 typedef Ppmd_Ref_Type(struct CPpmd8_Context_) CPpmd8_Context_Ref; 23 24 // MY_CPU_pragma_pack_push_1 25 26 typedef struct CPpmd8_Context_ 27 { 28 Byte NumStats; 29 Byte Flags; 30 31 union 32 { 33 UInt16 SummFreq; 34 CPpmd_State2 State2; 35 } Union2; 36 37 union 38 { 39 CPpmd_State_Ref Stats; 40 CPpmd_State4 State4; 41 } Union4; 42 43 CPpmd8_Context_Ref Suffix; 44 } CPpmd8_Context; 45 46 // MY_CPU_pragma_pop 47 48 #define Ppmd8Context_OneState(p) ((CPpmd_State *)&(p)->Union2) 49 50 /* PPMdI code rev.2 contains the fix over PPMdI code rev.1. 51 But the code PPMdI.2 is not compatible with PPMdI.1 for some files compressed 52 in FREEZE mode. So we disable FREEZE mode support. */ 53 54 // #define PPMD8_FREEZE_SUPPORT 55 56 enum 57 { 58 PPMD8_RESTORE_METHOD_RESTART, 59 PPMD8_RESTORE_METHOD_CUT_OFF 60 #ifdef PPMD8_FREEZE_SUPPORT 61 , PPMD8_RESTORE_METHOD_FREEZE 62 #endif 63 , PPMD8_RESTORE_METHOD_UNSUPPPORTED 64 }; 65 66 67 68 69 70 71 72 73 typedef struct 74 { 75 CPpmd8_Context *MinContext, *MaxContext; 76 CPpmd_State *FoundState; 77 unsigned OrderFall, InitEsc, PrevSuccess, MaxOrder, RestoreMethod; 78 Int32 RunLength, InitRL; /* must be 32-bit at least */ 79 80 UInt32 Size; 81 UInt32 GlueCount; 82 UInt32 AlignOffset; 83 Byte *Base, *LoUnit, *HiUnit, *Text, *UnitsStart; 84 85 UInt32 Range; 86 UInt32 Code; 87 UInt32 Low; 88 union 89 { 90 IByteInPtr In; 91 IByteOutPtr Out; 92 } Stream; 93 94 Byte Indx2Units[PPMD_NUM_INDEXES + 2]; // +2 for alignment 95 Byte Units2Indx[128]; 96 CPpmd_Void_Ref FreeList[PPMD_NUM_INDEXES]; 97 UInt32 Stamps[PPMD_NUM_INDEXES]; 98 Byte NS2BSIndx[256], NS2Indx[260]; 99 Byte ExpEscape[16]; 100 CPpmd_See DummySee, See[24][32]; 101 UInt16 BinSumm[25][64]; 102 103 } CPpmd8; 104 105 106 void Ppmd8_Construct(CPpmd8 *p); 107 BoolInt Ppmd8_Alloc(CPpmd8 *p, UInt32 size, ISzAllocPtr alloc); 108 void Ppmd8_Free(CPpmd8 *p, ISzAllocPtr alloc); 109 void Ppmd8_Init(CPpmd8 *p, unsigned maxOrder, unsigned restoreMethod); 110 #define Ppmd8_WasAllocated(p) ((p)->Base != NULL) 111 112 113 /* ---------- Internal Functions ---------- */ 114 115 #define Ppmd8_GetPtr(p, ptr) Ppmd_GetPtr(p, ptr) 116 #define Ppmd8_GetContext(p, ptr) Ppmd_GetPtr_Type(p, ptr, CPpmd8_Context) 117 #define Ppmd8_GetStats(p, ctx) Ppmd_GetPtr_Type(p, (ctx)->Union4.Stats, CPpmd_State) 118 119 void Ppmd8_Update1(CPpmd8 *p); 120 void Ppmd8_Update1_0(CPpmd8 *p); 121 void Ppmd8_Update2(CPpmd8 *p); 122 123 124 125 126 127 128 #define Ppmd8_GetBinSumm(p) \ 129 &p->BinSumm[p->NS2Indx[(size_t)Ppmd8Context_OneState(p->MinContext)->Freq - 1]] \ 130 [ p->PrevSuccess + ((p->RunLength >> 26) & 0x20) \ 131 + p->NS2BSIndx[Ppmd8_GetContext(p, p->MinContext->Suffix)->NumStats] + \ 132 + p->MinContext->Flags ] 133 134 135 CPpmd_See *Ppmd8_MakeEscFreq(CPpmd8 *p, unsigned numMasked, UInt32 *scale); 136 137 138 /* 20.01: the original PPMdI encoder and decoder probably could work incorrectly in some rare cases, 139 where the original PPMdI code can give "Divide by Zero" operation. 140 We use the following fix to allow correct working of encoder and decoder in any cases. 141 We correct (Escape_Freq) and (_sum_), if (_sum_) is larger than p->Range) */ 142 #define PPMD8_CORRECT_SUM_RANGE(p, _sum_) if (_sum_ > p->Range /* /1 */) _sum_ = p->Range; 143 144 145 /* ---------- Decode ---------- */ 146 147 #define PPMD8_SYM_END (-1) 148 #define PPMD8_SYM_ERROR (-2) 149 150 /* 151 You must set (CPpmd8::Stream.In) before Ppmd8_RangeDec_Init() 152 153 Ppmd8_DecodeSymbol() 154 out: 155 >= 0 : decoded byte 156 -1 : PPMD8_SYM_END : End of payload marker 157 -2 : PPMD8_SYM_ERROR : Data error 158 */ 159 160 161 BoolInt Ppmd8_Init_RangeDec(CPpmd8 *p); 162 #define Ppmd8_RangeDec_IsFinishedOK(p) ((p)->Code == 0) 163 int Ppmd8_DecodeSymbol(CPpmd8 *p); 164 165 166 167 168 169 170 171 172 /* ---------- Encode ---------- */ 173 174 #define Ppmd8_Init_RangeEnc(p) { (p)->Low = 0; (p)->Range = 0xFFFFFFFF; } 175 void Ppmd8_Flush_RangeEnc(CPpmd8 *p); 176 void Ppmd8_EncodeSymbol(CPpmd8 *p, int symbol); 177 178 179 EXTERN_C_END 180 181 #endif 182