/*
bpftp.c - reads and decrypts entries from bpftp.dat.
(c) Chopsui-cide/MmM 1999
The Mad Midget Mafia - http://midgets.box.sk/
Tested with MSVC++ 2.0 under Win95

This decrypts the entries from Bullet Proof FTP's site database (bpftp.dat).

Usage: bpftp <bpftp.dat>

Note: this does not attach any meaning/name to the data decrypted - it just
dumps it all (you'll have to figure this out yourself). Each record will be
seperated by a newline, and entries cannot be longer than 17 characters.

Feel free to modify this, so long as you give me credit as the original
author.
*/

#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Yuck.
const char key[17][17] = {
{8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{16, -80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{24, -123, -30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{32, 89, 58, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{40, 46, -111, -115, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{48, 2, -23, -97, -86, -115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{56, -42, 64, -79, 16, 51,  56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{64, -85, -105, -61, 118, -39, 16, -40, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{72, 127, -17, -43, -36, 126, -24, 14, -38, 0, 0, 0, 0, 0, 0, 0, 0},
{80, 83, 70, -25, 66, 36, -65, 68, -24, -3,  0, 0, 0, 0, 0, 0, 0},
{88, 40, -98, -7, -88, -55, -105, 122, -11, -91, 103, 0, 0, 0, 0, 0, 0},
{96, -4, -11, 11, 14,  111, 111, -81, 3, 77, -53, -97, 0, 0, 0, 0, 0},
{104, -47, 77, 29, 116, 21, 71, -27, 17, -11, 48, 56, 21, 0, 0, 0, 0},
{112, -91, -92, 47, -38, -70, 31, 27, 30, -99, -108, -48, -38, -25, 0, 0, 0},
{120, 121, -5, 65, 65, 96, -9, 81, 44, 69, -7, 105, -97, 31, -112, 0, 0},
{-128, 78, 83, 83, -89, 6, -50, -121, 58, -19,93, 1, 101, 87, -52, 70, 0},
{-120, 34, -86, 101, 13, -85, -90, -67, 71, -107, -62, -102, 42, -113, 7, -26, -109}
};


void decrypt(int clen, char *ciphertext);
int search_string(int str1len, char *string1, char *string2, int start);
int fsize(char *filename);
void fatal_error(char *message);

void main(int argc, char *argv[])
{
	char *data;
	FILE *fp;
	int c = 0, d, f = 0;
	int t1 = 0;
	int offset_start = 15, offset_end;
	char ciphertext[256];
	if (argc < 2)
	{
		printf("Usage: bpftp <bpftp.dat>\n");
		exit(1);
	}
	if ((fp = fopen(argv[1], "rb")) == NULL) fatal_error("Could not open file.");
	data = (char *)malloc(sizeof(data) * fsize(argv[1]));
	fread(data, sizeof(data), fsize(argv[1]), fp);
	do
	{
		if (offset_start > fsize(argv[1])) f = 1;
		if (data[offset_start] == 0)
		{
			do
			{
				offset_start++;
			} while(data[offset_start] == 0);
			printf("\n");
			t1 = 0;
		}
		offset_end = offset_start + data[offset_start];
		offset_start++;
		if (offset_start > fsize(argv[1])) f = 1;
		if (offset_end > fsize(argv[1])) f = 1;
		memset(&ciphertext, 0, 256);
		d = offset_start;
		do
		{
			ciphertext[d - offset_start] = data[d];
			d++;
		} while(d < offset_end + 1);
		decrypt(data[offset_start - 1], ciphertext); // (offset_end + 1) - 
		offset_start = offset_end + 1;
	} while(!f);
	fclose(fp);
	free(data);
}

void decrypt(int clen, char *ciphertext)
{
	int c = 0;
	do
	{
		printf("%c", ciphertext[c] ^ key[clen - 1][c]);
		c++;
	} while((c < clen) && (c < 18));
	printf("\n");
}

int search_string(int str1len, char *string1, char *string2, int start)
{
	int c;
	unsigned int d;
	int i = 0;
	int f;
	c = start;
	do
	{
		if (string1[c] == string2[0])
		{
			d = 0;
			f = 0;
			do
			{				
				if (string1[c + d] != string2[d]) f = 1;
				d++;
			} while((d < strlen(string2)) && (!f));
			if (!f)
			{
				return c;
			}
		}
		c++;
	} while(c < str1len);
	return -1;
}

int fsize(char *filename)
{
	int fh;
	int sz;
	fh = open(filename, O_RDONLY, S_IFREG);
	if (fh == -1) fatal_error("Could not open file.");
	sz = filelength(fh);
	close(fh);
	return sz;
}

void fatal_error(char *message)
{
	printf("Fatal error: %s\n", message);
	exit(1);
}
