| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PDF_CHUNK_STREAM_H_ | 5 #ifndef PDF_CHUNK_STREAM_H_ |
| 6 #define PDF_CHUNK_STREAM_H_ | 6 #define PDF_CHUNK_STREAM_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <string.h> | |
| 10 | 9 |
| 11 #include <algorithm> | 10 #include <map> |
| 12 #include <array> | 11 #include <utility> |
| 13 #include <memory> | |
| 14 #include <vector> | 12 #include <vector> |
| 15 | 13 |
| 16 #include "pdf/range_set.h" | |
| 17 | |
| 18 namespace chrome_pdf { | 14 namespace chrome_pdf { |
| 19 | 15 |
| 20 // This class collects a chunks of data into one data stream. Client can check | 16 // This class collects a chunks of data into one data stream. Client can check |
| 21 // if data in certain range is available, and get missing chunks of data. | 17 // if data in certain range is available, and get missing chunks of data. |
| 22 template <uint32_t N> | |
| 23 class ChunkStream { | 18 class ChunkStream { |
| 24 public: | 19 public: |
| 25 static constexpr uint32_t kChunkSize = N; | 20 ChunkStream(); |
| 26 using ChunkData = typename std::array<unsigned char, N>; | 21 ~ChunkStream(); |
| 27 | 22 |
| 28 ChunkStream() {} | 23 void Clear(); |
| 29 ~ChunkStream() {} | |
| 30 | 24 |
| 31 void SetChunkData(uint32_t chunk_index, std::unique_ptr<ChunkData> data) { | 25 void Preallocate(size_t stream_size); |
| 32 if (!data) | 26 size_t GetSize() const; |
| 33 return; | |
| 34 if (chunk_index >= data_.size()) { | |
| 35 data_.resize(chunk_index + 1); | |
| 36 } | |
| 37 if (!data_[chunk_index]) { | |
| 38 ++filled_chunks_count_; | |
| 39 } | |
| 40 data_[chunk_index] = std::move(data); | |
| 41 filled_chunks_.Union(gfx::Range(chunk_index, chunk_index + 1)); | |
| 42 } | |
| 43 | 27 |
| 44 bool ReadData(const gfx::Range& range, void* buffer) const { | 28 bool WriteData(size_t offset, void* buffer, size_t size); |
| 45 if (!IsRangeAvailable(range)) { | 29 bool ReadData(size_t offset, size_t size, void* buffer) const; |
| 46 return false; | |
| 47 } | |
| 48 unsigned char* data_buffer = static_cast<unsigned char*>(buffer); | |
| 49 uint32_t start = range.start(); | |
| 50 while (start != range.end()) { | |
| 51 const uint32_t chunk_index = GetChunkIndex(start); | |
| 52 const uint32_t chunk_start = start % kChunkSize; | |
| 53 const uint32_t len = | |
| 54 std::min(kChunkSize - chunk_start, range.end() - start); | |
| 55 memcpy(data_buffer, data_[chunk_index]->data() + chunk_start, len); | |
| 56 data_buffer += len; | |
| 57 start += len; | |
| 58 } | |
| 59 return true; | |
| 60 } | |
| 61 | 30 |
| 62 uint32_t GetChunkIndex(uint32_t offset) const { return offset / kChunkSize; } | 31 // Returns vector of pairs where first is an offset, second is a size. |
| 32 bool GetMissedRanges(size_t offset, size_t size, |
| 33 std::vector<std::pair<size_t, size_t> >* ranges) const; |
| 34 bool IsRangeAvailable(size_t offset, size_t size) const; |
| 35 size_t GetFirstMissingByte() const; |
| 63 | 36 |
| 64 gfx::Range GetChunksRange(uint32_t offset, uint32_t size) const { | 37 // Finds the first byte of the missing byte interval that offset belongs to. |
| 65 return gfx::Range(GetChunkIndex(offset), | 38 size_t GetFirstMissingByteInInterval(size_t offset) const; |
| 66 GetChunkIndex(offset + size + kChunkSize - 1)); | 39 // Returns the last byte of the missing byte interval that offset belongs to. |
| 67 } | 40 size_t GetLastMissingByteInInterval(size_t offset) const; |
| 68 | |
| 69 bool IsRangeAvailable(const gfx::Range& range) const { | |
| 70 if (!range.IsValid() || range.is_reversed() || | |
| 71 (eof_pos_ > 0 && eof_pos_ < range.end())) | |
| 72 return false; | |
| 73 if (range.is_empty()) | |
| 74 return true; | |
| 75 const gfx::Range chunks_range(GetChunkIndex(range.start()), | |
| 76 GetChunkIndex(range.end() + kChunkSize - 1)); | |
| 77 return filled_chunks_.Contains(chunks_range); | |
| 78 } | |
| 79 | |
| 80 void set_eof_pos(uint32_t eof_pos) { eof_pos_ = eof_pos; } | |
| 81 uint32_t eof_pos() const { return eof_pos_; } | |
| 82 | |
| 83 const RangeSet& filled_chunks() const { return filled_chunks_; } | |
| 84 | |
| 85 bool IsComplete() const { | |
| 86 return eof_pos_ > 0 && IsRangeAvailable(gfx::Range(0, eof_pos_)); | |
| 87 } | |
| 88 | |
| 89 void Clear() { | |
| 90 data_.clear(); | |
| 91 eof_pos_ = 0; | |
| 92 filled_chunks_.Clear(); | |
| 93 filled_chunks_count_ = 0; | |
| 94 } | |
| 95 | |
| 96 uint32_t filled_chunks_count() const { return filled_chunks_count_; } | |
| 97 uint32_t total_chunks_count() const { | |
| 98 return GetChunkIndex(eof_pos_ + kChunkSize - 1); | |
| 99 } | |
| 100 | 41 |
| 101 private: | 42 private: |
| 102 std::vector<std::unique_ptr<ChunkData>> data_; | 43 std::vector<unsigned char> data_; |
| 103 uint32_t eof_pos_ = 0; | 44 |
| 104 RangeSet filled_chunks_; | 45 // Pair, first - begining of the chunk, second - size of the chunk. |
| 105 uint32_t filled_chunks_count_ = 0; | 46 std::map<size_t, size_t> chunks_; |
| 47 |
| 48 size_t stream_size_; |
| 106 }; | 49 }; |
| 107 | 50 |
| 108 }; // namespace chrome_pdf | 51 }; // namespace chrome_pdf |
| 109 | 52 |
| 110 #endif // PDF_CHUNK_STREAM_H_ | 53 #endif // PDF_CHUNK_STREAM_H_ |
| OLD | NEW |