Anonymous Anonymous - 6 months ago
238 0

No description

C

open ppm p3 file and embed into

//////////////////////////////////////////////////////////////
/*
	execute as ./emb -i <img2embed> -i <text file> -o <embedIMG>

*/
//////////////////////////////////////////////////////////////


#include<stdio.h>
#include<stdlib.h>
#include<string.h>


typedef struct {
    int x, y;
    unsigned char *pixel;
} PPMImage;

#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{
	FILE * fp;
	PPMImage *img;
    	int rgb_comp_color;
	int size = 0;
	fp = fopen(filename, "a+");

	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	
	unsigned char *buff;
	unsigned char stuff[16];
	int c;
	int x,y;
	buff = (unsigned char*) malloc(sizeof(unsigned char)*size +1);
	memset(buff, '\0', sizeof(unsigned char)*size+1);

    	fgets(stuff, sizeof(stuff), fp);
    
    if (stuff[0] != 'P' || stuff[1] != '3') {
	fprintf(stderr, "Invalid image format (must be 'P3')\n");
	exit(1);
    }

    //alloc memory form image
    img = (PPMImage*)malloc(sizeof(PPMImage));
    if (!img) {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }
    
    c = getc(fp);
    while (c == '#') {
        while (getc(fp) != '\n') ;
        c = getc(fp);
    }
    
    ungetc(c, fp);

    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
        fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
        exit(1);
    }
    
    if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
        fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
        exit(1);
    }
    if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
        fprintf(stderr, "'%s' does not have 8-bits components\n",filename);
        exit(1);
    }
   	
	//printf("x: %d y: %d\n", img->x, img->y);
	
	unsigned char buffer[1024];
	memset(buffer,0,1024);	
	fgets(buffer,1024,fp);
	 
	fread(buff, 1, size, fp);	
	
	img->pixel = buff;
/*	
	for(int h = 0; h < 20; h++)
	{
		printf("%c", buff2[h]);
		
	}
	printf("%s", buff2);
*/
	fclose(fp);

	return img;

} 

void writePPM(const char *filename, unsigned char * img, int x, int y)
{
    FILE *fp;
    //open file for output
    fp = fopen(filename, "wb");
    if (!fp) {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        exit(1);
    }
    
    //write the header file
    //image format
    fprintf(fp, "P3\n");
    
    //comments
   // fprintf(fp, "# Created by %s\n",CREATOR);
    
    //image size
    fprintf(fp, "%d %d\n",x,y);
    
    // rgb component depth
    fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);
    
    // pixel pixel
    fwrite(img,1, strlen(img), fp);
    fclose(fp);
}

