github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/crypto/x509/internal/macos/corefoundation.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build darwin && !ios
     6  // +build darwin,!ios
     7  
     8  // Package macOS provides cgo-less wrappers for Core Foundation and
     9  // Security.framework, similarly to how package syscall provides access to
    10  // libSystem.dylib.
    11  package macOS
    12  
    13  import (
    14  	"errors"
    15  	"internal/abi"
    16  	"reflect"
    17  	"runtime"
    18  	"unsafe"
    19  )
    20  
    21  // Core Foundation linker flags for the external linker. See Issue 42459.
    22  //go:cgo_ldflag "-framework"
    23  //go:cgo_ldflag "CoreFoundation"
    24  
    25  // CFRef is an opaque reference to a Core Foundation object. It is a pointer,
    26  // but to memory not owned by Go, so not an unsafe.Pointer.
    27  type CFRef uintptr
    28  
    29  // CFDataToSlice returns a copy of the contents of data as a bytes slice.
    30  func CFDataToSlice(data CFRef) []byte {
    31  	length := CFDataGetLength(data)
    32  	ptr := CFDataGetBytePtr(data)
    33  	src := (*[1 << 20]byte)(unsafe.Pointer(ptr))[:length:length]
    34  	out := make([]byte, length)
    35  	copy(out, src)
    36  	return out
    37  }
    38  
    39  type CFString CFRef
    40  
    41  const kCFAllocatorDefault = 0
    42  const kCFStringEncodingUTF8 = 0x08000100
    43  
    44  //go:cgo_import_dynamic x509_CFStringCreateWithBytes CFStringCreateWithBytes "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
    45  
    46  // StringToCFString returns a copy of the UTF-8 contents of s as a new CFString.
    47  func StringToCFString(s string) CFString {
    48  	p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
    49  	ret := syscall(abi.FuncPCABI0(x509_CFStringCreateWithBytes_trampoline), kCFAllocatorDefault, uintptr(p),
    50  		uintptr(len(s)), uintptr(kCFStringEncodingUTF8), 0 /* isExternalRepresentation */, 0)
    51  	runtime.KeepAlive(p)
    52  	return CFString(ret)
    53  }
    54  func x509_CFStringCreateWithBytes_trampoline()
    55  
    56  //go:cgo_import_dynamic x509_CFDictionaryGetValueIfPresent CFDictionaryGetValueIfPresent "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
    57  
    58  func CFDictionaryGetValueIfPresent(dict CFRef, key CFString) (value CFRef, ok bool) {
    59  	ret := syscall(abi.FuncPCABI0(x509_CFDictionaryGetValueIfPresent_trampoline), uintptr(dict), uintptr(key),
    60  		uintptr(unsafe.Pointer(&value)), 0, 0, 0)
    61  	if ret == 0 {
    62  		return 0, false
    63  	}
    64  	return value, true
    65  }
    66  func x509_CFDictionaryGetValueIfPresent_trampoline()
    67  
    68  const kCFNumberSInt32Type = 3
    69  
    70  //go:cgo_import_dynamic x509_CFNumberGetValue CFNumberGetValue "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
    71  
    72  func CFNumberGetValue(num CFRef) (int32, error) {
    73  	var value int32
    74  	ret := syscall(abi.FuncPCABI0(x509_CFNumberGetValue_trampoline), uintptr(num), uintptr(kCFNumberSInt32Type),
    75  		uintptr(unsafe.Pointer(&value)), 0, 0, 0)
    76  	if ret == 0 {
    77  		return 0, errors.New("CFNumberGetValue call failed")
    78  	}
    79  	return value, nil
    80  }
    81  func x509_CFNumberGetValue_trampoline()
    82  
    83  //go:cgo_import_dynamic x509_CFDataGetLength CFDataGetLength "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
    84  
    85  func CFDataGetLength(data CFRef) int {
    86  	ret := syscall(abi.FuncPCABI0(x509_CFDataGetLength_trampoline), uintptr(data), 0, 0, 0, 0, 0)
    87  	return int(ret)
    88  }
    89  func x509_CFDataGetLength_trampoline()
    90  
    91  //go:cgo_import_dynamic x509_CFDataGetBytePtr CFDataGetBytePtr "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
    92  
    93  func CFDataGetBytePtr(data CFRef) uintptr {
    94  	ret := syscall(abi.FuncPCABI0(x509_CFDataGetBytePtr_trampoline), uintptr(data), 0, 0, 0, 0, 0)
    95  	return ret
    96  }
    97  func x509_CFDataGetBytePtr_trampoline()
    98  
    99  //go:cgo_import_dynamic x509_CFArrayGetCount CFArrayGetCount "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
   100  
   101  func CFArrayGetCount(array CFRef) int {
   102  	ret := syscall(abi.FuncPCABI0(x509_CFArrayGetCount_trampoline), uintptr(array), 0, 0, 0, 0, 0)
   103  	return int(ret)
   104  }
   105  func x509_CFArrayGetCount_trampoline()
   106  
   107  //go:cgo_import_dynamic x509_CFArrayGetValueAtIndex CFArrayGetValueAtIndex "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
   108  
   109  func CFArrayGetValueAtIndex(array CFRef, index int) CFRef {
   110  	ret := syscall(abi.FuncPCABI0(x509_CFArrayGetValueAtIndex_trampoline), uintptr(array), uintptr(index), 0, 0, 0, 0)
   111  	return CFRef(ret)
   112  }
   113  func x509_CFArrayGetValueAtIndex_trampoline()
   114  
   115  //go:cgo_import_dynamic x509_CFEqual CFEqual "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
   116  
   117  func CFEqual(a, b CFRef) bool {
   118  	ret := syscall(abi.FuncPCABI0(x509_CFEqual_trampoline), uintptr(a), uintptr(b), 0, 0, 0, 0)
   119  	return ret == 1
   120  }
   121  func x509_CFEqual_trampoline()
   122  
   123  //go:cgo_import_dynamic x509_CFRelease CFRelease "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
   124  
   125  func CFRelease(ref CFRef) {
   126  	syscall(abi.FuncPCABI0(x509_CFRelease_trampoline), uintptr(ref), 0, 0, 0, 0, 0)
   127  }
   128  func x509_CFRelease_trampoline()
   129  
   130  // syscall is implemented in the runtime package (runtime/sys_darwin.go)
   131  func syscall(fn, a1, a2, a3, a4, a5, a6 uintptr) uintptr