Home | History | Annotate | Download | only in fxcrt
      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/fxcrt/cfx_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 
     15 CFX_SeekableMultiStream::CFX_SeekableMultiStream(
     16     const std::vector<CPDF_Stream*>& streams) {
     17   for (CPDF_Stream* pStream : streams) {
     18     m_Data.push_back(pdfium::MakeRetain<CPDF_StreamAcc>(pStream));
     19     m_Data.back()->LoadAllDataFiltered();
     20   }
     21 }
     22 
     23 CFX_SeekableMultiStream::~CFX_SeekableMultiStream() {}
     24 
     25 FX_FILESIZE CFX_SeekableMultiStream::GetSize() {
     26   uint32_t dwSize = 0;
     27   for (const auto& acc : m_Data)
     28     dwSize += acc->GetSize();
     29   return dwSize;
     30 }
     31 
     32 bool CFX_SeekableMultiStream::ReadBlock(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->GetData() + offset, 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 
     62 size_t CFX_SeekableMultiStream::ReadBlock(void* buffer, size_t size) {
     63   NOTREACHED();
     64   return 0;
     65 }
     66 
     67 FX_FILESIZE CFX_SeekableMultiStream::GetPosition() {
     68   return 0;
     69 }
     70 
     71 bool CFX_SeekableMultiStream::IsEOF() {
     72   return false;
     73 }
     74 
     75 bool CFX_SeekableMultiStream::Flush() {
     76   NOTREACHED();
     77   return false;
     78 }
     79 
     80 bool CFX_SeekableMultiStream::WriteBlock(const void* pData,
     81                                          FX_FILESIZE offset,
     82                                          size_t size) {
     83   NOTREACHED();
     84   return false;
     85 }
     86