//unsigned char * embBits(PPMImage *img, int messageLen, unsigned char*msgOne, unsigned char *encMsg)
int embBits(PPMImage *img, int messageLen, unsigned char*msgOne, int embLen)
{
	//int count = 2;
	int embCount = 0;
	int count = 1;
	unsigned char *eMsg;	
	unsigned char byte;
	int mask;
	unsigned char update;
	unsigned char flipOne = 0x01;  //0x01
	unsigned char flipZero = 0xFE;  //0x00
	int skip = 0;
	unsigned char c;

	for(int g=0;g<messageLen *16;g++)
	{
		printf("%c", img->pixel[g]);
	}

	printf("\njust a space\n");	
	const char * right;	
	unsigned char *test = strtok(img->pixel, "\n");
	while(test){
		//printf("test: %s\n", test);
		test = strtok(NULL, "\n");
	}
	//printf("right: %s", right);
	for(int j = 0; j < messageLen-1; j++)
	{
		
		byte = msgOne[j];
		//encMsg[j] = byte;
/*
		if(img->pixel[count] == '\0')
		{	
			printf("warning - embedding into newline\n");
		}

		if(img->pixel[count] == ' ')
		{	
			printf("warning - embedding into space\n");
		}

*/
		printf("byte: %c\n", byte);	
		for(int k=7; -1 < k; k--)
		{
			/*
			if(img->pixel[count+1] != ' ' || img->pixel[count+1] != '\0')
			{
				//printf("warning - you've lost your place\n");
				count = count +1;
			}*/
/*
			if(img->pixel[count] == '\0')
			{	
			//	printf("warning - embedding into newline\n");
				count = count + 3;
			}
*/
/*
			if(img->pixel[count] == '\0' || img->pixel[count] == '\n' || img->pixel[count] == '\r' || img->pixel[count] == '\t')
			{	
			//	printf("warning - embedding into newline\n");
				count = count + 3;
			}
*/
/*			if(img->pixel[count] == 0x0D && img->pixel[count+1] == 0x0A)
			{	
			//	printf("warning - embedding into newline\n");
				count = count + 2;
			}
*/
			if(img->pixel[count] == ' ')
			{	
			//	printf("warning - embedding into space\n");
				count = count + 2;
			}

			if(img->pixel[count] == '\r')
			{	
			//	printf("warning - embedding into space\n");
				count = count + 2;
			}



			/*if(embCount == 24)
			{
				count = count + 1;
				embCount = 0;
			}
*/
			update = byte;	
		//	printf("update: %c\n", update);	
		//	printf("update: %x\n", update);	
			update = update & (1<<k);
		//	printf("update: %c\n", update);	
		//	printf("update: %x\n", update);	
			//printf("pixel:%c\n", img->pixel[count]);		
			//printf("pixel+1:%c\n", img->pixel[count+1]);		
			//printf("pixel+2:%c\n", img->pixel[count+1]);		
			if(update == 0)
			{
				// if i see 1   |=
				// if i see a 0 &=
				//img->pixel[count] = img->pixel[count] &= flipZero;		
				printf("&& this pixel: %c\n", img->pixel[count]);
				//printf("flipZero: %x\n", flipZero);
			//	printf("byte: %c\n", byte);	
			//	printf("update: %c\n", update);	
				img->pixel[count] &= flipZero;		
			}	
			else
			{
				//flip bit
					//check LSB and FLIP 
				//	img->pixel[count] = img->pixel[count] |= flipOne;		
				printf("|| this pixel: %c\n", img->pixel[count]);
				img->pixel[count] |= flipOne;		
			}		
			//eMsg[count] = img->pixel[count];
			//printf("count: %d\n", count);
			
				
			embCount++;	
			count = count +1;
		}	
//		eMsg[j] = byte;	
	}

	//return encMsg = eMsg;
	//unsigned char *yes = "sucess";
	/*
	for(int a = 0; a < messageLen; a++)
	{
		printf("pixel: %c", img->pixel[a]);		
		printf("msg: %c\n", eMsg[a]);		
	//	eMsg[a] = img->pixel[a];
	}
*/
	
	embCount = count;	
	return embLen = embCount;
}

int main(int argc, char **argv){
	
	int messageLen;
	int i = 0; 
	PPMImage *img;
	int size = 0;
	FILE * fp;
	int testSize;
	fp = fopen(argv[4], "a+");
	
	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	unsigned char *buff;
	buff = (unsigned char*) malloc(sizeof(unsigned char)*size +1);
	memset(buff, '\0', sizeof(unsigned char)*size+1);
	fread(buff, 1, size, fp);
	
	fclose(fp);
	printf("text encryption: %s\n", buff);
	testSize = strlen(buff);	
	printf("Size of text %d\n", testSize);
   	
	messageLen = strlen(buff); 
 
	img = readPPM(argv[2]);
/*
	int testing = strlen(img->pixel);
	for (int f=0;f<6;f++)
	{	
		//f2 = 1
		//f3 = 6
		printf("%c", img->pixel[f]);			
	}		
*/

//	printf("%c \n", img->pixel[2]);
//	printf("%c \n", img->pixel[5]);
	printf("\n");
//	unsigned char * encMsg;
	
	int encMsg = 0;			
	encMsg = embBits(img, messageLen, buff, encMsg);
	
//	printf("cipher msg:%s\n", img->pixel);
	printf("message length: %d\n", messageLen);
//	printf("cipher msg length: %d\n", encMsg);
	
		
	writePPM(argv[6], img->pixel, img->x, img->y);

	printf("Please press enter to complete\n");
	getchar();
}
Comments