f535537f |
1 | /* LzFind.h -- Match finder for LZ algorithms |
2 | 2024-01-22 : Igor Pavlov : Public domain */ |
3 | |
4 | #ifndef ZIP7_INC_LZ_FIND_H |
5 | #define ZIP7_INC_LZ_FIND_H |
6 | |
7 | #include "7zTypes.h" |
8 | |
9 | EXTERN_C_BEGIN |
10 | |
11 | typedef UInt32 CLzRef; |
12 | |
13 | typedef struct |
14 | { |
15 | const Byte *buffer; |
16 | UInt32 pos; |
17 | UInt32 posLimit; |
18 | UInt32 streamPos; /* wrap over Zero is allowed (streamPos < pos). Use (UInt32)(streamPos - pos) */ |
19 | UInt32 lenLimit; |
20 | |
21 | UInt32 cyclicBufferPos; |
22 | UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ |
23 | |
24 | Byte streamEndWasReached; |
25 | Byte btMode; |
26 | Byte bigHash; |
27 | Byte directInput; |
28 | |
29 | UInt32 matchMaxLen; |
30 | CLzRef *hash; |
31 | CLzRef *son; |
32 | UInt32 hashMask; |
33 | UInt32 cutValue; |
34 | |
35 | Byte *bufBase; |
36 | ISeqInStreamPtr stream; |
37 | |
38 | UInt32 blockSize; |
39 | UInt32 keepSizeBefore; |
40 | UInt32 keepSizeAfter; |
41 | |
42 | UInt32 numHashBytes; |
43 | size_t directInputRem; |
44 | UInt32 historySize; |
45 | UInt32 fixedHashSize; |
46 | Byte numHashBytes_Min; |
47 | Byte numHashOutBits; |
48 | Byte _pad2_[2]; |
49 | SRes result; |
50 | UInt32 crc[256]; |
51 | size_t numRefs; |
52 | |
53 | UInt64 expectedDataSize; |
54 | } CMatchFinder; |
55 | |
56 | #define Inline_MatchFinder_GetPointerToCurrentPos(p) ((const Byte *)(p)->buffer) |
57 | |
58 | #define Inline_MatchFinder_GetNumAvailableBytes(p) ((UInt32)((p)->streamPos - (p)->pos)) |
59 | |
60 | /* |
61 | #define Inline_MatchFinder_IsFinishedOK(p) \ |
62 | ((p)->streamEndWasReached \ |
63 | && (p)->streamPos == (p)->pos \ |
64 | && (!(p)->directInput || (p)->directInputRem == 0)) |
65 | */ |
66 | |
67 | int MatchFinder_NeedMove(CMatchFinder *p); |
68 | /* Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p); */ |
69 | void MatchFinder_MoveBlock(CMatchFinder *p); |
70 | void MatchFinder_ReadIfRequired(CMatchFinder *p); |
71 | |
72 | void MatchFinder_Construct(CMatchFinder *p); |
73 | |
74 | /* (directInput = 0) is default value. |
75 | It's required to provide correct (directInput) value |
76 | before calling MatchFinder_Create(). |
77 | You can set (directInput) by any of the following calls: |
78 | - MatchFinder_SET_DIRECT_INPUT_BUF() |
79 | - MatchFinder_SET_STREAM() |
80 | - MatchFinder_SET_STREAM_MODE() |
81 | */ |
82 | |
83 | #define MatchFinder_SET_DIRECT_INPUT_BUF(p, _src_, _srcLen_) { \ |
84 | (p)->stream = NULL; \ |
85 | (p)->directInput = 1; \ |
86 | (p)->buffer = (_src_); \ |
87 | (p)->directInputRem = (_srcLen_); } |
88 | |
89 | /* |
90 | #define MatchFinder_SET_STREAM_MODE(p) { \ |
91 | (p)->directInput = 0; } |
92 | */ |
93 | |
94 | #define MatchFinder_SET_STREAM(p, _stream_) { \ |
95 | (p)->stream = _stream_; \ |
96 | (p)->directInput = 0; } |
97 | |
98 | |
99 | int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, |
100 | UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, |
101 | ISzAllocPtr alloc); |
102 | void MatchFinder_Free(CMatchFinder *p, ISzAllocPtr alloc); |
103 | void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, size_t numItems); |
104 | |
105 | /* |
106 | #define MatchFinder_INIT_POS(p, val) \ |
107 | (p)->pos = (val); \ |
108 | (p)->streamPos = (val); |
109 | */ |
110 | |
111 | // void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue); |
112 | #define MatchFinder_REDUCE_OFFSETS(p, subValue) \ |
113 | (p)->pos -= (subValue); \ |
114 | (p)->streamPos -= (subValue); |
115 | |
116 | |
117 | UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son, |
118 | size_t _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, |
119 | UInt32 *distances, UInt32 maxLen); |
120 | |
121 | /* |
122 | Conditions: |
123 | Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func. |
124 | Mf_GetPointerToCurrentPos_Func's result must be used only before any other function |
125 | */ |
126 | |
127 | typedef void (*Mf_Init_Func)(void *object); |
128 | typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object); |
129 | typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object); |
130 | typedef UInt32 * (*Mf_GetMatches_Func)(void *object, UInt32 *distances); |
131 | typedef void (*Mf_Skip_Func)(void *object, UInt32); |
132 | |
133 | typedef struct |
134 | { |
135 | Mf_Init_Func Init; |
136 | Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; |
137 | Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos; |
138 | Mf_GetMatches_Func GetMatches; |
139 | Mf_Skip_Func Skip; |
140 | } IMatchFinder2; |
141 | |
142 | void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder2 *vTable); |
143 | |
144 | void MatchFinder_Init_LowHash(CMatchFinder *p); |
145 | void MatchFinder_Init_HighHash(CMatchFinder *p); |
146 | void MatchFinder_Init_4(CMatchFinder *p); |
147 | // void MatchFinder_Init(CMatchFinder *p); |
148 | void MatchFinder_Init(void *p); |
149 | |
150 | UInt32* Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); |
151 | UInt32* Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); |
152 | |
153 | void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); |
154 | void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); |
155 | |
156 | void LzFindPrepare(void); |
157 | |
158 | EXTERN_C_END |
159 | |
160 | #endif |