3

I'm relatively new to both C and bitwise operations and I'm having trouble with an assignment I've been given in my class. A majority of the code has been given to me, but I've been having issues figuring out a part pertaining to bitwise operations. Once I figure this part out, I'll be golden. I hope that someone can help!

Here is the excerpt from my assignment:

You will need to use 8 bytes of an image to hide one byte of information (remember that only LSBs of the cover image can be modified). You will use the rest of the 16 bytes of the cover image to embed 16 bits of b.size (two least significant bytes of the size field for the binary data), next 32 bytes of the cover will be used to embed the file extension for the payload file, and after that you will use 8*b.size bytes to embed the payload (b.data).

What this program is doing is stenography of an image, and I have to modify the least significant bits of the image read in using data from a file that I created. Like I said, all of the code for that is already written. I just can't figure out how to modify the LSBs. Any help would be greatly appreciated!!!

The functions I have to use for reformatting the LSBs are as follows:

byte getlsbs(byte *b);
void setlsbs(byte *b, byte b0);

This is what I've attempted thus far:

/* In main function */
b0 = getlsbs(&img.gray[0])

/* Passing arguments */
byte getlsbs(byte *b)
{
    byte b0;
    b0[0] = b >> 8;
    return b0;
}

I'm honestly at a complete loss. I've been working on this all night and I still barely have made headway.

Simon F
  • 1,299
  • 1
  • 11
  • 14
JamesClem
  • 33
  • 6
  • BTW: [stenography](http://en.wikipedia.org/wiki/Stenography) // [steganography](http://en.wikipedia.org/wiki/Steganography) - please correct your post – Mat Apr 11 '12 at 12:24
  • I apologize; I didn't mean to make it seem like I was trying to have it done for me. Everything I've tried has failed miserably because I just can't seem to understand bitwise operations. I've never had to deal with them before in my college career. – JamesClem Apr 11 '12 at 12:29
  • Then post the code you did try so people can explain what you're doing wrong. – Mat Apr 11 '12 at 12:32
  • What I've tried thus far is passing each byte of the image to the getlsbs() function and appending to them an array. After that I wanted to try to see if I could go in manually to edit the values (probably in a loop), but I keep getting stuck at this point. Nothing I've tried seems to be working. – JamesClem Apr 11 '12 at 12:33
  • Post the **code** you tried, and do that by adding it to your question. – Mat Apr 11 '12 at 12:34
  • Do you have to _use_ the functions, or to write them so the program uses them? If you have to use them, then how are they defined? If you have to write them, then what behavior is expected of them? What is a `byte`? Is it a `typedef` for `unsigned char` or something like that? – ArjunShankar Apr 11 '12 at 12:35
  • Who implemented the version of `getlsbs` you pasted? Too many things wrong with it. 1. You can't access a `byte` as an array. 2. Shifting a `byte` gets *rid* of LSBs. 3. shifting *8 bits* basically gets rid of *all* bits and makes b0 = 0. – ArjunShankar Apr 11 '12 at 12:45
  • ALSO: How many LSBs are you supposed to use? – ArjunShankar Apr 11 '12 at 12:48

1 Answers1

4

To set LSB of b to 1:

b |= 1;

To set LSB of b to 0:

b &= 0xFE;

Here is an idea how the functions could be implemented. This code is not tested.

byte getlsbs(byte *b)
{
    byte result = 0;
    for (int i = 0; i < 8; ++i)
    {
        result >>= 1;
        if (*b & 1)
            result |=  0x80;
        ++b;
    }
    return result;
}

void setlsbs(byte *b, byte b0)
{
    for (int i = 0; i < 8; ++i)
    {
        if (b0 & 1)
            *b |= 1;
        else
            *b &= 0xFE;
        ++b;
        b0 >>= 1;
    }
}
Henrik
  • 23,186
  • 6
  • 42
  • 92
  • +1. Although I wish OP had mentioned how many of the LSBs (s)he is allowed to play with (I don't think its just 1 because (s)he has used the word 'bits'). – ArjunShankar Apr 11 '12 at 12:49
  • I apologize that I haven't paid attention to this post; I've been quite busy with other classes recently and haven't had much time to focus on this program. I apprecaite the help! I will try it out and let you know if this fixes my issue. I believe I'm beginning to understand bitwise operations better now. Also, to the poster above me, I do have to manipulate multiple bytes, but I am allows to use a looping structure to handle this in the main function. From the looks of it, this may solve the issue I've been having with getting and setting my LSBs. – JamesClem Apr 24 '12 at 05:10
  • The solutions you've created for getlsbs seems to be working perfectly, but unfortunately setting them doesn't seem to be working correctly. I thank you very much for getting me this far, however. I was so stuck before. I'll play around with them and see if I can get them to work properly – JamesClem May 01 '12 at 06:55
  • Nevermind, I've found the issue. I wrote the code you gave me incorrectly. Thank you so much! – JamesClem May 01 '12 at 06:58