node.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package maxminddb
  2. type nodeReader interface {
  3. readLeft(uint) uint
  4. readRight(uint) uint
  5. }
  6. type nodeReader24 struct {
  7. buffer []byte
  8. }
  9. func (n nodeReader24) readLeft(nodeNumber uint) uint {
  10. return (uint(n.buffer[nodeNumber]) << 16) | (uint(n.buffer[nodeNumber+1]) << 8) | uint(n.buffer[nodeNumber+2])
  11. }
  12. func (n nodeReader24) readRight(nodeNumber uint) uint {
  13. return (uint(n.buffer[nodeNumber+3]) << 16) | (uint(n.buffer[nodeNumber+4]) << 8) | uint(n.buffer[nodeNumber+5])
  14. }
  15. type nodeReader28 struct {
  16. buffer []byte
  17. }
  18. func (n nodeReader28) readLeft(nodeNumber uint) uint {
  19. return ((uint(n.buffer[nodeNumber+3]) & 0xF0) << 20) | (uint(n.buffer[nodeNumber]) << 16) | (uint(n.buffer[nodeNumber+1]) << 8) | uint(n.buffer[nodeNumber+2])
  20. }
  21. func (n nodeReader28) readRight(nodeNumber uint) uint {
  22. return ((uint(n.buffer[nodeNumber+3]) & 0x0F) << 24) | (uint(n.buffer[nodeNumber+4]) << 16) | (uint(n.buffer[nodeNumber+5]) << 8) | uint(n.buffer[nodeNumber+6])
  23. }
  24. type nodeReader32 struct {
  25. buffer []byte
  26. }
  27. func (n nodeReader32) readLeft(nodeNumber uint) uint {
  28. return (uint(n.buffer[nodeNumber]) << 24) | (uint(n.buffer[nodeNumber+1]) << 16) | (uint(n.buffer[nodeNumber+2]) << 8) | uint(n.buffer[nodeNumber+3])
  29. }
  30. func (n nodeReader32) readRight(nodeNumber uint) uint {
  31. return (uint(n.buffer[nodeNumber+4]) << 24) | (uint(n.buffer[nodeNumber+5]) << 16) | (uint(n.buffer[nodeNumber+6]) << 8) | uint(n.buffer[nodeNumber+7])
  32. }