Home | History | Annotate | Line # | Download | only in libarchive
      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 
     87 /* ---------- Internal Functions ---------- */
     88 
     89 extern const Byte PPMD8_kExpEscape[16];
     90 
     91 #ifdef PPMD_32BIT
     92   #define Ppmd8_GetPtr(p, ptr) (ptr)
     93   #define Ppmd8_GetContext(p, ptr) (ptr)
     94   #define Ppmd8_GetStats(p, ctx) ((ctx)->Stats)
     95 #else
     96   #define Ppmd8_GetPtr(p, offs) ((void *)((p)->Base + (offs)))
     97   #define Ppmd8_GetContext(p, offs) ((CPpmd8_Context *)Ppmd8_GetPtr((p), (offs)))
     98   #define Ppmd8_GetStats(p, ctx) ((CPpmd_State *)Ppmd8_GetPtr((p), ((ctx)->Stats)))
     99 #endif
    100 
    101 #define Ppmd8_GetBinSumm(p) \
    102     &p->BinSumm[p->NS2Indx[Ppmd8Context_OneState(p->MinContext)->Freq - 1]][ \
    103     p->NS2BSIndx[Ppmd8_GetContext(p, p->MinContext->Suffix)->NumStats] + \
    104     p->PrevSuccess + p->MinContext->Flags + ((p->RunLength >> 26) & 0x20)]
    105 
    106 
    107 typedef struct
    108 {
    109   /* Base Functions */
    110   void (*Ppmd8_Construct)(CPpmd8 *p);
    111   Bool (*Ppmd8_Alloc)(CPpmd8 *p, UInt32 size);
    112   void (*Ppmd8_Free)(CPpmd8 *p);
    113   void (*Ppmd8_Init)(CPpmd8 *p, unsigned max_order, unsigned restore_method);
    114   #define Ppmd7_WasAllocated(p) ((p)->Base != NULL)
    115 
    116   /* Decode Functions */
    117   int (*Ppmd8_RangeDec_Init)(CPpmd8 *p);
    118   int (*Ppmd8_DecodeSymbol)(CPpmd8 *p);
    119 } IPpmd8;
    120 
    121 extern const IPpmd8 __archive_ppmd8_functions;
    122 
    123 #endif
    124