【问题标题】:Printing a structure in C用 C 打印结构
【发布时间】:2017-02-17 01:33:24
【问题描述】:

我正在对wireshark 源代码进行一些更改。在某个特定点上,我打印出一个名为SSLDecoder 的结构,如下所示

FILE *f;  
f=fopen("file.txt","a+");
size_decoder=sizeof(SslDecoder);  
for(i=0; i < size_decoder; i++){
    fprintf(f,"%02x",*(ssl_session->client_new+i));
}
fprintf(f,"\n");

ssl_session 是 struct SslDecryptSession 类型,定义为

typedef struct _SslDecryptSession {
    guchar _master_secret[SSL_MASTER_SECRET_LENGTH];
    guchar _session_id[256];
    guchar _client_random[32];
    guchar _server_random[32];
    StringInfo session_id;
    StringInfo session_ticket;
    StringInfo server_random;
    StringInfo client_random;
    StringInfo master_secret;
    StringInfo handshake_data;
    /* the data store for this StringInfo must be allocated explicitly with a capture lifetime scope */
    StringInfo pre_master_secret;
    guchar _server_data_for_iv[24];
    StringInfo server_data_for_iv;
    guchar _client_data_for_iv[24];
    StringInfo client_data_for_iv;

    gint state;
    SslCipherSuite cipher_suite;
    SslDecoder *server;
    SslDecoder *client;
    SslDecoder *server_new;
    SslDecoder *client_new;
    gcry_sexp_t private_key;
    StringInfo psk;
    guint16 version_netorder;
    StringInfo app_data_segment;
    SslSession session;
} SslDecryptSession;

SslDecoder 的类型为

typedef struct _SslDecoder {
    SslCipherSuite* cipher_suite;
    gint compression;
    guchar _mac_key_or_write_iv[48];
    StringInfo mac_key; /* for block and stream ciphers */
    StringInfo write_iv; /* for AEAD ciphers (at least GCM, CCM) */
    SSL_CIPHER_CTX evp;
    SslDecompress *decomp;
    guint32 seq;
    guint16 epoch;
    SslFlow *flow;
} SslDecoder;

但是,打印到文件的输出是不规则的。 示例输出在pastebin

谁能告诉我我做错了什么?

我稍后在文件中使用此输出来填充另一个像这样的解码器(现在,由于文件的不规则打印,我面临 seg 错误)

SslDecoder *temp_decoder;
c = malloc(sizeof(SslDecoder));

for (i=0; i<sizeof(SslDecoder); i++){
    fscanf(f,"%02x",&c[i]);
}
memcpy(temp_decoder,c, sizeof(SslDecoder));

【问题讨论】:

    标签: c file io printf fwrite


    【解决方案1】:

    当你执行指针运算时,指针偏移了所指向类型大小的倍数;它确实不将指针偏移了指定的字节数。请记住,如果你有T* p,那么*(p + i) 就等于p[i]。

    在您的代码中,您可以:

    size_decoder=sizeof(SslDecoder);  
    for(i=0; i < size_decoder; i++){
        fprintf(f,"%02x",*(ssl_session->client_new+i));
    }
    

    *(ssl_session-&gt;client_new+i) 在每次迭代中将client_new 指针偏移i * sizeof (SslDecoder) 字节。

    您打算在每次迭代中仅将指针前移一个字节,因此您必须对指向 char 的指针执行指针运算:

    size_decoder=sizeof(SslDecoder);  
    const unsigned char* p = (unsigned char*) ssl_session->client_new;
    for(i=0; i < size_decoder; i++){
        fprintf(f,"%02x", p[i]);
    }
    

    在读回文件时,您同样需要做类似的事情。我还应该在您的阅读代码中指出:

    SslDecoder *temp_decoder;
    c = malloc(sizeof(SslDecoder));
    
    for (i=0; i<sizeof(SslDecoder); i++){
        fscanf(f,"%02x",&c[i]);
    }
    memcpy(temp_decoder,c, sizeof(SslDecoder));
    

    memcpy 调用不正确;您还没有为temp_decoder 分配任何内存。您根本不需要使用memcpy:

    SslDecoder *temp_decoder = malloc(sizeof *temp_decoder);
    unsigned char* p = (unsigned char*) temp_decoder;
    for (i=0; i<sizeof(SslDecoder); i++){
        int temp; // "%x" corresponds to an int type
        fscanf(f, "%02x", &temp);
        p[i] = (unsigned char) temp;
    }
    

    【讨论】:

    • 对于写作部分,我确实需要在此代码之后调用 memcpy。 memcpy(temp_decoder, p,sizeof (temp_decoder));
    • @AbhimanyuKhanna 不,在我展示的示例中,您不需要 memcpy 调用; p 指向与temp_decoder 相同的地址(它们只是指针运算的类型不同)。因此它直接写入为temp_decoder 分配的内存。
    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多