#include <stdio.h>
#include <errno.h>
#include <windows.h>

#define TITLE "swf version9 changer"

int main(int argc,char* argv[])
{
  FILE* f;
  unsigned char buf[4];
  char* s;
  if (argc != 2 || argv == NULL || argv[1] == NULL || argv[1][0] == '\0'){
    MessageBox(NULL,"usage: Drag&Drop swf to this exe\n  version 20110919.2000\n  copyright(c) 2011 Ituki Kirihara/NI All rights reserved.\n  support: http://fc.to/ituki/ or http://blog.proj.jp/ituki/",TITLE,MB_OK);
    return -1;
  }
  f = fopen(argv[1],"r+b");
  if (f == NULL){
    perror(NULL);
    MessageBox(NULL,"File open error",TITLE,MB_OK);
    return -2;
  }
  if (fread(buf,4,1,f) != 1){
    if(ferror(f)){
      perror(NULL);
      fclose(f);
      MessageBox(NULL,"File read error",TITLE,MB_OK);
      return -3;
    }
    fprintf(stderr,"fread fail\n");
    fclose(f);
    MessageBox(NULL,"File read error",TITLE,MB_OK);
    return -4;
  }
  if (!((buf[0] == 'C' || buf[0] == 'F') && buf[1] == 'W' && buf[2] == 'S')){
    fclose(f);
    fprintf(stderr,"not swf file\n");
    MessageBox(NULL,"File is not swf file",TITLE,MB_OK);
    return -5;
  }
  if (fseek(f,3,SEEK_SET) != 0){
    perror(NULL);
    fclose(f);
    MessageBox(NULL,"File error",TITLE,MB_OK);
    return -6;
  }
  if (fwrite("\x09",1,1,f) != 1){
    if(ferror(f)){
      perror(NULL);
      fclose(f);
      MessageBox(NULL,"File write error",TITLE,MB_OK);
      return -7;
    }
    fprintf(stderr,"fwrite fail\n");
    fclose(f);
    MessageBox(NULL,"File write error",TITLE,MB_OK);
    return -8;
  }
  if (fclose(f) != 0){
    perror(NULL);
    MessageBox(NULL,"File close error(file updated)",TITLE,MB_OK);
    return -9;
  }
  printf("%s (original swf version=%d) update version 9",argv[1],(int)buf[3]);
  MessageBox(NULL,"swf file version updated to 9",TITLE,MB_OK);
  return 0;
}

