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.

171 lines
3.9 KiB

11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package binary
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. . "github.com/tendermint/tendermint/common"
  7. "io"
  8. "reflect"
  9. "time"
  10. )
  11. type Encoder func(o interface{}, w io.Writer, n *int64, err *error)
  12. type Decoder func(r io.Reader, n *int64, err *error) interface{}
  13. type Comparator func(o1 interface{}, o2 interface{}) int
  14. type Codec struct {
  15. Encode Encoder
  16. Decode Decoder
  17. Compare Comparator
  18. }
  19. const (
  20. typeByte = byte(0x01)
  21. typeInt8 = byte(0x02)
  22. // typeUint8 = byte(0x03)
  23. typeInt16 = byte(0x04)
  24. typeUint16 = byte(0x05)
  25. typeInt32 = byte(0x06)
  26. typeUint32 = byte(0x07)
  27. typeInt64 = byte(0x08)
  28. typeUint64 = byte(0x09)
  29. typeVarint = byte(0x0A)
  30. typeUvarint = byte(0x0B)
  31. typeString = byte(0x10)
  32. typeByteSlice = byte(0x11)
  33. typeTime = byte(0x20)
  34. )
  35. func BasicCodecEncoder(o interface{}, w io.Writer, n *int64, err *error) {
  36. switch o := o.(type) {
  37. case nil:
  38. PanicSanity("nil type unsupported")
  39. case byte:
  40. WriteByte(typeByte, w, n, err)
  41. WriteByte(o, w, n, err)
  42. case int8:
  43. WriteByte(typeInt8, w, n, err)
  44. WriteInt8(o, w, n, err)
  45. //case uint8:
  46. // WriteByte( typeUint8, w, n, err)
  47. // WriteUint8( o, w, n, err)
  48. case int16:
  49. WriteByte(typeInt16, w, n, err)
  50. WriteInt16(o, w, n, err)
  51. case uint16:
  52. WriteByte(typeUint16, w, n, err)
  53. WriteUint16(o, w, n, err)
  54. case int32:
  55. WriteByte(typeInt32, w, n, err)
  56. WriteInt32(o, w, n, err)
  57. case uint32:
  58. WriteByte(typeUint32, w, n, err)
  59. WriteUint32(o, w, n, err)
  60. case int64:
  61. WriteByte(typeInt64, w, n, err)
  62. WriteInt64(o, w, n, err)
  63. case uint64:
  64. WriteByte(typeUint64, w, n, err)
  65. WriteUint64(o, w, n, err)
  66. case int:
  67. WriteByte(typeVarint, w, n, err)
  68. WriteVarint(o, w, n, err)
  69. case uint:
  70. WriteByte(typeUvarint, w, n, err)
  71. WriteUvarint(o, w, n, err)
  72. case string:
  73. WriteByte(typeString, w, n, err)
  74. WriteString(o, w, n, err)
  75. case []byte:
  76. WriteByte(typeByteSlice, w, n, err)
  77. WriteByteSlice(o, w, n, err)
  78. case time.Time:
  79. WriteByte(typeTime, w, n, err)
  80. WriteTime(o, w, n, err)
  81. default:
  82. PanicSanity(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o)))
  83. }
  84. }
  85. func BasicCodecDecoder(r io.Reader, n *int64, err *error) (o interface{}) {
  86. type_ := ReadByte(r, n, err)
  87. if *err != nil {
  88. return
  89. }
  90. switch type_ {
  91. case typeByte:
  92. o = ReadByte(r, n, err)
  93. case typeInt8:
  94. o = ReadInt8(r, n, err)
  95. //case typeUint8:
  96. // o = ReadUint8(r, n, err)
  97. case typeInt16:
  98. o = ReadInt16(r, n, err)
  99. case typeUint16:
  100. o = ReadUint16(r, n, err)
  101. case typeInt32:
  102. o = ReadInt32(r, n, err)
  103. case typeUint32:
  104. o = ReadUint32(r, n, err)
  105. case typeInt64:
  106. o = ReadInt64(r, n, err)
  107. case typeUint64:
  108. o = ReadUint64(r, n, err)
  109. case typeVarint:
  110. o = ReadVarint(r, n, err)
  111. case typeUvarint:
  112. o = ReadUvarint(r, n, err)
  113. case typeString:
  114. o = ReadString(r, n, err)
  115. case typeByteSlice:
  116. o = ReadByteSlice(r, n, err)
  117. case typeTime:
  118. o = ReadTime(r, n, err)
  119. default:
  120. *err = errors.New(Fmt("Unsupported type byte: %X", type_))
  121. }
  122. return
  123. }
  124. // Contract: Caller must ensure that types match.
  125. func BasicCodecComparator(o1 interface{}, o2 interface{}) int {
  126. switch o1.(type) {
  127. case byte:
  128. return int(o1.(byte) - o2.(byte))
  129. case int8:
  130. return int(o1.(int8) - o2.(int8))
  131. //case uint8:
  132. case int16:
  133. return int(o1.(int16) - o2.(int16))
  134. case uint16:
  135. return int(o1.(uint16) - o2.(uint16))
  136. case int32:
  137. return int(o1.(int32) - o2.(int32))
  138. case uint32:
  139. return int(o1.(uint32) - o2.(uint32))
  140. case int64:
  141. return int(o1.(int64) - o2.(int64))
  142. case uint64:
  143. return int(o1.(uint64) - o2.(uint64))
  144. case int:
  145. return o1.(int) - o2.(int)
  146. case uint:
  147. return int(o1.(uint)) - int(o2.(uint))
  148. case string:
  149. return bytes.Compare([]byte(o1.(string)), []byte(o2.(string)))
  150. case []byte:
  151. return bytes.Compare(o1.([]byte), o2.([]byte))
  152. case time.Time:
  153. return int(o1.(time.Time).UnixNano() - o2.(time.Time).UnixNano())
  154. default:
  155. PanicSanity(Fmt("Unsupported type: %v", reflect.TypeOf(o1)))
  156. }
  157. return 0
  158. }
  159. var BasicCodec = Codec{
  160. Encode: BasicCodecEncoder,
  161. Decode: BasicCodecDecoder,
  162. Compare: BasicCodecComparator,
  163. }