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.

inflate_gen.go 25KB

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