/* ensteg.c
** do steganography stuff
** note that this assumes a binary-type pnm (P4,P5,P6) without a comment field
**
** usage:
**   steg image.pnm file.dat output.pnm
** enstegs the file in file.dat into image.pnm to form output.pnm.  Assumes
** that the destegged file can have trailing garbage without a problem
** (.tar.gz is fine).
*/

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *fp1 = fopen(argv[1], "r");
    FILE *fp2 = fopen(argv[2], "r");
    FILE *fout = fopen(argv[3], "w");
    int w, h, max;
    char buf, copybuf[1024];

    fscanf(fp1, "%1024s", copybuf);
    fscanf(fp1, "%d%d%d\n", &w, &h, &max);
    fprintf(fout, "%s\n%d %d\n%d\n", copybuf, w, h, max);

    // ensteg the data
    printf ("enstegging...\n");
    while (fread(&buf, 1, 1, fp2))
    {
	int i;

	for (i = 0; i < 8; i++)
	{
	    char b = fgetc(fp1);
	    char s = (b&0xFE) | ((buf>>i)&1);
	    fwrite(&s, 1, 1, fout);
	}
    }

    // copy the remainder of the file (random-pad everything though)
    printf ("copying rest/adding garbage...\n");
    while ((max = fread(copybuf, 1, 1024, fp1)))
    {
	int i;

	for (i = 0; i < 1024; i++)
	    copybuf[i] &= (0xFE | (rand()>>3));
	fwrite(copybuf, 1, max, fout);
    }

    printf ("done\n");

    // clean up, such as it is
    fclose (fp1);
    fclose (fp2);
    fclose (fout);

    return 0;
}

