MCPcopy Create free account
hub / github.com/creatale/node-dv / encodeLZ77

Function encodeLZ77

deps/lodepng/lodepng.cpp:1482–1657  ·  view source on GitHub ↗

LZ77-encode the data. Return value is error code. The input are raw bytes, the output is in the form of unsigned integers with codes representing for example literal bytes, or length/distance pairs. It uses a hash table technique to let it encode faster. When doing LZ77 encoding, a sliding window (of windowsize) is used, and all past bytes in that window can be used as the "dictionary". A brute fo

Source from the content-addressed store, hash-verified

1480this hash technique is one out of several ways to speed this up.
1481*/
1482static unsigned encodeLZ77(uivector* out, Hash* hash,
1483 const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize,
1484 unsigned minmatch, unsigned nicematch, unsigned lazymatching)
1485{
1486 size_t pos;
1487 unsigned i, error = 0;
1488 /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/
1489 unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8;
1490 unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64;
1491
1492 unsigned usezeros = 1; /*not sure if setting it to false for windowsize < 8192 is better or worse*/
1493 unsigned numzeros = 0;
1494
1495 unsigned offset; /*the offset represents the distance in LZ77 terminology*/
1496 unsigned length;
1497 unsigned lazy = 0;
1498 unsigned lazylength = 0, lazyoffset = 0;
1499 unsigned hashval;
1500 unsigned current_offset, current_length;
1501 unsigned prev_offset;
1502 const unsigned char *lastptr, *foreptr, *backptr;
1503 unsigned hashpos;
1504
1505 if(windowsize == 0 || windowsize > 32768) return 60; /*error: windowsize smaller/larger than allowed*/
1506 if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/
1507
1508 if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH;
1509
1510 for(pos = inpos; pos < insize; ++pos)
1511 {
1512 size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/
1513 unsigned chainlength = 0;
1514
1515 hashval = getHash(in, insize, pos);
1516
1517 if(usezeros && hashval == 0)
1518 {
1519 if(numzeros == 0) numzeros = countZeros(in, insize, pos);
1520 else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
1521 }
1522 else
1523 {
1524 numzeros = 0;
1525 }
1526
1527 updateHashChain(hash, wpos, hashval, numzeros);
1528
1529 /*the length and offset found for the current position*/
1530 length = 0;
1531 offset = 0;
1532
1533 hashpos = hash->chain[wpos];
1534
1535 lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];
1536
1537 /*search for the longest string*/
1538 prev_offset = 0;
1539 for(;;)

Callers 2

deflateDynamicFunction · 0.85
deflateFixedFunction · 0.85

Calls 5

getHashFunction · 0.85
countZerosFunction · 0.85
updateHashChainFunction · 0.85
uivector_push_backFunction · 0.85
addLengthDistanceFunction · 0.85

Tested by

no test coverage detected