| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Detecting mime types is a tricky business because we need to balance | 5 // Detecting mime types is a tricky business because we need to balance |
| 6 // compatibility concerns with security issues. Here is a survey of how other | 6 // compatibility concerns with security issues. Here is a survey of how other |
| 7 // browsers behave and then a description of how we intend to behave. | 7 // browsers behave and then a description of how we intend to behave. |
| 8 // | 8 // |
| 9 // HTML payload, no Content-Type header: | 9 // HTML payload, no Content-Type header: |
| 10 // * IE 7: Render as HTML | 10 // * IE 7: Render as HTML |
| 11 // * Firefox 2: Render as HTML | 11 // * Firefox 2: Render as HTML |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 MAGIC_NUMBER("text/plain", ">From") | 139 MAGIC_NUMBER("text/plain", ">From") |
| 140 // Chrome specific | 140 // Chrome specific |
| 141 MAGIC_NUMBER("application/x-gzip", "\x1F\x8B\x08") | 141 MAGIC_NUMBER("application/x-gzip", "\x1F\x8B\x08") |
| 142 MAGIC_NUMBER("audio/x-pn-realaudio", "\x2E\x52\x4D\x46") | 142 MAGIC_NUMBER("audio/x-pn-realaudio", "\x2E\x52\x4D\x46") |
| 143 MAGIC_NUMBER("video/x-ms-asf", | 143 MAGIC_NUMBER("video/x-ms-asf", |
| 144 "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C") | 144 "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C") |
| 145 MAGIC_NUMBER("image/tiff", "I I") | 145 MAGIC_NUMBER("image/tiff", "I I") |
| 146 MAGIC_NUMBER("image/tiff", "II*") | 146 MAGIC_NUMBER("image/tiff", "II*") |
| 147 MAGIC_NUMBER("image/tiff", "MM\x00*") | 147 MAGIC_NUMBER("image/tiff", "MM\x00*") |
| 148 MAGIC_NUMBER("audio/mpeg", "ID3") | 148 MAGIC_NUMBER("audio/mpeg", "ID3") |
| 149 MAGIC_NUMBER("image/webp", "RIFF....WEBPVP8 ") |
| 150 MAGIC_NUMBER("video/webm", "\x1A\x45\xDF\xA3") |
| 149 // TODO(abarth): we don't handle partial byte matches yet | 151 // TODO(abarth): we don't handle partial byte matches yet |
| 150 // MAGIC_NUMBER("video/mpeg", "\x00\x00\x01\xB") | 152 // MAGIC_NUMBER("video/mpeg", "\x00\x00\x01\xB") |
| 151 // MAGIC_NUMBER("audio/mpeg", "\xFF\xE") | 153 // MAGIC_NUMBER("audio/mpeg", "\xFF\xE") |
| 152 // MAGIC_NUMBER("audio/mpeg", "\xFF\xF") | 154 // MAGIC_NUMBER("audio/mpeg", "\xFF\xF") |
| 153 MAGIC_NUMBER("application/zip", "PK\x03\x04") | 155 MAGIC_NUMBER("application/zip", "PK\x03\x04") |
| 154 MAGIC_NUMBER("application/x-rar-compressed", "Rar!\x1A\x07\x00") | 156 MAGIC_NUMBER("application/x-rar-compressed", "Rar!\x1A\x07\x00") |
| 155 MAGIC_NUMBER("application/x-msmetafile", "\xD7\xCD\xC6\x9A") | 157 MAGIC_NUMBER("application/x-msmetafile", "\xD7\xCD\xC6\x9A") |
| 156 MAGIC_NUMBER("application/octet-stream", "MZ") // EXE | 158 MAGIC_NUMBER("application/octet-stream", "MZ") // EXE |
| 157 // Sniffing for Flash: | 159 // Sniffing for Flash: |
| 158 // | 160 // |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 }; | 210 }; |
| 209 | 211 |
| 210 static scoped_refptr<base::Histogram> UMASnifferHistogramGet(const char* name, | 212 static scoped_refptr<base::Histogram> UMASnifferHistogramGet(const char* name, |
| 211 int array_size) { | 213 int array_size) { |
| 212 scoped_refptr<base::Histogram> counter = | 214 scoped_refptr<base::Histogram> counter = |
| 213 base::LinearHistogram::FactoryGet(name, 1, array_size - 1, array_size, | 215 base::LinearHistogram::FactoryGet(name, 1, array_size - 1, array_size, |
| 214 base::Histogram::kUmaTargetedHistogramFlag); | 216 base::Histogram::kUmaTargetedHistogramFlag); |
| 215 return counter; | 217 return counter; |
| 216 } | 218 } |
| 217 | 219 |
| 220 // Compare content header to a magic number where magic_entry can contain '.' |
| 221 // for single character of anything, allowing some bytes to be skipped. |
| 222 static bool MagicCmp(const char* magic_entry, const char* content, size_t len) { |
| 223 while (len) { |
| 224 if ((*magic_entry != '.') && (*magic_entry != *content)) |
| 225 return false; |
| 226 ++magic_entry; |
| 227 ++content; |
| 228 --len; |
| 229 } |
| 230 return true; |
| 231 } |
| 232 |
| 218 static bool MatchMagicNumber(const char* content, size_t size, | 233 static bool MatchMagicNumber(const char* content, size_t size, |
| 219 const MagicNumber* magic_entry, | 234 const MagicNumber* magic_entry, |
| 220 std::string* result) { | 235 std::string* result) { |
| 221 const size_t len = magic_entry->magic_len; | 236 const size_t len = magic_entry->magic_len; |
| 222 | 237 |
| 223 // Keep kBytesRequiredForMagic honest. | 238 // Keep kBytesRequiredForMagic honest. |
| 224 DCHECK_LE(len, kBytesRequiredForMagic); | 239 DCHECK_LE(len, kBytesRequiredForMagic); |
| 225 | 240 |
| 226 // To compare with magic strings, we need to compute strlen(content), but | 241 // To compare with magic strings, we need to compute strlen(content), but |
| 227 // content might not actually have a null terminator. In that case, we | 242 // content might not actually have a null terminator. In that case, we |
| 228 // pretend the length is content_size. | 243 // pretend the length is content_size. |
| 229 const char* end = | 244 const char* end = |
| 230 static_cast<const char*>(memchr(content, '\0', size)); | 245 static_cast<const char*>(memchr(content, '\0', size)); |
| 231 const size_t content_strlen = | 246 const size_t content_strlen = |
| 232 (end != NULL) ? static_cast<size_t>(end - content) : size; | 247 (end != NULL) ? static_cast<size_t>(end - content) : size; |
| 233 | 248 |
| 234 bool match = false; | 249 bool match = false; |
| 235 if (magic_entry->is_string) { | 250 if (magic_entry->is_string) { |
| 236 if (content_strlen >= len) { | 251 if (content_strlen >= len) { |
| 237 // String comparisons are case-insensitive | 252 // String comparisons are case-insensitive |
| 238 match = (base::strncasecmp(magic_entry->magic, content, len) == 0); | 253 match = (base::strncasecmp(magic_entry->magic, content, len) == 0); |
| 239 } | 254 } |
| 240 } else { | 255 } else { |
| 241 if (size >= len) | 256 if (size >= len) |
| 242 match = (memcmp(magic_entry->magic, content, len) == 0); | 257 match = MagicCmp(magic_entry->magic, content, len); |
| 243 } | 258 } |
| 244 | 259 |
| 245 if (match) { | 260 if (match) { |
| 246 result->assign(magic_entry->mime_type); | 261 result->assign(magic_entry->mime_type); |
| 247 return true; | 262 return true; |
| 248 } | 263 } |
| 249 return false; | 264 return false; |
| 250 } | 265 } |
| 251 | 266 |
| 252 static bool CheckForMagicNumbers(const char* content, size_t size, | 267 static bool CheckForMagicNumbers(const char* content, size_t size, |
| 253 const MagicNumber* magic, size_t magic_len, | 268 const MagicNumber* magic, size_t magic_len, |
| 254 base::Histogram* counter, std::string* result)
{ | 269 base::Histogram* counter, |
| 270 std::string* result) { |
| 255 for (size_t i = 0; i < magic_len; ++i) { | 271 for (size_t i = 0; i < magic_len; ++i) { |
| 256 if (MatchMagicNumber(content, size, &(magic[i]), result)) { | 272 if (MatchMagicNumber(content, size, &(magic[i]), result)) { |
| 257 if (counter) counter->Add(static_cast<int>(i)); | 273 if (counter) counter->Add(static_cast<int>(i)); |
| 258 return true; | 274 return true; |
| 259 } | 275 } |
| 260 } | 276 } |
| 261 return false; | 277 return false; |
| 262 } | 278 } |
| 263 | 279 |
| 264 // Truncates |size| to |max_size| and returns true if |size| is at least | 280 // Truncates |size| to |max_size| and returns true if |size| is at least |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 // Now we look in our large table of magic numbers to see if we can find | 671 // Now we look in our large table of magic numbers to see if we can find |
| 656 // anything that matches the content. | 672 // anything that matches the content. |
| 657 if (SniffForMagicNumbers(content, content_size, | 673 if (SniffForMagicNumbers(content, content_size, |
| 658 &have_enough_content, result)) | 674 &have_enough_content, result)) |
| 659 return true; // We've matched a magic number. No more content needed. | 675 return true; // We've matched a magic number. No more content needed. |
| 660 | 676 |
| 661 return have_enough_content; | 677 return have_enough_content; |
| 662 } | 678 } |
| 663 | 679 |
| 664 } // namespace net | 680 } // namespace net |
| OLD | NEW |