framedec.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Copyright 2019+ Klaus Post. All rights reserved.
  2. // License information can be found in the LICENSE file.
  3. // Based on work by Yann Collet, released under BSD License.
  4. package zstd
  5. import (
  6. "encoding/binary"
  7. "encoding/hex"
  8. "errors"
  9. "io"
  10. "github.com/klauspost/compress/zstd/internal/xxhash"
  11. )
  12. type frameDec struct {
  13. o decoderOptions
  14. crc *xxhash.Digest
  15. WindowSize uint64
  16. // Frame history passed between blocks
  17. history history
  18. rawInput byteBuffer
  19. // Byte buffer that can be reused for small input blocks.
  20. bBuf byteBuf
  21. FrameContentSize uint64
  22. DictionaryID uint32
  23. HasCheckSum bool
  24. SingleSegment bool
  25. }
  26. const (
  27. // MinWindowSize is the minimum Window Size, which is 1 KB.
  28. MinWindowSize = 1 << 10
  29. // MaxWindowSize is the maximum encoder window size
  30. // and the default decoder maximum window size.
  31. MaxWindowSize = 1 << 29
  32. )
  33. const (
  34. frameMagic = "\x28\xb5\x2f\xfd"
  35. skippableFrameMagic = "\x2a\x4d\x18"
  36. )
  37. func newFrameDec(o decoderOptions) *frameDec {
  38. if o.maxWindowSize > o.maxDecodedSize {
  39. o.maxWindowSize = o.maxDecodedSize
  40. }
  41. d := frameDec{
  42. o: o,
  43. }
  44. return &d
  45. }
  46. // reset will read the frame header and prepare for block decoding.
  47. // If nothing can be read from the input, io.EOF will be returned.
  48. // Any other error indicated that the stream contained data, but
  49. // there was a problem.
  50. func (d *frameDec) reset(br byteBuffer) error {
  51. d.HasCheckSum = false
  52. d.WindowSize = 0
  53. var signature [4]byte
  54. for {
  55. var err error
  56. // Check if we can read more...
  57. b, err := br.readSmall(1)
  58. switch err {
  59. case io.EOF, io.ErrUnexpectedEOF:
  60. return io.EOF
  61. case nil:
  62. signature[0] = b[0]
  63. default:
  64. return err
  65. }
  66. // Read the rest, don't allow io.ErrUnexpectedEOF
  67. b, err = br.readSmall(3)
  68. switch err {
  69. case io.EOF:
  70. return io.EOF
  71. case nil:
  72. copy(signature[1:], b)
  73. default:
  74. return err
  75. }
  76. if string(signature[1:4]) != skippableFrameMagic || signature[0]&0xf0 != 0x50 {
  77. if debugDecoder {
  78. println("Not skippable", hex.EncodeToString(signature[:]), hex.EncodeToString([]byte(skippableFrameMagic)))
  79. }
  80. // Break if not skippable frame.
  81. break
  82. }
  83. // Read size to skip
  84. b, err = br.readSmall(4)
  85. if err != nil {
  86. if debugDecoder {
  87. println("Reading Frame Size", err)
  88. }
  89. return err
  90. }
  91. n := uint32(b[0]) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)
  92. println("Skipping frame with", n, "bytes.")
  93. err = br.skipN(int64(n))
  94. if err != nil {
  95. if debugDecoder {
  96. println("Reading discarded frame", err)
  97. }
  98. return err
  99. }
  100. }
  101. if string(signature[:]) != frameMagic {
  102. if debugDecoder {
  103. println("Got magic numbers: ", signature, "want:", []byte(frameMagic))
  104. }
  105. return ErrMagicMismatch
  106. }
  107. // Read Frame_Header_Descriptor
  108. fhd, err := br.readByte()
  109. if err != nil {
  110. if debugDecoder {
  111. println("Reading Frame_Header_Descriptor", err)
  112. }
  113. return err
  114. }
  115. d.SingleSegment = fhd&(1<<5) != 0
  116. if fhd&(1<<3) != 0 {
  117. return errors.New("reserved bit set on frame header")
  118. }
  119. // Read Window_Descriptor
  120. // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#window_descriptor
  121. d.WindowSize = 0
  122. if !d.SingleSegment {
  123. wd, err := br.readByte()
  124. if err != nil {
  125. if debugDecoder {
  126. println("Reading Window_Descriptor", err)
  127. }
  128. return err
  129. }
  130. printf("raw: %x, mantissa: %d, exponent: %d\n", wd, wd&7, wd>>3)
  131. windowLog := 10 + (wd >> 3)
  132. windowBase := uint64(1) << windowLog
  133. windowAdd := (windowBase / 8) * uint64(wd&0x7)
  134. d.WindowSize = windowBase + windowAdd
  135. }
  136. // Read Dictionary_ID
  137. // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#dictionary_id
  138. d.DictionaryID = 0
  139. if size := fhd & 3; size != 0 {
  140. if size == 3 {
  141. size = 4
  142. }
  143. b, err := br.readSmall(int(size))
  144. if err != nil {
  145. println("Reading Dictionary_ID", err)
  146. return err
  147. }
  148. var id uint32
  149. switch len(b) {
  150. case 1:
  151. id = uint32(b[0])
  152. case 2:
  153. id = uint32(b[0]) | (uint32(b[1]) << 8)
  154. case 4:
  155. id = uint32(b[0]) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)
  156. }
  157. if debugDecoder {
  158. println("Dict size", size, "ID:", id)
  159. }
  160. d.DictionaryID = id
  161. }
  162. // Read Frame_Content_Size
  163. // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#frame_content_size
  164. var fcsSize int
  165. v := fhd >> 6
  166. switch v {
  167. case 0:
  168. if d.SingleSegment {
  169. fcsSize = 1
  170. }
  171. default:
  172. fcsSize = 1 << v
  173. }
  174. d.FrameContentSize = fcsUnknown
  175. if fcsSize > 0 {
  176. b, err := br.readSmall(fcsSize)
  177. if err != nil {
  178. println("Reading Frame content", err)
  179. return err
  180. }
  181. switch len(b) {
  182. case 1:
  183. d.FrameContentSize = uint64(b[0])
  184. case 2:
  185. // When FCS_Field_Size is 2, the offset of 256 is added.
  186. d.FrameContentSize = uint64(b[0]) | (uint64(b[1]) << 8) + 256
  187. case 4:
  188. d.FrameContentSize = uint64(b[0]) | (uint64(b[1]) << 8) | (uint64(b[2]) << 16) | (uint64(b[3]) << 24)
  189. case 8:
  190. d1 := uint32(b[0]) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)
  191. d2 := uint32(b[4]) | (uint32(b[5]) << 8) | (uint32(b[6]) << 16) | (uint32(b[7]) << 24)
  192. d.FrameContentSize = uint64(d1) | (uint64(d2) << 32)
  193. }
  194. if debugDecoder {
  195. println("Read FCS:", d.FrameContentSize)
  196. }
  197. }
  198. // Move this to shared.
  199. d.HasCheckSum = fhd&(1<<2) != 0
  200. if d.HasCheckSum {
  201. if d.crc == nil {
  202. d.crc = xxhash.New()
  203. }
  204. d.crc.Reset()
  205. }
  206. if d.WindowSize > d.o.maxWindowSize {
  207. if debugDecoder {
  208. printf("window size %d > max %d\n", d.WindowSize, d.o.maxWindowSize)
  209. }
  210. return ErrWindowSizeExceeded
  211. }
  212. if d.WindowSize == 0 && d.SingleSegment {
  213. // We may not need window in this case.
  214. d.WindowSize = d.FrameContentSize
  215. if d.WindowSize < MinWindowSize {
  216. d.WindowSize = MinWindowSize
  217. }
  218. if d.WindowSize > d.o.maxDecodedSize {
  219. if debugDecoder {
  220. printf("window size %d > max %d\n", d.WindowSize, d.o.maxWindowSize)
  221. }
  222. return ErrDecoderSizeExceeded
  223. }
  224. }
  225. // The minimum Window_Size is 1 KB.
  226. if d.WindowSize < MinWindowSize {
  227. if debugDecoder {
  228. println("got window size: ", d.WindowSize)
  229. }
  230. return ErrWindowSizeTooSmall
  231. }
  232. d.history.windowSize = int(d.WindowSize)
  233. if !d.o.lowMem || d.history.windowSize < maxBlockSize {
  234. // Alloc 2x window size if not low-mem, or window size below 2MB.
  235. d.history.allocFrameBuffer = d.history.windowSize * 2
  236. } else {
  237. if d.o.lowMem {
  238. // Alloc with 1MB extra.
  239. d.history.allocFrameBuffer = d.history.windowSize + maxBlockSize/2
  240. } else {
  241. // Alloc with 2MB extra.
  242. d.history.allocFrameBuffer = d.history.windowSize + maxBlockSize
  243. }
  244. }
  245. if debugDecoder {
  246. println("Frame: Dict:", d.DictionaryID, "FrameContentSize:", d.FrameContentSize, "singleseg:", d.SingleSegment, "window:", d.WindowSize, "crc:", d.HasCheckSum)
  247. }
  248. // history contains input - maybe we do something
  249. d.rawInput = br
  250. return nil
  251. }
  252. // next will start decoding the next block from stream.
  253. func (d *frameDec) next(block *blockDec) error {
  254. if debugDecoder {
  255. println("decoding new block")
  256. }
  257. err := block.reset(d.rawInput, d.WindowSize)
  258. if err != nil {
  259. println("block error:", err)
  260. // Signal the frame decoder we have a problem.
  261. block.sendErr(err)
  262. return err
  263. }
  264. return nil
  265. }
  266. // checkCRC will check the checksum, assuming the frame has one.
  267. // Will return ErrCRCMismatch if crc check failed, otherwise nil.
  268. func (d *frameDec) checkCRC() error {
  269. // We can overwrite upper tmp now
  270. buf, err := d.rawInput.readSmall(4)
  271. if err != nil {
  272. println("CRC missing?", err)
  273. return err
  274. }
  275. want := binary.LittleEndian.Uint32(buf[:4])
  276. got := uint32(d.crc.Sum64())
  277. if got != want {
  278. if debugDecoder {
  279. printf("CRC check failed: got %08x, want %08x\n", got, want)
  280. }
  281. return ErrCRCMismatch
  282. }
  283. if debugDecoder {
  284. printf("CRC ok %08x\n", got)
  285. }
  286. return nil
  287. }
  288. // consumeCRC skips over the checksum, assuming the frame has one.
  289. func (d *frameDec) consumeCRC() error {
  290. _, err := d.rawInput.readSmall(4)
  291. if err != nil {
  292. println("CRC missing?", err)
  293. }
  294. return err
  295. }
  296. // runDecoder will run the decoder for the remainder of the frame.
  297. func (d *frameDec) runDecoder(dst []byte, dec *blockDec) ([]byte, error) {
  298. saved := d.history.b
  299. // We use the history for output to avoid copying it.
  300. d.history.b = dst
  301. d.history.ignoreBuffer = len(dst)
  302. // Store input length, so we only check new data.
  303. crcStart := len(dst)
  304. d.history.decoders.maxSyncLen = 0
  305. if d.o.limitToCap {
  306. d.history.decoders.maxSyncLen = uint64(cap(dst) - len(dst))
  307. }
  308. if d.FrameContentSize != fcsUnknown {
  309. if !d.o.limitToCap || d.FrameContentSize+uint64(len(dst)) < d.history.decoders.maxSyncLen {
  310. d.history.decoders.maxSyncLen = d.FrameContentSize + uint64(len(dst))
  311. }
  312. if d.history.decoders.maxSyncLen > d.o.maxDecodedSize {
  313. if debugDecoder {
  314. println("maxSyncLen:", d.history.decoders.maxSyncLen, "> maxDecodedSize:", d.o.maxDecodedSize)
  315. }
  316. return dst, ErrDecoderSizeExceeded
  317. }
  318. if debugDecoder {
  319. println("maxSyncLen:", d.history.decoders.maxSyncLen)
  320. }
  321. if !d.o.limitToCap && uint64(cap(dst)) < d.history.decoders.maxSyncLen {
  322. // Alloc for output
  323. dst2 := make([]byte, len(dst), d.history.decoders.maxSyncLen+compressedBlockOverAlloc)
  324. copy(dst2, dst)
  325. dst = dst2
  326. }
  327. }
  328. var err error
  329. for {
  330. err = dec.reset(d.rawInput, d.WindowSize)
  331. if err != nil {
  332. break
  333. }
  334. if debugDecoder {
  335. println("next block:", dec)
  336. }
  337. err = dec.decodeBuf(&d.history)
  338. if err != nil {
  339. break
  340. }
  341. if uint64(len(d.history.b)-crcStart) > d.o.maxDecodedSize {
  342. println("runDecoder: maxDecodedSize exceeded", uint64(len(d.history.b)-crcStart), ">", d.o.maxDecodedSize)
  343. err = ErrDecoderSizeExceeded
  344. break
  345. }
  346. if d.o.limitToCap && len(d.history.b) > cap(dst) {
  347. println("runDecoder: cap exceeded", uint64(len(d.history.b)), ">", cap(dst))
  348. err = ErrDecoderSizeExceeded
  349. break
  350. }
  351. if uint64(len(d.history.b)-crcStart) > d.FrameContentSize {
  352. println("runDecoder: FrameContentSize exceeded", uint64(len(d.history.b)-crcStart), ">", d.FrameContentSize)
  353. err = ErrFrameSizeExceeded
  354. break
  355. }
  356. if dec.Last {
  357. break
  358. }
  359. if debugDecoder {
  360. println("runDecoder: FrameContentSize", uint64(len(d.history.b)-crcStart), "<=", d.FrameContentSize)
  361. }
  362. }
  363. dst = d.history.b
  364. if err == nil {
  365. if d.FrameContentSize != fcsUnknown && uint64(len(d.history.b)-crcStart) != d.FrameContentSize {
  366. err = ErrFrameSizeMismatch
  367. } else if d.HasCheckSum {
  368. if d.o.ignoreChecksum {
  369. err = d.consumeCRC()
  370. } else {
  371. d.crc.Write(dst[crcStart:])
  372. err = d.checkCRC()
  373. }
  374. }
  375. }
  376. d.history.b = saved
  377. return dst, err
  378. }