提交 a60a145f 编写于 作者: martianzhang's avatar martianzhang

update pingcap parser deps

上级 cbae6057
......@@ -81,7 +81,7 @@ cover: test
{print "\033[93m"$$0"%\033[0m"}}'
# Builds the project
build: fmt tidb-parser
build: fmt
@echo "\033[92mBuilding ...\033[0m"
@mkdir -p bin
@bash ./genver.sh
......@@ -91,16 +91,6 @@ build: fmt tidb-parser
done ; exit $$ret
@echo "build Success!"
.PHONY: fast
fast: fmt
@echo "\033[92mBuilding ...\033[0m"
@bash ./genver.sh
@ret=0 && for d in $$(go list -f '{{if (eq .Name "main")}}{{.ImportPath}}{{end}}' ./...); do \
b=$$(basename $${d}) ; \
go build ${GCFLAGS} -o bin/$${b} $$d || ret=$$? ; \
done ; exit $$ret
@echo "build Success!"
# Installs our project: copies binaries
install: build
@echo "\033[92mInstall ...\033[0m"
......@@ -109,7 +99,7 @@ install: build
# Generate doc use -list* command
.PHONY: doc
doc: fast
doc: build
@echo "\033[92mAuto generate doc ...\033[0m"
./bin/soar -list-heuristic-rules > doc/heuristic.md
./bin/soar -list-rewrite-rules > doc/rewrite.md
......@@ -133,27 +123,22 @@ vitess:
.PHONY: tidb
tidb:
@echo "\033[92mUpdate tidb deps ...\033[0m"
@echo -n "Current TiDB commit hash: "
@(cd ${GOPATH}/src/github.com/pingcap/tidb/ 2>/dev/null && git checkout master && git rev-parse HEAD) || echo "(init)"
@(go get -v -d github.com/pingcap/tidb/... || echo "go get tidb")
@echo -n "TiDB update to: "
@cd ${GOPATH}/src/github.com/pingcap/tidb/ && git pull && git rev-parse HEAD
govendor fetch -v github.com/pingcap/tidb/...
# make pingcap parser
.PHONY: pingcap-parser
pingcap-parser: tidb
@echo "\033[92mUpdate pingcap parser deps ...\033[0m"
govendor fetch -v github.com/pingcap/parser/...
# Update all vendor
.PHONY: vendor
vendor: vitess tidb
# make tidb parser
.PHONY: tidb-parser
tidb-parser: tidb
@echo "\033[92mimporting tidb sql parser ...\033[0m"
@cd ${GOPATH}/src/github.com/pingcap/tidb && git checkout --quiet ec9672cea6612481b1da845dbab620b7a5581ca4 && make parser
vendor: vitess pingcap-parser
# gometalinter
# 如果有不想改的lint问题可以使用metalinter.sh加黑名单
#@bash doc/example/metalinter.sh
.PHONY: lint
lint: fast
lint: build
@echo "\033[92mRun linter check ...\033[0m"
CGO_ENABLED=0 retool do gometalinter.v2 -j 1 --config doc/example/metalinter.json ./...
retool do revive -formatter friendly --exclude vendor/... -config doc/example/revive.toml ./...
......@@ -206,10 +191,10 @@ main_test: install
@echo "main_test Success!"
.PHONY: daily
daily: | deps fmt vendor tidb-parser docker cover doc lint release install main_test clean logo
daily: | deps fmt vendor docker cover doc lint release install main_test clean logo
@echo "\033[92mdaily build finished\033[0m"
# vendor, tidb-parser, docker will cost long time, if all those are ready, daily-quick will much more fast.
# vendor, docker will cost long time, if all those are ready, daily-quick will much more fast.
.PHONY: daily-quick
daily-quick: | deps fmt cover doc lint logo
@echo "\033[92mdaily-quick build finished\033[0m"
......
......@@ -27,12 +27,10 @@ import (
"github.com/XiaoMi/soar/ast"
"github.com/XiaoMi/soar/common"
"github.com/XiaoMi/soar/database"
"github.com/gedex/inflector"
"github.com/percona/go-mysql/query"
tidb "github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/types"
tidb "github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"
"vitess.io/vitess/go/vt/sqlparser"
)
......@@ -1608,8 +1606,8 @@ func (q *Query4Audit) RuleImpreciseDataType() Rule {
// Insert statement
for _, values := range node.Lists {
for _, value := range values {
switch value.GetDatum().Kind() {
case types.KindFloat32, types.KindFloat64, types.KindMysqlDecimal:
switch value.GetType().Tp {
case mysql.TypeNewDecimal, mysql.TypeFloat:
rule = HeuristicRules["COL.009"]
}
}
......@@ -1619,8 +1617,8 @@ func (q *Query4Audit) RuleImpreciseDataType() Rule {
// Select statement
switch where := node.Where.(type) {
case *tidb.BinaryOperationExpr:
switch where.R.GetDatum().Kind() {
case types.KindFloat32, types.KindFloat64, types.KindMysqlDecimal:
switch where.R.GetType().Tp {
case mysql.TypeNewDecimal, mysql.TypeFloat:
rule = HeuristicRules["COL.009"]
}
}
......
......@@ -29,7 +29,7 @@ import (
"github.com/kr/pretty"
"github.com/percona/go-mysql/query"
tidb "github.com/pingcap/tidb/ast"
tidb "github.com/pingcap/parser/ast"
"vitess.io/vitess/go/vt/sqlparser"
)
......
......@@ -20,8 +20,10 @@ import (
"github.com/XiaoMi/soar/common"
"github.com/kr/pretty"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
// for pincap parser
_ "github.com/pingcap/tidb/types/parser_driver"
)
// TiParse TiDB 语法解析
......
......@@ -28,7 +28,7 @@ import (
"github.com/XiaoMi/soar/ast"
"github.com/XiaoMi/soar/common"
tidb "github.com/pingcap/tidb/ast"
tidb "github.com/pingcap/parser/ast"
"github.com/tidwall/gjson"
"vitess.io/vitess/go/vt/sqlparser"
)
......
The MIT License (MIT)
Copyright (c) 2013 Stack Exchange
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wmi
===
Package wmi provides a WQL interface to Windows WMI.
Note: It interfaces with WMI on the local machine, therefore it only runs on Windows.
// +build windows
package wmi
import (
"fmt"
"reflect"
"runtime"
"sync"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
// SWbemServices is used to access wmi. See https://msdn.microsoft.com/en-us/library/aa393719(v=vs.85).aspx
type SWbemServices struct {
//TODO: track namespace. Not sure if we can re connect to a different namespace using the same instance
cWMIClient *Client //This could also be an embedded struct, but then we would need to branch on Client vs SWbemServices in the Query method
sWbemLocatorIUnknown *ole.IUnknown
sWbemLocatorIDispatch *ole.IDispatch
queries chan *queryRequest
closeError chan error
lQueryorClose sync.Mutex
}
type queryRequest struct {
query string
dst interface{}
args []interface{}
finished chan error
}
// InitializeSWbemServices will return a new SWbemServices object that can be used to query WMI
func InitializeSWbemServices(c *Client, connectServerArgs ...interface{}) (*SWbemServices, error) {
//fmt.Println("InitializeSWbemServices: Starting")
//TODO: implement connectServerArgs as optional argument for init with connectServer call
s := new(SWbemServices)
s.cWMIClient = c
s.queries = make(chan *queryRequest)
initError := make(chan error)
go s.process(initError)
err, ok := <-initError
if ok {
return nil, err //Send error to caller
}
//fmt.Println("InitializeSWbemServices: Finished")
return s, nil
}
// Close will clear and release all of the SWbemServices resources
func (s *SWbemServices) Close() error {
s.lQueryorClose.Lock()
if s == nil || s.sWbemLocatorIDispatch == nil {
s.lQueryorClose.Unlock()
return fmt.Errorf("SWbemServices is not Initialized")
}
if s.queries == nil {
s.lQueryorClose.Unlock()
return fmt.Errorf("SWbemServices has been closed")
}
//fmt.Println("Close: sending close request")
var result error
ce := make(chan error)
s.closeError = ce //Race condition if multiple callers to close. May need to lock here
close(s.queries) //Tell background to shut things down
s.lQueryorClose.Unlock()
err, ok := <-ce
if ok {
result = err
}
//fmt.Println("Close: finished")
return result
}
func (s *SWbemServices) process(initError chan error) {
//fmt.Println("process: starting background thread initialization")
//All OLE/WMI calls must happen on the same initialized thead, so lock this goroutine
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
if err != nil {
oleCode := err.(*ole.OleError).Code()
if oleCode != ole.S_OK && oleCode != S_FALSE {
initError <- fmt.Errorf("ole.CoInitializeEx error: %v", err)
return
}
}
defer ole.CoUninitialize()
unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
if err != nil {
initError <- fmt.Errorf("CreateObject SWbemLocator error: %v", err)
return
} else if unknown == nil {
initError <- ErrNilCreateObject
return
}
defer unknown.Release()
s.sWbemLocatorIUnknown = unknown
dispatch, err := s.sWbemLocatorIUnknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
initError <- fmt.Errorf("SWbemLocator QueryInterface error: %v", err)
return
}
defer dispatch.Release()
s.sWbemLocatorIDispatch = dispatch
// we can't do the ConnectServer call outside the loop unless we find a way to track and re-init the connectServerArgs
//fmt.Println("process: initialized. closing initError")
close(initError)
//fmt.Println("process: waiting for queries")
for q := range s.queries {
//fmt.Printf("process: new query: len(query)=%d\n", len(q.query))
errQuery := s.queryBackground(q)
//fmt.Println("process: s.queryBackground finished")
if errQuery != nil {
q.finished <- errQuery
}
close(q.finished)
}
//fmt.Println("process: queries channel closed")
s.queries = nil //set channel to nil so we know it is closed
//TODO: I think the Release/Clear calls can panic if things are in a bad state.
//TODO: May need to recover from panics and send error to method caller instead.
close(s.closeError)
}
// Query runs the WQL query using a SWbemServices instance and appends the values to dst.
//
// dst must have type *[]S or *[]*S, for some struct type S. Fields selected in
// the query must have the same name in dst. Supported types are all signed and
// unsigned integers, time.Time, string, bool, or a pointer to one of those.
// Array types are not supported.
//
// By default, the local machine and default namespace are used. These can be
// changed using connectServerArgs. See
// http://msdn.microsoft.com/en-us/library/aa393720.aspx for details.
func (s *SWbemServices) Query(query string, dst interface{}, connectServerArgs ...interface{}) error {
s.lQueryorClose.Lock()
if s == nil || s.sWbemLocatorIDispatch == nil {
s.lQueryorClose.Unlock()
return fmt.Errorf("SWbemServices is not Initialized")
}
if s.queries == nil {
s.lQueryorClose.Unlock()
return fmt.Errorf("SWbemServices has been closed")
}
//fmt.Println("Query: Sending query request")
qr := queryRequest{
query: query,
dst: dst,
args: connectServerArgs,
finished: make(chan error),
}
s.queries <- &qr
s.lQueryorClose.Unlock()
err, ok := <-qr.finished
if ok {
//fmt.Println("Query: Finished with error")
return err //Send error to caller
}
//fmt.Println("Query: Finished")
return nil
}
func (s *SWbemServices) queryBackground(q *queryRequest) error {
if s == nil || s.sWbemLocatorIDispatch == nil {
return fmt.Errorf("SWbemServices is not Initialized")
}
wmi := s.sWbemLocatorIDispatch //Should just rename in the code, but this will help as we break things apart
//fmt.Println("queryBackground: Starting")
dv := reflect.ValueOf(q.dst)
if dv.Kind() != reflect.Ptr || dv.IsNil() {
return ErrInvalidEntityType
}
dv = dv.Elem()
mat, elemType := checkMultiArg(dv)
if mat == multiArgTypeInvalid {
return ErrInvalidEntityType
}
// service is a SWbemServices
serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", q.args...)
if err != nil {
return err
}
service := serviceRaw.ToIDispatch()
defer serviceRaw.Clear()
// result is a SWBemObjectSet
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", q.query)
if err != nil {
return err
}
result := resultRaw.ToIDispatch()
defer resultRaw.Clear()
count, err := oleInt64(result, "Count")
if err != nil {
return err
}
enumProperty, err := result.GetProperty("_NewEnum")
if err != nil {
return err
}
defer enumProperty.Clear()
enum, err := enumProperty.ToIUnknown().IEnumVARIANT(ole.IID_IEnumVariant)
if err != nil {
return err
}
if enum == nil {
return fmt.Errorf("can't get IEnumVARIANT, enum is nil")
}
defer enum.Release()
// Initialize a slice with Count capacity
dv.Set(reflect.MakeSlice(dv.Type(), 0, int(count)))
var errFieldMismatch error
for itemRaw, length, err := enum.Next(1); length > 0; itemRaw, length, err = enum.Next(1) {
if err != nil {
return err
}
err := func() error {
// item is a SWbemObject, but really a Win32_Process
item := itemRaw.ToIDispatch()
defer item.Release()
ev := reflect.New(elemType)
if err = s.cWMIClient.loadEntity(ev.Interface(), item); err != nil {
if _, ok := err.(*ErrFieldMismatch); ok {
// We continue loading entities even in the face of field mismatch errors.
// If we encounter any other error, that other error is returned. Otherwise,
// an ErrFieldMismatch is returned.
errFieldMismatch = err
} else {
return err
}
}
if mat != multiArgTypeStructPtr {
ev = ev.Elem()
}
dv.Set(reflect.Append(dv, ev))
return nil
}()
if err != nil {
return err
}
}
//fmt.Println("queryBackground: Finished")
return errFieldMismatch
}
// +build windows
/*
Package wmi provides a WQL interface for WMI on Windows.
Example code to print names of running processes:
type Win32_Process struct {
Name string
}
func main() {
var dst []Win32_Process
q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst)
if err != nil {
log.Fatal(err)
}
for i, v := range dst {
println(i, v.Name)
}
}
*/
package wmi
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
var l = log.New(os.Stdout, "", log.LstdFlags)
var (
ErrInvalidEntityType = errors.New("wmi: invalid entity type")
// ErrNilCreateObject is the error returned if CreateObject returns nil even
// if the error was nil.
ErrNilCreateObject = errors.New("wmi: create object returned nil")
lock sync.Mutex
)
// S_FALSE is returned by CoInitializeEx if it was already called on this thread.
const S_FALSE = 0x00000001
// QueryNamespace invokes Query with the given namespace on the local machine.
func QueryNamespace(query string, dst interface{}, namespace string) error {
return Query(query, dst, nil, namespace)
}
// Query runs the WQL query and appends the values to dst.
//
// dst must have type *[]S or *[]*S, for some struct type S. Fields selected in
// the query must have the same name in dst. Supported types are all signed and
// unsigned integers, time.Time, string, bool, or a pointer to one of those.
// Array types are not supported.
//
// By default, the local machine and default namespace are used. These can be
// changed using connectServerArgs. See
// http://msdn.microsoft.com/en-us/library/aa393720.aspx for details.
//
// Query is a wrapper around DefaultClient.Query.
func Query(query string, dst interface{}, connectServerArgs ...interface{}) error {
if DefaultClient.SWbemServicesClient == nil {
return DefaultClient.Query(query, dst, connectServerArgs...)
}
return DefaultClient.SWbemServicesClient.Query(query, dst, connectServerArgs...)
}
// A Client is an WMI query client.
//
// Its zero value (DefaultClient) is a usable client.
type Client struct {
// NonePtrZero specifies if nil values for fields which aren't pointers
// should be returned as the field types zero value.
//
// Setting this to true allows stucts without pointer fields to be used
// without the risk failure should a nil value returned from WMI.
NonePtrZero bool
// PtrNil specifies if nil values for pointer fields should be returned
// as nil.
//
// Setting this to true will set pointer fields to nil where WMI
// returned nil, otherwise the types zero value will be returned.
PtrNil bool
// AllowMissingFields specifies that struct fields not present in the
// query result should not result in an error.
//
// Setting this to true allows custom queries to be used with full
// struct definitions instead of having to define multiple structs.
AllowMissingFields bool
// SWbemServiceClient is an optional SWbemServices object that can be
// initialized and then reused across multiple queries. If it is null
// then the method will initialize a new temporary client each time.
SWbemServicesClient *SWbemServices
}
// DefaultClient is the default Client and is used by Query, QueryNamespace
var DefaultClient = &Client{}
// Query runs the WQL query and appends the values to dst.
//
// dst must have type *[]S or *[]*S, for some struct type S. Fields selected in
// the query must have the same name in dst. Supported types are all signed and
// unsigned integers, time.Time, string, bool, or a pointer to one of those.
// Array types are not supported.
//
// By default, the local machine and default namespace are used. These can be
// changed using connectServerArgs. See
// http://msdn.microsoft.com/en-us/library/aa393720.aspx for details.
func (c *Client) Query(query string, dst interface{}, connectServerArgs ...interface{}) error {
dv := reflect.ValueOf(dst)
if dv.Kind() != reflect.Ptr || dv.IsNil() {
return ErrInvalidEntityType
}
dv = dv.Elem()
mat, elemType := checkMultiArg(dv)
if mat == multiArgTypeInvalid {
return ErrInvalidEntityType
}
lock.Lock()
defer lock.Unlock()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
if err != nil {
oleCode := err.(*ole.OleError).Code()
if oleCode != ole.S_OK && oleCode != S_FALSE {
return err
}
}
defer ole.CoUninitialize()
unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
if err != nil {
return err
} else if unknown == nil {
return ErrNilCreateObject
}
defer unknown.Release()
wmi, err := unknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
return err
}
defer wmi.Release()
// service is a SWbemServices
serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", connectServerArgs...)
if err != nil {
return err
}
service := serviceRaw.ToIDispatch()
defer serviceRaw.Clear()
// result is a SWBemObjectSet
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", query)
if err != nil {
return err
}
result := resultRaw.ToIDispatch()
defer resultRaw.Clear()
count, err := oleInt64(result, "Count")
if err != nil {
return err
}
enumProperty, err := result.GetProperty("_NewEnum")
if err != nil {
return err
}
defer enumProperty.Clear()
enum, err := enumProperty.ToIUnknown().IEnumVARIANT(ole.IID_IEnumVariant)
if err != nil {
return err
}
if enum == nil {
return fmt.Errorf("can't get IEnumVARIANT, enum is nil")
}
defer enum.Release()
// Initialize a slice with Count capacity
dv.Set(reflect.MakeSlice(dv.Type(), 0, int(count)))
var errFieldMismatch error
for itemRaw, length, err := enum.Next(1); length > 0; itemRaw, length, err = enum.Next(1) {
if err != nil {
return err
}
err := func() error {
// item is a SWbemObject, but really a Win32_Process
item := itemRaw.ToIDispatch()
defer item.Release()
ev := reflect.New(elemType)
if err = c.loadEntity(ev.Interface(), item); err != nil {
if _, ok := err.(*ErrFieldMismatch); ok {
// We continue loading entities even in the face of field mismatch errors.
// If we encounter any other error, that other error is returned. Otherwise,
// an ErrFieldMismatch is returned.
errFieldMismatch = err
} else {
return err
}
}
if mat != multiArgTypeStructPtr {
ev = ev.Elem()
}
dv.Set(reflect.Append(dv, ev))
return nil
}()
if err != nil {
return err
}
}
return errFieldMismatch
}
// ErrFieldMismatch is returned when a field is to be loaded into a different
// type than the one it was stored from, or when a field is missing or
// unexported in the destination struct.
// StructType is the type of the struct pointed to by the destination argument.
type ErrFieldMismatch struct {
StructType reflect.Type
FieldName string
Reason string
}
func (e *ErrFieldMismatch) Error() string {
return fmt.Sprintf("wmi: cannot load field %q into a %q: %s",
e.FieldName, e.StructType, e.Reason)
}
var timeType = reflect.TypeOf(time.Time{})
// loadEntity loads a SWbemObject into a struct pointer.
func (c *Client) loadEntity(dst interface{}, src *ole.IDispatch) (errFieldMismatch error) {
v := reflect.ValueOf(dst).Elem()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
of := f
isPtr := f.Kind() == reflect.Ptr
if isPtr {
ptr := reflect.New(f.Type().Elem())
f.Set(ptr)
f = f.Elem()
}
n := v.Type().Field(i).Name
if !f.CanSet() {
return &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: "CanSet() is false",
}
}
prop, err := oleutil.GetProperty(src, n)
if err != nil {
if !c.AllowMissingFields {
errFieldMismatch = &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: "no such struct field",
}
}
continue
}
defer prop.Clear()
if prop.Value() == nil {
continue
}
switch val := prop.Value().(type) {
case int8, int16, int32, int64, int:
v := reflect.ValueOf(val).Int()
switch f.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f.SetInt(v)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f.SetUint(uint64(v))
default:
return &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: "not an integer class",
}
}
case uint8, uint16, uint32, uint64:
v := reflect.ValueOf(val).Uint()
switch f.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f.SetInt(int64(v))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f.SetUint(v)
default:
return &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: "not an integer class",
}
}
case string:
switch f.Kind() {
case reflect.String:
f.SetString(val)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
iv, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
f.SetInt(iv)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uv, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return err
}
f.SetUint(uv)
case reflect.Struct:
switch f.Type() {
case timeType:
if len(val) == 25 {
mins, err := strconv.Atoi(val[22:])
if err != nil {
return err
}
val = val[:22] + fmt.Sprintf("%02d%02d", mins/60, mins%60)
}
t, err := time.Parse("20060102150405.000000-0700", val)
if err != nil {
return err
}
f.Set(reflect.ValueOf(t))
}
}
case bool:
switch f.Kind() {
case reflect.Bool:
f.SetBool(val)
default:
return &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: "not a bool",
}
}
case float32:
switch f.Kind() {
case reflect.Float32:
f.SetFloat(float64(val))
default:
return &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: "not a Float32",
}
}
default:
if f.Kind() == reflect.Slice {
switch f.Type().Elem().Kind() {
case reflect.String:
safeArray := prop.ToArray()
if safeArray != nil {
arr := safeArray.ToValueArray()
fArr := reflect.MakeSlice(f.Type(), len(arr), len(arr))
for i, v := range arr {
s := fArr.Index(i)
s.SetString(v.(string))
}
f.Set(fArr)
}
case reflect.Uint8:
safeArray := prop.ToArray()
if safeArray != nil {
arr := safeArray.ToValueArray()
fArr := reflect.MakeSlice(f.Type(), len(arr), len(arr))
for i, v := range arr {
s := fArr.Index(i)
s.SetUint(reflect.ValueOf(v).Uint())
}
f.Set(fArr)
}
default:
return &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: fmt.Sprintf("unsupported slice type (%T)", val),
}
}
} else {
typeof := reflect.TypeOf(val)
if typeof == nil && (isPtr || c.NonePtrZero) {
if (isPtr && c.PtrNil) || (!isPtr && c.NonePtrZero) {
of.Set(reflect.Zero(of.Type()))
}
break
}
return &ErrFieldMismatch{
StructType: of.Type(),
FieldName: n,
Reason: fmt.Sprintf("unsupported type (%T)", val),
}
}
}
}
return errFieldMismatch
}
type multiArgType int
const (
multiArgTypeInvalid multiArgType = iota
multiArgTypeStruct
multiArgTypeStructPtr
)
// checkMultiArg checks that v has type []S, []*S for some struct type S.
//
// It returns what category the slice's elements are, and the reflect.Type
// that represents S.
func checkMultiArg(v reflect.Value) (m multiArgType, elemType reflect.Type) {
if v.Kind() != reflect.Slice {
return multiArgTypeInvalid, nil
}
elemType = v.Type().Elem()
switch elemType.Kind() {
case reflect.Struct:
return multiArgTypeStruct, elemType
case reflect.Ptr:
elemType = elemType.Elem()
if elemType.Kind() == reflect.Struct {
return multiArgTypeStructPtr, elemType
}
}
return multiArgTypeInvalid, nil
}
func oleInt64(item *ole.IDispatch, prop string) (int64, error) {
v, err := oleutil.GetProperty(item, prop)
if err != nil {
return 0, err
}
defer v.Clear()
i := int64(v.Val)
return i, nil
}
// CreateQuery returns a WQL query string that queries all columns of src. where
// is an optional string that is appended to the query, to be used with WHERE
// clauses. In such a case, the "WHERE" string should appear at the beginning.
func CreateQuery(src interface{}, where string) string {
var b bytes.Buffer
b.WriteString("SELECT ")
s := reflect.Indirect(reflect.ValueOf(src))
t := s.Type()
if s.Kind() == reflect.Slice {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return ""
}
var fields []string
for i := 0; i < t.NumField(); i++ {
fields = append(fields, t.Field(i).Name)
}
b.WriteString(strings.Join(fields, ", "))
b.WriteString(" FROM ")
b.WriteString(t.Name())
b.WriteString(" " + where)
return b.String()
}
......@@ -16,48 +16,57 @@ As of now this logs support console, file,smtp and conn.
First you must import it
import (
"github.com/astaxie/beego/logs"
)
```golang
import (
"github.com/astaxie/beego/logs"
)
```
Then init a Log (example with console adapter)
log := NewLogger(10000)
log.SetLogger("console", "")
```golang
log := logs.NewLogger(10000)
log.SetLogger("console", "")
```
> the first params stand for how many channel
Use it like this:
log.Trace("trace")
log.Info("info")
log.Warn("warning")
log.Debug("debug")
log.Critical("critical")
Use it like this:
```golang
log.Trace("trace")
log.Info("info")
log.Warn("warning")
log.Debug("debug")
log.Critical("critical")
```
## File adapter
Configure file adapter like this:
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log"}`)
```golang
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log"}`)
```
## Conn adapter
Configure like this:
log := NewLogger(1000)
log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`)
log.Info("info")
```golang
log := NewLogger(1000)
log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`)
log.Info("info")
```
## Smtp adapter
Configure like this:
log := NewLogger(10000)
log.SetLogger("smtp", `{"username":"beegotest@gmail.com","password":"xxxxxxxx","host":"smtp.gmail.com:587","sendTos":["xiemengjun@gmail.com"]}`)
log.Critical("sendmail critical")
time.Sleep(time.Second * 30)
```golang
log := NewLogger(10000)
log.SetLogger("smtp", `{"username":"beegotest@gmail.com","password":"xxxxxxxx","host":"smtp.gmail.com:587","sendTos":["xiemengjun@gmail.com"]}`)
log.Critical("sendmail critical")
time.Sleep(time.Second * 30)
```
......@@ -16,13 +16,14 @@ package logs
import (
"bytes"
"strings"
"encoding/json"
"time"
"fmt"
"time"
)
const (
apacheFormatPattern = "%s - - [%s] \"%s %d %d\" %f %s %s\n"
apacheFormatPattern = "%s - - [%s] \"%s %d %d\" %f %s %s"
apacheFormat = "APACHE_FORMAT"
jsonFormat = "JSON_FORMAT"
)
......@@ -53,10 +54,9 @@ func (r *AccessLogRecord) json() ([]byte, error) {
}
func disableEscapeHTML(i interface{}) {
e, ok := i.(interface {
if e, ok := i.(interface {
SetEscapeHTML(bool)
});
if ok {
}); ok {
e.SetEscapeHTML(false)
}
}
......@@ -64,9 +64,7 @@ func disableEscapeHTML(i interface{}) {
// AccessLog - Format and print access log.
func AccessLog(r *AccessLogRecord, format string) {
var msg string
switch format {
case apacheFormat:
timeFormatted := r.RequestTime.Format("02/Jan/2006 03:04:05")
msg = fmt.Sprintf(apacheFormatPattern, r.RemoteAddr, timeFormatted, r.Request, r.Status, r.BodyBytesSent,
......@@ -81,6 +79,5 @@ func AccessLog(r *AccessLogRecord, format string) {
msg = string(jsonData)
}
}
beeLogger.Debug(msg)
beeLogger.writeMsg(levelLoggerImpl, strings.TrimSpace(msg))
}
......@@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strconv"
"strings"
......@@ -40,6 +41,9 @@ type fileLogWriter struct {
MaxLines int `json:"maxlines"`
maxLinesCurLines int
MaxFiles int `json:"maxfiles"`
MaxFilesCurFiles int
// Rotate at size
MaxSize int `json:"maxsize"`
maxSizeCurSize int
......@@ -50,6 +54,12 @@ type fileLogWriter struct {
dailyOpenDate int
dailyOpenTime time.Time
// Rotate hourly
Hourly bool `json:"hourly"`
MaxHours int64 `json:"maxhours"`
hourlyOpenDate int
hourlyOpenTime time.Time
Rotate bool `json:"rotate"`
Level int `json:"level"`
......@@ -66,25 +76,30 @@ func newFileWriter() Logger {
w := &fileLogWriter{
Daily: true,
MaxDays: 7,
Hourly: false,
MaxHours: 168,
Rotate: true,
RotatePerm: "0440",
Level: LevelTrace,
Perm: "0660",
MaxLines: 10000000,
MaxFiles: 999,
MaxSize: 1 << 28,
}
return w
}
// Init file logger with json config.
// jsonConfig like:
// {
// "filename":"logs/beego.log",
// "maxLines":10000,
// "maxsize":1024,
// "daily":true,
// "maxDays":15,
// "rotate":true,
// "perm":"0600"
// }
// {
// "filename":"logs/beego.log",
// "maxLines":10000,
// "maxsize":1024,
// "daily":true,
// "maxDays":15,
// "rotate":true,
// "perm":"0600"
// }
func (w *fileLogWriter) Init(jsonConfig string) error {
err := json.Unmarshal([]byte(jsonConfig), w)
if err != nil {
......@@ -115,10 +130,16 @@ func (w *fileLogWriter) startLogger() error {
return w.initFd()
}
func (w *fileLogWriter) needRotate(size int, day int) bool {
func (w *fileLogWriter) needRotateDaily(size int, day int) bool {
return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) ||
(w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) ||
(w.Daily && day != w.dailyOpenDate)
}
func (w *fileLogWriter) needRotateHourly(size int, hour int) bool {
return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) ||
(w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) ||
(w.Hourly && hour != w.hourlyOpenDate)
}
......@@ -127,14 +148,23 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > w.Level {
return nil
}
h, d := formatTimeHeader(when)
msg = string(h) + msg + "\n"
hd, d, h := formatTimeHeader(when)
msg = string(hd) + msg + "\n"
if w.Rotate {
w.RLock()
if w.needRotate(len(msg), d) {
if w.needRotateHourly(len(msg), h) {
w.RUnlock()
w.Lock()
if w.needRotateHourly(len(msg), h) {
if err := w.doRotate(when); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
}
}
w.Unlock()
} else if w.needRotateDaily(len(msg), d) {
w.RUnlock()
w.Lock()
if w.needRotate(len(msg), d) {
if w.needRotateDaily(len(msg), d) {
if err := w.doRotate(when); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
}
......@@ -161,6 +191,10 @@ func (w *fileLogWriter) createLogFile() (*os.File, error) {
if err != nil {
return nil, err
}
filepath := path.Dir(w.Filename)
os.MkdirAll(filepath, os.FileMode(perm))
fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm))
if err == nil {
// Make sure file perm is user set perm cause of `os.OpenFile` will obey umask
......@@ -178,8 +212,12 @@ func (w *fileLogWriter) initFd() error {
w.maxSizeCurSize = int(fInfo.Size())
w.dailyOpenTime = time.Now()
w.dailyOpenDate = w.dailyOpenTime.Day()
w.hourlyOpenTime = time.Now()
w.hourlyOpenDate = w.hourlyOpenTime.Hour()
w.maxLinesCurLines = 0
if w.Daily {
if w.Hourly {
go w.hourlyRotate(w.hourlyOpenTime)
} else if w.Daily {
go w.dailyRotate(w.dailyOpenTime)
}
if fInfo.Size() > 0 && w.MaxLines > 0 {
......@@ -198,7 +236,22 @@ func (w *fileLogWriter) dailyRotate(openTime time.Time) {
tm := time.NewTimer(time.Duration(nextDay.UnixNano() - openTime.UnixNano() + 100))
<-tm.C
w.Lock()
if w.needRotate(0, time.Now().Day()) {
if w.needRotateDaily(0, time.Now().Day()) {
if err := w.doRotate(time.Now()); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
}
}
w.Unlock()
}
func (w *fileLogWriter) hourlyRotate(openTime time.Time) {
y, m, d := openTime.Add(1 * time.Hour).Date()
h, _, _ := openTime.Add(1 * time.Hour).Clock()
nextHour := time.Date(y, m, d, h, 0, 0, 0, openTime.Location())
tm := time.NewTimer(time.Duration(nextHour.UnixNano() - openTime.UnixNano() + 100))
<-tm.C
w.Lock()
if w.needRotateHourly(0, time.Now().Hour()) {
if err := w.doRotate(time.Now()); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
}
......@@ -238,8 +291,10 @@ func (w *fileLogWriter) lines() (int, error) {
func (w *fileLogWriter) doRotate(logTime time.Time) error {
// file exists
// Find the next available number
num := 1
num := w.MaxFilesCurFiles + 1
fName := ""
format := ""
var openTime time.Time
rotatePerm, err := strconv.ParseInt(w.RotatePerm, 8, 64)
if err != nil {
return err
......@@ -251,19 +306,26 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error {
goto RESTART_LOGGER
}
if w.Hourly {
format = "2006010215"
openTime = w.hourlyOpenTime
} else if w.Daily {
format = "2006-01-02"
openTime = w.dailyOpenTime
}
// only when one of them be setted, then the file would be splited
if w.MaxLines > 0 || w.MaxSize > 0 {
for ; err == nil && num <= 999; num++ {
fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, w.suffix)
for ; err == nil && num <= w.MaxFiles; num++ {
fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format(format), num, w.suffix)
_, err = os.Lstat(fName)
}
} else {
fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, w.dailyOpenTime.Format("2006-01-02"), w.suffix)
fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", openTime.Format(format), num, w.suffix)
_, err = os.Lstat(fName)
for ; err == nil && num <= 999; num++ {
fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", w.dailyOpenTime.Format("2006-01-02"), num, w.suffix)
_, err = os.Lstat(fName)
}
w.MaxFilesCurFiles = num
}
// return error if the last file checked still existed
if err == nil {
return fmt.Errorf("Rotate: Cannot find free log number to rename %s", w.Filename)
......@@ -307,13 +369,21 @@ func (w *fileLogWriter) deleteOldLog() {
if info == nil {
return
}
if !info.IsDir() && info.ModTime().Add(24*time.Hour*time.Duration(w.MaxDays)).Before(time.Now()) {
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) &&
strings.HasSuffix(filepath.Base(path), w.suffix) {
os.Remove(path)
}
}
if w.Hourly {
if !info.IsDir() && info.ModTime().Add(1 * time.Hour * time.Duration(w.MaxHours)).Before(time.Now()) {
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) &&
strings.HasSuffix(filepath.Base(path), w.suffix) {
os.Remove(path)
}
}
} else if w.Daily {
if !info.IsDir() && info.ModTime().Add(24 * time.Hour * time.Duration(w.MaxDays)).Before(time.Now()) {
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) &&
strings.HasSuffix(filepath.Base(path), w.suffix) {
os.Remove(path)
}
}
}
return
})
}
......
......@@ -47,7 +47,7 @@ import (
// RFC5424 log message levels.
const (
LevelEmergency = iota
LevelEmergency = iota
LevelAlert
LevelCritical
LevelError
......@@ -116,6 +116,7 @@ type BeeLogger struct {
enableFuncCallDepth bool
loggerFuncCallDepth int
asynchronous bool
prefix string
msgChanLen int64
msgChan chan *logMsg
signalChan chan string
......@@ -247,7 +248,7 @@ func (bl *BeeLogger) Write(p []byte) (n int, err error) {
}
// writeMsg will always add a '\n' character
if p[len(p)-1] == '\n' {
p = p[0 : len(p)-1]
p = p[0: len(p)-1]
}
// set levelLoggerImpl to ensure all log message will be write out
err = bl.writeMsg(levelLoggerImpl, string(p))
......@@ -267,6 +268,9 @@ func (bl *BeeLogger) writeMsg(logLevel int, msg string, v ...interface{}) error
if len(v) > 0 {
msg = fmt.Sprintf(msg, v...)
}
msg = bl.prefix + " " + msg
when := time.Now()
if bl.enableFuncCallDepth {
_, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth)
......@@ -305,6 +309,11 @@ func (bl *BeeLogger) SetLevel(l int) {
bl.level = l
}
// GetLevel Get Current log message level.
func (bl *BeeLogger) GetLevel() int {
return bl.level
}
// SetLogFuncCallDepth set log funcCallDepth
func (bl *BeeLogger) SetLogFuncCallDepth(d int) {
bl.loggerFuncCallDepth = d
......@@ -320,6 +329,11 @@ func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
bl.enableFuncCallDepth = b
}
// set prefix
func (bl *BeeLogger) SetPrefix(s string) {
bl.prefix = s
}
// start logger chan reading.
// when chan is not empty, write logs.
func (bl *BeeLogger) startLogger() {
......@@ -544,6 +558,11 @@ func SetLevel(l int) {
beeLogger.SetLevel(l)
}
// SetPrefix sets the prefix
func SetPrefix(s string) {
beeLogger.SetPrefix(s)
}
// EnableFuncCallDepth enable log funcCallDepth
func EnableFuncCallDepth(b bool) {
beeLogger.enableFuncCallDepth = b
......
......@@ -33,7 +33,7 @@ func newLogWriter(wr io.Writer) *logWriter {
func (lg *logWriter) println(when time.Time, msg string) {
lg.Lock()
h, _ := formatTimeHeader(when)
h, _, _:= formatTimeHeader(when)
lg.writer.Write(append(append(h, msg...), '\n'))
lg.Unlock()
}
......@@ -90,10 +90,10 @@ const (
ns1 = `0123456789`
)
func formatTimeHeader(when time.Time) ([]byte, int) {
func formatTimeHeader(when time.Time) ([]byte, int, int) {
y, mo, d := when.Date()
h, mi, s := when.Clock()
ns := when.Nanosecond()/1000000
ns := when.Nanosecond() / 1000000
//len("2006/01/02 15:04:05.123 ")==24
var buf [24]byte
......@@ -123,7 +123,7 @@ func formatTimeHeader(when time.Time) ([]byte, int) {
buf[23] = ' '
return buf[0:], d
return buf[0:], d, h
}
var (
......
......@@ -67,7 +67,10 @@ func (f *multiFileLogWriter) Init(config string) error {
jsonMap["level"] = i
bs, _ := json.Marshal(jsonMap)
writer = newFileWriter().(*fileLogWriter)
writer.Init(string(bs))
err := writer.Init(string(bs))
if err != nil {
return err
}
f.writers[i] = writer
}
}
......
# This file lists authors for copyright purposes. This file is distinct from
# the CONTRIBUTORS files. See the latter for an explanation.
#
# Names should be added to this file as:
# Name or Organization <email address>
#
# The email address is not required for organizations.
#
# Please keep the list sorted.
CZ.NIC z.s.p.o. <kontakt@nic.cz>
Edward Betts <edward@4angle.com>
Jan Mercl <0xjnml@gmail.com>
# This file lists people who contributed code to this repository. The AUTHORS
# file lists the copyright holders; this file lists people.
#
# Names should be added to this file like so:
# Name <email address>
#
# Please keep the list sorted.
Bodecker DellaMaria <bojdell@gmail.com>
Edward Betts <edward@4angle.com>
Faiz Abbasi <faizamodo@gmail.com>
Gary Burd <gary@beagledreams.com>
Jan Mercl <0xjnml@gmail.com>
Muhammad Surya <surya.asriadie@gmail.com>
# Copyright (c) 2016 The mathutil Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
.PHONY: all clean cover cpu editor internalError later mem nuke todo edit
grep=--include=*.go --include=*.l --include=*.y --include=*.yy
ngrep='TODOOK\|parser\.go\|scanner\.go\|.*_string\.go'
all: editor
go vet 2>&1 | grep -v $(ngrep) || true
golint 2>&1 | grep -v $(ngrep) || true
make todo
unused . || true
misspell *.go
gosimple || true
unconvert || true
maligned || true
clean:
go clean
rm -f *~ *.test *.out
cover:
t=$(shell tempfile) ; go test -coverprofile $$t && go tool cover -html $$t && unlink $$t
cpu: clean
go test -run @ -bench . -cpuprofile cpu.out
go tool pprof -lines *.test cpu.out
edit:
@ 1>/dev/null 2>/dev/null gvim -p Makefile *.go
editor:
gofmt -l -s -w *.go
go test
go build
internalError:
egrep -ho '"internal error.*"' *.go | sort | cat -n
later:
@grep -n $(grep) LATER * || true
@grep -n $(grep) MAYBE * || true
mem: clean
go test -run @ -bench . -memprofile mem.out -memprofilerate 1 -timeout 24h
go tool pprof -lines -web -alloc_space *.test mem.out
nuke: clean
go clean -i
todo:
@grep -nr $(grep) ^[[:space:]]*_[[:space:]]*=[[:space:]][[:alpha:]][[:alnum:]]* * | grep -v $(ngrep) || true
@grep -nr $(grep) TODO * | grep -v $(ngrep) || true
@grep -nr $(grep) BUG * | grep -v $(ngrep) || true
@grep -nr $(grep) [^[:alpha:]]println * | grep -v $(ngrep) || true
This is a goinstall-able mirror of modified code already published at:
http://git.nic.cz/redmine/projects/gornd/repository
Packages in this repository:
Install: $ go get github.com/cznic/mathutil
Godocs: http://godoc.org/github.com/cznic/mathutil
Install: $ go get github.com/cznic/mathutil/mersenne
Godocs: http://godoc.org/github.com/cznic/mathutil/mersenne
// Copyright (c) 2016 The mathutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mathutil
import (
"math/big"
"github.com/remyoudompheng/bigfft"
)
type float struct {
n *big.Int
fracBits int
maxFracBits int
}
func newFloat(n *big.Int, fracBits, maxFracBits int) float {
f := float{n: n, fracBits: fracBits, maxFracBits: maxFracBits}
f.normalize()
return f
}
func (f *float) normalize() {
n := f.n.BitLen()
if n == 0 {
return
}
if n := f.fracBits - f.maxFracBits; n > 0 {
bit := f.n.Bit(n - 1)
f.n.Rsh(f.n, uint(n))
if bit != 0 {
f.n.Add(f.n, _1)
}
f.fracBits -= n
}
var i int
for ; f.fracBits > 0 && i <= f.fracBits && f.n.Bit(i) == 0; i++ {
f.fracBits--
}
if i != 0 {
f.n.Rsh(f.n, uint(i))
}
}
func (f *float) eq1() bool { return f.fracBits == 0 && f.n.BitLen() == 1 }
func (f *float) ge2() bool { return f.n.BitLen() > f.fracBits+1 }
func (f *float) div2() {
f.fracBits++
f.normalize()
}
func (f *float) sqr() {
f.n = bigfft.Mul(f.n, f.n)
f.fracBits *= 2
f.normalize()
}
// BinaryLog computes the binary logarithm of n. The result consists of a
// characteristic and a mantissa having precision mantissaBits. The value of
// the binary logarithm is
//
// characteristic + mantissa*(2^-mantissaBits)
//
// BinaryLog panics for n <= 0 or mantissaBits < 0.
func BinaryLog(n *big.Int, mantissaBits int) (characteristic int, mantissa *big.Int) {
if n.Sign() <= 0 || mantissaBits < 0 {
panic("invalid argument of BinaryLog")
}
characteristic = n.BitLen() - 1
mantissa = big.NewInt(0)
x := newFloat(n, characteristic, mantissaBits)
for ; mantissaBits != 0 && !x.eq1(); mantissaBits-- {
x.sqr()
mantissa.Lsh(mantissa, 1)
if x.ge2() {
mantissa.SetBit(mantissa, 0, 1)
x.div2()
}
}
return characteristic, mantissa
}
......@@ -149,7 +149,7 @@ func BitLenUintptr(n uintptr) int {
// PopCountByte returns population count of n (number of bits set in n).
func PopCountByte(n byte) int {
return int(popcnt[byte(n)])
return int(popcnt[n])
}
// PopCountUint16 returns population count of n (number of bits set in n).
......
// Copyright (c) 2018 The mathutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mathutil
import (
"fmt"
"math"
"math/big"
)
var (
// The maximun Int128 value.
MaxInt128 *big.Int
// The minimun Int128 value.
MinInt128 *big.Int
)
func init() {
MaxInt128 = big.NewInt(0)
MaxInt128.SetBit(MaxInt128, 127, 1)
MaxInt128.Sub(MaxInt128, _1)
MinInt128 = big.NewInt(0)
MinInt128.Set(MaxInt128)
MinInt128.Add(MinInt128, _1)
MinInt128.Neg(MinInt128)
}
// Int128 is an 128 bit integer.
type Int128 struct {
Lo int64 // Bits 63..0.
Hi int64 // Bits 127..64.
}
// Add returns the sum of x and y and a carry indication.
func (x Int128) Add(y Int128) (r Int128, cy bool) {
r.Lo = x.Lo + y.Lo
r.Hi = x.Hi + y.Hi
if uint64(r.Lo) < uint64(x.Lo) {
r.Hi++
}
return r, (r.Cmp(x) < 0) == (y.Sign() >= 0)
}
// BigInt returns x in the form of a big.Int.
func (x Int128) BigInt() *big.Int {
r := big.NewInt(x.Hi)
r.Lsh(r, 64)
lo := big.NewInt(0)
lo.SetUint64(uint64(x.Lo))
return r.Add(r, lo)
}
// Cmp compares x and y and returns:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
func (x Int128) Cmp(y Int128) int {
if x.Hi > y.Hi {
return 1
}
if x.Hi < y.Hi {
return -1
}
if uint64(x.Lo) > uint64(y.Lo) {
return 1
}
if uint64(x.Lo) < uint64(y.Lo) {
return -1
}
return 0
}
// Neg returns -x and an indication that x was not equal to MinInt128.
func (x Int128) Neg() (r Int128, ok bool) {
if x == (Int128{Hi: math.MinInt64}) {
return x, false
}
x.Lo = ^x.Lo
x.Hi = ^x.Hi
r, _ = x.Add(Int128{Lo: 1})
return r, true
}
// SetBigInt sets x to y, returns x and an error, if any.
func (x *Int128) SetBigInt(y *big.Int) (r Int128, err error) {
if y.Cmp(MaxInt128) > 0 {
return *x, fmt.Errorf("%T.SetInt: overflow", x)
}
if y.Cmp(MinInt128) < 0 {
return *x, fmt.Errorf("%T.SetInt: underflow", x)
}
neg := y.Sign() < 0
var z big.Int
z.Set(y)
if neg {
z.Neg(&z)
}
r.Lo = z.Int64()
z.Rsh(&z, 64)
r.Hi = z.Int64()
if neg {
r, _ = r.Neg()
}
*x = r
return r, nil
}
// SetInt64 sets x to y and returns x.
func (x *Int128) SetInt64(y int64) (r Int128) {
r.Lo = y
if y >= 0 {
r.Hi = 0
*x = r
return r
}
r.Hi = -1
*x = r
return r
}
// SetInt64 sets x to y and returns x.
func (x *Int128) SetUint64(y uint64) (r Int128) {
r = Int128{Lo: int64(y)}
*x = r
return r
}
// Sign returns:
//
// -1 if x < 0
// 0 if x == 0
// +1 if x > 0
func (x Int128) Sign() int {
if x.Hi < 0 {
return -1
}
if x.Hi != 0 || x.Lo != 0 {
return 1
}
return 0
}
// String implements fmt.Stringer()
func (x Int128) String() string { return x.BigInt().String() }
......@@ -5,7 +5,25 @@
// Package mathutil provides utilities supplementing the standard 'math' and
// 'math/rand' packages.
//
// Compatibility issues
// Release history and compatibility issues
//
// 2018-10-21 Added BinaryLog
//
// 2018-04-25: New functions for determinig Max/Min of nullable values. Ex:
// func MaxPtr(a, b *int) *int {
// func MinPtr(a, b *int) *int {
// func MaxBytePtr(a, b *byte) *byte {
// func MinBytePtr(a, b *byte) *byte {
// ...
//
// 2017-10-14: New variadic functions for Max/Min. Ex:
// func MaxVal(val int, vals ...int) int {
// func MinVal(val int, vals ...int) int {
// func MaxByteVal(val byte, vals ...byte) byte {
// func MinByteVal(val byte, vals ...byte) byte {
// ...
//
// 2016-10-10: New functions QuadPolyDiscriminant and QuadPolyFactors.
//
// 2013-12-13: The following functions have been REMOVED
//
......@@ -68,8 +86,9 @@ const (
)
var (
_1 = big.NewInt(1)
_2 = big.NewInt(2)
_m1 = big.NewInt(-1)
_1 = big.NewInt(1)
_2 = big.NewInt(2)
)
// GCDByte returns the greatest common divisor of a and b. Based on:
......@@ -89,7 +108,7 @@ func GCDUint16(a, b uint16) uint16 {
return a
}
// GCD returns the greatest common divisor of a and b.
// GCDUint32 returns the greatest common divisor of a and b.
func GCDUint32(a, b uint32) uint32 {
for b != 0 {
a, b = b, a%b
......@@ -97,7 +116,7 @@ func GCDUint32(a, b uint32) uint32 {
return a
}
// GCD64 returns the greatest common divisor of a and b.
// GCDUint64 returns the greatest common divisor of a and b.
func GCDUint64(a, b uint64) uint64 {
for b != 0 {
a, b = b, a%b
......@@ -257,7 +276,7 @@ func ModPowByte(b, e, m byte) byte {
return byte(r)
}
// ModPowByte computes (b^e)%m. It panics for m == 0 || b == e == 0.
// ModPowUint16 computes (b^e)%m. It panics for m == 0 || b == e == 0.
func ModPowUint16(b, e, m uint16) uint16 {
if b == 0 && e == 0 {
panic(0)
......@@ -360,7 +379,7 @@ func AddUint128_64(a, b uint64) (hi uint64, lo uint64) {
if lo < a {
hi = 1
}
return
return hi, lo
}
// MulUint128_64 returns the uint128 bit product of uint64 a and b.
......@@ -382,7 +401,7 @@ func MulUint128_64(a, b uint64) (hi, lo uint64) {
mid2 := ahi * blo
c1, lo := AddUint128_64(lo, mid1<<w)
c2, lo := AddUint128_64(lo, mid2<<w)
_, hi = AddUint128_64(ahi*bhi, mid1>>w+mid2>>w+uint64(c1+c2))
_, hi = AddUint128_64(ahi*bhi, mid1>>w+mid2>>w+c1+c2)
return
}
......@@ -629,6 +648,63 @@ func Min(a, b int) int {
return b
}
// MaxPtr returns a pointer to the larger of a and b, or nil.
func MaxPtr(a, b *int) *int {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinPtr returns a pointer to the smaller of a and b, or nil.
func MinPtr(a, b *int) *int {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxVal returns the largest argument passed.
func MaxVal(val int, vals ...int) int {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinVal returns the smallest argument passed.
func MinVal(val int, vals ...int) int {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// Clamp returns a value restricted between lo and hi.
func Clamp(v, lo, hi int) int {
return Min(Max(v, lo), hi)
}
// UMax returns the larger of a and b.
func UMax(a, b uint) uint {
if a > b {
......@@ -647,6 +723,63 @@ func UMin(a, b uint) uint {
return b
}
// UMaxPtr returns a pointer to the larger of a and b, or nil.
func UMaxPtr(a, b *uint) *uint {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// UMinPtr returns a pointer to the smaller of a and b, or nil.
func UMinPtr(a, b *uint) *uint {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// UMaxVal returns the largest argument passed.
func UMaxVal(val uint, vals ...uint) uint {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// UMinVal returns the smallest argument passed.
func UMinVal(val uint, vals ...uint) uint {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// UClamp returns a value restricted between lo and hi.
func UClamp(v, lo, hi uint) uint {
return UMin(UMax(v, lo), hi)
}
// MaxByte returns the larger of a and b.
func MaxByte(a, b byte) byte {
if a > b {
......@@ -665,6 +798,63 @@ func MinByte(a, b byte) byte {
return b
}
// MaxBytePtr returns a pointer to the larger of a and b, or nil.
func MaxBytePtr(a, b *byte) *byte {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinBytePtr returns a pointer to the smaller of a and b, or nil.
func MinBytePtr(a, b *byte) *byte {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxByteVal returns the largest argument passed.
func MaxByteVal(val byte, vals ...byte) byte {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinByteVal returns the smallest argument passed.
func MinByteVal(val byte, vals ...byte) byte {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampByte returns a value restricted between lo and hi.
func ClampByte(v, lo, hi byte) byte {
return MinByte(MaxByte(v, lo), hi)
}
// MaxInt8 returns the larger of a and b.
func MaxInt8(a, b int8) int8 {
if a > b {
......@@ -683,6 +873,63 @@ func MinInt8(a, b int8) int8 {
return b
}
// MaxInt8Ptr returns a pointer to the larger of a and b, or nil.
func MaxInt8Ptr(a, b *int8) *int8 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinInt8Ptr returns a pointer to the smaller of a and b, or nil.
func MinInt8Ptr(a, b *int8) *int8 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxInt8Val returns the largest argument passed.
func MaxInt8Val(val int8, vals ...int8) int8 {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinInt8Val returns the smallest argument passed.
func MinInt8Val(val int8, vals ...int8) int8 {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampInt8 returns a value restricted between lo and hi.
func ClampInt8(v, lo, hi int8) int8 {
return MinInt8(MaxInt8(v, lo), hi)
}
// MaxUint16 returns the larger of a and b.
func MaxUint16(a, b uint16) uint16 {
if a > b {
......@@ -701,6 +948,63 @@ func MinUint16(a, b uint16) uint16 {
return b
}
// MaxUint16Ptr returns a pointer to the larger of a and b, or nil.
func MaxUint16Ptr(a, b *uint16) *uint16 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinUint16Ptr returns a pointer to the smaller of a and b, or nil.
func MinUint16Ptr(a, b *uint16) *uint16 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxUint16Val returns the largest argument passed.
func MaxUint16Val(val uint16, vals ...uint16) uint16 {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinUint16Val returns the smallest argument passed.
func MinUint16Val(val uint16, vals ...uint16) uint16 {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampUint16 returns a value restricted between lo and hi.
func ClampUint16(v, lo, hi uint16) uint16 {
return MinUint16(MaxUint16(v, lo), hi)
}
// MaxInt16 returns the larger of a and b.
func MaxInt16(a, b int16) int16 {
if a > b {
......@@ -719,6 +1023,63 @@ func MinInt16(a, b int16) int16 {
return b
}
// MaxInt16Ptr returns a pointer to the larger of a and b, or nil.
func MaxInt16Ptr(a, b *int16) *int16 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinInt16Ptr returns a pointer to the smaller of a and b, or nil.
func MinInt16Ptr(a, b *int16) *int16 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxInt16Val returns the largest argument passed.
func MaxInt16Val(val int16, vals ...int16) int16 {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinInt16Val returns the smallest argument passed.
func MinInt16Val(val int16, vals ...int16) int16 {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampInt16 returns a value restricted between lo and hi.
func ClampInt16(v, lo, hi int16) int16 {
return MinInt16(MaxInt16(v, lo), hi)
}
// MaxUint32 returns the larger of a and b.
func MaxUint32(a, b uint32) uint32 {
if a > b {
......@@ -737,6 +1098,63 @@ func MinUint32(a, b uint32) uint32 {
return b
}
// MaxUint32Ptr returns a pointer to the larger of a and b, or nil.
func MaxUint32Ptr(a, b *uint32) *uint32 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinUint32Ptr returns a pointer to the smaller of a and b, or nil.
func MinUint32Ptr(a, b *uint32) *uint32 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxUint32Val returns the largest argument passed.
func MaxUint32Val(val uint32, vals ...uint32) uint32 {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinUint32Val returns the smallest argument passed.
func MinUint32Val(val uint32, vals ...uint32) uint32 {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampUint32 returns a value restricted between lo and hi.
func ClampUint32(v, lo, hi uint32) uint32 {
return MinUint32(MaxUint32(v, lo), hi)
}
// MaxInt32 returns the larger of a and b.
func MaxInt32(a, b int32) int32 {
if a > b {
......@@ -755,6 +1173,63 @@ func MinInt32(a, b int32) int32 {
return b
}
// MaxInt32Ptr returns a pointer to the larger of a and b, or nil.
func MaxInt32Ptr(a, b *int32) *int32 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinInt32Ptr returns a pointer to the smaller of a and b, or nil.
func MinInt32Ptr(a, b *int32) *int32 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxInt32Val returns the largest argument passed.
func MaxInt32Val(val int32, vals ...int32) int32 {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinInt32Val returns the smallest argument passed.
func MinInt32Val(val int32, vals ...int32) int32 {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampInt32 returns a value restricted between lo and hi.
func ClampInt32(v, lo, hi int32) int32 {
return MinInt32(MaxInt32(v, lo), hi)
}
// MaxUint64 returns the larger of a and b.
func MaxUint64(a, b uint64) uint64 {
if a > b {
......@@ -773,6 +1248,63 @@ func MinUint64(a, b uint64) uint64 {
return b
}
// MaxUint64Ptr returns a pointer to the larger of a and b, or nil.
func MaxUint64Ptr(a, b *uint64) *uint64 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinUint64Ptr returns a pointer to the smaller of a and b, or nil.
func MinUint64Ptr(a, b *uint64) *uint64 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxUint64Val returns the largest argument passed.
func MaxUint64Val(val uint64, vals ...uint64) uint64 {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinUint64Val returns the smallest argument passed.
func MinUint64Val(val uint64, vals ...uint64) uint64 {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampUint64 returns a value restricted between lo and hi.
func ClampUint64(v, lo, hi uint64) uint64 {
return MinUint64(MaxUint64(v, lo), hi)
}
// MaxInt64 returns the larger of a and b.
func MaxInt64(a, b int64) int64 {
if a > b {
......@@ -791,6 +1323,63 @@ func MinInt64(a, b int64) int64 {
return b
}
// MaxInt64Ptr returns a pointer to the larger of a and b, or nil.
func MaxInt64Ptr(a, b *int64) *int64 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a > *b {
return a
}
return b
}
// MinInt64Ptr returns a pointer to the smaller of a and b, or nil.
func MinInt64Ptr(a, b *int64) *int64 {
if a == nil {
return b
}
if b == nil {
return a
}
if *a < *b {
return a
}
return b
}
// MaxInt64Val returns the largest argument passed.
func MaxInt64Val(val int64, vals ...int64) int64 {
res := val
for _, v := range vals {
if v > res {
res = v
}
}
return res
}
// MinInt64Val returns the smallest argument passed.
func MinInt64Val(val int64, vals ...int64) int64 {
res := val
for _, v := range vals {
if v < res {
res = v
}
}
return res
}
// ClampInt64 returns a value restricted between lo and hi.
func ClampInt64(v, lo, hi int64) int64 {
return MinInt64(MaxInt64(v, lo), hi)
}
// ToBase produces n in base b. For example
//
// ToBase(2047, 22) -> [1, 5, 4]
......
$ ./example -max 100000000 > rnd.dat
$ ./assess 1000000
G E N E R A T O R S E L E C T I O N
______________________________________
[0] Input File [1] Linear Congruential
[2] Quadratic Congruential I [3] Quadratic Congruential II
[4] Cubic Congruential [5] XOR
[6] Modular Exponentiation [7] Blum-Blum-Shub
[8] Micali-Schnorr [9] G Using SHA-1
Enter Choice: 0
User Prescribed Input File: rnd.dat
S T A T I S T I C A L T E S T S
_________________________________
[01] Frequency [02] Block Frequency
[03] Cumulative Sums [04] Runs
[05] Longest Run of Ones [06] Rank
[07] Discrete Fourier Transform [08] Nonperiodic Template Matchings
[09] Overlapping Template Matchings [10] Universal Statistical
[11] Approximate Entropy [12] Random Excursions
[13] Random Excursions Variant [14] Serial
[15] Linear Complexity
INSTRUCTIONS
Enter 0 if you DO NOT want to apply all of the
statistical tests to each sequence and 1 if you DO.
Enter Choice: 1
P a r a m e t e r A d j u s t m e n t s
-----------------------------------------
[1] Block Frequency Test - block length(M): 128
[2] NonOverlapping Template Test - block length(m): 9
[3] Overlapping Template Test - block length(m): 9
[4] Approximate Entropy Test - block length(m): 10
[5] Serial Test - block length(m): 16
[6] Linear Complexity Test - block length(M): 500
Select Test (0 to continue): 0
How many bitstreams? 200
Input File Format:
[0] ASCII - A sequence of ASCII 0's and 1's
[1] Binary - Each byte in data file contains 8 bits of data
Select input mode: 1
Statistical Testing In Progress.........
Statistical Testing Complete!!!!!!!!!!!!
$ cat experiments/AlgorithmTesting/finalAnalysisReport.txt
------------------------------------------------------------------------------
RESULTS FOR THE UNIFORMITY OF P-VALUES AND THE PROPORTION OF PASSING SEQUENCES
------------------------------------------------------------------------------
generator is <rnd.dat>
------------------------------------------------------------------------------
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 P-VALUE PROPORTION STATISTICAL TEST
------------------------------------------------------------------------------
28 22 17 19 15 8 24 23 19 25 0.093720 198/200 Frequency
20 18 24 14 18 17 16 28 21 24 0.504219 199/200 BlockFrequency
25 22 17 24 19 21 22 15 16 19 0.825505 197/200 CumulativeSums
27 17 16 22 14 26 14 25 19 20 0.304126 199/200 CumulativeSums
22 19 14 23 22 22 13 28 13 24 0.224821 199/200 Runs
20 24 18 21 15 13 22 23 24 20 0.719747 197/200 LongestRun
22 26 18 22 26 15 17 22 20 12 0.410055 199/200 Rank
25 22 26 22 20 16 20 20 16 13 0.585209 195/200 FFT
22 11 15 26 33 24 21 13 14 21 0.013102 197/200 NonOverlappingTemplate
17 11 16 27 19 24 19 20 28 19 0.219006 200/200 NonOverlappingTemplate
23 27 24 15 21 11 18 27 15 19 0.162606 197/200 NonOverlappingTemplate
21 18 13 20 19 23 20 17 26 23 0.749884 197/200 NonOverlappingTemplate
24 22 24 24 24 21 13 15 17 16 0.494392 196/200 NonOverlappingTemplate
24 16 23 15 23 18 25 16 18 22 0.699313 199/200 NonOverlappingTemplate
19 23 21 16 27 18 17 20 18 21 0.859637 198/200 NonOverlappingTemplate
12 20 16 19 26 14 30 20 24 19 0.141256 198/200 NonOverlappingTemplate
18 21 17 21 20 14 25 19 24 21 0.859637 198/200 NonOverlappingTemplate
24 25 21 18 23 15 23 17 16 18 0.749884 199/200 NonOverlappingTemplate
20 22 22 18 16 22 28 16 14 22 0.574903 198/200 NonOverlappingTemplate
18 23 22 17 24 25 19 16 23 13 0.626709 199/200 NonOverlappingTemplate
17 22 14 19 21 21 18 19 24 25 0.842937 198/200 NonOverlappingTemplate
18 17 26 21 22 15 22 18 21 20 0.883171 197/200 NonOverlappingTemplate
19 25 16 32 15 19 20 18 16 20 0.236810 199/200 NonOverlappingTemplate
19 18 15 21 24 22 18 21 20 22 0.964295 200/200 NonOverlappingTemplate
21 14 17 23 26 19 20 22 20 18 0.834308 196/200 NonOverlappingTemplate
15 21 17 27 26 23 21 17 24 9 0.129620 198/200 NonOverlappingTemplate
25 17 19 19 18 22 21 22 21 16 0.951205 196/200 NonOverlappingTemplate
20 19 24 21 19 24 16 18 17 22 0.946308 197/200 NonOverlappingTemplate
27 16 19 18 23 19 22 17 22 17 0.807412 197/200 NonOverlappingTemplate
14 18 21 23 23 20 14 22 20 25 0.719747 198/200 NonOverlappingTemplate
18 22 19 12 24 25 25 22 18 15 0.474986 198/200 NonOverlappingTemplate
21 18 23 17 19 18 28 19 20 17 0.825505 198/200 NonOverlappingTemplate
20 19 15 16 27 20 26 17 20 20 0.657933 198/200 NonOverlappingTemplate
17 25 21 21 11 19 22 16 27 21 0.401199 198/200 NonOverlappingTemplate
19 16 15 18 24 19 25 25 19 20 0.769527 199/200 NonOverlappingTemplate
18 20 20 26 20 12 24 25 19 16 0.524101 198/200 NonOverlappingTemplate
14 16 18 23 21 21 19 19 28 21 0.668321 197/200 NonOverlappingTemplate
21 20 23 25 21 22 19 17 14 18 0.875539 197/200 NonOverlappingTemplate
14 16 29 22 23 13 20 29 17 17 0.099513 197/200 NonOverlappingTemplate
14 19 27 19 17 23 18 24 20 19 0.709558 199/200 NonOverlappingTemplate
18 15 21 19 27 22 21 23 17 17 0.779188 198/200 NonOverlappingTemplate
13 23 13 22 22 23 22 21 21 20 0.689019 199/200 NonOverlappingTemplate
17 14 26 26 16 21 30 15 21 14 0.096578 199/200 NonOverlappingTemplate
18 21 24 23 21 13 23 23 19 15 0.719747 197/200 NonOverlappingTemplate
19 21 14 32 20 15 16 18 24 21 0.202268 199/200 NonOverlappingTemplate
27 22 20 21 21 14 15 22 14 24 0.474986 196/200 NonOverlappingTemplate
31 12 25 11 21 18 19 16 24 23 0.050305 197/200 NonOverlappingTemplate
17 26 20 22 15 27 22 19 12 20 0.383827 199/200 NonOverlappingTemplate
15 22 14 14 31 15 27 18 23 21 0.078086 194/200 NonOverlappingTemplate
19 19 14 15 24 21 25 21 20 22 0.788728 197/200 NonOverlappingTemplate
20 21 19 22 25 18 13 24 28 10 0.153763 195/200 NonOverlappingTemplate
23 17 21 25 21 20 13 30 14 16 0.196920 196/200 NonOverlappingTemplate
17 31 17 22 16 15 28 23 11 20 0.050305 197/200 NonOverlappingTemplate
15 21 26 27 15 18 19 21 18 20 0.605916 198/200 NonOverlappingTemplate
23 18 15 14 20 21 20 20 20 29 0.554420 200/200 NonOverlappingTemplate
22 19 19 18 19 17 22 21 31 12 0.311542 199/200 NonOverlappingTemplate
16 22 23 21 19 19 18 24 21 17 0.960198 197/200 NonOverlappingTemplate
21 21 17 20 16 23 25 22 18 17 0.917870 200/200 NonOverlappingTemplate
27 17 17 16 21 20 22 18 21 21 0.859637 197/200 NonOverlappingTemplate
18 24 15 27 18 21 18 16 24 19 0.657933 199/200 NonOverlappingTemplate
13 16 21 21 15 25 18 22 29 20 0.326749 198/200 NonOverlappingTemplate
18 17 23 23 15 19 26 30 11 18 0.125927 198/200 NonOverlappingTemplate
30 21 18 22 17 21 15 17 21 18 0.544254 195/200 NonOverlappingTemplate
12 18 19 24 16 24 18 24 28 17 0.311542 199/200 NonOverlappingTemplate
20 15 23 15 18 30 23 18 17 21 0.410055 196/200 NonOverlappingTemplate
15 18 23 16 29 21 22 16 19 21 0.544254 200/200 NonOverlappingTemplate
18 16 27 13 21 22 22 21 16 24 0.534146 199/200 NonOverlappingTemplate
20 25 18 21 16 21 17 28 21 13 0.484646 200/200 NonOverlappingTemplate
23 22 13 22 14 20 26 18 19 23 0.574903 197/200 NonOverlappingTemplate
21 24 25 13 19 22 18 13 24 21 0.504219 199/200 NonOverlappingTemplate
19 13 18 25 22 15 23 28 19 18 0.410055 195/200 NonOverlappingTemplate
20 15 27 22 26 26 14 13 21 16 0.181557 198/200 NonOverlappingTemplate
18 18 19 23 18 20 19 21 24 20 0.991468 200/200 NonOverlappingTemplate
18 23 17 14 20 25 22 22 22 17 0.816537 198/200 NonOverlappingTemplate
26 15 15 11 23 21 21 16 36 16 0.005557 196/200 NonOverlappingTemplate
27 13 21 23 21 16 19 20 16 24 0.544254 198/200 NonOverlappingTemplate
16 15 32 17 20 23 22 19 20 16 0.262249 200/200 NonOverlappingTemplate
26 19 24 13 24 16 18 18 13 29 0.137282 199/200 NonOverlappingTemplate
15 18 14 27 32 21 15 20 19 19 0.112047 198/200 NonOverlappingTemplate
22 23 22 18 20 23 19 22 16 15 0.924076 196/200 NonOverlappingTemplate
18 17 21 22 14 17 22 24 20 25 0.798139 199/200 NonOverlappingTemplate
15 17 19 24 21 23 17 25 23 16 0.739918 196/200 NonOverlappingTemplate
22 11 15 26 32 25 21 13 14 21 0.017305 197/200 NonOverlappingTemplate
22 16 19 23 22 21 21 19 17 20 0.985788 200/200 NonOverlappingTemplate
22 28 18 24 14 20 23 21 20 10 0.230755 198/200 NonOverlappingTemplate
14 13 22 28 14 28 17 22 23 19 0.129620 197/200 NonOverlappingTemplate
22 16 22 20 21 21 16 19 18 25 0.935716 198/200 NonOverlappingTemplate
15 20 23 17 19 22 21 23 18 22 0.951205 200/200 NonOverlappingTemplate
20 24 21 19 17 19 19 24 15 22 0.930026 198/200 NonOverlappingTemplate
18 21 15 21 17 28 24 22 20 14 0.534146 200/200 NonOverlappingTemplate
19 15 19 19 20 20 15 25 23 25 0.779188 198/200 NonOverlappingTemplate
17 24 25 16 15 21 18 19 23 22 0.788728 198/200 NonOverlappingTemplate
15 20 18 25 24 15 21 31 18 13 0.141256 200/200 NonOverlappingTemplate
24 17 19 20 18 21 15 22 24 20 0.924076 196/200 NonOverlappingTemplate
23 18 17 21 17 28 23 21 18 14 0.605916 197/200 NonOverlappingTemplate
21 19 22 23 16 17 20 21 22 19 0.985788 200/200 NonOverlappingTemplate
27 17 21 27 24 15 15 17 15 22 0.304126 199/200 NonOverlappingTemplate
25 28 20 24 13 14 16 22 19 19 0.304126 197/200 NonOverlappingTemplate
27 16 14 24 22 18 24 20 18 17 0.564639 196/200 NonOverlappingTemplate
18 18 24 19 19 19 26 11 27 19 0.375313 195/200 NonOverlappingTemplate
20 15 29 19 26 16 21 11 18 25 0.141256 197/200 NonOverlappingTemplate
19 14 21 25 11 23 22 25 26 14 0.176657 199/200 NonOverlappingTemplate
18 23 20 17 19 18 29 22 26 8 0.102526 199/200 NonOverlappingTemplate
22 17 18 16 18 20 19 19 25 26 0.834308 198/200 NonOverlappingTemplate
25 18 14 16 16 24 18 18 30 21 0.268917 198/200 NonOverlappingTemplate
24 21 23 13 12 22 20 23 20 22 0.554420 196/200 NonOverlappingTemplate
18 21 21 30 22 17 19 14 18 20 0.534146 197/200 NonOverlappingTemplate
25 20 22 21 15 18 17 20 17 25 0.825505 199/200 NonOverlappingTemplate
18 21 22 21 18 20 26 16 20 18 0.941144 197/200 NonOverlappingTemplate
23 18 22 25 12 16 17 19 26 22 0.474986 198/200 NonOverlappingTemplate
22 18 29 23 19 23 17 17 15 17 0.534146 198/200 NonOverlappingTemplate
19 21 17 26 18 15 22 26 15 21 0.626709 197/200 NonOverlappingTemplate
16 20 20 23 18 21 18 18 25 21 0.955835 199/200 NonOverlappingTemplate
23 21 20 21 22 10 15 27 15 26 0.186566 198/200 NonOverlappingTemplate
18 26 20 26 26 18 17 17 20 12 0.358641 198/200 NonOverlappingTemplate
24 20 21 18 24 12 19 27 14 21 0.401199 195/200 NonOverlappingTemplate
16 25 15 21 24 18 18 25 22 16 0.657933 199/200 NonOverlappingTemplate
24 14 17 26 15 17 17 25 21 24 0.428095 200/200 NonOverlappingTemplate
22 24 11 20 22 24 19 18 12 28 0.176657 196/200 NonOverlappingTemplate
27 16 27 18 27 14 13 16 21 21 0.141256 197/200 NonOverlappingTemplate
23 25 20 18 23 17 15 23 19 17 0.834308 196/200 NonOverlappingTemplate
19 21 20 27 16 16 18 25 16 22 0.678686 199/200 NonOverlappingTemplate
25 22 21 19 15 19 22 19 25 13 0.657933 197/200 NonOverlappingTemplate
19 28 21 25 20 12 18 13 29 15 0.073417 198/200 NonOverlappingTemplate
20 24 21 19 21 15 17 24 20 19 0.941144 198/200 NonOverlappingTemplate
18 29 23 17 24 19 17 18 16 19 0.585209 200/200 NonOverlappingTemplate
18 28 18 16 25 21 18 20 14 22 0.544254 198/200 NonOverlappingTemplate
22 19 23 22 22 21 21 26 12 12 0.401199 199/200 NonOverlappingTemplate
22 15 25 16 21 27 14 22 21 17 0.484646 199/200 NonOverlappingTemplate
18 25 20 23 30 17 13 22 18 14 0.213309 200/200 NonOverlappingTemplate
20 23 21 21 23 29 16 13 16 18 0.410055 199/200 NonOverlappingTemplate
21 19 16 22 31 18 20 17 18 18 0.514124 198/200 NonOverlappingTemplate
26 22 12 14 23 17 21 24 21 20 0.455937 197/200 NonOverlappingTemplate
21 17 18 17 14 32 21 26 18 16 0.162606 197/200 NonOverlappingTemplate
22 24 22 23 11 15 17 18 29 19 0.230755 198/200 NonOverlappingTemplate
19 27 20 19 23 15 24 15 21 17 0.657933 198/200 NonOverlappingTemplate
20 25 16 10 24 13 23 21 21 27 0.149495 200/200 NonOverlappingTemplate
19 21 21 27 17 17 19 21 21 17 0.904708 200/200 NonOverlappingTemplate
18 23 15 19 24 21 23 21 13 23 0.719747 198/200 NonOverlappingTemplate
26 16 28 19 19 18 17 17 16 24 0.474986 199/200 NonOverlappingTemplate
24 32 17 18 20 13 18 18 19 21 0.236810 195/200 NonOverlappingTemplate
26 25 18 17 12 19 20 23 21 19 0.585209 196/200 NonOverlappingTemplate
18 26 25 12 18 16 24 19 18 24 0.410055 199/200 NonOverlappingTemplate
27 21 22 27 21 14 18 14 23 13 0.219006 197/200 NonOverlappingTemplate
18 23 24 16 19 21 16 26 20 17 0.798139 199/200 NonOverlappingTemplate
19 30 15 27 14 19 24 11 22 19 0.073417 198/200 NonOverlappingTemplate
20 23 22 20 22 15 22 21 18 17 0.964295 198/200 NonOverlappingTemplate
22 31 16 26 13 19 17 22 24 10 0.037566 197/200 NonOverlappingTemplate
18 24 22 14 23 19 16 18 19 27 0.637119 197/200 NonOverlappingTemplate
19 20 21 22 21 18 19 22 20 18 0.999438 198/200 NonOverlappingTemplate
27 15 21 18 28 18 15 23 18 17 0.375313 195/200 NonOverlappingTemplate
26 23 20 20 23 19 20 23 14 12 0.514124 199/200 NonOverlappingTemplate
18 19 11 15 21 24 20 26 23 23 0.428095 198/200 NonOverlappingTemplate
19 16 21 25 19 21 15 24 24 16 0.749884 197/200 NonOverlappingTemplate
17 26 23 18 20 26 23 14 18 15 0.494392 198/200 NonOverlappingTemplate
15 17 19 24 21 23 17 25 23 16 0.739918 196/200 NonOverlappingTemplate
26 19 20 20 24 22 22 13 14 20 0.605916 198/200 OverlappingTemplate
29 24 17 21 18 13 18 21 17 22 0.446556 196/200 Universal
22 18 22 20 20 21 22 21 18 16 0.992952 198/200 ApproximateEntropy
14 8 13 9 11 13 13 8 7 10 0.719747 106/106 RandomExcursions
13 18 9 7 12 12 9 6 12 8 0.236810 104/106 RandomExcursions
11 15 10 7 11 14 9 6 12 11 0.595549 106/106 RandomExcursions
15 7 12 12 9 11 16 8 10 6 0.350485 106/106 RandomExcursions
10 10 12 16 10 12 10 7 13 6 0.554420 106/106 RandomExcursions
8 7 12 10 11 16 11 13 10 8 0.657933 106/106 RandomExcursions
9 6 12 12 14 9 11 13 10 10 0.816537 104/106 RandomExcursions
10 10 7 12 11 9 10 13 14 10 0.911413 105/106 RandomExcursions
8 8 12 9 10 5 13 12 17 12 0.319084 104/106 RandomExcursionsVariant
5 11 10 11 7 11 10 15 11 15 0.455937 104/106 RandomExcursionsVariant
6 12 11 8 12 12 12 13 13 7 0.699313 104/106 RandomExcursionsVariant
14 10 11 6 12 9 8 12 11 13 0.779188 104/106 RandomExcursionsVariant
12 12 10 7 17 6 6 12 13 11 0.262249 103/106 RandomExcursionsVariant
13 8 14 13 7 6 6 13 15 11 0.249284 102/106 RandomExcursionsVariant
12 12 12 13 7 9 6 13 12 10 0.739918 105/106 RandomExcursionsVariant
13 15 12 8 9 10 6 9 14 10 0.574903 106/106 RandomExcursionsVariant
10 15 9 12 14 10 8 11 7 10 0.739918 105/106 RandomExcursionsVariant
13 12 8 11 12 11 9 10 11 9 0.978072 103/106 RandomExcursionsVariant
10 13 12 12 8 13 8 9 14 7 0.739918 104/106 RandomExcursionsVariant
12 10 10 14 7 8 7 13 14 11 0.657933 106/106 RandomExcursionsVariant
10 13 10 10 13 10 12 6 10 12 0.897763 106/106 RandomExcursionsVariant
9 12 15 8 13 8 12 8 11 10 0.779188 106/106 RandomExcursionsVariant
9 13 15 10 10 10 8 14 6 11 0.616305 106/106 RandomExcursionsVariant
7 17 9 12 9 11 10 16 4 11 0.129620 106/106 RandomExcursionsVariant
10 9 10 15 7 12 7 8 12 16 0.419021 106/106 RandomExcursionsVariant
9 12 11 8 8 9 15 12 9 13 0.798139 106/106 RandomExcursionsVariant
17 34 11 22 22 17 19 20 13 25 0.026057 199/200 Serial
22 20 16 22 20 18 20 18 23 21 0.989786 199/200 Serial
12 33 25 29 21 11 21 15 14 19 0.003996 199/200 LinearComplexity
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The minimum pass rate for each statistical test with the exception of the
random excursion (variant) test is approximately = 193 for a
sample size = 200 binary sequences.
The minimum pass rate for the random excursion (variant) test
is approximately = 101 for a sample size = 106 binary sequences.
For further guidelines construct a probability table using the MAPLE program
provided in the addendum section of the documentation.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$
......@@ -8,14 +8,14 @@ import (
"sort"
)
// Generate the first permutation of data.
// PermutationFirst generates the first permutation of data.
func PermutationFirst(data sort.Interface) {
sort.Sort(data)
}
// Generate the next permutation of data if possible and return true.
// Return false if there is no more permutation left.
// Based on the algorithm described here:
// PermutationNext generates the next permutation of data if possible and
// return true. Return false if there is no more permutation left. Based on
// the algorithm described here:
// http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
func PermutationNext(data sort.Interface) bool {
var k, l int
......
// Copyright (c) 2016 The mathutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mathutil
import (
"fmt"
"math/big"
)
func abs(n int) uint64 {
if n >= 0 {
return uint64(n)
}
return uint64(-n)
}
// QuadPolyDiscriminant returns the discriminant of a quadratic polynomial in
// one variable of the form a*x^2+b*x+c with integer coefficients a, b, c, or
// an error on overflow.
//
// ds is the square of the discriminant. If |ds| is a square number, d is set
// to sqrt(|ds|), otherwise d is < 0.
func QuadPolyDiscriminant(a, b, c int) (ds, d int, _ error) {
if 2*BitLenUint64(abs(b)) > IntBits-1 ||
2+BitLenUint64(abs(a))+BitLenUint64(abs(c)) > IntBits-1 {
return 0, 0, fmt.Errorf("overflow")
}
ds = b*b - 4*a*c
s := ds
if s < 0 {
s = -s
}
d64 := SqrtUint64(uint64(s))
if d64*d64 != uint64(s) {
return ds, -1, nil
}
return ds, int(d64), nil
}
// PolyFactor describes an irreducible factor of a polynomial in one variable
// with integer coefficients P, Q of the form P*x+Q.
type PolyFactor struct {
P, Q int
}
// QuadPolyFactors returns the content and the irreducible factors of the
// primitive part of a quadratic polynomial in one variable with integer
// coefficients a, b, c of the form a*x^2+b*x+c in integers, or an error on
// overflow.
//
// If the factorization in integers does not exists, the return value is (0,
// nil, nil).
//
// See also:
// https://en.wikipedia.org/wiki/Factorization_of_polynomials#Primitive_part.E2.80.93content_factorization
func QuadPolyFactors(a, b, c int) (content int, primitivePart []PolyFactor, _ error) {
content = int(GCDUint64(abs(a), GCDUint64(abs(b), abs(c))))
switch {
case content == 0:
content = 1
case content > 0:
if a < 0 || a == 0 && b < 0 {
content = -content
}
}
a /= content
b /= content
c /= content
if a == 0 {
if b == 0 {
return content, []PolyFactor{{0, c}}, nil
}
if b < 0 && c < 0 {
b = -b
c = -c
}
if b < 0 {
b = -b
c = -c
}
return content, []PolyFactor{{b, c}}, nil
}
ds, d, err := QuadPolyDiscriminant(a, b, c)
if err != nil {
return 0, nil, err
}
if ds < 0 || d < 0 {
return 0, nil, nil
}
x1num := -b + d
x1denom := 2 * a
gcd := int(GCDUint64(abs(x1num), abs(x1denom)))
x1num /= gcd
x1denom /= gcd
x2num := -b - d
x2denom := 2 * a
gcd = int(GCDUint64(abs(x2num), abs(x2denom)))
x2num /= gcd
x2denom /= gcd
return content, []PolyFactor{{x1denom, -x1num}, {x2denom, -x2num}}, nil
}
// QuadPolyDiscriminantBig returns the discriminant of a quadratic polynomial
// in one variable of the form a*x^2+b*x+c with integer coefficients a, b, c.
//
// ds is the square of the discriminant. If |ds| is a square number, d is set
// to sqrt(|ds|), otherwise d is nil.
func QuadPolyDiscriminantBig(a, b, c *big.Int) (ds, d *big.Int) {
ds = big.NewInt(0).Set(b)
ds.Mul(ds, b)
x := big.NewInt(4)
x.Mul(x, a)
x.Mul(x, c)
ds.Sub(ds, x)
s := big.NewInt(0).Set(ds)
if s.Sign() < 0 {
s.Neg(s)
}
if s.Bit(1) != 0 { // s is not a square number
return ds, nil
}
d = SqrtBig(s)
x.Set(d)
x.Mul(x, x)
if x.Cmp(s) != 0 { // s is not a square number
d = nil
}
return ds, d
}
// PolyFactorBig describes an irreducible factor of a polynomial in one
// variable with integer coefficients P, Q of the form P*x+Q.
type PolyFactorBig struct {
P, Q *big.Int
}
// QuadPolyFactorsBig returns the content and the irreducible factors of the
// primitive part of a quadratic polynomial in one variable with integer
// coefficients a, b, c of the form a*x^2+b*x+c in integers.
//
// If the factorization in integers does not exists, the return value is (nil,
// nil).
//
// See also:
// https://en.wikipedia.org/wiki/Factorization_of_polynomials#Primitive_part.E2.80.93content_factorization
func QuadPolyFactorsBig(a, b, c *big.Int) (content *big.Int, primitivePart []PolyFactorBig) {
content = bigGCD(bigAbs(a), bigGCD(bigAbs(b), bigAbs(c)))
switch {
case content.Sign() == 0:
content.SetInt64(1)
case content.Sign() > 0:
if a.Sign() < 0 || a.Sign() == 0 && b.Sign() < 0 {
content = bigNeg(content)
}
}
a = bigDiv(a, content)
b = bigDiv(b, content)
c = bigDiv(c, content)
if a.Sign() == 0 {
if b.Sign() == 0 {
return content, []PolyFactorBig{{big.NewInt(0), c}}
}
if b.Sign() < 0 && c.Sign() < 0 {
b = bigNeg(b)
c = bigNeg(c)
}
if b.Sign() < 0 {
b = bigNeg(b)
c = bigNeg(c)
}
return content, []PolyFactorBig{{b, c}}
}
ds, d := QuadPolyDiscriminantBig(a, b, c)
if ds.Sign() < 0 || d == nil {
return nil, nil
}
x1num := bigAdd(bigNeg(b), d)
x1denom := bigMul(_2, a)
gcd := bigGCD(bigAbs(x1num), bigAbs(x1denom))
x1num = bigDiv(x1num, gcd)
x1denom = bigDiv(x1denom, gcd)
x2num := bigAdd(bigNeg(b), bigNeg(d))
x2denom := bigMul(_2, a)
gcd = bigGCD(bigAbs(x2num), bigAbs(x2denom))
x2num = bigDiv(x2num, gcd)
x2denom = bigDiv(x2denom, gcd)
return content, []PolyFactorBig{{x1denom, bigNeg(x1num)}, {x2denom, bigNeg(x2num)}}
}
func bigAbs(n *big.Int) *big.Int {
n = big.NewInt(0).Set(n)
if n.Sign() >= 0 {
return n
}
return n.Neg(n)
}
func bigDiv(a, b *big.Int) *big.Int {
a = big.NewInt(0).Set(a)
return a.Div(a, b)
}
func bigGCD(a, b *big.Int) *big.Int {
a = big.NewInt(0).Set(a)
b = big.NewInt(0).Set(b)
for b.Sign() != 0 {
c := big.NewInt(0)
c.Mod(a, b)
a, b = b, c
}
return a
}
func bigNeg(n *big.Int) *big.Int {
n = big.NewInt(0).Set(n)
return n.Neg(n)
}
func bigMul(a, b *big.Int) *big.Int {
r := big.NewInt(0).Set(a)
return r.Mul(r, b)
}
func bigAdd(a, b *big.Int) *big.Int {
r := big.NewInt(0).Set(a)
return r.Add(r, b)
}
# Version 1.x.x
* **Add more test cases and reference new test COM server project.** (Placeholder for future additions)
# Version 1.2.0-alphaX
**Minimum supported version is now Go 1.4. Go 1.1 support is deprecated, but should still build.**
* Added CI configuration for Travis-CI and AppVeyor.
* Added test InterfaceID and ClassID for the COM Test Server project.
* Added more inline documentation (#83).
* Added IEnumVARIANT implementation (#88).
* Added IEnumVARIANT test cases (#99, #100, #101).
* Added support for retrieving `time.Time` from VARIANT (#92).
* Added test case for IUnknown (#64).
* Added test case for IDispatch (#64).
* Added test cases for scalar variants (#64, #76).
# Version 1.1.1
* Fixes for Linux build.
* Fixes for Windows build.
# Version 1.1.0
The change to provide building on all platforms is a new feature. The increase in minor version reflects that and allows those who wish to stay on 1.0.x to continue to do so. Support for 1.0.x will be limited to bug fixes.
* Move GUID out of variables.go into its own file to make new documentation available.
* Move OleError out of ole.go into its own file to make new documentation available.
* Add documentation to utility functions.
* Add documentation to variant receiver functions.
* Add documentation to ole structures.
* Make variant available to other systems outside of Windows.
* Make OLE structures available to other systems outside of Windows.
## New Features
* Library should now be built on all platforms supported by Go. Library will NOOP on any platform that is not Windows.
* More functions are now documented and available on godoc.org.
# Version 1.0.1
1. Fix package references from repository location change.
# Version 1.0.0
This version is stable enough for use. The COM API is still incomplete, but provides enough functionality for accessing COM servers using IDispatch interface.
There is no changelog for this version. Check commits for history.
The MIT License (MIT)
Copyright © 2013-2017 Yasuhiro Matsumoto, <mattn.jp@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Go OLE
[![Build status](https://ci.appveyor.com/api/projects/status/qr0u2sf7q43us9fj?svg=true)](https://ci.appveyor.com/project/jacobsantos/go-ole-jgs28)
[![Build Status](https://travis-ci.org/go-ole/go-ole.svg?branch=master)](https://travis-ci.org/go-ole/go-ole)
[![GoDoc](https://godoc.org/github.com/go-ole/go-ole?status.svg)](https://godoc.org/github.com/go-ole/go-ole)
Go bindings for Windows COM using shared libraries instead of cgo.
By Yasuhiro Matsumoto.
## Install
To experiment with go-ole, you can just compile and run the example program:
```
go get github.com/go-ole/go-ole
cd /path/to/go-ole/
go test
cd /path/to/go-ole/example/excel
go run excel.go
```
## Continuous Integration
Continuous integration configuration has been added for both Travis-CI and AppVeyor. You will have to add these to your own account for your fork in order for it to run.
**Travis-CI**
Travis-CI was added to check builds on Linux to ensure that `go get` works when cross building. Currently, Travis-CI is not used to test cross-building, but this may be changed in the future. It is also not currently possible to test the library on Linux, since COM API is specific to Windows and it is not currently possible to run a COM server on Linux or even connect to a remote COM server.
**AppVeyor**
AppVeyor is used to build on Windows using the (in-development) test COM server. It is currently only used to test the build and ensure that the code works on Windows. It will be used to register a COM server and then run the test cases based on the test COM server.
The tests currently do run and do pass and this should be maintained with commits.
## Versioning
Go OLE uses [semantic versioning](http://semver.org) for version numbers, which is similar to the version contract of the Go language. Which means that the major version will always maintain backwards compatibility with minor versions. Minor versions will only add new additions and changes. Fixes will always be in patch.
This contract should allow you to upgrade to new minor and patch versions without breakage or modifications to your existing code. Leave a ticket, if there is breakage, so that it could be fixed.
## LICENSE
Under the MIT License: http://mattn.mit-license.org/2013
# Notes:
# - Minimal appveyor.yml file is an empty file. All sections are optional.
# - Indent each level of configuration with 2 spaces. Do not use tabs!
# - All section names are case-sensitive.
# - Section names should be unique on each level.
version: "1.3.0.{build}-alpha-{branch}"
os: Windows Server 2012 R2
branches:
only:
- master
- v1.2
- v1.1
- v1.0
skip_tags: true
clone_folder: c:\gopath\src\github.com\go-ole\go-ole
environment:
GOPATH: c:\gopath
matrix:
- GOARCH: amd64
GOVERSION: 1.5
GOROOT: c:\go
DOWNLOADPLATFORM: "x64"
install:
- choco install mingw
- SET PATH=c:\tools\mingw64\bin;%PATH%
# - Download COM Server
- ps: Start-FileDownload "https://github.com/go-ole/test-com-server/releases/download/v1.0.2/test-com-server-${env:DOWNLOADPLATFORM}.zip"
- 7z e test-com-server-%DOWNLOADPLATFORM%.zip -oc:\gopath\src\github.com\go-ole\go-ole > NUL
- c:\gopath\src\github.com\go-ole\go-ole\build\register-assembly.bat
# - set
- go version
- go env
- go get -u golang.org/x/tools/cmd/cover
- go get -u golang.org/x/tools/cmd/godoc
- go get -u golang.org/x/tools/cmd/stringer
build_script:
- cd c:\gopath\src\github.com\go-ole\go-ole
- go get -v -t ./...
- go build
- go test -v -cover ./...
# disable automatic tests
test: off
# disable deployment
deploy: off
// +build windows
package ole
import (
"syscall"
"unicode/utf16"
"unsafe"
)
var (
procCoInitialize, _ = modole32.FindProc("CoInitialize")
procCoInitializeEx, _ = modole32.FindProc("CoInitializeEx")
procCoUninitialize, _ = modole32.FindProc("CoUninitialize")
procCoCreateInstance, _ = modole32.FindProc("CoCreateInstance")
procCoTaskMemFree, _ = modole32.FindProc("CoTaskMemFree")
procCLSIDFromProgID, _ = modole32.FindProc("CLSIDFromProgID")
procCLSIDFromString, _ = modole32.FindProc("CLSIDFromString")
procStringFromCLSID, _ = modole32.FindProc("StringFromCLSID")
procStringFromIID, _ = modole32.FindProc("StringFromIID")
procIIDFromString, _ = modole32.FindProc("IIDFromString")
procCoGetObject, _ = modole32.FindProc("CoGetObject")
procGetUserDefaultLCID, _ = modkernel32.FindProc("GetUserDefaultLCID")
procCopyMemory, _ = modkernel32.FindProc("RtlMoveMemory")
procVariantInit, _ = modoleaut32.FindProc("VariantInit")
procVariantClear, _ = modoleaut32.FindProc("VariantClear")
procVariantTimeToSystemTime, _ = modoleaut32.FindProc("VariantTimeToSystemTime")
procSysAllocString, _ = modoleaut32.FindProc("SysAllocString")
procSysAllocStringLen, _ = modoleaut32.FindProc("SysAllocStringLen")
procSysFreeString, _ = modoleaut32.FindProc("SysFreeString")
procSysStringLen, _ = modoleaut32.FindProc("SysStringLen")
procCreateDispTypeInfo, _ = modoleaut32.FindProc("CreateDispTypeInfo")
procCreateStdDispatch, _ = modoleaut32.FindProc("CreateStdDispatch")
procGetActiveObject, _ = modoleaut32.FindProc("GetActiveObject")
procGetMessageW, _ = moduser32.FindProc("GetMessageW")
procDispatchMessageW, _ = moduser32.FindProc("DispatchMessageW")
)
// coInitialize initializes COM library on current thread.
//
// MSDN documentation suggests that this function should not be called. Call
// CoInitializeEx() instead. The reason has to do with threading and this
// function is only for single-threaded apartments.
//
// That said, most users of the library have gotten away with just this
// function. If you are experiencing threading issues, then use
// CoInitializeEx().
func coInitialize() (err error) {
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx
// Suggests that no value should be passed to CoInitialized.
// Could just be Call() since the parameter is optional. <-- Needs testing to be sure.
hr, _, _ := procCoInitialize.Call(uintptr(0))
if hr != 0 {
err = NewError(hr)
}
return
}
// coInitializeEx initializes COM library with concurrency model.
func coInitializeEx(coinit uint32) (err error) {
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms695279(v=vs.85).aspx
// Suggests that the first parameter is not only optional but should always be NULL.
hr, _, _ := procCoInitializeEx.Call(uintptr(0), uintptr(coinit))
if hr != 0 {
err = NewError(hr)
}
return
}
// CoInitialize initializes COM library on current thread.
//
// MSDN documentation suggests that this function should not be called. Call
// CoInitializeEx() instead. The reason has to do with threading and this
// function is only for single-threaded apartments.
//
// That said, most users of the library have gotten away with just this
// function. If you are experiencing threading issues, then use
// CoInitializeEx().
func CoInitialize(p uintptr) (err error) {
// p is ignored and won't be used.
// Avoid any variable not used errors.
p = uintptr(0)
return coInitialize()
}
// CoInitializeEx initializes COM library with concurrency model.
func CoInitializeEx(p uintptr, coinit uint32) (err error) {
// Avoid any variable not used errors.
p = uintptr(0)
return coInitializeEx(coinit)
}
// CoUninitialize uninitializes COM Library.
func CoUninitialize() {
procCoUninitialize.Call()
}
// CoTaskMemFree frees memory pointer.
func CoTaskMemFree(memptr uintptr) {
procCoTaskMemFree.Call(memptr)
}
// CLSIDFromProgID retrieves Class Identifier with the given Program Identifier.
//
// The Programmatic Identifier must be registered, because it will be looked up
// in the Windows Registry. The registry entry has the following keys: CLSID,
// Insertable, Protocol and Shell
// (https://msdn.microsoft.com/en-us/library/dd542719(v=vs.85).aspx).
//
// programID identifies the class id with less precision and is not guaranteed
// to be unique. These are usually found in the registry under
// HKEY_LOCAL_MACHINE\SOFTWARE\Classes, usually with the format of
// "Program.Component.Version" with version being optional.
//
// CLSIDFromProgID in Windows API.
func CLSIDFromProgID(progId string) (clsid *GUID, err error) {
var guid GUID
lpszProgID := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(progId)))
hr, _, _ := procCLSIDFromProgID.Call(lpszProgID, uintptr(unsafe.Pointer(&guid)))
if hr != 0 {
err = NewError(hr)
}
clsid = &guid
return
}
// CLSIDFromString retrieves Class ID from string representation.
//
// This is technically the string version of the GUID and will convert the
// string to object.
//
// CLSIDFromString in Windows API.
func CLSIDFromString(str string) (clsid *GUID, err error) {
var guid GUID
lpsz := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(str)))
hr, _, _ := procCLSIDFromString.Call(lpsz, uintptr(unsafe.Pointer(&guid)))
if hr != 0 {
err = NewError(hr)
}
clsid = &guid
return
}
// StringFromCLSID returns GUID formated string from GUID object.
func StringFromCLSID(clsid *GUID) (str string, err error) {
var p *uint16
hr, _, _ := procStringFromCLSID.Call(uintptr(unsafe.Pointer(clsid)), uintptr(unsafe.Pointer(&p)))
if hr != 0 {
err = NewError(hr)
}
str = LpOleStrToString(p)
return
}
// IIDFromString returns GUID from program ID.
func IIDFromString(progId string) (clsid *GUID, err error) {
var guid GUID
lpsz := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(progId)))
hr, _, _ := procIIDFromString.Call(lpsz, uintptr(unsafe.Pointer(&guid)))
if hr != 0 {
err = NewError(hr)
}
clsid = &guid
return
}
// StringFromIID returns GUID formatted string from GUID object.
func StringFromIID(iid *GUID) (str string, err error) {
var p *uint16
hr, _, _ := procStringFromIID.Call(uintptr(unsafe.Pointer(iid)), uintptr(unsafe.Pointer(&p)))
if hr != 0 {
err = NewError(hr)
}
str = LpOleStrToString(p)
return
}
// CreateInstance of single uninitialized object with GUID.
func CreateInstance(clsid *GUID, iid *GUID) (unk *IUnknown, err error) {
if iid == nil {
iid = IID_IUnknown
}
hr, _, _ := procCoCreateInstance.Call(
uintptr(unsafe.Pointer(clsid)),
0,
CLSCTX_SERVER,
uintptr(unsafe.Pointer(iid)),
uintptr(unsafe.Pointer(&unk)))
if hr != 0 {
err = NewError(hr)
}
return
}
// GetActiveObject retrieves pointer to active object.
func GetActiveObject(clsid *GUID, iid *GUID) (unk *IUnknown, err error) {
if iid == nil {
iid = IID_IUnknown
}
hr, _, _ := procGetActiveObject.Call(
uintptr(unsafe.Pointer(clsid)),
uintptr(unsafe.Pointer(iid)),
uintptr(unsafe.Pointer(&unk)))
if hr != 0 {
err = NewError(hr)
}
return
}
type BindOpts struct {
CbStruct uint32
GrfFlags uint32
GrfMode uint32
TickCountDeadline uint32
}
// GetObject retrieves pointer to active object.
func GetObject(programID string, bindOpts *BindOpts, iid *GUID) (unk *IUnknown, err error) {
if bindOpts != nil {
bindOpts.CbStruct = uint32(unsafe.Sizeof(BindOpts{}))
}
if iid == nil {
iid = IID_IUnknown
}
hr, _, _ := procCoGetObject.Call(
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(programID))),
uintptr(unsafe.Pointer(bindOpts)),
uintptr(unsafe.Pointer(iid)),
uintptr(unsafe.Pointer(&unk)))
if hr != 0 {
err = NewError(hr)
}
return
}
// VariantInit initializes variant.
func VariantInit(v *VARIANT) (err error) {
hr, _, _ := procVariantInit.Call(uintptr(unsafe.Pointer(v)))
if hr != 0 {
err = NewError(hr)
}
return
}
// VariantClear clears value in Variant settings to VT_EMPTY.
func VariantClear(v *VARIANT) (err error) {
hr, _, _ := procVariantClear.Call(uintptr(unsafe.Pointer(v)))
if hr != 0 {
err = NewError(hr)
}
return
}
// SysAllocString allocates memory for string and copies string into memory.
func SysAllocString(v string) (ss *int16) {
pss, _, _ := procSysAllocString.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v))))
ss = (*int16)(unsafe.Pointer(pss))
return
}
// SysAllocStringLen copies up to length of given string returning pointer.
func SysAllocStringLen(v string) (ss *int16) {
utf16 := utf16.Encode([]rune(v + "\x00"))
ptr := &utf16[0]
pss, _, _ := procSysAllocStringLen.Call(uintptr(unsafe.Pointer(ptr)), uintptr(len(utf16)-1))
ss = (*int16)(unsafe.Pointer(pss))
return
}
// SysFreeString frees string system memory. This must be called with SysAllocString.
func SysFreeString(v *int16) (err error) {
hr, _, _ := procSysFreeString.Call(uintptr(unsafe.Pointer(v)))
if hr != 0 {
err = NewError(hr)
}
return
}
// SysStringLen is the length of the system allocated string.
func SysStringLen(v *int16) uint32 {
l, _, _ := procSysStringLen.Call(uintptr(unsafe.Pointer(v)))
return uint32(l)
}
// CreateStdDispatch provides default IDispatch implementation for IUnknown.
//
// This handles default IDispatch implementation for objects. It haves a few
// limitations with only supporting one language. It will also only return
// default exception codes.
func CreateStdDispatch(unk *IUnknown, v uintptr, ptinfo *IUnknown) (disp *IDispatch, err error) {
hr, _, _ := procCreateStdDispatch.Call(
uintptr(unsafe.Pointer(unk)),
v,
uintptr(unsafe.Pointer(ptinfo)),
uintptr(unsafe.Pointer(&disp)))
if hr != 0 {
err = NewError(hr)
}
return
}
// CreateDispTypeInfo provides default ITypeInfo implementation for IDispatch.
//
// This will not handle the full implementation of the interface.
func CreateDispTypeInfo(idata *INTERFACEDATA) (pptinfo *IUnknown, err error) {
hr, _, _ := procCreateDispTypeInfo.Call(
uintptr(unsafe.Pointer(idata)),
uintptr(GetUserDefaultLCID()),
uintptr(unsafe.Pointer(&pptinfo)))
if hr != 0 {
err = NewError(hr)
}
return
}
// copyMemory moves location of a block of memory.
func copyMemory(dest unsafe.Pointer, src unsafe.Pointer, length uint32) {
procCopyMemory.Call(uintptr(dest), uintptr(src), uintptr(length))
}
// GetUserDefaultLCID retrieves current user default locale.
func GetUserDefaultLCID() (lcid uint32) {
ret, _, _ := procGetUserDefaultLCID.Call()
lcid = uint32(ret)
return
}
// GetMessage in message queue from runtime.
//
// This function appears to block. PeekMessage does not block.
func GetMessage(msg *Msg, hwnd uint32, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) {
r0, _, err := procGetMessageW.Call(uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(MsgFilterMin), uintptr(MsgFilterMax))
ret = int32(r0)
return
}
// DispatchMessage to window procedure.
func DispatchMessage(msg *Msg) (ret int32) {
r0, _, _ := procDispatchMessageW.Call(uintptr(unsafe.Pointer(msg)))
ret = int32(r0)
return
}
// +build !windows
package ole
import (
"time"
"unsafe"
)
// coInitialize initializes COM library on current thread.
//
// MSDN documentation suggests that this function should not be called. Call
// CoInitializeEx() instead. The reason has to do with threading and this
// function is only for single-threaded apartments.
//
// That said, most users of the library have gotten away with just this
// function. If you are experiencing threading issues, then use
// CoInitializeEx().
func coInitialize() error {
return NewError(E_NOTIMPL)
}
// coInitializeEx initializes COM library with concurrency model.
func coInitializeEx(coinit uint32) error {
return NewError(E_NOTIMPL)
}
// CoInitialize initializes COM library on current thread.
//
// MSDN documentation suggests that this function should not be called. Call
// CoInitializeEx() instead. The reason has to do with threading and this
// function is only for single-threaded apartments.
//
// That said, most users of the library have gotten away with just this
// function. If you are experiencing threading issues, then use
// CoInitializeEx().
func CoInitialize(p uintptr) error {
return NewError(E_NOTIMPL)
}
// CoInitializeEx initializes COM library with concurrency model.
func CoInitializeEx(p uintptr, coinit uint32) error {
return NewError(E_NOTIMPL)
}
// CoUninitialize uninitializes COM Library.
func CoUninitialize() {}
// CoTaskMemFree frees memory pointer.
func CoTaskMemFree(memptr uintptr) {}
// CLSIDFromProgID retrieves Class Identifier with the given Program Identifier.
//
// The Programmatic Identifier must be registered, because it will be looked up
// in the Windows Registry. The registry entry has the following keys: CLSID,
// Insertable, Protocol and Shell
// (https://msdn.microsoft.com/en-us/library/dd542719(v=vs.85).aspx).
//
// programID identifies the class id with less precision and is not guaranteed
// to be unique. These are usually found in the registry under
// HKEY_LOCAL_MACHINE\SOFTWARE\Classes, usually with the format of
// "Program.Component.Version" with version being optional.
//
// CLSIDFromProgID in Windows API.
func CLSIDFromProgID(progId string) (*GUID, error) {
return nil, NewError(E_NOTIMPL)
}
// CLSIDFromString retrieves Class ID from string representation.
//
// This is technically the string version of the GUID and will convert the
// string to object.
//
// CLSIDFromString in Windows API.
func CLSIDFromString(str string) (*GUID, error) {
return nil, NewError(E_NOTIMPL)
}
// StringFromCLSID returns GUID formated string from GUID object.
func StringFromCLSID(clsid *GUID) (string, error) {
return "", NewError(E_NOTIMPL)
}
// IIDFromString returns GUID from program ID.
func IIDFromString(progId string) (*GUID, error) {
return nil, NewError(E_NOTIMPL)
}
// StringFromIID returns GUID formatted string from GUID object.
func StringFromIID(iid *GUID) (string, error) {
return "", NewError(E_NOTIMPL)
}
// CreateInstance of single uninitialized object with GUID.
func CreateInstance(clsid *GUID, iid *GUID) (*IUnknown, error) {
return nil, NewError(E_NOTIMPL)
}
// GetActiveObject retrieves pointer to active object.
func GetActiveObject(clsid *GUID, iid *GUID) (*IUnknown, error) {
return nil, NewError(E_NOTIMPL)
}
// VariantInit initializes variant.
func VariantInit(v *VARIANT) error {
return NewError(E_NOTIMPL)
}
// VariantClear clears value in Variant settings to VT_EMPTY.
func VariantClear(v *VARIANT) error {
return NewError(E_NOTIMPL)
}
// SysAllocString allocates memory for string and copies string into memory.
func SysAllocString(v string) *int16 {
u := int16(0)
return &u
}
// SysAllocStringLen copies up to length of given string returning pointer.
func SysAllocStringLen(v string) *int16 {
u := int16(0)
return &u
}
// SysFreeString frees string system memory. This must be called with SysAllocString.
func SysFreeString(v *int16) error {
return NewError(E_NOTIMPL)
}
// SysStringLen is the length of the system allocated string.
func SysStringLen(v *int16) uint32 {
return uint32(0)
}
// CreateStdDispatch provides default IDispatch implementation for IUnknown.
//
// This handles default IDispatch implementation for objects. It haves a few
// limitations with only supporting one language. It will also only return
// default exception codes.
func CreateStdDispatch(unk *IUnknown, v uintptr, ptinfo *IUnknown) (*IDispatch, error) {
return nil, NewError(E_NOTIMPL)
}
// CreateDispTypeInfo provides default ITypeInfo implementation for IDispatch.
//
// This will not handle the full implementation of the interface.
func CreateDispTypeInfo(idata *INTERFACEDATA) (*IUnknown, error) {
return nil, NewError(E_NOTIMPL)
}
// copyMemory moves location of a block of memory.
func copyMemory(dest unsafe.Pointer, src unsafe.Pointer, length uint32) {}
// GetUserDefaultLCID retrieves current user default locale.
func GetUserDefaultLCID() uint32 {
return uint32(0)
}
// GetMessage in message queue from runtime.
//
// This function appears to block. PeekMessage does not block.
func GetMessage(msg *Msg, hwnd uint32, MsgFilterMin uint32, MsgFilterMax uint32) (int32, error) {
return int32(0), NewError(E_NOTIMPL)
}
// DispatchMessage to window procedure.
func DispatchMessage(msg *Msg) int32 {
return int32(0)
}
func GetVariantDate(value uint64) (time.Time, error) {
return time.Now(), NewError(E_NOTIMPL)
}
package ole
// Connection contains IUnknown for fluent interface interaction.
//
// Deprecated. Use oleutil package instead.
type Connection struct {
Object *IUnknown // Access COM
}
// Initialize COM.
func (*Connection) Initialize() (err error) {
return coInitialize()
}
// Uninitialize COM.
func (*Connection) Uninitialize() {
CoUninitialize()
}
// Create IUnknown object based first on ProgId and then from String.
func (c *Connection) Create(progId string) (err error) {
var clsid *GUID
clsid, err = CLSIDFromProgID(progId)
if err != nil {
clsid, err = CLSIDFromString(progId)
if err != nil {
return
}
}
unknown, err := CreateInstance(clsid, IID_IUnknown)
if err != nil {
return
}
c.Object = unknown
return
}
// Release IUnknown object.
func (c *Connection) Release() {
c.Object.Release()
}
// Load COM object from list of programIDs or strings.
func (c *Connection) Load(names ...string) (errors []error) {
var tempErrors []error = make([]error, len(names))
var numErrors int = 0
for _, name := range names {
err := c.Create(name)
if err != nil {
tempErrors = append(tempErrors, err)
numErrors += 1
continue
}
break
}
copy(errors, tempErrors[0:numErrors])
return
}
// Dispatch returns Dispatch object.
func (c *Connection) Dispatch() (object *Dispatch, err error) {
dispatch, err := c.Object.QueryInterface(IID_IDispatch)
if err != nil {
return
}
object = &Dispatch{dispatch}
return
}
// Dispatch stores IDispatch object.
type Dispatch struct {
Object *IDispatch // Dispatch object.
}
// Call method on IDispatch with parameters.
func (d *Dispatch) Call(method string, params ...interface{}) (result *VARIANT, err error) {
id, err := d.GetId(method)
if err != nil {
return
}
result, err = d.Invoke(id, DISPATCH_METHOD, params)
return
}
// MustCall method on IDispatch with parameters.
func (d *Dispatch) MustCall(method string, params ...interface{}) (result *VARIANT) {
id, err := d.GetId(method)
if err != nil {
panic(err)
}
result, err = d.Invoke(id, DISPATCH_METHOD, params)
if err != nil {
panic(err)
}
return
}
// Get property on IDispatch with parameters.
func (d *Dispatch) Get(name string, params ...interface{}) (result *VARIANT, err error) {
id, err := d.GetId(name)
if err != nil {
return
}
result, err = d.Invoke(id, DISPATCH_PROPERTYGET, params)
return
}
// MustGet property on IDispatch with parameters.
func (d *Dispatch) MustGet(name string, params ...interface{}) (result *VARIANT) {
id, err := d.GetId(name)
if err != nil {
panic(err)
}
result, err = d.Invoke(id, DISPATCH_PROPERTYGET, params)
if err != nil {
panic(err)
}
return
}
// Set property on IDispatch with parameters.
func (d *Dispatch) Set(name string, params ...interface{}) (result *VARIANT, err error) {
id, err := d.GetId(name)
if err != nil {
return
}
result, err = d.Invoke(id, DISPATCH_PROPERTYPUT, params)
return
}
// MustSet property on IDispatch with parameters.
func (d *Dispatch) MustSet(name string, params ...interface{}) (result *VARIANT) {
id, err := d.GetId(name)
if err != nil {
panic(err)
}
result, err = d.Invoke(id, DISPATCH_PROPERTYPUT, params)
if err != nil {
panic(err)
}
return
}
// GetId retrieves ID of name on IDispatch.
func (d *Dispatch) GetId(name string) (id int32, err error) {
var dispid []int32
dispid, err = d.Object.GetIDsOfName([]string{name})
if err != nil {
return
}
id = dispid[0]
return
}
// GetIds retrieves all IDs of names on IDispatch.
func (d *Dispatch) GetIds(names ...string) (dispid []int32, err error) {
dispid, err = d.Object.GetIDsOfName(names)
return
}
// Invoke IDispatch on DisplayID of dispatch type with parameters.
//
// There have been problems where if send cascading params..., it would error
// out because the parameters would be empty.
func (d *Dispatch) Invoke(id int32, dispatch int16, params []interface{}) (result *VARIANT, err error) {
if len(params) < 1 {
result, err = d.Object.Invoke(id, dispatch)
} else {
result, err = d.Object.Invoke(id, dispatch, params...)
}
return
}
// Release IDispatch object.
func (d *Dispatch) Release() {
d.Object.Release()
}
// Connect initializes COM and attempts to load IUnknown based on given names.
func Connect(names ...string) (connection *Connection) {
connection.Initialize()
connection.Load(names...)
return
}
package ole
const (
CLSCTX_INPROC_SERVER = 1
CLSCTX_INPROC_HANDLER = 2
CLSCTX_LOCAL_SERVER = 4
CLSCTX_INPROC_SERVER16 = 8
CLSCTX_REMOTE_SERVER = 16
CLSCTX_ALL = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER
CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER
CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER
)
const (
COINIT_APARTMENTTHREADED = 0x2
COINIT_MULTITHREADED = 0x0
COINIT_DISABLE_OLE1DDE = 0x4
COINIT_SPEED_OVER_MEMORY = 0x8
)
const (
DISPATCH_METHOD = 1
DISPATCH_PROPERTYGET = 2
DISPATCH_PROPERTYPUT = 4
DISPATCH_PROPERTYPUTREF = 8
)
const (
S_OK = 0x00000000
E_UNEXPECTED = 0x8000FFFF
E_NOTIMPL = 0x80004001
E_OUTOFMEMORY = 0x8007000E
E_INVALIDARG = 0x80070057
E_NOINTERFACE = 0x80004002
E_POINTER = 0x80004003
E_HANDLE = 0x80070006
E_ABORT = 0x80004004
E_FAIL = 0x80004005
E_ACCESSDENIED = 0x80070005
E_PENDING = 0x8000000A
CO_E_CLASSSTRING = 0x800401F3
)
const (
CC_FASTCALL = iota
CC_CDECL
CC_MSCPASCAL
CC_PASCAL = CC_MSCPASCAL
CC_MACPASCAL
CC_STDCALL
CC_FPFASTCALL
CC_SYSCALL
CC_MPWCDECL
CC_MPWPASCAL
CC_MAX = CC_MPWPASCAL
)
type VT uint16
const (
VT_EMPTY VT = 0x0
VT_NULL VT = 0x1
VT_I2 VT = 0x2
VT_I4 VT = 0x3
VT_R4 VT = 0x4
VT_R8 VT = 0x5
VT_CY VT = 0x6
VT_DATE VT = 0x7
VT_BSTR VT = 0x8
VT_DISPATCH VT = 0x9
VT_ERROR VT = 0xa
VT_BOOL VT = 0xb
VT_VARIANT VT = 0xc
VT_UNKNOWN VT = 0xd
VT_DECIMAL VT = 0xe
VT_I1 VT = 0x10
VT_UI1 VT = 0x11
VT_UI2 VT = 0x12
VT_UI4 VT = 0x13
VT_I8 VT = 0x14
VT_UI8 VT = 0x15
VT_INT VT = 0x16
VT_UINT VT = 0x17
VT_VOID VT = 0x18
VT_HRESULT VT = 0x19
VT_PTR VT = 0x1a
VT_SAFEARRAY VT = 0x1b
VT_CARRAY VT = 0x1c
VT_USERDEFINED VT = 0x1d
VT_LPSTR VT = 0x1e
VT_LPWSTR VT = 0x1f
VT_RECORD VT = 0x24
VT_INT_PTR VT = 0x25
VT_UINT_PTR VT = 0x26
VT_FILETIME VT = 0x40
VT_BLOB VT = 0x41
VT_STREAM VT = 0x42
VT_STORAGE VT = 0x43
VT_STREAMED_OBJECT VT = 0x44
VT_STORED_OBJECT VT = 0x45
VT_BLOB_OBJECT VT = 0x46
VT_CF VT = 0x47
VT_CLSID VT = 0x48
VT_BSTR_BLOB VT = 0xfff
VT_VECTOR VT = 0x1000
VT_ARRAY VT = 0x2000
VT_BYREF VT = 0x4000
VT_RESERVED VT = 0x8000
VT_ILLEGAL VT = 0xffff
VT_ILLEGALMASKED VT = 0xfff
VT_TYPEMASK VT = 0xfff
)
const (
DISPID_UNKNOWN = -1
DISPID_VALUE = 0
DISPID_PROPERTYPUT = -3
DISPID_NEWENUM = -4
DISPID_EVALUATE = -5
DISPID_CONSTRUCTOR = -6
DISPID_DESTRUCTOR = -7
DISPID_COLLECT = -8
)
const (
TKIND_ENUM = 1
TKIND_RECORD = 2
TKIND_MODULE = 3
TKIND_INTERFACE = 4
TKIND_DISPATCH = 5
TKIND_COCLASS = 6
TKIND_ALIAS = 7
TKIND_UNION = 8
TKIND_MAX = 9
)
// Safe Array Feature Flags
const (
FADF_AUTO = 0x0001
FADF_STATIC = 0x0002
FADF_EMBEDDED = 0x0004
FADF_FIXEDSIZE = 0x0010
FADF_RECORD = 0x0020
FADF_HAVEIID = 0x0040
FADF_HAVEVARTYPE = 0x0080
FADF_BSTR = 0x0100
FADF_UNKNOWN = 0x0200
FADF_DISPATCH = 0x0400
FADF_VARIANT = 0x0800
FADF_RESERVED = 0xF008
)
package ole
// OleError stores COM errors.
type OleError struct {
hr uintptr
description string
subError error
}
// NewError creates new error with HResult.
func NewError(hr uintptr) *OleError {
return &OleError{hr: hr}
}
// NewErrorWithDescription creates new COM error with HResult and description.
func NewErrorWithDescription(hr uintptr, description string) *OleError {
return &OleError{hr: hr, description: description}
}
// NewErrorWithSubError creates new COM error with parent error.
func NewErrorWithSubError(hr uintptr, description string, err error) *OleError {
return &OleError{hr: hr, description: description, subError: err}
}
// Code is the HResult.
func (v *OleError) Code() uintptr {
return uintptr(v.hr)
}
// String description, either manually set or format message with error code.
func (v *OleError) String() string {
if v.description != "" {
return errstr(int(v.hr)) + " (" + v.description + ")"
}
return errstr(int(v.hr))
}
// Error implements error interface.
func (v *OleError) Error() string {
return v.String()
}
// Description retrieves error summary, if there is one.
func (v *OleError) Description() string {
return v.description
}
// SubError returns parent error, if there is one.
func (v *OleError) SubError() error {
return v.subError
}
// +build !windows
package ole
// errstr converts error code to string.
func errstr(errno int) string {
return ""
}
// +build windows
package ole
import (
"fmt"
"syscall"
"unicode/utf16"
)
// errstr converts error code to string.
func errstr(errno int) string {
// ask windows for the remaining errors
var flags uint32 = syscall.FORMAT_MESSAGE_FROM_SYSTEM | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
b := make([]uint16, 300)
n, err := syscall.FormatMessage(flags, 0, uint32(errno), 0, b, nil)
if err != nil {
return fmt.Sprintf("error %d (FormatMessage failed with: %v)", errno, err)
}
// trim terminating \r and \n
for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
}
return string(utf16.Decode(b[:n]))
}
package ole
var (
// IID_NULL is null Interface ID, used when no other Interface ID is known.
IID_NULL = NewGUID("{00000000-0000-0000-0000-000000000000}")
// IID_IUnknown is for IUnknown interfaces.
IID_IUnknown = NewGUID("{00000000-0000-0000-C000-000000000046}")
// IID_IDispatch is for IDispatch interfaces.
IID_IDispatch = NewGUID("{00020400-0000-0000-C000-000000000046}")
// IID_IEnumVariant is for IEnumVariant interfaces
IID_IEnumVariant = NewGUID("{00020404-0000-0000-C000-000000000046}")
// IID_IConnectionPointContainer is for IConnectionPointContainer interfaces.
IID_IConnectionPointContainer = NewGUID("{B196B284-BAB4-101A-B69C-00AA00341D07}")
// IID_IConnectionPoint is for IConnectionPoint interfaces.
IID_IConnectionPoint = NewGUID("{B196B286-BAB4-101A-B69C-00AA00341D07}")
// IID_IInspectable is for IInspectable interfaces.
IID_IInspectable = NewGUID("{AF86E2E0-B12D-4C6A-9C5A-D7AA65101E90}")
// IID_IProvideClassInfo is for IProvideClassInfo interfaces.
IID_IProvideClassInfo = NewGUID("{B196B283-BAB4-101A-B69C-00AA00341D07}")
)
// These are for testing and not part of any library.
var (
// IID_ICOMTestString is for ICOMTestString interfaces.
//
// {E0133EB4-C36F-469A-9D3D-C66B84BE19ED}
IID_ICOMTestString = NewGUID("{E0133EB4-C36F-469A-9D3D-C66B84BE19ED}")
// IID_ICOMTestInt8 is for ICOMTestInt8 interfaces.
//
// {BEB06610-EB84-4155-AF58-E2BFF53680B4}
IID_ICOMTestInt8 = NewGUID("{BEB06610-EB84-4155-AF58-E2BFF53680B4}")
// IID_ICOMTestInt16 is for ICOMTestInt16 interfaces.
//
// {DAA3F9FA-761E-4976-A860-8364CE55F6FC}
IID_ICOMTestInt16 = NewGUID("{DAA3F9FA-761E-4976-A860-8364CE55F6FC}")
// IID_ICOMTestInt32 is for ICOMTestInt32 interfaces.
//
// {E3DEDEE7-38A2-4540-91D1-2EEF1D8891B0}
IID_ICOMTestInt32 = NewGUID("{E3DEDEE7-38A2-4540-91D1-2EEF1D8891B0}")
// IID_ICOMTestInt64 is for ICOMTestInt64 interfaces.
//
// {8D437CBC-B3ED-485C-BC32-C336432A1623}
IID_ICOMTestInt64 = NewGUID("{8D437CBC-B3ED-485C-BC32-C336432A1623}")
// IID_ICOMTestFloat is for ICOMTestFloat interfaces.
//
// {BF1ED004-EA02-456A-AA55-2AC8AC6B054C}
IID_ICOMTestFloat = NewGUID("{BF1ED004-EA02-456A-AA55-2AC8AC6B054C}")
// IID_ICOMTestDouble is for ICOMTestDouble interfaces.
//
// {BF908A81-8687-4E93-999F-D86FAB284BA0}
IID_ICOMTestDouble = NewGUID("{BF908A81-8687-4E93-999F-D86FAB284BA0}")
// IID_ICOMTestBoolean is for ICOMTestBoolean interfaces.
//
// {D530E7A6-4EE8-40D1-8931-3D63B8605010}
IID_ICOMTestBoolean = NewGUID("{D530E7A6-4EE8-40D1-8931-3D63B8605010}")
// IID_ICOMEchoTestObject is for ICOMEchoTestObject interfaces.
//
// {6485B1EF-D780-4834-A4FE-1EBB51746CA3}
IID_ICOMEchoTestObject = NewGUID("{6485B1EF-D780-4834-A4FE-1EBB51746CA3}")
// IID_ICOMTestTypes is for ICOMTestTypes interfaces.
//
// {CCA8D7AE-91C0-4277-A8B3-FF4EDF28D3C0}
IID_ICOMTestTypes = NewGUID("{CCA8D7AE-91C0-4277-A8B3-FF4EDF28D3C0}")
// CLSID_COMEchoTestObject is for COMEchoTestObject class.
//
// {3C24506A-AE9E-4D50-9157-EF317281F1B0}
CLSID_COMEchoTestObject = NewGUID("{3C24506A-AE9E-4D50-9157-EF317281F1B0}")
// CLSID_COMTestScalarClass is for COMTestScalarClass class.
//
// {865B85C5-0334-4AC6-9EF6-AACEC8FC5E86}
CLSID_COMTestScalarClass = NewGUID("{865B85C5-0334-4AC6-9EF6-AACEC8FC5E86}")
)
const hextable = "0123456789ABCDEF"
const emptyGUID = "{00000000-0000-0000-0000-000000000000}"
// GUID is Windows API specific GUID type.
//
// This exists to match Windows GUID type for direct passing for COM.
// Format is in xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx.
type GUID struct {
Data1 uint32
Data2 uint16
Data3 uint16
Data4 [8]byte
}
// NewGUID converts the given string into a globally unique identifier that is
// compliant with the Windows API.
//
// The supplied string may be in any of these formats:
//
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
//
// The conversion of the supplied string is not case-sensitive.
func NewGUID(guid string) *GUID {
d := []byte(guid)
var d1, d2, d3, d4a, d4b []byte
switch len(d) {
case 38:
if d[0] != '{' || d[37] != '}' {
return nil
}
d = d[1:37]
fallthrough
case 36:
if d[8] != '-' || d[13] != '-' || d[18] != '-' || d[23] != '-' {
return nil
}
d1 = d[0:8]
d2 = d[9:13]
d3 = d[14:18]
d4a = d[19:23]
d4b = d[24:36]
case 32:
d1 = d[0:8]
d2 = d[8:12]
d3 = d[12:16]
d4a = d[16:20]
d4b = d[20:32]
default:
return nil
}
var g GUID
var ok1, ok2, ok3, ok4 bool
g.Data1, ok1 = decodeHexUint32(d1)
g.Data2, ok2 = decodeHexUint16(d2)
g.Data3, ok3 = decodeHexUint16(d3)
g.Data4, ok4 = decodeHexByte64(d4a, d4b)
if ok1 && ok2 && ok3 && ok4 {
return &g
}
return nil
}
func decodeHexUint32(src []byte) (value uint32, ok bool) {
var b1, b2, b3, b4 byte
var ok1, ok2, ok3, ok4 bool
b1, ok1 = decodeHexByte(src[0], src[1])
b2, ok2 = decodeHexByte(src[2], src[3])
b3, ok3 = decodeHexByte(src[4], src[5])
b4, ok4 = decodeHexByte(src[6], src[7])
value = (uint32(b1) << 24) | (uint32(b2) << 16) | (uint32(b3) << 8) | uint32(b4)
ok = ok1 && ok2 && ok3 && ok4
return
}
func decodeHexUint16(src []byte) (value uint16, ok bool) {
var b1, b2 byte
var ok1, ok2 bool
b1, ok1 = decodeHexByte(src[0], src[1])
b2, ok2 = decodeHexByte(src[2], src[3])
value = (uint16(b1) << 8) | uint16(b2)
ok = ok1 && ok2
return
}
func decodeHexByte64(s1 []byte, s2 []byte) (value [8]byte, ok bool) {
var ok1, ok2, ok3, ok4, ok5, ok6, ok7, ok8 bool
value[0], ok1 = decodeHexByte(s1[0], s1[1])
value[1], ok2 = decodeHexByte(s1[2], s1[3])
value[2], ok3 = decodeHexByte(s2[0], s2[1])
value[3], ok4 = decodeHexByte(s2[2], s2[3])
value[4], ok5 = decodeHexByte(s2[4], s2[5])
value[5], ok6 = decodeHexByte(s2[6], s2[7])
value[6], ok7 = decodeHexByte(s2[8], s2[9])
value[7], ok8 = decodeHexByte(s2[10], s2[11])
ok = ok1 && ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8
return
}
func decodeHexByte(c1, c2 byte) (value byte, ok bool) {
var n1, n2 byte
var ok1, ok2 bool
n1, ok1 = decodeHexChar(c1)
n2, ok2 = decodeHexChar(c2)
value = (n1 << 4) | n2
ok = ok1 && ok2
return
}
func decodeHexChar(c byte) (byte, bool) {
switch {
case '0' <= c && c <= '9':
return c - '0', true
case 'a' <= c && c <= 'f':
return c - 'a' + 10, true
case 'A' <= c && c <= 'F':
return c - 'A' + 10, true
}
return 0, false
}
// String converts the GUID to string form. It will adhere to this pattern:
//
// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
//
// If the GUID is nil, the string representation of an empty GUID is returned:
//
// {00000000-0000-0000-0000-000000000000}
func (guid *GUID) String() string {
if guid == nil {
return emptyGUID
}
var c [38]byte
c[0] = '{'
putUint32Hex(c[1:9], guid.Data1)
c[9] = '-'
putUint16Hex(c[10:14], guid.Data2)
c[14] = '-'
putUint16Hex(c[15:19], guid.Data3)
c[19] = '-'
putByteHex(c[20:24], guid.Data4[0:2])
c[24] = '-'
putByteHex(c[25:37], guid.Data4[2:8])
c[37] = '}'
return string(c[:])
}
func putUint32Hex(b []byte, v uint32) {
b[0] = hextable[byte(v>>24)>>4]
b[1] = hextable[byte(v>>24)&0x0f]
b[2] = hextable[byte(v>>16)>>4]
b[3] = hextable[byte(v>>16)&0x0f]
b[4] = hextable[byte(v>>8)>>4]
b[5] = hextable[byte(v>>8)&0x0f]
b[6] = hextable[byte(v)>>4]
b[7] = hextable[byte(v)&0x0f]
}
func putUint16Hex(b []byte, v uint16) {
b[0] = hextable[byte(v>>8)>>4]
b[1] = hextable[byte(v>>8)&0x0f]
b[2] = hextable[byte(v)>>4]
b[3] = hextable[byte(v)&0x0f]
}
func putByteHex(dst, src []byte) {
for i := 0; i < len(src); i++ {
dst[i*2] = hextable[src[i]>>4]
dst[i*2+1] = hextable[src[i]&0x0f]
}
}
// IsEqualGUID compares two GUID.
//
// Not constant time comparison.
func IsEqualGUID(guid1 *GUID, guid2 *GUID) bool {
return guid1.Data1 == guid2.Data1 &&
guid1.Data2 == guid2.Data2 &&
guid1.Data3 == guid2.Data3 &&
guid1.Data4[0] == guid2.Data4[0] &&
guid1.Data4[1] == guid2.Data4[1] &&
guid1.Data4[2] == guid2.Data4[2] &&
guid1.Data4[3] == guid2.Data4[3] &&
guid1.Data4[4] == guid2.Data4[4] &&
guid1.Data4[5] == guid2.Data4[5] &&
guid1.Data4[6] == guid2.Data4[6] &&
guid1.Data4[7] == guid2.Data4[7]
}
package ole
import "unsafe"
type IConnectionPoint struct {
IUnknown
}
type IConnectionPointVtbl struct {
IUnknownVtbl
GetConnectionInterface uintptr
GetConnectionPointContainer uintptr
Advise uintptr
Unadvise uintptr
EnumConnections uintptr
}
func (v *IConnectionPoint) VTable() *IConnectionPointVtbl {
return (*IConnectionPointVtbl)(unsafe.Pointer(v.RawVTable))
}
// +build !windows
package ole
import "unsafe"
func (v *IConnectionPoint) GetConnectionInterface(piid **GUID) int32 {
return int32(0)
}
func (v *IConnectionPoint) Advise(unknown *IUnknown) (uint32, error) {
return uint32(0), NewError(E_NOTIMPL)
}
func (v *IConnectionPoint) Unadvise(cookie uint32) error {
return NewError(E_NOTIMPL)
}
func (v *IConnectionPoint) EnumConnections(p *unsafe.Pointer) (err error) {
return NewError(E_NOTIMPL)
}
// +build windows
package ole
import (
"syscall"
"unsafe"
)
func (v *IConnectionPoint) GetConnectionInterface(piid **GUID) int32 {
// XXX: This doesn't look like it does what it's supposed to
return release((*IUnknown)(unsafe.Pointer(v)))
}
func (v *IConnectionPoint) Advise(unknown *IUnknown) (cookie uint32, err error) {
hr, _, _ := syscall.Syscall(
v.VTable().Advise,
3,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(unknown)),
uintptr(unsafe.Pointer(&cookie)))
if hr != 0 {
err = NewError(hr)
}
return
}
func (v *IConnectionPoint) Unadvise(cookie uint32) (err error) {
hr, _, _ := syscall.Syscall(
v.VTable().Unadvise,
2,
uintptr(unsafe.Pointer(v)),
uintptr(cookie),
0)
if hr != 0 {
err = NewError(hr)
}
return
}
func (v *IConnectionPoint) EnumConnections(p *unsafe.Pointer) error {
return NewError(E_NOTIMPL)
}
package ole
import "unsafe"
type IConnectionPointContainer struct {
IUnknown
}
type IConnectionPointContainerVtbl struct {
IUnknownVtbl
EnumConnectionPoints uintptr
FindConnectionPoint uintptr
}
func (v *IConnectionPointContainer) VTable() *IConnectionPointContainerVtbl {
return (*IConnectionPointContainerVtbl)(unsafe.Pointer(v.RawVTable))
}
// +build !windows
package ole
func (v *IConnectionPointContainer) EnumConnectionPoints(points interface{}) error {
return NewError(E_NOTIMPL)
}
func (v *IConnectionPointContainer) FindConnectionPoint(iid *GUID, point **IConnectionPoint) error {
return NewError(E_NOTIMPL)
}
// +build windows
package ole
import (
"syscall"
"unsafe"
)
func (v *IConnectionPointContainer) EnumConnectionPoints(points interface{}) error {
return NewError(E_NOTIMPL)
}
func (v *IConnectionPointContainer) FindConnectionPoint(iid *GUID, point **IConnectionPoint) (err error) {
hr, _, _ := syscall.Syscall(
v.VTable().FindConnectionPoint,
3,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(iid)),
uintptr(unsafe.Pointer(point)))
if hr != 0 {
err = NewError(hr)
}
return
}
package ole
import "unsafe"
type IDispatch struct {
IUnknown
}
type IDispatchVtbl struct {
IUnknownVtbl
GetTypeInfoCount uintptr
GetTypeInfo uintptr
GetIDsOfNames uintptr
Invoke uintptr
}
func (v *IDispatch) VTable() *IDispatchVtbl {
return (*IDispatchVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IDispatch) GetIDsOfName(names []string) (dispid []int32, err error) {
dispid, err = getIDsOfName(v, names)
return
}
func (v *IDispatch) Invoke(dispid int32, dispatch int16, params ...interface{}) (result *VARIANT, err error) {
result, err = invoke(v, dispid, dispatch, params...)
return
}
func (v *IDispatch) GetTypeInfoCount() (c uint32, err error) {
c, err = getTypeInfoCount(v)
return
}
func (v *IDispatch) GetTypeInfo() (tinfo *ITypeInfo, err error) {
tinfo, err = getTypeInfo(v)
return
}
// GetSingleIDOfName is a helper that returns single display ID for IDispatch name.
//
// This replaces the common pattern of attempting to get a single name from the list of available
// IDs. It gives the first ID, if it is available.
func (v *IDispatch) GetSingleIDOfName(name string) (displayID int32, err error) {
var displayIDs []int32
displayIDs, err = v.GetIDsOfName([]string{name})
if err != nil {
return
}
displayID = displayIDs[0]
return
}
// InvokeWithOptionalArgs accepts arguments as an array, works like Invoke.
//
// Accepts name and will attempt to retrieve Display ID to pass to Invoke.
//
// Passing params as an array is a workaround that could be fixed in later versions of Go that
// prevent passing empty params. During testing it was discovered that this is an acceptable way of
// getting around not being able to pass params normally.
func (v *IDispatch) InvokeWithOptionalArgs(name string, dispatch int16, params []interface{}) (result *VARIANT, err error) {
displayID, err := v.GetSingleIDOfName(name)
if err != nil {
return
}
if len(params) < 1 {
result, err = v.Invoke(displayID, dispatch)
} else {
result, err = v.Invoke(displayID, dispatch, params...)
}
return
}
// CallMethod invokes named function with arguments on object.
func (v *IDispatch) CallMethod(name string, params ...interface{}) (*VARIANT, error) {
return v.InvokeWithOptionalArgs(name, DISPATCH_METHOD, params)
}
// GetProperty retrieves the property with the name with the ability to pass arguments.
//
// Most of the time you will not need to pass arguments as most objects do not allow for this
// feature. Or at least, should not allow for this feature. Some servers don't follow best practices
// and this is provided for those edge cases.
func (v *IDispatch) GetProperty(name string, params ...interface{}) (*VARIANT, error) {
return v.InvokeWithOptionalArgs(name, DISPATCH_PROPERTYGET, params)
}
// PutProperty attempts to mutate a property in the object.
func (v *IDispatch) PutProperty(name string, params ...interface{}) (*VARIANT, error) {
return v.InvokeWithOptionalArgs(name, DISPATCH_PROPERTYPUT, params)
}
// +build !windows
package ole
func getIDsOfName(disp *IDispatch, names []string) ([]int32, error) {
return []int32{}, NewError(E_NOTIMPL)
}
func getTypeInfoCount(disp *IDispatch) (uint32, error) {
return uint32(0), NewError(E_NOTIMPL)
}
func getTypeInfo(disp *IDispatch) (*ITypeInfo, error) {
return nil, NewError(E_NOTIMPL)
}
func invoke(disp *IDispatch, dispid int32, dispatch int16, params ...interface{}) (*VARIANT, error) {
return nil, NewError(E_NOTIMPL)
}
// +build windows
package ole
import (
"math/big"
"syscall"
"time"
"unsafe"
)
func getIDsOfName(disp *IDispatch, names []string) (dispid []int32, err error) {
wnames := make([]*uint16, len(names))
for i := 0; i < len(names); i++ {
wnames[i] = syscall.StringToUTF16Ptr(names[i])
}
dispid = make([]int32, len(names))
namelen := uint32(len(names))
hr, _, _ := syscall.Syscall6(
disp.VTable().GetIDsOfNames,
6,
uintptr(unsafe.Pointer(disp)),
uintptr(unsafe.Pointer(IID_NULL)),
uintptr(unsafe.Pointer(&wnames[0])),
uintptr(namelen),
uintptr(GetUserDefaultLCID()),
uintptr(unsafe.Pointer(&dispid[0])))
if hr != 0 {
err = NewError(hr)
}
return
}
func getTypeInfoCount(disp *IDispatch) (c uint32, err error) {
hr, _, _ := syscall.Syscall(
disp.VTable().GetTypeInfoCount,
2,
uintptr(unsafe.Pointer(disp)),
uintptr(unsafe.Pointer(&c)),
0)
if hr != 0 {
err = NewError(hr)
}
return
}
func getTypeInfo(disp *IDispatch) (tinfo *ITypeInfo, err error) {
hr, _, _ := syscall.Syscall(
disp.VTable().GetTypeInfo,
3,
uintptr(unsafe.Pointer(disp)),
uintptr(GetUserDefaultLCID()),
uintptr(unsafe.Pointer(&tinfo)))
if hr != 0 {
err = NewError(hr)
}
return
}
func invoke(disp *IDispatch, dispid int32, dispatch int16, params ...interface{}) (result *VARIANT, err error) {
var dispparams DISPPARAMS
if dispatch&DISPATCH_PROPERTYPUT != 0 {
dispnames := [1]int32{DISPID_PROPERTYPUT}
dispparams.rgdispidNamedArgs = uintptr(unsafe.Pointer(&dispnames[0]))
dispparams.cNamedArgs = 1
} else if dispatch&DISPATCH_PROPERTYPUTREF != 0 {
dispnames := [1]int32{DISPID_PROPERTYPUT}
dispparams.rgdispidNamedArgs = uintptr(unsafe.Pointer(&dispnames[0]))
dispparams.cNamedArgs = 1
}
var vargs []VARIANT
if len(params) > 0 {
vargs = make([]VARIANT, len(params))
for i, v := range params {
//n := len(params)-i-1
n := len(params) - i - 1
VariantInit(&vargs[n])
switch vv := v.(type) {
case bool:
if vv {
vargs[n] = NewVariant(VT_BOOL, 0xffff)
} else {
vargs[n] = NewVariant(VT_BOOL, 0)
}
case *bool:
vargs[n] = NewVariant(VT_BOOL|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*bool)))))
case uint8:
vargs[n] = NewVariant(VT_I1, int64(v.(uint8)))
case *uint8:
vargs[n] = NewVariant(VT_I1|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint8)))))
case int8:
vargs[n] = NewVariant(VT_I1, int64(v.(int8)))
case *int8:
vargs[n] = NewVariant(VT_I1|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint8)))))
case int16:
vargs[n] = NewVariant(VT_I2, int64(v.(int16)))
case *int16:
vargs[n] = NewVariant(VT_I2|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int16)))))
case uint16:
vargs[n] = NewVariant(VT_UI2, int64(v.(uint16)))
case *uint16:
vargs[n] = NewVariant(VT_UI2|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint16)))))
case int32:
vargs[n] = NewVariant(VT_I4, int64(v.(int32)))
case *int32:
vargs[n] = NewVariant(VT_I4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int32)))))
case uint32:
vargs[n] = NewVariant(VT_UI4, int64(v.(uint32)))
case *uint32:
vargs[n] = NewVariant(VT_UI4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint32)))))
case int64:
vargs[n] = NewVariant(VT_I8, int64(v.(int64)))
case *int64:
vargs[n] = NewVariant(VT_I8|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int64)))))
case uint64:
vargs[n] = NewVariant(VT_UI8, int64(uintptr(v.(uint64))))
case *uint64:
vargs[n] = NewVariant(VT_UI8|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint64)))))
case int:
vargs[n] = NewVariant(VT_I4, int64(v.(int)))
case *int:
vargs[n] = NewVariant(VT_I4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int)))))
case uint:
vargs[n] = NewVariant(VT_UI4, int64(v.(uint)))
case *uint:
vargs[n] = NewVariant(VT_UI4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint)))))
case float32:
vargs[n] = NewVariant(VT_R4, *(*int64)(unsafe.Pointer(&vv)))
case *float32:
vargs[n] = NewVariant(VT_R4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*float32)))))
case float64:
vargs[n] = NewVariant(VT_R8, *(*int64)(unsafe.Pointer(&vv)))
case *float64:
vargs[n] = NewVariant(VT_R8|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*float64)))))
case *big.Int:
vargs[n] = NewVariant(VT_DECIMAL, v.(*big.Int).Int64())
case string:
vargs[n] = NewVariant(VT_BSTR, int64(uintptr(unsafe.Pointer(SysAllocStringLen(v.(string))))))
case *string:
vargs[n] = NewVariant(VT_BSTR|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*string)))))
case time.Time:
s := vv.Format("2006-01-02 15:04:05")
vargs[n] = NewVariant(VT_BSTR, int64(uintptr(unsafe.Pointer(SysAllocStringLen(s)))))
case *time.Time:
s := vv.Format("2006-01-02 15:04:05")
vargs[n] = NewVariant(VT_BSTR|VT_BYREF, int64(uintptr(unsafe.Pointer(&s))))
case *IDispatch:
vargs[n] = NewVariant(VT_DISPATCH, int64(uintptr(unsafe.Pointer(v.(*IDispatch)))))
case **IDispatch:
vargs[n] = NewVariant(VT_DISPATCH|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(**IDispatch)))))
case nil:
vargs[n] = NewVariant(VT_NULL, 0)
case *VARIANT:
vargs[n] = NewVariant(VT_VARIANT|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*VARIANT)))))
case []byte:
safeByteArray := safeArrayFromByteSlice(v.([]byte))
vargs[n] = NewVariant(VT_ARRAY|VT_UI1, int64(uintptr(unsafe.Pointer(safeByteArray))))
defer VariantClear(&vargs[n])
case []string:
safeByteArray := safeArrayFromStringSlice(v.([]string))
vargs[n] = NewVariant(VT_ARRAY|VT_BSTR, int64(uintptr(unsafe.Pointer(safeByteArray))))
defer VariantClear(&vargs[n])
default:
panic("unknown type")
}
}
dispparams.rgvarg = uintptr(unsafe.Pointer(&vargs[0]))
dispparams.cArgs = uint32(len(params))
}
result = new(VARIANT)
var excepInfo EXCEPINFO
VariantInit(result)
hr, _, _ := syscall.Syscall9(
disp.VTable().Invoke,
9,
uintptr(unsafe.Pointer(disp)),
uintptr(dispid),
uintptr(unsafe.Pointer(IID_NULL)),
uintptr(GetUserDefaultLCID()),
uintptr(dispatch),
uintptr(unsafe.Pointer(&dispparams)),
uintptr(unsafe.Pointer(result)),
uintptr(unsafe.Pointer(&excepInfo)),
0)
if hr != 0 {
err = NewErrorWithSubError(hr, BstrToString(excepInfo.bstrDescription), excepInfo)
}
for i, varg := range vargs {
n := len(params) - i - 1
if varg.VT == VT_BSTR && varg.Val != 0 {
SysFreeString(((*int16)(unsafe.Pointer(uintptr(varg.Val)))))
}
if varg.VT == (VT_BSTR|VT_BYREF) && varg.Val != 0 {
*(params[n].(*string)) = LpOleStrToString(*(**uint16)(unsafe.Pointer(uintptr(varg.Val))))
}
}
return
}
package ole
import "unsafe"
type IEnumVARIANT struct {
IUnknown
}
type IEnumVARIANTVtbl struct {
IUnknownVtbl
Next uintptr
Skip uintptr
Reset uintptr
Clone uintptr
}
func (v *IEnumVARIANT) VTable() *IEnumVARIANTVtbl {
return (*IEnumVARIANTVtbl)(unsafe.Pointer(v.RawVTable))
}
// +build !windows
package ole
func (enum *IEnumVARIANT) Clone() (*IEnumVARIANT, error) {
return nil, NewError(E_NOTIMPL)
}
func (enum *IEnumVARIANT) Reset() error {
return NewError(E_NOTIMPL)
}
func (enum *IEnumVARIANT) Skip(celt uint) error {
return NewError(E_NOTIMPL)
}
func (enum *IEnumVARIANT) Next(celt uint) (VARIANT, uint, error) {
return NewVariant(VT_NULL, int64(0)), 0, NewError(E_NOTIMPL)
}
// +build windows
package ole
import (
"syscall"
"unsafe"
)
func (enum *IEnumVARIANT) Clone() (cloned *IEnumVARIANT, err error) {
hr, _, _ := syscall.Syscall(
enum.VTable().Clone,
2,
uintptr(unsafe.Pointer(enum)),
uintptr(unsafe.Pointer(&cloned)),
0)
if hr != 0 {
err = NewError(hr)
}
return
}
func (enum *IEnumVARIANT) Reset() (err error) {
hr, _, _ := syscall.Syscall(
enum.VTable().Reset,
1,
uintptr(unsafe.Pointer(enum)),
0,
0)
if hr != 0 {
err = NewError(hr)
}
return
}
func (enum *IEnumVARIANT) Skip(celt uint) (err error) {
hr, _, _ := syscall.Syscall(
enum.VTable().Skip,
2,
uintptr(unsafe.Pointer(enum)),
uintptr(celt),
0)
if hr != 0 {
err = NewError(hr)
}
return
}
func (enum *IEnumVARIANT) Next(celt uint) (array VARIANT, length uint, err error) {
hr, _, _ := syscall.Syscall6(
enum.VTable().Next,
4,
uintptr(unsafe.Pointer(enum)),
uintptr(celt),
uintptr(unsafe.Pointer(&array)),
uintptr(unsafe.Pointer(&length)),
0,
0)
if hr != 0 {
err = NewError(hr)
}
return
}
package ole
import "unsafe"
type IInspectable struct {
IUnknown
}
type IInspectableVtbl struct {
IUnknownVtbl
GetIIds uintptr
GetRuntimeClassName uintptr
GetTrustLevel uintptr
}
func (v *IInspectable) VTable() *IInspectableVtbl {
return (*IInspectableVtbl)(unsafe.Pointer(v.RawVTable))
}
// +build !windows
package ole
func (v *IInspectable) GetIids() ([]*GUID, error) {
return []*GUID{}, NewError(E_NOTIMPL)
}
func (v *IInspectable) GetRuntimeClassName() (string, error) {
return "", NewError(E_NOTIMPL)
}
func (v *IInspectable) GetTrustLevel() (uint32, error) {
return uint32(0), NewError(E_NOTIMPL)
}
此差异已折叠。
package ole
import "unsafe"
type IProvideClassInfo struct {
IUnknown
}
type IProvideClassInfoVtbl struct {
IUnknownVtbl
GetClassInfo uintptr
}
func (v *IProvideClassInfo) VTable() *IProvideClassInfoVtbl {
return (*IProvideClassInfoVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IProvideClassInfo) GetClassInfo() (cinfo *ITypeInfo, err error) {
cinfo, err = getClassInfo(v)
return
}
// +build !windows
package ole
func getClassInfo(disp *IProvideClassInfo) (tinfo *ITypeInfo, err error) {
return nil, NewError(E_NOTIMPL)
}
// +build windows
package ole
import (
"syscall"
"unsafe"
)
func getClassInfo(disp *IProvideClassInfo) (tinfo *ITypeInfo, err error) {
hr, _, _ := syscall.Syscall(
disp.VTable().GetClassInfo,
2,
uintptr(unsafe.Pointer(disp)),
uintptr(unsafe.Pointer(&tinfo)),
0)
if hr != 0 {
err = NewError(hr)
}
return
}
此差异已折叠。
// +build !windows
package ole
func (v *ITypeInfo) GetTypeAttr() (*TYPEATTR, error) {
return nil, NewError(E_NOTIMPL)
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册