summaryrefslogtreecommitdiff
path: root/core/fxcodec/codec/cfx_codec_memory.cpp
blob: 640db12415ee7ed4879e0c34099b75cc34ab00d3 (plain)
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
// Copyright 2018 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/fxcodec/codec/cfx_codec_memory.h"

#include <algorithm>

CFX_CodecMemory::CFX_CodecMemory(size_t buffer_size)
    : buffer_(FX_Alloc(uint8_t, buffer_size)), size_(buffer_size) {}

CFX_CodecMemory::~CFX_CodecMemory() = default;

bool CFX_CodecMemory::Seek(size_t pos) {
  if (pos > size_)
    return false;

  pos_ = pos;
  return true;
}

size_t CFX_CodecMemory::ReadBlock(void* buffer, size_t size) {
  if (!buffer || !size || IsEOF())
    return 0;

  size_t bytes_to_read = std::min(size, size_ - pos_);
  memcpy(buffer, buffer_.get() + pos_, bytes_to_read);
  pos_ += bytes_to_read;
  return bytes_to_read;
}

bool CFX_CodecMemory::TryResize(size_t new_buffer_size) {
  uint8_t* pOldBuf = buffer_.release();
  uint8_t* pNewBuf = FX_TryRealloc(uint8_t, pOldBuf, new_buffer_size);
  if (!pNewBuf) {
    buffer_.reset(pOldBuf);
    return false;
  }
  buffer_.reset(pNewBuf);
  size_ = new_buffer_size;
  return true;
}

void CFX_CodecMemory::Consume(size_t consumed) {
  size_t unconsumed = size_ - consumed;
  memmove(buffer_.get(), buffer_.get() + consumed, unconsumed);
}