0

my recover program keeps coming up with error invalid operands to binary expression

at the line where I check if(buffer[0] == pattern1 || buffer[0] == pattern2)

I'm not sure why this error occurs since I have already declared struct JPEG to be 4 bytes and read the same amount to the buffer.

// define BYTE of size 8 bits

typedef uint8_t BYTE;

// define struct for finding JPG signature

typedef struct
{
    BYTE sig1;
    BYTE sig2;
    BYTE sig3;
    BYTE sig4;
} 
__attribute__((__packed__))

JPEG;

int main(int argc, char* argv[])

{

// assign values to struct JPG to find the signature bytes

JPEG pattern1 = {0xff, 0xd8, 0xff, 0xe0};

JPEG pattern2 = {0xff, 0xd8, 0xff, 0xe1};

    // initialize buffer
    JPEG buffer [128];

    // initialize file number
    int number = 0;

    // open file to recover images from
    FILE* card = fopen("card.raw", "r");
    //repeat until end of card
    while (!feof(card))
    {        
        // read 512 bytes into a buffer
        fread(&buffer, sizeof(JPEG), 128, card);

        // find the beginning of the jpeg
        if(buffer[0] == pattern1 || buffer[0] == pattern2)
        {
            // open a file to store the jpeg
        }
0

buffer[0] is a variable of type JPEG which is a struct type as well as pattern1 and pattern2.

You can compare the contents of variables of a struct type with ==! Besides, I see this is unnecessary wrapping. An array of 512 is probably enough and it would be easy for you to compare the first 4 elements of it with the values you want.

In order to compare the contents of your struct variables, you may extract their contents and compare them with each other. For example,

if (buffer[0].sig1 == patter1.sig1 && buffer[0].sig2 == pattern1.sig2)
    // do something

Or you could use a function like memcmp like that

memcmp(s1Ptr, s2Ptr, size);

where s1Ptr and s2Ptr are pointers to the structs that you wanna compare their contents and size is the number of bytes you wanna compare.

Execute

man memcmp

in the terminal for more information!

10
  • so it is not possible to compare it this way buffer[0] == pattern1? I thought that if it was wrapped as 4 bytes I would be allowed to compare them this way. – Lucas Dec 1 '14 at 16:10
  • @Lucas that's not true! – kzidane Dec 1 '14 at 16:32
  • but it returns an error. I used this method in my code. And I tried memcmp but it is not working either. I passed it memcmp(buffer[0], pattern1, 4) but it states that it is an incompatible type – Lucas Dec 1 '14 at 16:46
  • @Lucas I'm sorry! I fixed the code in the answer above. As for memcmp, it takes two pointers and the size. You need to pass &buffer[0] and &pattern1 respectively. Also, you need to calculate the size correctly because 4 here means 4 bytes! – kzidane Dec 1 '14 at 16:50
  • ok so that means if we want to compare structs, we must pull out each member and we cannot compare it as a whole? – Lucas Dec 1 '14 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .