Skip to content

Commit

Permalink
Use golang.org/x/sys
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max authored and danieljoos committed Aug 20, 2021
1 parent 00cf980 commit 258b3ae
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 113 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
32 changes: 0 additions & 32 deletions .github/workflows/go.yml

This file was deleted.

24 changes: 24 additions & 0 deletions .github/workflows/godev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: godev

on:
push:
tags:
- 'v*'

jobs:
update:
runs-on: windows-latest
steps:
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
-
name: Call pkg.go.dev
run: |
go get github.com/${GITHUB_REPOSITORY}@${GITHUB_REF#refs/tags/}
env:
GO111MODULE: on
GOPROXY: https://proxy.golang.org
shell: bash
59 changes: 59 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: test

on:
push:
branches:
- 'master'
tags:
- 'v*'
pull_request:
branches:
- 'master'

jobs:
test:
runs-on: windows-latest
env:
GO111MODULE: on
strategy:
fail-fast: false
matrix:
go:
- 1.14
- 1.15
- 1.16
- 1.17
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
-
name: Cache Go modules
uses: actions/cache@v2
with:
path: |
%LocalAppData%\go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go }}-
-
name: Go mod
run: |
go mod download
shell: bash
-
name: Test
run: |
go test -coverprofile=coverage.txt -covermode=atomic ./...
shell: bash
-
name: Upload coverage
uses: codecov/codecov-action@v2
with:
file: ./coverage.txt
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ _testmain.go

*.exe
*.test

coverage.txt
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ wincred

Go wrapper around the Windows Credential Manager API functions.

