util.go 816 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Package util contains small helpers used across the repo
  2. package util
  3. import (
  4. "encoding/binary"
  5. )
  6. // BigEndianUint24 returns the value of a big endian uint24
  7. func BigEndianUint24(raw []byte) uint32 {
  8. if len(raw) < 3 {
  9. return 0
  10. }
  11. rawCopy := make([]byte, 4)
  12. copy(rawCopy[1:], raw)
  13. return binary.BigEndian.Uint32(rawCopy)
  14. }
  15. // PutBigEndianUint24 encodes a uint24 and places into out
  16. func PutBigEndianUint24(out []byte, in uint32) {
  17. tmp := make([]byte, 4)
  18. binary.BigEndian.PutUint32(tmp, in)
  19. copy(out, tmp[1:])
  20. }
  21. // PutBigEndianUint48 encodes a uint64 and places into out
  22. func PutBigEndianUint48(out []byte, in uint64) {
  23. tmp := make([]byte, 8)
  24. binary.BigEndian.PutUint64(tmp, in)
  25. copy(out, tmp[2:])
  26. }
  27. // Max returns the larger value
  28. func Max(a, b int) int {
  29. if a > b {
  30. return a
  31. }
  32. return b
  33. }