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.

68 lines
1.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package binary
  2. import (
  3. "io"
  4. . "github.com/tendermint/tendermint/common"
  5. )
  6. func WriteByteSlice(bz []byte, w io.Writer, n *int64, err *error) {
  7. WriteVarint(len(bz), w, n, err)
  8. WriteTo(bz, w, n, err)
  9. }
  10. func ReadByteSlice(r io.Reader, n *int64, err *error) []byte {
  11. length := ReadVarint(r, n, err)
  12. if *err != nil {
  13. return nil
  14. }
  15. if length < 0 {
  16. *err = ErrBinaryReadSizeUnderflow
  17. return nil
  18. }
  19. if MaxBinaryReadSize < MaxInt64(int64(length), *n+int64(length)) {
  20. *err = ErrBinaryReadSizeOverflow
  21. return nil
  22. }
  23. buf := make([]byte, length)
  24. ReadFull(buf, r, n, err)
  25. return buf
  26. }
  27. //-----------------------------------------------------------------------------
  28. func WriteByteSlices(bzz [][]byte, w io.Writer, n *int64, err *error) {
  29. WriteVarint(len(bzz), w, n, err)
  30. for _, bz := range bzz {
  31. WriteByteSlice(bz, w, n, err)
  32. if *err != nil {
  33. return
  34. }
  35. }
  36. }
  37. func ReadByteSlices(r io.Reader, n *int64, err *error) [][]byte {
  38. length := ReadVarint(r, n, err)
  39. if *err != nil {
  40. return nil
  41. }
  42. if length < 0 {
  43. *err = ErrBinaryReadSizeUnderflow
  44. return nil
  45. }
  46. if MaxBinaryReadSize < MaxInt64(int64(length), *n+int64(length)) {
  47. *err = ErrBinaryReadSizeOverflow
  48. return nil
  49. }
  50. bzz := make([][]byte, length)
  51. for i := 0; i < length; i++ {
  52. bz := ReadByteSlice(r, n, err)
  53. if *err != nil {
  54. return nil
  55. }
  56. bzz[i] = bz
  57. }
  58. return bzz
  59. }