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 */
5 #ifndef __LIBARCHIVE_BUILD
6 #error This header is only to be used internally to libarchive.
9 #ifndef ARCHIVE_PPMD_PRIVATE_H_INCLUDED
10 #define ARCHIVE_PPMD_PRIVATE_H_INCLUDED
14 #include "archive_read_private.h"
16 /*** Begin defined in Types.h ***/
19 typedef unsigned char Byte;
22 typedef unsigned short UInt16;
24 #ifdef _LZMA_UINT32_IS_ULONG
26 typedef unsigned long UInt32;
29 typedef unsigned int UInt32;
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! */
38 typedef unsigned long UInt64;
42 #if defined(_MSC_VER) || defined(__BORLANDC__)
43 typedef __int64 Int64;
44 typedef unsigned __int64 UInt64;
45 #define UINT64_CONST(n) n
47 typedef long long int Int64;
48 typedef unsigned long long int UInt64;
49 #define UINT64_CONST(n) n ## ULL
58 /* The following interfaces use first parameter as pointer to structure */
62 struct archive_read *a;
63 Byte (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */
68 struct archive_write *a;
69 void (*Write)(void *p, Byte b);
75 void *(*Alloc)(void *p, size_t size);
76 void (*Free)(void *p, void *address); /* address can be 0 */
79 /*** End defined in Types.h ***/
80 /*** Begin defined in CpuArch.h ***/
82 #if defined(_M_IX86) || defined(__i386__)
86 #if defined(MY_CPU_X86) || defined(_M_ARM)
94 /*** End defined in CpuArch.h ***/
96 #define PPMD_INT_BITS 7
97 #define PPMD_PERIOD_BITS 7
98 #define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS))
100 #define PPMD_GET_MEAN_SPEC(summ, shift, round) (((summ) + (1 << ((shift) - (round)))) >> (shift))
101 #define PPMD_GET_MEAN(summ) PPMD_GET_MEAN_SPEC((summ), PPMD_PERIOD_BITS, 2)
102 #define PPMD_UPDATE_PROB_0(prob) ((prob) + (1 << PPMD_INT_BITS) - PPMD_GET_MEAN(prob))
103 #define PPMD_UPDATE_PROB_1(prob) ((prob) - PPMD_GET_MEAN(prob))
108 #define PPMD_N4 ((128 + 3 - 1 * PPMD_N1 - 2 * PPMD_N2 - 3 * PPMD_N3) / 4)
109 #define PPMD_NUM_INDEXES (PPMD_N1 + PPMD_N2 + PPMD_N3 + PPMD_N4)
111 /* SEE-contexts for PPM-contexts with masked symbols */
114 UInt16 Summ; /* Freq */
115 Byte Shift; /* Speed of Freq change; low Shift is for fast change */
116 Byte Count; /* Count to next change of Shift */
119 #define Ppmd_See_Update(p) if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \
120 { (p)->Summ <<= 1; (p)->Count = (Byte)(3 << (p)->Shift++); }
127 UInt16 SuccessorHigh;
154 #define PPMD_SetAllBitsIn256Bytes(p) \
155 { unsigned j; for (j = 0; j < 256 / sizeof(p[0]); j += 8) { \
156 p[j+7] = p[j+6] = p[j+5] = p[j+4] = p[j+3] = p[j+2] = p[j+1] = p[j+0] = ~(size_t)0; }}