1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef URL_URL_FILE_H_
6 #define URL_URL_FILE_H_
7
8 // Provides shared functions used by the internals of the parser and
9 // canonicalizer for file URLs. Do not use outside of these modules.
10
11 #include "base/strings/string_util.h"
12 #include "url/url_parse_internal.h"
13
14 namespace url {
15
16 // We allow both "c:" and "c|" as drive identifiers.
IsWindowsDriveSeparator(char16_t ch)17 inline bool IsWindowsDriveSeparator(char16_t ch) {
18 return ch == ':' || ch == '|';
19 }
IsWindowsDriveSeparator(char ch)20 inline bool IsWindowsDriveSeparator(char ch) {
21 return IsWindowsDriveSeparator(static_cast<char16_t>(ch));
22 }
23
24 // Returns the index of the next slash in the input after the given index, or
25 // spec_len if the end of the input is reached.
26 template<typename CHAR>
FindNextSlash(const CHAR * spec,int begin_index,int spec_len)27 inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) {
28 int idx = begin_index;
29 while (idx < spec_len && !IsURLSlash(spec[idx]))
30 idx++;
31 return idx;
32 }
33
34 // DoesContainWindowsDriveSpecUntil returns the least number between
35 // start_offset and max_offset such that the spec has a valid drive
36 // specification starting at that offset. Otherwise it returns -1. This function
37 // gracefully handles, by returning -1, start_offset values that are equal to or
38 // larger than the spec_len, and caps max_offset appropriately to simplify
39 // callers. max_offset must be at least start_offset.
40 template <typename CHAR>
DoesContainWindowsDriveSpecUntil(const CHAR * spec,int start_offset,int max_offset,int spec_len)41 inline int DoesContainWindowsDriveSpecUntil(const CHAR* spec,
42 int start_offset,
43 int max_offset,
44 int spec_len) {
45 CHECK_LE(start_offset, max_offset);
46 if (start_offset > spec_len - 2)
47 return -1; // Not enough room.
48 if (max_offset > spec_len - 2)
49 max_offset = spec_len - 2;
50 for (int offset = start_offset; offset <= max_offset; ++offset) {
51 if (!base::IsAsciiAlpha(spec[offset]))
52 continue; // Doesn't contain a valid drive letter.
53 if (!IsWindowsDriveSeparator(spec[offset + 1]))
54 continue; // Isn't followed with a drive separator.
55 return offset;
56 }
57 return -1;
58 }
59
60 // Returns true if the start_offset in the given spec looks like it begins a
61 // drive spec, for example "c:". This function explicitly handles start_offset
62 // values that are equal to or larger than the spec_len to simplify callers.
63 //
64 // If this returns true, the spec is guaranteed to have a valid drive letter
65 // plus a drive letter separator (a colon or a pipe) starting at |start_offset|.
66 template <typename CHAR>
DoesBeginWindowsDriveSpec(const CHAR * spec,int start_offset,int spec_len)67 inline bool DoesBeginWindowsDriveSpec(const CHAR* spec,
68 int start_offset,
69 int spec_len) {
70 return DoesContainWindowsDriveSpecUntil(spec, start_offset, start_offset,
71 spec_len) == start_offset;
72 }
73
74 #ifdef WIN32
75
76 // Returns true if the start_offset in the given text looks like it begins a
77 // UNC path, for example "\\". This function explicitly handles start_offset
78 // values that are equal to or larger than the spec_len to simplify callers.
79 //
80 // When strict_slashes is set, this function will only accept backslashes as is
81 // standard for Windows. Otherwise, it will accept forward slashes as well
82 // which we use for a lot of URL handling.
83 template<typename CHAR>
DoesBeginUNCPath(const CHAR * text,int start_offset,int len,bool strict_slashes)84 inline bool DoesBeginUNCPath(const CHAR* text,
85 int start_offset,
86 int len,
87 bool strict_slashes) {
88 int remaining_len = len - start_offset;
89 if (remaining_len < 2)
90 return false;
91
92 if (strict_slashes)
93 return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
94 return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]);
95 }
96
97 #endif // WIN32
98
99 } // namespace url
100
101 #endif // URL_URL_FILE_H_
102