util.go 918 B

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