home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 011.lha / IFF / iffdump.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  7KB  |  310 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 "iff.h"
  24. #include <graphics/view.h>
  25.  
  26.  
  27. #define MODE_V1   0x01
  28. #define MODE_V2   0x02
  29.  
  30. extern char *tabstr(), *ltos();
  31.  
  32. union {
  33.    X_BMHD bmhd;
  34.    X_CMAP cmap[64];
  35.    X_GRAB grab;
  36.    X_DEST dest;
  37.    X_SPRT sprt;
  38.    X_CAMG camg;
  39.    X_CRNG crng;
  40. } U;
  41.  
  42. int Modes;
  43.  
  44. main(ac, av)
  45. char *av[];
  46. {
  47.    int i, j;
  48.    char *str;
  49.    long fi;
  50.    long endpos;
  51.  
  52.    for (j = 0, i = 1; i < ac; ++i) {
  53.       str = av[i];
  54.       if (*str == '-') {
  55.          while (*++str) {
  56.             switch(*str) {
  57.             case 'v':
  58.                Modes |= MODE_V1;
  59.                break;
  60.             case 'V':
  61.                Modes |= MODE_V2;
  62.                break;
  63.             default:
  64.                printf ("'%lc' bad option\n", *str);
  65.             }
  66.          }
  67.       } else {
  68.          if (j) {
  69.             puts("May specify only one file");
  70.             exit(1);
  71.          }
  72.          j = i;
  73.       }
  74.    }
  75.    if (j == 0) {
  76.       puts ("IFFDUMP [-vV] file");
  77.       puts ("(c)1986 Matthew Dillon.  Public Domain V1.00");
  78.       puts ("");
  79.       puts ("-v   show detail on things the prog. knows about");
  80.       puts ("-V   show hex dump");
  81.       exit(1);
  82.    }
  83.    fi = xopen(av[j], "r", 8192);
  84.    if (fi == NULL) {
  85.       puts ("could not open file");
  86.       exit(1);
  87.    }
  88.    endpos = xseek(fi, 0, 1);
  89.    xseek(fi, 0, -1);
  90.    iffdecode(fi, 0, endpos);
  91.    xclose(fi);
  92.    if (checkbreak())
  93.       puts ("*BREAK*");
  94. }
  95.  
  96.  
  97. iffdecode(fi, tab, endpos)
  98. {
  99.    long chunk[2];
  100.    long curpos, bytes, curend;
  101.    long data;
  102.    char ok;
  103.    char count = 0;
  104.  
  105.    for (;;) {
  106.       curpos = xseek(fi, 0, 0);
  107.       if (curpos == endpos)
  108.          return(1);
  109.       if (count++ && tab == 0) {
  110.          puts ("Warning: Garbage after IFF-EOF");
  111.          return (0);
  112.       }
  113.       if (curpos + 8 > endpos) {
  114.          puts ("Error: Premature End Of File in header");
  115.          return(0);
  116.       }
  117.       xread(fi, chunk, 8);
  118.       curpos += 8;
  119.       bytes = chunk[1];
  120.       curend = curpos + bytes;
  121.       printf ("%sCHUNK %s (%8ld)", tabstr(tab), ltos(chunk[0]), bytes);
  122.       if (curend > endpos) {
  123.          puts ("\nError: Premature End Of File within chunk");
  124.          return(0);
  125.       }
  126.  
  127.       ok = 0;
  128.       switch(chunk[0]) {
  129.       case IFF_FILLER:
  130.          printf ("A filler chunk\n");
  131.          if (Modes & MODE_V2)
  132.             hexdump(fi, curpos, bytes, tab);
  133.          break;
  134.       case IFF_FORM:
  135.       case IFF_LIST:
  136.       case IFF_CAT:
  137.       case IFF_PROP:
  138.          xread(fi, &data, 4);
  139.          curpos += 4;
  140.          bytes  -= 4;
  141.          printf ("TYPE %s\n", ltos(data));
  142.          if (checkbreak())
  143.             break;
  144.          iffdecode(fi, tab + 4, curend);
  145.          if (checkbreak())
  146.             break;
  147.          break;
  148.       default:
  149.          ok = 1;
  150.          break;
  151.       }
  152.       if (ok) {
  153.          puts ("");
  154.          if (Modes & MODE_V1)
  155.             decode_sub(fi, tab, chunk[0], bytes);
  156.          if (Modes & MODE_V2)
  157.             hexdump(fi, curpos, bytes, tab);
  158.       }
  159.       if (curend & 1) {
  160.          printf ("%s(Filler Byte)\n", tabstr(tab));
  161.          ++curend;
  162.       }
  163.       xseek(fi, curend, -1);
  164.       if (checkbreak())
  165.          break;
  166.    }
  167. }
  168.  
  169.  
  170.  
  171. decode_sub(fi, tab, name, bytes)
  172. {
  173.    int i, j;
  174.  
  175.    tab += 4;
  176.    switch (name) {
  177.    case ILBM_BMHD:
  178.       cs(sizeof(U.bmhd), bytes);
  179.       xread(fi, &U.bmhd, sizeof(U.bmhd));
  180.       printf ("%s width= %-4ld\n", tabstr(tab), U.bmhd.w);
  181.       printf ("%sheight= %-4ld\n", tabstr(tab), U.bmhd.h);
  182.       printf ("%splanes= %-4ld\n", tabstr(tab), U.bmhd.planes);
  183.       printf ("%s     x= %-4ld\n", tabstr(tab), U.bmhd.x);
  184.       printf ("%s     y= %-4ld\n", tabstr(tab), U.bmhd.y);
  185.       printf ("%s  mask= x%-2lx\n",tabstr(tab), U.bmhd.masking);
  186.       printf ("%s  comp= x%-2lx\n",tabstr(tab), U.bmhd.compression);
  187.       printf ("%stcolor= %-2ld\n", tabstr(tab), U.bmhd.transparent_color);
  188.       printf ("%sxaspct= %-2ld\n", tabstr(tab), U.bmhd.xaspect);
  189.       printf ("%syaspct= %-2ld\n", tabstr(tab), U.bmhd.yaspect);
  190.       printf ("%s pagew= %-4ld\n", tabstr(tab), U.bmhd.pagewidth);
  191.       printf ("%s pageh= %-4ld\n", tabstr(tab), U.bmhd.pageheight);
  192.       break;
  193.    case ILBM_CMAP:
  194.       if (bytes % 3) {
  195.          puts ("Expected multiples of 3 bytes for colormap");
  196.          break;
  197.       }
  198.       if (bytes > 32*3) {
  199.          puts ("Color map is larger than 32 entries");
  200.          break;
  201.       }
  202.       xread(fi, &U.cmap, bytes);
  203.       j = bytes/3;
  204.       for (i = 0; i < j; ++i) {
  205.          printf ("%scolor %2ld   %2lx %2lx %2lx\n",
  206.             tabstr(tab), i, U.cmap[i][0], U.cmap[i][1], U.cmap[i][2]);
  207.       }
  208.       break;
  209.    case ILBM_GRAB:
  210.    case ILBM_DEST:
  211.    case ILBM_SPRT:
  212.       puts ("");
  213.       break;
  214.    case ILBM_CAMG:
  215.       xread(fi, &U.camg, sizeof(U.camg));
  216.       i = U.camg.vpmodes;
  217.       printf ("%sVP MODES = %8lx (", tabstr(tab), i);
  218.       if (i & HIRES)
  219.          printf("HIRES ");
  220.       if (i & SPRITES)
  221.          printf("SPRITES ");
  222.       if (i & VP_HIDE)
  223.          printf("VP_HIDE ");
  224.       if (i & HAM)
  225.          printf ("HAM ");
  226.       if (i & DUALPF)
  227.          printf ("DUALPF ");
  228.       if (i & GENLOCK_AUDIO)
  229.          printf ("GENLOCK_AUDIO ");
  230.       if (i & EXTRA_HALFBRITE)
  231.          printf ("EXTRA_HALFBRITE ");
  232.       if (i & PFBA)
  233.          printf ("PFBA ");
  234.       if (i & LACE)
  235.          printf ("LACE ");
  236.       if (i & GENLOCK_VIDEO)
  237.          printf ("GENLOCK_VIDEO ");
  238.       puts (")");
  239.       break;
  240.    case ILBM_CRNG:
  241.    case ILBM_BODY:
  242.       break;
  243.    }
  244. }
  245.  
  246.  
  247.  
  248. hexdump(fi, curpos, bytes, tab)
  249. {
  250.    long pos;
  251.    UBYTE ch;
  252.  
  253.    pos = 0;
  254.    xseek(fi, curpos, -1);
  255.    while (bytes) {
  256.       xread(fi, &ch, 1);
  257.       if ((pos & 15) == 0) {
  258.          if (checkbreak())
  259.             break;
  260.          printf("\n%s%5lx     ", tabstr(tab+4), pos);
  261.       }
  262.       printf ("%2lx ", ch);
  263.       ++pos;
  264.       --bytes;
  265.    }
  266.    puts("");
  267. }
  268.  
  269.  
  270.  
  271. cs(shouldbe, actual)
  272. {
  273.    if (shouldbe != actual)
  274.       printf ("Expected %ld bytes, got %ld\n", shouldbe, actual);
  275. }
  276.  
  277.  
  278. char *
  279. tabstr(tab)
  280. {
  281.    static char space[128];
  282.    static int lasttab;
  283.    static int init = 1;
  284.  
  285.    if (init) {
  286.       bset(space + 1, 127, ' ');
  287.       init = 0;
  288.    }
  289.    if (tab != lasttab) {
  290.       space[lasttab] = ' ';
  291.       space[lasttab = tab] = '\0';
  292.    }
  293.    return (space);
  294. }
  295.  
  296. char *
  297. ltos(data)
  298. unsigned long data;
  299. {
  300.    static char buf[5];
  301.  
  302.    buf[0] = (data >> 24) & 0xFF;
  303.    buf[1] = (data >> 16) & 0xFF;
  304.    buf[2] = (data >> 8 ) & 0xFF;
  305.    buf[3] = data & 0xFF;
  306.    return(buf);
  307. }
  308.  
  309.  
  310.