/* LzFind.c -- Match finder for LZ algorithms\r
-2017-06-10 : Igor Pavlov : Public domain */\r
+2018-07-08 : Igor Pavlov : Public domain */\r
\r
#include "Precomp.h"\r
\r
\r
void MatchFinder_Construct(CMatchFinder *p)\r
{\r
- UInt32 i;\r
+ unsigned i;\r
p->bufferBase = NULL;\r
p->directInput = 0;\r
p->hash = NULL;\r
\r
for (i = 0; i < 256; i++)\r
{\r
- UInt32 r = i;\r
+ UInt32 r = (UInt32)i;\r
unsigned j;\r
for (j = 0; j < 8; j++)\r
r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1)));\r
MatchFinder_ReduceOffsets(p, subValue);\r
}\r
\r
+\r
+MY_NO_INLINE\r
static void MatchFinder_CheckLimits(CMatchFinder *p)\r
{\r
if (p->pos == kMaxValForNormalize)\r
MatchFinder_SetLimits(p);\r
}\r
\r
-static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
+\r
+/*\r
+ (lenLimit > maxLen)\r
+*/\r
+MY_FORCE_INLINE\r
+static UInt32 * Hc_GetMatchesSpec(unsigned lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,\r
- UInt32 *distances, UInt32 maxLen)\r
+ UInt32 *distances, unsigned maxLen)\r
{\r
+ /*\r
son[_cyclicBufferPos] = curMatch;\r
for (;;)\r
{\r
break;\r
if (maxLen < len)\r
{\r
- *distances++ = maxLen = len;\r
+ maxLen = len;\r
+ *distances++ = len;\r
*distances++ = delta - 1;\r
if (len == lenLimit)\r
return distances;\r
}\r
}\r
}\r
+ */\r
+\r
+ const Byte *lim = cur + lenLimit;\r
+ son[_cyclicBufferPos] = curMatch;\r
+ do\r
+ {\r
+ UInt32 delta = pos - curMatch;\r
+ if (delta >= _cyclicBufferSize)\r
+ break;\r
+ {\r
+ ptrdiff_t diff;\r
+ curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];\r
+ diff = (ptrdiff_t)0 - delta;\r
+ if (cur[maxLen] == cur[maxLen + diff])\r
+ {\r
+ const Byte *c = cur;\r
+ while (*c == c[diff])\r
+ {\r
+ if (++c == lim)\r
+ {\r
+ distances[0] = (UInt32)(lim - cur);\r
+ distances[1] = delta - 1;\r
+ return distances + 2;\r
+ }\r
+ }\r
+ {\r
+ unsigned len = (unsigned)(c - cur);\r
+ if (maxLen < len)\r
+ {\r
+ maxLen = len;\r
+ distances[0] = (UInt32)len;\r
+ distances[1] = delta - 1;\r
+ distances += 2;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ while (--cutValue);\r
+\r
+ return distances;\r
}\r
\r
+\r
+MY_FORCE_INLINE\r
UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,\r
UInt32 *distances, UInt32 maxLen)\r
{\r
- CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;\r
- CLzRef *ptr1 = son + (_cyclicBufferPos << 1);\r
- UInt32 len0 = 0, len1 = 0;\r
+ CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;\r
+ CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);\r
+ unsigned len0 = 0, len1 = 0;\r
for (;;)\r
{\r
UInt32 delta = pos - curMatch;\r
return distances;\r
}\r
{\r
- CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
+ CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
const Byte *pb = cur - delta;\r
- UInt32 len = (len0 < len1 ? len0 : len1);\r
+ unsigned len = (len0 < len1 ? len0 : len1);\r
+ UInt32 pair0 = pair[0];\r
if (pb[len] == cur[len])\r
{\r
if (++len != lenLimit && pb[len] == cur[len])\r
break;\r
if (maxLen < len)\r
{\r
- *distances++ = maxLen = len;\r
+ maxLen = (UInt32)len;\r
+ *distances++ = (UInt32)len;\r
*distances++ = delta - 1;\r
if (len == lenLimit)\r
{\r
- *ptr1 = pair[0];\r
+ *ptr1 = pair0;\r
*ptr0 = pair[1];\r
return distances;\r
}\r
static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)\r
{\r
- CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;\r
- CLzRef *ptr1 = son + (_cyclicBufferPos << 1);\r
- UInt32 len0 = 0, len1 = 0;\r
+ CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;\r
+ CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);\r
+ unsigned len0 = 0, len1 = 0;\r
for (;;)\r
{\r
UInt32 delta = pos - curMatch;\r
return;\r
}\r
{\r
- CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
+ CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
const Byte *pb = cur - delta;\r
- UInt32 len = (len0 < len1 ? len0 : len1);\r
+ unsigned len = (len0 < len1 ? len0 : len1);\r
if (pb[len] == cur[len])\r
{\r
while (++len != lenLimit)\r
p->buffer++; \\r
if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);\r
\r
-#define MOVE_POS_RET MOVE_POS return offset;\r
+#define MOVE_POS_RET MOVE_POS return (UInt32)offset;\r
\r
static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }\r
\r
#define GET_MATCHES_HEADER2(minLen, ret_op) \\r
- UInt32 lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \\r
- lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \\r
+ unsigned lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \\r
+ lenLimit = (unsigned)p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \\r
cur = p->buffer;\r
\r
#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)\r
#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue\r
\r
#define GET_MATCHES_FOOTER(offset, maxLen) \\r
- offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \\r
- distances + offset, maxLen) - distances); MOVE_POS_RET;\r
+ offset = (unsigned)(GetMatchesSpec1((UInt32)lenLimit, curMatch, MF_PARAMS(p), \\r
+ distances + offset, (UInt32)maxLen) - distances); MOVE_POS_RET;\r
\r
#define SKIP_FOOTER \\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;\r
+ SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;\r
\r
#define UPDATE_maxLen { \\r
ptrdiff_t diff = (ptrdiff_t)0 - d2; \\r
const Byte *c = cur + maxLen; \\r
const Byte *lim = cur + lenLimit; \\r
for (; c != lim; c++) if (*(c + diff) != *c) break; \\r
- maxLen = (UInt32)(c - cur); }\r
+ maxLen = (unsigned)(c - cur); }\r
\r
static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 offset;\r
+ unsigned offset;\r
GET_MATCHES_HEADER(2)\r
HASH2_CALC;\r
curMatch = p->hash[hv];\r
\r
UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 offset;\r
+ unsigned offset;\r
GET_MATCHES_HEADER(3)\r
HASH_ZIP_CALC;\r
curMatch = p->hash[hv];\r
\r
static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 h2, d2, maxLen, offset, pos;\r
+ UInt32 h2, d2, pos;\r
+ unsigned maxLen, offset;\r
UInt32 *hash;\r
GET_MATCHES_HEADER(3)\r
\r
if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
{\r
UPDATE_maxLen\r
- distances[0] = maxLen;\r
+ distances[0] = (UInt32)maxLen;\r
distances[1] = d2 - 1;\r
offset = 2;\r
if (maxLen == lenLimit)\r
{\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
+ SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));\r
MOVE_POS_RET;\r
}\r
}\r
\r
static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
+ UInt32 h2, h3, d2, d3, pos;\r
+ unsigned maxLen, offset;\r
UInt32 *hash;\r
GET_MATCHES_HEADER(4)\r
\r
\r
if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
{\r
- distances[0] = maxLen = 2;\r
+ maxLen = 2;\r
+ distances[0] = 2;\r
distances[1] = d2 - 1;\r
offset = 2;\r
}\r
if (offset != 0)\r
{\r
UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
+ distances[(size_t)offset - 2] = (UInt32)maxLen;\r
if (maxLen == lenLimit)\r
{\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
+ SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));\r
MOVE_POS_RET;\r
}\r
}\r
\r
static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
+ UInt32 h2, h3, d2, d3, pos;\r
+ unsigned maxLen, offset;\r
UInt32 *hash;\r
GET_MATCHES_HEADER(4)\r
\r
\r
if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
{\r
- distances[0] = maxLen = 2;\r
+ maxLen = 2;\r
+ distances[0] = 2;\r
distances[1] = d2 - 1;\r
offset = 2;\r
}\r
if (offset != 0)\r
{\r
UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
+ distances[(size_t)offset - 2] = (UInt32)maxLen;\r
if (maxLen == lenLimit)\r
{\r
p->son[p->cyclicBufferPos] = curMatch;\r
if (maxLen < 3)\r
maxLen = 3;\r
\r
- offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
+ offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
distances + offset, maxLen) - (distances));\r
MOVE_POS_RET\r
}\r
\r
UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 offset;\r
+ unsigned offset;\r
GET_MATCHES_HEADER(3)\r
HASH_ZIP_CALC;\r
curMatch = p->hash[hv];\r
p->hash[hv] = p->pos;\r
- offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
+ offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
distances, 2) - (distances));\r
MOVE_POS_RET\r
}\r
/* LzmaDec.c -- LZMA Decoder\r
-2018-02-28 : Igor Pavlov : Public domain */\r
+2018-07-04 : Igor Pavlov : Public domain */\r
\r
#include "Precomp.h"\r
\r
-/* #include "CpuArch.h" */\r
-#include "LzmaDec.h"\r
-\r
#ifndef EFIAPI\r
#include <string.h>\r
#endif\r
\r
+/* #include "CpuArch.h" */\r
+#include "LzmaDec.h"\r
+\r
#define kNumTopBits 24\r
#define kTopValue ((UInt32)1 << kNumTopBits)\r
\r
\r
#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }\r
\r
-#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)\r
+#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound)\r
#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));\r
#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));\r
#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \\r
\r
#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }\r
\r
-#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)\r
+#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound)\r
#define UPDATE_0_CHECK range = bound;\r
#define UPDATE_1_CHECK range -= bound; code -= bound;\r
#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \\r
curLen = ((rem < len) ? (unsigned)rem : len);\r
pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0);\r
\r
- processedPos += curLen;\r
+ processedPos += (UInt32)curLen;\r
\r
len -= curLen;\r
if (curLen <= dicBufSize - pos)\r
Byte *dest = dic + dicPos;\r
ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;\r
const Byte *lim = dest + curLen;\r
- dicPos += curLen;\r
+ dicPos += (SizeT)curLen;\r
do\r
*(dest) = (Byte)*(dest + src);\r
while (++dest != lim);\r
p->buf = buf;\r
p->range = range;\r
p->code = code;\r
- p->remainLen = len;\r
+ p->remainLen = (UInt32)len;\r
p->dicPos = dicPos;\r
p->processedPos = processedPos;\r
p->reps[0] = rep0;\r
p->reps[1] = rep1;\r
p->reps[2] = rep2;\r
p->reps[3] = rep3;\r
- p->state = state;\r
+ p->state = (UInt32)state;\r
\r
return SZ_OK;\r
}\r
if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)\r
p->checkDicSize = p->prop.dicSize;\r
\r
- p->processedPos += len;\r
- p->remainLen -= len;\r
+ p->processedPos += (UInt32)len;\r
+ p->remainLen -= (UInt32)len;\r
while (len != 0)\r
{\r
len--;\r
}\r
\r
\r
-void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)\r
+void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState)\r
{\r
p->remainLen = kMatchSpecLenStart + 1;\r
p->tempBufSize = 0;\r
p->tempBufSize = rem;\r
if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)\r
{\r
- int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);\r
+ int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, (SizeT)rem);\r
if (dummyRes == DUMMY_ERROR)\r
{\r
- (*srcLen) += lookAhead;\r
+ (*srcLen) += (SizeT)lookAhead;\r
*status = LZMA_STATUS_NEEDS_MORE_INPUT;\r
return SZ_OK;\r
}\r
return SZ_ERROR_FAIL; /* some internal error */\r
lookAhead -= rem;\r
}\r
- (*srcLen) += lookAhead;\r
+ (*srcLen) += (SizeT)lookAhead;\r
src += lookAhead;\r
- inSize -= lookAhead;\r
+ inSize -= (SizeT)lookAhead;\r
p->tempBufSize = 0;\r
}\r
}\r