decode.go 7.5 KB

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