本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.9KB

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