size_t read_file(const char *fname, char **content) {
	size_t fsize, readsize;
	char *buff;

	FILE *fd = fopen(fname, "rb");
	if (!fd) {
		fprintf(stderr, "%s not found\n", fname);
		return 0;
	}

	fseek(fd, 0, SEEK_END);
	fsize = ftell(fd);

	buff = (char *)malloc(fsize);
	rewind(fd);
	readsize = fread(buff, 1, fsize, fd);
	if (fsize != readsize) {
		fprintf(stderr, "could only read %zu/%zu bytes from %s\n",
			readsize, fsize, fname);
		free(buff);
		return 0;
	}
	buff[fsize] = '\0';

	printf("read %zu bytes from %s\n", fsize, fname);

	*content = buff;

	return fsize;
}

size_t write_file(const char *fname, const char *content, size_t fsize) {
	size_t wsize = 0;

	FILE *fd = fopen(fname, "wb");
	if (!fd) {
		fprintf(stderr, "cannot open %s for writing\n", fname);
		return wsize;
	}

	wsize = fwrite(content, 1, fsize, fd);
	if (wsize != fsize) {
		fprintf(stderr, "could only write %zu/%zu bytes from %s\n",
			wsize, fsize, fname);
	} else {
		printf("write %zu bytes to %s\n", fsize, fname);
	}

	return wsize;
}

