home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 043.lha / IFFdump.c < prev    next >
C/C++ Source or Header  |  1987-05-17  |  8KB  |  344 lines

  1.  
  2. /*
  3.  * DECODE IFF formatted files    V1.00
  4.  *
  5.  *    COMPILE W/ASTARTUP.OBJ and MY.LIB
  6.  *
  7.  * iffdump [-vV] file
  8.  *
  9.  * -v    :  display a little more information (only for forms and types
  10.  *          the program knows about)
  11.  * -V    :  display additional hex dump of all sections
  12.  *
  13.  *
  14.  * NOTE:  The '-v' flag could use *a lot* of expansion... Since I haven't
  15.  *        got the latest IFF documentation.
  16.  *
  17.  *
  18.  * NOTE:  This program does almost no format checking (e.g. PROP's only
  19.  *        within LIST's, etc...), it is meant to display the format of
  20.  *        valid IFF files.
  21.  */
  22.  
  23. #include "iffdump.h"
  24. #include <graphics/view.h>
  25. #include "stdio.h"
  26. #include "functions.h"
  27.  
  28. #define MODE_V1   0x01
  29. #define MODE_V2   0x02
  30.  
  31. extern char *tabstr(), *ltos();
  32.  
  33. union {
  34.    X_ANHD anhd;
  35.    X_BMHD bmhd;
  36.    X_CMAP cmap[64];
  37.    X_GRAB grab;
  38.    X_DEST dest;
  39.    X_SPRT sprt;
  40.    X_CAMG camg;
  41.    X_CRNG crng;
  42. } U;
  43.  
  44. int Modes;
  45.  
  46. main(ac, av)
  47. char *av[];
  48. {
  49.    int i, j;
  50.    char *str;
  51.    FILE *fi;
  52.    long endpos;
  53.  
  54.    for (j = 0, i = 1; i < ac; ++i) {
  55.       str = av[i];
  56.       if (*str == '-') {
  57.          while (*++str) {
  58.             switch(*str) {
  59.             case 'v':
  60.                Modes |= MODE_V1;
  61.                break;
  62.             case 'V':
  63.                Modes |= MODE_V2;
  64.                break;
  65.             default:
  66.                printf ("'%lc' bad option\n", *str);
  67.             }
  68.          }
  69.       } else {
  70.          if (j) {
  71.             puts("May specify only one file");
  72.             exit(1);
  73.          }
  74.          j = i;
  75.       }
  76.    }
  77.    if (j == 0) {
  78.       puts ("IFFDUMP [-vV] file");
  79.       puts ("(c)1986 Matthew Dillon.  Public Domain V1.00");
  80.       puts ("");
  81.       puts ("-v   show detail on things the prog. knows about");
  82.       puts ("-V   show hex dump");
  83.       exit(1);
  84.    }
  85.    fi = fopen(av[j],"r");
  86.    if (fi == NULL) {
  87.       puts ("could not open file");
  88.       exit(1L);
  89.    }
  90.    fseek(fi, 0L, 2);
  91.    endpos = ftell(fi);
  92.    fseek(fi, 0L, 0);
  93. #if 0
  94. printf("file end: %ld\n",endpos);
  95. #endif
  96.    iffdecode(fi, 0L, endpos);
  97.    fclose(fi);
  98. }
  99.  
  100.  
  101. iffdecode(fi, tab, endpos)
  102. FILE *fi;
  103. long tab;
  104. long endpos;
  105. {
  106.    long chunk[2];
  107.    long curpos, bytes, curend;
  108.    long data;
  109.    char ok;
  110.    char count = 0;
  111.  
  112. #if 0
  113. printf("call iffdecode: %ld %ld\n",tab,endpos);
  114. #endif
  115.    for (;;) {
  116.       curpos = ftell(fi);
  117. #if 0
  118. printf("curpos: %ld\n",curpos);
  119. #endif
  120.       if (curpos == endpos)
  121.          return(1);
  122.       if (count++ && tab == 0) {
  123.          puts ("Warning: Garbage after IFF-EOF");
  124.          return (0);
  125.       }
  126.       if (curpos + 8 > endpos) {
  127.          puts ("Error: Premature End Of File in header");
  128.          return(0);
  129.       }
  130.       fread(chunk,1,8,fi);
  131.       curpos += 8;
  132.       bytes = chunk[1];
  133.       curend = curpos + bytes;
  134.       printf ("%sCHUNK %s (%8ld)", tabstr(tab), ltos(chunk[0]), bytes);
  135.       if (curend > endpos) {
  136.          puts ("\nError: Premature End Of File within chunk");
  137.          return(0);
  138.       }
  139. #if 0
  140. printf("chunk: %lx\n",chunk[0]);
  141. #endif
  142.       ok = 0;
  143.       switch(chunk[0]) {
  144.       case IFF_FILLER:
  145.          printf ("A filler chunk\n");
  146.          if (Modes & MODE_V2)
  147.             hexdump(fi, curpos, bytes, tab);
  148.          break;
  149.       case IFF_FORM:
  150.       case IFF_LIST:
  151.       case IFF_CAT:
  152.       case IFF_PROP:
  153.          fread(&data,1, 4,fi);
  154.          curpos += 4;
  155.          bytes  -= 4;
  156.          printf ("TYPE %s\n", ltos(data));
  157.          iffdecode(fi, tab + 4, curend);
  158.          break;
  159.       default:
  160.          ok = 1;
  161.          break;
  162.       }
  163.       if (ok) {
  164.          puts ("");
  165.          if (Modes & MODE_V1)
  166.             decode_sub(fi, tab, chunk[0], bytes);
  167.          if (Modes & MODE_V2)
  168.             hexdump(fi, curpos, bytes, tab);
  169.       }
  170.       if (curend & 1) {
  171.          printf ("%s(Filler Byte)\n", tabstr(tab));
  172.          ++curend;
  173.       }
  174.       fseek(fi, curend, 0);
  175.    }
  176. }
  177.  
  178.  
  179.  
  180. decode_sub(fi, tab, name, bytes)
  181. FILE *fi;
  182. long tab,bytes,name;
  183. {
  184.    int i, j;
  185.  
  186.    tab += 4;
  187.    switch (name) {
  188.    case ILBM_ANHD:
  189.       cs((long)sizeof(U.anhd), bytes);
  190.       fread(&U.anhd,1, sizeof(U.anhd),fi);
  191.       printf ("%s    op= %-4d\n", tabstr(tab), U.anhd.operation);
  192.       printf ("%s  mask= %-2x\n", tabstr(tab), U.anhd.mask);
  193.       printf ("%s width= %-4d\n", tabstr(tab), U.anhd.w);
  194.       printf ("%sheight= %-4d\n", tabstr(tab), U.anhd.h);
  195.       printf ("%s     x= %-4d\n", tabstr(tab), U.anhd.x);
  196.       printf ("%s     y= %-4d\n", tabstr(tab), U.anhd.y);
  197.       printf ("%sabstim= %-4ld\n", tabstr(tab), U.anhd.abstime);
  198.       printf ("%sreltim= %-4ld\n", tabstr(tab), U.anhd.reltime);
  199.       printf ("%sintrlv= %-4d\n", tabstr(tab), U.anhd.interleave);
  200.       break;
  201.    case ILBM_BMHD:
  202.       cs((long)sizeof(U.bmhd), bytes);
  203.       fread(&U.bmhd,1, sizeof(U.bmhd),fi);
  204.       printf ("%s width= %-4d\n", tabstr(tab), U.bmhd.w);
  205.       printf ("%sheight= %-4d\n", tabstr(tab), U.bmhd.h);
  206.       printf ("%splanes= %-4d\n", tabstr(tab), U.bmhd.planes);
  207.       printf ("%s     x= %-4d\n", tabstr(tab), U.bmhd.x);
  208.       printf ("%s     y= %-4d\n", tabstr(tab), U.bmhd.y);
  209.       printf ("%s  mask= x%-2x\n",tabstr(tab), U.bmhd.masking);
  210.       printf ("%s  comp= x%-2x\n",tabstr(tab), U.bmhd.compression);
  211.       printf ("%stcolor= %-2d\n", tabstr(tab), U.bmhd.transparent_color);
  212.       printf ("%sxaspct= %-2d\n", tabstr(tab), U.bmhd.xaspect);
  213.       printf ("%syaspct= %-2d\n", tabstr(tab), U.bmhd.yaspect);
  214.       printf ("%s pagew= %-4d\n", tabstr(tab), U.bmhd.pagewidth);
  215.       printf ("%s pageh= %-4d\n", tabstr(tab), U.bmhd.pageheight);
  216.       break;
  217.    case ILBM_CMAP:
  218.       if (bytes % 3) {
  219.          puts ("Expected multiples of 3 bytes for colormap");
  220.          break;
  221.       }
  222.       if (bytes > 32*3) {
  223.          puts ("Color map is larger than 32 entries");
  224.          break;
  225.       }
  226.       fread(&U.cmap,1,(int)bytes,fi);
  227.       j = bytes/3;
  228.       for (i = 0; i < j; ++i) {
  229.          printf ("%scolor %2d   %2x %2x %2x\n",
  230.             tabstr(tab), i, U.cmap[i][0], U.cmap[i][1], U.cmap[i][2]);
  231.       }
  232.       break;
  233.    case ILBM_GRAB:
  234.    case ILBM_DEST:
  235.    case ILBM_SPRT:
  236.       puts ("");
  237.       break;
  238.    case ILBM_CAMG:
  239.       fread(&U.camg,1, sizeof(U.camg),fi);
  240.       i = U.camg.vpmodes;
  241.       printf ("%sVP MODES = %8x (", tabstr(tab), i);
  242.       if (i & HIRES)
  243.          printf("HIRES ");
  244.       if (i & SPRITES)
  245.          printf("SPRITES ");
  246.       if (i & VP_HIDE)
  247.          printf("VP_HIDE ");
  248.       if (i & HAM)
  249.          printf ("HAM ");
  250.       if (i & DUALPF)
  251.          printf ("DUALPF ");
  252.       if (i & GENLOCK_AUDIO)
  253.          printf ("GENLOCK_AUDIO ");
  254.       if (i & EXTRA_HALFBRITE)
  255.          printf ("EXTRA_HALFBRITE ");
  256.       if (i & PFBA)
  257.          printf ("PFBA ");
  258.       if (i & LACE)
  259.          printf ("LACE ");
  260.       if (i & GENLOCK_VIDEO)
  261.          printf ("GENLOCK_VIDEO ");
  262.       puts (")");
  263.       break;
  264.    case ILBM_CRNG:
  265.       cs((long)sizeof(U.crng), bytes);
  266.       fread(&U.crng,1, sizeof(U.crng),fi);
  267.       printf ("%s count= %-4d\n", tabstr(tab), U.crng.count);
  268.       printf ("%s  rate= %-4d\n", tabstr(tab), U.crng.rate);
  269.       printf ("%s flags= %-4d\n", tabstr(tab), U.crng.flags);
  270.       printf ("%s   low= %-4d\n", tabstr(tab), U.crng.low);
  271.       printf ("%s  high= %-4d\n", tabstr(tab), U.crng.high);
  272.       break;
  273.    case ILBM_BODY:
  274.       break;
  275.    }
  276. }
  277.  
  278.  
  279.  
  280. hexdump(fi, curpos, bytes, tab)
  281. FILE *fi;
  282. long curpos,bytes,tab;
  283. {
  284.    long pos;
  285.    UBYTE ch;
  286.  
  287.    pos = 0;
  288.    fseek(fi, curpos, 0);
  289.    while (bytes) {
  290.       fread( &ch,1, 1,fi);
  291.       if ((pos & 15) == 0) {
  292.          printf("\n%s%5lx     ", tabstr(tab+4), pos);
  293.       }
  294.       printf ("%2x ", ch);
  295.       ++pos;
  296.       --bytes;
  297.    }
  298.    puts("");
  299. }
  300.  
  301.  
  302.  
  303. cs(shouldbe, actual)
  304. long shouldbe,actual;
  305. {
  306.    if (shouldbe != actual)
  307.       printf ("Expected %ld bytes, got %ld\n", shouldbe, actual);
  308. }
  309.  
  310.  
  311. char *
  312. tabstr(tab)
  313. long tab;
  314. {
  315.    static char space[128];
  316.    static int lasttab = 0;
  317.    static int init = 1;
  318.  
  319.    if (init) {
  320.       for (init=1;init<128;init++) space[init] = ' ';
  321.       init = 0;
  322.    }
  323.    if (tab != lasttab) {
  324.       space[lasttab] = ' ';
  325.       space[lasttab = tab] = '\0';
  326.    }
  327.    return (space);
  328. }
  329.  
  330. char *
  331. ltos(data)
  332. unsigned long data;
  333. {
  334.    static char buf[5];
  335.  
  336.    buf[0] = (data >> 24) & 0xFF;
  337.    buf[1] = (data >> 16) & 0xFF;
  338.    buf[2] = (data >> 8 ) & 0xFF;
  339.    buf[3] = data & 0xFF;
  340.    return(buf);
  341. }
  342.  
  343.  
  344.