0
// start of new jpeg?
    if (buffer[0] == 0xff && buffer[1] == 0xd8 &&
        buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))

There must be, because this doesn't look very elegant, but I can't think of one.

2

It may not look clean, but it is. Complex doesn't necessarily mean inelegant. Think about this. Both signatures have the same first 3 bytes, and there are two choices for the last byte. You've tested for exactly that.

As efficiency goes, as soon as one of the first 3 bytes doesn't match, the whole test is false so the code stops looking. If it gets to the 4th byte, then it will test the first version, and if it doesn't match, it'll test the second. That's about as efficient as it gets. Fastest test, the first byte doesn't match. Worst case, the block has the second signature and checks 5 bytes.

Another very common approach is to create two variables, one for each signature and test for if( <first 4 bytes> == sig_A || <first 4 bytes> == sig_B ). Arguably simpler, or not from a coding point of view. As for efficiency, it will have to check a minimum of 4 bytes to a maximum of 8. Look at the worst case. If a block doesn't have a signature, the test has to check all 4 bytes of the first signature, find that it doesn't match, and then start checking against the second signature until it finds a byte that doesn't match, or that it is a complete match.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

You must log in to answer this question.

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