github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/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 "reflect" 16 "runtime" 17 "unsafe" 18 ) 19 20 // Core Foundation linker flags for the external linker. See Issue 42459. 21 //go:cgo_ldflag "-framework" 22 //go:cgo_ldflag "CoreFoundation" 23 24 // CFRef is an opaque reference to a Core Foundation object. It is a pointer, 25 // but to memory not owned by Go, so not an unsafe.Pointer. 26 type CFRef uintptr 27 28 // CFDataToSlice returns a copy of the contents of data as a bytes slice. 29 func CFDataToSlice(data CFRef) []byte { 30 length := CFDataGetLength(data) 31 ptr := CFDataGetBytePtr(data) 32 src := (*[1 << 20]byte)(unsafe.Pointer(ptr))[:length:length] 33 out := make([]byte, length) 34 copy(out, src) 35 return out 36 } 37 38 type CFString CFRef 39 40 const kCFAllocatorDefault = 0 41 const kCFStringEncodingUTF8 = 0x08000100 42 43 //go:cgo_import_dynamic x509_CFStringCreateWithBytes CFStringCreateWithBytes "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 44 45 // StringToCFString returns a copy of the UTF-8 contents of s as a new CFString. 46 func StringToCFString(s string) CFString { 47 p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data) 48 ret := syscall(funcPC(x509_CFStringCreateWithBytes_trampoline), kCFAllocatorDefault, uintptr(p), 49 uintptr(len(s)), uintptr(kCFStringEncodingUTF8), 0 /* isExternalRepresentation */, 0) 50 runtime.KeepAlive(p) 51 return CFString(ret) 52 } 53 func x509_CFStringCreateWithBytes_trampoline() 54 55 //go:cgo_import_dynamic x509_CFDictionaryGetValueIfPresent CFDictionaryGetValueIfPresent "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 56 57 func CFDictionaryGetValueIfPresent(dict CFRef, key CFString) (value CFRef, ok bool) { 58 ret := syscall(funcPC(x509_CFDictionaryGetValueIfPresent_trampoline), uintptr(dict), uintptr(key), 59 uintptr(unsafe.Pointer(&value)), 0, 0, 0) 60 if ret == 0 { 61 return 0, false 62 } 63 return value, true 64 } 65 func x509_CFDictionaryGetValueIfPresent_trampoline() 66 67 const kCFNumberSInt32Type = 3 68 69 //go:cgo_import_dynamic x509_CFNumberGetValue CFNumberGetValue "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 70 71 func CFNumberGetValue(num CFRef) (int32, error) { 72 var value int32 73 ret := syscall(funcPC(x509_CFNumberGetValue_trampoline), uintptr(num), uintptr(kCFNumberSInt32Type), 74 uintptr(unsafe.Pointer(&value)), 0, 0, 0) 75 if ret == 0 { 76 return 0, errors.New("CFNumberGetValue call failed") 77 } 78 return value, nil 79 } 80 func x509_CFNumberGetValue_trampoline() 81 82 //go:cgo_import_dynamic x509_CFDataGetLength CFDataGetLength "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 83 84 func CFDataGetLength(data CFRef) int { 85 ret := syscall(funcPC(x509_CFDataGetLength_trampoline), uintptr(data), 0, 0, 0, 0, 0) 86 return int(ret) 87 } 88 func x509_CFDataGetLength_trampoline() 89 90 //go:cgo_import_dynamic x509_CFDataGetBytePtr CFDataGetBytePtr "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 91 92 func CFDataGetBytePtr(data CFRef) uintptr { 93 ret := syscall(funcPC(x509_CFDataGetBytePtr_trampoline), uintptr(data), 0, 0, 0, 0, 0) 94 return ret 95 } 96 func x509_CFDataGetBytePtr_trampoline() 97 98 //go:cgo_import_dynamic x509_CFArrayGetCount CFArrayGetCount "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 99 100 func CFArrayGetCount(array CFRef) int { 101 ret := syscall(funcPC(x509_CFArrayGetCount_trampoline), uintptr(array), 0, 0, 0, 0, 0) 102 return int(ret) 103 } 104 func x509_CFArrayGetCount_trampoline() 105 106 //go:cgo_import_dynamic x509_CFArrayGetValueAtIndex CFArrayGetValueAtIndex "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 107 108 func CFArrayGetValueAtIndex(array CFRef, index int) CFRef { 109 ret := syscall(funcPC(x509_CFArrayGetValueAtIndex_trampoline), uintptr(array), uintptr(index), 0, 0, 0, 0) 110 return CFRef(ret) 111 } 112 func x509_CFArrayGetValueAtIndex_trampoline() 113 114 //go:cgo_import_dynamic x509_CFEqual CFEqual "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 115 116 func CFEqual(a, b CFRef) bool { 117 ret := syscall(funcPC(x509_CFEqual_trampoline), uintptr(a), uintptr(b), 0, 0, 0, 0) 118 return ret == 1 119 } 120 func x509_CFEqual_trampoline() 121 122 //go:cgo_import_dynamic x509_CFRelease CFRelease "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation" 123 124 func CFRelease(ref CFRef) { 125 syscall(funcPC(x509_CFRelease_trampoline), uintptr(ref), 0, 0, 0, 0, 0) 126 } 127 func x509_CFRelease_trampoline() 128 129 // syscall is implemented in the runtime package (runtime/sys_darwin.go) 130 func syscall(fn, a1, a2, a3, a4, a5, a6 uintptr) uintptr 131 132 // funcPC returns the entry point for f. See comments in runtime/proc.go 133 // for the function of the same name. 134 //go:nosplit 135 func funcPC(f func()) uintptr { 136 return **(**uintptr)(unsafe.Pointer(&f)) 137 }