c - Security.h 中结构的 macOS 文档

标签 c macos struct jna

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

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

最佳答案

要快速查看,您可以在 Apple's open source site 上找到标题, 但它很难导航,尤其是因为标题位于不同的位置,具体取决于您要检查的操作系统版本。在所有情况下,我都发现它是在 SecBase.h 中定义的。例如here is the one for latest macOS .

然后你会得到这个:

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

因此您可能需要其他 header 来追踪结构的确切字段。一个更好的方法是安装带有适用于您想要的操作系统的框架的 XCode,您将在本地系统上获得 header 。例如:

$ 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

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

另一种方式,从内存来看,它对于在 Java 中为 JNA 重新创建结构的任务更有帮助,将是构建一个最小的 C 程序(但我不确定如何在 macOS 上执行此操作,链接到安全框架,也许你这样做了),然后使用 ptype 将它交给 gdb 打印结构布局:

(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

这是 Xcode 的调试 session :Xcode screenshot

关于c - Security.h 中结构的 macOS 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43949920/

相关文章:

CLONE_VM undeclared(第一次在这个函数中使用)

c++ - 如何使用 write() 函数将结构写入文件?

c++ - GCC Segmentation Fault Mac

Java 编译器错误 (OSX)

c - 简单 Mac OSX C 程序中的总线错误

c - 如何通过引用将创建的指针数组传递到结构中?

C:将元素插入结构数组

C 中无法打印最大行

c - 如何将 4 个字节转换为无符号长变量?

mongodb - 模型结构中的嵌套文档