Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/ds/Dafsa.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
8
// Use of this source code is governed by a BSD-style license that can be
9
// found in the LICENSE file.
10
11
#include "Dafsa.h"
12
13
#include "mozilla/Assertions.h"
14
#include "nsAString.h"
15
16
const int mozilla::Dafsa::kKeyNotFound = -1;
17
18
// Note the DAFSA implementation was lifted from eTLD code in Chromium that was
19
// originally lifted from Firefox.
20
21
// Read next offset from pos.
22
// Returns true if an offset could be read, false otherwise.
23
bool GetNextOffset(const unsigned char** pos, const unsigned char* end,
24
0
                   const unsigned char** offset) {
25
0
  if (*pos == end)
26
0
    return false;
27
0
28
0
  // When reading an offset the byte array must always contain at least
29
0
  // three more bytes to consume. First the offset to read, then a node
30
0
  // to skip over and finally a destination node. No object can be smaller
31
0
  // than one byte.
32
0
  MOZ_ASSERT(*pos + 2 < end);
33
0
  size_t bytes_consumed;
34
0
  switch (**pos & 0x60) {
35
0
    case 0x60:  // Read three byte offset
36
0
      *offset += (((*pos)[0] & 0x1F) << 16) | ((*pos)[1] << 8) | (*pos)[2];
37
0
      bytes_consumed = 3;
38
0
      break;
39
0
    case 0x40:  // Read two byte offset
40
0
      *offset += (((*pos)[0] & 0x1F) << 8) | (*pos)[1];
41
0
      bytes_consumed = 2;
42
0
      break;
43
0
    default:
44
0
      *offset += (*pos)[0] & 0x3F;
45
0
      bytes_consumed = 1;
46
0
  }
47
0
  if ((**pos & 0x80) != 0) {
48
0
    *pos = end;
49
0
  } else {
50
0
    *pos += bytes_consumed;
51
0
  }
52
0
  return true;
53
0
}
54
55
// Check if byte at offset is last in label.
56
0
bool IsEOL(const unsigned char* offset, const unsigned char* end) {
57
0
  MOZ_ASSERT(offset < end);
58
0
  return (*offset & 0x80) != 0;
59
0
}
60
61
// Check if byte at offset matches first character in key.
62
// This version matches characters not last in label.
63
bool IsMatch(const unsigned char* offset, const unsigned char* end,
64
0
             const char* key) {
65
0
  MOZ_ASSERT(offset < end);
66
0
  return *offset == *key;
67
0
}
68
69
// Check if byte at offset matches first character in key.
70
// This version matches characters last in label.
71
bool IsEndCharMatch(const unsigned char* offset, const unsigned char* end,
72
0
                    const char* key) {
73
0
  MOZ_ASSERT(offset < end);
74
0
  return *offset == (*key | 0x80);
75
0
}
76
77
// Read return value at offset.
78
// Returns true if a return value could be read, false otherwise.
79
bool GetReturnValue(const unsigned char* offset, const unsigned char* end,
80
0
                    int* return_value) {
81
0
  MOZ_ASSERT(offset < end);
82
0
  if ((*offset & 0xE0) == 0x80) {
83
0
    *return_value = *offset & 0x0F;
84
0
    return true;
85
0
  }
86
0
  return false;
87
0
}
88
89
// Lookup a domain key in a byte array generated by make_dafsa.py.
90
// The rule type is returned if key is found, otherwise kKeyNotFound is returned.
91
int LookupString(const unsigned char* graph, size_t length, const char* key,
92
0
                 size_t key_length) {
93
0
  const unsigned char* pos = graph;
94
0
  const unsigned char* end = graph + length;
95
0
  const unsigned char* offset = pos;
96
0
  const char* key_end = key + key_length;
97
0
  while (GetNextOffset(&pos, end, &offset)) {
98
0
    //   char <char>+ end_char offsets
99
0
    //   char <char>+ return value
100
0
    //   char end_char offsets
101
0
    //   char return value
102
0
    //   end_char offsets
103
0
    //   return_value
104
0
    bool did_consume = false;
105
0
    if (key != key_end && !IsEOL(offset, end)) {
106
0
      // Leading <char> is not a match. Don't dive into this child
107
0
      if (!IsMatch(offset, end, key))
108
0
        continue;
109
0
      did_consume = true;
110
0
      ++offset;
111
0
      ++key;
112
0
      // Possible matches at this point:
113
0
      // <char>+ end_char offsets
114
0
      // <char>+ return value
115
0
      // end_char offsets
116
0
      // return value
117
0
      // Remove all remaining <char> nodes possible
118
0
      while (!IsEOL(offset, end) && key != key_end) {
119
0
        if (!IsMatch(offset, end, key))
120
0
          return mozilla::Dafsa::kKeyNotFound;
121
0
        ++key;
122
0
        ++offset;
123
0
      }
124
0
    }
125
0
    // Possible matches at this point:
126
0
    // end_char offsets
127
0
    // return_value
128
0
    // If one or more <char> elements were consumed, a failure
129
0
    // to match is terminal. Otherwise, try the next node.
130
0
    if (key == key_end) {
131
0
      int return_value;
132
0
      if (GetReturnValue(offset, end, &return_value))
133
0
        return return_value;
134
0
      // The DAFSA guarantees that if the first char is a match, all
135
0
      // remaining char elements MUST match if the key is truly present.
136
0
      if (did_consume)
137
0
        return mozilla::Dafsa::kKeyNotFound;
138
0
      continue;
139
0
    }
140
0
    if (!IsEndCharMatch(offset, end, key)) {
141
0
      if (did_consume)
142
0
        return mozilla::Dafsa::kKeyNotFound;  // Unexpected
143
0
      continue;
144
0
    }
145
0
    ++key;
146
0
    pos = ++offset;  // Dive into child
147
0
  }
148
0
  return mozilla::Dafsa::kKeyNotFound;  // No match
149
0
}
150
151
namespace mozilla {
152
153
int Dafsa::Lookup(const nsACString& aKey) const
154
0
{
155
0
  return LookupString(mData.Elements(), mData.Length(),
156
0
                      aKey.BeginReading(), aKey.Length());
157
0
}
158
159
} // namespace mozilla