Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfx_codec_memory.cpp
Go to the documentation of this file.
1// Copyright 2018 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fxcodec/cfx_codec_memory.h"
6
7#include <algorithm>
8
9#include "core/fxcrt/span_util.h"
10#include "core/fxcrt/stl_util.h"
11
12CFX_CodecMemory::CFX_CodecMemory(size_t buffer_size)
13 : buffer_(FX_Alloc(uint8_t, buffer_size)), size_(buffer_size) {}
14
15CFX_CodecMemory::~CFX_CodecMemory() = default;
16
17bool CFX_CodecMemory::Seek(size_t pos) {
18 if (pos > size_)
19 return false;
20
21 pos_ = pos;
22 return true;
23}
24
25size_t CFX_CodecMemory::ReadBlock(pdfium::span<uint8_t> buffer) {
26 if (buffer.empty() || IsEOF())
27 return 0;
28
29 size_t bytes_to_read = std::min(buffer.size(), size_ - pos_);
30 fxcrt::Copy(GetBufferSpan().subspan(pos_, bytes_to_read), buffer);
31 pos_ += bytes_to_read;
32 return bytes_to_read;
33}
34
35bool CFX_CodecMemory::TryResize(size_t new_buffer_size) {
36 uint8_t* pOldBuf = buffer_.release();
37 uint8_t* pNewBuf = FX_TryRealloc(uint8_t, pOldBuf, new_buffer_size);
38 if (new_buffer_size && !pNewBuf) {
39 buffer_.reset(pOldBuf);
40 return false;
41 }
42 buffer_.reset(pNewBuf);
43 size_ = new_buffer_size;
44 return true;
45}
46
47void CFX_CodecMemory::Consume(size_t consumed) {
48 fxcrt::spanmove(GetBufferSpan(), GetBufferSpan().subspan(consumed));
49}
bool TryResize(size_t new_buffer_size)
bool IsEOF() const
~CFX_CodecMemory() override
size_t ReadBlock(pdfium::span< uint8_t > buffer)
bool Seek(size_t pos)
void Consume(size_t consumed)