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