mmu_man

dump_gp_file.cpp

Nov 16th, 2018
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. /* dump Gobe Productive file structure
  2.  * François Revol, 2007
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <ctype.h>
  9. #include <ByteOrder.h>
  10.  
  11. char buff[256];
  12.  
  13. #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);} }
  14.  
  15. #define READTAG() { READ(4); printf("%stag:'%4.4s'\n", indent, buff); }
  16.  
  17. // read and compare
  18. #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); } }
  19. #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); } }
  20.  
  21. #define READSTR() { int l; READ(2); l = B_BENDIAN_TO_HOST_INT16(*(uint16 *)buff)+1; \
  22. if (l > 255) { printf("string too long (%d)\n", l); exit(1); } \
  23. READ(l); printf("%sstring: '%s' (%d)\n", indent, buff, l); }
  24.  
  25. #define SKIP(n) { lseek(fd, n, SEEK_CUR); }
  26.  
  27. #define TELL() { off_t pos = lseek(fd, 0LL, SEEK_CUR); printf("%s @ %Ld 0x%Lx\n", indent, pos, pos); }
  28.  
  29. int dump(int level, int fd, int count)
  30. {
  31.     char indent[20] = "";
  32.     int i;
  33.     int tags = 0;
  34.     int v;
  35.     if (level > 19) {
  36.         printf("level>19\n");
  37.         exit(1);
  38.     }
  39.     memset(indent, '\0', 20);
  40.     memset(indent, '\t', level);
  41.    
  42.     do {
  43.         int len;
  44.         TELL();
  45.         READTAG();
  46.         if (!isupper(buff[0]) && !isupper(buff[1])
  47.          && !isupper(buff[2]) && !isupper(buff[3])) {
  48.             printf("%s<<<<\n", indent);
  49.             return 1;
  50.         }
  51.         if (level==0 && strncmp(buff, "DOC!", 4)) {
  52.             printf("invalid!\n");
  53.             exit(1);
  54.         }
  55.         printf("%sstart tag: %4.4s\n", indent, buff);
  56.         switch (B_BENDIAN_TO_HOST_INT32(*(uint32 *)buff)) {
  57.         case 'DOC!':
  58.             SKIP(0x4e);
  59.             dump(level+1, fd, -1);
  60.             READETAGC("ENT!");
  61.             break;
  62.         case 'VERM':
  63.             SKIP(0x20-4);
  64.             break;
  65.         case 'GRFX':
  66.             READ(0x4); // number of strings ??
  67.             v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
  68.             while (v--)
  69.                 READSTR();
  70.             break;
  71.         case 'WORD':
  72.         case 'PROT':
  73.             READ(0x4);
  74.             READSTR();
  75.             break;
  76.         case 'PAGE':
  77.         case 'BROT':
  78.             SKIP(0x4);
  79.             READSTR();
  80.             SKIP(0x20-4);
  81.             break;
  82.         case 'SETM':
  83.             SKIP(0x10);
  84.             break;
  85.         case 'PENM':
  86.             SKIP(0x10-2);
  87.             break;
  88.         case 'STYM':
  89.             dump(level+1, fd, 3);
  90.             READETAGC("StyM");
  91.             break;
  92.         case 'PRPM':
  93.             READ(4);
  94.             printf("%s%d properties following...\n", indent, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
  95.             dump(level+1, fd, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
  96.             READETAGC("PrpM"); // (Property Manager ?)
  97.             break;
  98.         case 'PROP':
  99.             READ(4);
  100.             printf("%s%4.4s\n", indent, buff);
  101.             READ(4);
  102.             SKIP(B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
  103.             SKIP(4);
  104.             break;
  105.         case 'STYP':
  106.             SKIP(4); // 0 ?
  107.             READETAGC("StyP");
  108.             break;
  109.         case 'STLT': // StyLe LisT
  110.             READ(4);
  111.             dump(level+1, fd, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
  112.             READETAGC("StlT"); //
  113.             break;
  114.         case 'PTAB':
  115.             READ(4);
  116.             v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff) * 4;
  117.             printf("%sskipping %d\n", indent, v);
  118.             SKIP(v);
  119.             READETAGC("PtaB");
  120.             break;
  121.         case 'REFT':
  122.             READ(4);
  123.             v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff) * 4;
  124.             printf("%sskipping %d\n", indent, v);
  125.             SKIP(v);
  126.             READETAGC("RefT");
  127.             break;
  128.         case 'GRAP':
  129.             READ(4);
  130.             SKIP(16+3);
  131.             READ(4);
  132.             v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
  133.             while (v--) {
  134.                 READSTR();
  135.             }
  136.             dump(level+1, fd, 2);
  137.             SKIP(v);
  138.             READETAGC("GraP");
  139.             break;
  140.         case 'STYL':
  141.             READTAG();
  142.             READ(4);
  143.             SKIP(6);
  144.             TELL();
  145.             READTAG(); //??
  146.             if (strncmp(buff, "Basi", 4) == 0) {
  147.                 READSTR();
  148.                 SKIP(16-4);
  149.             } else if (strncmp(buff, "GRFX", 4) == 0) {
  150.                 //READSTR();
  151.                 SKIP(1+16+8);
  152.             } else {
  153.                 printf("invalid tag\n");
  154.                 exit(1);
  155.             }
  156.             dump(level+1, fd, B_BENDIAN_TO_HOST_INT32(*(uint32*)buff));
  157.             READETAGC("STYL");
  158.             break;
  159.         case 'lyr+':
  160.             READ(4);
  161.             v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
  162.             while (v--) {
  163.                 READSTR();
  164.             }
  165.             READETAGC("lyr-");
  166.             break;
  167.         case 'vew+':
  168.             READ(4);
  169.             v = B_BENDIAN_TO_HOST_INT32(*(uint32*)buff);
  170.             while (v--) {
  171.                 READSTR();
  172.             }
  173.             SKIP(4*3+1);
  174.             READETAGC("vew-");
  175.             break;
  176.         default:
  177.             printf("%sunknown tag level %d, tag %d left %d\n", indent, tags, count);
  178.             exit(1);
  179.         }
  180.         tags++;
  181.     } while (--count);
  182.     return 0;
  183. }
  184.  
  185. int main(int argc, char **argv)
  186. {
  187.     char *f = argv[1];
  188.     int fd = open(f, O_RDONLY);
  189.     READ(16);
  190.     printf("%16.16s\n", buff);
  191.     //READ(4);
  192. /*  if (strncmp(buff, "DOC!", 4)) {
  193.         printf("bad tag\n");
  194.         exit(1);
  195.     }*/
  196.     dump(0, fd, -1);
  197. }
Add Comment
Please, Sign In to add comment