![Go](https://github.com/danieljoos/wincred/workflows/Go/badge.svg)
[![GoDoc](https://godoc.org/github.com/danieljoos/wincred?status.svg)](https://godoc.org/github.com/danieljoos/wincred)

[![GitHub release](https://img.shields.io/github/release/danieljoos/wincred.svg?style=flat-square)](https://github.com/danieljoos/wincred/releases/latest)
[![Test Status](https://img.shields.io/github/workflow/status/danieljoos/wincred/test?label=test&logo=github&style=flat-square)](https://github.com/danieljoos/wincred/actions?query=workflow%3Atest)
[![Go Report Card](https://goreportcard.com/badge/github.com/danieljoos/wincred)](https://goreportcard.com/report/github.com/danieljoos/wincred)
[![Codecov](https://img.shields.io/codecov/c/github/danieljoos/wincred?logo=codecov&style=flat-square)](https://codecov.io/gh/danieljoos/wincred)
[![PkgGoDev](https://img.shields.io/badge/go.dev-docs-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/danieljoos/wincred)

Installation
------------
Expand Down
39 changes: 12 additions & 27 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,11 @@ package wincred
import (
"encoding/binary"
"reflect"
"syscall"
"time"
"unicode/utf16"
"unsafe"
)

// uf16PtrToString creates a Go string from a pointer to a UTF16 encoded zero-terminated string.
// Such pointers are returned from the Windows API calls.
// The function creates a copy of the string.
func utf16PtrToString(wstr *uint16) string {
if wstr != nil {
for len := 0; ; len++ {
ptr := unsafe.Pointer(uintptr(unsafe.Pointer(wstr)) + uintptr(len)*unsafe.Sizeof(*wstr)) // see https://golang.org/pkg/unsafe/#Pointer (3)
if *(*uint16)(ptr) == 0 {
return string(utf16.Decode(*(*[]uint16)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(wstr)),
Len: len,
Cap: len,
}))))
}
}
}
return ""
}
syscall "golang.org/x/sys/windows"
)

// utf16ToByte creates a byte array from a given UTF 16 char array.
func utf16ToByte(wstr []uint16) (result []byte) {
Expand All @@ -41,7 +22,11 @@ func utf16ToByte(wstr []uint16) (result []byte) {

// utf16FromString creates a UTF16 char array from a string.
func utf16FromString(str string) []uint16 {
return syscall.StringToUTF16(str)
res, err := syscall.UTF16FromString(str)
if err != nil {
return []uint16{}
}
return res
}

// goBytes copies the given C byte array to a Go byte array (see `C.GoBytes`).
Expand All @@ -65,10 +50,10 @@ func sysToCredential(cred *sysCREDENTIAL) (result *Credential) {
return nil
}
result = new(Credential)
result.Comment = utf16PtrToString(cred.Comment)
result.TargetName = utf16PtrToString(cred.TargetName)
result.TargetAlias = utf16PtrToString(cred.TargetAlias)
result.UserName = utf16PtrToString(cred.UserName)
result.Comment = syscall.UTF16PtrToString(cred.Comment)
result.TargetName = syscall.UTF16PtrToString(cred.TargetName)
result.TargetAlias = syscall.UTF16PtrToString(cred.TargetAlias)
result.UserName = syscall.UTF16PtrToString(cred.UserName)
result.LastWritten = time.Unix(0, cred.LastWritten.Nanoseconds())
result.Persist = CredentialPersistence(cred.Persist)
result.CredentialBlob = goBytes(cred.CredentialBlob, cred.CredentialBlobSize)
Expand All @@ -80,7 +65,7 @@ func sysToCredential(cred *sysCREDENTIAL) (result *Credential) {
}))
for i, attr := range attrSlice {
resultAttr := &result.Attributes[i]
resultAttr.Keyword = utf16PtrToString(attr.Keyword)
resultAttr.Keyword = syscall.UTF16PtrToString(attr.Keyword)
resultAttr.Value = goBytes(attr.Value, attr.ValueSize)
}
return result
Expand Down
23 changes: 0 additions & 23 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package wincred

import (
"syscall"
"testing"
"time"
"unsafe"
Expand All @@ -22,28 +21,6 @@ func fixtureCredential() (cred *Credential) {
return
}

func TestUtf16PtrToString(t *testing.T) {
input := "Foo Bar"
utf16Ptr, err := syscall.UTF16PtrFromString(input)
output := utf16PtrToString(utf16Ptr)
assert.Nil(t, err)
assert.Equal(t, input, output)
}

func TestUtf16PtrToString_Nil(t *testing.T) {
output := utf16PtrToString(nil)
assert.Equal(t, "", output)
}

func BenchmarkUtf16PtrToString(b *testing.B) {
input := "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam" +
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam"
utf16Ptr, _ := syscall.UTF16PtrFromString(input)
for i := 0; i < b.N; i++ {
utf16PtrToString(utf16Ptr)
}
}

func TestUtf16ToByte(t *testing.T) {
input := []uint16{1, 2, 3, 4, 258}
output := utf16ToByte(input)
Expand Down
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ module github.com/danieljoos/wincred
go 1.13

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c
)
22 changes: 6 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c h1:Lyn7+CqXIiC+LOR9aHD6jDK+hPcmAuCfuXztd1v4w1Q=
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package wincred

import (
"reflect"
"syscall"
"unsafe"

syscall "golang.org/x/sys/windows"
)

var (
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")

modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
procCredRead proc = modadvapi32.NewProc("CredReadW")
procCredWrite proc = modadvapi32.NewProc("CredWriteW")
procCredDelete proc = modadvapi32.NewProc("CredDeleteW")
Expand Down
4 changes: 2 additions & 2 deletions wincred.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
func GetGenericCredential(targetName string) (*GenericCredential, error) {
cred, err := sysCredRead(targetName, sysCRED_TYPE_GENERIC)
if cred != nil {
return &GenericCredential{*cred}, err
return &GenericCredential{Credential: *cred}, err
}
return nil, err
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func (t *GenericCredential) Delete() (err error) {
func GetDomainPassword(targetName string) (*DomainPassword, error) {
cred, err := sysCredRead(targetName, sysCRED_TYPE_DOMAIN_PASSWORD)
if cred != nil {
return &DomainPassword{*cred}, err
return &DomainPassword{Credential: *cred}, err
}
return nil, err
}
Expand Down

0 comments on commit 258b3ae

Please sign in to comment.