xref: /aosp_15_r20/external/cronet/url/url_file.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 && !IsSlashOrBackslash(spec[idx])) {
30     idx++;
31   }
32   return idx;
33 }
34 
35 // DoesContainWindowsDriveSpecUntil returns the least number between
36 // start_offset and max_offset such that the spec has a valid drive
37 // specification starting at that offset. Otherwise it returns -1. This function
38 // gracefully handles, by returning -1, start_offset values that are equal to or
39 // larger than the spec_len, and caps max_offset appropriately to simplify
40 // callers. max_offset must be at least start_offset.
41 template <typename CHAR>
DoesContainWindowsDriveSpecUntil(const CHAR * spec,int start_offset,int max_offset,int spec_len)42 inline int DoesContainWindowsDriveSpecUntil(const CHAR* spec,
43                                             int start_offset,
44                                             int max_offset,
45                                             int spec_len) {
46   CHECK_LE(start_offset, max_offset);
47   if (start_offset > spec_len - 2)
48     return -1;  // Not enough room.
49   if (max_offset > spec_len - 2)
50     max_offset = spec_len - 2;
51   for (int offset = start_offset; offset <= max_offset; ++offset) {
52     if (!base::IsAsciiAlpha(spec[offset]))
53       continue;  // Doesn't contain a valid drive letter.
54     if (!IsWindowsDriveSeparator(spec[offset + 1]))
55       continue;  // Isn't followed with a drive separator.
56     return offset;
57   }
58   return -1;
59 }
60 
61 // Returns true if the start_offset in the given spec looks like it begins a
62 // drive spec, for example "c:". This function explicitly handles start_offset
63 // values that are equal to or larger than the spec_len to simplify callers.
64 //
65 // If this returns true, the spec is guaranteed to have a valid drive letter
66 // plus a drive letter separator (a colon or a pipe) starting at |start_offset|.
67 template <typename CHAR>
DoesBeginWindowsDriveSpec(const CHAR * spec,int start_offset,int spec_len)68 inline bool DoesBeginWindowsDriveSpec(const CHAR* spec,
69                                       int start_offset,
70                                       int spec_len) {
71   return DoesContainWindowsDriveSpecUntil(spec, start_offset, start_offset,
72                                           spec_len) == start_offset;
73 }
74 
75 #ifdef WIN32
76 
77 // Returns true if the start_offset in the given text looks like it begins a
78 // UNC path, for example "\\". This function explicitly handles start_offset
79 // values that are equal to or larger than the spec_len to simplify callers.
80 //
81 // When strict_slashes is set, this function will only accept backslashes as is
82 // standard for Windows. Otherwise, it will accept forward slashes as well
83 // which we use for a lot of URL handling.
84 template<typename CHAR>
DoesBeginUNCPath(const CHAR * text,int start_offset,int len,bool strict_slashes)85 inline bool DoesBeginUNCPath(const CHAR* text,
86                              int start_offset,
87                              int len,
88                              bool strict_slashes) {
89   int remaining_len = len - start_offset;
90   if (remaining_len < 2)
91     return false;
92 
93   if (strict_slashes)
94     return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
95   return IsSlashOrBackslash(text[start_offset]) &&
96          IsSlashOrBackslash(text[start_offset + 1]);
97 }
98 
99 #endif  // WIN32
100 
101 }  // namespace url
102 
103 #endif  // URL_URL_FILE_H_
104