...

Source file src/github.com/xi2/xz/dec_delta.go

Documentation: github.com/xi2/xz

     1  /*
     2   * Delta decoder
     3   *
     4   * Author: Lasse Collin <lasse.collin@tukaani.org>
     5   *
     6   * Translation to Go: Michael Cross <https://github.com/xi2>
     7   *
     8   * This file has been put into the public domain.
     9   * You can do whatever you want with this file.
    10   */
    11  
    12  package xz
    13  
    14  type xzDecDelta struct {
    15  	delta    [256]byte
    16  	pos      byte
    17  	distance int // in range [1, 256]
    18  }
    19  
    20  /*
    21   * Decode raw stream which has a delta filter as the first filter.
    22   */
    23  func xzDecDeltaRun(s *xzDecDelta, b *xzBuf, chain func(*xzBuf) xzRet) xzRet {
    24  	outStart := b.outPos
    25  	ret := chain(b)
    26  	for i := outStart; i < b.outPos; i++ {
    27  		tmp := b.out[i] + s.delta[byte(s.distance+int(s.pos))]
    28  		s.delta[s.pos] = tmp
    29  		b.out[i] = tmp
    30  		s.pos--
    31  	}
    32  	return ret
    33  }
    34  
    35  /*
    36   * Allocate memory for a delta decoder. xzDecDeltaReset must be used
    37   * before calling xzDecDeltaRun.
    38   */
    39  func xzDecDeltaCreate() *xzDecDelta {
    40  	return new(xzDecDelta)
    41  }
    42  
    43  /*
    44   * Returns xzOK if the given distance is valid. Otherwise
    45   * xzOptionsError is returned.
    46   */
    47  func xzDecDeltaReset(s *xzDecDelta, distance int) xzRet {
    48  	if distance < 1 || distance > 256 {
    49  		return xzOptionsError
    50  	}
    51  	s.delta = [256]byte{}
    52  	s.pos = 0
    53  	s.distance = distance
    54  	return xzOK
    55  }
    56  

View as plain text