xref: /aosp_15_r20/external/pdfium/core/fxcrt/cfx_read_only_span_stream.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2022 The PDFium Authors
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_read_only_span_stream.h"
8 
9 #include "core/fxcrt/fx_safe_types.h"
10 #include "core/fxcrt/span_util.h"
11 #include "third_party/base/numerics/safe_conversions.h"
12 
CFX_ReadOnlySpanStream(pdfium::span<const uint8_t> span)13 CFX_ReadOnlySpanStream::CFX_ReadOnlySpanStream(pdfium::span<const uint8_t> span)
14     : span_(span) {}
15 
16 CFX_ReadOnlySpanStream::~CFX_ReadOnlySpanStream() = default;
17 
GetSize()18 FX_FILESIZE CFX_ReadOnlySpanStream::GetSize() {
19   return pdfium::base::checked_cast<FX_FILESIZE>(span_.size());
20 }
21 
ReadBlockAtOffset(pdfium::span<uint8_t> buffer,FX_FILESIZE offset)22 bool CFX_ReadOnlySpanStream::ReadBlockAtOffset(pdfium::span<uint8_t> buffer,
23                                                FX_FILESIZE offset) {
24   if (buffer.empty() || offset < 0)
25     return false;
26 
27   FX_SAFE_SIZE_T pos = buffer.size();
28   pos += offset;
29   if (!pos.IsValid() || pos.ValueOrDie() > span_.size())
30     return false;
31 
32   fxcrt::spancpy(
33       buffer,
34       span_.subspan(pdfium::base::checked_cast<size_t>(offset), buffer.size()));
35   return true;
36 }
37