我用 C++ 定义了这个宏;
#define EXT_FIRST_EXTENT(__hdr__) \
((struct ext4_extent *) (((char *) (__hdr__)) + \
sizeof(struct ext4_extent_header)))
其中 ext4_extent_header
是一个结构;
typedef struct ext4_extent_header {
uint16_t eh_magic; /* probably will support different formats */
uint16_t eh_entries; /* number of valid entries */
uint16_t eh_max; /* capacity of store in entries */
uint16_t eh_depth; /* has tree real underlying blocks? */
uint32_t eh_generation; /* generation of the tree */
}__attribute__ ((__packed__)) EXT4_EXTENT_HEADER;
ext4_extent也是一个结构体;
typedef struct ext4_extent {
uint32_t ee_block; /* first logical block extent covers */
uint16_t ee_len; /* number of blocks covered by extent */
uint16_t ee_start_hi; /* high 16 bits of physical block */
uint32_t ee_start_lo; /* low 32 bits of physical block */
} __attribute__ ((__packed__)) EXT4_EXTENT;
这是我尝试用 Delphi 编写的;
function Ext2Partition.EXT_First_Extent(hdr: PExt4_extent_header):PExt4_ExtEnt;
begin
Result := PExt4_ExtEnt(hdr + sizeof(ext4_extent_header));
end;
但是编译器告诉我运算符不适用于此操作数类型。
这是我为 ext4_extent_header
和 ext4_extent
转换为 Delphi 记录的结构;
Type
PExt4_extent_header = ^Ext4_extent_header;
Ext4_extent_header = REcord
eh_magic : Word;
eh_entries : Word;
eh_max : Word;
eh_depth : Word;
eh_generation : Cardinal;
End;
Type
PExt4_ExtEnt = ^Ext4_ExtEnt;
Ext4_ExtEnt = Record
ee_block : Cardinal;
ee_len : Word;
ee_start_hi : Word;
ee_start_low : Cardinal;
End;
谢谢!
最佳答案
以与 C++ 代码相同的方式转换 hdr
。它将指针强制转换为八位字节,以便指针算术将偏移量视为八位字节值。在德尔福中:
Result := PExt4_ExtEnt(PAnsiChar(hdr) + sizeof(ext4_extent_header));
您可以启用指针运算并使其更简单:
{$POINTERMATH ON}
....
Result := hdr + 1;
您的代码中可能还有其他问题。如果 hdr
真的是一个 PExt4_ExtEnt
那么 C++ 就不需要宏了。它可以只写 hdr + 1
。所以我怀疑您需要更深入地研究 C++ 代码才能找出 hdr
的真正含义。
另请注意,C++ 代码指定这些记录是打包的。在您的 Delphi 代码中使用 packed record
进行匹配。
关于C++ 到 Delphi - 结构和指针加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28459948/