title: Steganography in C
body: <p>Trying to do basic Steganography on a PPM Image.</p>
  
  <p>I have the basic algorithm completed. Read in the file, check the header starts with P6, get the image width and height, and the pixel data.</p>
  
  <p>I need to have a total of four methods: ReadPPM, WritePPM, WriteMsg and ReadMsg.</p>
  
  <p>I have the ReadImg and WriteImg methods down, but where I am stuck is with my WriteMsg method. This is basic steganography that just writes each bit of the string to the last bit in each byte. The first 8 bytes are suppose to contain the size of the string being hidden, then each byte after that starts the hidden message.</p>
  
  <p>My idea was to create a massive array that holds the binary code for the size of the string, then the binary code of the string itself. I'm just trying to figure out how I would take that array and add it to each byte in the image.</p>
  
  <p>Any help is much appreciated. Here is my current code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  typedef struct {
       unsigned char red,green,blue;
  } PPMPixel;
  
  typedef struct {
       int x, y;
       PPMPixel *data;
  } PPMImage;
  
  #define CREATOR "RPFELGUEIRAS"
  #define RGB_COMPONENT_COLOR 255
  
  static PPMImage *readPPM(const char *filename)
  {
      char buff[16];
      PPMImage *img;
      FILE *fp;
      int c, rgb_comp_color;
      //open PPM file for reading
      fp = fopen(filename, "rb");
      if (!fp) {
           fprintf(stderr, "Unable to open file '%s'\n", filename);
           exit(1);
      }
  
      //read image format
      if (!fgets(buff, sizeof(buff), fp)) {
           perror(filename);
           exit(1);
      }
  
      //check the image format
      if (buff[0] != 'P' || buff[1] != '6') {
           fprintf(stderr, "Invalid image format (must be 'P6')\n");
           exit(1);
      }
  
      //alloc memory form image
      img = (PPMImage *)malloc(sizeof(PPMImage));
      if (!img) {
           fprintf(stderr, "Unable to allocate memory\n");
           exit(1);
      }
  
      //check for comments
      c = getc(fp);
      while (c == '#') {
      while (getc(fp) != '\n') ;
           c = getc(fp);
      }
  
      ungetc(c, fp);
      //read image size information
      if (fscanf(fp, "%d %d", &amp;img-&gt;x, &amp;img-&gt;y) != 2) {
           fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
           exit(1);
      }
  
      //read rgb component
      if (fscanf(fp, "%d", &amp;rgb_comp_color) != 1) {
           fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
           exit(1);
      }
  
      //check rgb component depth
      if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
           fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
           exit(1);
      }
  
      while (fgetc(fp) != '\n') ;
      //memory allocation for pixel data
      img-&gt;data = (PPMPixel*)malloc(img-&gt;x * img-&gt;y * sizeof(PPMPixel));
  
      if (!img) {
           fprintf(stderr, "Unable to allocate memory\n");
           exit(1);
      }
  
      //read pixel data from file
      if (fread(img-&gt;data, 3 * img-&gt;x, img-&gt;y, fp) != img-&gt;y) {
           fprintf(stderr, "Error loading image '%s'\n", filename);
           exit(1);
      }
  
      fclose(fp);
      return img;
  }
  
  void writePPM(const char *filename, PPMImage *img)
  {
      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, "P6\n");
  
      //comments
      fprintf(fp, "# Created by %s\n",CREATOR);
  
      //image size
      fprintf(fp, "%d %d\n",img-&gt;x,img-&gt;y);
  
      // rgb component depth
      fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);
  
      // pixel data
      fwrite(img-&gt;data, 3 * img-&gt;x, img-&gt;y, fp);
      fclose(fp);
  }
  
  void writeMsg(PPMImage *img, char *s)
  {   
  
      int i;
      int len;
      len = sizeof(s);
  
      if (img)
      {
           j = 0;
           for (i=0; i &lt; img-&gt;x * img-&gt;y; i++)
           {
                while(j &lt; 8)
                {
                     if(len &amp; 0x80)   
                     {
                          img-&gt;data[i].red= img-&gt;data[i].red | 0x01;
                     }
                     else 
                     {
                          img-&gt;data[i].red= img-&gt;data[i].red &amp; 0xFE;
                     }
  
                     len=len &lt;&lt; 1;
                     j++;
  
                     if (len &amp; 0x80)  
                     {
                          img-&gt;data[i].green= img-&gt;data[i].green | 0x01;
                     }
                     else
                     {
                          img-&gt;data[i].green= img-&gt;data[i].green &amp; 0xFE;
                     }
  
  
                     len = len &lt;&lt; 1;
                     j++;
  
                     if (len &amp; 0x80)  
                     {
                          img-&gt;data[i].blue= img-&gt;data[i].blue | 0x01;
                     }
                     else
                     {
                          img-&gt;data[i].blue= img-&gt;data[i].blue &amp; 0xFE;
                     }
                     j++;
                }
           }
      }
  }
  </code></pre>