decode.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2011 The Snappy-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package snappy
  5. import (
  6. "encoding/binary"
  7. "errors"
  8. "io"
  9. )
  10. var (
  11. // ErrCorrupt reports that the input is invalid.
  12. ErrCorrupt = errors.New("snappy: corrupt input")
  13. // ErrTooLarge reports that the uncompressed length is too large.
  14. ErrTooLarge = errors.New("snappy: decoded block is too large")
  15. // ErrUnsupported reports that the input isn't supported.
  16. ErrUnsupported = errors.New("snappy: unsupported input")
  17. errUnsupportedLiteralLength = errors.New("snappy: unsupported literal length")
  18. )
  19. // DecodedLen returns the length of the decoded block.
  20. func DecodedLen(src []byte) (int, error) {
  21. v, _, err := decodedLen(src)
  22. return v, err
  23. }
  24. // decodedLen returns the length of the decoded block and the number of bytes
  25. // that the length header occupied.
  26. func decodedLen(src []byte) (blockLen, headerLen int, err error) {
  27. v, n := binary.Uvarint(src)
  28. if n <= 0 || v > 0xffffffff {
  29. return 0, 0, ErrCorrupt
  30. }
  31. const wordSize = 32 << (^uint(0) >> 32 & 1)
  32. if wordSize == 32 && v > 0x7fffffff {
  33. return 0, 0, ErrTooLarge
  34. }
  35. return int(v), n, nil
  36. }
  37. const (
  38. decodeErrCodeCorrupt = 1
  39. decodeErrCodeUnsupportedLiteralLength = 2
  40. )
  41. // Decode returns the decoded form of src. The returned slice may be a sub-
  42. // slice of dst if dst was large enough to hold the entire decoded block.
  43. // Otherwise, a newly allocated slice will be returned.
  44. //
  45. // The dst and src must not overlap. It is valid to pass a nil dst.
  46. //
  47. // Decode handles the Snappy block format, not the Snappy stream format.
  48. func Decode(dst, src []byte) ([]byte, error) {
  49. dLen, s, err := decodedLen(src)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if dLen <= len(dst) {
  54. dst = dst[:dLen]
  55. } else {
  56. dst = make([]byte, dLen)
  57. }
  58. switch decode(dst, src[s:]) {
  59. case 0:
  60. return dst, nil
  61. case decodeErrCodeUnsupportedLiteralLength:
  62. return nil, errUnsupportedLiteralLength
  63. }
  64. return nil, ErrCorrupt
  65. }
  66. // NewReader returns a new Reader that decompresses from r, using the framing
  67. // format described at
  68. // https://github.com/google/snappy/blob/master/framing_format.txt
  69. func NewReader(r io.Reader) *Reader {
  70. return &Reader{
  71. r: r,
  72. decoded: make([]byte, maxBlockSize),
  73. buf: make([]byte, maxEncodedLenOfMaxBlockSize+checksumSize),
  74. }
  75. }
  76. // Reader is an io.Reader that can read Snappy-compressed bytes.
  77. //
  78. // Reader handles the Snappy stream format, not the Snappy block format.
  79. type Reader struct {
  80. r io.Reader
  81. err error
  82. decoded []byte
  83. buf []byte
  84. // decoded[i:j] contains decoded bytes that have not yet been passed on.
  85. i, j int
  86. readHeader bool
  87. }
  88. // Reset discards any buffered data, resets all state, and switches the Snappy
  89. // reader to read from r. This permits reusing a Reader rather than allocating
  90. // a new one.
  91. func (r *Reader) Reset(reader io.Reader) {
  92. r.r = reader
  93. r.err = nil
  94. r.i = 0
  95. r.j = 0
  96. r.readHeader = false
  97. }
  98. func (r *Reader) readFull(p []byte, allowEOF bool) (ok bool) {
  99. if _, r.err = io.ReadFull(r.r, p); r.err != nil {
  100. if r.err == io.ErrUnexpectedEOF || (r.err == io.EOF && !allowEOF) {
  101. r.err = ErrCorrupt
  102. }
  103. return false
  104. }
  105. return true
  106. }
  107. // Read satisfies the io.Reader interface.
  108. func (r *Reader) Read(p []byte) (int, error) {
  109. if r.err != nil {
  110. return 0, r.err
  111. }
  112. for {
  113. if r.i < r.j {
  114. n := copy(p, r.decoded[r.i:r.j])
  115. r.i += n
  116. return n, nil
  117. }
  118. if !r.readFull(r.buf[:4], true) {
  119. return 0, r.err
  120. }
  121. chunkType := r.buf[0]
  122. if !r.readHeader {
  123. if chunkType != chunkTypeStreamIdentifier {
  124. r.err = ErrCorrupt
  125. return 0, r.err
  126. }
  127. r.readHeader = true
  128. }
  129. chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
  130. if chunkLen > len(r.buf) {
  131. r.err = ErrUnsupported
  132. return 0, r.err
  133. }
  134. // The chunk types are specified at
  135. // https://github.com/google/snappy/blob/master/framing_format.txt
  136. switch chunkType {
  137. case chunkTypeCompressedData:
  138. // Section 4.2. Compressed data (chunk type 0x00).
  139. if chunkLen < checksumSize {
  140. r.err = ErrCorrupt
  141. return 0, r.err
  142. }
  143. buf := r.buf[:chunkLen]
  144. if !r.readFull(buf, false) {
  145. return 0, r.err
  146. }
  147. checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
  148. buf = buf[checksumSize:]
  149. n, err := DecodedLen(buf)
  150. if err != nil {
  151. r.err = err
  152. return 0, r.err
  153. }
  154. if n > len(r.decoded) {
  155. r.err = ErrCorrupt
  156. return 0, r.err
  157. }
  158. if _, err := Decode(r.decoded, buf); err != nil {
  159. r.err = err
  160. return 0, r.err
  161. }
  162. if crc(r.decoded[:n]) != checksum {
  163. r.err = ErrCorrupt
  164. return 0, r.err
  165. }
  166. r.i, r.j = 0, n
  167. continue
  168. case chunkTypeUncompressedData:
  169. // Section 4.3. Uncompressed data (chunk type 0x01).
  170. if chunkLen < checksumSize {
  171. r.err = ErrCorrupt
  172. return 0, r.err
  173. }
  174. buf := r.buf[:checksumSize]
  175. if !r.readFull(buf, false) {
  176. return 0, r.err
  177. }
  178. checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
  179. // Read directly into r.decoded instead of via r.buf.
  180. n := chunkLen - checksumSize
  181. if n > len(r.decoded) {
  182. r.err = ErrCorrupt
  183. return 0, r.err
  184. }
  185. if !r.readFull(r.decoded[:n], false) {
  186. return 0, r.err
  187. }
  188. if crc(r.decoded[:n]) != checksum {
  189. r.err = ErrCorrupt
  190. return 0, r.err
  191. }
  192. r.i, r.j = 0, n
  193. continue
  194. case chunkTypeStreamIdentifier:
  195. // Section 4.1. Stream identifier (chunk type 0xff).
  196. if chunkLen != len(magicBody) {
  197. r.err = ErrCorrupt
  198. return 0, r.err
  199. }
  200. if !r.readFull(r.buf[:len(magicBody)], false) {
  201. return 0, r.err
  202. }
  203. for i := 0; i < len(magicBody); i++ {
  204. if r.buf[i] != magicBody[i] {
  205. r.err = ErrCorrupt
  206. return 0, r.err
  207. }
  208. }
  209. continue
  210. }
  211. if chunkType <= 0x7f {
  212. // Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f).
  213. r.err = ErrUnsupported
  214. return 0, r.err
  215. }
  216. // Section 4.4 Padding (chunk type 0xfe).
  217. // Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd).
  218. if !r.readFull(r.buf[:chunkLen], false) {
  219. return 0, r.err
  220. }
  221. }
  222. }