/* by pmsac@toxyn.org, 1998 */
/* Check your endian */
/* Reduced login to 9 characters and mantained password at 14   */
/* Used 4 of the missing bytes to produce an identical checksum */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* This is the type of the machine were you're running this */
/* iNTEL is a "YES", SPARC is a "NO", your's I don't know */
#define LITTLEENDIAN "NO"

#define EXTRA 2
#define MAXLOGIN 9
#define MAXPASSWORD 14
#define LOGINPLACE 0
#define PASSWORDPLACE 15
/* Don't forget iNTEL is little endian (IBM 8237 hubs use i386) */
/* and we're gonna put this in the firmware */
#define DIFFCHECKSUMHIPLACE 8
#define DIFFCHECKSUMLOPLACE 12
#define LASTFOURPLACE 28
#define FIRMWARELENGTH 32

/* Home brewed fpurge, you may have to comment it out */
void fpurge(FILE *stream) {
	fflush(stream);
	fseek(stream, 0L, SEEK_END);
}

void printBuffer(unsigned char *buffer, char *message, int length) {
int i;
	printf("\n%s", message);
	for (i = 0; i < length; i++)
		if (!isprint((unsigned int)buffer[i]))
			printf("\\x%02x", buffer[i]);
		else
			printf("%c", buffer[i]);
	printf("\n----\n");
}

unsigned long int checksumIt(unsigned long int *uliPtr) {
unsigned long int checksum = 0;
	checksum ^= *uliPtr++;
	checksum ^= *uliPtr++;
	checksum ^= *uliPtr++;
	checksum ^= *uliPtr++;
	checksum ^= *uliPtr++;
	checksum ^= *uliPtr++;
	checksum ^= *uliPtr++;
	checksum ^= *uliPtr;
	return checksum;
}

int main(void) {
/* Get this from the firmware (this is from v1.27 & v1.31) */
char firmwareChunk[] = {'I','5','r','D','v','2','b','2','J','j',
'A','8','M','m','\0','A','5','2','8','9','6','n','G','9','3','0','9','6',
'a','\0','\x02','\0'};
unsigned long int oldChecksum, newChecksum, diffChecksum;
char login[MAXLOGIN+EXTRA], password[MAXPASSWORD+EXTRA];
char newChunk[FIRMWARELENGTH+1];
unsigned long int *uliPtr;

	/* Compute old checksum */
	oldChecksum = checksumIt((unsigned long int *)firmwareChunk);

	/* Get new login */
	printf("New login (%i chars max): ", MAXLOGIN);
	fgets(login, MAXLOGIN+EXTRA, stdin);
	fpurge(stdin);
	if (login[strlen(login)-1] == '\n')
		login[strlen(login)-1] = '\0';
	login[MAXLOGIN] = '\0';

	/* Get new password */
	printf("New password (%i chars max): ", MAXPASSWORD);
	fgets(password, MAXPASSWORD+EXTRA, stdin);
	fpurge(stdin);
	if (password[strlen(password)-1] == '\n')
		password[strlen(password)-1] = '\0';
	password[MAXPASSWORD] = '\0';

	/* Concatenate new login/password */
	bzero(newChunk, FIRMWARELENGTH);
	strcat(newChunk, login);
	strcpy(&newChunk[PASSWORDPLACE], password);
	
	/* Last four bytes of original firmware have to enter here */
	memcpy(&newChunk[LASTFOURPLACE],
	       &firmwareChunk[LASTFOURPLACE], 4);

	/* Compute new checksum */
	newChecksum = checksumIt((unsigned long int *)newChunk);
	
	/* Compute differential checksum */
	diffChecksum = oldChecksum ^ newChecksum;
	
	/* Put differential checksum in newChunk */
	/* Watch out for those endians... */
	uliPtr = (unsigned long int *)&newChunk[DIFFCHECKSUMLOPLACE];
	if (strcmp(LITTLEENDIAN, "YES") == 0)
		*uliPtr |= (diffChecksum & 0x0000ffff);
	else
		*uliPtr |= (diffChecksum & 0xffff0000);
		
	uliPtr = (unsigned long int *)&newChunk[DIFFCHECKSUMHIPLACE];
	if (strcmp(LITTLEENDIAN, "YES") == 0)
		*uliPtr |= (diffChecksum & 0xffff0000);
	else
		*uliPtr |= (diffChecksum & 0x0000ffff);

	/* Confirm the checksuming */
	newChecksum = checksumIt((unsigned long int *)newChunk);
	if (newChecksum != oldChecksum) {
		printf("Check source for \"#define LITTLEENDIAN\"\n");
		exit(1);
	}

	/* Show firmware chunks to audience */
	printBuffer(firmwareChunk, "Old firmware chunk\n", FIRMWARELENGTH);
	printBuffer(newChunk, "New firmware chunk\n", FIRMWARELENGTH);

	/* Remember these are the new login and password */
	printf("\nNew magic login: %s\n", login);
	printf("New magic password: %s\n\n", password);
	
	/* Aplaude */
	return 0;
}
