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.

91 lines
1.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. )
  5. const (
  6. TYPE_NIL = Byte(0x00)
  7. TYPE_BYTE = Byte(0x01)
  8. TYPE_INT8 = Byte(0x02)
  9. TYPE_UINT8 = Byte(0x03)
  10. TYPE_INT16 = Byte(0x04)
  11. TYPE_UINT16 = Byte(0x05)
  12. TYPE_INT32 = Byte(0x06)
  13. TYPE_UINT32 = Byte(0x07)
  14. TYPE_INT64 = Byte(0x08)
  15. TYPE_UINT64 = Byte(0x09)
  16. TYPE_STRING = Byte(0x10)
  17. TYPE_BYTESLICE = Byte(0x11)
  18. TYPE_TIME = Byte(0x20)
  19. )
  20. func GetBinaryType(o Binary) Byte {
  21. switch o.(type) {
  22. case nil:
  23. return TYPE_NIL
  24. case Byte:
  25. return TYPE_BYTE
  26. case Int8:
  27. return TYPE_INT8
  28. case UInt8:
  29. return TYPE_UINT8
  30. case Int16:
  31. return TYPE_INT16
  32. case UInt16:
  33. return TYPE_UINT16
  34. case Int32:
  35. return TYPE_INT32
  36. case UInt32:
  37. return TYPE_UINT32
  38. case Int64:
  39. return TYPE_INT64
  40. case UInt64:
  41. return TYPE_UINT64
  42. case String:
  43. return TYPE_STRING
  44. case ByteSlice:
  45. return TYPE_BYTESLICE
  46. case Time:
  47. return TYPE_TIME
  48. default:
  49. panic("Unsupported type")
  50. }
  51. }
  52. func ReadBinaryN(r io.Reader) (o Binary, n int64) {
  53. type_, n_ := ReadByteN(r)
  54. n += n_
  55. switch type_ {
  56. case TYPE_NIL:
  57. o, n_ = nil, 0
  58. case TYPE_BYTE:
  59. o, n_ = ReadByteN(r)
  60. case TYPE_INT8:
  61. o, n_ = ReadInt8N(r)
  62. case TYPE_UINT8:
  63. o, n_ = ReadUInt8N(r)
  64. case TYPE_INT16:
  65. o, n_ = ReadInt16N(r)
  66. case TYPE_UINT16:
  67. o, n_ = ReadUInt16N(r)
  68. case TYPE_INT32:
  69. o, n_ = ReadInt32N(r)
  70. case TYPE_UINT32:
  71. o, n_ = ReadUInt32N(r)
  72. case TYPE_INT64:
  73. o, n_ = ReadInt64N(r)
  74. case TYPE_UINT64:
  75. o, n_ = ReadUInt64N(r)
  76. case TYPE_STRING:
  77. o, n_ = ReadStringN(r)
  78. case TYPE_BYTESLICE:
  79. o, n_ = ReadByteSliceN(r)
  80. case TYPE_TIME:
  81. o, n_ = ReadTimeN(r)
  82. default:
  83. panic("Unsupported type")
  84. }
  85. n += n_
  86. return o, n
  87. }