You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dec_delta.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. package xz
  12. type xzDecDelta struct {
  13. delta [256]byte
  14. pos byte
  15. distance int // in range [1, 256]
  16. }
  17. /*
  18. * Decode raw stream which has a delta filter as the first filter.
  19. */
  20. func xzDecDeltaRun(s *xzDecDelta, b *xzBuf, chain func(*xzBuf) xzRet) xzRet {
  21. outStart := b.outPos
  22. ret := chain(b)
  23. for i := outStart; i < b.outPos; i++ {
  24. tmp := b.out[i] + s.delta[byte(s.distance+int(s.pos))]
  25. s.delta[s.pos] = tmp
  26. b.out[i] = tmp
  27. s.pos--
  28. }
  29. return ret
  30. }
  31. /*
  32. * Allocate memory for a delta decoder. xzDecDeltaReset must be used
  33. * before calling xzDecDeltaRun.
  34. */
  35. func xzDecDeltaCreate() *xzDecDelta {
  36. return new(xzDecDelta)
  37. }
  38. /*
  39. * Returns xzOK if the given distance is valid. Otherwise
  40. * xzOptionsError is returned.
  41. */
  42. func xzDecDeltaReset(s *xzDecDelta, distance int) xzRet {
  43. if distance < 1 || distance > 256 {
  44. return xzOptionsError
  45. }
  46. s.delta = [256]byte{}
  47. s.pos = 0
  48. s.distance = distance
  49. return xzOK
  50. }