summaryrefslogtreecommitdiff
path: root/core/fxcodec/codec/cfx_codec_memory.cpp
blob: 8eac02c9eef56b0e49502c8542c82a611ec74565 (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
// 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(uint8_t* buffer, size_t size)
    : buffer_(buffer), size_(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_[pos_], bytes_to_read);
  pos_ += bytes_to_read;
  return bytes_to_read;
}