tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IDNBlocklistUtils.h (2045B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef IDNBlocklistUtils_h__
      7 #define IDNBlocklistUtils_h__
      8 
      9 #include <utility>
     10 #include "nsTArray.h"
     11 
     12 namespace mozilla {
     13 namespace net {
     14 
     15 // A blocklist range is defined as all of the characters between:
     16 // { firstCharacterInRange, lastCharacterInRange }
     17 using BlocklistRange = std::pair<char16_t, char16_t>;
     18 
     19 // Used to perform a binary search of the needle in the sorted array of pairs
     20 class BlocklistPairToCharComparator {
     21 public:
     22  bool Equals(const BlocklistRange& pair, char16_t needle) const {
     23    // If the needle is between pair.first and pair.second it
     24    // is part of the range.
     25    return pair.first <= needle && needle <= pair.second;
     26  }
     27 
     28  bool LessThan(const BlocklistRange& pair, char16_t needle) const {
     29    // The needle has to be larger than the second value,
     30    // otherwise it may be equal.
     31    return pair.second < needle;
     32  }
     33 };
     34 
     35 // Used to sort the array of pairs
     36 class BlocklistEntryComparator {
     37 public:
     38  bool Equals(const BlocklistRange& a, const BlocklistRange& b) const {
     39    return a.first == b.first && a.second == b.second;
     40  }
     41 
     42  bool LessThan(const BlocklistRange& a, const BlocklistRange& b) const {
     43    return a.first < b.first;
     44  }
     45 };
     46 
     47 // Returns true if the char can be found in the blocklist
     48 inline bool CharInBlocklist(char16_t aChar,
     49                            const nsTArray<BlocklistRange>& aBlocklist) {
     50  return aBlocklist.ContainsSorted(aChar, BlocklistPairToCharComparator());
     51 }
     52 
     53 // Initializes the blocklist based on the statically defined list
     54 void InitializeBlocklist(nsTArray<BlocklistRange>& aBlocklist);
     55 
     56 void RemoveCharFromBlocklist(char16_t aChar,
     57                             nsTArray<BlocklistRange>& aBlocklist);
     58 
     59 }  // namespace net
     60 }  // namespace mozilla
     61 
     62 #endif  // IDNBlocklistUtils_h__