blob: 12e23646d17a33941a3bb23013910711e73521e9 [file] [log] [blame]
Dan Sinclair9d6a2082017-04-12 11:32:32 -04001// 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 "xfa/fxfa/cxfa_fileread.h"
8
9#include <algorithm>
10
11#include "core/fpdfapi/parser/cpdf_stream_acc.h"
Lei Zhangb45324b2017-05-22 17:05:40 -070012#include "third_party/base/logging.h"
Dan Sinclair9d6a2082017-04-12 11:32:32 -040013#include "third_party/base/stl_util.h"
14
15CXFA_FileRead::CXFA_FileRead(const std::vector<CPDF_Stream*>& streams) {
16 for (CPDF_Stream* pStream : streams) {
17 m_Data.push_back(pdfium::MakeRetain<CPDF_StreamAcc>(pStream));
18 m_Data.back()->LoadAllData();
19 }
20}
21
22CXFA_FileRead::~CXFA_FileRead() {}
23
24FX_FILESIZE CXFA_FileRead::GetSize() {
25 uint32_t dwSize = 0;
26 for (const auto& acc : m_Data)
27 dwSize += acc->GetSize();
28 return dwSize;
29}
30
31bool CXFA_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) {
32 int32_t iCount = pdfium::CollectionSize<int32_t>(m_Data);
33 int32_t index = 0;
34 while (index < iCount) {
35 const auto& acc = m_Data[index];
36 FX_FILESIZE dwSize = acc->GetSize();
37 if (offset < dwSize)
38 break;
39
40 offset -= dwSize;
41 index++;
42 }
43 while (index < iCount) {
44 const auto& acc = m_Data[index];
45 uint32_t dwSize = acc->GetSize();
46 size_t dwRead = std::min(size, static_cast<size_t>(dwSize - offset));
47 memcpy(buffer, acc->GetData() + offset, dwRead);
48 size -= dwRead;
49 if (size == 0)
50 return true;
51
52 buffer = static_cast<uint8_t*>(buffer) + dwRead;
53 offset = 0;
54 index++;
55 }
56 return false;
57}
Dan Sinclair4fcdf052017-04-18 11:55:27 -040058
59size_t CXFA_FileRead::ReadBlock(void* buffer, size_t size) {
Lei Zhangb45324b2017-05-22 17:05:40 -070060 NOTREACHED();
Dan Sinclair4fcdf052017-04-18 11:55:27 -040061 return 0;
62}
63
64FX_FILESIZE CXFA_FileRead::GetPosition() {
65 return 0;
66}
67
68bool CXFA_FileRead::IsEOF() {
69 return false;
70}
71
72bool CXFA_FileRead::Flush() {
Lei Zhangb45324b2017-05-22 17:05:40 -070073 NOTREACHED();
Dan Sinclair4fcdf052017-04-18 11:55:27 -040074 return false;
75}
76
77bool CXFA_FileRead::WriteBlock(const void* pData,
78 FX_FILESIZE offset,
79 size_t size) {
Lei Zhangb45324b2017-05-22 17:05:40 -070080 NOTREACHED();
Dan Sinclair4fcdf052017-04-18 11:55:27 -040081 return false;
82}