/*   IFFWizard v1.10
 *   written by Stefan Zeiger in 9/1991
 *   (c) 1991 by ! WIZARD WORKS !
 *   FREEWARE.
 */


#include <exec/types.h>


#define NUMCHUNKS 117
#define NUMWRAPS   61


char *Chunks[]=
{
  "    ",  /*   0 */
  "OPGM",
  "OCPU",
  "OCMP",
  "OSN ",
  "UNAM",
  "VHDR",
  "ATAK",
  "RLSE",
  "BODY",
  "CHAN",  /*  10 */
  "PAN ",
  "SEQN",
  "FADE",
  "ABIT",
  "COMM",
  "SSND",
  "MARK",
  "INST",
  "MIDI",
  "AESD",  /*  20 */
  "APPL",
  "COMT",
  "FSQN",
  "ANHD",
  "DLTA",
  "DPAN",
  "DRHD",
  "PPRF",
  "CMAP",
  "FONS",  /*  30 */
  "DASH",
  "AROW",
  "FILL",
  "LAYR",
  "ATTR",
  "BBOX",
  "VBM ",
  "CPLY",
  "OPLY",
  "GRUP",  /*  40 */
  "STXT",
  "TPTH",
  "FHDR",
  "FRAM",
  "POLY",
  "CHRS",
  "NEST",
  "TEXT",
  "FSCC",
  "BMHD",  /*  50 */
  "GRAB",
  "DEST",
  "SPRT",
  "CAMG",
  "CLUT",
  "CRNG",
  "CCRT",
  "DPPV",
  "DPPS",
  "3DCM",  /*  60 */
  "3DPA",
  "ASDG",
  "BHBA",
  "BHCP",
  "BHSM",
  "CTBL",
  "DYCP",
  "DCTV",
  "DGVW",
  "DPI ",  /*  70 */
  "DRNG",
  "EPSF",
  "ATXT",
  "PTXT",
  "TMAP",
  "OFHD",
  "KERN",
  "CHDEF",
  "FAIL",
  "REGS",  /*  80 */
  "VERS",
  "STAK",
  "UDAT",
  "XY  ",
  "PICT",
  "PRHD",
  "FONT",
  "ICTL",
  "INPT",
  "OSCN",  /*  90 */
  "PGFX",
  "SCRM",
  "SERL",
  "WBCF",
  "INPF",
  "OUTF",
  "PMSG",
  "PSEQ",
  "SHDR",
  "INS1",  /* 100 */
  "TRAK",
  "COLR",
  "DOC ",
  "FOOT",
  "HEAD",
  "PARA",
  "TABS",
  "PAGE",
  "PCTS",
  "PINF",  /* 110 */
  "(C) ",
  "ANNO",
  "AUTH",
  "CSET",
  "FVER",
  "NAME"
};

