protected static uint LZ4_hashPosition(void *p, tableType_t tableType) { #if !BIT32 if (tableType != tableType_t.byU16) { return(LZ4_hash5(Mem.PeekW(p), tableType)); } #endif return(LZ4_hash4(Mem.Peek4(p), tableType)); }
protected static uint LZ4_count(byte *pIn, byte *pMatch, byte *pInLimit) { const int STEPSIZE = ALGORITHM_ARCH; var pStart = pIn; if (pIn < pInLimit - (STEPSIZE - 1)) { var diff = Mem.PeekW(pMatch) ^ Mem.PeekW(pIn); if (diff != 0) { return(LZ4_NbCommonBytes(diff)); } pIn += STEPSIZE; pMatch += STEPSIZE; } while (pIn < pInLimit - (STEPSIZE - 1)) { var diff = Mem.PeekW(pMatch) ^ Mem.PeekW(pIn); if (diff != 0) { return((uint)(pIn + LZ4_NbCommonBytes(diff) - pStart)); } pIn += STEPSIZE; pMatch += STEPSIZE; } #if !BIT32 if (pIn < pInLimit - 3 && Mem.Peek4(pMatch) == Mem.Peek4(pIn)) { pIn += 4; pMatch += 4; } #endif if (pIn < pInLimit - 1 && Mem.Peek2(pMatch) == Mem.Peek2(pIn)) { pIn += 2; pMatch += 2; } if (pIn < pInLimit && *pMatch == *pIn) { pIn++; } return((uint)(pIn - pStart)); }
protected static int LZ4_compress_generic( LZ4_stream_t *cctx, byte *source, byte *dest, int inputSize, int *inputConsumed, /* only written when outputDirective == fillOutput */ int maxOutputSize, limitedOutput_directive outputDirective, tableType_t tableType, dict_directive dictDirective, dictIssue_directive dictIssue, int acceleration) { int result; byte *ip = (byte *)source; uint startIndex = cctx->currentOffset; byte * @base = (byte *)source - startIndex; byte * lowLimit; LZ4_stream_t *dictCtx = (LZ4_stream_t *)cctx->dictCtx; byte * dictionary = dictDirective == dict_directive.usingDictCtx ? dictCtx->dictionary : cctx->dictionary; uint dictSize = dictDirective == dict_directive.usingDictCtx ? dictCtx->dictSize : cctx->dictSize; uint dictDelta = (dictDirective == dict_directive.usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; bool maybe_extMem = (dictDirective == dict_directive.usingExtDict) || (dictDirective == dict_directive.usingDictCtx); uint prefixIdxLimit = startIndex - dictSize; byte *dictEnd = dictionary + dictSize; byte *anchor = (byte *)source; byte *iend = ip + inputSize; byte *mflimitPlusOne = iend - MFLIMIT + 1; byte *matchlimit = iend - LASTLITERALS; /* the dictCtx currentOffset is indexed on the start of the dictionary, * while a dictionary in the current context precedes the currentOffset */ byte *dictBase = (dictDirective == dict_directive.usingDictCtx) ? dictionary + dictSize - dictCtx->currentOffset : dictionary + dictSize - startIndex; byte *op = (byte *)dest; byte *olimit = op + maxOutputSize; uint offset = 0; uint forwardH; if (outputDirective == limitedOutput_directive.fillOutput && maxOutputSize < 1) { return(0); } if ((uint)inputSize > (uint)LZ4_MAX_INPUT_SIZE) { return(0); } if ((tableType == tableType_t.byU16) && (inputSize >= LZ4_64Klimit)) { return(0); } if (tableType == tableType_t.byPtr) { Assert(dictDirective == dict_directive.noDict); } Assert(acceleration >= 1); lowLimit = (byte *)source - (dictDirective == dict_directive.withPrefix64k ? dictSize : 0); /* Update context state */ if (dictDirective == dict_directive.usingDictCtx) { /* Subsequent linked blocks can't use the dictionary. */ /* Instead, they use the block we just compressed. */ cctx->dictCtx = null; cctx->dictSize = (uint)inputSize; } else { cctx->dictSize += (uint)inputSize; } cctx->currentOffset += (uint)inputSize; cctx->tableType = tableType; if (inputSize < LZ4_minLength) { goto _last_literals; } /* First Byte */ LZ4_putPosition(ip, cctx->hashTable, tableType, @base); ip++; forwardH = LZ4_hashPosition(ip, tableType); /* Main Loop */ for (;;) { byte *match; byte *token; byte *filledIp; /* Find a match */ if (tableType == tableType_t.byPtr) { byte *forwardIp = ip; int step = 1; int searchMatchNb = acceleration << LZ4_skipTrigger; do { uint h = forwardH; ip = forwardIp; forwardIp += step; step = (searchMatchNb++ >> LZ4_skipTrigger); if ((forwardIp > mflimitPlusOne)) { goto _last_literals; } Assert(ip < mflimitPlusOne); match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, @base); forwardH = LZ4_hashPosition(forwardIp, tableType); LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, @base); }while ((match + LZ4_DISTANCE_MAX < ip) || (Mem.Peek4(match) != Mem.Peek4(ip))); } else { /* byU32, byU16 */ byte *forwardIp = ip; int step = 1; int searchMatchNb = acceleration << LZ4_skipTrigger; do { uint h = forwardH; uint current = (uint)(forwardIp - @base); uint matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); Assert(matchIndex <= current); Assert(forwardIp - @base < (2 * GB - 1)); ip = forwardIp; forwardIp += step; step = (searchMatchNb++ >> LZ4_skipTrigger); if ((forwardIp > mflimitPlusOne)) { goto _last_literals; } Assert(ip < mflimitPlusOne); if (dictDirective == dict_directive.usingDictCtx) { if (matchIndex < startIndex) { Assert(tableType == tableType_t.byU32); matchIndex = LZ4_getIndexOnHash( h, dictCtx->hashTable, tableType_t.byU32); match = dictBase + matchIndex; matchIndex += dictDelta; lowLimit = dictionary; } else { match = @base + matchIndex; lowLimit = (byte *)source; } } else if (dictDirective == dict_directive.usingExtDict) { if (matchIndex < startIndex) { Assert(startIndex - matchIndex >= MINMATCH); match = dictBase + matchIndex; lowLimit = dictionary; } else { match = @base + matchIndex; lowLimit = (byte *)source; } } else { match = @base + matchIndex; } forwardH = LZ4_hashPosition(forwardIp, tableType); LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); if ((dictIssue == dictIssue_directive.dictSmall) && (matchIndex < prefixIdxLimit)) { continue; } Assert(matchIndex < current); if (((tableType != tableType_t.byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX)) && (matchIndex + LZ4_DISTANCE_MAX < current)) { continue; } Assert((current - matchIndex) <= LZ4_DISTANCE_MAX); if (Mem.Peek4(match) == Mem.Peek4(ip)) { if (maybe_extMem) { offset = current - matchIndex; } break; } }while (true); } filledIp = ip; while (((ip > anchor) & (match > lowLimit)) && ((ip[-1] == match[-1]))) { ip--; match--; } { var litLength = (uint)(ip - anchor); token = op++; if ((outputDirective == limitedOutput_directive.limitedOutput) && ((op + litLength + (2 + 1 + LASTLITERALS) + (litLength / 255) > olimit))) { return(0); } if ((outputDirective == limitedOutput_directive.fillOutput) && ((op + (litLength + 240) / 255 + litLength + 2 + 1 + MFLIMIT - MINMATCH > olimit))) { op--; goto _last_literals; } if (litLength >= RUN_MASK) { int len = (int)(litLength - RUN_MASK); *token = (byte)(RUN_MASK << ML_BITS); for (; len >= 255; len -= 255) { *op++ = 255; } *op++ = (byte)len; } else { *token = (byte)(litLength << ML_BITS); } Mem.WildCopy8(op, anchor, op + litLength); op += litLength; } _next_match: /* at this stage, the following variables must be correctly set : * - ip : at start of LZ operation * - match : at start of previous pattern occurence; can be within current prefix, or within extDict * - offset : if maybe_ext_memSegment==1 (constant) * - lowLimit : must be == dictionary to mean "match is within extDict"; must be == source otherwise * - token and *token : position to write 4-bits for match length; higher 4-bits for literal length supposed already written */ if ((outputDirective == limitedOutput_directive.fillOutput) && (op + 2 + 1 + MFLIMIT - MINMATCH > olimit)) { /* the match was too close to the end, rewind and go to last literals */ op = token; goto _last_literals; } /* Encode Offset */ if (maybe_extMem) { /* static test */ Assert(offset <= LZ4_DISTANCE_MAX && offset > 0); Mem.Poke2(op, (ushort)offset); op += 2; } else { Assert(ip - match <= LZ4_DISTANCE_MAX); Mem.Poke2(op, (ushort)(ip - match)); op += 2; } /* Encode MatchLength */ { uint matchCode; if ((dictDirective == dict_directive.usingExtDict || dictDirective == dict_directive.usingDictCtx) && (lowLimit == dictionary) /* match within extDict */) { byte *limit = ip + (dictEnd - match); Assert(dictEnd > match); if (limit > matchlimit) { limit = matchlimit; } matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, limit); ip += (uint)matchCode + MINMATCH; if (ip == limit) { uint more = LZ4_count(limit, (byte *)source, matchlimit); matchCode += more; ip += more; } } else { matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, matchlimit); ip += (uint)matchCode + MINMATCH; } if ((outputDirective != 0) && ((op + (1 + LASTLITERALS) + (matchCode + 240) / 255 > olimit))) { if (outputDirective == limitedOutput_directive.fillOutput) { /* Match description too long : reduce it */ uint newMatchCode = 15 - 1 + ((uint)(olimit - op) - 1 - LASTLITERALS) * 255; ip -= matchCode - newMatchCode; Assert(newMatchCode < matchCode); matchCode = newMatchCode; if ((ip <= filledIp)) { /* We have already filled up to filledIp so if ip ends up less than filledIp * we have positions in the hash table beyond the current position. This is * a problem if we reuse the hash table. So we have to remove these positions * from the hash table. */ byte *ptr; for (ptr = ip; ptr <= filledIp; ++ptr) { uint h = LZ4_hashPosition(ptr, tableType); LZ4_clearHash(h, cctx->hashTable, tableType); } } } else { Assert(outputDirective == limitedOutput_directive.limitedOutput); return(0); } } if (matchCode >= ML_MASK) { *token += (byte)ML_MASK; //!!! matchCode -= ML_MASK; Mem.Poke4(op, 0xFFFFFFFF); while (matchCode >= 4 * 255) { op += 4; Mem.Poke4(op, 0xFFFFFFFF); matchCode -= 4 * 255; } op += matchCode / 255; *op++ = (byte)(matchCode % 255); } else { *token += (byte)(matchCode); } } /* Ensure we have enough space for the last literals. */ Assert( !(outputDirective == limitedOutput_directive.fillOutput && op + 1 + LASTLITERALS > olimit)); anchor = ip; /* Test end of chunk */ if (ip >= mflimitPlusOne) { break; } /* Fill table */ LZ4_putPosition(ip - 2, cctx->hashTable, tableType, @base); /* Test next position */ if (tableType == tableType_t.byPtr) { match = LZ4_getPosition(ip, cctx->hashTable, tableType, @base); LZ4_putPosition(ip, cctx->hashTable, tableType, @base); if ((match + LZ4_DISTANCE_MAX >= ip) && (Mem.Peek4(match) == Mem.Peek4(ip))) { token = op++; *token = 0; goto _next_match; } } else { /* byU32, byU16 */ uint h = LZ4_hashPosition(ip, tableType); uint current = (uint)(ip - @base); uint matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); Assert(matchIndex < current); if (dictDirective == dict_directive.usingDictCtx) { if (matchIndex < startIndex) { matchIndex = LZ4_getIndexOnHash( h, dictCtx->hashTable, tableType_t.byU32); match = dictBase + matchIndex; lowLimit = dictionary; matchIndex += dictDelta; } else { match = @base + matchIndex; lowLimit = (byte *)source; } } else if (dictDirective == dict_directive.usingExtDict) { if (matchIndex < startIndex) { match = dictBase + matchIndex; lowLimit = dictionary; } else { match = @base + matchIndex; lowLimit = (byte *)source; } } else { match = @base + matchIndex; } LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); Assert(matchIndex < current); if (((dictIssue != dictIssue_directive.dictSmall) || (matchIndex >= prefixIdxLimit)) && (((tableType == tableType_t.byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) || (matchIndex + LZ4_DISTANCE_MAX >= current)) && (Mem.Peek4(match) == Mem.Peek4(ip))) { token = op++; *token = 0; if (maybe_extMem) { offset = current - matchIndex; } goto _next_match; } } forwardH = LZ4_hashPosition(++ip, tableType); } _last_literals: { var lastRun = (size_t)(iend - anchor); if ((outputDirective != 0) && (op + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > olimit)) { if (outputDirective == limitedOutput_directive.fillOutput) { Assert(olimit >= op); lastRun = (size_t)(olimit - op) - 1; lastRun -= (lastRun + 240) / 255; } else { Assert(outputDirective == limitedOutput_directive.limitedOutput); return(0); } } if (lastRun >= RUN_MASK) { var accumulator = (size_t)(lastRun - RUN_MASK); *op++ = (byte)(RUN_MASK << ML_BITS); for (; accumulator >= 255; accumulator -= 255) { *op++ = 255; } *op++ = (byte)accumulator; } else { *op++ = (byte)(lastRun << ML_BITS); } Mem.Copy(op, anchor, (int)lastRun); ip = anchor + lastRun; op += lastRun; } if (outputDirective == limitedOutput_directive.fillOutput) { *inputConsumed = (int)(((byte *)ip) - source); } result = (int)(((byte *)op) - dest); Assert(result > 0); return(result); }