- (int)isJPEGValid:(NSData *)jpeg {
if ([jpeg length] < 4) return 1;
const unsigned char * bytes = (const unsigned char *)[jpeg bytes];
if (bytes[0] != 0xFF || bytes[1] != 0xD8) return 2;
if (bytes[[jpeg length] - 2] != 0xFF ||
bytes[[jpeg length] - 1] != 0xD9) return 3;
return 0;
}