inflate_gen.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  21. // but is smart enough to keep local variables in registers, so use nb and b,
  22. // inline call to moreBits and reassign b,nb back to f on return.
  23. fnb, fb, dict := f.nb, f.b, &f.dict
  24. switch f.stepState {
  25. case stateInit:
  26. goto readLiteral
  27. case stateDict:
  28. goto copyHistory
  29. }
  30. readLiteral:
  31. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  32. {
  33. var v int
  34. {
  35. // Inlined v, err := f.huffSym(f.hl)
  36. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  37. // with single element, huffSym must error on these two edge cases. In both
  38. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  39. // satisfy the n == 0 check below.
  40. n := uint(f.hl.maxRead)
  41. for {
  42. for fnb < n {
  43. c, err := fr.ReadByte()
  44. if err != nil {
  45. f.b, f.nb = fb, fnb
  46. f.err = noEOF(err)
  47. return
  48. }
  49. f.roffset++
  50. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  51. fnb += 8
  52. }
  53. chunk := f.hl.chunks[fb&(huffmanNumChunks-1)]
  54. n = uint(chunk & huffmanCountMask)
  55. if n > huffmanChunkBits {
  56. chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask]
  57. n = uint(chunk & huffmanCountMask)
  58. }
  59. if n <= fnb {
  60. if n == 0 {
  61. f.b, f.nb = fb, fnb
  62. if debugDecode {
  63. fmt.Println("huffsym: n==0")
  64. }
  65. f.err = CorruptInputError(f.roffset)
  66. return
  67. }
  68. fb = fb >> (n & regSizeMaskUint32)
  69. fnb = fnb - n
  70. v = int(chunk >> huffmanValueShift)
  71. break
  72. }
  73. }
  74. }
  75. var length int
  76. switch {
  77. case v < 256:
  78. dict.writeByte(byte(v))
  79. if dict.availWrite() == 0 {
  80. f.toRead = dict.readFlush()
  81. f.step = huffmanBytesBuffer
  82. f.stepState = stateInit
  83. f.b, f.nb = fb, fnb
  84. return
  85. }
  86. goto readLiteral
  87. case v == 256:
  88. f.b, f.nb = fb, fnb
  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 fnb < n {
  99. c, err := fr.ReadByte()
  100. if err != nil {
  101. f.b, f.nb = fb, fnb
  102. if debugDecode {
  103. fmt.Println("morebits n>0:", err)
  104. }
  105. f.err = err
  106. return
  107. }
  108. f.roffset++
  109. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  110. fnb += 8
  111. }
  112. length += int(fb & bitMask32[n])
  113. fb >>= n & regSizeMaskUint32
  114. fnb -= n
  115. default:
  116. if debugDecode {
  117. fmt.Println(v, ">= maxNumLit")
  118. }
  119. f.err = CorruptInputError(f.roffset)
  120. f.b, f.nb = fb, fnb
  121. return
  122. }
  123. var dist uint32
  124. if f.hd == nil {
  125. for fnb < 5 {
  126. c, err := fr.ReadByte()
  127. if err != nil {
  128. f.b, f.nb = fb, fnb
  129. if debugDecode {
  130. fmt.Println("morebits f.nb<5:", err)
  131. }
  132. f.err = err
  133. return
  134. }
  135. f.roffset++
  136. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  137. fnb += 8
  138. }
  139. dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3)))
  140. fb >>= 5
  141. fnb -= 5
  142. } else {
  143. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  144. // with single element, huffSym must error on these two edge cases. In both
  145. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  146. // satisfy the n == 0 check below.
  147. n := uint(f.hd.maxRead)
  148. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  149. // but is smart enough to keep local variables in registers, so use nb and b,
  150. // inline call to moreBits and reassign b,nb back to f on return.
  151. for {
  152. for fnb < n {
  153. c, err := fr.ReadByte()
  154. if err != nil {
  155. f.b, f.nb = fb, fnb
  156. f.err = noEOF(err)
  157. return
  158. }
  159. f.roffset++
  160. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  161. fnb += 8
  162. }
  163. chunk := f.hd.chunks[fb&(huffmanNumChunks-1)]
  164. n = uint(chunk & huffmanCountMask)
  165. if n > huffmanChunkBits {
  166. chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask]
  167. n = uint(chunk & huffmanCountMask)
  168. }
  169. if n <= fnb {
  170. if n == 0 {
  171. f.b, f.nb = fb, fnb
  172. if debugDecode {
  173. fmt.Println("huffsym: n==0")
  174. }
  175. f.err = CorruptInputError(f.roffset)
  176. return
  177. }
  178. fb = fb >> (n & regSizeMaskUint32)
  179. fnb = fnb - 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 fnb < nb {
  193. c, err := fr.ReadByte()
  194. if err != nil {
  195. f.b, f.nb = fb, fnb
  196. if debugDecode {
  197. fmt.Println("morebits f.nb<nb:", err)
  198. }
  199. f.err = err
  200. return
  201. }
  202. f.roffset++
  203. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  204. fnb += 8
  205. }
  206. extra |= fb & bitMask32[nb]
  207. fb >>= nb & regSizeMaskUint32
  208. fnb -= nb
  209. dist = 1<<((nb+1)&regSizeMaskUint32) + 1 + extra
  210. // slower: dist = bitMask32[nb+1] + 2 + extra
  211. default:
  212. f.b, f.nb = fb, fnb
  213. if debugDecode {
  214. fmt.Println("dist too big:", dist, maxNumDist)
  215. }
  216. f.err = CorruptInputError(f.roffset)
  217. return
  218. }
  219. // No check on length; encoding can be prescient.
  220. if dist > uint32(dict.histSize()) {
  221. f.b, f.nb = fb, fnb
  222. if debugDecode {
  223. fmt.Println("dist > dict.histSize():", dist, dict.histSize())
  224. }
  225. f.err = CorruptInputError(f.roffset)
  226. return
  227. }
  228. f.copyLen, f.copyDist = length, int(dist)
  229. goto copyHistory
  230. }
  231. copyHistory:
  232. // Perform a backwards copy according to RFC section 3.2.3.
  233. {
  234. cnt := dict.tryWriteCopy(f.copyDist, f.copyLen)
  235. if cnt == 0 {
  236. cnt = dict.writeCopy(f.copyDist, f.copyLen)
  237. }
  238. f.copyLen -= cnt
  239. if dict.availWrite() == 0 || f.copyLen > 0 {
  240. f.toRead = dict.readFlush()
  241. f.step = huffmanBytesBuffer // We need to continue this work
  242. f.stepState = stateDict
  243. f.b, f.nb = fb, fnb
  244. return
  245. }
  246. goto readLiteral
  247. }
  248. // Not reached
  249. }
  250. // Decode a single Huffman block from f.
  251. // hl and hd are the Huffman states for the lit/length values
  252. // and the distance values, respectively. If hd == nil, using the
  253. // fixed distance encoding associated with fixed Huffman blocks.
  254. func (f *decompressor) huffmanBytesReader() {
  255. const (
  256. stateInit = iota // Zero value must be stateInit
  257. stateDict
  258. )
  259. fr := f.r.(*bytes.Reader)
  260. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  261. // but is smart enough to keep local variables in registers, so use nb and b,
  262. // inline call to moreBits and reassign b,nb back to f on return.
  263. fnb, fb, dict := f.nb, f.b, &f.dict
  264. switch f.stepState {
  265. case stateInit:
  266. goto readLiteral
  267. case stateDict:
  268. goto copyHistory
  269. }
  270. readLiteral:
  271. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  272. {
  273. var v int
  274. {
  275. // Inlined v, err := f.huffSym(f.hl)
  276. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  277. // with single element, huffSym must error on these two edge cases. In both
  278. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  279. // satisfy the n == 0 check below.
  280. n := uint(f.hl.maxRead)
  281. for {
  282. for fnb < n {
  283. c, err := fr.ReadByte()
  284. if err != nil {
  285. f.b, f.nb = fb, fnb
  286. f.err = noEOF(err)
  287. return
  288. }
  289. f.roffset++
  290. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  291. fnb += 8
  292. }
  293. chunk := f.hl.chunks[fb&(huffmanNumChunks-1)]
  294. n = uint(chunk & huffmanCountMask)
  295. if n > huffmanChunkBits {
  296. chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask]
  297. n = uint(chunk & huffmanCountMask)
  298. }
  299. if n <= fnb {
  300. if n == 0 {
  301. f.b, f.nb = fb, fnb
  302. if debugDecode {
  303. fmt.Println("huffsym: n==0")
  304. }
  305. f.err = CorruptInputError(f.roffset)
  306. return
  307. }
  308. fb = fb >> (n & regSizeMaskUint32)
  309. fnb = fnb - n
  310. v = int(chunk >> huffmanValueShift)
  311. break
  312. }
  313. }
  314. }
  315. var length int
  316. switch {
  317. case v < 256:
  318. dict.writeByte(byte(v))
  319. if dict.availWrite() == 0 {
  320. f.toRead = dict.readFlush()
  321. f.step = huffmanBytesReader
  322. f.stepState = stateInit
  323. f.b, f.nb = fb, fnb
  324. return
  325. }
  326. goto readLiteral
  327. case v == 256:
  328. f.b, f.nb = fb, fnb
  329. f.finishBlock()
  330. return
  331. // otherwise, reference to older data
  332. case v < 265:
  333. length = v - (257 - 3)
  334. case v < maxNumLit:
  335. val := decCodeToLen[(v - 257)]
  336. length = int(val.length) + 3
  337. n := uint(val.extra)
  338. for fnb < n {
  339. c, err := fr.ReadByte()
  340. if err != nil {
  341. f.b, f.nb = fb, fnb
  342. if debugDecode {
  343. fmt.Println("morebits n>0:", err)
  344. }
  345. f.err = err
  346. return
  347. }
  348. f.roffset++
  349. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  350. fnb += 8
  351. }
  352. length += int(fb & bitMask32[n])
  353. fb >>= n & regSizeMaskUint32
  354. fnb -= n
  355. default:
  356. if debugDecode {
  357. fmt.Println(v, ">= maxNumLit")
  358. }
  359. f.err = CorruptInputError(f.roffset)
  360. f.b, f.nb = fb, fnb
  361. return
  362. }
  363. var dist uint32
  364. if f.hd == nil {
  365. for fnb < 5 {
  366. c, err := fr.ReadByte()
  367. if err != nil {
  368. f.b, f.nb = fb, fnb
  369. if debugDecode {
  370. fmt.Println("morebits f.nb<5:", err)
  371. }
  372. f.err = err
  373. return
  374. }
  375. f.roffset++
  376. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  377. fnb += 8
  378. }
  379. dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3)))
  380. fb >>= 5
  381. fnb -= 5
  382. } else {
  383. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  384. // with single element, huffSym must error on these two edge cases. In both
  385. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  386. // satisfy the n == 0 check below.
  387. n := uint(f.hd.maxRead)
  388. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  389. // but is smart enough to keep local variables in registers, so use nb and b,
  390. // inline call to moreBits and reassign b,nb back to f on return.
  391. for {
  392. for fnb < n {
  393. c, err := fr.ReadByte()
  394. if err != nil {
  395. f.b, f.nb = fb, fnb
  396. f.err = noEOF(err)
  397. return
  398. }
  399. f.roffset++
  400. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  401. fnb += 8
  402. }
  403. chunk := f.hd.chunks[fb&(huffmanNumChunks-1)]
  404. n = uint(chunk & huffmanCountMask)
  405. if n > huffmanChunkBits {
  406. chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask]
  407. n = uint(chunk & huffmanCountMask)
  408. }
  409. if n <= fnb {
  410. if n == 0 {
  411. f.b, f.nb = fb, fnb
  412. if debugDecode {
  413. fmt.Println("huffsym: n==0")
  414. }
  415. f.err = CorruptInputError(f.roffset)
  416. return
  417. }
  418. fb = fb >> (n & regSizeMaskUint32)
  419. fnb = fnb - n
  420. dist = uint32(chunk >> huffmanValueShift)
  421. break
  422. }
  423. }
  424. }
  425. switch {
  426. case dist < 4:
  427. dist++
  428. case dist < maxNumDist:
  429. nb := uint(dist-2) >> 1
  430. // have 1 bit in bottom of dist, need nb more.
  431. extra := (dist & 1) << (nb & regSizeMaskUint32)
  432. for fnb < nb {
  433. c, err := fr.ReadByte()
  434. if err != nil {
  435. f.b, f.nb = fb, fnb
  436. if debugDecode {
  437. fmt.Println("morebits f.nb<nb:", err)
  438. }
  439. f.err = err
  440. return
  441. }
  442. f.roffset++
  443. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  444. fnb += 8
  445. }
  446. extra |= fb & bitMask32[nb]
  447. fb >>= nb & regSizeMaskUint32
  448. fnb -= nb
  449. dist = 1<<((nb+1)&regSizeMaskUint32) + 1 + extra
  450. // slower: dist = bitMask32[nb+1] + 2 + extra
  451. default:
  452. f.b, f.nb = fb, fnb
  453. if debugDecode {
  454. fmt.Println("dist too big:", dist, maxNumDist)
  455. }
  456. f.err = CorruptInputError(f.roffset)
  457. return
  458. }
  459. // No check on length; encoding can be prescient.
  460. if dist > uint32(dict.histSize()) {
  461. f.b, f.nb = fb, fnb
  462. if debugDecode {
  463. fmt.Println("dist > dict.histSize():", dist, dict.histSize())
  464. }
  465. f.err = CorruptInputError(f.roffset)
  466. return
  467. }
  468. f.copyLen, f.copyDist = length, int(dist)
  469. goto copyHistory
  470. }
  471. copyHistory:
  472. // Perform a backwards copy according to RFC section 3.2.3.
  473. {
  474. cnt := dict.tryWriteCopy(f.copyDist, f.copyLen)
  475. if cnt == 0 {
  476. cnt = dict.writeCopy(f.copyDist, f.copyLen)
  477. }
  478. f.copyLen -= cnt
  479. if dict.availWrite() == 0 || f.copyLen > 0 {
  480. f.toRead = dict.readFlush()
  481. f.step = huffmanBytesReader // We need to continue this work
  482. f.stepState = stateDict
  483. f.b, f.nb = fb, fnb
  484. return
  485. }
  486. goto readLiteral
  487. }
  488. // Not reached
  489. }
  490. // Decode a single Huffman block from f.
  491. // hl and hd are the Huffman states for the lit/length values
  492. // and the distance values, respectively. If hd == nil, using the
  493. // fixed distance encoding associated with fixed Huffman blocks.
  494. func (f *decompressor) huffmanBufioReader() {
  495. const (
  496. stateInit = iota // Zero value must be stateInit
  497. stateDict
  498. )
  499. fr := f.r.(*bufio.Reader)
  500. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  501. // but is smart enough to keep local variables in registers, so use nb and b,
  502. // inline call to moreBits and reassign b,nb back to f on return.
  503. fnb, fb, dict := f.nb, f.b, &f.dict
  504. switch f.stepState {
  505. case stateInit:
  506. goto readLiteral
  507. case stateDict:
  508. goto copyHistory
  509. }
  510. readLiteral:
  511. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  512. {
  513. var v int
  514. {
  515. // Inlined v, err := f.huffSym(f.hl)
  516. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  517. // with single element, huffSym must error on these two edge cases. In both
  518. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  519. // satisfy the n == 0 check below.
  520. n := uint(f.hl.maxRead)
  521. for {
  522. for fnb < n {
  523. c, err := fr.ReadByte()
  524. if err != nil {
  525. f.b, f.nb = fb, fnb
  526. f.err = noEOF(err)
  527. return
  528. }
  529. f.roffset++
  530. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  531. fnb += 8
  532. }
  533. chunk := f.hl.chunks[fb&(huffmanNumChunks-1)]
  534. n = uint(chunk & huffmanCountMask)
  535. if n > huffmanChunkBits {
  536. chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask]
  537. n = uint(chunk & huffmanCountMask)
  538. }
  539. if n <= fnb {
  540. if n == 0 {
  541. f.b, f.nb = fb, fnb
  542. if debugDecode {
  543. fmt.Println("huffsym: n==0")
  544. }
  545. f.err = CorruptInputError(f.roffset)
  546. return
  547. }
  548. fb = fb >> (n & regSizeMaskUint32)
  549. fnb = fnb - n
  550. v = int(chunk >> huffmanValueShift)
  551. break
  552. }
  553. }
  554. }
  555. var length int
  556. switch {
  557. case v < 256:
  558. dict.writeByte(byte(v))
  559. if dict.availWrite() == 0 {
  560. f.toRead = dict.readFlush()
  561. f.step = huffmanBufioReader
  562. f.stepState = stateInit
  563. f.b, f.nb = fb, fnb
  564. return
  565. }
  566. goto readLiteral
  567. case v == 256:
  568. f.b, f.nb = fb, fnb
  569. f.finishBlock()
  570. return
  571. // otherwise, reference to older data
  572. case v < 265:
  573. length = v - (257 - 3)
  574. case v < maxNumLit:
  575. val := decCodeToLen[(v - 257)]
  576. length = int(val.length) + 3
  577. n := uint(val.extra)
  578. for fnb < n {
  579. c, err := fr.ReadByte()
  580. if err != nil {
  581. f.b, f.nb = fb, fnb
  582. if debugDecode {
  583. fmt.Println("morebits n>0:", err)
  584. }
  585. f.err = err
  586. return
  587. }
  588. f.roffset++
  589. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  590. fnb += 8
  591. }
  592. length += int(fb & bitMask32[n])
  593. fb >>= n & regSizeMaskUint32
  594. fnb -= n
  595. default:
  596. if debugDecode {
  597. fmt.Println(v, ">= maxNumLit")
  598. }
  599. f.err = CorruptInputError(f.roffset)
  600. f.b, f.nb = fb, fnb
  601. return
  602. }
  603. var dist uint32
  604. if f.hd == nil {
  605. for fnb < 5 {
  606. c, err := fr.ReadByte()
  607. if err != nil {
  608. f.b, f.nb = fb, fnb
  609. if debugDecode {
  610. fmt.Println("morebits f.nb<5:", err)
  611. }
  612. f.err = err
  613. return
  614. }
  615. f.roffset++
  616. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  617. fnb += 8
  618. }
  619. dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3)))
  620. fb >>= 5
  621. fnb -= 5
  622. } else {
  623. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  624. // with single element, huffSym must error on these two edge cases. In both
  625. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  626. // satisfy the n == 0 check below.
  627. n := uint(f.hd.maxRead)
  628. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  629. // but is smart enough to keep local variables in registers, so use nb and b,
  630. // inline call to moreBits and reassign b,nb back to f on return.
  631. for {
  632. for fnb < n {
  633. c, err := fr.ReadByte()
  634. if err != nil {
  635. f.b, f.nb = fb, fnb
  636. f.err = noEOF(err)
  637. return
  638. }
  639. f.roffset++
  640. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  641. fnb += 8
  642. }
  643. chunk := f.hd.chunks[fb&(huffmanNumChunks-1)]
  644. n = uint(chunk & huffmanCountMask)
  645. if n > huffmanChunkBits {
  646. chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask]
  647. n = uint(chunk & huffmanCountMask)
  648. }
  649. if n <= fnb {
  650. if n == 0 {
  651. f.b, f.nb = fb, fnb
  652. if debugDecode {
  653. fmt.Println("huffsym: n==0")
  654. }
  655. f.err = CorruptInputError(f.roffset)
  656. return
  657. }
  658. fb = fb >> (n & regSizeMaskUint32)
  659. fnb = fnb - n
  660. dist = uint32(chunk >> huffmanValueShift)
  661. break
  662. }
  663. }
  664. }
  665. switch {
  666. case dist < 4:
  667. dist++
  668. case dist < maxNumDist:
  669. nb := uint(dist-2) >> 1
  670. // have 1 bit in bottom of dist, need nb more.
  671. extra := (dist & 1) << (nb & regSizeMaskUint32)
  672. for fnb < nb {
  673. c, err := fr.ReadByte()
  674. if err != nil {
  675. f.b, f.nb = fb, fnb
  676. if debugDecode {
  677. fmt.Println("morebits f.nb<nb:", err)
  678. }
  679. f.err = err
  680. return
  681. }
  682. f.roffset++
  683. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  684. fnb += 8
  685. }
  686. extra |= fb & bitMask32[nb]
  687. fb >>= nb & regSizeMaskUint32
  688. fnb -= nb
  689. dist = 1<<((nb+1)&regSizeMaskUint32) + 1 + extra
  690. // slower: dist = bitMask32[nb+1] + 2 + extra
  691. default:
  692. f.b, f.nb = fb, fnb
  693. if debugDecode {
  694. fmt.Println("dist too big:", dist, maxNumDist)
  695. }
  696. f.err = CorruptInputError(f.roffset)
  697. return
  698. }
  699. // No check on length; encoding can be prescient.
  700. if dist > uint32(dict.histSize()) {
  701. f.b, f.nb = fb, fnb
  702. if debugDecode {
  703. fmt.Println("dist > dict.histSize():", dist, dict.histSize())
  704. }
  705. f.err = CorruptInputError(f.roffset)
  706. return
  707. }
  708. f.copyLen, f.copyDist = length, int(dist)
  709. goto copyHistory
  710. }
  711. copyHistory:
  712. // Perform a backwards copy according to RFC section 3.2.3.
  713. {
  714. cnt := dict.tryWriteCopy(f.copyDist, f.copyLen)
  715. if cnt == 0 {
  716. cnt = dict.writeCopy(f.copyDist, f.copyLen)
  717. }
  718. f.copyLen -= cnt
  719. if dict.availWrite() == 0 || f.copyLen > 0 {
  720. f.toRead = dict.readFlush()
  721. f.step = huffmanBufioReader // We need to continue this work
  722. f.stepState = stateDict
  723. f.b, f.nb = fb, fnb
  724. return
  725. }
  726. goto readLiteral
  727. }
  728. // Not reached
  729. }
  730. // Decode a single Huffman block from f.
  731. // hl and hd are the Huffman states for the lit/length values
  732. // and the distance values, respectively. If hd == nil, using the
  733. // fixed distance encoding associated with fixed Huffman blocks.
  734. func (f *decompressor) huffmanStringsReader() {
  735. const (
  736. stateInit = iota // Zero value must be stateInit
  737. stateDict
  738. )
  739. fr := f.r.(*strings.Reader)
  740. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  741. // but is smart enough to keep local variables in registers, so use nb and b,
  742. // inline call to moreBits and reassign b,nb back to f on return.
  743. fnb, fb, dict := f.nb, f.b, &f.dict
  744. switch f.stepState {
  745. case stateInit:
  746. goto readLiteral
  747. case stateDict:
  748. goto copyHistory
  749. }
  750. readLiteral:
  751. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  752. {
  753. var v int
  754. {
  755. // Inlined v, err := f.huffSym(f.hl)
  756. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  757. // with single element, huffSym must error on these two edge cases. In both
  758. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  759. // satisfy the n == 0 check below.
  760. n := uint(f.hl.maxRead)
  761. for {
  762. for fnb < n {
  763. c, err := fr.ReadByte()
  764. if err != nil {
  765. f.b, f.nb = fb, fnb
  766. f.err = noEOF(err)
  767. return
  768. }
  769. f.roffset++
  770. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  771. fnb += 8
  772. }
  773. chunk := f.hl.chunks[fb&(huffmanNumChunks-1)]
  774. n = uint(chunk & huffmanCountMask)
  775. if n > huffmanChunkBits {
  776. chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask]
  777. n = uint(chunk & huffmanCountMask)
  778. }
  779. if n <= fnb {
  780. if n == 0 {
  781. f.b, f.nb = fb, fnb
  782. if debugDecode {
  783. fmt.Println("huffsym: n==0")
  784. }
  785. f.err = CorruptInputError(f.roffset)
  786. return
  787. }
  788. fb = fb >> (n & regSizeMaskUint32)
  789. fnb = fnb - n
  790. v = int(chunk >> huffmanValueShift)
  791. break
  792. }
  793. }
  794. }
  795. var length int
  796. switch {
  797. case v < 256:
  798. dict.writeByte(byte(v))
  799. if dict.availWrite() == 0 {
  800. f.toRead = dict.readFlush()
  801. f.step = huffmanStringsReader
  802. f.stepState = stateInit
  803. f.b, f.nb = fb, fnb
  804. return
  805. }
  806. goto readLiteral
  807. case v == 256:
  808. f.b, f.nb = fb, fnb
  809. f.finishBlock()
  810. return
  811. // otherwise, reference to older data
  812. case v < 265:
  813. length = v - (257 - 3)
  814. case v < maxNumLit:
  815. val := decCodeToLen[(v - 257)]
  816. length = int(val.length) + 3
  817. n := uint(val.extra)
  818. for fnb < n {
  819. c, err := fr.ReadByte()
  820. if err != nil {
  821. f.b, f.nb = fb, fnb
  822. if debugDecode {
  823. fmt.Println("morebits n>0:", err)
  824. }
  825. f.err = err
  826. return
  827. }
  828. f.roffset++
  829. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  830. fnb += 8
  831. }
  832. length += int(fb & bitMask32[n])
  833. fb >>= n & regSizeMaskUint32
  834. fnb -= n
  835. default:
  836. if debugDecode {
  837. fmt.Println(v, ">= maxNumLit")
  838. }
  839. f.err = CorruptInputError(f.roffset)
  840. f.b, f.nb = fb, fnb
  841. return
  842. }
  843. var dist uint32
  844. if f.hd == nil {
  845. for fnb < 5 {
  846. c, err := fr.ReadByte()
  847. if err != nil {
  848. f.b, f.nb = fb, fnb
  849. if debugDecode {
  850. fmt.Println("morebits f.nb<5:", err)
  851. }
  852. f.err = err
  853. return
  854. }
  855. f.roffset++
  856. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  857. fnb += 8
  858. }
  859. dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3)))
  860. fb >>= 5
  861. fnb -= 5
  862. } else {
  863. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  864. // with single element, huffSym must error on these two edge cases. In both
  865. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  866. // satisfy the n == 0 check below.
  867. n := uint(f.hd.maxRead)
  868. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  869. // but is smart enough to keep local variables in registers, so use nb and b,
  870. // inline call to moreBits and reassign b,nb back to f on return.
  871. for {
  872. for fnb < n {
  873. c, err := fr.ReadByte()
  874. if err != nil {
  875. f.b, f.nb = fb, fnb
  876. f.err = noEOF(err)
  877. return
  878. }
  879. f.roffset++
  880. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  881. fnb += 8
  882. }
  883. chunk := f.hd.chunks[fb&(huffmanNumChunks-1)]
  884. n = uint(chunk & huffmanCountMask)
  885. if n > huffmanChunkBits {
  886. chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask]
  887. n = uint(chunk & huffmanCountMask)
  888. }
  889. if n <= fnb {
  890. if n == 0 {
  891. f.b, f.nb = fb, fnb
  892. if debugDecode {
  893. fmt.Println("huffsym: n==0")
  894. }
  895. f.err = CorruptInputError(f.roffset)
  896. return
  897. }
  898. fb = fb >> (n & regSizeMaskUint32)
  899. fnb = fnb - n
  900. dist = uint32(chunk >> huffmanValueShift)
  901. break
  902. }
  903. }
  904. }
  905. switch {
  906. case dist < 4:
  907. dist++
  908. case dist < maxNumDist:
  909. nb := uint(dist-2) >> 1
  910. // have 1 bit in bottom of dist, need nb more.
  911. extra := (dist & 1) << (nb & regSizeMaskUint32)
  912. for fnb < nb {
  913. c, err := fr.ReadByte()
  914. if err != nil {
  915. f.b, f.nb = fb, fnb
  916. if debugDecode {
  917. fmt.Println("morebits f.nb<nb:", err)
  918. }
  919. f.err = err
  920. return
  921. }
  922. f.roffset++
  923. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  924. fnb += 8
  925. }
  926. extra |= fb & bitMask32[nb]
  927. fb >>= nb & regSizeMaskUint32
  928. fnb -= nb
  929. dist = 1<<((nb+1)&regSizeMaskUint32) + 1 + extra
  930. // slower: dist = bitMask32[nb+1] + 2 + extra
  931. default:
  932. f.b, f.nb = fb, fnb
  933. if debugDecode {
  934. fmt.Println("dist too big:", dist, maxNumDist)
  935. }
  936. f.err = CorruptInputError(f.roffset)
  937. return
  938. }
  939. // No check on length; encoding can be prescient.
  940. if dist > uint32(dict.histSize()) {
  941. f.b, f.nb = fb, fnb
  942. if debugDecode {
  943. fmt.Println("dist > dict.histSize():", dist, dict.histSize())
  944. }
  945. f.err = CorruptInputError(f.roffset)
  946. return
  947. }
  948. f.copyLen, f.copyDist = length, int(dist)
  949. goto copyHistory
  950. }
  951. copyHistory:
  952. // Perform a backwards copy according to RFC section 3.2.3.
  953. {
  954. cnt := dict.tryWriteCopy(f.copyDist, f.copyLen)
  955. if cnt == 0 {
  956. cnt = dict.writeCopy(f.copyDist, f.copyLen)
  957. }
  958. f.copyLen -= cnt
  959. if dict.availWrite() == 0 || f.copyLen > 0 {
  960. f.toRead = dict.readFlush()
  961. f.step = huffmanStringsReader // We need to continue this work
  962. f.stepState = stateDict
  963. f.b, f.nb = fb, fnb
  964. return
  965. }
  966. goto readLiteral
  967. }
  968. // Not reached
  969. }
  970. // Decode a single Huffman block from f.
  971. // hl and hd are the Huffman states for the lit/length values
  972. // and the distance values, respectively. If hd == nil, using the
  973. // fixed distance encoding associated with fixed Huffman blocks.
  974. func (f *decompressor) huffmanGenericReader() {
  975. const (
  976. stateInit = iota // Zero value must be stateInit
  977. stateDict
  978. )
  979. fr := f.r.(Reader)
  980. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  981. // but is smart enough to keep local variables in registers, so use nb and b,
  982. // inline call to moreBits and reassign b,nb back to f on return.
  983. fnb, fb, dict := f.nb, f.b, &f.dict
  984. switch f.stepState {
  985. case stateInit:
  986. goto readLiteral
  987. case stateDict:
  988. goto copyHistory
  989. }
  990. readLiteral:
  991. // Read literal and/or (length, distance) according to RFC section 3.2.3.
  992. {
  993. var v int
  994. {
  995. // Inlined v, err := f.huffSym(f.hl)
  996. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  997. // with single element, huffSym must error on these two edge cases. In both
  998. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  999. // satisfy the n == 0 check below.
  1000. n := uint(f.hl.maxRead)
  1001. for {
  1002. for fnb < n {
  1003. c, err := fr.ReadByte()
  1004. if err != nil {
  1005. f.b, f.nb = fb, fnb
  1006. f.err = noEOF(err)
  1007. return
  1008. }
  1009. f.roffset++
  1010. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  1011. fnb += 8
  1012. }
  1013. chunk := f.hl.chunks[fb&(huffmanNumChunks-1)]
  1014. n = uint(chunk & huffmanCountMask)
  1015. if n > huffmanChunkBits {
  1016. chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask]
  1017. n = uint(chunk & huffmanCountMask)
  1018. }
  1019. if n <= fnb {
  1020. if n == 0 {
  1021. f.b, f.nb = fb, fnb
  1022. if debugDecode {
  1023. fmt.Println("huffsym: n==0")
  1024. }
  1025. f.err = CorruptInputError(f.roffset)
  1026. return
  1027. }
  1028. fb = fb >> (n & regSizeMaskUint32)
  1029. fnb = fnb - n
  1030. v = int(chunk >> huffmanValueShift)
  1031. break
  1032. }
  1033. }
  1034. }
  1035. var length int
  1036. switch {
  1037. case v < 256:
  1038. dict.writeByte(byte(v))
  1039. if dict.availWrite() == 0 {
  1040. f.toRead = dict.readFlush()
  1041. f.step = huffmanGenericReader
  1042. f.stepState = stateInit
  1043. f.b, f.nb = fb, fnb
  1044. return
  1045. }
  1046. goto readLiteral
  1047. case v == 256:
  1048. f.b, f.nb = fb, fnb
  1049. f.finishBlock()
  1050. return
  1051. // otherwise, reference to older data
  1052. case v < 265:
  1053. length = v - (257 - 3)
  1054. case v < maxNumLit:
  1055. val := decCodeToLen[(v - 257)]
  1056. length = int(val.length) + 3
  1057. n := uint(val.extra)
  1058. for fnb < n {
  1059. c, err := fr.ReadByte()
  1060. if err != nil {
  1061. f.b, f.nb = fb, fnb
  1062. if debugDecode {
  1063. fmt.Println("morebits n>0:", err)
  1064. }
  1065. f.err = err
  1066. return
  1067. }
  1068. f.roffset++
  1069. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  1070. fnb += 8
  1071. }
  1072. length += int(fb & bitMask32[n])
  1073. fb >>= n & regSizeMaskUint32
  1074. fnb -= n
  1075. default:
  1076. if debugDecode {
  1077. fmt.Println(v, ">= maxNumLit")
  1078. }
  1079. f.err = CorruptInputError(f.roffset)
  1080. f.b, f.nb = fb, fnb
  1081. return
  1082. }
  1083. var dist uint32
  1084. if f.hd == nil {
  1085. for fnb < 5 {
  1086. c, err := fr.ReadByte()
  1087. if err != nil {
  1088. f.b, f.nb = fb, fnb
  1089. if debugDecode {
  1090. fmt.Println("morebits f.nb<5:", err)
  1091. }
  1092. f.err = err
  1093. return
  1094. }
  1095. f.roffset++
  1096. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  1097. fnb += 8
  1098. }
  1099. dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3)))
  1100. fb >>= 5
  1101. fnb -= 5
  1102. } else {
  1103. // Since a huffmanDecoder can be empty or be composed of a degenerate tree
  1104. // with single element, huffSym must error on these two edge cases. In both
  1105. // cases, the chunks slice will be 0 for the invalid sequence, leading it
  1106. // satisfy the n == 0 check below.
  1107. n := uint(f.hd.maxRead)
  1108. // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
  1109. // but is smart enough to keep local variables in registers, so use nb and b,
  1110. // inline call to moreBits and reassign b,nb back to f on return.
  1111. for {
  1112. for fnb < n {
  1113. c, err := fr.ReadByte()
  1114. if err != nil {
  1115. f.b, f.nb = fb, fnb
  1116. f.err = noEOF(err)
  1117. return
  1118. }
  1119. f.roffset++
  1120. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  1121. fnb += 8
  1122. }
  1123. chunk := f.hd.chunks[fb&(huffmanNumChunks-1)]
  1124. n = uint(chunk & huffmanCountMask)
  1125. if n > huffmanChunkBits {
  1126. chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask]
  1127. n = uint(chunk & huffmanCountMask)
  1128. }
  1129. if n <= fnb {
  1130. if n == 0 {
  1131. f.b, f.nb = fb, fnb
  1132. if debugDecode {
  1133. fmt.Println("huffsym: n==0")
  1134. }
  1135. f.err = CorruptInputError(f.roffset)
  1136. return
  1137. }
  1138. fb = fb >> (n & regSizeMaskUint32)
  1139. fnb = fnb - n
  1140. dist = uint32(chunk >> huffmanValueShift)
  1141. break
  1142. }
  1143. }
  1144. }
  1145. switch {
  1146. case dist < 4:
  1147. dist++
  1148. case dist < maxNumDist:
  1149. nb := uint(dist-2) >> 1
  1150. // have 1 bit in bottom of dist, need nb more.
  1151. extra := (dist & 1) << (nb & regSizeMaskUint32)
  1152. for fnb < nb {
  1153. c, err := fr.ReadByte()
  1154. if err != nil {
  1155. f.b, f.nb = fb, fnb
  1156. if debugDecode {
  1157. fmt.Println("morebits f.nb<nb:", err)
  1158. }
  1159. f.err = err
  1160. return
  1161. }
  1162. f.roffset++
  1163. fb |= uint32(c) << (fnb & regSizeMaskUint32)
  1164. fnb += 8
  1165. }
  1166. extra |= fb & bitMask32[nb]
  1167. fb >>= nb & regSizeMaskUint32
  1168. fnb -= nb
  1169. dist = 1<<((nb+1)&regSizeMaskUint32) + 1 + extra
  1170. // slower: dist = bitMask32[nb+1] + 2 + extra
  1171. default:
  1172. f.b, f.nb = fb, fnb
  1173. if debugDecode {
  1174. fmt.Println("dist too big:", dist, maxNumDist)
  1175. }
  1176. f.err = CorruptInputError(f.roffset)
  1177. return
  1178. }
  1179. // No check on length; encoding can be prescient.
  1180. if dist > uint32(dict.histSize()) {
  1181. f.b, f.nb = fb, fnb
  1182. if debugDecode {
  1183. fmt.Println("dist > dict.histSize():", dist, dict.histSize())
  1184. }
  1185. f.err = CorruptInputError(f.roffset)
  1186. return
  1187. }
  1188. f.copyLen, f.copyDist = length, int(dist)
  1189. goto copyHistory
  1190. }
  1191. copyHistory:
  1192. // Perform a backwards copy according to RFC section 3.2.3.
  1193. {
  1194. cnt := dict.tryWriteCopy(f.copyDist, f.copyLen)
  1195. if cnt == 0 {
  1196. cnt = dict.writeCopy(f.copyDist, f.copyLen)
  1197. }
  1198. f.copyLen -= cnt
  1199. if dict.availWrite() == 0 || f.copyLen > 0 {
  1200. f.toRead = dict.readFlush()
  1201. f.step = huffmanGenericReader // We need to continue this work
  1202. f.stepState = stateDict
  1203. f.b, f.nb = fb, fnb
  1204. return
  1205. }
  1206. goto readLiteral
  1207. }
  1208. // Not reached
  1209. }
  1210. func (f *decompressor) huffmanBlockDecoder() {
  1211. switch f.r.(type) {
  1212. case *bytes.Buffer:
  1213. f.huffmanBytesBuffer()
  1214. case *bytes.Reader:
  1215. f.huffmanBytesReader()
  1216. case *bufio.Reader:
  1217. f.huffmanBufioReader()
  1218. case *strings.Reader:
  1219. f.huffmanStringsReader()
  1220. case Reader:
  1221. f.huffmanGenericReader()
  1222. default:
  1223. f.huffmanGenericReader()
  1224. }
  1225. }