Classify the contents of a file based on starting bytes (the magic number).
| 43 | |
| 44 | // Classify the contents of a file based on starting bytes (the magic number). |
| 45 | FileFormat ClassifyFileFormat(StringPiece data) { |
| 46 | // The 4th byte of JPEG is '\xe0' or '\xe1', so check just the first three |
| 47 | if (absl::StartsWith(data, "\xff\xd8\xff")) return kJpgFormat; |
| 48 | if (absl::StartsWith(data, "\x89PNG\r\n\x1a\n")) return kPngFormat; |
| 49 | if (absl::StartsWith(data, "\x47\x49\x46\x38")) return kGifFormat; |
| 50 | return kUnknownFormat; |
| 51 | } |
| 52 | |
| 53 | string FileFormatString(FileFormat magic, StringPiece data) { |
| 54 | switch (magic) { |