reader.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "encoding/binary"
  28. "errors"
  29. "io"
  30. )
  31. var (
  32. // ErrCorrupt indicates the input was corrupt
  33. ErrCorrupt = errors.New("corrupt input")
  34. )
  35. const (
  36. mlBits = 4
  37. mlMask = (1 << mlBits) - 1
  38. runBits = 8 - mlBits
  39. runMask = (1 << runBits) - 1
  40. )
  41. type decoder struct {
  42. src []byte
  43. dst []byte
  44. spos uint32
  45. dpos uint32
  46. ref uint32
  47. }
  48. func (d *decoder) readByte() (uint8, error) {
  49. if int(d.spos) == len(d.src) {
  50. return 0, io.EOF
  51. }
  52. b := d.src[d.spos]
  53. d.spos++
  54. return b, nil
  55. }
  56. func (d *decoder) getLen() (uint32, error) {
  57. length := uint32(0)
  58. ln, err := d.readByte()
  59. if err != nil {
  60. return 0, ErrCorrupt
  61. }
  62. for ln == 255 {
  63. length += 255
  64. ln, err = d.readByte()
  65. if err != nil {
  66. return 0, ErrCorrupt
  67. }
  68. }
  69. length += uint32(ln)
  70. return length, nil
  71. }
  72. func (d *decoder) cp(length, decr uint32) {
  73. if int(d.ref+length) < int(d.dpos) {
  74. copy(d.dst[d.dpos:], d.dst[d.ref:d.ref+length])
  75. } else {
  76. for ii := uint32(0); ii < length; ii++ {
  77. d.dst[d.dpos+ii] = d.dst[d.ref+ii]
  78. }
  79. }
  80. d.dpos += length
  81. d.ref += length - decr
  82. }
  83. func (d *decoder) finish(err error) error {
  84. if err == io.EOF {
  85. return nil
  86. }
  87. return err
  88. }
  89. // Decode returns the decoded form of src. The returned slice may be a
  90. // subslice of dst if it was large enough to hold the entire decoded block.
  91. func Decode(dst, src []byte) ([]byte, error) {
  92. if len(src) < 4 {
  93. return nil, ErrCorrupt
  94. }
  95. uncompressedLen := binary.LittleEndian.Uint32(src)
  96. if uncompressedLen == 0 {
  97. return nil, nil
  98. }
  99. if uncompressedLen > MaxInputSize {
  100. return nil, ErrTooLarge
  101. }
  102. if dst == nil || len(dst) < int(uncompressedLen) {
  103. dst = make([]byte, uncompressedLen)
  104. }
  105. d := decoder{src: src, dst: dst[:uncompressedLen], spos: 4}
  106. decr := []uint32{0, 3, 2, 3}
  107. for {
  108. code, err := d.readByte()
  109. if err != nil {
  110. return d.dst, d.finish(err)
  111. }
  112. length := uint32(code >> mlBits)
  113. if length == runMask {
  114. ln, err := d.getLen()
  115. if err != nil {
  116. return nil, ErrCorrupt
  117. }
  118. length += ln
  119. }
  120. if int(d.spos+length) > len(d.src) || int(d.dpos+length) > len(d.dst) {
  121. return nil, ErrCorrupt
  122. }
  123. for ii := uint32(0); ii < length; ii++ {
  124. d.dst[d.dpos+ii] = d.src[d.spos+ii]
  125. }
  126. d.spos += length
  127. d.dpos += length
  128. if int(d.spos) == len(d.src) {
  129. return d.dst, nil
  130. }
  131. if int(d.spos+2) >= len(d.src) {
  132. return nil, ErrCorrupt
  133. }
  134. back := uint32(d.src[d.spos]) | uint32(d.src[d.spos+1])<<8
  135. if back > d.dpos {
  136. return nil, ErrCorrupt
  137. }
  138. d.spos += 2
  139. d.ref = d.dpos - back
  140. length = uint32(code & mlMask)
  141. if length == mlMask {
  142. ln, err := d.getLen()
  143. if err != nil {
  144. return nil, ErrCorrupt
  145. }
  146. length += ln
  147. }
  148. literal := d.dpos - d.ref
  149. if literal < 4 {
  150. if int(d.dpos+4) > len(d.dst) {
  151. return nil, ErrCorrupt
  152. }
  153. d.cp(4, decr[literal])
  154. } else {
  155. length += 4
  156. }
  157. if d.dpos+length > uncompressedLen {
  158. return nil, ErrCorrupt
  159. }
  160. d.cp(length, 0)
  161. }
  162. }