ChunkSet.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //* -*- Mode: C++; tab-width: 8; 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. #ifndef ChunkSet_h__
  6. #define ChunkSet_h__
  7. #include "Entries.h"
  8. #include "nsString.h"
  9. #include "nsTArray.h"
  10. namespace mozilla {
  11. namespace safebrowsing {
  12. /**
  13. * Store the chunk numbers as an array of ranges of uint32_t.
  14. * We need chunk numbers in order to ask for incremental updates from the
  15. * server.
  16. */
  17. class ChunkSet {
  18. public:
  19. nsresult Serialize(nsACString& aStr);
  20. nsresult Set(uint32_t aChunk);
  21. bool Has(uint32_t chunk) const;
  22. nsresult Merge(const ChunkSet& aOther);
  23. uint32_t Length() const;
  24. nsresult Remove(const ChunkSet& aOther);
  25. void Clear();
  26. nsresult Write(nsIOutputStream* aOut);
  27. nsresult Read(nsIInputStream* aIn, uint32_t aNumElements);
  28. private:
  29. class Range {
  30. public:
  31. Range(uint32_t aBegin, uint32_t aEnd) : mBegin(aBegin), mEnd(aEnd) {}
  32. uint32_t Length() const;
  33. nsresult Remove(const Range& aRange, ChunkSet& aRemainderSet) const;
  34. bool FoldLeft(const Range& aRange);
  35. bool operator==(const Range& rhs) const {
  36. return mBegin == rhs.mBegin;
  37. }
  38. bool operator<(const Range& rhs) const {
  39. return mBegin < rhs.mBegin;
  40. }
  41. uint32_t Begin() const {
  42. return mBegin;
  43. }
  44. void Begin(const uint32_t aBegin) {
  45. mBegin = aBegin;
  46. }
  47. uint32_t End() const {
  48. return mEnd;
  49. }
  50. void End(const uint32_t aEnd) {
  51. mEnd = aEnd;
  52. }
  53. bool Contains(const Range& aRange) const {
  54. return mBegin <= aRange.mBegin && aRange.mEnd <= mEnd;
  55. }
  56. bool Precedes(const Range& aRange) const {
  57. return mEnd + 1 == aRange.mBegin;
  58. }
  59. struct IntersectionComparator {
  60. int operator()(const Range& aRange) const {
  61. if (aRange.mBegin > mTarget.mEnd) {
  62. return -1;
  63. }
  64. if (mTarget.mBegin > aRange.mEnd) {
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. explicit IntersectionComparator(const Range& aTarget) : mTarget(aTarget){}
  70. const Range& mTarget;
  71. };
  72. private:
  73. uint32_t mBegin;
  74. uint32_t mEnd;
  75. };
  76. static const size_t IO_BUFFER_SIZE = 1024;
  77. FallibleTArray<Range> mRanges;
  78. bool HasSubrange(const Range& aSubrange) const;
  79. };
  80. } // namespace safebrowsing
  81. } // namespace mozilla
  82. #endif