decompress.go 35 KB

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