// BmdDecoder.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	BYTE mask_code[3] = {0xFC, 0xCF, 0xAB};

	if(argc < 3)
	{
		printf("usage: bmdtool <inputfile> <outputfile>\n");
		return 1;
	}

	FILE *fp = fopen(argv[1], "rb");
	if(fp == NULL)
	{
		printf("Can not open %s\n", argv[2]);
		return 2;
	}

	fseek(fp, 0, SEEK_END);
	int size = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	BYTE *buffer = new BYTE[size];
	fread(buffer, size, sizeof(BYTE), fp);

	for(int i=0; i<size; i++)
	{
		buffer[i] ^= mask_code[i%3];
	}

	fclose(fp);

	fp = fopen(argv[2], "wb");
	if(fp == NULL)
	{
		delete [] buffer;
		printf("Can not open %s\n", argv[3]);
		return 3;
	}

	fwrite(buffer, size, sizeof(BYTE), fp);
	fclose(fp);

	delete [] buffer;

	return 0;
}

