1// Copyright 2013 The Chromium Authors. All rights reserved.
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 "url/url_parse_internal.h"
12
13namespace url {
14
15#ifdef WIN32
16
17// We allow both "c:" and "c|" as drive identifiers.
18inline bool IsWindowsDriveSeparator(base::char16 ch) {
19  return ch == ':' || ch == '|';
20}
21inline bool IsWindowsDriveLetter(base::char16 ch) {
22  return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
23}
24
25#endif  // WIN32
26
27// Returns the index of the next slash in the input after the given index, or
28// spec_len if the end of the input is reached.
29template<typename CHAR>
30inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) {
31  int idx = begin_index;
32  while (idx < spec_len && !IsURLSlash(spec[idx]))
33    idx++;
34  return idx;
35}
36
37#ifdef WIN32
38
39// Returns true if the start_offset in the given spec looks like it begins a
40// drive spec, for example "c:". This function explicitly handles start_offset
41// values that are equal to or larger than the spec_len to simplify callers.
42//
43// If this returns true, the spec is guaranteed to have a valid drive letter
44// plus a colon starting at |start_offset|.
45template<typename CHAR>
46inline bool DoesBeginWindowsDriveSpec(const CHAR* spec, int start_offset,
47                                      int spec_len) {
48  int remaining_len = spec_len - start_offset;
49  if (remaining_len < 2)
50    return false;  // Not enough room.
51  if (!IsWindowsDriveLetter(spec[start_offset]))
52    return false;  // Doesn't start with a valid drive letter.
53  if (!IsWindowsDriveSeparator(spec[start_offset + 1]))
54    return false;  // Isn't followed with a drive separator.
55  return true;
56}
57
58// Returns true if the start_offset in the given text looks like it begins a
59// UNC path, for example "\\". This function explicitly handles start_offset
60// values that are equal to or larger than the spec_len to simplify callers.
61//
62// When strict_slashes is set, this function will only accept backslashes as is
63// standard for Windows. Otherwise, it will accept forward slashes as well
64// which we use for a lot of URL handling.
65template<typename CHAR>
66inline bool DoesBeginUNCPath(const CHAR* text,
67                             int start_offset,
68                             int len,
69                             bool strict_slashes) {
70  int remaining_len = len - start_offset;
71  if (remaining_len < 2)
72    return false;
73
74  if (strict_slashes)
75    return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
76  return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]);
77}
78
79#endif  // WIN32
80
81}  // namespace url
82
83#endif  // URL_URL_FILE_H_
84