decode.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. )
  18. // DecodedLen returns the length of the decoded block.
  19. func DecodedLen(src []byte) (int, error) {
  20. v, _, err := decodedLen(src)
  21. return v, err
  22. }
  23. // decodedLen returns the length of the decoded block and the number of bytes
  24. // that the length header occupied.
  25. func decodedLen(src []byte) (blockLen, headerLen int, err error) {
  26. v, n := binary.Uvarint(src)
  27. if n <= 0 || v > 0xffffffff {
  28. return 0, 0, ErrCorrupt
  29. }
  30. const wordSize = 32 << (^uint(0) >> 32 & 1)
  31. if wordSize == 32 && v > 0x7fffffff {
  32. return 0, 0, ErrTooLarge
  33. }
  34. return int(v), n, nil
  35. }
  36. // Decode returns the decoded form of src. The returned slice may be a sub-
  37. // slice of dst if dst was large enough to hold the entire decoded block.
  38. // Otherwise, a newly allocated slice will be returned.
  39. // It is valid to pass a nil dst.
  40. func Decode(dst, src []byte) ([]byte, error) {
  41. dLen, s, err := decodedLen(src)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if len(dst) < dLen {
  46. dst = make([]byte, dLen)
  47. }
  48. var d, offset, length int
  49. for s < len(src) {
  50. switch src[s] & 0x03 {
  51. case tagLiteral:
  52. x := uint(src[s] >> 2)
  53. switch {
  54. case x < 60:
  55. s++
  56. case x == 60:
  57. s += 2
  58. if s > len(src) {
  59. return nil, ErrCorrupt
  60. }
  61. x = uint(src[s-1])
  62. case x == 61:
  63. s += 3
  64. if s > len(src) {
  65. return nil, ErrCorrupt
  66. }
  67. x = uint(src[s-2]) | uint(src[s-1])<<8
  68. case x == 62:
  69. s += 4
  70. if s > len(src) {
  71. return nil, ErrCorrupt
  72. }
  73. x = uint(src[s-3]) | uint(src[s-2])<<8 | uint(src[s-1])<<16
  74. case x == 63:
  75. s += 5
  76. if s > len(src) {
  77. return nil, ErrCorrupt
  78. }
  79. x = uint(src[s-4]) | uint(src[s-3])<<8 | uint(src[s-2])<<16 | uint(src[s-1])<<24
  80. }
  81. length = int(x + 1)
  82. if length <= 0 {
  83. return nil, errors.New("snappy: unsupported literal length")
  84. }
  85. if length > len(dst)-d || length > len(src)-s {
  86. return nil, ErrCorrupt
  87. }
  88. copy(dst[d:], src[s:s+length])
  89. d += length
  90. s += length
  91. continue
  92. case tagCopy1:
  93. s += 2
  94. if s > len(src) {
  95. return nil, ErrCorrupt
  96. }
  97. length = 4 + int(src[s-2])>>2&0x7
  98. offset = int(src[s-2])&0xe0<<3 | int(src[s-1])
  99. case tagCopy2:
  100. s += 3
  101. if s > len(src) {
  102. return nil, ErrCorrupt
  103. }
  104. length = 1 + int(src[s-3])>>2
  105. offset = int(src[s-2]) | int(src[s-1])<<8
  106. case tagCopy4:
  107. return nil, errors.New("snappy: unsupported COPY_4 tag")
  108. }
  109. end := d + length
  110. if offset > d || end > len(dst) {
  111. return nil, ErrCorrupt
  112. }
  113. for ; d < end; d++ {
  114. dst[d] = dst[d-offset]
  115. }
  116. }
  117. if d != dLen {
  118. return nil, ErrCorrupt
  119. }
  120. return dst[:d], nil
  121. }
  122. // NewReader returns a new Reader that decompresses from r, using the framing
  123. // format described at
  124. // https://github.com/google/snappy/blob/master/framing_format.txt
  125. func NewReader(r io.Reader) *Reader {
  126. return &Reader{
  127. r: r,
  128. decoded: make([]byte, maxUncompressedChunkLen),
  129. buf: make([]byte, MaxEncodedLen(maxUncompressedChunkLen)+checksumSize),
  130. }
  131. }
  132. // Reader is an io.Reader that can read Snappy-compressed bytes.
  133. type Reader struct {
  134. r io.Reader
  135. err error
  136. decoded []byte
  137. buf []byte
  138. // decoded[i:j] contains decoded bytes that have not yet been passed on.
  139. i, j int
  140. readHeader bool
  141. }
  142. // Reset discards any buffered data, resets all state, and switches the Snappy
  143. // reader to read from r. This permits reusing a Reader rather than allocating
  144. // a new one.
  145. func (r *Reader) Reset(reader io.Reader) {
  146. r.r = reader
  147. r.err = nil
  148. r.i = 0
  149. r.j = 0
  150. r.readHeader = false
  151. }
  152. func (r *Reader) readFull(p []byte) (ok bool) {
  153. if _, r.err = io.ReadFull(r.r, p); r.err != nil {
  154. if r.err == io.ErrUnexpectedEOF {
  155. r.err = ErrCorrupt
  156. }
  157. return false
  158. }
  159. return true
  160. }
  161. // Read satisfies the io.Reader interface.
  162. func (r *Reader) Read(p []byte) (int, error) {
  163. if r.err != nil {
  164. return 0, r.err
  165. }
  166. for {
  167. if r.i < r.j {
  168. n := copy(p, r.decoded[r.i:r.j])
  169. r.i += n
  170. return n, nil
  171. }
  172. if !r.readFull(r.buf[:4]) {
  173. return 0, r.err
  174. }
  175. chunkType := r.buf[0]
  176. if !r.readHeader {
  177. if chunkType != chunkTypeStreamIdentifier {
  178. r.err = ErrCorrupt
  179. return 0, r.err
  180. }
  181. r.readHeader = true
  182. }
  183. chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
  184. if chunkLen > len(r.buf) {
  185. r.err = ErrUnsupported
  186. return 0, r.err
  187. }
  188. // The chunk types are specified at
  189. // https://github.com/google/snappy/blob/master/framing_format.txt
  190. switch chunkType {
  191. case chunkTypeCompressedData:
  192. // Section 4.2. Compressed data (chunk type 0x00).
  193. if chunkLen < checksumSize {
  194. r.err = ErrCorrupt
  195. return 0, r.err
  196. }
  197. buf := r.buf[:chunkLen]
  198. if !r.readFull(buf) {
  199. return 0, r.err
  200. }
  201. checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
  202. buf = buf[checksumSize:]
  203. n, err := DecodedLen(buf)
  204. if err != nil {
  205. r.err = err
  206. return 0, r.err
  207. }
  208. if n > len(r.decoded) {
  209. r.err = ErrCorrupt
  210. return 0, r.err
  211. }
  212. if _, err := Decode(r.decoded, buf); err != nil {
  213. r.err = err
  214. return 0, r.err
  215. }
  216. if crc(r.decoded[:n]) != checksum {
  217. r.err = ErrCorrupt
  218. return 0, r.err
  219. }
  220. r.i, r.j = 0, n
  221. continue
  222. case chunkTypeUncompressedData:
  223. // Section 4.3. Uncompressed data (chunk type 0x01).
  224. if chunkLen < checksumSize {
  225. r.err = ErrCorrupt
  226. return 0, r.err
  227. }
  228. buf := r.buf[:checksumSize]
  229. if !r.readFull(buf) {
  230. return 0, r.err
  231. }
  232. checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
  233. // Read directly into r.decoded instead of via r.buf.
  234. n := chunkLen - checksumSize
  235. if !r.readFull(r.decoded[:n]) {
  236. return 0, r.err
  237. }
  238. if crc(r.decoded[:n]) != checksum {
  239. r.err = ErrCorrupt
  240. return 0, r.err
  241. }
  242. r.i, r.j = 0, n
  243. continue
  244. case chunkTypeStreamIdentifier:
  245. // Section 4.1. Stream identifier (chunk type 0xff).
  246. if chunkLen != len(magicBody) {
  247. r.err = ErrCorrupt
  248. return 0, r.err
  249. }
  250. if !r.readFull(r.buf[:len(magicBody)]) {
  251. return 0, r.err
  252. }
  253. for i := 0; i < len(magicBody); i++ {
  254. if r.buf[i] != magicBody[i] {
  255. r.err = ErrCorrupt
  256. return 0, r.err
  257. }
  258. }
  259. continue
  260. }
  261. if chunkType <= 0x7f {
  262. // Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f).
  263. r.err = ErrUnsupported
  264. return 0, r.err
  265. }
  266. // Section 4.4 Padding (chunk type 0xfe).
  267. // Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd).
  268. if !r.readFull(r.buf[:chunkLen]) {
  269. return 0, r.err
  270. }
  271. }
  272. }