Exemplo n.º 1
0
int enumResources(res_majorid_t id
	, int (*fct)()
	, void * const arg)
{
	dprintf(("[RES: searching file '%s']\n", comResFile()));
	return enumFileResources(comResFile(), id, fct, arg);
}
Exemplo n.º 2
0
main(int argc, char **argv)
{	struct EXE_header exe;
	unsigned tosize;

	if(argc <= 1) {
		puts("Patch heap size into FreeCOM executable\n"
			"\nuseage: PTCHSIZE freecom [{ size }]\n"
			"\nSize may be given hexadecimal (0x###) or decimal.\n"
			"If 'KB' is appended to size, it's multiplied with 1024.\n"
			"A leading plus sign is ignored.\n"
			"A plus sign by its own is treated as estimated minimum size.\n"
			"More than one sizes are summed up.\n"
			"If size evaluates to 0 (zero), the restriction is disabled.\n"
			"\ne.g.: PTCHSIZE freecom + 5K\n"
			"means: reserve 5*1024 bytes in addition to minimum size."
		);
		return 127;
	}

	if(enumFileResources(argv[1], RES_ID_INFO, getInfo, 0) != 1) {
		puts("Failed to locate FreeCOM's info resource\n"
			"Possible errors:\n"
			"\tAn error is issued above.\n"
			"\tThe file could not be opened.\n"
			"\tThe version of FreeCOM is too old.\n"
			"\tFile read error.");
		return 80;
	}

	if((freecom = fopen(argv[1], "r+b")) == 0) {
		printf("Cannot open FreeCOM: %s\n", argv[1]);
		return 30;
	}

	if(fread(&exe, sizeof(exe), 1, freecom) != 1) {
		printf("Read error from: %s\n", argv[1]);
		return 44;
	}
	/* exe.extraMin is not trustworthy as it might have been tweak
		in a previous run */

	assert(info);
	/* Decompose the info resource */
	getInfoValues();
	minSize = ival.extraSpace				/* default stuff */
		+ 5 * (unsigned long)ival.bufsize	/* command lines */
		+ 3 * (64 + 3 + 8 + 5)				/* filenames */
		+ 256								/* env var buffer %PATH% etc */
		;
	addUns(ival.alias);
	addUns(ival.hist);
	addUns(ival.dirs);
	if(minSize >= INT_MAX) {
		puts("Estimated minimum heap size exceeds 64KB.\n"
			"There is most probably a corrupted info resource.");
		return 71;
	}

	if(argc == 2 || ival.heapPos == ~0) {
		prUns(ival.alias, "Aliases");
		prUns(ival.hist, "Command line history");
		prUns(ival.dirs, "Directory stack");
		prUns(ival.bufsize, "Command line buffer");
		if(ival.heapPos == ~0)
			puts("Missing heap position -> cannot change heap size");
		else {
			if(fseek(freecom, ival.heapPos, SEEK_SET) != 0) {
				printf("Failed to seek to heap size offset in %s\n", argv[1]);
				return 43;
			}
			assert(sizeof(tosize) == 2);
			if(fread(&tosize, sizeof(tosize), 1, freecom) != 1) {
				puts("Failed to read heap size from FreeCOM executable.");
				return 76;
			}
			fclose(freecom);
			if(tosize)
				printf("Heap size restricted to %u bytes\n"
				 , tosize);
			else
				puts("Heap size is currently not restricted");
		}
		printf("Estimated minimum of heap: %u\n", (unsigned)minSize);
		return 0;
	}

	tosize = 0;
	argc = 1;
	while(argv[++argc])
		addSize(&tosize, argv[argc]);
	if(tosize && tosize < minSize) {
		printf("size smaller than estimated minimum size: %u\n"
		 , (unsigned)minSize);
		return 72;
	}

	printf("Patching '%s' to heap size of %u bytes\n"
	 , argv[1], tosize);
	if(tosize)
		exe.extraMin = exe.extraMax = ival.extraSpace + tosize / 16;
	else {
		exe.extraMax = 0xffff;
		exe.extraMin = ival.extraSpace;
	}
	rewind(freecom);
	if(fwrite(&exe, sizeof(exe), 1, freecom) != 1) {
		printf("Failed to patch heap size into FreeCOM executable.\n"
			"File most probably corrupted now: %s\n", argv[1]);
		return 77;
	}

	if(fseek(freecom, ival.heapPos, SEEK_SET) != 0) {
		printf("Failed to seek to heap size offset in %s\n", argv[1]);
		return 42;
	}
	assert(sizeof(tosize) == 2);
	if(fwrite(&tosize, sizeof(tosize), 1, freecom) != 1) {
		printf("Failed to patch heap size into FreeCOM executable.\n"
			"File most probably corrupted now: %s\n", argv[1]);
		return 75;
	}

	fflush(freecom);
	if(ferror(freecom)) {
		printf("Failed to patch heap size into FreeCOM executable.\n"
			"File most probably corrupted now: %s\n", argv[1]);
		return 76;
	}
	fclose(freecom);
	return 0;
}
Exemplo n.º 3
0
int enumResources(res_majorid_t id
	, int (*fct)()
	, void * const arg)
{
	return enumFileResources(comFile(), id, fct, arg);
}