#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <windows.h>

void main() {
	// local variables
	unsigned char* install_directory; // the key value
	unsigned long value_length; // the length of the key
	char *target_file;
	long ret_value=0;
	HKEY hKey, hSubKey;
	int find_and_patch_file(char *);



		// update the status window
		printf("Attempting to locate netscape install directory...\n");
		SleepEx(1000,FALSE);


		// try to open the registry key
		ret_value = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
			                 "Software",
							 0,
							 KEY_QUERY_VALUE,
							 &hKey
							);
		ret_value = RegOpenKeyEx(hKey,
			                 "Microsoft",
							 0,
							 KEY_QUERY_VALUE,
							 &hSubKey
							);
		hKey = hSubKey;

		ret_value = RegOpenKeyEx(hKey,
			                 "Windows",
							 0,
							 KEY_QUERY_VALUE,
							 &hSubKey
							);
		hKey = hSubKey;

		ret_value = RegOpenKeyEx(hKey,
			                 "CurrentVersion",
							 0,
							 KEY_QUERY_VALUE,
							 &hSubKey
							);
		hKey = hSubKey;

		ret_value = RegOpenKeyEx(hKey,
			                 "App Paths",
							 0,
							 KEY_QUERY_VALUE,
							 &hSubKey
							);
		hKey = hSubKey;

		ret_value = RegOpenKeyEx(hKey,
			                 "Netscape.exe",
							 0,
							 KEY_QUERY_VALUE,
							 &hSubKey
							);
		hKey = hSubKey;



		if (ret_value != ERROR_SUCCESS) { // if there's an error
			printf("Unable to find a valid Netscape installation.\n");
		}

		else {


			// first query value to get size of value
			ret_value = RegQueryValueEx(hKey,
			                        "Path",
									NULL,
									NULL,
									NULL,
									&value_length
									);

			install_directory = (unsigned char*)malloc(value_length);
			target_file = (char*)malloc(value_length+15); // add some for our filename

			ret_value = RegQueryValueEx(hKey,
				                        "Path",
										NULL,
										NULL,
										install_directory,
										&value_length
										);
			if (ret_value != ERROR_SUCCESS) {
					printf("Unable to get installation path.\n");
			}
			else {
				printf("Found Netscape installation.\n");
				SleepEx(1000,FALSE);

				target_file = install_directory; // convert to CString

				strcat(target_file,"\\resdll.dll");

				printf("Opening file %s\n",target_file);
				SleepEx(1000,FALSE);

				find_and_patch_file(target_file); // call the function to find the code and patch it


			}
		}
}

int find_and_patch_file(char *file)
{
	int file_handle;
	char* filename;

	// the code signature to look for in file
	unsigned char code_sig[35] = { 0x70, 0x72, 0x65, 0x66, 0x28, 0x22, 0x6d, 0x61, 0x69,
                  0x6c, 0x6e, 0x65, 0x77, 0x73, 0x2e, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f,
				  0x70, 0x61, 0x67, 0x65, 0x2e, 0x65, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64,
				  0x22, 0x2c }; // pref("mailnews.start_page.enabled",
	unsigned char patch_bytes[5] = { 0x66, 0x61, 0x6c, 0x73, 0x65 }; // "false"
	int index = 0; // the index into the code_sig array
	unsigned char read_byte; // a 1 byte buffer

	// open the file

	if ((file_handle = open(file, O_RDWR | O_BINARY)) == -1) {
		printf("Error: Can't open file. Please make sure Netscape is closed.\n");
		return (0); // failure
	}


	printf("Finding Code Signature\n");
	SleepEx(1000,FALSE);

	while (file_handle != (int)NULL) {


		// we've read the entire string from file
		if (index == 35) {
			printf("Found Code Signature\n");
			SleepEx(1000,FALSE);

			printf("Patching file\n");
			SleepEx(1000,FALSE);

			write(file_handle, &patch_bytes, 5); // patch the file

			printf("Done.\n");
			SleepEx(1000,FALSE);


			return (1); // success
		}



		read(file_handle, &read_byte, 1); // read a byte from the file

		if (read_byte == code_sig[index]) {
			++index;
		}
		else {
			index = 0; // reset index if ever a mismatch
		}

	}
	printf("Error: Code Signature Not Found.\n");
	return (0); // fail
}
