Dan Sinclair | 9d6a208 | 2017-04-12 11:32:32 -0400 | [diff] [blame] | 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 "xfa/fxfa/cxfa_fileread.h" |
| 8 | |
| 9 | #include <algorithm> |
| 10 | |
| 11 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
Lei Zhang | b45324b | 2017-05-22 17:05:40 -0700 | [diff] [blame] | 12 | #include "third_party/base/logging.h" |
Dan Sinclair | 9d6a208 | 2017-04-12 11:32:32 -0400 | [diff] [blame] | 13 | #include "third_party/base/stl_util.h" |
| 14 | |
| 15 | CXFA_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 | |
| 22 | CXFA_FileRead::~CXFA_FileRead() {} |
| 23 | |
| 24 | FX_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 | |
| 31 | bool 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 Sinclair | 4fcdf05 | 2017-04-18 11:55:27 -0400 | [diff] [blame] | 58 | |
| 59 | size_t CXFA_FileRead::ReadBlock(void* buffer, size_t size) { |
Lei Zhang | b45324b | 2017-05-22 17:05:40 -0700 | [diff] [blame] | 60 | NOTREACHED(); |
Dan Sinclair | 4fcdf05 | 2017-04-18 11:55:27 -0400 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | FX_FILESIZE CXFA_FileRead::GetPosition() { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | bool CXFA_FileRead::IsEOF() { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | bool CXFA_FileRead::Flush() { |
Lei Zhang | b45324b | 2017-05-22 17:05:40 -0700 | [diff] [blame] | 73 | NOTREACHED(); |
Dan Sinclair | 4fcdf05 | 2017-04-18 11:55:27 -0400 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
| 77 | bool CXFA_FileRead::WriteBlock(const void* pData, |
| 78 | FX_FILESIZE offset, |
| 79 | size_t size) { |
Lei Zhang | b45324b | 2017-05-22 17:05:40 -0700 | [diff] [blame] | 80 | NOTREACHED(); |
Dan Sinclair | 4fcdf05 | 2017-04-18 11:55:27 -0400 | [diff] [blame] | 81 | return false; |
| 82 | } |