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.

52 lines
1.8 KiB

11 years ago
  1. package merkle
  2. const (
  3. TYPE_BYTE = byte(0x00)
  4. TYPE_INT8 = byte(0x02)
  5. TYPE_UINT8 = byte(0x03)
  6. TYPE_INT16 = byte(0x04)
  7. TYPE_UINT16 = byte(0x05)
  8. TYPE_INT32 = byte(0x06)
  9. TYPE_UINT32 = byte(0x07)
  10. TYPE_INT64 = byte(0x08)
  11. TYPE_UINT64 = byte(0x09)
  12. TYPE_STRING = byte(0x10)
  13. TYPE_BYTESLICE = byte(0x11)
  14. )
  15. func GetBinaryType(o Binary) byte {
  16. switch o.(type) {
  17. case Byte: return TYPE_BYTE
  18. case Int8: return TYPE_INT8
  19. case UInt8: return TYPE_UINT8
  20. case Int16: return TYPE_INT16
  21. case UInt16: return TYPE_UINT16
  22. case Int32: return TYPE_INT32
  23. case UInt32: return TYPE_UINT32
  24. case Int64: return TYPE_INT64
  25. case UInt64: return TYPE_UINT64
  26. case Int: panic("Int not supported")
  27. case UInt: panic("UInt not supported")
  28. case String: return TYPE_STRING
  29. case ByteSlice: return TYPE_BYTESLICE
  30. default: panic("Unsupported type")
  31. }
  32. }
  33. func LoadBinary(buf []byte, start int) (Binary, int) {
  34. typeByte := buf[start]
  35. switch typeByte {
  36. case TYPE_BYTE: return LoadByte(buf[start+1:]), start+2
  37. case TYPE_INT8: return LoadInt8(buf[start+1:]), start+2
  38. case TYPE_UINT8: return LoadUInt8(buf[start+1:]), start+2
  39. case TYPE_INT16: return LoadInt16(buf[start+1:]), start+3
  40. case TYPE_UINT16: return LoadUInt16(buf[start+1:]), start+3
  41. case TYPE_INT32: return LoadInt32(buf[start+1:]), start+5
  42. case TYPE_UINT32: return LoadUInt32(buf[start+1:]), start+5
  43. case TYPE_INT64: return LoadInt64(buf[start+1:]), start+9
  44. case TYPE_UINT64: return LoadUInt64(buf[start+1:]), start+9
  45. case TYPE_STRING: return LoadString(buf, start+1)
  46. case TYPE_BYTESLICE:return LoadByteSlice(buf, start+1)
  47. default: panic("Unsupported type")
  48. }
  49. }