archive_ppmd_private.h 3.3 KB

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