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.

60 lines
2.0 KiB

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
11 years ago
11 years ago
  1. package binary
  2. const (
  3. TYPE_NIL = byte(0x00)
  4. TYPE_BYTE = byte(0x01)
  5. TYPE_INT8 = byte(0x02)
  6. TYPE_UINT8 = byte(0x03)
  7. TYPE_INT16 = byte(0x04)
  8. TYPE_UINT16 = byte(0x05)
  9. TYPE_INT32 = byte(0x06)
  10. TYPE_UINT32 = byte(0x07)
  11. TYPE_INT64 = byte(0x08)
  12. TYPE_UINT64 = byte(0x09)
  13. TYPE_STRING = byte(0x10)
  14. TYPE_BYTESLICE = byte(0x11)
  15. )
  16. func GetBinaryType(o Binary) byte {
  17. switch o.(type) {
  18. case nil: return TYPE_NIL
  19. case Byte: return TYPE_BYTE
  20. case Int8: return TYPE_INT8
  21. case UInt8: return TYPE_UINT8
  22. case Int16: return TYPE_INT16
  23. case UInt16: return TYPE_UINT16
  24. case Int32: return TYPE_INT32
  25. case UInt32: return TYPE_UINT32
  26. case Int64: return TYPE_INT64
  27. case UInt64: return TYPE_UINT64
  28. case Int: panic("Int not supported")
  29. case UInt: panic("UInt not supported")
  30. case String: return TYPE_STRING
  31. case ByteSlice: return TYPE_BYTESLICE
  32. default: panic("Unsupported type")
  33. }
  34. }
  35. func ReadBinary(buf []byte, start int) (Binary, int) {
  36. typeByte := buf[start]
  37. switch typeByte {
  38. case TYPE_NIL: return nil, start+1
  39. case TYPE_BYTE: return ReadByte(buf[start+1:]), start+2
  40. case TYPE_INT8: return ReadInt8(buf[start+1:]), start+2
  41. case TYPE_UINT8: return ReadUInt8(buf[start+1:]), start+2
  42. case TYPE_INT16: return ReadInt16(buf[start+1:]), start+3
  43. case TYPE_UINT16: return ReadUInt16(buf[start+1:]), start+3
  44. case TYPE_INT32: return ReadInt32(buf[start+1:]), start+5
  45. case TYPE_UINT32: return ReadUInt32(buf[start+1:]), start+5
  46. case TYPE_INT64: return ReadInt64(buf[start+1:]), start+9
  47. case TYPE_UINT64: return ReadUInt64(buf[start+1:]), start+9
  48. case TYPE_STRING: return ReadString(buf, start+1)
  49. case TYPE_BYTESLICE:return ReadByteSlice(buf, start+1)
  50. default: panic("Unsupported type")
  51. }
  52. }