Update go-winio in vendor.conf
Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
This commit is contained in:
21
vendor/github.com/Microsoft/go-winio/pkg/etw/eventdescriptor.go
generated
vendored
21
vendor/github.com/Microsoft/go-winio/pkg/etw/eventdescriptor.go
generated
vendored
@@ -17,7 +17,7 @@ const (
|
||||
// will always be collected.
|
||||
type Level uint8
|
||||
|
||||
// Predefined ETW log levels.
|
||||
// Predefined ETW log levels from winmeta.xml in the Windows SDK.
|
||||
const (
|
||||
LevelAlways Level = iota
|
||||
LevelCritical
|
||||
@@ -27,13 +27,30 @@ const (
|
||||
LevelVerbose
|
||||
)
|
||||
|
||||
// Opcode represents the operation that the event indicates is being performed.
|
||||
type Opcode uint8
|
||||
|
||||
// Predefined ETW opcodes from winmeta.xml in the Windows SDK.
|
||||
const (
|
||||
// OpcodeInfo indicates an informational event.
|
||||
OpcodeInfo Opcode = iota
|
||||
// OpcodeStart indicates the start of an operation.
|
||||
OpcodeStart
|
||||
// OpcodeStop indicates the end of an operation.
|
||||
OpcodeStop
|
||||
// OpcodeDCStart indicates the start of a provider capture state operation.
|
||||
OpcodeDCStart
|
||||
// OpcodeDCStop indicates the end of a provider capture state operation.
|
||||
OpcodeDCStop
|
||||
)
|
||||
|
||||
// EventDescriptor represents various metadata for an ETW event.
|
||||
type eventDescriptor struct {
|
||||
id uint16
|
||||
version uint8
|
||||
channel Channel
|
||||
level Level
|
||||
opcode uint8
|
||||
opcode Opcode
|
||||
task uint16
|
||||
keyword uint64
|
||||
}
|
||||
|
||||
20
vendor/github.com/Microsoft/go-winio/pkg/etw/eventopt.go
generated
vendored
20
vendor/github.com/Microsoft/go-winio/pkg/etw/eventopt.go
generated
vendored
@@ -1,13 +1,13 @@
|
||||
package etw
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
"github.com/Microsoft/go-winio/pkg/guid"
|
||||
)
|
||||
|
||||
type eventOptions struct {
|
||||
descriptor *eventDescriptor
|
||||
activityID *windows.GUID
|
||||
relatedActivityID *windows.GUID
|
||||
activityID *guid.GUID
|
||||
relatedActivityID *guid.GUID
|
||||
tags uint32
|
||||
}
|
||||
|
||||
@@ -36,12 +36,20 @@ func WithKeyword(keyword uint64) EventOpt {
|
||||
}
|
||||
}
|
||||
|
||||
// WithChannel specifies the channel of the event to be written.
|
||||
func WithChannel(channel Channel) EventOpt {
|
||||
return func(options *eventOptions) {
|
||||
options.descriptor.channel = channel
|
||||
}
|
||||
}
|
||||
|
||||
// WithOpcode specifies the opcode of the event to be written.
|
||||
func WithOpcode(opcode Opcode) EventOpt {
|
||||
return func(options *eventOptions) {
|
||||
options.descriptor.opcode = opcode
|
||||
}
|
||||
}
|
||||
|
||||
// WithTags specifies the tags of the event to be written. Tags is a 28-bit
|
||||
// value (top 4 bits are ignored) which are interpreted by the event consumer.
|
||||
func WithTags(newTags uint32) EventOpt {
|
||||
@@ -50,13 +58,15 @@ func WithTags(newTags uint32) EventOpt {
|
||||
}
|
||||
}
|
||||
|
||||
func WithActivityID(activityID *windows.GUID) EventOpt {
|
||||
// WithActivityID specifies the activity ID of the event to be written.
|
||||
func WithActivityID(activityID *guid.GUID) EventOpt {
|
||||
return func(options *eventOptions) {
|
||||
options.activityID = activityID
|
||||
}
|
||||
}
|
||||
|
||||
func WithRelatedActivityID(activityID *windows.GUID) EventOpt {
|
||||
// WithRelatedActivityID specifies the parent activity ID of the event to be written.
|
||||
func WithRelatedActivityID(activityID *guid.GUID) EventOpt {
|
||||
return func(options *eventOptions) {
|
||||
options.relatedActivityID = activityID
|
||||
}
|
||||
|
||||
123
vendor/github.com/Microsoft/go-winio/pkg/etw/fieldopt.go
generated
vendored
123
vendor/github.com/Microsoft/go-winio/pkg/etw/fieldopt.go
generated
vendored
@@ -1,7 +1,9 @@
|
||||
package etw
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -377,3 +379,124 @@ func Struct(name string, opts ...FieldOpt) FieldOpt {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Currently, we support logging basic builtin types (int, string, etc), slices
|
||||
// of basic builtin types, error, types derived from the basic types (e.g. "type
|
||||
// foo int"), and structs (recursively logging their fields). We do not support
|
||||
// slices of derived types (e.g. "[]foo").
|
||||
//
|
||||
// For types that we don't support, the value is formatted via fmt.Sprint, and
|
||||
// we also log a message that the type is unsupported along with the formatted
|
||||
// type. The intent of this is to make it easier to see which types are not
|
||||
// supported in traces, so we can evaluate adding support for more types in the
|
||||
// future.
|
||||
func SmartField(name string, v interface{}) FieldOpt {
|
||||
switch v := v.(type) {
|
||||
case bool:
|
||||
return BoolField(name, v)
|
||||
case []bool:
|
||||
return BoolArray(name, v)
|
||||
case string:
|
||||
return StringField(name, v)
|
||||
case []string:
|
||||
return StringArray(name, v)
|
||||
case int:
|
||||
return IntField(name, v)
|
||||
case []int:
|
||||
return IntArray(name, v)
|
||||
case int8:
|
||||
return Int8Field(name, v)
|
||||
case []int8:
|
||||
return Int8Array(name, v)
|
||||
case int16:
|
||||
return Int16Field(name, v)
|
||||
case []int16:
|
||||
return Int16Array(name, v)
|
||||
case int32:
|
||||
return Int32Field(name, v)
|
||||
case []int32:
|
||||
return Int32Array(name, v)
|
||||
case int64:
|
||||
return Int64Field(name, v)
|
||||
case []int64:
|
||||
return Int64Array(name, v)
|
||||
case uint:
|
||||
return UintField(name, v)
|
||||
case []uint:
|
||||
return UintArray(name, v)
|
||||
case uint8:
|
||||
return Uint8Field(name, v)
|
||||
case []uint8:
|
||||
return Uint8Array(name, v)
|
||||
case uint16:
|
||||
return Uint16Field(name, v)
|
||||
case []uint16:
|
||||
return Uint16Array(name, v)
|
||||
case uint32:
|
||||
return Uint32Field(name, v)
|
||||
case []uint32:
|
||||
return Uint32Array(name, v)
|
||||
case uint64:
|
||||
return Uint64Field(name, v)
|
||||
case []uint64:
|
||||
return Uint64Array(name, v)
|
||||
case uintptr:
|
||||
return UintptrField(name, v)
|
||||
case []uintptr:
|
||||
return UintptrArray(name, v)
|
||||
case float32:
|
||||
return Float32Field(name, v)
|
||||
case []float32:
|
||||
return Float32Array(name, v)
|
||||
case float64:
|
||||
return Float64Field(name, v)
|
||||
case []float64:
|
||||
return Float64Array(name, v)
|
||||
case error:
|
||||
return StringField(name, v.Error())
|
||||
default:
|
||||
switch rv := reflect.ValueOf(v); rv.Kind() {
|
||||
case reflect.Bool:
|
||||
return SmartField(name, rv.Bool())
|
||||
case reflect.Int:
|
||||
return SmartField(name, int(rv.Int()))
|
||||
case reflect.Int8:
|
||||
return SmartField(name, int8(rv.Int()))
|
||||
case reflect.Int16:
|
||||
return SmartField(name, int16(rv.Int()))
|
||||
case reflect.Int32:
|
||||
return SmartField(name, int32(rv.Int()))
|
||||
case reflect.Int64:
|
||||
return SmartField(name, int64(rv.Int()))
|
||||
case reflect.Uint:
|
||||
return SmartField(name, uint(rv.Uint()))
|
||||
case reflect.Uint8:
|
||||
return SmartField(name, uint8(rv.Uint()))
|
||||
case reflect.Uint16:
|
||||
return SmartField(name, uint16(rv.Uint()))
|
||||
case reflect.Uint32:
|
||||
return SmartField(name, uint32(rv.Uint()))
|
||||
case reflect.Uint64:
|
||||
return SmartField(name, uint64(rv.Uint()))
|
||||
case reflect.Uintptr:
|
||||
return SmartField(name, uintptr(rv.Uint()))
|
||||
case reflect.Float32:
|
||||
return SmartField(name, float32(rv.Float()))
|
||||
case reflect.Float64:
|
||||
return SmartField(name, float64(rv.Float()))
|
||||
case reflect.String:
|
||||
return SmartField(name, rv.String())
|
||||
case reflect.Struct:
|
||||
fields := make([]FieldOpt, 0, rv.NumField())
|
||||
for i := 0; i < rv.NumField(); i++ {
|
||||
field := rv.Field(i)
|
||||
if field.CanInterface() {
|
||||
fields = append(fields, SmartField(name, field.Interface()))
|
||||
}
|
||||
}
|
||||
return Struct(name, fields...)
|
||||
}
|
||||
}
|
||||
|
||||
return StringField(name, fmt.Sprintf("(Unsupported: %T) %v", v, v))
|
||||
}
|
||||
|
||||
41
vendor/github.com/Microsoft/go-winio/pkg/etw/provider.go
generated
vendored
41
vendor/github.com/Microsoft/go-winio/pkg/etw/provider.go
generated
vendored
@@ -4,12 +4,11 @@ import (
|
||||
"bytes"
|
||||
"crypto/sha1"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf16"
|
||||
"unsafe"
|
||||
|
||||
"github.com/Microsoft/go-winio/pkg/guid"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
@@ -17,7 +16,7 @@ import (
|
||||
// name and ID (GUID), which should always have a 1:1 mapping to each other
|
||||
// (e.g. don't use multiple provider names with the same ID, or vice versa).
|
||||
type Provider struct {
|
||||
ID *windows.GUID
|
||||
ID *guid.GUID
|
||||
handle providerHandle
|
||||
metadata []byte
|
||||
callback EnableCallback
|
||||
@@ -30,19 +29,7 @@ type Provider struct {
|
||||
|
||||
// String returns the `provider`.ID as a string
|
||||
func (provider *Provider) String() string {
|
||||
data1 := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(data1, provider.ID.Data1)
|
||||
data2 := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(data2, provider.ID.Data2)
|
||||
data3 := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(data3, provider.ID.Data3)
|
||||
return fmt.Sprintf(
|
||||
"%s-%s-%s-%s-%s",
|
||||
hex.EncodeToString(data1),
|
||||
hex.EncodeToString(data2),
|
||||
hex.EncodeToString(data3),
|
||||
hex.EncodeToString(provider.ID.Data4[:2]),
|
||||
hex.EncodeToString(provider.ID.Data4[2:]))
|
||||
return provider.ID.String()
|
||||
}
|
||||
|
||||
type providerHandle windows.Handle
|
||||
@@ -72,9 +59,9 @@ const (
|
||||
|
||||
// EnableCallback is the form of the callback function that receives provider
|
||||
// enable/disable notifications from ETW.
|
||||
type EnableCallback func(*windows.GUID, ProviderState, Level, uint64, uint64, uintptr)
|
||||
type EnableCallback func(*guid.GUID, ProviderState, Level, uint64, uint64, uintptr)
|
||||
|
||||
func providerCallback(sourceID *windows.GUID, state ProviderState, level Level, matchAnyKeyword uint64, matchAllKeyword uint64, filterData uintptr, i uintptr) {
|
||||
func providerCallback(sourceID *guid.GUID, state ProviderState, level Level, matchAnyKeyword uint64, matchAllKeyword uint64, filterData uintptr, i uintptr) {
|
||||
provider := providers.getProvider(uint(i))
|
||||
|
||||
switch state {
|
||||
@@ -96,7 +83,7 @@ func providerCallback(sourceID *windows.GUID, state ProviderState, level Level,
|
||||
// for provider notifications. Because Go has trouble with callback arguments of
|
||||
// different size, it has only pointer-sized arguments, which are then cast to
|
||||
// the appropriate types when calling providerCallback.
|
||||
func providerCallbackAdapter(sourceID *windows.GUID, state uintptr, level uintptr, matchAnyKeyword uintptr, matchAllKeyword uintptr, filterData uintptr, i uintptr) uintptr {
|
||||
func providerCallbackAdapter(sourceID *guid.GUID, state uintptr, level uintptr, matchAnyKeyword uintptr, matchAllKeyword uintptr, filterData uintptr, i uintptr) uintptr {
|
||||
providerCallback(sourceID, ProviderState(state), Level(level), uint64(matchAnyKeyword), uint64(matchAllKeyword), filterData, i)
|
||||
return 0
|
||||
}
|
||||
@@ -108,7 +95,7 @@ func providerCallbackAdapter(sourceID *windows.GUID, state uintptr, level uintpt
|
||||
// The algorithm is roughly:
|
||||
// Hash = Sha1(namespace + arg.ToUpper().ToUtf16be())
|
||||
// Guid = Hash[0..15], with Hash[7] tweaked according to RFC 4122
|
||||
func providerIDFromName(name string) *windows.GUID {
|
||||
func providerIDFromName(name string) *guid.GUID {
|
||||
buffer := sha1.New()
|
||||
|
||||
namespace := []byte{0x48, 0x2C, 0x2D, 0xB2, 0xC3, 0x90, 0x47, 0xC8, 0x87, 0xF8, 0x1A, 0x15, 0xBF, 0xC1, 0x30, 0xFB}
|
||||
@@ -119,7 +106,7 @@ func providerIDFromName(name string) *windows.GUID {
|
||||
sum := buffer.Sum(nil)
|
||||
sum[7] = (sum[7] & 0xf) | 0x50
|
||||
|
||||
return &windows.GUID{
|
||||
return &guid.GUID{
|
||||
Data1: binary.LittleEndian.Uint32(sum[0:4]),
|
||||
Data2: binary.LittleEndian.Uint16(sum[4:6]),
|
||||
Data3: binary.LittleEndian.Uint16(sum[6:8]),
|
||||
@@ -137,7 +124,7 @@ func NewProvider(name string, callback EnableCallback) (provider *Provider, err
|
||||
// provider ID to be manually specified. This is most useful when there is an
|
||||
// existing provider ID that must be used to conform to existing diagnostic
|
||||
// infrastructure.
|
||||
func NewProviderWithID(name string, id *windows.GUID, callback EnableCallback) (provider *Provider, err error) {
|
||||
func NewProviderWithID(name string, id *guid.GUID, callback EnableCallback) (provider *Provider, err error) {
|
||||
providerCallbackOnce.Do(func() {
|
||||
globalProviderCallback = windows.NewCallback(providerCallbackAdapter)
|
||||
})
|
||||
@@ -151,7 +138,7 @@ func NewProviderWithID(name string, id *windows.GUID, callback EnableCallback) (
|
||||
provider.ID = id
|
||||
provider.callback = callback
|
||||
|
||||
if err := eventRegister(provider.ID, globalProviderCallback, uintptr(provider.index), &provider.handle); err != nil {
|
||||
if err := eventRegister((*windows.GUID)(provider.ID), globalProviderCallback, uintptr(provider.index), &provider.handle); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -247,7 +234,7 @@ func (provider *Provider) WriteEvent(name string, eventOpts []EventOpt, fieldOpt
|
||||
dataBlobs = [][]byte{ed.bytes()}
|
||||
}
|
||||
|
||||
return provider.writeEventRaw(options.descriptor, nil, nil, [][]byte{em.bytes()}, dataBlobs)
|
||||
return provider.writeEventRaw(options.descriptor, options.activityID, options.relatedActivityID, [][]byte{em.bytes()}, dataBlobs)
|
||||
}
|
||||
|
||||
// writeEventRaw writes a single ETW event from the provider. This function is
|
||||
@@ -259,8 +246,8 @@ func (provider *Provider) WriteEvent(name string, eventOpts []EventOpt, fieldOpt
|
||||
// the ETW infrastructure.
|
||||
func (provider *Provider) writeEventRaw(
|
||||
descriptor *eventDescriptor,
|
||||
activityID *windows.GUID,
|
||||
relatedActivityID *windows.GUID,
|
||||
activityID *guid.GUID,
|
||||
relatedActivityID *guid.GUID,
|
||||
metadataBlobs [][]byte,
|
||||
dataBlobs [][]byte) error {
|
||||
|
||||
@@ -275,5 +262,5 @@ func (provider *Provider) writeEventRaw(
|
||||
dataDescriptors = append(dataDescriptors, newEventDataDescriptor(eventDataDescriptorTypeUserData, blob))
|
||||
}
|
||||
|
||||
return eventWriteTransfer(provider.handle, descriptor, activityID, relatedActivityID, dataDescriptorCount, &dataDescriptors[0])
|
||||
return eventWriteTransfer(provider.handle, descriptor, (*windows.GUID)(activityID), (*windows.GUID)(relatedActivityID), dataDescriptorCount, &dataDescriptors[0])
|
||||
}
|
||||
|
||||
126
vendor/github.com/Microsoft/go-winio/pkg/etwlogrus/hook.go
generated
vendored
126
vendor/github.com/Microsoft/go-winio/pkg/etwlogrus/hook.go
generated
vendored
@@ -1,9 +1,6 @@
|
||||
package etwlogrus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/Microsoft/go-winio/pkg/etw"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -71,7 +68,7 @@ func (h *Hook) Fire(e *logrus.Entry) error {
|
||||
fields = append(fields, etw.StringField("Message", e.Message))
|
||||
|
||||
for k, v := range e.Data {
|
||||
fields = append(fields, getFieldOpt(k, v))
|
||||
fields = append(fields, etw.SmartField(k, v))
|
||||
}
|
||||
|
||||
return h.provider.WriteEvent(
|
||||
@@ -80,127 +77,6 @@ func (h *Hook) Fire(e *logrus.Entry) error {
|
||||
fields)
|
||||
}
|
||||
|
||||
// Currently, we support logging basic builtin types (int, string, etc), slices
|
||||
// of basic builtin types, error, types derived from the basic types (e.g. "type
|
||||
// foo int"), and structs (recursively logging their fields). We do not support
|
||||
// slices of derived types (e.g. "[]foo").
|
||||
//
|
||||
// For types that we don't support, the value is formatted via fmt.Sprint, and
|
||||
// we also log a message that the type is unsupported along with the formatted
|
||||
// type. The intent of this is to make it easier to see which types are not
|
||||
// supported in traces, so we can evaluate adding support for more types in the
|
||||
// future.
|
||||
func getFieldOpt(k string, v interface{}) etw.FieldOpt {
|
||||
switch v := v.(type) {
|
||||
case bool:
|
||||
return etw.BoolField(k, v)
|
||||
case []bool:
|
||||
return etw.BoolArray(k, v)
|
||||
case string:
|
||||
return etw.StringField(k, v)
|
||||
case []string:
|
||||
return etw.StringArray(k, v)
|
||||
case int:
|
||||
return etw.IntField(k, v)
|
||||
case []int:
|
||||
return etw.IntArray(k, v)
|
||||
case int8:
|
||||
return etw.Int8Field(k, v)
|
||||
case []int8:
|
||||
return etw.Int8Array(k, v)
|
||||
case int16:
|
||||
return etw.Int16Field(k, v)
|
||||
case []int16:
|
||||
return etw.Int16Array(k, v)
|
||||
case int32:
|
||||
return etw.Int32Field(k, v)
|
||||
case []int32:
|
||||
return etw.Int32Array(k, v)
|
||||
case int64:
|
||||
return etw.Int64Field(k, v)
|
||||
case []int64:
|
||||
return etw.Int64Array(k, v)
|
||||
case uint:
|
||||
return etw.UintField(k, v)
|
||||
case []uint:
|
||||
return etw.UintArray(k, v)
|
||||
case uint8:
|
||||
return etw.Uint8Field(k, v)
|
||||
case []uint8:
|
||||
return etw.Uint8Array(k, v)
|
||||
case uint16:
|
||||
return etw.Uint16Field(k, v)
|
||||
case []uint16:
|
||||
return etw.Uint16Array(k, v)
|
||||
case uint32:
|
||||
return etw.Uint32Field(k, v)
|
||||
case []uint32:
|
||||
return etw.Uint32Array(k, v)
|
||||
case uint64:
|
||||
return etw.Uint64Field(k, v)
|
||||
case []uint64:
|
||||
return etw.Uint64Array(k, v)
|
||||
case uintptr:
|
||||
return etw.UintptrField(k, v)
|
||||
case []uintptr:
|
||||
return etw.UintptrArray(k, v)
|
||||
case float32:
|
||||
return etw.Float32Field(k, v)
|
||||
case []float32:
|
||||
return etw.Float32Array(k, v)
|
||||
case float64:
|
||||
return etw.Float64Field(k, v)
|
||||
case []float64:
|
||||
return etw.Float64Array(k, v)
|
||||
case error:
|
||||
return etw.StringField(k, v.Error())
|
||||
default:
|
||||
switch rv := reflect.ValueOf(v); rv.Kind() {
|
||||
case reflect.Bool:
|
||||
return getFieldOpt(k, rv.Bool())
|
||||
case reflect.Int:
|
||||
return getFieldOpt(k, int(rv.Int()))
|
||||
case reflect.Int8:
|
||||
return getFieldOpt(k, int8(rv.Int()))
|
||||
case reflect.Int16:
|
||||
return getFieldOpt(k, int16(rv.Int()))
|
||||
case reflect.Int32:
|
||||
return getFieldOpt(k, int32(rv.Int()))
|
||||
case reflect.Int64:
|
||||
return getFieldOpt(k, int64(rv.Int()))
|
||||
case reflect.Uint:
|
||||
return getFieldOpt(k, uint(rv.Uint()))
|
||||
case reflect.Uint8:
|
||||
return getFieldOpt(k, uint8(rv.Uint()))
|
||||
case reflect.Uint16:
|
||||
return getFieldOpt(k, uint16(rv.Uint()))
|
||||
case reflect.Uint32:
|
||||
return getFieldOpt(k, uint32(rv.Uint()))
|
||||
case reflect.Uint64:
|
||||
return getFieldOpt(k, uint64(rv.Uint()))
|
||||
case reflect.Uintptr:
|
||||
return getFieldOpt(k, uintptr(rv.Uint()))
|
||||
case reflect.Float32:
|
||||
return getFieldOpt(k, float32(rv.Float()))
|
||||
case reflect.Float64:
|
||||
return getFieldOpt(k, float64(rv.Float()))
|
||||
case reflect.String:
|
||||
return getFieldOpt(k, rv.String())
|
||||
case reflect.Struct:
|
||||
fields := make([]etw.FieldOpt, 0, rv.NumField())
|
||||
for i := 0; i < rv.NumField(); i++ {
|
||||
field := rv.Field(i)
|
||||
if field.CanInterface() {
|
||||
fields = append(fields, getFieldOpt(k, field.Interface()))
|
||||
}
|
||||
}
|
||||
return etw.Struct(k, fields...)
|
||||
}
|
||||
}
|
||||
|
||||
return etw.StringField(k, fmt.Sprintf("(Unsupported: %T) %v", v, v))
|
||||
}
|
||||
|
||||
// Close cleans up the hook and closes the ETW provider. If the provder was
|
||||
// registered by etwlogrus, it will be closed as part of `Close`. If the
|
||||
// provider was passed in, it will not be closed.
|
||||
|
||||
110
vendor/github.com/Microsoft/go-winio/pkg/guid/guid.go
generated
vendored
Normal file
110
vendor/github.com/Microsoft/go-winio/pkg/guid/guid.go
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
package guid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
var _ = (json.Marshaler)(&GUID{})
|
||||
var _ = (json.Unmarshaler)(&GUID{})
|
||||
|
||||
// GUID represents a GUID/UUID. It has the same structure as
|
||||
// golang.org/x/sys/windows.GUID so that it can be used with functions expecting
|
||||
// that type. It is defined as its own type so that stringification and
|
||||
// marshaling can be supported. The representation matches that used by native
|
||||
// Windows code.
|
||||
type GUID windows.GUID
|
||||
|
||||
// NewV4 returns a new version 4 (pseudorandom) GUID, as defined by RFC 4122.
|
||||
func NewV4() (*GUID, error) {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var g GUID
|
||||
g.Data1 = binary.LittleEndian.Uint32(b[0:4])
|
||||
g.Data2 = binary.LittleEndian.Uint16(b[4:6])
|
||||
g.Data3 = binary.LittleEndian.Uint16(b[6:8])
|
||||
copy(g.Data4[:], b[8:16])
|
||||
|
||||
g.Data3 = (g.Data3 & 0x0fff) | 0x4000 // Version 4 (randomly generated)
|
||||
g.Data4[0] = (g.Data4[0] & 0x3f) | 0x80 // RFC4122 variant
|
||||
return &g, nil
|
||||
}
|
||||
|
||||
func (g *GUID) String() string {
|
||||
return fmt.Sprintf(
|
||||
"%08x-%04x-%04x-%04x-%012x",
|
||||
g.Data1,
|
||||
g.Data2,
|
||||
g.Data3,
|
||||
g.Data4[:2],
|
||||
g.Data4[2:])
|
||||
}
|
||||
|
||||
// FromString parses a string containing a GUID and returns the GUID. The only
|
||||
// format currently supported is the `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`
|
||||
// format.
|
||||
func FromString(s string) (*GUID, error) {
|
||||
if len(s) != 36 {
|
||||
return nil, errors.New("invalid GUID format (length)")
|
||||
}
|
||||
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
|
||||
return nil, errors.New("invalid GUID format (dashes)")
|
||||
}
|
||||
|
||||
var g GUID
|
||||
|
||||
data1, err := strconv.ParseUint(s[0:8], 16, 32)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid GUID format (Data1)")
|
||||
}
|
||||
g.Data1 = uint32(data1)
|
||||
|
||||
data2, err := strconv.ParseUint(s[9:13], 16, 16)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid GUID format (Data2)")
|
||||
}
|
||||
g.Data2 = uint16(data2)
|
||||
|
||||
data3, err := strconv.ParseUint(s[14:18], 16, 16)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid GUID format (Data3)")
|
||||
}
|
||||
g.Data3 = uint16(data3)
|
||||
|
||||
for i, x := range []int{19, 21, 24, 26, 28, 30, 32, 34} {
|
||||
v, err := strconv.ParseUint(s[x:x+2], 16, 8)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid GUID format (Data4)")
|
||||
}
|
||||
g.Data4[i] = uint8(v)
|
||||
}
|
||||
|
||||
return &g, nil
|
||||
}
|
||||
|
||||
// MarshalJSON marshals the GUID to JSON representation and returns it as a
|
||||
// slice of bytes.
|
||||
func (g *GUID) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(g.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON unmarshals a GUID from JSON representation and sets itself to
|
||||
// the unmarshaled GUID.
|
||||
func (g *GUID) UnmarshalJSON(data []byte) error {
|
||||
g2, err := FromString(strings.Trim(string(data), "\""))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*g = *g2
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user