decode.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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) io.Reader {
  123. return &reader{
  124. r: r,
  125. decoded: make([]byte, maxUncompressedChunkLen),
  126. buf: make([]byte, MaxEncodedLen(maxUncompressedChunkLen)+checksumSize),
  127. }
  128. }
  129. type reader struct {
  130. r io.Reader
  131. err error
  132. decoded []byte
  133. buf []byte
  134. // decoded[i:j] contains decoded bytes that have not yet been passed on.
  135. i, j int
  136. readHeader bool
  137. }
  138. func (r *reader) readFull(p []byte) (ok bool) {
  139. if _, r.err = io.ReadFull(r.r, p); r.err != nil {
  140. if r.err == io.ErrUnexpectedEOF {
  141. r.err = ErrCorrupt
  142. }
  143. return false
  144. }
  145. return true
  146. }
  147. func (r *reader) Read(p []byte) (int, error) {
  148. if r.err != nil {
  149. return 0, r.err
  150. }
  151. for {
  152. if r.i < r.j {
  153. n := copy(p, r.decoded[r.i:r.j])
  154. r.i += n
  155. return n, nil
  156. }
  157. if !r.readFull(r.buf[:4]) {
  158. return 0, r.err
  159. }
  160. chunkType := r.buf[0]
  161. if !r.readHeader {
  162. if chunkType != chunkTypeStreamIdentifier {
  163. r.err = ErrCorrupt
  164. return 0, r.err
  165. }
  166. r.readHeader = true
  167. }
  168. chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
  169. if chunkLen > len(r.buf) {
  170. r.err = ErrUnsupported
  171. return 0, r.err
  172. }
  173. // The chunk types are specified at
  174. // https://code.google.com/p/snappy/source/browse/trunk/framing_format.txt
  175. switch chunkType {
  176. case chunkTypeCompressedData:
  177. // Section 4.2. Compressed data (chunk type 0x00).
  178. if chunkLen < checksumSize {
  179. r.err = ErrCorrupt
  180. return 0, r.err
  181. }
  182. buf := r.buf[:chunkLen]
  183. if !r.readFull(buf) {
  184. return 0, r.err
  185. }
  186. checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
  187. buf = buf[checksumSize:]
  188. n, err := DecodedLen(buf)
  189. if err != nil {
  190. r.err = err
  191. return 0, r.err
  192. }
  193. if n > len(r.decoded) {
  194. r.err = ErrCorrupt
  195. return 0, r.err
  196. }
  197. if _, err := Decode(r.decoded, buf); err != nil {
  198. r.err = err
  199. return 0, r.err
  200. }
  201. if crc(r.decoded[:n]) != checksum {
  202. r.err = ErrCorrupt
  203. return 0, r.err
  204. }
  205. r.i, r.j = 0, n
  206. continue
  207. case chunkTypeUncompressedData:
  208. // Section 4.3. Uncompressed data (chunk type 0x01).
  209. if chunkLen < checksumSize {
  210. r.err = ErrCorrupt
  211. return 0, r.err
  212. }
  213. buf := r.buf[:checksumSize]
  214. if !r.readFull(buf) {
  215. return 0, r.err
  216. }
  217. checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
  218. // Read directly into r.decoded instead of via r.buf.
  219. n := chunkLen - checksumSize
  220. if !r.readFull(r.decoded[:n]) {
  221. return 0, r.err
  222. }
  223. if crc(r.decoded[:n]) != checksum {
  224. r.err = ErrCorrupt
  225. return 0, r.err
  226. }
  227. r.i, r.j = 0, n
  228. continue
  229. case chunkTypeStreamIdentifier:
  230. // Section 4.1. Stream identifier (chunk type 0xff).
  231. if chunkLen != len(magicBody) {
  232. r.err = ErrCorrupt
  233. return 0, r.err
  234. }
  235. if !r.readFull(r.buf[:len(magicBody)]) {
  236. return 0, r.err
  237. }
  238. for i := 0; i < len(magicBody); i++ {
  239. if r.buf[i] != magicBody[i] {
  240. r.err = ErrCorrupt
  241. return 0, r.err
  242. }
  243. }
  244. continue
  245. }
  246. if chunkType <= 0x7f {
  247. // Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f).
  248. r.err = ErrUnsupported
  249. return 0, r.err
  250. } else {
  251. // Section 4.4 Padding (chunk type 0xfe).
  252. // Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd).
  253. if !r.readFull(r.buf[:chunkLen]) {
  254. return 0, r.err
  255. }
  256. }
  257. }
  258. }