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.

36 lines
741 B

  1. package ringct
  2. import "io"
  3. import "fmt"
  4. // these functions basically what golang varint does, ( however theere is a minor spec diff, so these are here for exact compatibility)
  5. func ReadVarInt(buf io.Reader) (result uint64, err error) {
  6. b := make([]byte, 1)
  7. var r uint64
  8. var n int
  9. for i := 0; ; i++ {
  10. n, err = buf.Read(b)
  11. if err != nil {
  12. return
  13. }
  14. if n != 1 {
  15. err = fmt.Errorf("Buffer ended prematurely for varint")
  16. return
  17. }
  18. r += (uint64(b[0]) & 0x7f) << uint(i*7)
  19. if uint64(b[0])&0x80 == 0 {
  20. break
  21. }
  22. }
  23. result = r
  24. return
  25. }
  26. func Uint64ToBytes(num uint64) (result []byte) {
  27. for ; num >= 0x80; num >>= 7 {
  28. result = append(result, byte((num&0x7f)|0x80))
  29. }
  30. result = append(result, byte(num))
  31. return
  32. }