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.

1160 lines
29 KiB

  1. package huff0
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "sync"
  7. "github.com/klauspost/compress/fse"
  8. )
  9. type dTable struct {
  10. single []dEntrySingle
  11. }
  12. // single-symbols decoding
  13. type dEntrySingle struct {
  14. entry uint16
  15. }
  16. // Uses special code for all tables that are < 8 bits.
  17. const use8BitTables = true
  18. // ReadTable will read a table from the input.
  19. // The size of the input may be larger than the table definition.
  20. // Any content remaining after the table definition will be returned.
  21. // If no Scratch is provided a new one is allocated.
  22. // The returned Scratch can be used for encoding or decoding input using this table.
  23. func ReadTable(in []byte, s *Scratch) (s2 *Scratch, remain []byte, err error) {
  24. s, err = s.prepare(nil)
  25. if err != nil {
  26. return s, nil, err
  27. }
  28. if len(in) <= 1 {
  29. return s, nil, errors.New("input too small for table")
  30. }
  31. iSize := in[0]
  32. in = in[1:]
  33. if iSize >= 128 {
  34. // Uncompressed
  35. oSize := iSize - 127
  36. iSize = (oSize + 1) / 2
  37. if int(iSize) > len(in) {
  38. return s, nil, errors.New("input too small for table")
  39. }
  40. for n := uint8(0); n < oSize; n += 2 {
  41. v := in[n/2]
  42. s.huffWeight[n] = v >> 4
  43. s.huffWeight[n+1] = v & 15
  44. }
  45. s.symbolLen = uint16(oSize)
  46. in = in[iSize:]
  47. } else {
  48. if len(in) < int(iSize) {
  49. return s, nil, fmt.Errorf("input too small for table, want %d bytes, have %d", iSize, len(in))
  50. }
  51. // FSE compressed weights
  52. s.fse.DecompressLimit = 255
  53. hw := s.huffWeight[:]
  54. s.fse.Out = hw
  55. b, err := fse.Decompress(in[:iSize], s.fse)
  56. s.fse.Out = nil
  57. if err != nil {
  58. return s, nil, err
  59. }
  60. if len(b) > 255 {
  61. return s, nil, errors.New("corrupt input: output table too large")
  62. }
  63. s.symbolLen = uint16(len(b))
  64. in = in[iSize:]
  65. }
  66. // collect weight stats
  67. var rankStats [16]uint32
  68. weightTotal := uint32(0)
  69. for _, v := range s.huffWeight[:s.symbolLen] {
  70. if v > tableLogMax {
  71. return s, nil, errors.New("corrupt input: weight too large")
  72. }
  73. v2 := v & 15
  74. rankStats[v2]++
  75. // (1 << (v2-1)) is slower since the compiler cannot prove that v2 isn't 0.
  76. weightTotal += (1 << v2) >> 1
  77. }
  78. if weightTotal == 0 {
  79. return s, nil, errors.New("corrupt input: weights zero")
  80. }
  81. // get last non-null symbol weight (implied, total must be 2^n)
  82. {
  83. tableLog := highBit32(weightTotal) + 1
  84. if tableLog > tableLogMax {
  85. return s, nil, errors.New("corrupt input: tableLog too big")
  86. }
  87. s.actualTableLog = uint8(tableLog)
  88. // determine last weight
  89. {
  90. total := uint32(1) << tableLog
  91. rest := total - weightTotal
  92. verif := uint32(1) << highBit32(rest)
  93. lastWeight := highBit32(rest) + 1
  94. if verif != rest {
  95. // last value must be a clean power of 2
  96. return s, nil, errors.New("corrupt input: last value not power of two")
  97. }
  98. s.huffWeight[s.symbolLen] = uint8(lastWeight)
  99. s.symbolLen++
  100. rankStats[lastWeight]++
  101. }
  102. }
  103. if (rankStats[1] < 2) || (rankStats[1]&1 != 0) {
  104. // by construction : at least 2 elts of rank 1, must be even
  105. return s, nil, errors.New("corrupt input: min elt size, even check failed ")
  106. }
  107. // TODO: Choose between single/double symbol decoding
  108. // Calculate starting value for each rank
  109. {
  110. var nextRankStart uint32
  111. for n := uint8(1); n < s.actualTableLog+1; n++ {
  112. current := nextRankStart
  113. nextRankStart += rankStats[n] << (n - 1)
  114. rankStats[n] = current
  115. }
  116. }
  117. // fill DTable (always full size)
  118. tSize := 1 << tableLogMax
  119. if len(s.dt.single) != tSize {
  120. s.dt.single = make([]dEntrySingle, tSize)
  121. }
  122. cTable := s.prevTable
  123. if cap(cTable) < maxSymbolValue+1 {
  124. cTable = make([]cTableEntry, 0, maxSymbolValue+1)
  125. }
  126. cTable = cTable[:maxSymbolValue+1]
  127. s.prevTable = cTable[:s.symbolLen]
  128. s.prevTableLog = s.actualTableLog
  129. for n, w := range s.huffWeight[:s.symbolLen] {
  130. if w == 0 {
  131. cTable[n] = cTableEntry{
  132. val: 0,
  133. nBits: 0,
  134. }
  135. continue
  136. }
  137. length := (uint32(1) << w) >> 1
  138. d := dEntrySingle{
  139. entry: uint16(s.actualTableLog+1-w) | (uint16(n) << 8),
  140. }
  141. rank := &rankStats[w]
  142. cTable[n] = cTableEntry{
  143. val: uint16(*rank >> (w - 1)),
  144. nBits: uint8(d.entry),
  145. }
  146. single := s.dt.single[*rank : *rank+length]
  147. for i := range single {
  148. single[i] = d
  149. }
  150. *rank += length
  151. }
  152. return s, in, nil
  153. }
  154. // Decompress1X will decompress a 1X encoded stream.
  155. // The length of the supplied input must match the end of a block exactly.
  156. // Before this is called, the table must be initialized with ReadTable unless
  157. // the encoder re-used the table.
  158. // deprecated: Use the stateless Decoder() to get a concurrent version.
  159. func (s *Scratch) Decompress1X(in []byte) (out []byte, err error) {
  160. if cap(s.Out) < s.MaxDecodedSize {
  161. s.Out = make([]byte, s.MaxDecodedSize)
  162. }
  163. s.Out = s.Out[:0:s.MaxDecodedSize]
  164. s.Out, err = s.Decoder().Decompress1X(s.Out, in)
  165. return s.Out, err
  166. }
  167. // Decompress4X will decompress a 4X encoded stream.
  168. // Before this is called, the table must be initialized with ReadTable unless
  169. // the encoder re-used the table.
  170. // The length of the supplied input must match the end of a block exactly.
  171. // The destination size of the uncompressed data must be known and provided.
  172. // deprecated: Use the stateless Decoder() to get a concurrent version.
  173. func (s *Scratch) Decompress4X(in []byte, dstSize int) (out []byte, err error) {
  174. if dstSize > s.MaxDecodedSize {
  175. return nil, ErrMaxDecodedSizeExceeded
  176. }
  177. if cap(s.Out) < dstSize {
  178. s.Out = make([]byte, s.MaxDecodedSize)
  179. }
  180. s.Out = s.Out[:0:dstSize]
  181. s.Out, err = s.Decoder().Decompress4X(s.Out, in)
  182. return s.Out, err
  183. }
  184. // Decoder will return a stateless decoder that can be used by multiple
  185. // decompressors concurrently.
  186. // Before this is called, the table must be initialized with ReadTable.
  187. // The Decoder is still linked to the scratch buffer so that cannot be reused.
  188. // However, it is safe to discard the scratch.
  189. func (s *Scratch) Decoder() *Decoder {
  190. return &Decoder{
  191. dt: s.dt,
  192. actualTableLog: s.actualTableLog,
  193. bufs: &s.decPool,
  194. }
  195. }
  196. // Decoder provides stateless decoding.
  197. type Decoder struct {
  198. dt dTable
  199. actualTableLog uint8
  200. bufs *sync.Pool
  201. }
  202. func (d *Decoder) buffer() *[4][256]byte {
  203. buf, ok := d.bufs.Get().(*[4][256]byte)
  204. if ok {
  205. return buf
  206. }
  207. return &[4][256]byte{}
  208. }
  209. // decompress1X8Bit will decompress a 1X encoded stream with tablelog <= 8.
  210. // The cap of the output buffer will be the maximum decompressed size.
  211. // The length of the supplied input must match the end of a block exactly.
  212. func (d *Decoder) decompress1X8Bit(dst, src []byte) ([]byte, error) {
  213. if d.actualTableLog == 8 {
  214. return d.decompress1X8BitExactly(dst, src)
  215. }
  216. var br bitReaderBytes
  217. err := br.init(src)
  218. if err != nil {
  219. return dst, err
  220. }
  221. maxDecodedSize := cap(dst)
  222. dst = dst[:0]
  223. // Avoid bounds check by always having full sized table.
  224. dt := d.dt.single[:256]
  225. // Use temp table to avoid bound checks/append penalty.
  226. bufs := d.buffer()
  227. buf := &bufs[0]
  228. var off uint8
  229. switch d.actualTableLog {
  230. case 8:
  231. const shift = 8 - 8
  232. for br.off >= 4 {
  233. br.fillFast()
  234. v := dt[uint8(br.value>>(56+shift))]
  235. br.advance(uint8(v.entry))
  236. buf[off+0] = uint8(v.entry >> 8)
  237. v = dt[uint8(br.value>>(56+shift))]
  238. br.advance(uint8(v.entry))
  239. buf[off+1] = uint8(v.entry >> 8)
  240. v = dt[uint8(br.value>>(56+shift))]
  241. br.advance(uint8(v.entry))
  242. buf[off+2] = uint8(v.entry >> 8)
  243. v = dt[uint8(br.value>>(56+shift))]
  244. br.advance(uint8(v.entry))
  245. buf[off+3] = uint8(v.entry >> 8)
  246. off += 4
  247. if off == 0 {
  248. if len(dst)+256 > maxDecodedSize {
  249. br.close()
  250. d.bufs.Put(bufs)
  251. return nil, ErrMaxDecodedSizeExceeded
  252. }
  253. dst = append(dst, buf[:]...)
  254. }
  255. }
  256. case 7:
  257. const shift = 8 - 7
  258. for br.off >= 4 {
  259. br.fillFast()
  260. v := dt[uint8(br.value>>(56+shift))]
  261. br.advance(uint8(v.entry))
  262. buf[off+0] = uint8(v.entry >> 8)
  263. v = dt[uint8(br.value>>(56+shift))]
  264. br.advance(uint8(v.entry))
  265. buf[off+1] = uint8(v.entry >> 8)
  266. v = dt[uint8(br.value>>(56+shift))]
  267. br.advance(uint8(v.entry))
  268. buf[off+2] = uint8(v.entry >> 8)
  269. v = dt[uint8(br.value>>(56+shift))]
  270. br.advance(uint8(v.entry))
  271. buf[off+3] = uint8(v.entry >> 8)
  272. off += 4
  273. if off == 0 {
  274. if len(dst)+256 > maxDecodedSize {
  275. br.close()
  276. d.bufs.Put(bufs)
  277. return nil, ErrMaxDecodedSizeExceeded
  278. }
  279. dst = append(dst, buf[:]...)
  280. }
  281. }
  282. case 6:
  283. const shift = 8 - 6
  284. for br.off >= 4 {
  285. br.fillFast()
  286. v := dt[uint8(br.value>>(56+shift))]
  287. br.advance(uint8(v.entry))
  288. buf[off+0] = uint8(v.entry >> 8)
  289. v = dt[uint8(br.value>>(56+shift))]
  290. br.advance(uint8(v.entry))
  291. buf[off+1] = uint8(v.entry >> 8)
  292. v = dt[uint8(br.value>>(56+shift))]
  293. br.advance(uint8(v.entry))
  294. buf[off+2] = uint8(v.entry >> 8)
  295. v = dt[uint8(br.value>>(56+shift))]
  296. br.advance(uint8(v.entry))
  297. buf[off+3] = uint8(v.entry >> 8)
  298. off += 4
  299. if off == 0 {
  300. if len(dst)+256 > maxDecodedSize {
  301. d.bufs.Put(bufs)
  302. br.close()
  303. return nil, ErrMaxDecodedSizeExceeded
  304. }
  305. dst = append(dst, buf[:]...)
  306. }
  307. }
  308. case 5:
  309. const shift = 8 - 5
  310. for br.off >= 4 {
  311. br.fillFast()
  312. v := dt[uint8(br.value>>(56+shift))]
  313. br.advance(uint8(v.entry))
  314. buf[off+0] = uint8(v.entry >> 8)
  315. v = dt[uint8(br.value>>(56+shift))]
  316. br.advance(uint8(v.entry))
  317. buf[off+1] = uint8(v.entry >> 8)
  318. v = dt[uint8(br.value>>(56+shift))]
  319. br.advance(uint8(v.entry))
  320. buf[off+2] = uint8(v.entry >> 8)
  321. v = dt[uint8(br.value>>(56+shift))]
  322. br.advance(uint8(v.entry))
  323. buf[off+3] = uint8(v.entry >> 8)
  324. off += 4
  325. if off == 0 {
  326. if len(dst)+256 > maxDecodedSize {
  327. d.bufs.Put(bufs)
  328. br.close()
  329. return nil, ErrMaxDecodedSizeExceeded
  330. }
  331. dst = append(dst, buf[:]...)
  332. }
  333. }
  334. case 4:
  335. const shift = 8 - 4
  336. for br.off >= 4 {
  337. br.fillFast()
  338. v := dt[uint8(br.value>>(56+shift))]
  339. br.advance(uint8(v.entry))
  340. buf[off+0] = uint8(v.entry >> 8)
  341. v = dt[uint8(br.value>>(56+shift))]
  342. br.advance(uint8(v.entry))
  343. buf[off+1] = uint8(v.entry >> 8)
  344. v = dt[uint8(br.value>>(56+shift))]
  345. br.advance(uint8(v.entry))
  346. buf[off+2] = uint8(v.entry >> 8)
  347. v = dt[uint8(br.value>>(56+shift))]
  348. br.advance(uint8(v.entry))
  349. buf[off+3] = uint8(v.entry >> 8)
  350. off += 4
  351. if off == 0 {
  352. if len(dst)+256 > maxDecodedSize {
  353. d.bufs.Put(bufs)
  354. br.close()
  355. return nil, ErrMaxDecodedSizeExceeded
  356. }
  357. dst = append(dst, buf[:]...)
  358. }
  359. }
  360. case 3:
  361. const shift = 8 - 3
  362. for br.off >= 4 {
  363. br.fillFast()
  364. v := dt[uint8(br.value>>(56+shift))]
  365. br.advance(uint8(v.entry))
  366. buf[off+0] = uint8(v.entry >> 8)
  367. v = dt[uint8(br.value>>(56+shift))]
  368. br.advance(uint8(v.entry))
  369. buf[off+1] = uint8(v.entry >> 8)
  370. v = dt[uint8(br.value>>(56+shift))]
  371. br.advance(uint8(v.entry))
  372. buf[off+2] = uint8(v.entry >> 8)
  373. v = dt[uint8(br.value>>(56+shift))]
  374. br.advance(uint8(v.entry))
  375. buf[off+3] = uint8(v.entry >> 8)
  376. off += 4
  377. if off == 0 {
  378. if len(dst)+256 > maxDecodedSize {
  379. d.bufs.Put(bufs)
  380. br.close()
  381. return nil, ErrMaxDecodedSizeExceeded
  382. }
  383. dst = append(dst, buf[:]...)
  384. }
  385. }
  386. case 2:
  387. const shift = 8 - 2
  388. for br.off >= 4 {
  389. br.fillFast()
  390. v := dt[uint8(br.value>>(56+shift))]
  391. br.advance(uint8(v.entry))
  392. buf[off+0] = uint8(v.entry >> 8)
  393. v = dt[uint8(br.value>>(56+shift))]
  394. br.advance(uint8(v.entry))
  395. buf[off+1] = uint8(v.entry >> 8)
  396. v = dt[uint8(br.value>>(56+shift))]
  397. br.advance(uint8(v.entry))
  398. buf[off+2] = uint8(v.entry >> 8)
  399. v = dt[uint8(br.value>>(56+shift))]
  400. br.advance(uint8(v.entry))
  401. buf[off+3] = uint8(v.entry >> 8)
  402. off += 4
  403. if off == 0 {
  404. if len(dst)+256 > maxDecodedSize {
  405. d.bufs.Put(bufs)
  406. br.close()
  407. return nil, ErrMaxDecodedSizeExceeded
  408. }
  409. dst = append(dst, buf[:]...)
  410. }
  411. }
  412. case 1:
  413. const shift = 8 - 1
  414. for br.off >= 4 {
  415. br.fillFast()
  416. v := dt[uint8(br.value>>(56+shift))]
  417. br.advance(uint8(v.entry))
  418. buf[off+0] = uint8(v.entry >> 8)
  419. v = dt[uint8(br.value>>(56+shift))]
  420. br.advance(uint8(v.entry))
  421. buf[off+1] = uint8(v.entry >> 8)
  422. v = dt[uint8(br.value>>(56+shift))]
  423. br.advance(uint8(v.entry))
  424. buf[off+2] = uint8(v.entry >> 8)
  425. v = dt[uint8(br.value>>(56+shift))]
  426. br.advance(uint8(v.entry))
  427. buf[off+3] = uint8(v.entry >> 8)
  428. off += 4
  429. if off == 0 {
  430. if len(dst)+256 > maxDecodedSize {
  431. d.bufs.Put(bufs)
  432. br.close()
  433. return nil, ErrMaxDecodedSizeExceeded
  434. }
  435. dst = append(dst, buf[:]...)
  436. }
  437. }
  438. default:
  439. d.bufs.Put(bufs)
  440. return nil, fmt.Errorf("invalid tablelog: %d", d.actualTableLog)
  441. }
  442. if len(dst)+int(off) > maxDecodedSize {
  443. d.bufs.Put(bufs)
  444. br.close()
  445. return nil, ErrMaxDecodedSizeExceeded
  446. }
  447. dst = append(dst, buf[:off]...)
  448. // br < 4, so uint8 is fine
  449. bitsLeft := int8(uint8(br.off)*8 + (64 - br.bitsRead))
  450. shift := (8 - d.actualTableLog) & 7
  451. for bitsLeft > 0 {
  452. if br.bitsRead >= 64-8 {
  453. for br.off > 0 {
  454. br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
  455. br.bitsRead -= 8
  456. br.off--
  457. }
  458. }
  459. if len(dst) >= maxDecodedSize {
  460. br.close()
  461. d.bufs.Put(bufs)
  462. return nil, ErrMaxDecodedSizeExceeded
  463. }
  464. v := dt[br.peekByteFast()>>shift]
  465. nBits := uint8(v.entry)
  466. br.advance(nBits)
  467. bitsLeft -= int8(nBits)
  468. dst = append(dst, uint8(v.entry>>8))
  469. }
  470. d.bufs.Put(bufs)
  471. return dst, br.close()
  472. }
  473. // decompress1X8Bit will decompress a 1X encoded stream with tablelog <= 8.
  474. // The cap of the output buffer will be the maximum decompressed size.
  475. // The length of the supplied input must match the end of a block exactly.
  476. func (d *Decoder) decompress1X8BitExactly(dst, src []byte) ([]byte, error) {
  477. var br bitReaderBytes
  478. err := br.init(src)
  479. if err != nil {
  480. return dst, err
  481. }
  482. maxDecodedSize := cap(dst)
  483. dst = dst[:0]
  484. // Avoid bounds check by always having full sized table.
  485. dt := d.dt.single[:256]
  486. // Use temp table to avoid bound checks/append penalty.
  487. bufs := d.buffer()
  488. buf := &bufs[0]
  489. var off uint8
  490. const shift = 56
  491. //fmt.Printf("mask: %b, tl:%d\n", mask, d.actualTableLog)
  492. for br.off >= 4 {
  493. br.fillFast()
  494. v := dt[uint8(br.value>>shift)]
  495. br.advance(uint8(v.entry))
  496. buf[off+0] = uint8(v.entry >> 8)
  497. v = dt[uint8(br.value>>shift)]
  498. br.advance(uint8(v.entry))
  499. buf[off+1] = uint8(v.entry >> 8)
  500. v = dt[uint8(br.value>>shift)]
  501. br.advance(uint8(v.entry))
  502. buf[off+2] = uint8(v.entry >> 8)
  503. v = dt[uint8(br.value>>shift)]
  504. br.advance(uint8(v.entry))
  505. buf[off+3] = uint8(v.entry >> 8)
  506. off += 4
  507. if off == 0 {
  508. if len(dst)+256 > maxDecodedSize {
  509. d.bufs.Put(bufs)
  510. br.close()
  511. return nil, ErrMaxDecodedSizeExceeded
  512. }
  513. dst = append(dst, buf[:]...)
  514. }
  515. }
  516. if len(dst)+int(off) > maxDecodedSize {
  517. d.bufs.Put(bufs)
  518. br.close()
  519. return nil, ErrMaxDecodedSizeExceeded
  520. }
  521. dst = append(dst, buf[:off]...)
  522. // br < 4, so uint8 is fine
  523. bitsLeft := int8(uint8(br.off)*8 + (64 - br.bitsRead))
  524. for bitsLeft > 0 {
  525. if br.bitsRead >= 64-8 {
  526. for br.off > 0 {
  527. br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
  528. br.bitsRead -= 8
  529. br.off--
  530. }
  531. }
  532. if len(dst) >= maxDecodedSize {
  533. d.bufs.Put(bufs)
  534. br.close()
  535. return nil, ErrMaxDecodedSizeExceeded
  536. }
  537. v := dt[br.peekByteFast()]
  538. nBits := uint8(v.entry)
  539. br.advance(nBits)
  540. bitsLeft -= int8(nBits)
  541. dst = append(dst, uint8(v.entry>>8))
  542. }
  543. d.bufs.Put(bufs)
  544. return dst, br.close()
  545. }
  546. // Decompress4X will decompress a 4X encoded stream.
  547. // The length of the supplied input must match the end of a block exactly.
  548. // The *capacity* of the dst slice must match the destination size of
  549. // the uncompressed data exactly.
  550. func (d *Decoder) decompress4X8bit(dst, src []byte) ([]byte, error) {
  551. if d.actualTableLog == 8 {
  552. return d.decompress4X8bitExactly(dst, src)
  553. }
  554. var br [4]bitReaderBytes
  555. start := 6
  556. for i := 0; i < 3; i++ {
  557. length := int(src[i*2]) | (int(src[i*2+1]) << 8)
  558. if start+length >= len(src) {
  559. return nil, errors.New("truncated input (or invalid offset)")
  560. }
  561. err := br[i].init(src[start : start+length])
  562. if err != nil {
  563. return nil, err
  564. }
  565. start += length
  566. }
  567. err := br[3].init(src[start:])
  568. if err != nil {
  569. return nil, err
  570. }
  571. // destination, offset to match first output
  572. dstSize := cap(dst)
  573. dst = dst[:dstSize]
  574. out := dst
  575. dstEvery := (dstSize + 3) / 4
  576. shift := (56 + (8 - d.actualTableLog)) & 63
  577. const tlSize = 1 << 8
  578. single := d.dt.single[:tlSize]
  579. // Use temp table to avoid bound checks/append penalty.
  580. buf := d.buffer()
  581. var off uint8
  582. var decoded int
  583. // Decode 4 values from each decoder/loop.
  584. const bufoff = 256
  585. for {
  586. if br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4 {
  587. break
  588. }
  589. {
  590. // Interleave 2 decodes.
  591. const stream = 0
  592. const stream2 = 1
  593. br1 := &br[stream]
  594. br2 := &br[stream2]
  595. br1.fillFast()
  596. br2.fillFast()
  597. v := single[uint8(br1.value>>shift)].entry
  598. v2 := single[uint8(br2.value>>shift)].entry
  599. br1.bitsRead += uint8(v)
  600. br1.value <<= v & 63
  601. br2.bitsRead += uint8(v2)
  602. br2.value <<= v2 & 63
  603. buf[stream][off] = uint8(v >> 8)
  604. buf[stream2][off] = uint8(v2 >> 8)
  605. v = single[uint8(br1.value>>shift)].entry
  606. v2 = single[uint8(br2.value>>shift)].entry
  607. br1.bitsRead += uint8(v)
  608. br1.value <<= v & 63
  609. br2.bitsRead += uint8(v2)
  610. br2.value <<= v2 & 63
  611. buf[stream][off+1] = uint8(v >> 8)
  612. buf[stream2][off+1] = uint8(v2 >> 8)
  613. v = single[uint8(br1.value>>shift)].entry
  614. v2 = single[uint8(br2.value>>shift)].entry
  615. br1.bitsRead += uint8(v)
  616. br1.value <<= v & 63
  617. br2.bitsRead += uint8(v2)
  618. br2.value <<= v2 & 63
  619. buf[stream][off+2] = uint8(v >> 8)
  620. buf[stream2][off+2] = uint8(v2 >> 8)
  621. v = single[uint8(br1.value>>shift)].entry
  622. v2 = single[uint8(br2.value>>shift)].entry
  623. br1.bitsRead += uint8(v)
  624. br1.value <<= v & 63
  625. br2.bitsRead += uint8(v2)
  626. br2.value <<= v2 & 63
  627. buf[stream][off+3] = uint8(v >> 8)
  628. buf[stream2][off+3] = uint8(v2 >> 8)
  629. }
  630. {
  631. const stream = 2
  632. const stream2 = 3
  633. br1 := &br[stream]
  634. br2 := &br[stream2]
  635. br1.fillFast()
  636. br2.fillFast()
  637. v := single[uint8(br1.value>>shift)].entry
  638. v2 := single[uint8(br2.value>>shift)].entry
  639. br1.bitsRead += uint8(v)
  640. br1.value <<= v & 63
  641. br2.bitsRead += uint8(v2)
  642. br2.value <<= v2 & 63
  643. buf[stream][off] = uint8(v >> 8)
  644. buf[stream2][off] = uint8(v2 >> 8)
  645. v = single[uint8(br1.value>>shift)].entry
  646. v2 = single[uint8(br2.value>>shift)].entry
  647. br1.bitsRead += uint8(v)
  648. br1.value <<= v & 63
  649. br2.bitsRead += uint8(v2)
  650. br2.value <<= v2 & 63
  651. buf[stream][off+1] = uint8(v >> 8)
  652. buf[stream2][off+1] = uint8(v2 >> 8)
  653. v = single[uint8(br1.value>>shift)].entry
  654. v2 = single[uint8(br2.value>>shift)].entry
  655. br1.bitsRead += uint8(v)
  656. br1.value <<= v & 63
  657. br2.bitsRead += uint8(v2)
  658. br2.value <<= v2 & 63
  659. buf[stream][off+2] = uint8(v >> 8)
  660. buf[stream2][off+2] = uint8(v2 >> 8)
  661. v = single[uint8(br1.value>>shift)].entry
  662. v2 = single[uint8(br2.value>>shift)].entry
  663. br1.bitsRead += uint8(v)
  664. br1.value <<= v & 63
  665. br2.bitsRead += uint8(v2)
  666. br2.value <<= v2 & 63
  667. buf[stream][off+3] = uint8(v >> 8)
  668. buf[stream2][off+3] = uint8(v2 >> 8)
  669. }
  670. off += 4
  671. if off == 0 {
  672. if bufoff > dstEvery {
  673. d.bufs.Put(buf)
  674. return nil, errors.New("corruption detected: stream overrun 1")
  675. }
  676. copy(out, buf[0][:])
  677. copy(out[dstEvery:], buf[1][:])
  678. copy(out[dstEvery*2:], buf[2][:])
  679. copy(out[dstEvery*3:], buf[3][:])
  680. out = out[bufoff:]
  681. decoded += bufoff * 4
  682. // There must at least be 3 buffers left.
  683. if len(out) < dstEvery*3 {
  684. d.bufs.Put(buf)
  685. return nil, errors.New("corruption detected: stream overrun 2")
  686. }
  687. }
  688. }
  689. if off > 0 {
  690. ioff := int(off)
  691. if len(out) < dstEvery*3+ioff {
  692. d.bufs.Put(buf)
  693. return nil, errors.New("corruption detected: stream overrun 3")
  694. }
  695. copy(out, buf[0][:off])
  696. copy(out[dstEvery:], buf[1][:off])
  697. copy(out[dstEvery*2:], buf[2][:off])
  698. copy(out[dstEvery*3:], buf[3][:off])
  699. decoded += int(off) * 4
  700. out = out[off:]
  701. }
  702. // Decode remaining.
  703. // Decode remaining.
  704. remainBytes := dstEvery - (decoded / 4)
  705. for i := range br {
  706. offset := dstEvery * i
  707. endsAt := offset + remainBytes
  708. if endsAt > len(out) {
  709. endsAt = len(out)
  710. }
  711. br := &br[i]
  712. bitsLeft := br.remaining()
  713. for bitsLeft > 0 {
  714. if br.finished() {
  715. d.bufs.Put(buf)
  716. return nil, io.ErrUnexpectedEOF
  717. }
  718. if br.bitsRead >= 56 {
  719. if br.off >= 4 {
  720. v := br.in[br.off-4:]
  721. v = v[:4]
  722. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  723. br.value |= uint64(low) << (br.bitsRead - 32)
  724. br.bitsRead -= 32
  725. br.off -= 4
  726. } else {
  727. for br.off > 0 {
  728. br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
  729. br.bitsRead -= 8
  730. br.off--
  731. }
  732. }
  733. }
  734. // end inline...
  735. if offset >= endsAt {
  736. d.bufs.Put(buf)
  737. return nil, errors.New("corruption detected: stream overrun 4")
  738. }
  739. // Read value and increment offset.
  740. v := single[uint8(br.value>>shift)].entry
  741. nBits := uint8(v)
  742. br.advance(nBits)
  743. bitsLeft -= uint(nBits)
  744. out[offset] = uint8(v >> 8)
  745. offset++
  746. }
  747. if offset != endsAt {
  748. d.bufs.Put(buf)
  749. return nil, fmt.Errorf("corruption detected: short output block %d, end %d != %d", i, offset, endsAt)
  750. }
  751. decoded += offset - dstEvery*i
  752. err = br.close()
  753. if err != nil {
  754. d.bufs.Put(buf)
  755. return nil, err
  756. }
  757. }
  758. d.bufs.Put(buf)
  759. if dstSize != decoded {
  760. return nil, errors.New("corruption detected: short output block")
  761. }
  762. return dst, nil
  763. }
  764. // Decompress4X will decompress a 4X encoded stream.
  765. // The length of the supplied input must match the end of a block exactly.
  766. // The *capacity* of the dst slice must match the destination size of
  767. // the uncompressed data exactly.
  768. func (d *Decoder) decompress4X8bitExactly(dst, src []byte) ([]byte, error) {
  769. var br [4]bitReaderBytes
  770. start := 6
  771. for i := 0; i < 3; i++ {
  772. length := int(src[i*2]) | (int(src[i*2+1]) << 8)
  773. if start+length >= len(src) {
  774. return nil, errors.New("truncated input (or invalid offset)")
  775. }
  776. err := br[i].init(src[start : start+length])
  777. if err != nil {
  778. return nil, err
  779. }
  780. start += length
  781. }
  782. err := br[3].init(src[start:])
  783. if err != nil {
  784. return nil, err
  785. }
  786. // destination, offset to match first output
  787. dstSize := cap(dst)
  788. dst = dst[:dstSize]
  789. out := dst
  790. dstEvery := (dstSize + 3) / 4
  791. const shift = 56
  792. const tlSize = 1 << 8
  793. single := d.dt.single[:tlSize]
  794. // Use temp table to avoid bound checks/append penalty.
  795. buf := d.buffer()
  796. var off uint8
  797. var decoded int
  798. // Decode 4 values from each decoder/loop.
  799. const bufoff = 256
  800. for {
  801. if br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4 {
  802. break
  803. }
  804. {
  805. // Interleave 2 decodes.
  806. const stream = 0
  807. const stream2 = 1
  808. br1 := &br[stream]
  809. br2 := &br[stream2]
  810. br1.fillFast()
  811. br2.fillFast()
  812. v := single[uint8(br1.value>>shift)].entry
  813. v2 := single[uint8(br2.value>>shift)].entry
  814. br1.bitsRead += uint8(v)
  815. br1.value <<= v & 63
  816. br2.bitsRead += uint8(v2)
  817. br2.value <<= v2 & 63
  818. buf[stream][off] = uint8(v >> 8)
  819. buf[stream2][off] = uint8(v2 >> 8)
  820. v = single[uint8(br1.value>>shift)].entry
  821. v2 = single[uint8(br2.value>>shift)].entry
  822. br1.bitsRead += uint8(v)
  823. br1.value <<= v & 63
  824. br2.bitsRead += uint8(v2)
  825. br2.value <<= v2 & 63
  826. buf[stream][off+1] = uint8(v >> 8)
  827. buf[stream2][off+1] = uint8(v2 >> 8)
  828. v = single[uint8(br1.value>>shift)].entry
  829. v2 = single[uint8(br2.value>>shift)].entry
  830. br1.bitsRead += uint8(v)
  831. br1.value <<= v & 63
  832. br2.bitsRead += uint8(v2)
  833. br2.value <<= v2 & 63
  834. buf[stream][off+2] = uint8(v >> 8)
  835. buf[stream2][off+2] = uint8(v2 >> 8)
  836. v = single[uint8(br1.value>>shift)].entry
  837. v2 = single[uint8(br2.value>>shift)].entry
  838. br1.bitsRead += uint8(v)
  839. br1.value <<= v & 63
  840. br2.bitsRead += uint8(v2)
  841. br2.value <<= v2 & 63
  842. buf[stream][off+3] = uint8(v >> 8)
  843. buf[stream2][off+3] = uint8(v2 >> 8)
  844. }
  845. {
  846. const stream = 2
  847. const stream2 = 3
  848. br1 := &br[stream]
  849. br2 := &br[stream2]
  850. br1.fillFast()
  851. br2.fillFast()
  852. v := single[uint8(br1.value>>shift)].entry
  853. v2 := single[uint8(br2.value>>shift)].entry
  854. br1.bitsRead += uint8(v)
  855. br1.value <<= v & 63
  856. br2.bitsRead += uint8(v2)
  857. br2.value <<= v2 & 63
  858. buf[stream][off] = uint8(v >> 8)
  859. buf[stream2][off] = uint8(v2 >> 8)
  860. v = single[uint8(br1.value>>shift)].entry
  861. v2 = single[uint8(br2.value>>shift)].entry
  862. br1.bitsRead += uint8(v)
  863. br1.value <<= v & 63
  864. br2.bitsRead += uint8(v2)
  865. br2.value <<= v2 & 63
  866. buf[stream][off+1] = uint8(v >> 8)
  867. buf[stream2][off+1] = uint8(v2 >> 8)
  868. v = single[uint8(br1.value>>shift)].entry
  869. v2 = single[uint8(br2.value>>shift)].entry
  870. br1.bitsRead += uint8(v)
  871. br1.value <<= v & 63
  872. br2.bitsRead += uint8(v2)
  873. br2.value <<= v2 & 63
  874. buf[stream][off+2] = uint8(v >> 8)
  875. buf[stream2][off+2] = uint8(v2 >> 8)
  876. v = single[uint8(br1.value>>shift)].entry
  877. v2 = single[uint8(br2.value>>shift)].entry
  878. br1.bitsRead += uint8(v)
  879. br1.value <<= v & 63
  880. br2.bitsRead += uint8(v2)
  881. br2.value <<= v2 & 63
  882. buf[stream][off+3] = uint8(v >> 8)
  883. buf[stream2][off+3] = uint8(v2 >> 8)
  884. }
  885. off += 4
  886. if off == 0 {
  887. if bufoff > dstEvery {
  888. d.bufs.Put(buf)
  889. return nil, errors.New("corruption detected: stream overrun 1")
  890. }
  891. copy(out, buf[0][:])
  892. copy(out[dstEvery:], buf[1][:])
  893. copy(out[dstEvery*2:], buf[2][:])
  894. copy(out[dstEvery*3:], buf[3][:])
  895. out = out[bufoff:]
  896. decoded += bufoff * 4
  897. // There must at least be 3 buffers left.
  898. if len(out) < dstEvery*3 {
  899. d.bufs.Put(buf)
  900. return nil, errors.New("corruption detected: stream overrun 2")
  901. }
  902. }
  903. }
  904. if off > 0 {
  905. ioff := int(off)
  906. if len(out) < dstEvery*3+ioff {
  907. return nil, errors.New("corruption detected: stream overrun 3")
  908. }
  909. copy(out, buf[0][:off])
  910. copy(out[dstEvery:], buf[1][:off])
  911. copy(out[dstEvery*2:], buf[2][:off])
  912. copy(out[dstEvery*3:], buf[3][:off])
  913. decoded += int(off) * 4
  914. out = out[off:]
  915. }
  916. // Decode remaining.
  917. remainBytes := dstEvery - (decoded / 4)
  918. for i := range br {
  919. offset := dstEvery * i
  920. endsAt := offset + remainBytes
  921. if endsAt > len(out) {
  922. endsAt = len(out)
  923. }
  924. br := &br[i]
  925. bitsLeft := br.remaining()
  926. for bitsLeft > 0 {
  927. if br.finished() {
  928. d.bufs.Put(buf)
  929. return nil, io.ErrUnexpectedEOF
  930. }
  931. if br.bitsRead >= 56 {
  932. if br.off >= 4 {
  933. v := br.in[br.off-4:]
  934. v = v[:4]
  935. low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
  936. br.value |= uint64(low) << (br.bitsRead - 32)
  937. br.bitsRead -= 32
  938. br.off -= 4
  939. } else {
  940. for br.off > 0 {
  941. br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
  942. br.bitsRead -= 8
  943. br.off--
  944. }
  945. }
  946. }
  947. // end inline...
  948. if offset >= endsAt {
  949. d.bufs.Put(buf)
  950. return nil, errors.New("corruption detected: stream overrun 4")
  951. }
  952. // Read value and increment offset.
  953. v := single[br.peekByteFast()].entry
  954. nBits := uint8(v)
  955. br.advance(nBits)
  956. bitsLeft -= uint(nBits)
  957. out[offset] = uint8(v >> 8)
  958. offset++
  959. }
  960. if offset != endsAt {
  961. d.bufs.Put(buf)
  962. return nil, fmt.Errorf("corruption detected: short output block %d, end %d != %d", i, offset, endsAt)
  963. }
  964. decoded += offset - dstEvery*i
  965. err = br.close()
  966. if err != nil {
  967. d.bufs.Put(buf)
  968. return nil, err
  969. }
  970. }
  971. d.bufs.Put(buf)
  972. if dstSize != decoded {
  973. return nil, errors.New("corruption detected: short output block")
  974. }
  975. return dst, nil
  976. }
  977. // matches will compare a decoding table to a coding table.
  978. // Errors are written to the writer.
  979. // Nothing will be written if table is ok.
  980. func (s *Scratch) matches(ct cTable, w io.Writer) {
  981. if s == nil || len(s.dt.single) == 0 {
  982. return
  983. }
  984. dt := s.dt.single[:1<<s.actualTableLog]
  985. tablelog := s.actualTableLog
  986. ok := 0
  987. broken := 0
  988. for sym, enc := range ct {
  989. errs := 0
  990. broken++
  991. if enc.nBits == 0 {
  992. for _, dec := range dt {
  993. if uint8(dec.entry>>8) == byte(sym) {
  994. fmt.Fprintf(w, "symbol %x has decoder, but no encoder\n", sym)
  995. errs++
  996. break
  997. }
  998. }
  999. if errs == 0 {
  1000. broken--
  1001. }
  1002. continue
  1003. }
  1004. // Unused bits in input
  1005. ub := tablelog - enc.nBits
  1006. top := enc.val << ub
  1007. // decoder looks at top bits.
  1008. dec := dt[top]
  1009. if uint8(dec.entry) != enc.nBits {
  1010. fmt.Fprintf(w, "symbol 0x%x bit size mismatch (enc: %d, dec:%d).\n", sym, enc.nBits, uint8(dec.entry))
  1011. errs++
  1012. }
  1013. if uint8(dec.entry>>8) != uint8(sym) {
  1014. fmt.Fprintf(w, "symbol 0x%x decoder output mismatch (enc: %d, dec:%d).\n", sym, sym, uint8(dec.entry>>8))
  1015. errs++
  1016. }
  1017. if errs > 0 {
  1018. fmt.Fprintf(w, "%d errros in base, stopping\n", errs)
  1019. continue
  1020. }
  1021. // Ensure that all combinations are covered.
  1022. for i := uint16(0); i < (1 << ub); i++ {
  1023. vval := top | i
  1024. dec := dt[vval]
  1025. if uint8(dec.entry) != enc.nBits {
  1026. fmt.Fprintf(w, "symbol 0x%x bit size mismatch (enc: %d, dec:%d).\n", vval, enc.nBits, uint8(dec.entry))
  1027. errs++
  1028. }
  1029. if uint8(dec.entry>>8) != uint8(sym) {
  1030. fmt.Fprintf(w, "symbol 0x%x decoder output mismatch (enc: %d, dec:%d).\n", vval, sym, uint8(dec.entry>>8))
  1031. errs++
  1032. }
  1033. if errs > 20 {
  1034. fmt.Fprintf(w, "%d errros, stopping\n", errs)
  1035. break
  1036. }
  1037. }
  1038. if errs == 0 {
  1039. ok++
  1040. broken--
  1041. }
  1042. }
  1043. if broken > 0 {
  1044. fmt.Fprintf(w, "%d broken, %d ok\n", broken, ok)
  1045. }
  1046. }