token.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package flate
  5. import "log"
  6. // The length code for length X (MIN_MATCH_LENGTH <= X <= MAX_MATCH_LENGTH)
  7. // is lengthCodes[length - MIN_MATCH_LENGTH]
  8. var lengthCodes = [...]uint32{
  9. 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
  10. 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
  11. 13, 13, 13, 13, 14, 14, 14, 14, 15, 15,
  12. 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
  13. 17, 17, 17, 17, 17, 17, 17, 17, 18, 18,
  14. 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
  15. 19, 19, 19, 19, 20, 20, 20, 20, 20, 20,
  16. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  17. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
  18. 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
  19. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
  20. 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  21. 23, 23, 23, 23, 23, 23, 23, 23, 24, 24,
  22. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  23. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  24. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  25. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  26. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  27. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  28. 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
  29. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  30. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  31. 26, 26, 26, 26, 27, 27, 27, 27, 27, 27,
  32. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  33. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  34. 27, 27, 27, 27, 27, 28,
  35. }
  36. var offsetCodes = [...]uint32{
  37. 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
  38. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  39. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  40. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  41. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  42. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  43. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  44. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  45. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  46. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  47. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  48. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  49. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  50. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  51. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  52. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  53. }
  54. func lengthCode(len int) uint32 {
  55. if len > 258 {
  56. panic("match too long")
  57. }
  58. return lengthCodes[len-baseMatchLength]
  59. }
  60. // Returns the offset code corresponding to a specific offset
  61. func offsetCode(off int) uint32 {
  62. if off > 32768 {
  63. log.Println(off)
  64. panic("match distance too high")
  65. }
  66. off -= baseMatchOffset
  67. if off < len(offsetCodes) {
  68. return offsetCodes[off]
  69. }
  70. if off>>7 < len(offsetCodes) {
  71. return offsetCodes[off>>7] + 14
  72. }
  73. return offsetCodes[off>>14] + 28
  74. }