token.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. const (
  6. // 2 bits: type 0 = literal 1=EOF 2=Match 3=Unused
  7. // 8 bits: xlength = length - MIN_MATCH_LENGTH
  8. // 22 bits xoffset = offset - MIN_OFFSET_SIZE, or literal
  9. lengthShift = 22
  10. offsetMask = 1<<lengthShift - 1
  11. typeMask = 3 << 30
  12. literalType = 0 << 30
  13. matchType = 1 << 30
  14. )
  15. // The length code for length X (MIN_MATCH_LENGTH <= X <= MAX_MATCH_LENGTH)
  16. // is lengthCodes[length - MIN_MATCH_LENGTH]
  17. var lengthCodes = [...]uint32{
  18. 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
  19. 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
  20. 13, 13, 13, 13, 14, 14, 14, 14, 15, 15,
  21. 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
  22. 17, 17, 17, 17, 17, 17, 17, 17, 18, 18,
  23. 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
  24. 19, 19, 19, 19, 20, 20, 20, 20, 20, 20,
  25. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  26. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
  27. 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
  28. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
  29. 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  30. 23, 23, 23, 23, 23, 23, 23, 23, 24, 24,
  31. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  32. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  33. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  34. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  35. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  36. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  37. 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
  38. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  39. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  40. 26, 26, 26, 26, 27, 27, 27, 27, 27, 27,
  41. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  42. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  43. 27, 27, 27, 27, 27, 28,
  44. }
  45. var offsetCodes = [...]uint32{
  46. 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
  47. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  48. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  49. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  50. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  51. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  52. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  53. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  54. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  55. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  56. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  57. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  59. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  60. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  61. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  62. }
  63. type token uint32
  64. // Convert a literal into a literal token.
  65. func literalToken(literal uint32) token { return token(literalType + literal) }
  66. // Convert a < xlength, xoffset > pair into a match token.
  67. func matchToken(xlength uint32, xoffset uint32) token {
  68. return token(matchType + xlength<<lengthShift + xoffset)
  69. }
  70. // Returns the type of a token
  71. func (t token) typ() uint32 { return uint32(t) & typeMask }
  72. // Returns the literal of a literal token
  73. func (t token) literal() uint32 { return uint32(t - literalType) }
  74. // Returns the extra offset of a match token
  75. func (t token) offset() uint32 { return uint32(t) & offsetMask }
  76. func (t token) length() uint32 { return uint32((t - matchType) >> lengthShift) }
  77. func lengthCode(len uint32) uint32 { return lengthCodes[len] }
  78. // Returns the offset code corresponding to a specific offset
  79. func offsetCode(off uint32) uint32 {
  80. const n = uint32(len(offsetCodes))
  81. switch {
  82. case off < n:
  83. return offsetCodes[off]
  84. case off>>7 < n:
  85. return offsetCodes[off>>7] + 14
  86. default:
  87. return offsetCodes[off>>14] + 28
  88. }
  89. }