Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* dump Gobe Productive file structure
- * François Revol, 2007
- */
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <ctype.h>
- #include <ByteOrder.h>
- char buff[256];
- #define READ(n) { int err; if ((err = read(fd, buff, (n))) < (n)) { printf("read(%d) = %d at line %s\n", (n), err, __LINE__); exit(1);} }
- #define READTAG() { READ(4); printf("%stag:'%4.4s'\n", indent, buff); }
- // read and compare
- #define READTAGC(t) { READ(4); printf("%stag:'%4.4s'\n", indent, buff); if (strncmp(buff, t, 4)) { printf("Unmatched tag %4.4s != '%4.4s'\n", buff, t); exit(1); } }
- #define READETAGC(t) { READ(4); printf("%send tag:'%4.4s'\n", indent, buff); if (strncmp(buff, t, 4)) { printf("Unmatched tag %4.4s != '%4.4s' level %d, tag %d left %d\n", buff, t, level, tags, count); exit(1); } }
- #define READSTR() { int l; READ(2); l = B_BENDIAN_TO_HOST_INT16(*(uint16 *)buff)+1; \
- if (l > 255) { printf("string too long (%d)\n", l); exit(1); } \
- READ(l); printf("%sstring: '%s' (%d)\n", indent, buff, l); }
- #define SKIP(n) { lseek(fd, n, SEEK_CUR); }
- #define TELL() { off_t pos = lseek(fd, 0LL, SEEK_CUR); printf("%s @ %Ld 0x%Lx\n", indent, pos, pos); }
- int dump(int level, int fd, int count)
- {
- char indent[20] = "";
- int i;
- int tags = 0;
- int v;
- if (level > 19) {
- printf("level>19\n");
- exit(1);
- }
- memset(indent, '\0', 20);
- memset(indent, '\t', level);
- do {
- int len;
- TELL();
- READTAG();
- if (!isupper(buff[0]) && !isupper(buff[1])
- && !isupper(buff[2]) && !isupper(buff[3])) {
- printf("%s<<<<\n", indent);
- return 1;
- }
- if (level==0 && strncmp(buff, "DOC!", 4)) {
- printf("invalid!\n");
- exit(1);
- }
- printf("%sstart tag: %4.4s\n", indent, buff);
- switch (B_BENDIAN_TO_HOST_INT32(*(uint32 *)buff)) {
- case 'DOC!':
- SKIP(0x4e);
- dump(level+1, fd, -1);
- READETAGC("ENT!");
- break;
- case 'VERM':
- SKIP(0x20-4);
- break;
- case 'GRFX':
- READ(0x4); // number of strings ??
- v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
- while (v--)
- READSTR();
- break;
- case 'WORD':
- case 'PROT':
- READ(0x4);
- READSTR();
- break;
- case 'PAGE':
- case 'BROT':
- SKIP(0x4);
- READSTR();
- SKIP(0x20-4);
- break;
- case 'SETM':
- SKIP(0x10);
- break;
- case 'PENM':
- SKIP(0x10-2);
- break;
- case 'STYM':
- dump(level+1, fd, 3);
- READETAGC("StyM");
- break;
- case 'PRPM':
- READ(4);
- printf("%s%d properties following...\n", indent, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
- dump(level+1, fd, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
- READETAGC("PrpM"); // (Property Manager ?)
- break;
- case 'PROP':
- READ(4);
- printf("%s%4.4s\n", indent, buff);
- READ(4);
- SKIP(B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
- SKIP(4);
- break;
- case 'STYP':
- SKIP(4); // 0 ?
- READETAGC("StyP");
- break;
- case 'STLT': // StyLe LisT
- READ(4);
- dump(level+1, fd, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
- READETAGC("StlT"); //
- break;
- case 'PTAB':
- READ(4);
- v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff) * 4;
- printf("%sskipping %d\n", indent, v);
- SKIP(v);
- READETAGC("PtaB");
- break;
- case 'REFT':
- READ(4);
- v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff) * 4;
- printf("%sskipping %d\n", indent, v);
- SKIP(v);
- READETAGC("RefT");
- break;
- case 'GRAP':
- READ(4);
- SKIP(16+3);
- READ(4);
- v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
- while (v--) {
- READSTR();
- }
- dump(level+1, fd, 2);
- SKIP(v);
- READETAGC("GraP");
- break;
- case 'STYL':
- READTAG();
- READ(4);
- SKIP(6);
- TELL();
- READTAG(); //??
- if (strncmp(buff, "Basi", 4) == 0) {
- READSTR();
- SKIP(16-4);
- } else if (strncmp(buff, "GRFX", 4) == 0) {
- //READSTR();
- SKIP(1+16+8);
- } else {
- printf("invalid tag\n");
- exit(1);
- }
- dump(level+1, fd, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
- READETAGC("STYL");
- break;
- case 'lyr+':
- READ(4);
- v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
- while (v--) {
- READSTR();
- }
- READETAGC("lyr-");
- break;
- case 'vew+':
- READ(4);
- v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
- while (v--) {
- READSTR();
- }
- SKIP(4*3+1);
- READETAGC("vew-");
- break;
- default:
- printf("%sunknown tag level %d, tag %d left %d\n", indent, tags, count);
- exit(1);
- }
- tags++;
- } while (--count);
- return 0;
- }
- int main(int argc, char **argv)
- {
- char *f = argv[1];
- int fd = open(f, O_RDONLY);
- READ(16);
- printf("%16.16s\n", buff);
- //READ(4);
- /* if (strncmp(buff, "DOC!", 4)) {
- printf("bad tag\n");
- exit(1);
- }*/
- dump(0, fd, -1);
- }
Add Comment
Please, Sign In to add comment