char *Comments[]=
{
  "Empty Fill chunk",                                   /*  0 */
  "Originating ProGraM",
  "Originating CPU",
  "Originating CoMPuter",
  "Originating Serial Number : ... or network address",
  "User NAMe",
  "Voice HeaDeR : Info about sample",
  "ATtAcK info",
  "ReLeaSE info",
  "BODY : The main data",
  "stereo CHANnel : Info about stereo sample",          /* 10 */
  "stereo PAN : Balance info for digi-sound",
  "SEQueNce : Loop data",
  "FADE in/out",
  "Amiga BITmap : Uncompressed picture",
  "COMMon : Description of digi-sound",
  "Sample SouND : The sample data",
  "MARKer : Marker between frames",
  "INSTrument : MIDI information",
  "MIDI : MIDI-exclusive information",
  "AES audio recording Data : ???",                     /* 20 */
  "APPLication specific : ???",
  "COMmenT",
  "Frame SeQueNce : How to animate the following frames",
  "ANimation HeaDer : Instead of BMHD ???",
  "DeLTA compressed data",
  "DPaint ANim brush",
  "DRawing HeaDer : Virtual position of object",
  "Page PReFerences : Preferences for ProVektor",
  "Color MAP",
  "FONt Specification : Text Font",                     /* 30 */
  "line DASH : Pattern for lines ???",
  "ARrOWhead pattern : Pattern for arrowheads",
  "FILL pattern : ???",
  "define LAYeR : Definition of the plain of an object",
  "object ATTRibutes",
  "Bounding BOX : Border for the following object",
  "Virtual BitMap : Bitmap for an object",
  "Closed PoLYgon : Definition of a closed polygon",
  "Open PoLYgon : Definition of an open polygon",
  "GRoUP : Following objects linked in FORM ???",       /* 40 */
  "Simple TeXT : Text in a drawing",
  "Text string along PaTH : Text shall follow this line",
  "Fantavision HeaDeR",
  "FRAMe definition",
  "POLYgon definition",
  "CHaracteRS : Text",
  "Change a nesting level",
  "TEXT : Unformatted ASCII text",
  "Font, Style, Color for previous TEXT chunk",
  "BitMap HeaDer : How to handle picture data",         /* 50 */
  "GRAB : Hot spot of a brush",
  "DESTination : How to insert bitmap into picture",
  "SPRiTe info : Priority of a sprite",
  "Commodore AMiGa : Amiga video mode",
  "Color LookUp Table : 8 bit special color table",
  "Color register RaNGe : Color cycling",
  "Color Cycling Range and Time",
  "DPaintII PerspectiVe : Perspective of rotating objects",
  "DPaint III specific chunk",
  "3D Color Map : Second picture CMAP (3D)",            /* 60 */
  "3D PArallax : Info about stereo parallax (3D)",
  "ASDG : ASDG specific chunk",
  "Alternative BMHD for brushes (Photon Paint)",
  "Alternative BMHD for full images (Photon Paint)",
  "Alternative BMHD (PhotonPaint) ???",
  "ham Color TaBLe : (NewTek)",
  "For NewTek's Dynamic HAM mode",
  "<reserved>",
  "DiGi VieW : For 21 bit color pictures",
  "Dots Per Inch",                                      /* 70 */
  "DPaint's enhanced color cycle",
  "Encapsulated PostScript Format",
  "Amiga TeXT : Bitmap text (VideoTitler)",
  "Polygon TeXT (ILBM) / Printer TeXT (PREF)",
  "Transparency MAP : <reserved>",
  "Font Header",
  "KERNing pair : Space between 2 characters",
  "CHaracter DeFinition : Outline character data",
  "FAILure : Environment snapshot at a GURU",
  "REGiSter dump : CPU registers",                      /* 80 */
  "program VERSion (PGTB/PTCH)",
  "STAcK dump : Stack contents",
  "User DATa : Values parsed by CATCH.O linked prg.",
  "XY : Position on raster/screen",
  "PICTure : QuickDraw picture",
  "PRef HeaDer : <not used> ???",
  "FONT : Workbench (PREF) / FONT-id : (WORD)",
  "Intuition ConTroL : ???",
  "INPuT control",
  "OverSCaN : ... parameters",                          /* 90 */
  "Printer GraFX",
  "SCReen Mode",
  "SERiaL : Parameters for serial port",
  "WorkBench ConFiguration",
  "INPut File",
  "OUTput File",
  "Patch MeSsaGe : Screen message",
  "Patch SEQuence : Patch data",
  "Score HeaDeR : Score info (Speed, Volume, Tracks)",
  "INStrument v1 : MIDI instrument information",       /* 100 */
  "TRAcK data : Musical score for 1 MIDI track",
  "COLoR translation table : For ISO compatibility",
  "begin DOCument section",
  "FOOTer",
  "HEADer",
  "PARAgraph format",
  "TAB Stops : New TAB positions",
  "PAGE breaker : Empty chunk, signal for new page",
  "begin PiCTure Section",
  "Picture INFo",                                      /* 110 */
  "© : Copyright",
  "ANNOtations",
  "AUTHor : Name of the author",
  "Character SET",
  "File VERsion",
  "NAME : e.g. of a picture or sound"
};

