图片解析应用
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.

135 lines
3.5 KiB

  1. import binascii
  2. from kafka.record._crc32c import crc as crc32c_py
  3. try:
  4. from crc32c import crc32c as crc32c_c
  5. except ImportError:
  6. crc32c_c = None
  7. def encode_varint(value, write):
  8. """ Encode an integer to a varint presentation. See
  9. https://developers.google.com/protocol-buffers/docs/encoding?csw=1#varints
  10. on how those can be produced.
  11. Arguments:
  12. value (int): Value to encode
  13. write (function): Called per byte that needs to be writen
  14. Returns:
  15. int: Number of bytes written
  16. """
  17. value = (value << 1) ^ (value >> 63)
  18. if value <= 0x7f: # 1 byte
  19. write(value)
  20. return 1
  21. if value <= 0x3fff: # 2 bytes
  22. write(0x80 | (value & 0x7f))
  23. write(value >> 7)
  24. return 2
  25. if value <= 0x1fffff: # 3 bytes
  26. write(0x80 | (value & 0x7f))
  27. write(0x80 | ((value >> 7) & 0x7f))
  28. write(value >> 14)
  29. return 3
  30. if value <= 0xfffffff: # 4 bytes
  31. write(0x80 | (value & 0x7f))
  32. write(0x80 | ((value >> 7) & 0x7f))
  33. write(0x80 | ((value >> 14) & 0x7f))
  34. write(value >> 21)
  35. return 4
  36. if value <= 0x7ffffffff: # 5 bytes
  37. write(0x80 | (value & 0x7f))
  38. write(0x80 | ((value >> 7) & 0x7f))
  39. write(0x80 | ((value >> 14) & 0x7f))
  40. write(0x80 | ((value >> 21) & 0x7f))
  41. write(value >> 28)
  42. return 5
  43. else:
  44. # Return to general algorithm
  45. bits = value & 0x7f
  46. value >>= 7
  47. i = 0
  48. while value:
  49. write(0x80 | bits)
  50. bits = value & 0x7f
  51. value >>= 7
  52. i += 1
  53. write(bits)
  54. return i
  55. def size_of_varint(value):
  56. """ Number of bytes needed to encode an integer in variable-length format.
  57. """
  58. value = (value << 1) ^ (value >> 63)
  59. if value <= 0x7f:
  60. return 1
  61. if value <= 0x3fff:
  62. return 2
  63. if value <= 0x1fffff:
  64. return 3
  65. if value <= 0xfffffff:
  66. return 4
  67. if value <= 0x7ffffffff:
  68. return 5
  69. if value <= 0x3ffffffffff:
  70. return 6
  71. if value <= 0x1ffffffffffff:
  72. return 7
  73. if value <= 0xffffffffffffff:
  74. return 8
  75. if value <= 0x7fffffffffffffff:
  76. return 9
  77. return 10
  78. def decode_varint(buffer, pos=0):
  79. """ Decode an integer from a varint presentation. See
  80. https://developers.google.com/protocol-buffers/docs/encoding?csw=1#varints
  81. on how those can be produced.
  82. Arguments:
  83. buffer (bytearray): buffer to read from.
  84. pos (int): optional position to read from
  85. Returns:
  86. (int, int): Decoded int value and next read position
  87. """
  88. result = buffer[pos]
  89. if not (result & 0x81):
  90. return (result >> 1), pos + 1
  91. if not (result & 0x80):
  92. return (result >> 1) ^ (~0), pos + 1
  93. result &= 0x7f
  94. pos += 1
  95. shift = 7
  96. while 1:
  97. b = buffer[pos]
  98. result |= ((b & 0x7f) << shift)
  99. pos += 1
  100. if not (b & 0x80):
  101. return ((result >> 1) ^ -(result & 1), pos)
  102. shift += 7
  103. if shift >= 64:
  104. raise ValueError("Out of int64 range")
  105. _crc32c = crc32c_py
  106. if crc32c_c is not None:
  107. _crc32c = crc32c_c
  108. def calc_crc32c(memview, _crc32c=_crc32c):
  109. """ Calculate CRC-32C (Castagnoli) checksum over a memoryview of data
  110. """
  111. return _crc32c(memview)
  112. def calc_crc32(memview):
  113. """ Calculate simple CRC-32 checksum over a memoryview of data
  114. """
  115. crc = binascii.crc32(memview) & 0xffffffff
  116. return crc