3

I'm working on steganography program in Java. But I got advice that I be able to resolve this task better in C program. I would like to try it, but I'm pretty bad in C programing. For now I would like to read one gif file and find byte which is used as image separator (0x2c from GIF format).

I tried to write this program:

int main(int argc, char *argv[])
{
    FILE *fileptr;
    char *buffer;
    long filelen = 0;

    fileptr = fopen("D:/test.gif", "rb");  // Open the file in binary mode
    fseek(fileptr, 0, SEEK_END);          // Jump to the end of the file
    filelen = ftell(fileptr);             // Get the current byte offset in the file
    rewind(fileptr);                      // Jump back to the beginning of the file

    buffer = (char *)malloc((filelen+1)*sizeof(char)); // Enough memory for file + \0
    fread(buffer, filelen, 1, fileptr); // Read in the entire file
    fclose(fileptr); // Close the file

    int i = 0;
    for(i = 0; buffer[ i ]; i++)
    {
        if(buffer[i] == 0x2c)
        {
            printf("Next image");
        }
    }


    return 0;
}

Could someone give me advice how to repair my loop?

Sk1X1
  • 1,305
  • 5
  • 22
  • 50
  • What is the problem you are seeing. `0x2C` or `0x00` can be data byte in image also. – Mohit Jain May 14 '15 at 18:00
  • How is your loop broken? As it is, it will exit the first time `buffer[i]` is zero; is that what you want? – Politank-Z May 14 '15 at 18:00
  • can you use `for(i = 0; i – ryyker May 14 '15 at 18:01
  • @Politank-Z In link I post is written that `0x2c` is byte used as image separator. How could I recognize if is it image separator or normal byte? – Sk1X1 May 14 '15 at 18:24
  • I don't understand - you are already testing `buffer[i] == 0x2c` inside the loop. From what you say, that seems to be how you differentiate between an image separator and a normal byte. – Politank-Z May 14 '15 at 18:30
  • @Politank-Z My problem is, that nothing is printed in console. So I my condition (or entire loop) is wrong or information in that link are wrong. Thats why I post here, to find out if code is ok. – Sk1X1 May 14 '15 at 19:00

2 Answers2

4

Could someone give me advice how to repair my loop?

Option 1: Don't depend on the terminating null character.

for(i = 0; i < filelen; i++)
{
    if(buffer[i] == 0x2c)
    {
        printf("Next image");
    }
}

Option 2: Add the terminating null character before relying on it. This is potentially unreliable since you are reading a binary file that could have embedded null characters in it.

buffer[filelen] = '\0';
for(i = 0; buffer[ i ]; i++)
{
    if(buffer[i] == 0x2c)
    {
        printf("Next image");
    }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Similar to the 'for()' based answer, if you only need to check for a specific byte (0x2c), you can simply do something like the following (and not worry about null in the byte stream), using while().

i = 0;
while(i < filelen)
{
    if(buffer[i++] == 0x2c)
    {
        printf("Next image");
    }
}
iammowgli
  • 159
  • 4