1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/parser/cpdf_seekablemultistream.h"
8
9 #include <algorithm>
10
11 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
12 #include "third_party/base/logging.h"
13 #include "third_party/base/stl_util.h"
14
CPDF_SeekableMultiStream(const std::vector<const CPDF_Stream * > & streams)15 CPDF_SeekableMultiStream::CPDF_SeekableMultiStream(
16 const std::vector<const CPDF_Stream*>& streams) {
17 for (const CPDF_Stream* pStream : streams) {
18 m_Data.push_back(pdfium::MakeRetain<CPDF_StreamAcc>(pStream));
19 m_Data.back()->LoadAllDataFiltered();
20 }
21 }
22
~CPDF_SeekableMultiStream()23 CPDF_SeekableMultiStream::~CPDF_SeekableMultiStream() {}
24
GetSize()25 FX_FILESIZE CPDF_SeekableMultiStream::GetSize() {
26 uint32_t dwSize = 0;
27 for (const auto& acc : m_Data)
28 dwSize += acc->GetSize();
29 return dwSize;
30 }
31
ReadBlockAtOffset(void * buffer,FX_FILESIZE offset,size_t size)32 bool CPDF_SeekableMultiStream::ReadBlockAtOffset(void* buffer,
33 FX_FILESIZE offset,
34 size_t size) {
35 int32_t iCount = pdfium::CollectionSize<int32_t>(m_Data);
36 int32_t index = 0;
37 while (index < iCount) {
38 const auto& acc = m_Data[index];
39 FX_FILESIZE dwSize = acc->GetSize();
40 if (offset < dwSize)
41 break;
42
43 offset -= dwSize;
44 index++;
45 }
46 while (index < iCount) {
47 const auto& acc = m_Data[index];
48 uint32_t dwSize = acc->GetSize();
49 size_t dwRead = std::min(size, static_cast<size_t>(dwSize - offset));
50 memcpy(buffer, acc->GetSpan().subspan(offset, dwRead).data(), dwRead);
51 size -= dwRead;
52 if (size == 0)
53 return true;
54
55 buffer = static_cast<uint8_t*>(buffer) + dwRead;
56 offset = 0;
57 index++;
58 }
59 return false;
60 }
61
ReadBlock(void * buffer,size_t size)62 size_t CPDF_SeekableMultiStream::ReadBlock(void* buffer, size_t size) {
63 NOTREACHED();
64 return 0;
65 }
66
GetPosition()67 FX_FILESIZE CPDF_SeekableMultiStream::GetPosition() {
68 return 0;
69 }
70
IsEOF()71 bool CPDF_SeekableMultiStream::IsEOF() {
72 return false;
73 }
74
Flush()75 bool CPDF_SeekableMultiStream::Flush() {
76 NOTREACHED();
77 return false;
78 }
79
WriteBlockAtOffset(const void * pData,FX_FILESIZE offset,size_t size)80 bool CPDF_SeekableMultiStream::WriteBlockAtOffset(const void* pData,
81 FX_FILESIZE offset,
82 size_t size) {
83 NOTREACHED();
84 return false;
85 }
86