char *Wraps[]=
{
  "8SVX", /*  0 */
  "ACBM",
  "AIFC",
  "AIFF",
  "ANBM",
  "ANIM",
  "AVCF",
  "AVCO",
  "AVEV",
  "BANK",
  "BBSD",  /* 10 */
  "C100",
  "CHBM",
  "CLIP",
  "CPFM",
  "DCCL",
  "DCPA",
  "DCTV",
  "DECK",
  "DR2D",
  "DRAW",  /* 20 */
  "FANT",
  "FIGR",
  "FNTR",
  "FNTV",
  "FTXT",
  "GSCR",
  "HEAD",
  "ILBM",
  "IOBJ",
  "ITRF",  /* 30 */
  "MIDI",
  "MOVI",
  "MSCX",
  "MSMP",
  "MTRX",
  "NSEQ",
  "OFNT",
  "PDEF",
  "PGTB",
  "PICS",  /* 40 */
  "PLBM",
  "PREF",
  "PRSP",
  "PTCH",
  "RGB4",
  "RGB5",
  "RGB8",
  "RGBN",
  "RGBX",
  "ROXN",  /* 50 */
  "SAMP",
  "SC3D",
  "SHAK",
  "SMUS",
  "SYTH",
  "TDDD",
  "USCR",
  "UVOX",
  "VDEO",
  "WORD"   /* 60 */
};

char *WrapCom[]=
{
  "8 bit Sample VoX : Digi sound",                      /*  0 */
  "Amiga Contiguous BitMap : Picture",
  "Audio IFf Compression : Compressed digi sound",
  "Audio IFF : Digi sound",
  "ANimated BitMap : Animation",
  "cel ANIMation",
  "Amiga Vision Flow ??? : ???",
  "Amiga Vision COmmands : ???",
  "Amiga Vision EVents : ???",
  "Midi data",
  "BBS Database : Data base file",                      /* 10 */
  "Cloanto italia ??? : Text",
  "CHunky BitMap : ???",
  "CLIPboard : ???",
  "Cloanto Personal Font Maker : Font",
  "DCtv paint CLip : ???",
  "DCtv paint PAlette : Palette",
  "DCTV raw picture file : Picture",
  "CanDo file ???",
  "DRawings 2D : 2D vector graphic",
  "<reserved>",                                         /* 20 */
  "FANTavision : Picture",
  "<reserved for Deluxe Video>",
  "FoNT Raster : Raster font",
  "FoNT Vector : Vector font",
  "Formattet TeXT : Text",
  "General-use musical SCoRe : Music",
  "HEADings : Text",
  "InterLeaved BitMap : Picture",
  "<reserved>",
  "<reserved>",                                         /* 30 */
  "MIDI : Midi data",
  "???",
  "MuSiC-X : ???",
  "<reserved>",
  "MaTRiX : Function plotter data",
  "Numerical SEQuence : ???",
  "Outline FoNTs : Font data",
  "Page DEFinition : Deluxe Print ???",
  "ProGram TraceBack : GURU error traceback snapshot",
  "Macintosh picture",                                  /* 40 */
  "<should not be used>",
  "PREFerences : Workbench 2.0 preferences",
  "PeRSPective move : DPaint IV perspective move",
  "PaTCH : Binary patch file",
  "12 bit color picture",
  "15 bit color picture",
  "24 bit color picture",
  "24 bit color picture R+G+B+Transparent",
  "<reserved>",
  "Animation",                                          /* 50 */
  "sound SAMPle : Digi sound",
  "SCene 3D : Raytracing scene",
  "SHAKespeare : Contains ILBM pictures",
  "Simple MUSical score : Musical score",
  "System-exclusive driver for SoundQuest",
  "3-D raytracing picture",
  "Uhuru sound software musical SCoRe",
  "Uhuru sound software macintosh VOX",
  "ViDEO : Deluxe Video format",
  "WORD processing : Complex texts (ProWrite)"          /* 60 */
};


