tor-browser

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

IDNBlocklistUtils.cpp (1858B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "IDNBlocklistUtils.h"
      6 
      7 #include "nsStringFwd.h"
      8 
      9 namespace mozilla {
     10 namespace net {
     11 
     12 static constexpr char16_t sBlocklistPairs[][2] = {
     13 #include "IDNCharacterBlocklist.inc"
     14 };
     15 
     16 void RemoveCharFromBlocklist(char16_t aChar,
     17                             nsTArray<BlocklistRange>& aBlocklist) {
     18  auto pos = aBlocklist.BinaryIndexOf(aChar, BlocklistPairToCharComparator());
     19  if (pos == nsTArray<BlocklistRange>::NoIndex) {
     20    return;
     21  }
     22 
     23  auto& pair = aBlocklist[pos];
     24 
     25  // If the matched range has a length of one, we can just remove it
     26  if (pair.second == pair.first) {
     27    aBlocklist.RemoveElementAt(pos);
     28    return;
     29  }
     30 
     31  // If the character matches the first element in the range, just update
     32  // the range.
     33  if (aChar == pair.first) {
     34    pair.first = pair.first + 1;
     35    return;
     36  }
     37 
     38  // Also if it matches the last character in the range, we just update it.
     39  if (aChar == pair.second) {
     40    pair.second = pair.second - 1;
     41    return;
     42  }
     43 
     44  // Our character is in the middle of the range, splitting it in two.
     45  // We update the matched range to reflect the values before the character,
     46  // and insert a new range that represents the values after.
     47  char16_t lastElement = pair.second;
     48  pair.second = aChar - 1;
     49  aBlocklist.InsertElementAt(pos + 1,
     50                             std::make_pair(char16_t(aChar + 1), lastElement));
     51 }
     52 
     53 void InitializeBlocklist(nsTArray<BlocklistRange>& aBlocklist) {
     54  aBlocklist.Clear();
     55  for (auto const& arr : sBlocklistPairs) {
     56    // The hardcoded pairs are already sorted.
     57    aBlocklist.AppendElement(std::make_pair(arr[0], arr[1]));
     58  }
     59 }
     60 
     61 }  // namespace net
     62 }  // namespace mozilla