// strip the MAC header from MAC binary files
//
// gcc StripMAC.c -o StripMAC

#include <stdio.h>

typedef struct
  {
  unsigned short Length;
  char Name[63];
  } name_t;

typedef struct
  {
  unsigned long Extension;
  unsigned long Identification;
  unsigned char boh[10];
  unsigned long Size;
  unsigned char boh2[40];
  } header_t;

int main(int argc, char *argv[])
  {
  FILE *hInput, *hOutput;
  char szInput[100];
  char szOutput[100];

  printf("StripMAC 0.9ß - "__DATE__" (trance@mail.org)\n\n");

  if (argc<2)
    {
    printf("%s Input {Output}\n",argv[0]);
    return(10);
    };

  bzero(szInput,sizeof(szInput));
  bzero(szOutput,sizeof(szInput));

  strcpy(szInput,argv[1]);

  if (argc>3) strcpy(szOutput,argv[2]);

  if ( ( hInput = fopen( szInput, "r" ) ) )
    {
    name_t name;
    header_t header;

    extern errno;

    fread(&name,sizeof(name),1,hInput);
    fseek(hInput,65,SEEK_SET);
    fread(&header,sizeof(header),1,hInput);

//    printf("[%d] %s %d\n",name.Length,name.Name,header.Size);

    if ( !( strlen(szOutput) ) ) strncpy( szOutput, name.Name, name.Length );

    if ( ( hOutput = fopen( szOutput, "w" ) ) )
      {
      unsigned char *pBuffer;

      fseek(hInput,128,SEEK_SET);

      if ( ( pBuffer = (unsigned char *)malloc( header.Size ) ) )
        {
        printf("Reading %d bytes.\n", header.Size);

        if ( ( fread(pBuffer,header.Size,1,hInput) ) == 0 )
          {
          if ( ( errno ) )
            {
            perror("Read()");
            goto Skip;
            };
          };

        printf("Writing %s.\n", szOutput);

        if ( ( fwrite(pBuffer,header.Size,1,hOutput) ) == 0 )
          {
          if ( ( errno ) )
            {
            perror("Write()");
            goto Skip;
            };
          };

Skip:

        free( pBuffer );
        }
      else
        {
        #define BUFFER_SIZE 0x10000

        if ( ( pBuffer = (unsigned char *)malloc( BUFFER_SIZE ) ) )
          {
          int size, position;

          printf("Writing %d bytes (%s).\n",header.Size,szOutput);

          while( !feof( hInput ) )
            {
            position = ftell(hInput);

            printf("Reading %d\r", position);
            if ( ( fread(pBuffer,BUFFER_SIZE,1,hInput) ) == 0 )
              {
              if ( ( errno ) )
                {
                perror("Read()");
                break;
                };
              };

            size = ftell(hInput)-position;

            printf("Writing %d\r", ftell(hInput)-128);
            if ( ( fwrite(pBuffer,size,1,hOutput) ) == 0 )
              {
              if ( ( errno ) )
                {
                perror("Write()");
                break;
                };
              };
            };

          printf("\n");

          free( pBuffer );
          }
        else printf("Error: insufficient memory available.\n");
        };

      fclose( hOutput );
      }
    else printf("Error: unable to open output file (%s).\n", szOutput);

    fclose( hInput );
    }
  else printf("Error: unable to open input file (%s).\n", szInput);

  return(0);
  };
