From d801c9c9caddf3941f6844fab58b19261d7d3cad Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 13 Aug 2018 20:36:16 +0000 Subject: Implement CFX_CodecMemory. This class is much simpler than CFX_MemoryStream and does only what CFX_BmpDecompressor and CFX_GifContext needs. Swap out CFX_MemoryStream and remove CFX_MemoryStream::Seek(). BUG=pdfium:263 Change-Id: Ifd8ce4d2b6c9fedd6ec842d46f54fc8e654fbca7 Reviewed-on: https://pdfium-review.googlesource.com/39880 Commit-Queue: Lei Zhang Reviewed-by: Ryan Harrison --- core/fxcodec/codec/cfx_codec_memory.cpp | 30 ++++++++++++++++++++++++++++++ core/fxcodec/codec/cfx_codec_memory.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 core/fxcodec/codec/cfx_codec_memory.cpp create mode 100644 core/fxcodec/codec/cfx_codec_memory.h (limited to 'core/fxcodec/codec') diff --git a/core/fxcodec/codec/cfx_codec_memory.cpp b/core/fxcodec/codec/cfx_codec_memory.cpp new file mode 100644 index 0000000000..8eac02c9ee --- /dev/null +++ b/core/fxcodec/codec/cfx_codec_memory.cpp @@ -0,0 +1,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 + +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; +} diff --git a/core/fxcodec/codec/cfx_codec_memory.h b/core/fxcodec/codec/cfx_codec_memory.h new file mode 100644 index 0000000000..6eee72a32d --- /dev/null +++ b/core/fxcodec/codec/cfx_codec_memory.h @@ -0,0 +1,33 @@ +// 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. + +#ifndef CORE_FXCODEC_CODEC_CFX_CODEC_MEMORY_H_ +#define CORE_FXCODEC_CODEC_CFX_CODEC_MEMORY_H_ + +#include "core/fxcrt/retain_ptr.h" + +class CFX_CodecMemory : public Retainable { + public: + template + friend RetainPtr pdfium::MakeRetain(Args&&... args); + + uint8_t* GetBuffer() { return buffer_; } + size_t GetSize() const { return size_; } + size_t GetPosition() const { return pos_; } + bool IsEOF() const { return pos_ >= size_; } + size_t ReadBlock(void* buffer, size_t size); + + // Sets the cursor position to |pos| if possible. + bool Seek(size_t pos); + + private: + CFX_CodecMemory(uint8_t* buffer, size_t size); + ~CFX_CodecMemory() override; + + uint8_t* const buffer_; + const size_t size_; + size_t pos_ = 0; +}; + +#endif // CORE_FXCODEC_CODEC_CFX_CODEC_MEMORY_H_ -- cgit v1.2.3