1 // Copyright 2016 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 #ifndef CORE_FPDFAPI_PARSER_FPDF_PARSER_DECODE_H_ 8 #define CORE_FPDFAPI_PARSER_FPDF_PARSER_DECODE_H_ 9 10 #include <stdint.h> 11 12 #include <array> 13 #include <memory> 14 #include <optional> 15 #include <utility> 16 #include <vector> 17 18 #include "core/fxcodec/data_and_bytes_consumed.h" 19 #include "core/fxcrt/data_vector.h" 20 #include "core/fxcrt/fx_string.h" 21 #include "core/fxcrt/retain_ptr.h" 22 #include "core/fxcrt/span.h" 23 24 class CPDF_Array; 25 class CPDF_Dictionary; 26 class CPDF_Object; 27 28 namespace fxcodec { 29 class ScanlineDecoder; 30 } 31 32 // Indexed by 8-bit char code, contains unicode code points. 33 extern const std::array<uint16_t, 256> kPDFDocEncoding; 34 35 bool ValidateDecoderPipeline(const CPDF_Array* pDecoders); 36 37 ByteString PDF_EncodeString(ByteStringView src); 38 ByteString PDF_HexEncodeString(ByteStringView src); 39 WideString PDF_DecodeText(pdfium::span<const uint8_t> span); 40 ByteString PDF_EncodeText(WideStringView str); 41 42 std::unique_ptr<fxcodec::ScanlineDecoder> CreateFaxDecoder( 43 pdfium::span<const uint8_t> src_span, 44 int width, 45 int height, 46 const CPDF_Dictionary* pParams); 47 48 std::unique_ptr<fxcodec::ScanlineDecoder> CreateFlateDecoder( 49 pdfium::span<const uint8_t> src_span, 50 int width, 51 int height, 52 int nComps, 53 int bpc, 54 const CPDF_Dictionary* pParams); 55 56 fxcodec::DataAndBytesConsumed RunLengthDecode( 57 pdfium::span<const uint8_t> src_span); 58 59 fxcodec::DataAndBytesConsumed A85Decode(pdfium::span<const uint8_t> src_span); 60 61 fxcodec::DataAndBytesConsumed HexDecode(pdfium::span<const uint8_t> src_span); 62 63 fxcodec::DataAndBytesConsumed FlateOrLZWDecode( 64 bool use_lzw, 65 pdfium::span<const uint8_t> src_span, 66 const CPDF_Dictionary* pParams, 67 uint32_t estimated_size); 68 69 // Returns std::nullopt if the filter in |pDict| is the wrong type or an 70 // invalid decoder pipeline. 71 // Returns an empty vector if there is no filter, or if the filter is an empty 72 // array. 73 // Otherwise, returns a vector of decoders. 74 using DecoderArray = 75 std::vector<std::pair<ByteString, RetainPtr<const CPDF_Object>>>; 76 std::optional<DecoderArray> GetDecoderArray( 77 RetainPtr<const CPDF_Dictionary> pDict); 78 79 struct PDFDataDecodeResult { 80 PDFDataDecodeResult(); 81 PDFDataDecodeResult(DataVector<uint8_t> data, 82 ByteString image_encoding, 83 RetainPtr<const CPDF_Dictionary> image_params); 84 PDFDataDecodeResult(PDFDataDecodeResult&& that) noexcept; 85 PDFDataDecodeResult& operator=(PDFDataDecodeResult&& that) noexcept; 86 ~PDFDataDecodeResult(); 87 88 DataVector<uint8_t> data; 89 ByteString image_encoding; 90 RetainPtr<const CPDF_Dictionary> image_params; 91 }; 92 93 std::optional<PDFDataDecodeResult> PDF_DataDecode( 94 pdfium::span<const uint8_t> src_span, 95 uint32_t estimated_size, 96 bool bImageAcc, 97 const DecoderArray& decoder_array); 98 99 #endif // CORE_FPDFAPI_PARSER_FPDF_PARSER_DECODE_H_ 100