decode.go 7.3 KB

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