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.

174 lines
4.0 KiB

11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 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. // SANITY CHECK
  39. panic("nil type unsupported")
  40. case byte:
  41. WriteByte(typeByte, w, n, err)
  42. WriteByte(o, w, n, err)
  43. case int8:
  44. WriteByte(typeInt8, w, n, err)
  45. WriteInt8(o, w, n, err)
  46. //case uint8:
  47. // WriteByte( typeUint8, w, n, err)
  48. // WriteUint8( o, w, n, err)
  49. case int16:
  50. WriteByte(typeInt16, w, n, err)
  51. WriteInt16(o, w, n, err)
  52. case uint16:
  53. WriteByte(typeUint16, w, n, err)
  54. WriteUint16(o, w, n, err)
  55. case int32:
  56. WriteByte(typeInt32, w, n, err)
  57. WriteInt32(o, w, n, err)
  58. case uint32:
  59. WriteByte(typeUint32, w, n, err)
  60. WriteUint32(o, w, n, err)
  61. case int64:
  62. WriteByte(typeInt64, w, n, err)
  63. WriteInt64(o, w, n, err)
  64. case uint64:
  65. WriteByte(typeUint64, w, n, err)
  66. WriteUint64(o, w, n, err)
  67. case int:
  68. WriteByte(typeVarint, w, n, err)
  69. WriteVarint(o, w, n, err)
  70. case uint:
  71. WriteByte(typeUvarint, w, n, err)
  72. WriteUvarint(o, w, n, err)
  73. case string:
  74. WriteByte(typeString, w, n, err)
  75. WriteString(o, w, n, err)
  76. case []byte:
  77. WriteByte(typeByteSlice, w, n, err)
  78. WriteByteSlice(o, w, n, err)
  79. case time.Time:
  80. WriteByte(typeTime, w, n, err)
  81. WriteTime(o, w, n, err)
  82. default:
  83. // SANITY CHECK
  84. panic(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o)))
  85. }
  86. }
  87. func BasicCodecDecoder(r io.Reader, n *int64, err *error) (o interface{}) {
  88. type_ := ReadByte(r, n, err)
  89. if *err != nil {
  90. return
  91. }
  92. switch type_ {
  93. case typeByte:
  94. o = ReadByte(r, n, err)
  95. case typeInt8:
  96. o = ReadInt8(r, n, err)
  97. //case typeUint8:
  98. // o = ReadUint8(r, n, err)
  99. case typeInt16:
  100. o = ReadInt16(r, n, err)
  101. case typeUint16:
  102. o = ReadUint16(r, n, err)
  103. case typeInt32:
  104. o = ReadInt32(r, n, err)
  105. case typeUint32:
  106. o = ReadUint32(r, n, err)
  107. case typeInt64:
  108. o = ReadInt64(r, n, err)
  109. case typeUint64:
  110. o = ReadUint64(r, n, err)
  111. case typeVarint:
  112. o = ReadVarint(r, n, err)
  113. case typeUvarint:
  114. o = ReadUvarint(r, n, err)
  115. case typeString:
  116. o = ReadString(r, n, err)
  117. case typeByteSlice:
  118. o = ReadByteSlice(r, n, err)
  119. case typeTime:
  120. o = ReadTime(r, n, err)
  121. default:
  122. *err = errors.New(Fmt("Unsupported type byte: %X", type_))
  123. }
  124. return
  125. }
  126. // Contract: Caller must ensure that types match.
  127. func BasicCodecComparator(o1 interface{}, o2 interface{}) int {
  128. switch o1.(type) {
  129. case byte:
  130. return int(o1.(byte) - o2.(byte))
  131. case int8:
  132. return int(o1.(int8) - o2.(int8))
  133. //case uint8:
  134. case int16:
  135. return int(o1.(int16) - o2.(int16))
  136. case uint16:
  137. return int(o1.(uint16) - o2.(uint16))
  138. case int32:
  139. return int(o1.(int32) - o2.(int32))
  140. case uint32:
  141. return int(o1.(uint32) - o2.(uint32))
  142. case int64:
  143. return int(o1.(int64) - o2.(int64))
  144. case uint64:
  145. return int(o1.(uint64) - o2.(uint64))
  146. case int:
  147. return o1.(int) - o2.(int)
  148. case uint:
  149. return int(o1.(uint)) - int(o2.(uint))
  150. case string:
  151. return bytes.Compare([]byte(o1.(string)), []byte(o2.(string)))
  152. case []byte:
  153. return bytes.Compare(o1.([]byte), o2.([]byte))
  154. case time.Time:
  155. return int(o1.(time.Time).UnixNano() - o2.(time.Time).UnixNano())
  156. default:
  157. // SANITY CHECK
  158. panic(Fmt("Unsupported type: %v", reflect.TypeOf(o1)))
  159. }
  160. return 0
  161. }
  162. var BasicCodec = Codec{
  163. Encode: BasicCodecEncoder,
  164. Decode: BasicCodecDecoder,
  165. Compare: BasicCodecComparator,
  166. }