2010-11-13 97 views
0

有人可以告訴我確實從C到德爾福是我的談話得好:C到德爾福結構的轉換

C代碼

struct nfq_data { 
    /* packet_hdr - it HAVE to be the same as struct nfqnl_msg_packet_hdr */ 
    struct { 
     uint32_t packet_id; /* unique ID of packet in queue in network order */ 
     uint16_t hw_protocol; /* hw protocol in network order */ 
     uint8_t hook; /* netfilter hook */ 
    } packet_hdr; 
    /* packet_hw - it HAVE to be the same as struct nfqnl_msg_packet_hw */ 
    struct { 
     uint16_t hw_addrlen; /* len of hw_addr in network order */ 
     uint16_t _pad; 
     uint8_t hw_addr[8]; 
    } packet_hw; 
    /* tm */ 
    struct { 
     long sec; 
     long usec; 
    } tm; 

    uint32_t if_index; /* Unique iface id */ 
    uint32_t verdict; /* Netfilter verdict value */ 
    uint32_t mark; /* Mark value */ 
    uint8_t if_name[IFNAMSIZE]; /* Name of incoming or outgoing iface */ 
    uint32_t data_len; /* Length of packet */ 
    uint8_t payload[0]; /* packet data */ 
}; 

其中

typedef unsigned char uint8_t; 
typedef unsigned __int16 uint16_t; 
typedef unsigned __int32 uint32_t; 
typedef unsigned __int64 uint64_t; 

德爾福

_packet_hdr = record 
    packet_id: Cardinal; 
    hw_protocol: Word; 
    hook: Byte; 
    end; 

    _packet_hw = record 
    hw_addrlen: Word; 
    _pad: Word; 
    hw_addr: array[0..7] of byte; 
    end; 

    _tm = record 
    sec: Int64; 
    usec: Int64; 
    end; 

    pnfq_data = ^nfq_data; 
    nfq_data = record 
    packet_hdr: _packet_hdr; 
    packet_hw: _packet_hw; 
    tm: _tm; 
    if_index: Cardinal; 
    verdict: Cardinal; 
    mark: Cardinal; 
    if_name: array[0..254] of Byte; 
    data_len: Cardinal; 
    payload: PChar; //TBytes; 
    end; 

    packet_hdr: _packet_hdr; 
    packet_hw: _packet_hw; 

我很好,但一個沒有什麼好的。

在此先感謝

博揚

+1

我想你不應該鍵入'uintN_t'標識符。如果你有C99,你將覆蓋系統標識符(在''中;如果你還沒有,那麼當你最終轉換到C99時,你只是在等待不好的事情發生。 – pmg 2010-11-13 12:36:14

+0

沒有足夠的信息來查找問題。在你的代碼中找到行爲不好的第一個位置,並將代碼放在這裏。在這裏描述當前的行爲和期望的行爲。如果有幫助,添加一些內存轉儲。 – Dialecticus 2010-11-13 13:19:33

回答

1
uint8_t payload[0]; 

那真的是一個指針(如在你的Delphi代碼)或大小爲0的就地數組用於表示數據的標題

後到來,你應該爲packed標記您的記錄。 我認爲這不會影響您的具體代碼,但這是一個很好的做法,因爲包裝規則很容易出錯,可能會改變,而不是每個人都知道。
它可能會有所作爲,因爲打包時packet_hdr大小爲7個字節,所以下面的packet_hw在打包時從偏移量7開始,並且在對齊時可能爲8。

+0

「這真的是一個指針」好問題。這也是有效載荷,最後一個字段的答案。 「Packed」之前解決了所有問題,有效載荷是必須採集數據的數組的起點,數據長度定義在data_len中。謝謝 – 2010-11-13 16:07:42

0

我認爲你需要圍繞你的結構與#pragma pack(push,1)/#pragma pack(pop)(如果我們談論的Visual Studio)。

+0

是的,我們正在談論Visual Studio。代碼在#pragma pack(1)/#pragma pack() – 2010-11-13 13:08:01

0

根據你的評論「我收到好,但在這些沒有好的。」

我假設你從一個「C」的應用程序中獲取數據 - 在「Delphi編譯器」代碼生成選項屏幕

看一看「記錄字段滋養品」 有許多的不同的選項,如「四字」或「字」對齊。