Skip to content

Commit

Permalink
gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lysShub committed May 25, 2024
1 parent 17a2d4b commit 82e3708
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 127 deletions.
42 changes: 21 additions & 21 deletions dll/dll_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//go:build windows
// +build windows

package dll_test

import (
"testing"

"github.com/lysShub/divert-go/dll"
)

var (
test = dll.NewLazyDLL(make([]byte, 3))
openProc = test.NewProc("WinDivertOpen")
)

func TestXxx(t *testing.T) {
dll.ResetLazyDll(test, `D:\OneDrive\code\go\divert-go\embed\WinDivert64.dll`)

openProc.Find()
}
//go:build windows
// +build windows

package dll_test

import (
"testing"

"github.com/lysShub/divert-go/dll"
)

var (
test = dll.NewLazyDLL(make([]byte, 3))
openProc = test.NewProc("WinDivertOpen")
)

func TestXxx(t *testing.T) {
dll.ResetLazyDll(test, `D:\OneDrive\code\go\divert-go\embed\WinDivert64.dll`)

openProc.Find()
}
182 changes: 91 additions & 91 deletions dll/mem.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
package dll

import (
"sync"
"sync/atomic"
"syscall"
"unsafe"

"github.com/pkg/errors"
"golang.zx2c4.com/wireguard/windows/driver/memmod"
)

type MemLazyDll struct {
Data []byte

mu sync.Mutex
dll *memmod.Module
}

var _ LazyDll = (*MemLazyDll)(nil)

func (d *MemLazyDll) Handle() uintptr {
d.mustLoad()
return d.dll.BaseAddr()
}
func (d *MemLazyDll) Load() (err error) {
if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil {
return nil
}
d.mu.Lock()
defer d.mu.Unlock()
if d.dll != nil {
return nil
}

dll, err := memmod.LoadLibrary(d.Data)
if err != nil {
return errors.WithStack(err)
}
atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))
return nil
}
func (d *MemLazyDll) mustLoad() {
err := d.Load()
if err != nil {
panic(err)
}
}

func (d *MemLazyDll) NewProc(name string) LazyProc {
return &MemLazyProc{Name: name, l: d}
}

type MemLazyProc struct {
Name string

mu sync.Mutex
l *MemLazyDll
proc uintptr
}

func (p *MemLazyProc) Addr() uintptr {
p.mustFind()
return p.proc
}
func (p *MemLazyProc) Call(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error) {
p.mustFind()
return syscall.SyscallN(p.Addr(), a...)
}
func (p *MemLazyProc) Find() error {
if atomic.LoadUintptr(&p.proc) == 0 {
p.mu.Lock()
defer p.mu.Unlock()

if p.proc == 0 {
err := p.l.Load()
if err != nil {
return err
}

proc, err := p.l.dll.ProcAddressByName(p.Name)
if err != nil {
return err
}
atomic.StoreUintptr(&p.proc, proc)
}

}
return nil
}
func (p *MemLazyProc) mustFind() {}
package dll

import (
"sync"
"sync/atomic"
"syscall"
"unsafe"

"github.com/pkg/errors"
"golang.zx2c4.com/wireguard/windows/driver/memmod"
)

type MemLazyDll struct {
Data []byte

mu sync.Mutex
dll *memmod.Module
}

var _ LazyDll = (*MemLazyDll)(nil)

func (d *MemLazyDll) Handle() uintptr {
d.mustLoad()
return d.dll.BaseAddr()
}
func (d *MemLazyDll) Load() (err error) {
if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil {
return nil
}
d.mu.Lock()
defer d.mu.Unlock()
if d.dll != nil {
return nil
}

dll, err := memmod.LoadLibrary(d.Data)
if err != nil {
return errors.WithStack(err)
}
atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll))
return nil
}
func (d *MemLazyDll) mustLoad() {
err := d.Load()
if err != nil {
panic(err)
}
}

func (d *MemLazyDll) NewProc(name string) LazyProc {
return &MemLazyProc{Name: name, l: d}
}

type MemLazyProc struct {
Name string

mu sync.Mutex
l *MemLazyDll
proc uintptr
}

func (p *MemLazyProc) Addr() uintptr {
p.mustFind()
return p.proc
}
func (p *MemLazyProc) Call(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error) {
p.mustFind()
return syscall.SyscallN(p.Addr(), a...)
}
func (p *MemLazyProc) Find() error {
if atomic.LoadUintptr(&p.proc) == 0 {
p.mu.Lock()
defer p.mu.Unlock()

if p.proc == 0 {
err := p.l.Load()
if err != nil {
return err
}

proc, err := p.l.dll.ProcAddressByName(p.Name)
if err != nil {
return err
}
atomic.StoreUintptr(&p.proc, proc)
}

}
return nil
}
func (p *MemLazyProc) mustFind() {}
30 changes: 15 additions & 15 deletions dll/sys.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dll

import (
"golang.org/x/sys/windows"
)

type SysLazyDll struct {
windows.LazyDLL
}

var _ LazyDll = (*SysLazyDll)(nil)

func (l *SysLazyDll) NewProc(name string) LazyProc {
return l.LazyDLL.NewProc(name)
}
package dll

import (
"golang.org/x/sys/windows"
)

type SysLazyDll struct {
windows.LazyDLL
}

var _ LazyDll = (*SysLazyDll)(nil)

func (l *SysLazyDll) NewProc(name string) LazyProc {
return l.LazyDLL.NewProc(name)
}

0 comments on commit 82e3708

Please sign in to comment.