diff options
author | sina <sina@snix.ir> | 2022-07-30 19:16:47 +0430 |
---|---|---|
committer | sina <sina@snix.ir> | 2022-07-30 19:16:47 +0430 |
commit | 6bdd58a59dc2c03b82c63fe89a729bd161192d71 (patch) | |
tree | d0b9a88f567796459df847a1cd608c6f02a366c8 /subtle | |
parent | e697fd436cabccb32114ebb74d1de9b9516dead3 (diff) |
subtle package
Diffstat (limited to 'subtle')
-rw-r--r-- | subtle/overlap.go | 18 | ||||
-rw-r--r-- | subtle/purelap.go | 18 |
2 files changed, 36 insertions, 0 deletions
diff --git a/subtle/overlap.go b/subtle/overlap.go new file mode 100644 index 0000000..82ab250 --- /dev/null +++ b/subtle/overlap.go @@ -0,0 +1,18 @@ +//go:build !purego + +package subtle + +import "unsafe" + +func AnyOverlap(x, y []byte) bool { + return len(x) > 0 && len(y) > 0 && + uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) && + uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1])) +} + +func InexactOverlap(x, y []byte) bool { + if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] { + return false + } + return AnyOverlap(x, y) +} diff --git a/subtle/purelap.go b/subtle/purelap.go new file mode 100644 index 0000000..e983129 --- /dev/null +++ b/subtle/purelap.go @@ -0,0 +1,18 @@ +//go:build purego + +package subtle + +import "reflect" + +func AnyOverlap(x, y []byte) bool { + return len(x) > 0 && len(y) > 0 && + reflect.ValueOf(&x[0]).Pointer() <= reflect.ValueOf(&y[len(y)-1]).Pointer() && + reflect.ValueOf(&y[0]).Pointer() <= reflect.ValueOf(&x[len(x)-1]).Pointer() +} + +func InexactOverlap(x, y []byte) bool { + if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] { + return false + } + return AnyOverlap(x, y) +} |