github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/uefivars/guid.go (about)

     1  // Copyright 2015-2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  //
     5  // SPDX-License-Identifier: BSD-3-Clause
     6  //
     7  
     8  package uefivars
     9  
    10  import (
    11  	"encoding/binary"
    12  	"fmt"
    13  )
    14  
    15  // MixedGUID is a mixed-endianness guid, as used by MS and UEFI.
    16  type MixedGUID [16]byte
    17  
    18  // UUID uses the normal ordering, compatible with github.com/google/uuid. Use
    19  // a package such as that if you need to generate UUIDs, check types, etc.
    20  type UUID [16]byte
    21  
    22  // ToStdEnc converts MixedGuid to a UUID.
    23  func (m MixedGUID) ToStdEnc() (u UUID) {
    24  	u[0], u[1], u[2], u[3] = m[3], m[2], m[1], m[0]
    25  	u[4], u[5] = m[5], m[4]
    26  	u[6], u[7] = m[7], m[6]
    27  	copy(u[8:], m[8:])
    28  	return
    29  }
    30  
    31  // String converts a MixedGUID to string.
    32  func (m MixedGUID) String() string {
    33  	le := binary.LittleEndian
    34  	data1 := le.Uint32(m[:4])
    35  	data2 := le.Uint16(m[4:6])
    36  	data3 := le.Uint16(m[6:8])
    37  	return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", data1, data2, data3, m[8:10], m[10:])
    38  }
    39  
    40  // ToMixedGuid converts UUID to MixedGuid.
    41  func (u UUID) ToMixedGUID() (m MixedGUID) {
    42  	m[0], m[1], m[2], m[3] = u[3], u[2], u[1], u[0]
    43  	m[4], m[5] = u[5], u[4]
    44  	m[6], m[7] = u[7], u[6]
    45  	copy(m[8:], u[8:])
    46  	return
    47  }
    48  
    49  // String converts a UUID to string.
    50  func (u UUID) String() string {
    51  	return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", u[:4], u[4:6], u[6:8], u[8:10], u[10:])
    52  }