1// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// Class for parsing lists of integers into ranges.
6//
7// The anti-phishing and anti-malware protocol sends ASCII strings of numbers
8// and ranges of numbers corresponding to chunks of whitelists and blacklists.
9// Clients of this protocol need to be able to convert back and forth between
10// this representation, and individual integer chunk numbers. The ChunkRange
11// class is a simple and compact mechanism for storing a continuous list of
12// chunk numbers.
13
14#ifndef CHROME_BROWSER_SAFE_BROWSING_CHUNK_RANGE_H_
15#define CHROME_BROWSER_SAFE_BROWSING_CHUNK_RANGE_H_
16#pragma once
17
18#include <string>
19#include <vector>
20
21// ChunkRange ------------------------------------------------------------------
22// Each ChunkRange represents a continuous range of chunk numbers [start, stop].
23
24class ChunkRange {
25 public:
26  explicit ChunkRange(int start);
27  ChunkRange(int start, int stop);
28  ChunkRange(const ChunkRange& rhs);
29
30  inline int start() const { return start_; }
31  inline int stop() const { return stop_; }
32
33  bool operator==(const ChunkRange& rhs) const {
34    return start_ == rhs.start() && stop_ == rhs.stop();
35  }
36
37 private:
38  int start_;
39  int stop_;
40};
41
42
43// Helper functions ------------------------------------------------------------
44
45// Convert a set of ranges into individual chunk numbers.
46void RangesToChunks(const std::vector<ChunkRange>& ranges,
47                    std::vector<int>* chunks);
48
49// Returns 'true' if the string was successfully converted to ChunkRanges,
50// 'false' if the input was malformed.
51// The string must be in the form: "1-100,398,415,1138-2001,2019".
52bool StringToRanges(const std::string& input,
53                    std::vector<ChunkRange>* ranges);
54
55// Convenience for going from a list of chunks to a string in protocol
56// format.
57void ChunksToRangeString(const std::vector<int>& chunks, std::string* result);
58
59// Tests if a chunk number is contained a sorted vector of ChunkRanges.
60bool IsChunkInRange(int chunk_number, const std::vector<ChunkRange>& ranges);
61
62#endif  // CHROME_BROWSER_SAFE_BROWSING_CHUNK_RANGE_H_
63