MCPcopy Create free account
hub / github.com/HiLab-git/SimpleCRF / encodeLZ77

Function encodeLZ77

dependency/densecrf/examples/lodepng.cpp:1474–1649  ·  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 bru

Source from the content-addressed store, hash-verified

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