开发者

C++ Using a file format

A couple of days ago, I asked how you could reverse engineer a file format. While that didn't really work out, someone gave me the file format. (Click Here) Thank you Xadet.

I'm still quite new to all this, and I was wondering where I should go from here. I am guessing I will have to use inline-asm in C++ to use this format, but I wouldn't know how to actually open the file using this, or insert data into it.

So the question would be, how do I use the file format to get or insert data? And the file format looks like asm, but I don't want to start progra开发者_运维百科mming in pure ASM. I've seen people programming asm in C++ before, that's why I think it would be a good choice

Any help would be greatly apreciated.


The file format description doesn't look like asm, it looks like pseudocode.


I assume you don't want to have a C++ program that reads that file format document when it starts, then parses the actual data file on that basis. Instead, you just want a C++ program dedicated to reading the current version of that file format? (This is much simpler and will run faster). You don't need to use ASM. What you do need to do is work out the C++ types that are equivalent to the names used in the format file. For example, I think DWORD is used in Microsoft languages to refer to an integer of a specific size - maybe 32 or 64 bits. Track that stuff down, then create C++ structs with equivalent members.

For example:

#include <inttypes.h> // if on Windows, try __int32, __int64 etc. instead

typedef int64_t DWORD;  // or whatever width you find it's meant to be
typedef int32_t WORD;
typedef ??? ZSTR;  // google it...?
typedef float FLOAT;

struct dds
{
    ZSTR path;
    WORD is_skin;
    WORD alpha_enabled;
    WORD two_sided;
    WORD alpha_test_enabled;
    WORD alpha_ref;
    WORD z_write_enabled;
    WORD z_test_enabled;
    WORD blending_mode; // None = 0, Custom = 1, Normal = 2, Lighten = 3
    WORD specular_enabled;
    FLOAT alpha;
    WORD glow_type; // None = 0, NotSet = 1, Simple = 2, Light = 3, Texture = 4, TextureLight = 5, Alpha = 6
    FLOAT red;
    FLOAT green;
    FLOAT blue;
};

// point p at the entire input, which you'll have loaded into memory somewhere
// (e.g. f/stat() the file size then allocate heap and read into it, or memory map)
const char* p = input;
DWORD mesh_count = *(const DWORD*)p;
p += sizeof(DWORD);
for (int i = 0; i < mesh_count; ++i)
{
    const dds& d = *(const dds*)p;
    // you can use d.red, d.alpha etc. here to do anything you like
    p += sizeof dds;
}

// continue processing effect count etc... in same style

HTH, Tony


It's a kind of scripting language for defining data format, something similar to XDR. You just have to write a parser for it (don't try to use the script in the runtime). Write some functions like get_WORD_BE() or get_DWORD_LE(), etc so that you don't depend on endiannes.

Yes, and if you want to use Tony's approach, add some #pragma pack(1).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