Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / pdf / chunk_stream.h
1 // Copyright (c) 2010 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 #ifndef PDF_CHUNK_STREAM_H_
6 #define PDF_CHUNK_STREAM_H_
7
8 #include <stddef.h>
9 #include <string.h>
10
11 #include <algorithm>
12 #include <array>
13 #include <memory>
14 #include <utility>
15 #include <vector>
16
17 #include "pdf/range_set.h"
18
19 namespace chrome_pdf {
20
21 // This class collects a chunks of data into one data stream. Client can check
22 // if data in certain range is available, and get missing chunks of data.
23 template <uint32_t N>
24 class ChunkStream {
25  public:
26   static constexpr uint32_t kChunkSize = N;
27   using ChunkData = typename std::array<unsigned char, N>;
28
29   ChunkStream() {}
30   ~ChunkStream() {}
31
32   void SetChunkData(uint32_t chunk_index, std::unique_ptr<ChunkData> data) {
33     if (!data)
34       return;
35
36     if (chunk_index >= data_.size())
37       data_.resize(chunk_index + 1);
38
39     if (!data_[chunk_index])
40       ++filled_chunks_count_;
41
42     data_[chunk_index] = std::move(data);
43     filled_chunks_.Union(gfx::Range(chunk_index, chunk_index + 1));
44   }
45
46   bool ReadData(const gfx::Range& range, void* buffer) const {
47     if (!IsRangeAvailable(range))
48       return false;
49
50     unsigned char* data_buffer = static_cast<unsigned char*>(buffer);
51     uint32_t start = range.start();
52     while (start != range.end()) {
53       const uint32_t chunk_index = GetChunkIndex(start);
54       const uint32_t chunk_start = start % kChunkSize;
55       const uint32_t len =
56           std::min(kChunkSize - chunk_start, range.end() - start);
57       memcpy(data_buffer, data_[chunk_index]->data() + chunk_start, len);
58       data_buffer += len;
59       start += len;
60     }
61     return true;
62   }
63
64   uint32_t GetChunkIndex(uint32_t offset) const { return offset / kChunkSize; }
65
66   gfx::Range GetChunksRange(uint32_t offset, uint32_t size) const {
67     return gfx::Range(GetChunkIndex(offset), GetChunkEnd(offset + size));
68   }
69
70   bool IsRangeAvailable(const gfx::Range& range) const {
71     if (!range.IsValid() || range.is_reversed() ||
72         (eof_pos_ > 0 && eof_pos_ < range.end())) {
73       return false;
74     }
75
76     if (range.is_empty())
77       return true;
78
79     const gfx::Range chunks_range(GetChunkIndex(range.start()),
80                                   GetChunkEnd(range.end()));
81     return filled_chunks_.Contains(chunks_range);
82   }
83
84   bool IsChunkAvailable(uint32_t chunk_index) const {
85     return filled_chunks_.Contains(chunk_index);
86   }
87
88   void set_eof_pos(uint32_t eof_pos) { eof_pos_ = eof_pos; }
89   uint32_t eof_pos() const { return eof_pos_; }
90
91   const RangeSet& filled_chunks() const { return filled_chunks_; }
92
93   bool IsComplete() const {
94     return eof_pos_ > 0 && IsRangeAvailable(gfx::Range(0, eof_pos_));
95   }
96
97   bool IsValidChunkIndex(uint32_t chunk_index) const {
98     return !eof_pos_ || (chunk_index <= GetChunkIndex(eof_pos_ - 1));
99   }
100
101   void Clear() {
102     data_.clear();
103     eof_pos_ = 0;
104     filled_chunks_.Clear();
105     filled_chunks_count_ = 0;
106   }
107
108   uint32_t filled_chunks_count() const { return filled_chunks_count_; }
109   uint32_t total_chunks_count() const { return GetChunkEnd(eof_pos_); }
110
111  private:
112   uint32_t GetChunkEnd(uint32_t offset) const {
113     return (offset + kChunkSize - 1) / kChunkSize;
114   }
115
116   std::vector<std::unique_ptr<ChunkData>> data_;
117   uint32_t eof_pos_ = 0;
118   RangeSet filled_chunks_;
119   uint32_t filled_chunks_count_ = 0;
120 };
121
122 }  // namespace chrome_pdf
123
124 #endif  // PDF_CHUNK_STREAM_H_