blockdec.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. "errors"
  7. "fmt"
  8. "io"
  9. "sync"
  10. "github.com/klauspost/compress/huff0"
  11. "github.com/klauspost/compress/zstd/internal/xxhash"
  12. )
  13. type blockType uint8
  14. //go:generate stringer -type=blockType,literalsBlockType,seqCompMode,tableIndex
  15. const (
  16. blockTypeRaw blockType = iota
  17. blockTypeRLE
  18. blockTypeCompressed
  19. blockTypeReserved
  20. )
  21. type literalsBlockType uint8
  22. const (
  23. literalsBlockRaw literalsBlockType = iota
  24. literalsBlockRLE
  25. literalsBlockCompressed
  26. literalsBlockTreeless
  27. )
  28. const (
  29. // maxCompressedBlockSize is the biggest allowed compressed block size (128KB)
  30. maxCompressedBlockSize = 128 << 10
  31. // Maximum possible block size (all Raw+Uncompressed).
  32. maxBlockSize = (1 << 21) - 1
  33. // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#literals_section_header
  34. maxCompressedLiteralSize = 1 << 18
  35. maxRLELiteralSize = 1 << 20
  36. maxMatchLen = 131074
  37. maxSequences = 0x7f00 + 0xffff
  38. // We support slightly less than the reference decoder to be able to
  39. // use ints on 32 bit archs.
  40. maxOffsetBits = 30
  41. )
  42. var (
  43. huffDecoderPool = sync.Pool{New: func() interface{} {
  44. return &huff0.Scratch{}
  45. }}
  46. fseDecoderPool = sync.Pool{New: func() interface{} {
  47. return &fseDecoder{}
  48. }}
  49. )
  50. type blockDec struct {
  51. // Raw source data of the block.
  52. data []byte
  53. dataStorage []byte
  54. // Destination of the decoded data.
  55. dst []byte
  56. // Buffer for literals data.
  57. literalBuf []byte
  58. // Window size of the block.
  59. WindowSize uint64
  60. history chan *history
  61. input chan struct{}
  62. result chan decodeOutput
  63. sequenceBuf []seq
  64. err error
  65. decWG sync.WaitGroup
  66. // Frame to use for singlethreaded decoding.
  67. // Should not be used by the decoder itself since parent may be another frame.
  68. localFrame *frameDec
  69. // Block is RLE, this is the size.
  70. RLESize uint32
  71. tmp [4]byte
  72. Type blockType
  73. // Is this the last block of a frame?
  74. Last bool
  75. // Use less memory
  76. lowMem bool
  77. }
  78. func (b *blockDec) String() string {
  79. if b == nil {
  80. return "<nil>"
  81. }
  82. return fmt.Sprintf("Steam Size: %d, Type: %v, Last: %t, Window: %d", len(b.data), b.Type, b.Last, b.WindowSize)
  83. }
  84. func newBlockDec(lowMem bool) *blockDec {
  85. b := blockDec{
  86. lowMem: lowMem,
  87. result: make(chan decodeOutput, 1),
  88. input: make(chan struct{}, 1),
  89. history: make(chan *history, 1),
  90. }
  91. b.decWG.Add(1)
  92. go b.startDecoder()
  93. return &b
  94. }
  95. // reset will reset the block.
  96. // Input must be a start of a block and will be at the end of the block when returned.
  97. func (b *blockDec) reset(br byteBuffer, windowSize uint64) error {
  98. b.WindowSize = windowSize
  99. tmp, err := br.readSmall(3)
  100. if err != nil {
  101. println("Reading block header:", err)
  102. return err
  103. }
  104. bh := uint32(tmp[0]) | (uint32(tmp[1]) << 8) | (uint32(tmp[2]) << 16)
  105. b.Last = bh&1 != 0
  106. b.Type = blockType((bh >> 1) & 3)
  107. // find size.
  108. cSize := int(bh >> 3)
  109. maxSize := maxBlockSize
  110. switch b.Type {
  111. case blockTypeReserved:
  112. return ErrReservedBlockType
  113. case blockTypeRLE:
  114. b.RLESize = uint32(cSize)
  115. if b.lowMem {
  116. maxSize = cSize
  117. }
  118. cSize = 1
  119. case blockTypeCompressed:
  120. if debugDecoder {
  121. println("Data size on stream:", cSize)
  122. }
  123. b.RLESize = 0
  124. maxSize = maxCompressedBlockSize
  125. if windowSize < maxCompressedBlockSize && b.lowMem {
  126. maxSize = int(windowSize)
  127. }
  128. if cSize > maxCompressedBlockSize || uint64(cSize) > b.WindowSize {
  129. if debugDecoder {
  130. printf("compressed block too big: csize:%d block: %+v\n", uint64(cSize), b)
  131. }
  132. return ErrCompressedSizeTooBig
  133. }
  134. case blockTypeRaw:
  135. b.RLESize = 0
  136. // We do not need a destination for raw blocks.
  137. maxSize = -1
  138. default:
  139. panic("Invalid block type")
  140. }
  141. // Read block data.
  142. if cap(b.dataStorage) < cSize {
  143. if b.lowMem || cSize > maxCompressedBlockSize {
  144. b.dataStorage = make([]byte, 0, cSize)
  145. } else {
  146. b.dataStorage = make([]byte, 0, maxCompressedBlockSize)
  147. }
  148. }
  149. if cap(b.dst) <= maxSize {
  150. b.dst = make([]byte, 0, maxSize+1)
  151. }
  152. b.data, err = br.readBig(cSize, b.dataStorage)
  153. if err != nil {
  154. if debugDecoder {
  155. println("Reading block:", err, "(", cSize, ")", len(b.data))
  156. printf("%T", br)
  157. }
  158. return err
  159. }
  160. return nil
  161. }
  162. // sendEOF will make the decoder send EOF on this frame.
  163. func (b *blockDec) sendErr(err error) {
  164. b.Last = true
  165. b.Type = blockTypeReserved
  166. b.err = err
  167. b.input <- struct{}{}
  168. }
  169. // Close will release resources.
  170. // Closed blockDec cannot be reset.
  171. func (b *blockDec) Close() {
  172. close(b.input)
  173. close(b.history)
  174. close(b.result)
  175. b.decWG.Wait()
  176. }
  177. // decodeAsync will prepare decoding the block when it receives input.
  178. // This will separate output and history.
  179. func (b *blockDec) startDecoder() {
  180. defer b.decWG.Done()
  181. for range b.input {
  182. //println("blockDec: Got block input")
  183. switch b.Type {
  184. case blockTypeRLE:
  185. if cap(b.dst) < int(b.RLESize) {
  186. if b.lowMem {
  187. b.dst = make([]byte, b.RLESize)
  188. } else {
  189. b.dst = make([]byte, maxBlockSize)
  190. }
  191. }
  192. o := decodeOutput{
  193. d: b,
  194. b: b.dst[:b.RLESize],
  195. err: nil,
  196. }
  197. v := b.data[0]
  198. for i := range o.b {
  199. o.b[i] = v
  200. }
  201. hist := <-b.history
  202. hist.append(o.b)
  203. b.result <- o
  204. case blockTypeRaw:
  205. o := decodeOutput{
  206. d: b,
  207. b: b.data,
  208. err: nil,
  209. }
  210. hist := <-b.history
  211. hist.append(o.b)
  212. b.result <- o
  213. case blockTypeCompressed:
  214. b.dst = b.dst[:0]
  215. err := b.decodeCompressed(nil)
  216. o := decodeOutput{
  217. d: b,
  218. b: b.dst,
  219. err: err,
  220. }
  221. if debugDecoder {
  222. println("Decompressed to", len(b.dst), "bytes, error:", err)
  223. }
  224. b.result <- o
  225. case blockTypeReserved:
  226. // Used for returning errors.
  227. <-b.history
  228. b.result <- decodeOutput{
  229. d: b,
  230. b: nil,
  231. err: b.err,
  232. }
  233. default:
  234. panic("Invalid block type")
  235. }
  236. if debugDecoder {
  237. println("blockDec: Finished block")
  238. }
  239. }
  240. }
  241. // decodeAsync will prepare decoding the block when it receives the history.
  242. // If history is provided, it will not fetch it from the channel.
  243. func (b *blockDec) decodeBuf(hist *history) error {
  244. switch b.Type {
  245. case blockTypeRLE:
  246. if cap(b.dst) < int(b.RLESize) {
  247. if b.lowMem {
  248. b.dst = make([]byte, b.RLESize)
  249. } else {
  250. b.dst = make([]byte, maxBlockSize)
  251. }
  252. }
  253. b.dst = b.dst[:b.RLESize]
  254. v := b.data[0]
  255. for i := range b.dst {
  256. b.dst[i] = v
  257. }
  258. hist.appendKeep(b.dst)
  259. return nil
  260. case blockTypeRaw:
  261. hist.appendKeep(b.data)
  262. return nil
  263. case blockTypeCompressed:
  264. saved := b.dst
  265. b.dst = hist.b
  266. hist.b = nil
  267. err := b.decodeCompressed(hist)
  268. if debugDecoder {
  269. println("Decompressed to total", len(b.dst), "bytes, hash:", xxhash.Sum64(b.dst), "error:", err)
  270. }
  271. hist.b = b.dst
  272. b.dst = saved
  273. return err
  274. case blockTypeReserved:
  275. // Used for returning errors.
  276. return b.err
  277. default:
  278. panic("Invalid block type")
  279. }
  280. }
  281. // decodeCompressed will start decompressing a block.
  282. // If no history is supplied the decoder will decodeAsync as much as possible
  283. // before fetching from blockDec.history
  284. func (b *blockDec) decodeCompressed(hist *history) error {
  285. in := b.data
  286. delayedHistory := hist == nil
  287. if delayedHistory {
  288. // We must always grab history.
  289. defer func() {
  290. if hist == nil {
  291. <-b.history
  292. }
  293. }()
  294. }
  295. // There must be at least one byte for Literals_Block_Type and one for Sequences_Section_Header
  296. if len(in) < 2 {
  297. return ErrBlockTooSmall
  298. }
  299. litType := literalsBlockType(in[0] & 3)
  300. var litRegenSize int
  301. var litCompSize int
  302. sizeFormat := (in[0] >> 2) & 3
  303. var fourStreams bool
  304. switch litType {
  305. case literalsBlockRaw, literalsBlockRLE:
  306. switch sizeFormat {
  307. case 0, 2:
  308. // Regenerated_Size uses 5 bits (0-31). Literals_Section_Header uses 1 byte.
  309. litRegenSize = int(in[0] >> 3)
  310. in = in[1:]
  311. case 1:
  312. // Regenerated_Size uses 12 bits (0-4095). Literals_Section_Header uses 2 bytes.
  313. litRegenSize = int(in[0]>>4) + (int(in[1]) << 4)
  314. in = in[2:]
  315. case 3:
  316. // Regenerated_Size uses 20 bits (0-1048575). Literals_Section_Header uses 3 bytes.
  317. if len(in) < 3 {
  318. println("too small: litType:", litType, " sizeFormat", sizeFormat, len(in))
  319. return ErrBlockTooSmall
  320. }
  321. litRegenSize = int(in[0]>>4) + (int(in[1]) << 4) + (int(in[2]) << 12)
  322. in = in[3:]
  323. }
  324. case literalsBlockCompressed, literalsBlockTreeless:
  325. switch sizeFormat {
  326. case 0, 1:
  327. // Both Regenerated_Size and Compressed_Size use 10 bits (0-1023).
  328. if len(in) < 3 {
  329. println("too small: litType:", litType, " sizeFormat", sizeFormat, len(in))
  330. return ErrBlockTooSmall
  331. }
  332. n := uint64(in[0]>>4) + (uint64(in[1]) << 4) + (uint64(in[2]) << 12)
  333. litRegenSize = int(n & 1023)
  334. litCompSize = int(n >> 10)
  335. fourStreams = sizeFormat == 1
  336. in = in[3:]
  337. case 2:
  338. fourStreams = true
  339. if len(in) < 4 {
  340. println("too small: litType:", litType, " sizeFormat", sizeFormat, len(in))
  341. return ErrBlockTooSmall
  342. }
  343. n := uint64(in[0]>>4) + (uint64(in[1]) << 4) + (uint64(in[2]) << 12) + (uint64(in[3]) << 20)
  344. litRegenSize = int(n & 16383)
  345. litCompSize = int(n >> 14)
  346. in = in[4:]
  347. case 3:
  348. fourStreams = true
  349. if len(in) < 5 {
  350. println("too small: litType:", litType, " sizeFormat", sizeFormat, len(in))
  351. return ErrBlockTooSmall
  352. }
  353. n := uint64(in[0]>>4) + (uint64(in[1]) << 4) + (uint64(in[2]) << 12) + (uint64(in[3]) << 20) + (uint64(in[4]) << 28)
  354. litRegenSize = int(n & 262143)
  355. litCompSize = int(n >> 18)
  356. in = in[5:]
  357. }
  358. }
  359. if debugDecoder {
  360. println("literals type:", litType, "litRegenSize:", litRegenSize, "litCompSize:", litCompSize, "sizeFormat:", sizeFormat, "4X:", fourStreams)
  361. }
  362. var literals []byte
  363. var huff *huff0.Scratch
  364. switch litType {
  365. case literalsBlockRaw:
  366. if len(in) < litRegenSize {
  367. println("too small: litType:", litType, " sizeFormat", sizeFormat, "remain:", len(in), "want:", litRegenSize)
  368. return ErrBlockTooSmall
  369. }
  370. literals = in[:litRegenSize]
  371. in = in[litRegenSize:]
  372. //printf("Found %d uncompressed literals\n", litRegenSize)
  373. case literalsBlockRLE:
  374. if len(in) < 1 {
  375. println("too small: litType:", litType, " sizeFormat", sizeFormat, "remain:", len(in), "want:", 1)
  376. return ErrBlockTooSmall
  377. }
  378. if cap(b.literalBuf) < litRegenSize {
  379. if b.lowMem {
  380. b.literalBuf = make([]byte, litRegenSize)
  381. } else {
  382. if litRegenSize > maxCompressedLiteralSize {
  383. // Exceptional
  384. b.literalBuf = make([]byte, litRegenSize)
  385. } else {
  386. b.literalBuf = make([]byte, litRegenSize, maxCompressedLiteralSize)
  387. }
  388. }
  389. }
  390. literals = b.literalBuf[:litRegenSize]
  391. v := in[0]
  392. for i := range literals {
  393. literals[i] = v
  394. }
  395. in = in[1:]
  396. if debugDecoder {
  397. printf("Found %d RLE compressed literals\n", litRegenSize)
  398. }
  399. case literalsBlockTreeless:
  400. if len(in) < litCompSize {
  401. println("too small: litType:", litType, " sizeFormat", sizeFormat, "remain:", len(in), "want:", litCompSize)
  402. return ErrBlockTooSmall
  403. }
  404. // Store compressed literals, so we defer decoding until we get history.
  405. literals = in[:litCompSize]
  406. in = in[litCompSize:]
  407. if debugDecoder {
  408. printf("Found %d compressed literals\n", litCompSize)
  409. }
  410. case literalsBlockCompressed:
  411. if len(in) < litCompSize {
  412. println("too small: litType:", litType, " sizeFormat", sizeFormat, "remain:", len(in), "want:", litCompSize)
  413. return ErrBlockTooSmall
  414. }
  415. literals = in[:litCompSize]
  416. in = in[litCompSize:]
  417. huff = huffDecoderPool.Get().(*huff0.Scratch)
  418. var err error
  419. // Ensure we have space to store it.
  420. if cap(b.literalBuf) < litRegenSize {
  421. if b.lowMem {
  422. b.literalBuf = make([]byte, 0, litRegenSize)
  423. } else {
  424. b.literalBuf = make([]byte, 0, maxCompressedLiteralSize)
  425. }
  426. }
  427. if huff == nil {
  428. huff = &huff0.Scratch{}
  429. }
  430. huff, literals, err = huff0.ReadTable(literals, huff)
  431. if err != nil {
  432. println("reading huffman table:", err)
  433. return err
  434. }
  435. // Use our out buffer.
  436. if fourStreams {
  437. literals, err = huff.Decoder().Decompress4X(b.literalBuf[:0:litRegenSize], literals)
  438. } else {
  439. literals, err = huff.Decoder().Decompress1X(b.literalBuf[:0:litRegenSize], literals)
  440. }
  441. if err != nil {
  442. println("decoding compressed literals:", err)
  443. return err
  444. }
  445. // Make sure we don't leak our literals buffer
  446. if len(literals) != litRegenSize {
  447. return fmt.Errorf("literal output size mismatch want %d, got %d", litRegenSize, len(literals))
  448. }
  449. if debugDecoder {
  450. printf("Decompressed %d literals into %d bytes\n", litCompSize, litRegenSize)
  451. }
  452. }
  453. // Decode Sequences
  454. // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#sequences-section
  455. if len(in) < 1 {
  456. return ErrBlockTooSmall
  457. }
  458. seqHeader := in[0]
  459. nSeqs := 0
  460. switch {
  461. case seqHeader == 0:
  462. in = in[1:]
  463. case seqHeader < 128:
  464. nSeqs = int(seqHeader)
  465. in = in[1:]
  466. case seqHeader < 255:
  467. if len(in) < 2 {
  468. return ErrBlockTooSmall
  469. }
  470. nSeqs = int(seqHeader-128)<<8 | int(in[1])
  471. in = in[2:]
  472. case seqHeader == 255:
  473. if len(in) < 3 {
  474. return ErrBlockTooSmall
  475. }
  476. nSeqs = 0x7f00 + int(in[1]) + (int(in[2]) << 8)
  477. in = in[3:]
  478. }
  479. // Allocate sequences
  480. if cap(b.sequenceBuf) < nSeqs {
  481. if b.lowMem {
  482. b.sequenceBuf = make([]seq, nSeqs)
  483. } else {
  484. // Allocate max
  485. b.sequenceBuf = make([]seq, nSeqs, maxSequences)
  486. }
  487. } else {
  488. // Reuse buffer
  489. b.sequenceBuf = b.sequenceBuf[:nSeqs]
  490. }
  491. var seqs = &sequenceDecs{}
  492. if nSeqs > 0 {
  493. if len(in) < 1 {
  494. return ErrBlockTooSmall
  495. }
  496. br := byteReader{b: in, off: 0}
  497. compMode := br.Uint8()
  498. br.advance(1)
  499. if debugDecoder {
  500. printf("Compression modes: 0b%b", compMode)
  501. }
  502. for i := uint(0); i < 3; i++ {
  503. mode := seqCompMode((compMode >> (6 - i*2)) & 3)
  504. if debugDecoder {
  505. println("Table", tableIndex(i), "is", mode)
  506. }
  507. var seq *sequenceDec
  508. switch tableIndex(i) {
  509. case tableLiteralLengths:
  510. seq = &seqs.litLengths
  511. case tableOffsets:
  512. seq = &seqs.offsets
  513. case tableMatchLengths:
  514. seq = &seqs.matchLengths
  515. default:
  516. panic("unknown table")
  517. }
  518. switch mode {
  519. case compModePredefined:
  520. seq.fse = &fsePredef[i]
  521. case compModeRLE:
  522. if br.remain() < 1 {
  523. return ErrBlockTooSmall
  524. }
  525. v := br.Uint8()
  526. br.advance(1)
  527. dec := fseDecoderPool.Get().(*fseDecoder)
  528. symb, err := decSymbolValue(v, symbolTableX[i])
  529. if err != nil {
  530. printf("RLE Transform table (%v) error: %v", tableIndex(i), err)
  531. return err
  532. }
  533. dec.setRLE(symb)
  534. seq.fse = dec
  535. if debugDecoder {
  536. printf("RLE set to %+v, code: %v", symb, v)
  537. }
  538. case compModeFSE:
  539. println("Reading table for", tableIndex(i))
  540. dec := fseDecoderPool.Get().(*fseDecoder)
  541. err := dec.readNCount(&br, uint16(maxTableSymbol[i]))
  542. if err != nil {
  543. println("Read table error:", err)
  544. return err
  545. }
  546. err = dec.transform(symbolTableX[i])
  547. if err != nil {
  548. println("Transform table error:", err)
  549. return err
  550. }
  551. if debugDecoder {
  552. println("Read table ok", "symbolLen:", dec.symbolLen)
  553. }
  554. seq.fse = dec
  555. case compModeRepeat:
  556. seq.repeat = true
  557. }
  558. if br.overread() {
  559. return io.ErrUnexpectedEOF
  560. }
  561. }
  562. in = br.unread()
  563. }
  564. // Wait for history.
  565. // All time spent after this is critical since it is strictly sequential.
  566. if hist == nil {
  567. hist = <-b.history
  568. if hist.error {
  569. return ErrDecoderClosed
  570. }
  571. }
  572. // Decode treeless literal block.
  573. if litType == literalsBlockTreeless {
  574. // TODO: We could send the history early WITHOUT the stream history.
  575. // This would allow decoding treeless literals before the byte history is available.
  576. // Silencia stats: Treeless 4393, with: 32775, total: 37168, 11% treeless.
  577. // So not much obvious gain here.
  578. if hist.huffTree == nil {
  579. return errors.New("literal block was treeless, but no history was defined")
  580. }
  581. // Ensure we have space to store it.
  582. if cap(b.literalBuf) < litRegenSize {
  583. if b.lowMem {
  584. b.literalBuf = make([]byte, 0, litRegenSize)
  585. } else {
  586. b.literalBuf = make([]byte, 0, maxCompressedLiteralSize)
  587. }
  588. }
  589. var err error
  590. // Use our out buffer.
  591. huff = hist.huffTree
  592. if fourStreams {
  593. literals, err = huff.Decoder().Decompress4X(b.literalBuf[:0:litRegenSize], literals)
  594. } else {
  595. literals, err = huff.Decoder().Decompress1X(b.literalBuf[:0:litRegenSize], literals)
  596. }
  597. // Make sure we don't leak our literals buffer
  598. if err != nil {
  599. println("decompressing literals:", err)
  600. return err
  601. }
  602. if len(literals) != litRegenSize {
  603. return fmt.Errorf("literal output size mismatch want %d, got %d", litRegenSize, len(literals))
  604. }
  605. } else {
  606. if hist.huffTree != nil && huff != nil {
  607. if hist.dict == nil || hist.dict.litEnc != hist.huffTree {
  608. huffDecoderPool.Put(hist.huffTree)
  609. }
  610. hist.huffTree = nil
  611. }
  612. }
  613. if huff != nil {
  614. hist.huffTree = huff
  615. }
  616. if debugDecoder {
  617. println("Final literals:", len(literals), "hash:", xxhash.Sum64(literals), "and", nSeqs, "sequences.")
  618. }
  619. if nSeqs == 0 {
  620. // Decompressed content is defined entirely as Literals Section content.
  621. b.dst = append(b.dst, literals...)
  622. if delayedHistory {
  623. hist.append(literals)
  624. }
  625. return nil
  626. }
  627. seqs, err := seqs.mergeHistory(&hist.decoders)
  628. if err != nil {
  629. return err
  630. }
  631. if debugDecoder {
  632. println("History merged ok")
  633. }
  634. br := &bitReader{}
  635. if err := br.init(in); err != nil {
  636. return err
  637. }
  638. // TODO: Investigate if sending history without decoders are faster.
  639. // This would allow the sequences to be decoded async and only have to construct stream history.
  640. // If only recent offsets were not transferred, this would be an obvious win.
  641. // Also, if first 3 sequences don't reference recent offsets, all sequences can be decoded.
  642. hbytes := hist.b
  643. if len(hbytes) > hist.windowSize {
  644. hbytes = hbytes[len(hbytes)-hist.windowSize:]
  645. // We do not need history any more.
  646. if hist.dict != nil {
  647. hist.dict.content = nil
  648. }
  649. }
  650. if err := seqs.initialize(br, hist, literals, b.dst); err != nil {
  651. println("initializing sequences:", err)
  652. return err
  653. }
  654. err = seqs.decode(nSeqs, br, hbytes)
  655. if err != nil {
  656. return err
  657. }
  658. if !br.finished() {
  659. return fmt.Errorf("%d extra bits on block, should be 0", br.remain())
  660. }
  661. err = br.close()
  662. if err != nil {
  663. printf("Closing sequences: %v, %+v\n", err, *br)
  664. }
  665. if len(b.data) > maxCompressedBlockSize {
  666. return fmt.Errorf("compressed block size too large (%d)", len(b.data))
  667. }
  668. // Set output and release references.
  669. b.dst = seqs.out
  670. seqs.out, seqs.literals, seqs.hist = nil, nil, nil
  671. if !delayedHistory {
  672. // If we don't have delayed history, no need to update.
  673. hist.recentOffsets = seqs.prevOffset
  674. return nil
  675. }
  676. if b.Last {
  677. // if last block we don't care about history.
  678. println("Last block, no history returned")
  679. hist.b = hist.b[:0]
  680. return nil
  681. }
  682. hist.append(b.dst)
  683. hist.recentOffsets = seqs.prevOffset
  684. if debugDecoder {
  685. println("Finished block with literals:", len(literals), "and", nSeqs, "sequences.")
  686. }
  687. return nil
  688. }