void main(int argc,char **argv)
{
  void __regargs Ex(char *,int);
  void __regargs Analyze(int,ULONG,USHORT);
  BOOL isiff=FALSE;
  int handle=-1;
  char buffer[5];
  buffer[4]=0;
  if( (argc!=2) || (strcmp(argv[1],"?")==0) ) Ex("\033[32mUsage: IFFWizard <filename>\033[31m",-1);
  handle=open(argv[1],0);
  if(handle==-1) Ex("Can't open file.",-1);
  read(handle,buffer,4);
  lseek(handle,(LONG)(-4),1);
  if(!strncmp(buffer,"FORM",4)) isiff=TRUE;
  if(!strncmp(buffer,"LIST",4)) isiff=TRUE;
  if(!strncmp(buffer,"CAT ",4)) isiff=TRUE;
  if(!isiff) Ex("No IFF file.",handle);
  printf("\n\033[32mIFFWizard\033[31m v1.10 - written by Stefan Zeiger\n© 1991 by \033[33m! WIZARD WORKS !\033[31m\n\n");
  Analyze(handle,8L,1);
  Ex(NULL,handle);
}


void __regargs Ex(char *failstring,int handle)
{
  if(handle!=-1) close(handle);
  if(failstring!=NULL) printf("\033[33m%s\033[31m\n",failstring);
  else puts("\033[31m");
  exit(0L);
}


void __regargs Analyze(int handle,ULONG totlen,USHORT intab)
{
  void tabu(USHORT);

  char buffer[5];
  ULONG longword;
  USHORT count;

  buffer[4]=0;

  while(totlen>7)
  {
    if(read(handle,buffer,4)<4) return;
    if(read(handle,&longword,4)<4) return;
    if(!strncmp(buffer,"FORM",4))
    {
      if(read(handle,buffer,4)<4) return;
      totlen-=12L;
      tabu(intab);
      printf("\033[32mFORM %s\033[31m     \t\033[33m%7ld\033[31m ",buffer,longword);
      for(count=0;count<NUMWRAPS;count++) if(!strncmp(buffer,Wraps[count],4)) printf(WrapCom[count]);
      printf("\n");
      Analyze(handle,longword,intab+1);
      totlen-=longword;
    }
    else
    if(!strncmp(buffer,"LIST",4))
    {
      if(read(handle,buffer,4)<4) return;
      totlen-=12L;
      tabu(intab);
      printf("\033[33mLIST\033[32m %s\033[31m     \t\033[33m%7ld\033[31m ",buffer,longword);
      for(count=0;count<NUMWRAPS;count++) if(!strncmp(buffer,Wraps[count],4)) printf(WrapCom[count]);
      printf("\n");
      Analyze(handle,longword,intab+1);
      totlen-=longword;
    }
    else
    if(!strncmp(buffer,"CAT ",4))
    {
      if(read(handle,buffer,4)<4) return;
      totlen-=12L;
      tabu(intab);
      printf("\033[33mCAT\033[32m  %s\033[31m     \t\033[33m%7ld\033[31m ",buffer,longword);
      for(count=0;count<NUMWRAPS;count++) if(!strncmp(buffer,Wraps[count],4)) printf(WrapCom[count]);
      printf("\n");
      Analyze(handle,longword,intab+1);
      totlen-=longword;
    }
    else
    if(!strncmp(buffer,"PROP",4))
    {
      if(read(handle,buffer,4)<4) return;
      totlen-=12L;
      tabu(intab);
      printf("\033[33mPROP\033[32m %s\033[31m     \t\033[33m%7ld\033[31m ",buffer,longword);
      for(count=0;count<NUMWRAPS;count++) if(!strncmp(buffer,Wraps[count],4)) printf(WrapCom[count]);
      printf("\n");
      Analyze(handle,longword,intab+1);
      totlen-=longword;
    }
    else
    {
      tabu(intab);
      printf("\033[32m%s ····\033[31m     \t\033[33m%7ld\033[31m ",buffer,longword);
      for(count=0;count<NUMCHUNKS;count++) if(!strncmp(buffer,Chunks[count],4)) printf(Comments[count]);
      printf("\n");
      totlen-=8L;
      longword+=(longword%2);
      /* totlen+=(longword%2);   I'm not sure about this line */
      lseek(handle,longword,1);
      totlen-=longword;
    }
  }
}


void tabu(USHORT num)
{
  USHORT i;
  printf("\033[33m%d\033[31m ",num);
  for(i=0;i<num;i++) printf("|-");
}
