Created
November 8, 2016 22:50
-
-
Save groob/34da9aa005dfc5a3360f65744ae52861 to your computer and use it in GitHub Desktop.
read safari homepage in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#cgo LDFLAGS: -framework IOKit -framework ApplicationServices | |
#include <CoreFoundation/CoreFoundation.h> | |
#include <stdlib.h> | |
// cfstring_utf8_length returns the number of characters successfully converted to UTF-8 and | |
// the bytes required to store them. | |
static inline CFIndex cfstring_utf8_length(CFStringRef str, CFIndex *need) { | |
CFIndex n, usedBufLen; | |
CFRange rng = CFRangeMake(0, CFStringGetLength(str)); | |
return CFStringGetBytes(str, rng, kCFStringEncodingUTF8, 0, 0, NULL, 0, need); | |
} | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"reflect" | |
"unsafe" | |
) | |
// cfstringGo creates a Go string for a CoreFoundation string using the CoreFoundation UTF-8 converter. | |
// For short strings this is an efficiency nightmare! In this package this function is not currently used | |
// in any critical path. | |
func cfstringGo(cfs C.CFStringRef) string { | |
var usedBufLen C.CFIndex | |
n := C.cfstring_utf8_length(cfs, &usedBufLen) | |
if n <= 0 { | |
return "" | |
} | |
rng := C.CFRange{location: C.CFIndex(0), length: n} | |
buf := make([]byte, int(usedBufLen)) | |
bufp := unsafe.Pointer(&buf[0]) | |
C.CFStringGetBytes(cfs, rng, C.kCFStringEncodingUTF8, 0, 0, (*C.UInt8)(bufp), C.CFIndex(len(buf)), &usedBufLen) | |
sh := &reflect.StringHeader{ | |
Data: uintptr(bufp), | |
Len: int(usedBufLen), | |
} | |
return *(*string)(unsafe.Pointer(sh)) | |
} | |
// cfstring efficiently creates a CFString from a Go String. | |
func cfstring(s string) C.CFStringRef { | |
n := C.CFIndex(len(s)) | |
return C.CFStringCreateWithBytes(nil, *(**C.UInt8)(unsafe.Pointer(&s)), n, C.kCFStringEncodingUTF8, 0) | |
} | |
func main() { | |
hp := cfstring("HomePage") | |
saf := cfstring("com.Apple.Safari") | |
cs := C.CFPreferencesCopyAppValue(hp, saf) | |
if cs == nil { | |
return | |
} | |
fmt.Printf("%#v\n", cfstringGo(C.CFStringRef(cs))) | |
} |
Maybe https://github.com/golang/go/wiki/InvalidFlag ? I don't know that com.Apple.Safari is a valid key to check anymore, but other keys should work.
@noelmom, this is pretty old. I believe some examples in
https://github.com/kolide/launcher/blob/0936d6f8cd865432fb78264ca9bef8e352fd4228/pkg/osquery/table/sus_darwin.go
https://github.com/kolide/launcher/blob/0936d6f8cd865432fb78264ca9bef8e352fd4228/pkg/osquery/table/corefoundation_helpers_darwin.go#L18
Thanks! I'll check it out.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm looking for something like this but can't get it to work. Getting an error:
package main: /Users/myuser/go/src/github.com/myuser/go_CFPreferencesCopyAppValue/main.go: invalid #cgo verb: #cgo LDFLGS: -framework IOKit -framework ApplicationServices
Any ideas?