summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/ChunkSet.cpp
blob: 162a74260c51dcc40e9e183568a11e895548c862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ChunkSet.h"

namespace mozilla {
namespace safebrowsing {

const size_t ChunkSet::IO_BUFFER_SIZE;

nsresult
ChunkSet::Serialize(nsACString& aChunkStr)
{
  aChunkStr.Truncate();
  for (const Range* range = mRanges.begin(); range != mRanges.end(); range++) {
    if (range != mRanges.begin()) {
      aChunkStr.Append(',');
    }

    aChunkStr.AppendInt((int32_t)range->Begin());
    if (range->Begin() != range->End()) {
      aChunkStr.Append('-');
      aChunkStr.AppendInt((int32_t)range->End());
    }
  }

  return NS_OK;
}

nsresult
ChunkSet::Set(uint32_t aChunk)
{
  if (!Has(aChunk)) {
    Range chunkRange(aChunk, aChunk);

    if (mRanges.Length() == 0) {
      if (!mRanges.AppendElement(chunkRange, fallible)) {
        return NS_ERROR_OUT_OF_MEMORY;
      }
      return NS_OK;
    }

    if (mRanges.LastElement().Precedes(chunkRange)) {
      mRanges.LastElement().End(aChunk);
    } else if (chunkRange.Precedes(mRanges[0])) {
      mRanges[0].Begin(aChunk);
    } else {
      ChunkSet tmp;
      if (!tmp.mRanges.AppendElement(chunkRange, fallible)) {
        return NS_ERROR_OUT_OF_MEMORY;
      }

      return Merge(tmp);
    }
  }

  return NS_OK;
}

bool
ChunkSet::Has(uint32_t aChunk) const
{
  size_t idx;
  return BinarySearchIf(mRanges, 0, mRanges.Length(),
                        Range::IntersectionComparator(Range(aChunk, aChunk)),
                        &idx);
                        // IntersectionComparator works because we create a
                        // single-chunk range.
}

nsresult
ChunkSet::Merge(const ChunkSet& aOther)
{
  size_t oldLen = mRanges.Length();

  for (const Range* mergeRange = aOther.mRanges.begin();
       mergeRange != aOther.mRanges.end(); mergeRange++) {
    if (!HasSubrange(*mergeRange)) {
      if (!mRanges.InsertElementSorted(*mergeRange, fallible)) {
        return NS_ERROR_OUT_OF_MEMORY;
      }
    }
  }

  if (oldLen < mRanges.Length()) {
    for (size_t i = 1; i < mRanges.Length(); i++) {
      while (mRanges[i - 1].FoldLeft(mRanges[i])) {
        mRanges.RemoveElementAt(i);

        if (i == mRanges.Length()) {
          return NS_OK;
        }
      }
    }
  }

  return NS_OK;
}

uint32_t
ChunkSet::Length() const
{
  uint32_t len = 0;
  for (const Range* range = mRanges.begin(); range != mRanges.end(); range++) {
    len += range->Length();
  }

  return len;
}

nsresult
ChunkSet::Remove(const ChunkSet& aOther)
{
  for (const Range* removalRange = aOther.mRanges.begin();
       removalRange != aOther.mRanges.end(); removalRange++) {

    if (mRanges.Length() == 0) {
      return NS_OK;
    }

    if (mRanges.LastElement().End() < removalRange->Begin() ||
        aOther.mRanges.LastElement().End() < mRanges[0].Begin()) {
      return NS_OK;
    }

    size_t intersectionIdx;
    while (BinarySearchIf(mRanges, 0, mRanges.Length(),
           Range::IntersectionComparator(*removalRange), &intersectionIdx)) {

      ChunkSet remains;
      nsresult rv = mRanges[intersectionIdx].Remove(*removalRange, remains);

      if (NS_FAILED(rv)) {
        return rv;
      }

      mRanges.RemoveElementAt(intersectionIdx);
      if (!mRanges.InsertElementsAt(intersectionIdx, remains.mRanges,
                                    fallible)) {
        return NS_ERROR_OUT_OF_MEMORY;
      }
    }
  }

  return NS_OK;
}

void
ChunkSet::Clear()
{
  mRanges.Clear();
}

nsresult
ChunkSet::Write(nsIOutputStream* aOut)
{
  nsTArray<uint32_t> chunks(IO_BUFFER_SIZE);

  for (const Range* range = mRanges.begin(); range != mRanges.end(); range++) {
    for (uint32_t chunk = range->Begin(); chunk <= range->End(); chunk++) {
      chunks.AppendElement(chunk);

      if (chunks.Length() == chunks.Capacity()) {
        nsresult rv = WriteTArray(aOut, chunks);

        if (NS_FAILED(rv)) {
          return rv;
        }

        chunks.Clear();
      }
    }
  }

  nsresult rv = WriteTArray(aOut, chunks);

  if (NS_FAILED(rv)) {
    return rv;
  }

  return NS_OK;
}

nsresult
ChunkSet::Read(nsIInputStream* aIn, uint32_t aNumElements)
{
  nsTArray<uint32_t> chunks(IO_BUFFER_SIZE);

  while (aNumElements != 0) {
    chunks.Clear();

    uint32_t numToRead = aNumElements > IO_BUFFER_SIZE ? IO_BUFFER_SIZE : aNumElements;

    nsresult rv = ReadTArray(aIn, &chunks, numToRead);

    if (NS_FAILED(rv)) {
      return rv;
    }

    aNumElements -= numToRead;

    for (const uint32_t* c = chunks.begin(); c != chunks.end(); c++) {
      rv = Set(*c);

      if (NS_FAILED(rv)) {
        return rv;
      }
    }
  }

  return NS_OK;
}

bool
ChunkSet::HasSubrange(const Range& aSubrange) const
{
  for (const Range* range = mRanges.begin(); range != mRanges.end(); range++) {
    if (range->Contains(aSubrange)) {
      return true;
    } else if (!(aSubrange.Begin() > range->End() ||
                 range->Begin() > aSubrange.End())) {
      // In this case, aSubrange overlaps this range but is not a subrange.
      // because the ChunkSet implementation ensures that there are no
      // overlapping ranges, this means that aSubrange cannot be a subrange of
      // any of the following ranges
      return false;
    }
  }

  return false;
}

uint32_t
ChunkSet::Range::Length() const
{
  return mEnd - mBegin + 1;
}

nsresult
ChunkSet::Range::Remove(const Range& aRange, ChunkSet& aRemainderSet) const
{
  if (mBegin < aRange.mBegin && aRange.mBegin <= mEnd) {
    // aRange overlaps & follows this range
    Range range(mBegin, aRange.mBegin - 1);
    if (!aRemainderSet.mRanges.AppendElement(range, fallible)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }

  if (mBegin <= aRange.mEnd && aRange.mEnd < mEnd) {
    // aRange overlaps & precedes this range
    Range range(aRange.mEnd + 1, mEnd);
    if (!aRemainderSet.mRanges.AppendElement(range, fallible)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }

  return NS_OK;
}

bool
ChunkSet::Range::FoldLeft(const Range& aRange)
{
  if (Contains(aRange)) {
    return true;
  } else if (Precedes(aRange) ||
             (mBegin <= aRange.mBegin && aRange.mBegin <= mEnd)) {
    mEnd = aRange.mEnd;
    return true;
  }

  return false;
}

} // namespace safebrowsing
} // namespace mozilla