1
0

header.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package protocol
  16. import "github.com/calmh/xdr"
  17. type header struct {
  18. version int
  19. msgID int
  20. msgType int
  21. compression bool
  22. }
  23. func (h header) encodeXDR(xw *xdr.Writer) (int, error) {
  24. u := encodeHeader(h)
  25. return xw.WriteUint32(u)
  26. }
  27. func (h *header) decodeXDR(xr *xdr.Reader) error {
  28. u := xr.ReadUint32()
  29. *h = decodeHeader(u)
  30. return xr.Error()
  31. }
  32. func encodeHeader(h header) uint32 {
  33. var isComp uint32
  34. if h.compression {
  35. isComp = 1 << 0 // the zeroth bit is the compression bit
  36. }
  37. return uint32(h.version&0xf)<<28 +
  38. uint32(h.msgID&0xfff)<<16 +
  39. uint32(h.msgType&0xff)<<8 +
  40. isComp
  41. }
  42. func decodeHeader(u uint32) header {
  43. return header{
  44. version: int(u>>28) & 0xf,
  45. msgID: int(u>>16) & 0xfff,
  46. msgType: int(u>>8) & 0xff,
  47. compression: u&1 == 1,
  48. }
  49. }