inflate_gen.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. // Code generated by go generate gen_inflate.go. DO NOT EDIT.
  2. package flate
  3. import (
  4. "bufio"
  5. "bytes"
  6. "fmt"
  7. "math/bits"
  8. "strings"
  9. )
  10. // Decode a single Huffman block from f.
  11. // hl and hd are the Huffman states for the lit/length values
  12. // and the distance values, respectively. If hd == nil, using the
  13. // fixed distance encoding associated with fixed Huffman blocks.
  14. func (f *decompressor) huffmanBytesBuffer() {
  15. const (
  16. stateInit = iota // Zero value must be stateInit
  17. stateDict
  18. )
  19. fr := f.r.(*bytes.Buffer)
  20. moreBits := func() error {
  21. c, err := fr.ReadByte()
  22. if err != nil {
  23. return noEOF(err)
  24. }
  25. f.roffset++
  26. f.b |= uint32(c) << f.nb
  27. f.nb += 8
  28. return nil
  29. }
  30. switch f.stepState {
  31. case stateInit:
  32. goto readLiteral
  33. case stateDict:
  34. goto copyHistory
  35. }
  36. readLiteral:
  37. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  38. {
  39. var v int
  40. {
  41. // Inlined v, err := f.huffSym(f.hl)
  42. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  43. // with single element, huffSym must error on these two edge cases. In both
  44. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  45. // satisfy the n == 0 check below.
  46. n := uint(f.hl.maxRead)
  47. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  48. // but is smart enough to keep local variables in registers, so use nb and b,
  49. // inline call to moreBits and reassign b,nb back to f on return.
  50. nb, b := f.nb, f.b
  51. for {
  52. for nb < n {
  53. c, err := fr.ReadByte()
  54. if err != nil {
  55. f.b = b
  56. f.nb = nb
  57. f.err = noEOF(err)
  58. return
  59. }
  60. f.roffset++
  61. b |= uint32(c) << (nb & 31)
  62. nb += 8
  63. }
  64. chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
  65. n = uint(chunk & huffmanCountMask)
  66. if n > huffmanChunkBits {
  67. chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
  68. n = uint(chunk & huffmanCountMask)
  69. }
  70. if n <= nb {
  71. if n == 0 {
  72. f.b = b
  73. f.nb = nb
  74. if debugDecode {
  75. fmt.Println("huffsym: n==0")
  76. }
  77. f.err = CorruptInputError(f.roffset)
  78. return
  79. }
  80. f.b = b >> (n & 31)
  81. f.nb = nb - n
  82. v = int(chunk >> huffmanValueShift)
  83. break
  84. }
  85. }
  86. }
  87. var n uint // number of bits extra
  88. var length int
  89. var err error
  90. switch {
  91. case v < 256:
  92. f.dict.writeByte(byte(v))
  93. if f.dict.availWrite() == 0 {
  94. f.toRead = f.dict.readFlush()
  95. f.step = (*decompressor).huffmanBytesBuffer
  96. f.stepState = stateInit
  97. return
  98. }
  99. goto readLiteral
  100. case v == 256:
  101. f.finishBlock()
  102. return
  103. // otherwise, reference to older data
  104. case v < 265:
  105. length = v - (257 - 3)
  106. n = 0
  107. case v < 269:
  108. length = v*2 - (265*2 - 11)
  109. n = 1
  110. case v < 273:
  111. length = v*4 - (269*4 - 19)
  112. n = 2
  113. case v < 277:
  114. length = v*8 - (273*8 - 35)
  115. n = 3
  116. case v < 281:
  117. length = v*16 - (277*16 - 67)
  118. n = 4
  119. case v < 285:
  120. length = v*32 - (281*32 - 131)
  121. n = 5
  122. case v < maxNumLit:
  123. length = 258
  124. n = 0
  125. default:
  126. if debugDecode {
  127. fmt.Println(v, ">= maxNumLit")
  128. }
  129. f.err = CorruptInputError(f.roffset)
  130. return
  131. }
  132. if n > 0 {
  133. for f.nb < n {
  134. if err = moreBits(); err != nil {
  135. if debugDecode {
  136. fmt.Println("morebits n>0:", err)
  137. }
  138. f.err = err
  139. return
  140. }
  141. }
  142. length += int(f.b & uint32(1<<n-1))
  143. f.b >>= n
  144. f.nb -= n
  145. }
  146. var dist int
  147. if f.hd == nil {
  148. for f.nb < 5 {
  149. if err = moreBits(); err != nil {
  150. if debugDecode {
  151. fmt.Println("morebits f.nb<5:", err)
  152. }
  153. f.err = err
  154. return
  155. }
  156. }
  157. dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
  158. f.b >>= 5
  159. f.nb -= 5
  160. } else {
  161. if dist, err = f.huffSym(f.hd); err != nil {
  162. if debugDecode {
  163. fmt.Println("huffsym:", err)
  164. }
  165. f.err = err
  166. return
  167. }
  168. }
  169. switch {
  170. case dist < 4:
  171. dist++
  172. case dist < maxNumDist:
  173. nb := uint(dist-2) >> 1
  174. // have 1 bit in bottom of dist, need nb more.
  175. extra := (dist & 1) << nb
  176. for f.nb < nb {
  177. if err = moreBits(); err != nil {
  178. if debugDecode {
  179. fmt.Println("morebits f.nb<nb:", err)
  180. }
  181. f.err = err
  182. return
  183. }
  184. }
  185. extra |= int(f.b & uint32(1<<nb-1))
  186. f.b >>= nb
  187. f.nb -= nb
  188. dist = 1<<(nb+1) + 1 + extra
  189. default:
  190. if debugDecode {
  191. fmt.Println("dist too big:", dist, maxNumDist)
  192. }
  193. f.err = CorruptInputError(f.roffset)
  194. return
  195. }
  196. // No check on length; encoding can be prescient.
  197. if dist > f.dict.histSize() {
  198. if debugDecode {
  199. fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
  200. }
  201. f.err = CorruptInputError(f.roffset)
  202. return
  203. }
  204. f.copyLen, f.copyDist = length, dist
  205. goto copyHistory
  206. }
  207. copyHistory:
  208. // Perform a backwards copy according to RFC section 3.2.3.
  209. {
  210. cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
  211. if cnt == 0 {
  212. cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
  213. }
  214. f.copyLen -= cnt
  215. if f.dict.availWrite() == 0 || f.copyLen > 0 {
  216. f.toRead = f.dict.readFlush()
  217. f.step = (*decompressor).huffmanBytesBuffer // We need to continue this work
  218. f.stepState = stateDict
  219. return
  220. }
  221. goto readLiteral
  222. }
  223. }
  224. // Decode a single Huffman block from f.
  225. // hl and hd are the Huffman states for the lit/length values
  226. // and the distance values, respectively. If hd == nil, using the
  227. // fixed distance encoding associated with fixed Huffman blocks.
  228. func (f *decompressor) huffmanBytesReader() {
  229. const (
  230. stateInit = iota // Zero value must be stateInit
  231. stateDict
  232. )
  233. fr := f.r.(*bytes.Reader)
  234. moreBits := func() error {
  235. c, err := fr.ReadByte()
  236. if err != nil {
  237. return noEOF(err)
  238. }
  239. f.roffset++
  240. f.b |= uint32(c) << f.nb
  241. f.nb += 8
  242. return nil
  243. }
  244. switch f.stepState {
  245. case stateInit:
  246. goto readLiteral
  247. case stateDict:
  248. goto copyHistory
  249. }
  250. readLiteral:
  251. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  252. {
  253. var v int
  254. {
  255. // Inlined v, err := f.huffSym(f.hl)
  256. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  257. // with single element, huffSym must error on these two edge cases. In both
  258. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  259. // satisfy the n == 0 check below.
  260. n := uint(f.hl.maxRead)
  261. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  262. // but is smart enough to keep local variables in registers, so use nb and b,
  263. // inline call to moreBits and reassign b,nb back to f on return.
  264. nb, b := f.nb, f.b
  265. for {
  266. for nb < n {
  267. c, err := fr.ReadByte()
  268. if err != nil {
  269. f.b = b
  270. f.nb = nb
  271. f.err = noEOF(err)
  272. return
  273. }
  274. f.roffset++
  275. b |= uint32(c) << (nb & 31)
  276. nb += 8
  277. }
  278. chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
  279. n = uint(chunk & huffmanCountMask)
  280. if n > huffmanChunkBits {
  281. chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
  282. n = uint(chunk & huffmanCountMask)
  283. }
  284. if n <= nb {
  285. if n == 0 {
  286. f.b = b
  287. f.nb = nb
  288. if debugDecode {
  289. fmt.Println("huffsym: n==0")
  290. }
  291. f.err = CorruptInputError(f.roffset)
  292. return
  293. }
  294. f.b = b >> (n & 31)
  295. f.nb = nb - n
  296. v = int(chunk >> huffmanValueShift)
  297. break
  298. }
  299. }
  300. }
  301. var n uint // number of bits extra
  302. var length int
  303. var err error
  304. switch {
  305. case v < 256:
  306. f.dict.writeByte(byte(v))
  307. if f.dict.availWrite() == 0 {
  308. f.toRead = f.dict.readFlush()
  309. f.step = (*decompressor).huffmanBytesReader
  310. f.stepState = stateInit
  311. return
  312. }
  313. goto readLiteral
  314. case v == 256:
  315. f.finishBlock()
  316. return
  317. // otherwise, reference to older data
  318. case v < 265:
  319. length = v - (257 - 3)
  320. n = 0
  321. case v < 269:
  322. length = v*2 - (265*2 - 11)
  323. n = 1
  324. case v < 273:
  325. length = v*4 - (269*4 - 19)
  326. n = 2
  327. case v < 277:
  328. length = v*8 - (273*8 - 35)
  329. n = 3
  330. case v < 281:
  331. length = v*16 - (277*16 - 67)
  332. n = 4
  333. case v < 285:
  334. length = v*32 - (281*32 - 131)
  335. n = 5
  336. case v < maxNumLit:
  337. length = 258
  338. n = 0
  339. default:
  340. if debugDecode {
  341. fmt.Println(v, ">= maxNumLit")
  342. }
  343. f.err = CorruptInputError(f.roffset)
  344. return
  345. }
  346. if n > 0 {
  347. for f.nb < n {
  348. if err = moreBits(); err != nil {
  349. if debugDecode {
  350. fmt.Println("morebits n>0:", err)
  351. }
  352. f.err = err
  353. return
  354. }
  355. }
  356. length += int(f.b & uint32(1<<n-1))
  357. f.b >>= n
  358. f.nb -= n
  359. }
  360. var dist int
  361. if f.hd == nil {
  362. for f.nb < 5 {
  363. if err = moreBits(); err != nil {
  364. if debugDecode {
  365. fmt.Println("morebits f.nb<5:", err)
  366. }
  367. f.err = err
  368. return
  369. }
  370. }
  371. dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
  372. f.b >>= 5
  373. f.nb -= 5
  374. } else {
  375. if dist, err = f.huffSym(f.hd); err != nil {
  376. if debugDecode {
  377. fmt.Println("huffsym:", err)
  378. }
  379. f.err = err
  380. return
  381. }
  382. }
  383. switch {
  384. case dist < 4:
  385. dist++
  386. case dist < maxNumDist:
  387. nb := uint(dist-2) >> 1
  388. // have 1 bit in bottom of dist, need nb more.
  389. extra := (dist & 1) << nb
  390. for f.nb < nb {
  391. if err = moreBits(); err != nil {
  392. if debugDecode {
  393. fmt.Println("morebits f.nb<nb:", err)
  394. }
  395. f.err = err
  396. return
  397. }
  398. }
  399. extra |= int(f.b & uint32(1<<nb-1))
  400. f.b >>= nb
  401. f.nb -= nb
  402. dist = 1<<(nb+1) + 1 + extra
  403. default:
  404. if debugDecode {
  405. fmt.Println("dist too big:", dist, maxNumDist)
  406. }
  407. f.err = CorruptInputError(f.roffset)
  408. return
  409. }
  410. // No check on length; encoding can be prescient.
  411. if dist > f.dict.histSize() {
  412. if debugDecode {
  413. fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
  414. }
  415. f.err = CorruptInputError(f.roffset)
  416. return
  417. }
  418. f.copyLen, f.copyDist = length, dist
  419. goto copyHistory
  420. }
  421. copyHistory:
  422. // Perform a backwards copy according to RFC section 3.2.3.
  423. {
  424. cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
  425. if cnt == 0 {
  426. cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
  427. }
  428. f.copyLen -= cnt
  429. if f.dict.availWrite() == 0 || f.copyLen > 0 {
  430. f.toRead = f.dict.readFlush()
  431. f.step = (*decompressor).huffmanBytesReader // We need to continue this work
  432. f.stepState = stateDict
  433. return
  434. }
  435. goto readLiteral
  436. }
  437. }
  438. // Decode a single Huffman block from f.
  439. // hl and hd are the Huffman states for the lit/length values
  440. // and the distance values, respectively. If hd == nil, using the
  441. // fixed distance encoding associated with fixed Huffman blocks.
  442. func (f *decompressor) huffmanBufioReader() {
  443. const (
  444. stateInit = iota // Zero value must be stateInit
  445. stateDict
  446. )
  447. fr := f.r.(*bufio.Reader)
  448. moreBits := func() error {
  449. c, err := fr.ReadByte()
  450. if err != nil {
  451. return noEOF(err)
  452. }
  453. f.roffset++
  454. f.b |= uint32(c) << f.nb
  455. f.nb += 8
  456. return nil
  457. }
  458. switch f.stepState {
  459. case stateInit:
  460. goto readLiteral
  461. case stateDict:
  462. goto copyHistory
  463. }
  464. readLiteral:
  465. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  466. {
  467. var v int
  468. {
  469. // Inlined v, err := f.huffSym(f.hl)
  470. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  471. // with single element, huffSym must error on these two edge cases. In both
  472. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  473. // satisfy the n == 0 check below.
  474. n := uint(f.hl.maxRead)
  475. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  476. // but is smart enough to keep local variables in registers, so use nb and b,
  477. // inline call to moreBits and reassign b,nb back to f on return.
  478. nb, b := f.nb, f.b
  479. for {
  480. for nb < n {
  481. c, err := fr.ReadByte()
  482. if err != nil {
  483. f.b = b
  484. f.nb = nb
  485. f.err = noEOF(err)
  486. return
  487. }
  488. f.roffset++
  489. b |= uint32(c) << (nb & 31)
  490. nb += 8
  491. }
  492. chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
  493. n = uint(chunk & huffmanCountMask)
  494. if n > huffmanChunkBits {
  495. chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
  496. n = uint(chunk & huffmanCountMask)
  497. }
  498. if n <= nb {
  499. if n == 0 {
  500. f.b = b
  501. f.nb = nb
  502. if debugDecode {
  503. fmt.Println("huffsym: n==0")
  504. }
  505. f.err = CorruptInputError(f.roffset)
  506. return
  507. }
  508. f.b = b >> (n & 31)
  509. f.nb = nb - n
  510. v = int(chunk >> huffmanValueShift)
  511. break
  512. }
  513. }
  514. }
  515. var n uint // number of bits extra
  516. var length int
  517. var err error
  518. switch {
  519. case v < 256:
  520. f.dict.writeByte(byte(v))
  521. if f.dict.availWrite() == 0 {
  522. f.toRead = f.dict.readFlush()
  523. f.step = (*decompressor).huffmanBufioReader
  524. f.stepState = stateInit
  525. return
  526. }
  527. goto readLiteral
  528. case v == 256:
  529. f.finishBlock()
  530. return
  531. // otherwise, reference to older data
  532. case v < 265:
  533. length = v - (257 - 3)
  534. n = 0
  535. case v < 269:
  536. length = v*2 - (265*2 - 11)
  537. n = 1
  538. case v < 273:
  539. length = v*4 - (269*4 - 19)
  540. n = 2
  541. case v < 277:
  542. length = v*8 - (273*8 - 35)
  543. n = 3
  544. case v < 281:
  545. length = v*16 - (277*16 - 67)
  546. n = 4
  547. case v < 285:
  548. length = v*32 - (281*32 - 131)
  549. n = 5
  550. case v < maxNumLit:
  551. length = 258
  552. n = 0
  553. default:
  554. if debugDecode {
  555. fmt.Println(v, ">= maxNumLit")
  556. }
  557. f.err = CorruptInputError(f.roffset)
  558. return
  559. }
  560. if n > 0 {
  561. for f.nb < n {
  562. if err = moreBits(); err != nil {
  563. if debugDecode {
  564. fmt.Println("morebits n>0:", err)
  565. }
  566. f.err = err
  567. return
  568. }
  569. }
  570. length += int(f.b & uint32(1<<n-1))
  571. f.b >>= n
  572. f.nb -= n
  573. }
  574. var dist int
  575. if f.hd == nil {
  576. for f.nb < 5 {
  577. if err = moreBits(); err != nil {
  578. if debugDecode {
  579. fmt.Println("morebits f.nb<5:", err)
  580. }
  581. f.err = err
  582. return
  583. }
  584. }
  585. dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
  586. f.b >>= 5
  587. f.nb -= 5
  588. } else {
  589. if dist, err = f.huffSym(f.hd); err != nil {
  590. if debugDecode {
  591. fmt.Println("huffsym:", err)
  592. }
  593. f.err = err
  594. return
  595. }
  596. }
  597. switch {
  598. case dist < 4:
  599. dist++
  600. case dist < maxNumDist:
  601. nb := uint(dist-2) >> 1
  602. // have 1 bit in bottom of dist, need nb more.
  603. extra := (dist & 1) << nb
  604. for f.nb < nb {
  605. if err = moreBits(); err != nil {
  606. if debugDecode {
  607. fmt.Println("morebits f.nb<nb:", err)
  608. }
  609. f.err = err
  610. return
  611. }
  612. }
  613. extra |= int(f.b & uint32(1<<nb-1))
  614. f.b >>= nb
  615. f.nb -= nb
  616. dist = 1<<(nb+1) + 1 + extra
  617. default:
  618. if debugDecode {
  619. fmt.Println("dist too big:", dist, maxNumDist)
  620. }
  621. f.err = CorruptInputError(f.roffset)
  622. return
  623. }
  624. // No check on length; encoding can be prescient.
  625. if dist > f.dict.histSize() {
  626. if debugDecode {
  627. fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
  628. }
  629. f.err = CorruptInputError(f.roffset)
  630. return
  631. }
  632. f.copyLen, f.copyDist = length, dist
  633. goto copyHistory
  634. }
  635. copyHistory:
  636. // Perform a backwards copy according to RFC section 3.2.3.
  637. {
  638. cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
  639. if cnt == 0 {
  640. cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
  641. }
  642. f.copyLen -= cnt
  643. if f.dict.availWrite() == 0 || f.copyLen > 0 {
  644. f.toRead = f.dict.readFlush()
  645. f.step = (*decompressor).huffmanBufioReader // We need to continue this work
  646. f.stepState = stateDict
  647. return
  648. }
  649. goto readLiteral
  650. }
  651. }
  652. // Decode a single Huffman block from f.
  653. // hl and hd are the Huffman states for the lit/length values
  654. // and the distance values, respectively. If hd == nil, using the
  655. // fixed distance encoding associated with fixed Huffman blocks.
  656. func (f *decompressor) huffmanStringsReader() {
  657. const (
  658. stateInit = iota // Zero value must be stateInit
  659. stateDict
  660. )
  661. fr := f.r.(*strings.Reader)
  662. moreBits := func() error {
  663. c, err := fr.ReadByte()
  664. if err != nil {
  665. return noEOF(err)
  666. }
  667. f.roffset++
  668. f.b |= uint32(c) << f.nb
  669. f.nb += 8
  670. return nil
  671. }
  672. switch f.stepState {
  673. case stateInit:
  674. goto readLiteral
  675. case stateDict:
  676. goto copyHistory
  677. }
  678. readLiteral:
  679. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  680. {
  681. var v int
  682. {
  683. // Inlined v, err := f.huffSym(f.hl)
  684. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  685. // with single element, huffSym must error on these two edge cases. In both
  686. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  687. // satisfy the n == 0 check below.
  688. n := uint(f.hl.maxRead)
  689. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  690. // but is smart enough to keep local variables in registers, so use nb and b,
  691. // inline call to moreBits and reassign b,nb back to f on return.
  692. nb, b := f.nb, f.b
  693. for {
  694. for nb < n {
  695. c, err := fr.ReadByte()
  696. if err != nil {
  697. f.b = b
  698. f.nb = nb
  699. f.err = noEOF(err)
  700. return
  701. }
  702. f.roffset++
  703. b |= uint32(c) << (nb & 31)
  704. nb += 8
  705. }
  706. chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
  707. n = uint(chunk & huffmanCountMask)
  708. if n > huffmanChunkBits {
  709. chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
  710. n = uint(chunk & huffmanCountMask)
  711. }
  712. if n <= nb {
  713. if n == 0 {
  714. f.b = b
  715. f.nb = nb
  716. if debugDecode {
  717. fmt.Println("huffsym: n==0")
  718. }
  719. f.err = CorruptInputError(f.roffset)
  720. return
  721. }
  722. f.b = b >> (n & 31)
  723. f.nb = nb - n
  724. v = int(chunk >> huffmanValueShift)
  725. break
  726. }
  727. }
  728. }
  729. var n uint // number of bits extra
  730. var length int
  731. var err error
  732. switch {
  733. case v < 256:
  734. f.dict.writeByte(byte(v))
  735. if f.dict.availWrite() == 0 {
  736. f.toRead = f.dict.readFlush()
  737. f.step = (*decompressor).huffmanStringsReader
  738. f.stepState = stateInit
  739. return
  740. }
  741. goto readLiteral
  742. case v == 256:
  743. f.finishBlock()
  744. return
  745. // otherwise, reference to older data
  746. case v < 265:
  747. length = v - (257 - 3)
  748. n = 0
  749. case v < 269:
  750. length = v*2 - (265*2 - 11)
  751. n = 1
  752. case v < 273:
  753. length = v*4 - (269*4 - 19)
  754. n = 2
  755. case v < 277:
  756. length = v*8 - (273*8 - 35)
  757. n = 3
  758. case v < 281:
  759. length = v*16 - (277*16 - 67)
  760. n = 4
  761. case v < 285:
  762. length = v*32 - (281*32 - 131)
  763. n = 5
  764. case v < maxNumLit:
  765. length = 258
  766. n = 0
  767. default:
  768. if debugDecode {
  769. fmt.Println(v, ">= maxNumLit")
  770. }
  771. f.err = CorruptInputError(f.roffset)
  772. return
  773. }
  774. if n > 0 {
  775. for f.nb < n {
  776. if err = moreBits(); err != nil {
  777. if debugDecode {
  778. fmt.Println("morebits n>0:", err)
  779. }
  780. f.err = err
  781. return
  782. }
  783. }
  784. length += int(f.b & uint32(1<<n-1))
  785. f.b >>= n
  786. f.nb -= n
  787. }
  788. var dist int
  789. if f.hd == nil {
  790. for f.nb < 5 {
  791. if err = moreBits(); err != nil {
  792. if debugDecode {
  793. fmt.Println("morebits f.nb<5:", err)
  794. }
  795. f.err = err
  796. return
  797. }
  798. }
  799. dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
  800. f.b >>= 5
  801. f.nb -= 5
  802. } else {
  803. if dist, err = f.huffSym(f.hd); err != nil {
  804. if debugDecode {
  805. fmt.Println("huffsym:", err)
  806. }
  807. f.err = err
  808. return
  809. }
  810. }
  811. switch {
  812. case dist < 4:
  813. dist++
  814. case dist < maxNumDist:
  815. nb := uint(dist-2) >> 1
  816. // have 1 bit in bottom of dist, need nb more.
  817. extra := (dist & 1) << nb
  818. for f.nb < nb {
  819. if err = moreBits(); err != nil {
  820. if debugDecode {
  821. fmt.Println("morebits f.nb<nb:", err)
  822. }
  823. f.err = err
  824. return
  825. }
  826. }
  827. extra |= int(f.b & uint32(1<<nb-1))
  828. f.b >>= nb
  829. f.nb -= nb
  830. dist = 1<<(nb+1) + 1 + extra
  831. default:
  832. if debugDecode {
  833. fmt.Println("dist too big:", dist, maxNumDist)
  834. }
  835. f.err = CorruptInputError(f.roffset)
  836. return
  837. }
  838. // No check on length; encoding can be prescient.
  839. if dist > f.dict.histSize() {
  840. if debugDecode {
  841. fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
  842. }
  843. f.err = CorruptInputError(f.roffset)
  844. return
  845. }
  846. f.copyLen, f.copyDist = length, dist
  847. goto copyHistory
  848. }
  849. copyHistory:
  850. // Perform a backwards copy according to RFC section 3.2.3.
  851. {
  852. cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
  853. if cnt == 0 {
  854. cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
  855. }
  856. f.copyLen -= cnt
  857. if f.dict.availWrite() == 0 || f.copyLen > 0 {
  858. f.toRead = f.dict.readFlush()
  859. f.step = (*decompressor).huffmanStringsReader // We need to continue this work
  860. f.stepState = stateDict
  861. return
  862. }
  863. goto readLiteral
  864. }
  865. }
  866. func (f *decompressor) huffmanBlockDecoder() func() {
  867. switch f.r.(type) {
  868. case *bytes.Buffer:
  869. return f.huffmanBytesBuffer
  870. case *bytes.Reader:
  871. return f.huffmanBytesReader
  872. case *bufio.Reader:
  873. return f.huffmanBufioReader
  874. case *strings.Reader:
  875. return f.huffmanStringsReader
  876. default:
  877. return f.huffmanBlockGeneric
  878. }
  879. }