archive_ppmd_private.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. typedef struct
  55. {
  56. void *(*Alloc)(void *p, size_t size);
  57. void (*Free)(void *p, void *address); /* address can be 0 */
  58. } ISzAlloc;
  59. /*** End defined in Types.h ***/
  60. /*** Begin defined in CpuArch.h ***/
  61. #if defined(_M_IX86) || defined(__i386__)
  62. #define MY_CPU_X86
  63. #endif
  64. #if defined(MY_CPU_X86) || defined(_M_ARM)
  65. #define MY_CPU_32BIT
  66. #endif
  67. #ifdef MY_CPU_32BIT
  68. #define PPMD_32BIT
  69. #endif
  70. /*** End defined in CpuArch.h ***/
  71. #define PPMD_INT_BITS 7
  72. #define PPMD_PERIOD_BITS 7
  73. #define PPMD_BIN_SCALE (1 << (PPMD_INT_BITS + PPMD_PERIOD_BITS))
  74. #define PPMD_GET_MEAN_SPEC(summ, shift, round) (((summ) + (1 << ((shift) - (round)))) >> (shift))
  75. #define PPMD_GET_MEAN(summ) PPMD_GET_MEAN_SPEC((summ), PPMD_PERIOD_BITS, 2)
  76. #define PPMD_UPDATE_PROB_0(prob) ((prob) + (1 << PPMD_INT_BITS) - PPMD_GET_MEAN(prob))
  77. #define PPMD_UPDATE_PROB_1(prob) ((prob) - PPMD_GET_MEAN(prob))
  78. #define PPMD_N1 4
  79. #define PPMD_N2 4
  80. #define PPMD_N3 4
  81. #define PPMD_N4 ((128 + 3 - 1 * PPMD_N1 - 2 * PPMD_N2 - 3 * PPMD_N3) / 4)
  82. #define PPMD_NUM_INDEXES (PPMD_N1 + PPMD_N2 + PPMD_N3 + PPMD_N4)
  83. /* SEE-contexts for PPM-contexts with masked symbols */
  84. typedef struct
  85. {
  86. UInt16 Summ; /* Freq */
  87. Byte Shift; /* Speed of Freq change; low Shift is for fast change */
  88. Byte Count; /* Count to next change of Shift */
  89. } CPpmd_See;
  90. #define Ppmd_See_Update(p) if ((p)->Shift < PPMD_PERIOD_BITS && --(p)->Count == 0) \
  91. { (p)->Summ <<= 1; (p)->Count = (Byte)(3 << (p)->Shift++); }
  92. typedef struct
  93. {
  94. Byte Symbol;
  95. Byte Freq;
  96. UInt16 SuccessorLow;
  97. UInt16 SuccessorHigh;
  98. } CPpmd_State;
  99. typedef
  100. #ifdef PPMD_32BIT
  101. CPpmd_State *
  102. #else
  103. UInt32
  104. #endif
  105. CPpmd_State_Ref;
  106. typedef
  107. #ifdef PPMD_32BIT
  108. void *
  109. #else
  110. UInt32
  111. #endif
  112. CPpmd_Void_Ref;
  113. typedef
  114. #ifdef PPMD_32BIT
  115. Byte *
  116. #else
  117. UInt32
  118. #endif
  119. CPpmd_Byte_Ref;
  120. #define PPMD_SetAllBitsIn256Bytes(p) \
  121. { unsigned j; for (j = 0; j < 256 / sizeof(p[0]); j += 8) { \
  122. 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; }}
  123. #endif