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.

103 lines
2.8 KiB

  1. package uuid
  2. /****************
  3. * Date: 31/01/14
  4. * Time: 3:34 PM
  5. ***************/
  6. import "net"
  7. // Struct is used for RFC4122 Version 1 UUIDs
  8. type Struct struct {
  9. timeLow uint32
  10. timeMid uint16
  11. timeHiAndVersion uint16
  12. sequenceHiAndVariant byte
  13. sequenceLow byte
  14. node []byte
  15. size int
  16. }
  17. func (o Struct) Size() int {
  18. return o.size
  19. }
  20. func (o Struct) Version() int {
  21. return int(o.timeHiAndVersion >> 12)
  22. }
  23. func (o Struct) Variant() byte {
  24. return variant(o.sequenceHiAndVariant)
  25. }
  26. // Sets the four most significant bits (bits 12 through 15) of the
  27. // timeHiAndVersion field to the 4-bit version number.
  28. func (o *Struct) setVersion(pVersion int) {
  29. o.timeHiAndVersion &= 0x0FFF
  30. o.timeHiAndVersion |= (uint16(pVersion) << 12)
  31. }
  32. func (o *Struct) setVariant(pVariant byte) {
  33. setVariant(&o.sequenceHiAndVariant, pVariant)
  34. }
  35. func (o *Struct) Unmarshal(pData []byte) {
  36. o.timeLow = uint32(pData[3]) | uint32(pData[2])<<8 | uint32(pData[1])<<16 | uint32(pData[0])<<24
  37. o.timeMid = uint16(pData[5]) | uint16(pData[4])<<8
  38. o.timeHiAndVersion = uint16(pData[7]) | uint16(pData[6])<<8
  39. o.sequenceHiAndVariant = pData[8]
  40. o.sequenceLow = pData[9]
  41. o.node = pData[10:o.Size()]
  42. }
  43. func (o *Struct) Bytes() (data []byte) {
  44. data = []byte{
  45. byte(o.timeLow >> 24), byte(o.timeLow >> 16), byte(o.timeLow >> 8), byte(o.timeLow),
  46. byte(o.timeMid >> 8), byte(o.timeMid),
  47. byte(o.timeHiAndVersion >> 8), byte(o.timeHiAndVersion),
  48. }
  49. data = append(data, o.sequenceHiAndVariant)
  50. data = append(data, o.sequenceLow)
  51. data = append(data, o.node...)
  52. return
  53. }
  54. // Marshals the UUID bytes into a slice
  55. func (o *Struct) MarshalBinary() ([]byte, error) {
  56. return o.Bytes(), nil
  57. }
  58. // Un-marshals the data bytes into the UUID struct.
  59. // Implements the BinaryUn-marshaller interface
  60. func (o *Struct) UnmarshalBinary(pData []byte) error {
  61. return UnmarshalBinary(o, pData)
  62. }
  63. func (o Struct) String() string {
  64. return formatter(&o, format)
  65. }
  66. func (o Struct) Format(pFormat string) string {
  67. return formatter(&o, pFormat)
  68. }
  69. // Set the three most significant bits (bits 0, 1 and 2) of the
  70. // sequenceHiAndVariant to variant mask 0x80.
  71. func (o *Struct) setRFC4122Variant() {
  72. o.sequenceHiAndVariant &= variantSet
  73. o.sequenceHiAndVariant |= ReservedRFC4122
  74. }
  75. // Unmarshals data into struct for V1 UUIDs
  76. func newV1(pNow Timestamp, pVersion uint16, pVariant byte, pNode net.HardwareAddr) UUID {
  77. o := new(Struct)
  78. o.timeLow = uint32(pNow & 0xFFFFFFFF)
  79. o.timeMid = uint16((pNow >> 32) & 0xFFFF)
  80. o.timeHiAndVersion = uint16((pNow >> 48) & 0x0FFF)
  81. o.timeHiAndVersion |= uint16(pVersion << 12)
  82. o.sequenceLow = byte(state.sequence & 0xFF)
  83. o.sequenceHiAndVariant = byte((state.sequence & 0x3F00) >> 8)
  84. o.sequenceHiAndVariant |= pVariant
  85. o.node = pNode
  86. return o
  87. }