zy.go 1.0 KB

123456789101112131415161718192021222324252627282930
  1. /*
  2. * Copyright (c) 2000-2018, 达梦数据库有限公司.
  3. * All rights reserved.
  4. */
  5. // This is a mirror of golang.org/x/crypto/internal/subtle.
  6. package security
  7. import "unsafe"
  8. // AnyOverlap reports whether x and y share memory at any (not necessarily
  9. // corresponding) index. The memory beyond the slice length is ignored.
  10. func AnyOverlap(x, y []byte) bool {
  11. return len(x) > 0 && len(y) > 0 &&
  12. uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) &&
  13. uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1]))
  14. }
  15. // InexactOverlap reports whether x and y share memory at any non-corresponding
  16. // index. The memory beyond the slice length is ignored. Note that x and y can
  17. // have different lengths and still not have any inexact overlap.
  18. //
  19. // InexactOverlap can be used to implement the requirements of the crypto/cipher
  20. // AEAD, Block, BlockMode and Stream interfaces.
  21. func InexactOverlap(x, y []byte) bool {
  22. if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
  23. return false
  24. }
  25. return AnyOverlap(x, y)
  26. }