reader.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this
  8. * list of conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  17. * SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  22. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package lz4
  26. import (
  27. "errors"
  28. "io"
  29. )
  30. var (
  31. // ErrCorrupt indicates the input was corrupt
  32. ErrCorrupt = errors.New("corrupt input")
  33. )
  34. const (
  35. mlBits = 4
  36. mlMask = (1 << mlBits) - 1
  37. runBits = 8 - mlBits
  38. runMask = (1 << runBits) - 1
  39. )
  40. type decoder struct {
  41. src []byte
  42. dst []byte
  43. spos uint32
  44. dpos uint32
  45. ref uint32
  46. }
  47. func (d *decoder) readByte() (uint8, error) {
  48. if int(d.spos) == len(d.src) {
  49. return 0, io.EOF
  50. }
  51. b := d.src[d.spos]
  52. d.spos++
  53. return b, nil
  54. }
  55. func (d *decoder) getLen() (uint32, error) {
  56. length := uint32(0)
  57. ln, err := d.readByte()
  58. if err != nil {
  59. return 0, ErrCorrupt
  60. }
  61. for ln == 255 {
  62. length += 255
  63. ln, err = d.readByte()
  64. if err != nil {
  65. return 0, ErrCorrupt
  66. }
  67. }
  68. length += uint32(ln)
  69. return length, nil
  70. }
  71. func (d *decoder) cp(length, decr uint32) {
  72. if int(d.ref+length) < int(d.dpos) {
  73. copy(d.dst[d.dpos:], d.dst[d.ref:d.ref+length])
  74. } else {
  75. for ii := uint32(0); ii < length; ii++ {
  76. d.dst[d.dpos+ii] = d.dst[d.ref+ii]
  77. }
  78. }
  79. d.dpos += length
  80. d.ref += length - decr
  81. }
  82. func (d *decoder) finish(err error) error {
  83. if err == io.EOF {
  84. return nil
  85. }
  86. return err
  87. }
  88. // Decode returns the decoded form of src. The returned slice may be a
  89. // subslice of dst if it was large enough to hold the entire decoded block.
  90. func Decode(dst, src []byte) (int, error) {
  91. d := decoder{src: src, dst: dst, spos: 0}
  92. decr := []uint32{0, 3, 2, 3}
  93. for {
  94. code, err := d.readByte()
  95. if err != nil {
  96. return len(d.dst), d.finish(err)
  97. }
  98. length := uint32(code >> mlBits)
  99. if length == runMask {
  100. ln, err := d.getLen()
  101. if err != nil {
  102. return 0, ErrCorrupt
  103. }
  104. length += ln
  105. }
  106. if int(d.spos+length) > len(d.src) || int(d.dpos+length) > len(d.dst) {
  107. return 0, ErrCorrupt
  108. }
  109. for ii := uint32(0); ii < length; ii++ {
  110. d.dst[d.dpos+ii] = d.src[d.spos+ii]
  111. }
  112. d.spos += length
  113. d.dpos += length
  114. if int(d.spos) == len(d.src) {
  115. return len(d.dst), nil
  116. }
  117. if int(d.spos+2) >= len(d.src) {
  118. return 0, ErrCorrupt
  119. }
  120. back := uint32(d.src[d.spos]) | uint32(d.src[d.spos+1])<<8
  121. if back > d.dpos {
  122. return 0, ErrCorrupt
  123. }
  124. d.spos += 2
  125. d.ref = d.dpos - back
  126. length = uint32(code & mlMask)
  127. if length == mlMask {
  128. ln, err := d.getLen()
  129. if err != nil {
  130. return 0, ErrCorrupt
  131. }
  132. length += ln
  133. }
  134. literal := d.dpos - d.ref
  135. if literal < 4 {
  136. if int(d.dpos+4) > len(d.dst) {
  137. return 0, ErrCorrupt
  138. }
  139. d.cp(4, decr[literal])
  140. } else {
  141. length += 4
  142. }
  143. if int(d.dpos+length) > len(d.dst) {
  144. return 0, ErrCorrupt
  145. }
  146. d.cp(length, 0)
  147. }
  148. }