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