1/* Ppmd.h -- PPMD codec common code
22010-03-12 : Igor Pavlov : Public domain
3This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4
5#ifndef __LIBARCHIVE_BUILD
6#error This header is only to be used internally to libarchive.
7#endif
8
9#ifndef ARCHIVE_PPMD_PRIVATE_H_INCLUDED
10#define ARCHIVE_PPMD_PRIVATE_H_INCLUDED
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)
19typedef unsigned char Byte;
20#endif
21typedef short Int16;
22typedef unsigned short UInt16;
23
24#ifdef _LZMA_UINT32_IS_ULONG
25typedef long Int32;
26typedef unsigned long UInt32;
27#else
28typedef int Int32;
29typedef 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
37typedef long Int64;
38typedef unsigned long UInt64;
39
40#else
41
42#if defined(_MSC_VER) || defined(__BORLANDC__)
43typedef __int64 Int64;
44typedef unsigned __int64 UInt64;
45#define UINT64_CONST(n) n
46#else
47typedef long long int Int64;
48typedef unsigned long long int UInt64;
49#define UINT64_CONST(n) n ## ULL
50#endif
51
52#endif
53
54typedef int Bool;
55#define True 1
56#define False 0
57
58/* The following interfaces use first parameter as pointer to structure */
59
60typedef 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
66typedef struct
67{
68  struct archive_write *a;
69  void (*Write)(void *p, Byte b);
70} IByteOut;
71
72
73typedef struct
74{
75  void *(*Alloc)(void *p, size_t size);
76  void (*Free)(void *p, void *address); /* address can be 0 */
77} ISzAlloc;
78
79/*** End defined in Types.h ***/
80/*** Begin defined in CpuArch.h ***/
81
82#if defined(_M_IX86) || defined(__i386__)
83#define MY_CPU_X86
84#endif
85
86#if defined(MY_CPU_X86) || defined(_M_ARM)
87#define MY_CPU_32BIT
88#endif
89
90#ifdef MY_CPU_32BIT
91#define PPMD_32BIT
92#endif
93
94/*** End defined in CpuArch.h ***/
95
96#define PPMD_INT_BITS 7
97#define PPMD_PERIOD_BITS 7
98#define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS))
99
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))
104
105#define PPMD_N1 4
106#define PPMD_N2 4
107#define PPMD_N3 4
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)
110
111/* SEE-contexts for PPM-contexts with masked symbols */
112typedef struct
113{
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 */
117} CPpmd_See;
118
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++); }
121
122typedef struct
123{
124  Byte Symbol;
125  Byte Freq;
126  UInt16 SuccessorLow;
127  UInt16 SuccessorHigh;
128} CPpmd_State;
129
130typedef
131  #ifdef PPMD_32BIT
132    CPpmd_State *
133  #else
134    UInt32
135  #endif
136  CPpmd_State_Ref;
137
138typedef
139  #ifdef PPMD_32BIT
140    void *
141  #else
142    UInt32
143  #endif
144  CPpmd_Void_Ref;
145
146typedef
147  #ifdef PPMD_32BIT
148    Byte *
149  #else
150    UInt32
151  #endif
152  CPpmd_Byte_Ref;
153
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; }}
157
158#endif
159