Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: net/base/mime_sniffer.cc

Issue 6124007: replace memcmp with MagicCmp that supports '.' for single character of anythi... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 bool same = true;
224 while (len && same) {
225 if (!*content && !*magic_entry)
abarth-chromium 2011/01/11 19:51:29 I don't think this is correct. We want to match n
fbarchard1 2011/01/11 19:57:39 I think its correct. If both strings have been th
Avi (use Gerrit) 2011/01/11 20:00:02 Given this argument between you two, I think it's
226 break;
227 same = (*magic_entry == *content) || (*magic_entry == '.' && *content);
228 if (!*content || !*magic_entry)
229 break;
230 ++magic_entry;
231 ++content;
232 --len;
233 }
234 return same;
235 }
236
218 static bool MatchMagicNumber(const char* content, size_t size, 237 static bool MatchMagicNumber(const char* content, size_t size,
219 const MagicNumber* magic_entry, 238 const MagicNumber* magic_entry,
220 std::string* result) { 239 std::string* result) {
221 const size_t len = magic_entry->magic_len; 240 const size_t len = magic_entry->magic_len;
222 241
223 // Keep kBytesRequiredForMagic honest. 242 // Keep kBytesRequiredForMagic honest.
224 DCHECK_LE(len, kBytesRequiredForMagic); 243 DCHECK_LE(len, kBytesRequiredForMagic);
225 244
226 // To compare with magic strings, we need to compute strlen(content), but 245 // 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 246 // content might not actually have a null terminator. In that case, we
228 // pretend the length is content_size. 247 // pretend the length is content_size.
229 const char* end = 248 const char* end =
230 static_cast<const char*>(memchr(content, '\0', size)); 249 static_cast<const char*>(memchr(content, '\0', size));
231 const size_t content_strlen = 250 const size_t content_strlen =
232 (end != NULL) ? static_cast<size_t>(end - content) : size; 251 (end != NULL) ? static_cast<size_t>(end - content) : size;
233 252
234 bool match = false; 253 bool match = false;
235 if (magic_entry->is_string) { 254 if (magic_entry->is_string) {
236 if (content_strlen >= len) { 255 if (content_strlen >= len) {
237 // String comparisons are case-insensitive 256 // String comparisons are case-insensitive
238 match = (base::strncasecmp(magic_entry->magic, content, len) == 0); 257 match = (base::strncasecmp(magic_entry->magic, content, len) == 0);
239 } 258 }
240 } else { 259 } else {
241 if (size >= len) 260 if (size >= len)
242 match = (memcmp(magic_entry->magic, content, len) == 0); 261 match = MagicCmp(magic_entry->magic, content, len);
243 } 262 }
244 263
245 if (match) { 264 if (match) {
246 result->assign(magic_entry->mime_type); 265 result->assign(magic_entry->mime_type);
247 return true; 266 return true;
248 } 267 }
249 return false; 268 return false;
250 } 269 }
251 270
252 static bool CheckForMagicNumbers(const char* content, size_t size, 271 static bool CheckForMagicNumbers(const char* content, size_t size,
253 const MagicNumber* magic, size_t magic_len, 272 const MagicNumber* magic, size_t magic_len,
254 base::Histogram* counter, std::string* result) { 273 base::Histogram* counter,
274 std::string* result) {
255 for (size_t i = 0; i < magic_len; ++i) { 275 for (size_t i = 0; i < magic_len; ++i) {
256 if (MatchMagicNumber(content, size, &(magic[i]), result)) { 276 if (MatchMagicNumber(content, size, &(magic[i]), result)) {
257 if (counter) counter->Add(static_cast<int>(i)); 277 if (counter) counter->Add(static_cast<int>(i));
258 return true; 278 return true;
259 } 279 }
260 } 280 }
261 return false; 281 return false;
262 } 282 }
263 283
264 // Truncates |size| to |max_size| and returns true if |size| is at least 284 // 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
655 // Now we look in our large table of magic numbers to see if we can find 675 // Now we look in our large table of magic numbers to see if we can find
656 // anything that matches the content. 676 // anything that matches the content.
657 if (SniffForMagicNumbers(content, content_size, 677 if (SniffForMagicNumbers(content, content_size,
658 &have_enough_content, result)) 678 &have_enough_content, result))
659 return true; // We've matched a magic number. No more content needed. 679 return true; // We've matched a magic number. No more content needed.
660 680
661 return have_enough_content; 681 return have_enough_content;
662 } 682 }
663 683
664 } // namespace net 684 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698