+ return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
+}
+
+pair<size_t, size_t> QHexView::get_selection() const
+{
+ size_t start = selectBegin_ / 2;
+ size_t end = selectEnd_ / 2;
+
+ if (start == end) {
+ // Nothing is currently selected
+ start = 0;
+ end = data_size_;
+ } if (end < data_size_)
+ end++;
+
+ return std::make_pair(start, end);
+}
+
+size_t QHexView::create_hex_line(size_t start, size_t end, QString* dest,
+ bool with_offset, bool with_ascii)
+{
+ dest->clear();
+
+ // Determine start address for the row
+ uint64_t row = start / BYTES_PER_LINE;
+ uint64_t offset = row * BYTES_PER_LINE;
+ end = std::min((uint64_t)end, offset + BYTES_PER_LINE);
+
+ if (with_offset)
+ dest->append(QString("%1 ").arg(row * BYTES_PER_LINE, 10, 16, QChar('0')).toUpper());
+
+ initialize_byte_iterator(offset);
+ for (size_t i = offset; i < offset + BYTES_PER_LINE; i++) {
+ uint8_t value = 0;
+
+ if (i < end)
+ value = get_next_byte();
+
+ if ((i < start) || (i >= end))
+ dest->append(" ");
+ else
+ dest->append(QString("%1 ").arg(value, 2, 16, QChar('0')).toUpper());
+ }
+
+ if (with_ascii) {
+ initialize_byte_iterator(offset);
+ for (size_t i = offset; i < end; i++) {
+ uint8_t value = get_next_byte();
+
+ if ((value < 0x20) || (value > 0x7E))
+ value = '.';
+
+ if (i < start)
+ dest->append(' ');
+ else
+ dest->append((char)value);
+ }
+ }
+
+ return end;
+}
+
+void QHexView::initialize_byte_iterator(size_t offset)
+{
+ current_chunk_id_ = 0;
+ current_chunk_offset_ = 0;
+ current_offset_ = offset;
+
+ size_t chunks = data_->chunks.size();
+ for (size_t i = 0; i < chunks; i++) {
+ size_t size = data_->chunks[i].data.size();
+
+ if (offset >= size) {
+ current_chunk_id_++;
+ offset -= size;
+ } else {
+ current_chunk_offset_ = offset;
+ break;
+ }
+ }
+
+ if (current_chunk_id_ < data_->chunks.size())
+ current_chunk_ = data_->chunks[current_chunk_id_];
+}
+
+uint8_t QHexView::get_next_byte(bool* is_next_chunk)
+{
+ if (is_next_chunk != nullptr)
+ *is_next_chunk = (current_chunk_offset_ == 0);
+
+ uint8_t v = 0;
+ if (current_chunk_offset_ < current_chunk_.data.size())
+ v = current_chunk_.data[current_chunk_offset_];
+
+ current_offset_++;
+ current_chunk_offset_++;
+
+ if (current_offset_ > data_size_) {
+ qWarning() << "QHexView::get_next_byte() overran binary data boundary:" <<
+ current_offset_ << "of" << data_size_ << "bytes";
+ return 0xEE;
+ }
+
+ if ((current_chunk_offset_ == current_chunk_.data.size()) && (current_offset_ < data_size_)) {
+ current_chunk_id_++;
+ current_chunk_offset_ = 0;
+ current_chunk_ = data_->chunks[current_chunk_id_];
+ }
+
+ return v;