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.

32 lines
502 B

  1. package packet
  2. // encodeUint16 converts the unsigned 16-bit integer
  3. // into a slice of bytes in big-endian order.
  4. func encodeUint16(n uint16) []byte {
  5. return []byte{byte(n >> 8), byte(n)}
  6. }
  7. // encodeLength encodes the unsigned integer
  8. // by using a variable length encoding scheme.
  9. func encodeLength(n uint32) uint32 {
  10. var value, digit uint32
  11. for n > 0 {
  12. if value != 0 {
  13. value <<= 8
  14. }
  15. digit = n % 128
  16. n /= 128
  17. if n > 0 {
  18. digit |= 0x80
  19. }
  20. value |= digit
  21. }
  22. return value
  23. }