segment is not SOI */ fputs("Not a valid JPEG file\n", stderr); return false; } else if (marker[1] == JPEG_MARKER_EOI) { /* End of image segment */ return true; } /* Call handler for each segment */ if (jpeg->ops[marker[1]]) { if (!jpeg->ops[marker[1]](jpeg)) return false; } } /* Reached end of file before EOI segment */ fputs("Truncated file (EOI not found)\n", stderr); return true; } /** * Parse JPEG file and call handler for each segment */ bool jpeg_walk (jpeg_file_t *jpeg) { bool encount_soi; u8 marker[2]; fseek(jpeg->file, 0, SEEK_SET); /* Read every segment */ encount_soi = false; while (!feof(jpeg->file)) { /* Read marker */ if (fread(marker, sizeof(marker), 1, jpeg->file) != 1) { fputs("Truncated file (broken marker)\n", stderr); return encount_soi; /* Return false only if it's not JPEG */ } if (marker[0] != 0xFF) { fputs("Invalid marker (Corrupted JPEG)\n", stderr); return encount_soi; /* Return false only if it's not JPEG */ } /* Handle special markers */ if (marker[1] == JPEG_MARKER_SOI) { /* Start of image segment */ if (encount_soi) /* Skip the segment if we encounter SOI at invalid position */ fputs("SOI at invalid position\n", stderr); else encount_soi = true; continue;