Home | History | Annotate | Line # | Download | only in libarchive
      1 /* Ppmd.h -- PPMD codec common code
      2 2010-03-12 : Igor Pavlov : Public domain
      3 This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
      4 
      5 #ifndef ARCHIVE_PPMD_PRIVATE_H_INCLUDED
      6 #define ARCHIVE_PPMD_PRIVATE_H_INCLUDED
      7 
      8 #ifndef __LIBARCHIVE_BUILD
      9 #error This header is only to be used internally to libarchive.
     10 #endif
     11 
     12 #include <stddef.h>
     13 
     14 #include "archive_read_private.h"
     15 
     16 /*** Begin defined in Types.h ***/
     17 
     18 #if !defined(ZCONF_H)
     19 typedef unsigned char Byte;
     20 #endif
     21 typedef short Int16;
     22 typedef unsigned short UInt16;
     23 
     24 #ifdef _LZMA_UINT32_IS_ULONG
     25 typedef long Int32;
     26 typedef unsigned long UInt32;
     27 #else
     28 typedef int Int32;
     29 typedef unsigned int UInt32;
     30 #endif
     31 
     32 #ifdef _SZ_NO_INT_64
     33 
     34 /* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
     35    NOTES: Some code will work incorrectly in that case! */
     36 
     37 typedef long Int64;
     38 typedef unsigned long UInt64;
     39 
     40 #else
     41 
     42 #if defined(_MSC_VER) || defined(__BORLANDC__)
     43 typedef __int64 Int64;
     44 typedef unsigned __int64 UInt64;
     45 #define UINT64_CONST(n) n
     46 #else
     47 typedef long long int Int64;
     48 typedef unsigned long long int UInt64;
     49 #define UINT64_CONST(n) n ## ULL
     50 #endif
     51 
     52 #endif
     53 
     54 typedef int Bool;
     55 #define True 1
     56 #define False 0
     57 
     58 /* The following interfaces use first parameter as pointer to structure */
     59 
     60 typedef struct
     61 {
     62   struct archive_read *a;
     63   Byte (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */
     64 } IByteIn;
     65 
     66 typedef struct
     67 {
     68   struct archive_write *a;
     69   void (*Write)(void *p, Byte b);
     70 } IByteOut;
     71 
     72 /*** End defined in Types.h ***/
     73 /*** Begin defined in CpuArch.h ***/
     74 
     75 #if defined(_M_IX86) || defined(__i386__)
     76 #define MY_CPU_X86
     77 #endif
     78 
     79 #if defined(MY_CPU_X86) || defined(_M_ARM)
     80 #define MY_CPU_32BIT
     81 #endif
     82 
     83 #ifdef MY_CPU_32BIT
     84 #define PPMD_32BIT
     85 #endif
     86 
     87 /*** End defined in CpuArch.h ***/
     88 
     89 #define PPMD_INT_BITS 7
     90 #define PPMD_PERIOD_BITS 7
     91 #define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS))
     92 
     93 #define PPMD_GET_MEAN_SPEC(summ, shift, round) (((summ) + (1 << ((shift) - (round)))) >> (shift))
     94 #define PPMD_GET_MEAN(summ) PPMD_GET_MEAN_SPEC((summ), PPMD_PERIOD_BITS, 2)
     95 #define PPMD_UPDATE_PROB_0(prob) ((prob) + (1 << PPMD_INT_BITS) - PPMD_GET_MEAN(prob))
     96 #define PPMD_UPDATE_PROB_1(prob) ((prob) - PPMD_GET_MEAN(prob))
     97 
     98 #define PPMD_N1 4
     99 #define PPMD_N2 4
    100 #define PPMD_N3 4
    101 #define PPMD_N4 ((128 + 3 - 1 * PPMD_N1 - 2 * PPMD_N2 - 3 * PPMD_N3) / 4)
    102 #define PPMD_NUM_INDEXES (PPMD_N1 + PPMD_N2 + PPMD_N3 + PPMD_N4)
    103 
    104 /* SEE-contexts for PPM-contexts with masked symbols */
    105 typedef struct
    106 {
    107   UInt16 Summ; /* Freq */
    108   Byte Shift;  /* Speed of Freq change; low Shift is for fast change */
    109   Byte Count;  /* Count to next change of Shift */
    110 } CPpmd_See;
    111 
    112 #define Ppmd_See_Update(p) do {						\
    113 	if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) {	\
    114 		(p)->Summ <<= 1;					\
    115 		(p)->Count = (Byte)(3 << (p)->Shift++);			\
    116     	}								\
    117 } while (0)
    118 
    119 typedef struct
    120 {
    121   Byte Symbol;
    122   Byte Freq;
    123   UInt16 SuccessorLow;
    124   UInt16 SuccessorHigh;
    125 } CPpmd_State;
    126 
    127 typedef
    128   #ifdef PPMD_32BIT
    129     CPpmd_State *
    130   #else
    131     UInt32
    132   #endif
    133   CPpmd_State_Ref;
    134 
    135 typedef
    136   #ifdef PPMD_32BIT
    137     void *
    138   #else
    139     UInt32
    140   #endif
    141   CPpmd_Void_Ref;
    142 
    143 typedef
    144   #ifdef PPMD_32BIT
    145     Byte *
    146   #else
    147     UInt32
    148   #endif
    149   CPpmd_Byte_Ref;
    150 
    151 #define PPMD_SetAllBitsIn256Bytes(p) do {				\
    152 	unsigned j;							\
    153 	for (j = 0; j < 256 / sizeof(p[0]); j += 8) {			\
    154 		p[j+7] = p[j+6] = p[j+5] = p[j+4] =			\
    155 		    p[j+3] = p[j+2] = p[j+1] = p[j+0] = ~(size_t)0;	\
    156 	}								\
    157 } while (0)
    158 
    159 #endif
    160