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