OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 #include "base/strings/string_util.h" | 101 #include "base/strings/string_util.h" |
102 #include "url/gurl.h" | 102 #include "url/gurl.h" |
103 | 103 |
104 namespace net { | 104 namespace net { |
105 | 105 |
106 // The number of content bytes we need to use all our magic numbers. Feel free | 106 // The number of content bytes we need to use all our magic numbers. Feel free |
107 // to increase this number if you add a longer magic number. | 107 // to increase this number if you add a longer magic number. |
108 static const size_t kBytesRequiredForMagic = 42; | 108 static const size_t kBytesRequiredForMagic = 42; |
109 | 109 |
110 struct MagicNumber { | 110 struct MagicNumber { |
111 const char* const mime_type; | 111 const char* mime_type; |
112 const char* const magic; | 112 const char* magic; |
113 size_t magic_len; | 113 size_t magic_len; |
114 bool is_string; | 114 bool is_string; |
115 const char* const mask; // if set, must have same length as |magic| | 115 const char* mask; // if set, must have same length as |magic| |
116 }; | 116 }; |
117 | 117 |
118 #define MAGIC_NUMBER(mime_type, magic) \ | 118 #define MAGIC_NUMBER(mime_type, magic) \ |
119 { (mime_type), (magic), sizeof(magic)-1, false, NULL }, | 119 { (mime_type), (magic), sizeof(magic)-1, false, NULL }, |
120 | 120 |
121 template <int MagicSize, int MaskSize> | 121 template <int MagicSize, int MaskSize> |
122 class VerifySizes { | 122 class VerifySizes { |
123 static_assert(MagicSize == MaskSize, "sizes must be equal"); | 123 static_assert(MagicSize == MaskSize, "sizes must be equal"); |
124 | 124 |
125 public: | 125 public: |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 196 |
197 enum OfficeDocType { | 197 enum OfficeDocType { |
198 DOC_TYPE_WORD, | 198 DOC_TYPE_WORD, |
199 DOC_TYPE_EXCEL, | 199 DOC_TYPE_EXCEL, |
200 DOC_TYPE_POWERPOINT, | 200 DOC_TYPE_POWERPOINT, |
201 DOC_TYPE_NONE | 201 DOC_TYPE_NONE |
202 }; | 202 }; |
203 | 203 |
204 struct OfficeExtensionType { | 204 struct OfficeExtensionType { |
205 OfficeDocType doc_type; | 205 OfficeDocType doc_type; |
206 const char* const extension; | 206 const char* extension; |
207 size_t extension_len; | 207 size_t extension_len; |
208 }; | 208 }; |
209 | 209 |
210 #define OFFICE_EXTENSION(type, extension) \ | 210 #define OFFICE_EXTENSION(type, extension) \ |
211 { (type), (extension), sizeof(extension) - 1 }, | 211 { (type), (extension), sizeof(extension) - 1 }, |
212 | 212 |
213 static const OfficeExtensionType kOfficeExtensionTypes[] = { | 213 static const OfficeExtensionType kOfficeExtensionTypes[] = { |
214 OFFICE_EXTENSION(DOC_TYPE_WORD, ".doc") | 214 OFFICE_EXTENSION(DOC_TYPE_WORD, ".doc") |
215 OFFICE_EXTENSION(DOC_TYPE_EXCEL, ".xls") | 215 OFFICE_EXTENSION(DOC_TYPE_EXCEL, ".xls") |
216 OFFICE_EXTENSION(DOC_TYPE_POWERPOINT, ".ppt") | 216 OFFICE_EXTENSION(DOC_TYPE_POWERPOINT, ".ppt") |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 ~(1u << '\t' | 1u << '\n' | 1u << '\r' | 1u << '\f' | 1u << '\x1b'); | 878 ~(1u << '\t' | 1u << '\n' | 1u << '\r' | 1u << '\f' | 1u << '\x1b'); |
879 for (size_t i = 0; i < size; ++i) { | 879 for (size_t i = 0; i < size; ++i) { |
880 uint8_t byte = static_cast<uint8_t>(content[i]); | 880 uint8_t byte = static_cast<uint8_t>(content[i]); |
881 if (byte < 0x20 && (kBinaryBits & (1u << byte))) | 881 if (byte < 0x20 && (kBinaryBits & (1u << byte))) |
882 return true; | 882 return true; |
883 } | 883 } |
884 return false; | 884 return false; |
885 } | 885 } |
886 | 886 |
887 } // namespace net | 887 } // namespace net |
OLD | NEW |