level5.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. package flate
  2. import "fmt"
  3. type fastEncL5 struct {
  4. fastGen
  5. table [tableSize]tableEntry
  6. bTable [tableSize]tableEntryPrev
  7. }
  8. func (e *fastEncL5) Encode(dst *tokens, src []byte) {
  9. const (
  10. inputMargin = 12 - 1
  11. minNonLiteralBlockSize = 1 + 1 + inputMargin
  12. hashShortBytes = 4
  13. )
  14. if debugDeflate && e.cur < 0 {
  15. panic(fmt.Sprint("e.cur < 0: ", e.cur))
  16. }
  17. // Protect against e.cur wraparound.
  18. for e.cur >= bufferReset {
  19. if len(e.hist) == 0 {
  20. for i := range e.table[:] {
  21. e.table[i] = tableEntry{}
  22. }
  23. for i := range e.bTable[:] {
  24. e.bTable[i] = tableEntryPrev{}
  25. }
  26. e.cur = maxMatchOffset
  27. break
  28. }
  29. // Shift down everything in the table that isn't already too far away.
  30. minOff := e.cur + int32(len(e.hist)) - maxMatchOffset
  31. for i := range e.table[:] {
  32. v := e.table[i].offset
  33. if v <= minOff {
  34. v = 0
  35. } else {
  36. v = v - e.cur + maxMatchOffset
  37. }
  38. e.table[i].offset = v
  39. }
  40. for i := range e.bTable[:] {
  41. v := e.bTable[i]
  42. if v.Cur.offset <= minOff {
  43. v.Cur.offset = 0
  44. v.Prev.offset = 0
  45. } else {
  46. v.Cur.offset = v.Cur.offset - e.cur + maxMatchOffset
  47. if v.Prev.offset <= minOff {
  48. v.Prev.offset = 0
  49. } else {
  50. v.Prev.offset = v.Prev.offset - e.cur + maxMatchOffset
  51. }
  52. }
  53. e.bTable[i] = v
  54. }
  55. e.cur = maxMatchOffset
  56. }
  57. s := e.addBlock(src)
  58. // This check isn't in the Snappy implementation, but there, the caller
  59. // instead of the callee handles this case.
  60. if len(src) < minNonLiteralBlockSize {
  61. // We do not fill the token table.
  62. // This will be picked up by caller.
  63. dst.n = uint16(len(src))
  64. return
  65. }
  66. // Override src
  67. src = e.hist
  68. nextEmit := s
  69. // sLimit is when to stop looking for offset/length copies. The inputMargin
  70. // lets us use a fast path for emitLiteral in the main loop, while we are
  71. // looking for copies.
  72. sLimit := int32(len(src) - inputMargin)
  73. // nextEmit is where in src the next emitLiteral should start from.
  74. cv := load6432(src, s)
  75. for {
  76. const skipLog = 6
  77. const doEvery = 1
  78. nextS := s
  79. var l int32
  80. var t int32
  81. for {
  82. nextHashS := hashLen(cv, tableBits, hashShortBytes)
  83. nextHashL := hash7(cv, tableBits)
  84. s = nextS
  85. nextS = s + doEvery + (s-nextEmit)>>skipLog
  86. if nextS > sLimit {
  87. goto emitRemainder
  88. }
  89. // Fetch a short+long candidate
  90. sCandidate := e.table[nextHashS]
  91. lCandidate := e.bTable[nextHashL]
  92. next := load6432(src, nextS)
  93. entry := tableEntry{offset: s + e.cur}
  94. e.table[nextHashS] = entry
  95. eLong := &e.bTable[nextHashL]
  96. eLong.Cur, eLong.Prev = entry, eLong.Cur
  97. nextHashS = hashLen(next, tableBits, hashShortBytes)
  98. nextHashL = hash7(next, tableBits)
  99. t = lCandidate.Cur.offset - e.cur
  100. if s-t < maxMatchOffset {
  101. if uint32(cv) == load3232(src, lCandidate.Cur.offset-e.cur) {
  102. // Store the next match
  103. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  104. eLong := &e.bTable[nextHashL]
  105. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  106. t2 := lCandidate.Prev.offset - e.cur
  107. if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  108. l = e.matchlen(s+4, t+4, src) + 4
  109. ml1 := e.matchlen(s+4, t2+4, src) + 4
  110. if ml1 > l {
  111. t = t2
  112. l = ml1
  113. break
  114. }
  115. }
  116. break
  117. }
  118. t = lCandidate.Prev.offset - e.cur
  119. if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  120. // Store the next match
  121. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  122. eLong := &e.bTable[nextHashL]
  123. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  124. break
  125. }
  126. }
  127. t = sCandidate.offset - e.cur
  128. if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
  129. // Found a 4 match...
  130. l = e.matchlen(s+4, t+4, src) + 4
  131. lCandidate = e.bTable[nextHashL]
  132. // Store the next match
  133. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  134. eLong := &e.bTable[nextHashL]
  135. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  136. // If the next long is a candidate, use that...
  137. t2 := lCandidate.Cur.offset - e.cur
  138. if nextS-t2 < maxMatchOffset {
  139. if load3232(src, lCandidate.Cur.offset-e.cur) == uint32(next) {
  140. ml := e.matchlen(nextS+4, t2+4, src) + 4
  141. if ml > l {
  142. t = t2
  143. s = nextS
  144. l = ml
  145. break
  146. }
  147. }
  148. // If the previous long is a candidate, use that...
  149. t2 = lCandidate.Prev.offset - e.cur
  150. if nextS-t2 < maxMatchOffset && load3232(src, lCandidate.Prev.offset-e.cur) == uint32(next) {
  151. ml := e.matchlen(nextS+4, t2+4, src) + 4
  152. if ml > l {
  153. t = t2
  154. s = nextS
  155. l = ml
  156. break
  157. }
  158. }
  159. }
  160. break
  161. }
  162. cv = next
  163. }
  164. // A 4-byte match has been found. We'll later see if more than 4 bytes
  165. // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
  166. // them as literal bytes.
  167. if l == 0 {
  168. // Extend the 4-byte match as long as possible.
  169. l = e.matchlenLong(s+4, t+4, src) + 4
  170. } else if l == maxMatchLength {
  171. l += e.matchlenLong(s+l, t+l, src)
  172. }
  173. // Try to locate a better match by checking the end of best match...
  174. if sAt := s + l; l < 30 && sAt < sLimit {
  175. // Allow some bytes at the beginning to mismatch.
  176. // Sweet spot is 2/3 bytes depending on input.
  177. // 3 is only a little better when it is but sometimes a lot worse.
  178. // The skipped bytes are tested in Extend backwards,
  179. // and still picked up as part of the match if they do.
  180. const skipBeginning = 2
  181. eLong := e.bTable[hash7(load6432(src, sAt), tableBits)].Cur.offset
  182. t2 := eLong - e.cur - l + skipBeginning
  183. s2 := s + skipBeginning
  184. off := s2 - t2
  185. if t2 >= 0 && off < maxMatchOffset && off > 0 {
  186. if l2 := e.matchlenLong(s2, t2, src); l2 > l {
  187. t = t2
  188. l = l2
  189. s = s2
  190. }
  191. }
  192. }
  193. // Extend backwards
  194. for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
  195. s--
  196. t--
  197. l++
  198. }
  199. if nextEmit < s {
  200. if false {
  201. emitLiteral(dst, src[nextEmit:s])
  202. } else {
  203. for _, v := range src[nextEmit:s] {
  204. dst.tokens[dst.n] = token(v)
  205. dst.litHist[v]++
  206. dst.n++
  207. }
  208. }
  209. }
  210. if debugDeflate {
  211. if t >= s {
  212. panic(fmt.Sprintln("s-t", s, t))
  213. }
  214. if (s - t) > maxMatchOffset {
  215. panic(fmt.Sprintln("mmo", s-t))
  216. }
  217. if l < baseMatchLength {
  218. panic("bml")
  219. }
  220. }
  221. dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))
  222. s += l
  223. nextEmit = s
  224. if nextS >= s {
  225. s = nextS + 1
  226. }
  227. if s >= sLimit {
  228. goto emitRemainder
  229. }
  230. // Store every 3rd hash in-between.
  231. if true {
  232. const hashEvery = 3
  233. i := s - l + 1
  234. if i < s-1 {
  235. cv := load6432(src, i)
  236. t := tableEntry{offset: i + e.cur}
  237. e.table[hashLen(cv, tableBits, hashShortBytes)] = t
  238. eLong := &e.bTable[hash7(cv, tableBits)]
  239. eLong.Cur, eLong.Prev = t, eLong.Cur
  240. // Do an long at i+1
  241. cv >>= 8
  242. t = tableEntry{offset: t.offset + 1}
  243. eLong = &e.bTable[hash7(cv, tableBits)]
  244. eLong.Cur, eLong.Prev = t, eLong.Cur
  245. // We only have enough bits for a short entry at i+2
  246. cv >>= 8
  247. t = tableEntry{offset: t.offset + 1}
  248. e.table[hashLen(cv, tableBits, hashShortBytes)] = t
  249. // Skip one - otherwise we risk hitting 's'
  250. i += 4
  251. for ; i < s-1; i += hashEvery {
  252. cv := load6432(src, i)
  253. t := tableEntry{offset: i + e.cur}
  254. t2 := tableEntry{offset: t.offset + 1}
  255. eLong := &e.bTable[hash7(cv, tableBits)]
  256. eLong.Cur, eLong.Prev = t, eLong.Cur
  257. e.table[hashLen(cv>>8, tableBits, hashShortBytes)] = t2
  258. }
  259. }
  260. }
  261. // We could immediately start working at s now, but to improve
  262. // compression we first update the hash table at s-1 and at s.
  263. x := load6432(src, s-1)
  264. o := e.cur + s - 1
  265. prevHashS := hashLen(x, tableBits, hashShortBytes)
  266. prevHashL := hash7(x, tableBits)
  267. e.table[prevHashS] = tableEntry{offset: o}
  268. eLong := &e.bTable[prevHashL]
  269. eLong.Cur, eLong.Prev = tableEntry{offset: o}, eLong.Cur
  270. cv = x >> 8
  271. }
  272. emitRemainder:
  273. if int(nextEmit) < len(src) {
  274. // If nothing was added, don't encode literals.
  275. if dst.n == 0 {
  276. return
  277. }
  278. emitLiteral(dst, src[nextEmit:])
  279. }
  280. }
  281. // fastEncL5Window is a level 5 encoder,
  282. // but with a custom window size.
  283. type fastEncL5Window struct {
  284. hist []byte
  285. cur int32
  286. maxOffset int32
  287. table [tableSize]tableEntry
  288. bTable [tableSize]tableEntryPrev
  289. }
  290. func (e *fastEncL5Window) Encode(dst *tokens, src []byte) {
  291. const (
  292. inputMargin = 12 - 1
  293. minNonLiteralBlockSize = 1 + 1 + inputMargin
  294. hashShortBytes = 4
  295. )
  296. maxMatchOffset := e.maxOffset
  297. if debugDeflate && e.cur < 0 {
  298. panic(fmt.Sprint("e.cur < 0: ", e.cur))
  299. }
  300. // Protect against e.cur wraparound.
  301. for e.cur >= bufferReset {
  302. if len(e.hist) == 0 {
  303. for i := range e.table[:] {
  304. e.table[i] = tableEntry{}
  305. }
  306. for i := range e.bTable[:] {
  307. e.bTable[i] = tableEntryPrev{}
  308. }
  309. e.cur = maxMatchOffset
  310. break
  311. }
  312. // Shift down everything in the table that isn't already too far away.
  313. minOff := e.cur + int32(len(e.hist)) - maxMatchOffset
  314. for i := range e.table[:] {
  315. v := e.table[i].offset
  316. if v <= minOff {
  317. v = 0
  318. } else {
  319. v = v - e.cur + maxMatchOffset
  320. }
  321. e.table[i].offset = v
  322. }
  323. for i := range e.bTable[:] {
  324. v := e.bTable[i]
  325. if v.Cur.offset <= minOff {
  326. v.Cur.offset = 0
  327. v.Prev.offset = 0
  328. } else {
  329. v.Cur.offset = v.Cur.offset - e.cur + maxMatchOffset
  330. if v.Prev.offset <= minOff {
  331. v.Prev.offset = 0
  332. } else {
  333. v.Prev.offset = v.Prev.offset - e.cur + maxMatchOffset
  334. }
  335. }
  336. e.bTable[i] = v
  337. }
  338. e.cur = maxMatchOffset
  339. }
  340. s := e.addBlock(src)
  341. // This check isn't in the Snappy implementation, but there, the caller
  342. // instead of the callee handles this case.
  343. if len(src) < minNonLiteralBlockSize {
  344. // We do not fill the token table.
  345. // This will be picked up by caller.
  346. dst.n = uint16(len(src))
  347. return
  348. }
  349. // Override src
  350. src = e.hist
  351. nextEmit := s
  352. // sLimit is when to stop looking for offset/length copies. The inputMargin
  353. // lets us use a fast path for emitLiteral in the main loop, while we are
  354. // looking for copies.
  355. sLimit := int32(len(src) - inputMargin)
  356. // nextEmit is where in src the next emitLiteral should start from.
  357. cv := load6432(src, s)
  358. for {
  359. const skipLog = 6
  360. const doEvery = 1
  361. nextS := s
  362. var l int32
  363. var t int32
  364. for {
  365. nextHashS := hashLen(cv, tableBits, hashShortBytes)
  366. nextHashL := hash7(cv, tableBits)
  367. s = nextS
  368. nextS = s + doEvery + (s-nextEmit)>>skipLog
  369. if nextS > sLimit {
  370. goto emitRemainder
  371. }
  372. // Fetch a short+long candidate
  373. sCandidate := e.table[nextHashS]
  374. lCandidate := e.bTable[nextHashL]
  375. next := load6432(src, nextS)
  376. entry := tableEntry{offset: s + e.cur}
  377. e.table[nextHashS] = entry
  378. eLong := &e.bTable[nextHashL]
  379. eLong.Cur, eLong.Prev = entry, eLong.Cur
  380. nextHashS = hashLen(next, tableBits, hashShortBytes)
  381. nextHashL = hash7(next, tableBits)
  382. t = lCandidate.Cur.offset - e.cur
  383. if s-t < maxMatchOffset {
  384. if uint32(cv) == load3232(src, lCandidate.Cur.offset-e.cur) {
  385. // Store the next match
  386. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  387. eLong := &e.bTable[nextHashL]
  388. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  389. t2 := lCandidate.Prev.offset - e.cur
  390. if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  391. l = e.matchlen(s+4, t+4, src) + 4
  392. ml1 := e.matchlen(s+4, t2+4, src) + 4
  393. if ml1 > l {
  394. t = t2
  395. l = ml1
  396. break
  397. }
  398. }
  399. break
  400. }
  401. t = lCandidate.Prev.offset - e.cur
  402. if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
  403. // Store the next match
  404. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  405. eLong := &e.bTable[nextHashL]
  406. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  407. break
  408. }
  409. }
  410. t = sCandidate.offset - e.cur
  411. if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
  412. // Found a 4 match...
  413. l = e.matchlen(s+4, t+4, src) + 4
  414. lCandidate = e.bTable[nextHashL]
  415. // Store the next match
  416. e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
  417. eLong := &e.bTable[nextHashL]
  418. eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
  419. // If the next long is a candidate, use that...
  420. t2 := lCandidate.Cur.offset - e.cur
  421. if nextS-t2 < maxMatchOffset {
  422. if load3232(src, lCandidate.Cur.offset-e.cur) == uint32(next) {
  423. ml := e.matchlen(nextS+4, t2+4, src) + 4
  424. if ml > l {
  425. t = t2
  426. s = nextS
  427. l = ml
  428. break
  429. }
  430. }
  431. // If the previous long is a candidate, use that...
  432. t2 = lCandidate.Prev.offset - e.cur
  433. if nextS-t2 < maxMatchOffset && load3232(src, lCandidate.Prev.offset-e.cur) == uint32(next) {
  434. ml := e.matchlen(nextS+4, t2+4, src) + 4
  435. if ml > l {
  436. t = t2
  437. s = nextS
  438. l = ml
  439. break
  440. }
  441. }
  442. }
  443. break
  444. }
  445. cv = next
  446. }
  447. // A 4-byte match has been found. We'll later see if more than 4 bytes
  448. // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
  449. // them as literal bytes.
  450. if l == 0 {
  451. // Extend the 4-byte match as long as possible.
  452. l = e.matchlenLong(s+4, t+4, src) + 4
  453. } else if l == maxMatchLength {
  454. l += e.matchlenLong(s+l, t+l, src)
  455. }
  456. // Try to locate a better match by checking the end of best match...
  457. if sAt := s + l; l < 30 && sAt < sLimit {
  458. // Allow some bytes at the beginning to mismatch.
  459. // Sweet spot is 2/3 bytes depending on input.
  460. // 3 is only a little better when it is but sometimes a lot worse.
  461. // The skipped bytes are tested in Extend backwards,
  462. // and still picked up as part of the match if they do.
  463. const skipBeginning = 2
  464. eLong := e.bTable[hash7(load6432(src, sAt), tableBits)].Cur.offset
  465. t2 := eLong - e.cur - l + skipBeginning
  466. s2 := s + skipBeginning
  467. off := s2 - t2
  468. if t2 >= 0 && off < maxMatchOffset && off > 0 {
  469. if l2 := e.matchlenLong(s2, t2, src); l2 > l {
  470. t = t2
  471. l = l2
  472. s = s2
  473. }
  474. }
  475. }
  476. // Extend backwards
  477. for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
  478. s--
  479. t--
  480. l++
  481. }
  482. if nextEmit < s {
  483. if false {
  484. emitLiteral(dst, src[nextEmit:s])
  485. } else {
  486. for _, v := range src[nextEmit:s] {
  487. dst.tokens[dst.n] = token(v)
  488. dst.litHist[v]++
  489. dst.n++
  490. }
  491. }
  492. }
  493. if debugDeflate {
  494. if t >= s {
  495. panic(fmt.Sprintln("s-t", s, t))
  496. }
  497. if (s - t) > maxMatchOffset {
  498. panic(fmt.Sprintln("mmo", s-t))
  499. }
  500. if l < baseMatchLength {
  501. panic("bml")
  502. }
  503. }
  504. dst.AddMatchLong(l, uint32(s-t-baseMatchOffset))
  505. s += l
  506. nextEmit = s
  507. if nextS >= s {
  508. s = nextS + 1
  509. }
  510. if s >= sLimit {
  511. goto emitRemainder
  512. }
  513. // Store every 3rd hash in-between.
  514. if true {
  515. const hashEvery = 3
  516. i := s - l + 1
  517. if i < s-1 {
  518. cv := load6432(src, i)
  519. t := tableEntry{offset: i + e.cur}
  520. e.table[hashLen(cv, tableBits, hashShortBytes)] = t
  521. eLong := &e.bTable[hash7(cv, tableBits)]
  522. eLong.Cur, eLong.Prev = t, eLong.Cur
  523. // Do an long at i+1
  524. cv >>= 8
  525. t = tableEntry{offset: t.offset + 1}
  526. eLong = &e.bTable[hash7(cv, tableBits)]
  527. eLong.Cur, eLong.Prev = t, eLong.Cur
  528. // We only have enough bits for a short entry at i+2
  529. cv >>= 8
  530. t = tableEntry{offset: t.offset + 1}
  531. e.table[hashLen(cv, tableBits, hashShortBytes)] = t
  532. // Skip one - otherwise we risk hitting 's'
  533. i += 4
  534. for ; i < s-1; i += hashEvery {
  535. cv := load6432(src, i)
  536. t := tableEntry{offset: i + e.cur}
  537. t2 := tableEntry{offset: t.offset + 1}
  538. eLong := &e.bTable[hash7(cv, tableBits)]
  539. eLong.Cur, eLong.Prev = t, eLong.Cur
  540. e.table[hashLen(cv>>8, tableBits, hashShortBytes)] = t2
  541. }
  542. }
  543. }
  544. // We could immediately start working at s now, but to improve
  545. // compression we first update the hash table at s-1 and at s.
  546. x := load6432(src, s-1)
  547. o := e.cur + s - 1
  548. prevHashS := hashLen(x, tableBits, hashShortBytes)
  549. prevHashL := hash7(x, tableBits)
  550. e.table[prevHashS] = tableEntry{offset: o}
  551. eLong := &e.bTable[prevHashL]
  552. eLong.Cur, eLong.Prev = tableEntry{offset: o}, eLong.Cur
  553. cv = x >> 8
  554. }
  555. emitRemainder:
  556. if int(nextEmit) < len(src) {
  557. // If nothing was added, don't encode literals.
  558. if dst.n == 0 {
  559. return
  560. }
  561. emitLiteral(dst, src[nextEmit:])
  562. }
  563. }
  564. // Reset the encoding table.
  565. func (e *fastEncL5Window) Reset() {
  566. // We keep the same allocs, since we are compressing the same block sizes.
  567. if cap(e.hist) < allocHistory {
  568. e.hist = make([]byte, 0, allocHistory)
  569. }
  570. // We offset current position so everything will be out of reach.
  571. // If we are above the buffer reset it will be cleared anyway since len(hist) == 0.
  572. if e.cur <= int32(bufferReset) {
  573. e.cur += e.maxOffset + int32(len(e.hist))
  574. }
  575. e.hist = e.hist[:0]
  576. }
  577. func (e *fastEncL5Window) addBlock(src []byte) int32 {
  578. // check if we have space already
  579. maxMatchOffset := e.maxOffset
  580. if len(e.hist)+len(src) > cap(e.hist) {
  581. if cap(e.hist) == 0 {
  582. e.hist = make([]byte, 0, allocHistory)
  583. } else {
  584. if cap(e.hist) < int(maxMatchOffset*2) {
  585. panic("unexpected buffer size")
  586. }
  587. // Move down
  588. offset := int32(len(e.hist)) - maxMatchOffset
  589. copy(e.hist[0:maxMatchOffset], e.hist[offset:])
  590. e.cur += offset
  591. e.hist = e.hist[:maxMatchOffset]
  592. }
  593. }
  594. s := int32(len(e.hist))
  595. e.hist = append(e.hist, src...)
  596. return s
  597. }
  598. // matchlen will return the match length between offsets and t in src.
  599. // The maximum length returned is maxMatchLength - 4.
  600. // It is assumed that s > t, that t >=0 and s < len(src).
  601. func (e *fastEncL5Window) matchlen(s, t int32, src []byte) int32 {
  602. if debugDecode {
  603. if t >= s {
  604. panic(fmt.Sprint("t >=s:", t, s))
  605. }
  606. if int(s) >= len(src) {
  607. panic(fmt.Sprint("s >= len(src):", s, len(src)))
  608. }
  609. if t < 0 {
  610. panic(fmt.Sprint("t < 0:", t))
  611. }
  612. if s-t > e.maxOffset {
  613. panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
  614. }
  615. }
  616. s1 := int(s) + maxMatchLength - 4
  617. if s1 > len(src) {
  618. s1 = len(src)
  619. }
  620. // Extend the match to be as long as possible.
  621. return int32(matchLen(src[s:s1], src[t:]))
  622. }
  623. // matchlenLong will return the match length between offsets and t in src.
  624. // It is assumed that s > t, that t >=0 and s < len(src).
  625. func (e *fastEncL5Window) matchlenLong(s, t int32, src []byte) int32 {
  626. if debugDeflate {
  627. if t >= s {
  628. panic(fmt.Sprint("t >=s:", t, s))
  629. }
  630. if int(s) >= len(src) {
  631. panic(fmt.Sprint("s >= len(src):", s, len(src)))
  632. }
  633. if t < 0 {
  634. panic(fmt.Sprint("t < 0:", t))
  635. }
  636. if s-t > e.maxOffset {
  637. panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
  638. }
  639. }
  640. // Extend the match to be as long as possible.
  641. return int32(matchLen(src[s:], src[t:]))
  642. }