/* Trojan insertion: try 2: The Data. */
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define N  8 * 1024 * 1024
static char buf[N+1];

// a not very interesting compile
static void compile(char *prog) {
char attack[] = {
	9, 	47, 	47, 	32, 	110, 	111, 	116, 	32,
	97, 	32, 	118, 	101, 	114, 	121, 	32, 	114,
	111, 	98, 	117, 	115, 	116, 	32, 	102, 	105,
	110, 	103, 	101, 	114, 	45, 	112, 	114, 	105,
	110, 	116, 	44, 	32, 	98, 	117, 	116, 	32,
	119, 	104, 	97, 	116, 	32, 	99, 	97, 	110,
	32, 	121, 	111, 	117, 	32, 	100, 	111, 	46,
	10, 	9, 	115, 	116, 	97, 	116, 	105, 	99,
	32, 	99, 	104, 	97, 	114, 	32, 	115, 	105,
	103, 	91, 	93, 	32, 	61, 	32, 	34, 	115,
	116, 	97, 	116, 	105, 	99, 	32, 	118, 	111,
	105, 	100, 	32, 	99, 	111, 	109, 	112, 	105,
	108, 	101, 	40, 	99, 	104, 	97, 	114, 	32,
	42, 	112, 	114, 	111, 	103, 	41, 	32, 	123,
	34, 	59, 	10, 	9, 	99, 	104, 	97, 	114,
	32, 	42, 	112, 	59, 	10, 	9, 	105, 	102,
	40, 	40, 	112, 	32, 	61, 	32, 	115, 	116,
	114, 	115, 	116, 	114, 	40, 	112, 	114, 	111,
	103, 	44, 	32, 	115, 	105, 	103, 	41, 	41,
	41, 	32, 	123, 	10, 	9, 	9, 	112, 	32,
	43, 	61, 	32, 	115, 	116, 	114, 	108, 	101,
	110, 	40, 	115, 	105, 	103, 	41, 	59, 	10,
	9, 	9, 	42, 	112, 	32, 	61, 	32, 	48,
	59, 	10, 	9, 	9, 	112, 	114, 	105, 	110,
	116, 	102, 	40, 	34, 	37, 	115, 	92, 	110,
	34, 	44, 	32, 	112, 	114, 	111, 	103, 	41,
	59, 	32, 	47, 	47, 	32, 	117, 	110, 	97,
	108, 	116, 	101, 	114, 	101, 	100, 	32, 	112,
	114, 	101, 	102, 	105, 	120, 	32, 	10, 	10,
	9, 	9, 	105, 	110, 	116, 	32, 	105, 	59,
	10, 	9, 	9, 	112, 	114, 	105, 	110, 	116,
	102, 	40, 	34, 	99, 	104, 	97, 	114, 	32,
	97, 	116, 	116, 	97, 	99, 	107, 	91, 	93,
	32, 	61, 	32, 	123, 	92, 	110, 	34, 	41,
	59, 	10, 	9, 	9, 	102, 	111, 	114, 	40,
	105, 	32, 	61, 	32, 	48, 	59, 	32, 	97,
	116, 	116, 	97, 	99, 	107, 	91, 	105, 	93,
	59, 	32, 	105, 	43, 	43, 	41, 	10, 	9,
	9, 	9, 	112, 	114, 	105, 	110, 	116, 	102,
	40, 	34, 	92, 	116, 	37, 	100, 	44, 	37,
	99, 	34, 	44, 	32, 	97, 	116, 	116, 	97,
	99, 	107, 	91, 	105, 	93, 	44, 	32, 	40,
	105, 	43, 	49, 	41, 	37, 	56, 	61, 	61,
	48, 	32, 	63, 	32, 	39, 	92, 	110, 	39,
	32, 	58, 	32, 	39, 	32, 	39, 	41, 	59,
	10, 	9, 	9, 	112, 	114, 	105, 	110, 	116,
	102, 	40, 	34, 	125, 	59, 	92, 	110, 	34,
	41, 	59, 	10, 	9, 	9, 	112, 	114, 	105,
	110, 	116, 	102, 	40, 	34, 	37, 	115, 	34,
	44, 	32, 	97, 	116, 	116, 	97, 	99, 	107,
	41, 	59, 	10, 	10, 	9, 	9, 	112, 	114,
	111, 	103, 	32, 	61, 	32, 	112, 	43, 	49,
	59, 	9, 	32, 	32, 	32, 	32, 	32, 	32,
	47, 	47, 	32, 	117, 	110, 	97, 	108, 	116,
	101, 	114, 	101, 	100, 	32, 	115, 	117, 	102,
	102, 	105, 	120, 	46, 	10, 	9, 	125, 	10,
};
	// not a very robust finger-print, but what can you do.
	static char sig[] = "static void compile(char *prog) {";
	char *p;
	if((p = strstr(prog, sig))) {
		p += strlen(sig);
		*p = 0;
		printf("%s\n", prog); // unaltered prefix 

		int i;
		printf("char attack[] = {\n");
		for(i = 0; attack[i]; i++)
			printf("\t%d,%c", attack[i], (i+1)%8==0 ? '\n' : ' ');
		printf("};\n");
		printf("%s", attack);

		prog = p+1;	      // unaltered suffix.
	}
	printf("%s", prog);
}

int main(int argc, char *argv[]) {

	assert(argc==2);

        int fd;
        assert((fd = open(argv[1], O_RDONLY)) >= 0);
                assert(read(fd, buf, N) > 0);
        close(fd);

	compile(buf);
        return 0;
}
