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 typedef std::pair<char16_t, char16_t> BlocklistRange;
18 
19 // Used to perform a binary search of the needle in the sorted array of pairs
20 class BlocklistPairToCharComparator {
21  public:
Equals(const BlocklistRange & pair,char16_t needle)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 
LessThan(const BlocklistRange & pair,char16_t needle)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:
Equals(const BlocklistRange & a,const BlocklistRange & b)38   bool Equals(const BlocklistRange& a, const BlocklistRange& b) const {
39     return a.first == b.first && a.second == b.second;
40   }
41 
LessThan(const BlocklistRange & a,const BlocklistRange & b)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
CharInBlocklist(char16_t aChar,const nsTArray<BlocklistRange> & aBlocklist)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 and the
54 // values of the following preferences:
55 //     - network.IDN.extra_allowed_chars
56 //     - network.IDN.extra_blocked_chars
57 void InitializeBlocklist(nsTArray<BlocklistRange>& aBlocklist);
58 
59 void RemoveCharFromBlocklist(char16_t aChar,
60                              nsTArray<BlocklistRange>& aBlocklist);
61 
62 }  // namespace net
63 }  // namespace mozilla
64 
65 #endif  // IDNBlocklistUtils_h__
66