Home | History | Annotate | Download | only in src
      1 // Copyright 2007, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 // Provides shared functions used by the internals of the parser and
     31 // canonicalizer for file URLs. Do not use outside of these modules.
     32 
     33 #ifndef GOOGLEURL_SRC_URL_FILE_H__
     34 #define GOOGLEURL_SRC_URL_FILE_H__
     35 
     36 #include "googleurl/src/url_parse_internal.h"
     37 
     38 namespace url_parse {
     39 
     40 #ifdef WIN32
     41 
     42 // We allow both "c:" and "c|" as drive identifiers.
     43 inline bool IsWindowsDriveSeparator(char16 ch) {
     44   return ch == ':' || ch == '|';
     45 }
     46 inline bool IsWindowsDriveLetter(char16 ch) {
     47   return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
     48 }
     49 
     50 #endif  // WIN32
     51 
     52 // Returns the index of the next slash in the input after the given index, or
     53 // spec_len if the end of the input is reached.
     54 template<typename CHAR>
     55 inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) {
     56   int idx = begin_index;
     57   while (idx < spec_len && !IsURLSlash(spec[idx]))
     58     idx++;
     59   return idx;
     60 }
     61 
     62 #ifdef WIN32
     63 
     64 // Returns true if the start_offset in the given spec looks like it begins a
     65 // drive spec, for example "c:". This function explicitly handles start_offset
     66 // values that are equal to or larger than the spec_len to simplify callers.
     67 //
     68 // If this returns true, the spec is guaranteed to have a valid drive letter
     69 // plus a colon starting at |start_offset|.
     70 template<typename CHAR>
     71 inline bool DoesBeginWindowsDriveSpec(const CHAR* spec, int start_offset,
     72                                       int spec_len) {
     73   int remaining_len = spec_len - start_offset;
     74   if (remaining_len < 2)
     75     return false;  // Not enough room.
     76   if (!IsWindowsDriveLetter(spec[start_offset]))
     77     return false;  // Doesn't start with a valid drive letter.
     78   if (!IsWindowsDriveSeparator(spec[start_offset + 1]))
     79     return false;  // Isn't followed with a drive separator.
     80   return true;
     81 }
     82 
     83 // Returns true if the start_offset in the given text looks like it begins a
     84 // UNC path, for example "\\". This function explicitly handles start_offset
     85 // values that are equal to or larger than the spec_len to simplify callers.
     86 //
     87 // When strict_slashes is set, this function will only accept backslashes as is
     88 // standard for Windows. Otherwise, it will accept forward slashes as well
     89 // which we use for a lot of URL handling.
     90 template<typename CHAR>
     91 inline bool DoesBeginUNCPath(const CHAR* text,
     92                              int start_offset,
     93                              int len,
     94                              bool strict_slashes) {
     95   int remaining_len = len - start_offset;
     96   if (remaining_len < 2)
     97     return false;
     98 
     99   if (strict_slashes)
    100     return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
    101   return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]);
    102 }
    103 
    104 #endif  // WIN32
    105 
    106 }  // namespace url_parse
    107 
    108 #endif  // GOOGLEURL_SRC_URL_FILE_H__
    109