static bool is_orientation_marker(jpeg_marker_struct* marker, SkCodec::Origin* orientation) { if (kExifMarker != marker->marker || marker->data_length < kExifHeaderSize) { return false; } const uint8_t* data = marker->data; static const uint8_t kExifSig[] { 'E', 'x', 'i', 'f', '\0' }; if (memcmp(data, kExifSig, sizeof(kExifSig))) { return false; } bool littleEndian; if (!is_valid_endian_marker(data + 6, &littleEndian)) { return false; } // Get the offset from the start of the marker. // Account for 'E', 'x', 'i', 'f', '\0', '<fill byte>'. uint32_t offset = get_endian_int(data + 10, littleEndian); offset += sizeof(kExifSig) + 1; // Require that the marker is at least large enough to contain the number of entries. if (marker->data_length < offset + 2) { return false; } uint32_t numEntries = get_endian_short(data + offset, littleEndian); // Tag (2 bytes), Datatype (2 bytes), Number of elements (4 bytes), Data (4 bytes) const uint32_t kEntrySize = 12; numEntries = SkTMin(numEntries, (marker->data_length - offset - 2) / kEntrySize); // Advance the data to the start of the entries. data += offset + 2; const uint16_t kOriginTag = 0x112; const uint16_t kOriginType = 3; for (uint32_t i = 0; i < numEntries; i++, data += kEntrySize) { uint16_t tag = get_endian_short(data, littleEndian); uint16_t type = get_endian_short(data + 2, littleEndian); uint32_t count = get_endian_int(data + 4, littleEndian); if (kOriginTag == tag && kOriginType == type && 1 == count) { uint16_t val = get_endian_short(data + 8, littleEndian); if (0 < val && val <= SkCodec::kLast_Origin) { *orientation = (SkCodec::Origin) val; return true; } } } return false; }
// Quick check if the image contains a valid TIFF header as requested by DNG format. bool isTiffHeaderValid() const { const size_t kHeaderSize = 4; SkAutoSTMalloc<kHeaderSize, unsigned char> header(kHeaderSize); if (!fStream->read(header.get(), 0 /* offset */, kHeaderSize)) { return false; } // Check if the header is valid (endian info and magic number "42"). bool littleEndian; if (!is_valid_endian_marker(header, &littleEndian)) { return false; } return 0x2A == get_endian_short(header + 2, littleEndian); }