decode.go 6.8 KB

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