| 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 #include "pdf/chunk_stream.h" | 5 #include "pdf/chunk_stream.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #define __STDC_LIMIT_MACROS | 10 #define __STDC_LIMIT_MACROS |
| 11 #ifdef _WIN32 | 11 #ifdef _WIN32 |
| 12 #include <limits.h> | 12 #include <limits.h> |
| 13 #else | 13 #else |
| 14 #include <stdint.h> | 14 #include <stdint.h> |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 #include <algorithm> | 17 #include <algorithm> |
| 18 | 18 |
| 19 namespace chrome_pdf { | 19 namespace chrome_pdf { |
| 20 | 20 |
| 21 ChunkStream::ChunkStream() : stream_size_(0) { | 21 ChunkStream::ChunkStream() : stream_size_(0) {} |
| 22 } | |
| 23 | 22 |
| 24 ChunkStream::~ChunkStream() { | 23 ChunkStream::~ChunkStream() {} |
| 25 } | |
| 26 | 24 |
| 27 void ChunkStream::Clear() { | 25 void ChunkStream::Clear() { |
| 28 chunks_.clear(); | 26 chunks_.clear(); |
| 29 data_.clear(); | 27 data_.clear(); |
| 30 stream_size_ = 0; | 28 stream_size_ = 0; |
| 31 } | 29 } |
| 32 | 30 |
| 33 void ChunkStream::Preallocate(size_t stream_size) { | 31 void ChunkStream::Preallocate(size_t stream_size) { |
| 34 data_.reserve(stream_size); | 32 data_.reserve(stream_size); |
| 35 stream_size_ = stream_size; | 33 stream_size_ = stream_size; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 78 |
| 81 bool ChunkStream::ReadData(size_t offset, size_t size, void* buffer) const { | 79 bool ChunkStream::ReadData(size_t offset, size_t size, void* buffer) const { |
| 82 if (!IsRangeAvailable(offset, size)) | 80 if (!IsRangeAvailable(offset, size)) |
| 83 return false; | 81 return false; |
| 84 | 82 |
| 85 memcpy(buffer, &data_[offset], size); | 83 memcpy(buffer, &data_[offset], size); |
| 86 return true; | 84 return true; |
| 87 } | 85 } |
| 88 | 86 |
| 89 bool ChunkStream::GetMissedRanges( | 87 bool ChunkStream::GetMissedRanges( |
| 90 size_t offset, size_t size, | 88 size_t offset, |
| 91 std::vector<std::pair<size_t, size_t> >* ranges) const { | 89 size_t size, |
| 90 std::vector<std::pair<size_t, size_t>>* ranges) const { |
| 92 if (IsRangeAvailable(offset, size)) | 91 if (IsRangeAvailable(offset, size)) |
| 93 return false; | 92 return false; |
| 94 | 93 |
| 95 ranges->clear(); | 94 ranges->clear(); |
| 96 if (chunks_.empty()) { | 95 if (chunks_.empty()) { |
| 97 ranges->push_back(std::pair<size_t, size_t>(offset, size)); | 96 ranges->push_back(std::pair<size_t, size_t>(offset, size)); |
| 98 return true; | 97 return true; |
| 99 } | 98 } |
| 100 | 99 |
| 101 std::map<size_t, size_t>::const_iterator start = chunks_.upper_bound(offset); | 100 std::map<size_t, size_t>::const_iterator start = chunks_.upper_bound(offset); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 118 size_t new_size = it->first - cur_offset; | 117 size_t new_size = it->first - cur_offset; |
| 119 ranges->push_back(std::pair<size_t, size_t>(cur_offset, new_size)); | 118 ranges->push_back(std::pair<size_t, size_t>(cur_offset, new_size)); |
| 120 cur_offset = it->first + it->second; | 119 cur_offset = it->first + it->second; |
| 121 } else if (cur_offset < it->first + it->second) { | 120 } else if (cur_offset < it->first + it->second) { |
| 122 cur_offset = it->first + it->second; | 121 cur_offset = it->first + it->second; |
| 123 } | 122 } |
| 124 } | 123 } |
| 125 | 124 |
| 126 // Add last chunk. | 125 // Add last chunk. |
| 127 if (cur_offset < offset + size) | 126 if (cur_offset < offset + size) |
| 128 ranges->push_back(std::pair<size_t, size_t>(cur_offset, | 127 ranges->push_back( |
| 129 offset + size - cur_offset)); | 128 std::pair<size_t, size_t>(cur_offset, offset + size - cur_offset)); |
| 130 | 129 |
| 131 return true; | 130 return true; |
| 132 } | 131 } |
| 133 | 132 |
| 134 bool ChunkStream::IsRangeAvailable(size_t offset, size_t size) const { | 133 bool ChunkStream::IsRangeAvailable(size_t offset, size_t size) const { |
| 135 if (chunks_.empty()) | 134 if (chunks_.empty()) |
| 136 return false; | 135 return false; |
| 137 | 136 |
| 138 if (SIZE_MAX - size < offset) | 137 if (SIZE_MAX - size < offset) |
| 139 return false; | 138 return false; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 166 size_t ChunkStream::GetLastMissingByteInInterval(size_t offset) const { | 165 size_t ChunkStream::GetLastMissingByteInInterval(size_t offset) const { |
| 167 if (chunks_.empty()) | 166 if (chunks_.empty()) |
| 168 return stream_size_ - 1; | 167 return stream_size_ - 1; |
| 169 std::map<size_t, size_t>::const_iterator it = chunks_.upper_bound(offset); | 168 std::map<size_t, size_t>::const_iterator it = chunks_.upper_bound(offset); |
| 170 if (it == chunks_.end()) | 169 if (it == chunks_.end()) |
| 171 return stream_size_ - 1; | 170 return stream_size_ - 1; |
| 172 return it->first - 1; | 171 return it->first - 1; |
| 173 } | 172 } |
| 174 | 173 |
| 175 } // namespace chrome_pdf | 174 } // namespace chrome_pdf |
| OLD | NEW |