File: chunk_stream.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 3,499 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2010 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PDF_LOADER_CHUNK_STREAM_H_
#define PDF_LOADER_CHUNK_STREAM_H_

#include <stddef.h>
#include <string.h>

#include <algorithm>
#include <array>
#include <memory>
#include <utility>
#include <vector>

#include "base/compiler_specific.h"
#include "pdf/loader/range_set.h"

namespace chrome_pdf {

// This class collects a chunks of data into one data stream. Client can check
// if data in certain range is available, and get missing chunks of data.
template <size_t N>
class ChunkStream {
 public:
  static constexpr size_t kChunkSize = N;
  using ChunkData = typename std::array<unsigned char, N>;

  ChunkStream() {}
  ~ChunkStream() {}

  void SetChunkData(size_t chunk_index, std::unique_ptr<ChunkData> data) {
    if (!data)
      return;

    if (chunk_index >= data_.size())
      data_.resize(chunk_index + 1);

    if (!data_[chunk_index])
      ++filled_chunks_count_;

    data_[chunk_index] = std::move(data);
    filled_chunks_.Union(gfx::Range(chunk_index, chunk_index + 1));
  }

  bool ReadData(const gfx::Range& range, void* buffer) const {
    if (!IsRangeAvailable(range))
      return false;

    unsigned char* data_buffer = static_cast<unsigned char*>(buffer);
    size_t start = range.start();
    while (start != range.end()) {
      const size_t chunk_index = GetChunkIndex(start);
      const size_t chunk_start = start % kChunkSize;
      const size_t len =
          std::min(kChunkSize - chunk_start, range.end() - start);
      UNSAFE_TODO({
        memcpy(data_buffer, data_[chunk_index]->data() + chunk_start, len);
        data_buffer += len;
      });
      start += len;
    }
    return true;
  }

  size_t GetChunkIndex(size_t offset) const { return offset / kChunkSize; }

  gfx::Range GetChunksRange(size_t offset, size_t size) const {
    return gfx::Range(GetChunkIndex(offset), GetChunkEnd(offset + size));
  }

  bool IsRangeAvailable(const gfx::Range& range) const {
    if (!range.IsValid() || range.is_reversed() ||
        (eof_pos_ > 0 && eof_pos_ < range.end())) {
      return false;
    }

    if (range.is_empty())
      return true;

    const gfx::Range chunks_range(GetChunkIndex(range.start()),
                                  GetChunkEnd(range.end()));
    return filled_chunks_.Contains(chunks_range);
  }

  bool IsChunkAvailable(size_t chunk_index) const {
    return filled_chunks_.Contains(chunk_index);
  }

  void set_eof_pos(size_t eof_pos) { eof_pos_ = eof_pos; }
  size_t eof_pos() const { return eof_pos_; }

  const RangeSet& filled_chunks() const { return filled_chunks_; }

  bool IsComplete() const {
    return eof_pos_ > 0 && IsRangeAvailable(gfx::Range(0, eof_pos_));
  }

  bool IsValidChunkIndex(size_t chunk_index) const {
    return !eof_pos_ || (chunk_index <= GetChunkIndex(eof_pos_ - 1));
  }

  void Clear() {
    data_.clear();
    eof_pos_ = 0;
    filled_chunks_.Clear();
    filled_chunks_count_ = 0;
  }

  size_t filled_chunks_count() const { return filled_chunks_count_; }
  size_t total_chunks_count() const { return GetChunkEnd(eof_pos_); }

 private:
  size_t GetChunkEnd(size_t offset) const {
    return (offset + kChunkSize - 1) / kChunkSize;
  }

  std::vector<std::unique_ptr<ChunkData>> data_;
  size_t eof_pos_ = 0;
  RangeSet filled_chunks_;
  size_t filled_chunks_count_ = 0;
};

}  // namespace chrome_pdf

#endif  // PDF_LOADER_CHUNK_STREAM_H_