1231200Smm/* Ppmd.h -- PPMD codec common code
2231200Smm2010-03-12 : Igor Pavlov : Public domain
3231200SmmThis code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4231200Smm
5231200Smm#ifndef __LIBARCHIVE_BUILD
6231200Smm#error This header is only to be used internally to libarchive.
7231200Smm#endif
8231200Smm
9231200Smm#ifndef ARCHIVE_PPMD_PRIVATE_H_INCLUDED
10231200Smm#define ARCHIVE_PPMD_PRIVATE_H_INCLUDED
11231200Smm
12231200Smm#include <stddef.h>
13231200Smm
14231200Smm#include "archive_read_private.h"
15231200Smm
16231200Smm/*** Begin defined in Types.h ***/
17231200Smm
18231200Smm#if !defined(ZCONF_H)
19231200Smmtypedef unsigned char Byte;
20231200Smm#endif
21231200Smmtypedef short Int16;
22231200Smmtypedef unsigned short UInt16;
23231200Smm
24231200Smm#ifdef _LZMA_UINT32_IS_ULONG
25231200Smmtypedef long Int32;
26231200Smmtypedef unsigned long UInt32;
27231200Smm#else
28231200Smmtypedef int Int32;
29231200Smmtypedef unsigned int UInt32;
30231200Smm#endif
31231200Smm
32231200Smm#ifdef _SZ_NO_INT_64
33231200Smm
34231200Smm/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
35231200Smm   NOTES: Some code will work incorrectly in that case! */
36231200Smm
37231200Smmtypedef long Int64;
38231200Smmtypedef unsigned long UInt64;
39231200Smm
40231200Smm#else
41231200Smm
42231200Smm#if defined(_MSC_VER) || defined(__BORLANDC__)
43231200Smmtypedef __int64 Int64;
44231200Smmtypedef unsigned __int64 UInt64;
45231200Smm#define UINT64_CONST(n) n
46231200Smm#else
47231200Smmtypedef long long int Int64;
48231200Smmtypedef unsigned long long int UInt64;
49231200Smm#define UINT64_CONST(n) n ## ULL
50231200Smm#endif
51231200Smm
52231200Smm#endif
53231200Smm
54231200Smmtypedef int Bool;
55231200Smm#define True 1
56231200Smm#define False 0
57231200Smm
58231200Smm/* The following interfaces use first parameter as pointer to structure */
59231200Smm
60231200Smmtypedef struct
61231200Smm{
62231200Smm  struct archive_read *a;
63231200Smm  Byte (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */
64231200Smm} IByteIn;
65231200Smm
66231200Smmtypedef struct
67231200Smm{
68231200Smm  struct archive_write *a;
69231200Smm  void (*Write)(void *p, Byte b);
70231200Smm} IByteOut;
71231200Smm
72231200Smm
73231200Smmtypedef struct
74231200Smm{
75231200Smm  void *(*Alloc)(void *p, size_t size);
76231200Smm  void (*Free)(void *p, void *address); /* address can be 0 */
77231200Smm} ISzAlloc;
78231200Smm
79231200Smm/*** End defined in Types.h ***/
80231200Smm/*** Begin defined in CpuArch.h ***/
81231200Smm
82231200Smm#if defined(_M_IX86) || defined(__i386__)
83231200Smm#define MY_CPU_X86
84231200Smm#endif
85231200Smm
86231200Smm#if defined(MY_CPU_X86) || defined(_M_ARM)
87231200Smm#define MY_CPU_32BIT
88231200Smm#endif
89231200Smm
90231200Smm#ifdef MY_CPU_32BIT
91231200Smm#define PPMD_32BIT
92231200Smm#endif
93231200Smm
94231200Smm/*** End defined in CpuArch.h ***/
95231200Smm
96231200Smm#define PPMD_INT_BITS 7
97231200Smm#define PPMD_PERIOD_BITS 7
98231200Smm#define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS))
99231200Smm
100231200Smm#define PPMD_GET_MEAN_SPEC(summ, shift, round) (((summ) + (1 << ((shift) - (round)))) >> (shift))
101231200Smm#define PPMD_GET_MEAN(summ) PPMD_GET_MEAN_SPEC((summ), PPMD_PERIOD_BITS, 2)
102231200Smm#define PPMD_UPDATE_PROB_0(prob) ((prob) + (1 << PPMD_INT_BITS) - PPMD_GET_MEAN(prob))
103231200Smm#define PPMD_UPDATE_PROB_1(prob) ((prob) - PPMD_GET_MEAN(prob))
104231200Smm
105231200Smm#define PPMD_N1 4
106231200Smm#define PPMD_N2 4
107231200Smm#define PPMD_N3 4
108231200Smm#define PPMD_N4 ((128 + 3 - 1 * PPMD_N1 - 2 * PPMD_N2 - 3 * PPMD_N3) / 4)
109231200Smm#define PPMD_NUM_INDEXES (PPMD_N1 + PPMD_N2 + PPMD_N3 + PPMD_N4)
110231200Smm
111231200Smm/* SEE-contexts for PPM-contexts with masked symbols */
112231200Smmtypedef struct
113231200Smm{
114231200Smm  UInt16 Summ; /* Freq */
115231200Smm  Byte Shift;  /* Speed of Freq change; low Shift is for fast change */
116231200Smm  Byte Count;  /* Count to next change of Shift */
117231200Smm} CPpmd_See;
118231200Smm
119231200Smm#define Ppmd_See_Update(p)  if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \
120231200Smm    { (p)->Summ <<= 1; (p)->Count = (Byte)(3 << (p)->Shift++); }
121231200Smm
122231200Smmtypedef struct
123231200Smm{
124231200Smm  Byte Symbol;
125231200Smm  Byte Freq;
126231200Smm  UInt16 SuccessorLow;
127231200Smm  UInt16 SuccessorHigh;
128231200Smm} CPpmd_State;
129231200Smm
130231200Smmtypedef
131231200Smm  #ifdef PPMD_32BIT
132231200Smm    CPpmd_State *
133231200Smm  #else
134231200Smm    UInt32
135231200Smm  #endif
136231200Smm  CPpmd_State_Ref;
137231200Smm
138231200Smmtypedef
139231200Smm  #ifdef PPMD_32BIT
140231200Smm    void *
141231200Smm  #else
142231200Smm    UInt32
143231200Smm  #endif
144231200Smm  CPpmd_Void_Ref;
145231200Smm
146231200Smmtypedef
147231200Smm  #ifdef PPMD_32BIT
148231200Smm    Byte *
149231200Smm  #else
150231200Smm    UInt32
151231200Smm  #endif
152231200Smm  CPpmd_Byte_Ref;
153231200Smm
154231200Smm#define PPMD_SetAllBitsIn256Bytes(p) \
155232153Smm  { unsigned j; for (j = 0; j < 256 / sizeof(p[0]); j += 8) { \
156232153Smm  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; }}
157231200Smm
158231200Smm#endif
159