layout.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package internal
  2. // Layout represents the layout of UUID. See page 5 in RFC 4122.
  3. type Layout byte
  4. const (
  5. // LayoutInvalid represents invalid layout.
  6. LayoutInvalid Layout = iota
  7. // LayoutNCS represents the NCS layout: Reserved, NCS backward compatibility (Values: 0x00-0x07).
  8. LayoutNCS
  9. // LayoutRFC4122 represents the RFC4122 layout: The variant specified in RFC 4122 (Values: 0x08-0x0b).
  10. LayoutRFC4122
  11. // LayoutMicrosoft represents the Microsoft layout: Reserved, Microsoft Corporation backward compatibility (Values: 0x0c-0x0d).
  12. LayoutMicrosoft
  13. // LayoutFuture represents the Future layout: Reserved for future definition. (Values: 0x0e-0x0f).
  14. LayoutFuture
  15. )
  16. // SetLayout sets the layout for uuid.
  17. // This is intended to be called from the New function in packages that implement uuid generating functions.
  18. func SetLayout(uuid []byte, layout Layout) {
  19. switch layout {
  20. case LayoutNCS:
  21. uuid[8] = (uuid[8] | 0x00) & 0x0f // Msb0=0
  22. case LayoutRFC4122:
  23. uuid[8] = (uuid[8] | 0x80) & 0x8f // Msb0=1, Msb1=0
  24. case LayoutMicrosoft:
  25. uuid[8] = (uuid[8] | 0xc0) & 0xcf // Msb0=1, Msb1=1, Msb2=0
  26. case LayoutFuture:
  27. uuid[8] = (uuid[8] | 0xe0) & 0xef // Msb0=1, Msb1=1, Msb2=1
  28. default:
  29. panic("layout is invalid")
  30. }
  31. }
  32. // GetLayout returns layout of uuid.
  33. func GetLayout(uuid []byte) Layout {
  34. switch {
  35. case (uuid[8] & 0x80) == 0x00:
  36. return LayoutNCS
  37. case (uuid[8] & 0xc0) == 0x80:
  38. return LayoutRFC4122
  39. case (uuid[8] & 0xe0) == 0xc0:
  40. return LayoutMicrosoft
  41. case (uuid[8] & 0xe0) == 0xe0:
  42. return LayoutFuture
  43. }
  44. return LayoutInvalid
  45. }