Security.h 中结构的 macOS 文档

macOS documentation for structs in Security.h

我正在尝试通过 Java 和 JNA 使用 Security.h macOS 框架。这意味着我需要将某些结构重建为 Java 类.

问题是,当我查看结构(例如 this one)的文档时,我看到的只是对该结构的简短描述,而没有提及它的字段。我在哪里可以获得 Apple 文档中结构的完整描述?

为了快速浏览,您可以在 Apple's open source site, but it's difficult to navigate, especially as the headers are under different locations depending on the version of the OS you want to check. In all cases I've found it's defined in SecBase.h. For example here is the one for latest macOS 上找到 headers。

然后你得到这个:

typedef struct CF_BRIDGED_TYPE(id) SECTYPE(SecKeychainItem) *SecKeychainItemRef;

因此您可能需要其他 headers 来追踪结构的确切字段。一个更好的方法是安装 XCode 和你想要的 OS 的框架,你会在你的本地系统上得到 headers。例如:

$ ls /Applications/Xcode.app/Contents/Developer/Platforms/*.platform/Developer/SDKs/*.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.2.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator9.2.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS2.2.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator2.2.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h

不过,我没有在线文档的好的解决方案。

另一种方式,从记忆中为 JNA 在 Java 中重新创建结构的任务更有帮助,将是构建一个最小的 C 程序(但我不确定如何在macOS,与安全框架链接,也许你这样做),然后 将它交给 gdb 以使用 ptype:

打印结构布局
(gdb) whatis v
type = struct complex
(gdb) ptype v
type = struct complex {
    double real;
    double imag;
}

但是正如评论中所指出的,如果我们在这里尝试这个,我们会得到这个:

(gdb) ptype SecKeychainItemRef
type = struct OpaqueSecKeychainItemRef {
  <incomplete type>
}

恐怕这个符号是自愿不透明的...由 Brendan 在评论中确认:

every macOS type I can think of that ends in Ref is an opaque type (really a pointer), only meant to be passed to functions

这是一个调试 session Xcode: