| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
| 6 | 6 |
| 7 #include "base/string_piece.h" | 7 #include "base/string_piece.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 if (path.empty()) | 39 if (path.empty()) |
| 40 return false; | 40 return false; |
| 41 std::replace(path.begin(), path.end(), '/', '\\'); | 41 std::replace(path.begin(), path.end(), '/', '\\'); |
| 42 | 42 |
| 43 // GURL stores strings as percent-encoded UTF-8, this will undo if possible. | 43 // GURL stores strings as percent-encoded UTF-8, this will undo if possible. |
| 44 path = UnescapeURLComponent(path, | 44 path = UnescapeURLComponent(path, |
| 45 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); | 45 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
| 46 | 46 |
| 47 if (!IsStringUTF8(path.c_str())) { | 47 if (!IsStringUTF8(path)) { |
| 48 // Not UTF-8, assume encoding is native codepage and we're done. We know we | 48 // Not UTF-8, assume encoding is native codepage and we're done. We know we |
| 49 // are giving the conversion function a nonempty string, and it may fail if | 49 // are giving the conversion function a nonempty string, and it may fail if |
| 50 // the given string is not in the current encoding and give us an empty | 50 // the given string is not in the current encoding and give us an empty |
| 51 // string back. We detect this and report failure. | 51 // string back. We detect this and report failure. |
| 52 *file_path = base::SysNativeMBToWide(path); | 52 *file_path = base::SysNativeMBToWide(path); |
| 53 return !file_path->empty(); | 53 return !file_path->empty(); |
| 54 } | 54 } |
| 55 file_path->assign(UTF8ToWide(path)); | 55 file_path->assign(UTF8ToWide(path)); |
| 56 | 56 |
| 57 // Now we have an unescaped filename, but are still not sure about its | 57 // Now we have an unescaped filename, but are still not sure about its |
| 58 // encoding. For example, each character could be part of a UTF-8 string. | 58 // encoding. For example, each character could be part of a UTF-8 string. |
| 59 if (file_path->empty() || !IsString8Bit(*file_path)) { | 59 if (file_path->empty() || !IsString8Bit(*file_path)) { |
| 60 // assume our 16-bit encoding is correct if it won't fit into an 8-bit | 60 // assume our 16-bit encoding is correct if it won't fit into an 8-bit |
| 61 // string | 61 // string |
| 62 return true; | 62 return true; |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Convert our narrow string into the native wide path. | 65 // Convert our narrow string into the native wide path. |
| 66 std::string narrow; | 66 std::string narrow; |
| 67 if (!WideToLatin1(*file_path, &narrow)) { | 67 if (!WideToLatin1(*file_path, &narrow)) { |
| 68 NOTREACHED() << "Should have filtered out non-8-bit strings above."; | 68 NOTREACHED() << "Should have filtered out non-8-bit strings above."; |
| 69 return false; | 69 return false; |
| 70 } | 70 } |
| 71 if (IsStringUTF8(narrow.c_str())) { | 71 if (IsStringUTF8(narrow)) { |
| 72 // Our string actually looks like it could be UTF-8, convert to 8-bit | 72 // Our string actually looks like it could be UTF-8, convert to 8-bit |
| 73 // UTF-8 and then to the corresponding wide string. | 73 // UTF-8 and then to the corresponding wide string. |
| 74 *file_path = UTF8ToWide(narrow); | 74 *file_path = UTF8ToWide(narrow); |
| 75 } else { | 75 } else { |
| 76 // Our wide string contains only 8-bit characters and it's not UTF-8, so | 76 // Our wide string contains only 8-bit characters and it's not UTF-8, so |
| 77 // we assume it's in the native codepage. | 77 // we assume it's in the native codepage. |
| 78 *file_path = base::SysNativeMBToWide(narrow); | 78 *file_path = base::SysNativeMBToWide(narrow); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Fail if 8-bit -> wide conversion failed and gave us an empty string back | 81 // Fail if 8-bit -> wide conversion failed and gave us an empty string back |
| 82 // (we already filtered out empty strings above). | 82 // (we already filtered out empty strings above). |
| 83 return !file_path->empty(); | 83 return !file_path->empty(); |
| 84 } | 84 } |
| 85 | 85 |
| 86 } // namespace net | 86 } // namespace net |
| 87 | 87 |
| OLD | NEW |