NS_IMETHODIMP nsMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest, const uint8_t* aData, const uint32_t aLength, nsACString& aSniffedType) { // For media, we want to sniff only if the Content-Type is unknown, or if it // is application/octet-stream. nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); if (channel) { nsAutoCString contentType; nsresult rv = channel->GetContentType(contentType); NS_ENSURE_SUCCESS(rv, rv); if (!contentType.IsEmpty() && !contentType.EqualsLiteral(APPLICATION_OCTET_STREAM) && !contentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE)) { return NS_ERROR_NOT_AVAILABLE; } } const uint32_t clampedLength = std::min(aLength, MAX_BYTES_SNIFFED); for (uint32_t i = 0; i < mozilla::ArrayLength(sSnifferEntries); ++i) { const nsMediaSnifferEntry& currentEntry = sSnifferEntries[i]; if (clampedLength < currentEntry.mLength || currentEntry.mLength == 0) { continue; } bool matched = true; for (uint32_t j = 0; j < currentEntry.mLength; ++j) { if ((currentEntry.mMask[j] & aData[j]) != currentEntry.mPattern[j]) { matched = false; break; } } if (matched) { aSniffedType.AssignASCII(currentEntry.mContentType); return NS_OK; } } if (MatchesMP4(aData, clampedLength)) { aSniffedType.AssignLiteral(VIDEO_MP4); return NS_OK; } if (MatchesWebM(aData, clampedLength)) { aSniffedType.AssignLiteral(VIDEO_WEBM); return NS_OK; } if (MatchesMP3(aData, clampedLength)) { aSniffedType.AssignLiteral(AUDIO_MP3); return NS_OK; } // Could not sniff the media type, we are required to set it to // application/octet-stream. aSniffedType.AssignLiteral(APPLICATION_OCTET_STREAM); return NS_ERROR_NOT_AVAILABLE; }
NS_IMETHODIMP nsMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest, const uint8_t* aData, const uint32_t aLength, nsACString& aSniffedType) { const uint32_t clampedLength = NS_MIN(aLength, MAX_BYTES_SNIFFED); for (uint32_t i = 0; i < NS_ARRAY_LENGTH(sSnifferEntries); ++i) { const nsMediaSnifferEntry& currentEntry = sSnifferEntries[i]; if (clampedLength < currentEntry.mLength || currentEntry.mLength == 0) { continue; } bool matched = true; for (uint32_t j = 0; j < currentEntry.mLength; ++j) { if ((currentEntry.mMask[j] & aData[j]) != currentEntry.mPattern[j]) { matched = false; break; } } if (matched) { aSniffedType.AssignASCII(currentEntry.mContentType); return NS_OK; } } if (MatchesMP4(aData, clampedLength)) { aSniffedType.AssignLiteral(VIDEO_MP4); return NS_OK; } // Could not sniff the media type, we are required to set it to // application/octet-stream. aSniffedType.AssignLiteral(APPLICATION_OCTET_STREAM); return NS_ERROR_NOT_AVAILABLE; }