npm | 014b012 | 2016-11-07 08:42:11 -0800 | [diff] [blame] | 1 | // Copyright 2016 PDFium Authors. All rights reserved. |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | 95e854f | 2015-06-13 00:58:06 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
tsepez | 1aff265 | 2016-11-22 10:45:59 -0800 | [diff] [blame] | 7 | #include "core/fpdfapi/page/cpdf_streamparser.h" |
Lei Zhang | a9fa50f | 2015-11-10 09:45:32 -0800 | [diff] [blame] | 8 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | #include <limits.h> |
Lei Zhang | 3789056 | 2015-09-25 22:51:39 -0700 | [diff] [blame] | 10 | |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 11 | #include <algorithm> |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 12 | #include <memory> |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 13 | #include <sstream> |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 16 | #include "constants/stream_dict_common.h" |
dsinclair | 39c62fd | 2016-09-29 12:49:17 -0700 | [diff] [blame] | 17 | #include "core/fpdfapi/cpdf_modulemgr.h" |
tsepez | 9fa95ef | 2016-10-11 12:29:38 -0700 | [diff] [blame] | 18 | #include "core/fpdfapi/page/cpdf_docpagedata.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 19 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_boolean.h" |
| 21 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 22 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 23 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 24 | #include "core/fpdfapi/parser/cpdf_null.h" |
| 25 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 26 | #include "core/fpdfapi/parser/cpdf_stream.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 27 | #include "core/fpdfapi/parser/cpdf_string.h" |
| 28 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
| 29 | #include "core/fpdfapi/parser/fpdf_parser_utility.h" |
Lei Zhang | 7acd826 | 2017-09-19 14:34:37 -0700 | [diff] [blame] | 30 | #include "core/fxcodec/codec/ccodec_jpegmodule.h" |
Lei Zhang | 4b8d363 | 2017-09-19 14:18:48 -0700 | [diff] [blame] | 31 | #include "core/fxcodec/codec/ccodec_scanlinedecoder.h" |
Dan Sinclair | cfb1944 | 2017-04-20 13:13:04 -0400 | [diff] [blame] | 32 | #include "core/fxcrt/fx_extension.h" |
Lei Zhang | a9fa50f | 2015-11-10 09:45:32 -0800 | [diff] [blame] | 33 | |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 36 | const uint32_t kMaxNestedParsingLevel = 512; |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 37 | const size_t kMaxStringLength = 32767; |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 38 | |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 39 | uint32_t DecodeAllScanlines(std::unique_ptr<CCodec_ScanlineDecoder> pDecoder, |
Nicolas Pena | 995a601 | 2017-06-20 17:42:39 -0400 | [diff] [blame] | 40 | uint8_t** dest_buf, |
| 41 | uint32_t* dest_size) { |
npm | 014b012 | 2016-11-07 08:42:11 -0800 | [diff] [blame] | 42 | if (!pDecoder) |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 43 | return FX_INVALID_OFFSET; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 44 | int ncomps = pDecoder->CountComps(); |
| 45 | int bpc = pDecoder->GetBPC(); |
| 46 | int width = pDecoder->GetWidth(); |
| 47 | int height = pDecoder->GetHeight(); |
| 48 | int pitch = (width * ncomps * bpc + 7) / 8; |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 49 | if (height == 0 || pitch > (1 << 30) / height) |
Wei Li | 8940993 | 2016-03-28 10:33:33 -0700 | [diff] [blame] | 50 | return FX_INVALID_OFFSET; |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 51 | |
Nicolas Pena | 995a601 | 2017-06-20 17:42:39 -0400 | [diff] [blame] | 52 | *dest_buf = FX_Alloc2D(uint8_t, pitch, height); |
| 53 | *dest_size = pitch * height; // Safe since checked alloc returned. |
| 54 | for (int row = 0; row < height; ++row) { |
Lei Zhang | e95f39f | 2015-10-01 14:08:32 -0700 | [diff] [blame] | 55 | const uint8_t* pLine = pDecoder->GetScanline(row); |
| 56 | if (!pLine) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | break; |
Lei Zhang | e95f39f | 2015-10-01 14:08:32 -0700 | [diff] [blame] | 58 | |
Nicolas Pena | 995a601 | 2017-06-20 17:42:39 -0400 | [diff] [blame] | 59 | memcpy(*dest_buf + row * pitch, pLine, pitch); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 60 | } |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 61 | return pDecoder->GetSrcOffset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 62 | } |
Wei Li | dc4d504 | 2016-01-19 13:19:44 -0800 | [diff] [blame] | 63 | |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 64 | uint32_t DecodeInlineStream(const uint8_t* src_buf, |
| 65 | uint32_t limit, |
| 66 | int width, |
| 67 | int height, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 68 | const ByteString& decoder, |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 69 | CPDF_Dictionary* pParam, |
| 70 | uint8_t** dest_buf, |
| 71 | uint32_t* dest_size) { |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 72 | if (decoder == "CCITTFaxDecode" || decoder == "CCF") { |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 73 | std::unique_ptr<CCodec_ScanlineDecoder> pDecoder = |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 74 | FPDFAPI_CreateFaxDecoder(src_buf, limit, width, height, pParam); |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 75 | return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | } |
npm | 014b012 | 2016-11-07 08:42:11 -0800 | [diff] [blame] | 77 | if (decoder == "ASCII85Decode" || decoder == "A85") |
Dan Sinclair | 4d6f03a | 2015-11-02 16:12:41 -0500 | [diff] [blame] | 78 | return A85Decode(src_buf, limit, dest_buf, dest_size); |
npm | 014b012 | 2016-11-07 08:42:11 -0800 | [diff] [blame] | 79 | if (decoder == "ASCIIHexDecode" || decoder == "AHx") |
Dan Sinclair | 4d6f03a | 2015-11-02 16:12:41 -0500 | [diff] [blame] | 80 | return HexDecode(src_buf, limit, dest_buf, dest_size); |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 81 | if (decoder == "FlateDecode" || decoder == "Fl") { |
Nicolas Pena | 995a601 | 2017-06-20 17:42:39 -0400 | [diff] [blame] | 82 | return FPDFAPI_FlateOrLZWDecode(false, src_buf, limit, pParam, *dest_size, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | dest_buf, dest_size); |
| 84 | } |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 85 | if (decoder == "LZWDecode" || decoder == "LZW") { |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 86 | return FPDFAPI_FlateOrLZWDecode(true, src_buf, limit, pParam, 0, dest_buf, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | dest_size); |
| 88 | } |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 89 | if (decoder == "DCTDecode" || decoder == "DCT") { |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 90 | std::unique_ptr<CCodec_ScanlineDecoder> pDecoder = |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | CPDF_ModuleMgr::Get()->GetJpegModule()->CreateDecoder( |
| 92 | src_buf, limit, width, height, 0, |
tsepez | 16b703c | 2016-10-26 08:45:31 -0700 | [diff] [blame] | 93 | !pParam || pParam->GetIntegerFor("ColorTransform", 1)); |
tsepez | 974b4a6 | 2016-12-13 12:48:13 -0800 | [diff] [blame] | 94 | return DecodeAllScanlines(std::move(pDecoder), dest_buf, dest_size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | } |
npm | 014b012 | 2016-11-07 08:42:11 -0800 | [diff] [blame] | 96 | if (decoder == "RunLengthDecode" || decoder == "RL") |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | return RunLengthDecode(src_buf, limit, dest_buf, dest_size); |
Nicolas Pena | 995a601 | 2017-06-20 17:42:39 -0400 | [diff] [blame] | 98 | *dest_size = 0; |
| 99 | *dest_buf = 0; |
Lei Zhang | a12159b | 2017-03-15 13:26:37 -0700 | [diff] [blame] | 100 | return 0xFFFFFFFF; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 101 | } |
Wei Li | dc4d504 | 2016-01-19 13:19:44 -0800 | [diff] [blame] | 102 | |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 103 | } // namespace |
| 104 | |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 105 | CPDF_StreamParser::CPDF_StreamParser(pdfium::span<const uint8_t> span) |
| 106 | : m_pBuf(span) {} |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 107 | |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 108 | CPDF_StreamParser::CPDF_StreamParser(pdfium::span<const uint8_t> span, |
Dan Sinclair | b872a93 | 2017-09-21 17:05:15 -0400 | [diff] [blame] | 109 | const WeakPtr<ByteStringPool>& pPool) |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 110 | : m_pPool(pPool), m_pBuf(span) {} |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 111 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 112 | CPDF_StreamParser::~CPDF_StreamParser() {} |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 113 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 114 | std::unique_ptr<CPDF_Stream> CPDF_StreamParser::ReadInlineStream( |
| 115 | CPDF_Document* pDoc, |
tsepez | 12ff1eb | 2016-11-21 17:47:29 -0800 | [diff] [blame] | 116 | std::unique_ptr<CPDF_Dictionary> pDict, |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 117 | CPDF_Object* pCSObj) { |
Tom Sepez | 5b1fd5c | 2018-04-11 18:09:30 +0000 | [diff] [blame] | 118 | if (m_Pos < m_pBuf.size() && PDFCharIsWhitespace(m_pBuf[m_Pos])) |
| 119 | m_Pos++; |
| 120 | |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 121 | if (m_Pos == m_pBuf.size()) |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 122 | return nullptr; |
| 123 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 124 | ByteString Decoder; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 125 | CPDF_Dictionary* pParam = nullptr; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 126 | CPDF_Object* pFilter = pDict->GetDirectObjectFor("Filter"); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 127 | if (pFilter) { |
| 128 | if (CPDF_Array* pArray = pFilter->AsArray()) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 129 | Decoder = pArray->GetStringAt(0); |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 130 | CPDF_Array* pParams = pDict->GetArrayFor(pdfium::stream::kDecodeParms); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 131 | if (pParams) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 132 | pParam = pParams->GetDictAt(0); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 133 | } else { |
| 134 | Decoder = pFilter->GetString(); |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame^] | 135 | pParam = pDict->GetDictFor(pdfium::stream::kDecodeParms); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 136 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | } |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 138 | uint32_t width = pDict->GetIntegerFor("Width"); |
| 139 | uint32_t height = pDict->GetIntegerFor("Height"); |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 140 | uint32_t OrigSize = 0; |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 141 | if (pCSObj) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 142 | uint32_t bpc = pDict->GetIntegerFor("BitsPerComponent"); |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 143 | uint32_t nComponents = 1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 144 | CPDF_ColorSpace* pCS = pDoc->LoadColorSpace(pCSObj); |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 145 | if (pCS) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | nComponents = pCS->CountComponents(); |
| 147 | pDoc->GetPageData()->ReleaseColorSpace(pCSObj); |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 148 | } else { |
| 149 | nComponents = 3; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 150 | } |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 151 | uint32_t pitch = width; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 152 | if (bpc && pitch > INT_MAX / bpc) |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 153 | return nullptr; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 154 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 155 | pitch *= bpc; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 156 | if (nComponents && pitch > INT_MAX / nComponents) |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 157 | return nullptr; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 158 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | pitch *= nComponents; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 160 | if (pitch > INT_MAX - 7) |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 161 | return nullptr; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 162 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | pitch += 7; |
| 164 | pitch /= 8; |
| 165 | OrigSize = pitch; |
| 166 | } else { |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 167 | if (width > INT_MAX - 7) |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 168 | return nullptr; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 169 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 170 | OrigSize = ((width + 7) / 8); |
| 171 | } |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 172 | if (height && OrigSize > INT_MAX / height) |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 173 | return nullptr; |
thestig | 823df2a | 2016-07-29 06:15:39 -0700 | [diff] [blame] | 174 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | OrigSize *= height; |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 176 | std::unique_ptr<uint8_t, FxFreeDeleter> pData; |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 177 | uint32_t dwStreamSize; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | if (Decoder.IsEmpty()) { |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 179 | if (OrigSize > m_pBuf.size() - m_Pos) |
| 180 | OrigSize = m_pBuf.size() - m_Pos; |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 181 | pData.reset(FX_Alloc(uint8_t, OrigSize)); |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 182 | memcpy(pData.get(), &m_pBuf[m_Pos], OrigSize); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | dwStreamSize = OrigSize; |
| 184 | m_Pos += OrigSize; |
| 185 | } else { |
tsepez | 04a4070 | 2016-12-19 16:09:57 -0800 | [diff] [blame] | 186 | uint8_t* pIgnore = nullptr; |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 187 | uint32_t dwDestSize = OrigSize; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 188 | dwStreamSize = |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 189 | DecodeInlineStream(&m_pBuf[m_Pos], m_pBuf.size() - m_Pos, width, height, |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 190 | Decoder, pParam, &pIgnore, &dwDestSize); |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 191 | FX_Free(pIgnore); |
npm | 014b012 | 2016-11-07 08:42:11 -0800 | [diff] [blame] | 192 | if (static_cast<int>(dwStreamSize) < 0) |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 193 | return nullptr; |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 194 | |
| 195 | uint32_t dwSavePos = m_Pos; |
| 196 | m_Pos += dwStreamSize; |
| 197 | while (1) { |
| 198 | uint32_t dwPrevPos = m_Pos; |
| 199 | CPDF_StreamParser::SyntaxType type = ParseNextElement(); |
| 200 | if (type == CPDF_StreamParser::EndOfData) |
| 201 | break; |
| 202 | |
| 203 | if (type != CPDF_StreamParser::Keyword) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | dwStreamSize += m_Pos - dwPrevPos; |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 205 | continue; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | } |
tsepez | 2763fba | 2017-01-24 10:50:20 -0800 | [diff] [blame] | 207 | if (GetWord() == "EI") { |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 208 | m_Pos = dwPrevPos; |
| 209 | break; |
| 210 | } |
| 211 | dwStreamSize += m_Pos - dwPrevPos; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 212 | } |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 213 | m_Pos = dwSavePos; |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 214 | pData.reset(FX_Alloc(uint8_t, dwStreamSize)); |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 215 | memcpy(pData.get(), &m_pBuf[m_Pos], dwStreamSize); |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 216 | m_Pos += dwStreamSize; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | } |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 218 | pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(dwStreamSize)); |
tsepez | 47fb8c0 | 2016-12-15 13:51:34 -0800 | [diff] [blame] | 219 | return pdfium::MakeUnique<CPDF_Stream>(std::move(pData), dwStreamSize, |
| 220 | std::move(pDict)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 221 | } |
Lei Zhang | 61aad4f | 2015-11-10 10:28:56 -0800 | [diff] [blame] | 222 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 223 | CPDF_StreamParser::SyntaxType CPDF_StreamParser::ParseNextElement() { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 224 | m_pLastObj.reset(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 225 | m_WordSize = 0; |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 226 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | return EndOfData; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 228 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 229 | int ch = m_pBuf[m_Pos++]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 230 | while (1) { |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 231 | while (PDFCharIsWhitespace(ch)) { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 232 | if (!PositionIsInBounds()) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 233 | return EndOfData; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 234 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 235 | ch = m_pBuf[m_Pos++]; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 236 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 237 | |
| 238 | if (ch != '%') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 239 | break; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 240 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 241 | while (1) { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 242 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 243 | return EndOfData; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 244 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | ch = m_pBuf[m_Pos++]; |
Dan Sinclair | 806af3a | 2015-10-28 17:24:07 -0400 | [diff] [blame] | 246 | if (PDFCharIsLineEnding(ch)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 249 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 250 | |
| 251 | if (PDFCharIsDelimiter(ch) && ch != '/') { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | m_Pos--; |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 253 | m_pLastObj = ReadNextObject(false, false, 0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 254 | return Others; |
| 255 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 256 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 257 | bool bIsNumber = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | while (1) { |
Tom Sepez | e96e6fd | 2018-04-03 15:02:37 +0000 | [diff] [blame] | 259 | if (m_WordSize < kMaxWordLength) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 260 | m_WordBuffer[m_WordSize++] = ch; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 261 | |
| 262 | if (!PDFCharIsNumeric(ch)) |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 263 | bIsNumber = false; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 264 | |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 265 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 266 | break; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 267 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | ch = m_pBuf[m_Pos++]; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 269 | |
| 270 | if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | m_Pos--; |
| 272 | break; |
| 273 | } |
| 274 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 275 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 276 | m_WordBuffer[m_WordSize] = 0; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 277 | if (bIsNumber) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | return Number; |
Tom Sepez | 282cee1 | 2016-03-24 10:43:11 -0700 | [diff] [blame] | 279 | |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 280 | if (m_WordBuffer[0] == '/') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | return Name; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 282 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | if (m_WordSize == 4) { |
Tom Sepez | 282cee1 | 2016-03-24 10:43:11 -0700 | [diff] [blame] | 284 | if (memcmp(m_WordBuffer, "true", 4) == 0) { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 285 | m_pLastObj = pdfium::MakeUnique<CPDF_Boolean>(true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 286 | return Others; |
| 287 | } |
Tom Sepez | 282cee1 | 2016-03-24 10:43:11 -0700 | [diff] [blame] | 288 | if (memcmp(m_WordBuffer, "null", 4) == 0) { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 289 | m_pLastObj = pdfium::MakeUnique<CPDF_Null>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 290 | return Others; |
| 291 | } |
| 292 | } else if (m_WordSize == 5) { |
Tom Sepez | 282cee1 | 2016-03-24 10:43:11 -0700 | [diff] [blame] | 293 | if (memcmp(m_WordBuffer, "false", 5) == 0) { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 294 | m_pLastObj = pdfium::MakeUnique<CPDF_Boolean>(false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 295 | return Others; |
| 296 | } |
| 297 | } |
| 298 | return Keyword; |
| 299 | } |
Dan Sinclair | 8018960 | 2015-10-28 14:24:14 -0400 | [diff] [blame] | 300 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 301 | std::unique_ptr<CPDF_Object> CPDF_StreamParser::ReadNextObject( |
| 302 | bool bAllowNestedArray, |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 303 | bool bInArray, |
| 304 | uint32_t dwRecursionLevel) { |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 305 | bool bIsNumber; |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 306 | // Must get the next word before returning to avoid infinite loops. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 307 | GetNextWord(bIsNumber); |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 308 | if (!m_WordSize || dwRecursionLevel > kMaxNestedParsingLevel) |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 309 | return nullptr; |
| 310 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | if (bIsNumber) { |
| 312 | m_WordBuffer[m_WordSize] = 0; |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 313 | return pdfium::MakeUnique<CPDF_Number>( |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 314 | ByteStringView(m_WordBuffer, m_WordSize)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | } |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 316 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | int first_char = m_WordBuffer[0]; |
| 318 | if (first_char == '/') { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 319 | ByteString name = |
| 320 | PDF_NameDecode(ByteStringView(m_WordBuffer + 1, m_WordSize - 1)); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 321 | return pdfium::MakeUnique<CPDF_Name>(m_pPool, name); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | } |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 323 | |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 324 | if (first_char == '(') { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 325 | ByteString str = ReadString(); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 326 | return pdfium::MakeUnique<CPDF_String>(m_pPool, str, false); |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 327 | } |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 328 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 329 | if (first_char == '<') { |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 330 | if (m_WordSize == 1) |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 331 | return pdfium::MakeUnique<CPDF_String>(m_pPool, ReadHexString(), true); |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 332 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 333 | auto pDict = pdfium::MakeUnique<CPDF_Dictionary>(m_pPool); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 334 | while (1) { |
| 335 | GetNextWord(bIsNumber); |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 336 | if (m_WordSize == 2 && m_WordBuffer[0] == '>') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 337 | break; |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 338 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 339 | if (!m_WordSize || m_WordBuffer[0] != '/') |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 340 | return nullptr; |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 341 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 342 | ByteString key = |
| 343 | PDF_NameDecode(ByteStringView(m_WordBuffer + 1, m_WordSize - 1)); |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 344 | std::unique_ptr<CPDF_Object> pObj = |
| 345 | ReadNextObject(true, bInArray, dwRecursionLevel + 1); |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 346 | if (!pObj) |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 347 | return nullptr; |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 348 | |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 349 | if (!key.IsEmpty()) |
| 350 | pDict->SetFor(key, std::move(pObj)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 351 | } |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 352 | return std::move(pDict); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 353 | } |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 354 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | if (first_char == '[') { |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 356 | if ((!bAllowNestedArray && bInArray)) |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 357 | return nullptr; |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 358 | |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 359 | auto pArray = pdfium::MakeUnique<CPDF_Array>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 360 | while (1) { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 361 | std::unique_ptr<CPDF_Object> pObj = |
Nicolas Pena | 6438c4f | 2017-01-27 10:05:36 -0500 | [diff] [blame] | 362 | ReadNextObject(bAllowNestedArray, true, dwRecursionLevel + 1); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 363 | if (pObj) { |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 364 | pArray->Add(std::move(pObj)); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 365 | continue; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 366 | } |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 367 | if (!m_WordSize || m_WordBuffer[0] == ']') |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 368 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 369 | } |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 370 | return std::move(pArray); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | } |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 372 | |
| 373 | if (m_WordSize == 5 && !memcmp(m_WordBuffer, "false", 5)) |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 374 | return pdfium::MakeUnique<CPDF_Boolean>(false); |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 375 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | if (m_WordSize == 4) { |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 377 | if (memcmp(m_WordBuffer, "true", 4) == 0) |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 378 | return pdfium::MakeUnique<CPDF_Boolean>(true); |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 379 | if (memcmp(m_WordBuffer, "null", 4) == 0) |
tsepez | 06104a8 | 2016-11-21 16:22:10 -0800 | [diff] [blame] | 380 | return pdfium::MakeUnique<CPDF_Null>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 381 | } |
thestig | 8e957ba | 2016-04-28 14:45:28 -0700 | [diff] [blame] | 382 | |
| 383 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 384 | } |
Wei Li | dc4d504 | 2016-01-19 13:19:44 -0800 | [diff] [blame] | 385 | |
npm | 014b012 | 2016-11-07 08:42:11 -0800 | [diff] [blame] | 386 | // TODO(npm): the following methods are almost identical in cpdf_syntaxparser |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 387 | void CPDF_StreamParser::GetNextWord(bool& bIsNumber) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 388 | m_WordSize = 0; |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 389 | bIsNumber = true; |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 390 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | return; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 392 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 393 | int ch = m_pBuf[m_Pos++]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 394 | while (1) { |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 395 | while (PDFCharIsWhitespace(ch)) { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 396 | if (!PositionIsInBounds()) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 398 | } |
| 399 | ch = m_pBuf[m_Pos++]; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 400 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 401 | |
| 402 | if (ch != '%') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 403 | break; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 404 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 405 | while (1) { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 406 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 407 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 408 | ch = m_pBuf[m_Pos++]; |
Dan Sinclair | 806af3a | 2015-10-28 17:24:07 -0400 | [diff] [blame] | 409 | if (PDFCharIsLineEnding(ch)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 411 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 413 | |
| 414 | if (PDFCharIsDelimiter(ch)) { |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 415 | bIsNumber = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | m_WordBuffer[m_WordSize++] = ch; |
| 417 | if (ch == '/') { |
| 418 | while (1) { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 419 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 420 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | ch = m_pBuf[m_Pos++]; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 422 | if (!PDFCharIsOther(ch) && !PDFCharIsNumeric(ch)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 423 | m_Pos--; |
| 424 | return; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 425 | } |
Tom Sepez | e96e6fd | 2018-04-03 15:02:37 +0000 | [diff] [blame] | 426 | if (m_WordSize < kMaxWordLength) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | m_WordBuffer[m_WordSize++] = ch; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 428 | } |
| 429 | } else if (ch == '<') { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 430 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 431 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 432 | ch = m_pBuf[m_Pos++]; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 433 | if (ch == '<') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 434 | m_WordBuffer[m_WordSize++] = ch; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 435 | else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | m_Pos--; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 437 | } else if (ch == '>') { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 438 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 440 | ch = m_pBuf[m_Pos++]; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 441 | if (ch == '>') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 442 | m_WordBuffer[m_WordSize++] = ch; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 443 | else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 444 | m_Pos--; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 445 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 448 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 449 | while (1) { |
Tom Sepez | e96e6fd | 2018-04-03 15:02:37 +0000 | [diff] [blame] | 450 | if (m_WordSize < kMaxWordLength) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 451 | m_WordBuffer[m_WordSize++] = ch; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 452 | if (!PDFCharIsNumeric(ch)) |
npm | e883afc | 2016-10-05 13:43:25 -0700 | [diff] [blame] | 453 | bIsNumber = false; |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 454 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 455 | return; |
Tom Sepez | e96e6fd | 2018-04-03 15:02:37 +0000 | [diff] [blame] | 456 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 457 | ch = m_pBuf[m_Pos++]; |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 458 | if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | m_Pos--; |
| 460 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 461 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 462 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 463 | } |
Dan Sinclair | 6947287 | 2015-10-28 10:20:35 -0400 | [diff] [blame] | 464 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 465 | ByteString CPDF_StreamParser::ReadString() { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 466 | if (!PositionIsInBounds()) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 467 | return ByteString(); |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 468 | |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 469 | uint8_t ch = m_pBuf[m_Pos++]; |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 470 | std::ostringstream buf; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 471 | int parlevel = 0; |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 472 | int status = 0; |
| 473 | int iEscCode = 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 474 | while (1) { |
| 475 | switch (status) { |
| 476 | case 0: |
| 477 | if (ch == ')') { |
| 478 | if (parlevel == 0) { |
Henrique Nakashima | aa1c787 | 2018-01-30 19:12:10 +0000 | [diff] [blame] | 479 | if (buf.tellp() <= 0) |
| 480 | return ByteString(); |
| 481 | |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 482 | return ByteString( |
| 483 | buf.str().c_str(), |
| 484 | std::min(static_cast<size_t>(buf.tellp()), kMaxStringLength)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 485 | } |
| 486 | parlevel--; |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 487 | buf << ')'; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 488 | } else if (ch == '(') { |
| 489 | parlevel++; |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 490 | buf << '('; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 491 | } else if (ch == '\\') { |
| 492 | status = 1; |
| 493 | } else { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 494 | buf << static_cast<char>(ch); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 495 | } |
| 496 | break; |
| 497 | case 1: |
| 498 | if (ch >= '0' && ch <= '7') { |
Lei Zhang | e8c1d41 | 2017-05-04 12:13:55 -0700 | [diff] [blame] | 499 | iEscCode = FXSYS_DecimalCharToInt(static_cast<char>(ch)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 500 | status = 2; |
| 501 | break; |
| 502 | } |
Lei Zhang | b8d8680 | 2017-12-01 22:52:42 +0000 | [diff] [blame] | 503 | if (ch == '\r') { |
| 504 | status = 4; |
| 505 | break; |
| 506 | } |
| 507 | if (ch == '\n') { |
| 508 | // Do nothing. |
| 509 | } else if (ch == 'n') { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 510 | buf << '\n'; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 511 | } else if (ch == 'r') { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 512 | buf << '\r'; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 513 | } else if (ch == 't') { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 514 | buf << '\t'; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 515 | } else if (ch == 'b') { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 516 | buf << '\b'; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 517 | } else if (ch == 'f') { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 518 | buf << '\f'; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 519 | } else { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 520 | buf << static_cast<char>(ch); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 521 | } |
| 522 | status = 0; |
| 523 | break; |
| 524 | case 2: |
| 525 | if (ch >= '0' && ch <= '7') { |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 526 | iEscCode = |
Lei Zhang | e8c1d41 | 2017-05-04 12:13:55 -0700 | [diff] [blame] | 527 | iEscCode * 8 + FXSYS_DecimalCharToInt(static_cast<char>(ch)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 528 | status = 3; |
| 529 | } else { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 530 | buf << static_cast<char>(iEscCode); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 531 | status = 0; |
| 532 | continue; |
| 533 | } |
| 534 | break; |
| 535 | case 3: |
| 536 | if (ch >= '0' && ch <= '7') { |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 537 | iEscCode = |
Lei Zhang | e8c1d41 | 2017-05-04 12:13:55 -0700 | [diff] [blame] | 538 | iEscCode * 8 + FXSYS_DecimalCharToInt(static_cast<char>(ch)); |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 539 | buf << static_cast<char>(iEscCode); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 540 | status = 0; |
| 541 | } else { |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 542 | buf << static_cast<char>(iEscCode); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 543 | status = 0; |
| 544 | continue; |
| 545 | } |
| 546 | break; |
| 547 | case 4: |
| 548 | status = 0; |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 549 | if (ch != '\n') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 550 | continue; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 551 | break; |
| 552 | } |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 553 | if (!PositionIsInBounds()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 554 | break; |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 555 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 556 | ch = m_pBuf[m_Pos++]; |
| 557 | } |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 558 | if (PositionIsInBounds()) |
Dan Sinclair | 1c91537 | 2016-03-03 17:12:58 -0500 | [diff] [blame] | 559 | ++m_Pos; |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 560 | |
Henrique Nakashima | aa1c787 | 2018-01-30 19:12:10 +0000 | [diff] [blame] | 561 | if (buf.tellp() <= 0) |
| 562 | return ByteString(); |
| 563 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 564 | return ByteString( |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 565 | buf.str().c_str(), |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 566 | std::min(static_cast<size_t>(buf.tellp()), kMaxStringLength)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 567 | } |
Wei Li | dc4d504 | 2016-01-19 13:19:44 -0800 | [diff] [blame] | 568 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 569 | ByteString CPDF_StreamParser::ReadHexString() { |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 570 | if (!PositionIsInBounds()) |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 571 | return ByteString(); |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 572 | |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 573 | std::ostringstream buf; |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 574 | bool bFirst = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 575 | int code = 0; |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 576 | while (PositionIsInBounds()) { |
| 577 | int ch = m_pBuf[m_Pos++]; |
| 578 | |
| 579 | if (ch == '>') |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 580 | break; |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 581 | |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 582 | if (!std::isxdigit(ch)) |
| 583 | continue; |
| 584 | |
Lei Zhang | e8c1d41 | 2017-05-04 12:13:55 -0700 | [diff] [blame] | 585 | int val = FXSYS_HexCharToInt(ch); |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 586 | if (bFirst) { |
| 587 | code = val * 16; |
| 588 | } else { |
| 589 | code += val; |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 590 | buf << static_cast<uint8_t>(code); |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 591 | } |
| 592 | bFirst = !bFirst; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 593 | } |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 594 | if (!bFirst) |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 595 | buf << static_cast<char>(code); |
Dan Sinclair | 10cfea1 | 2015-11-16 13:09:00 -0500 | [diff] [blame] | 596 | |
Henrique Nakashima | aa1c787 | 2018-01-30 19:12:10 +0000 | [diff] [blame] | 597 | if (buf.tellp() <= 0) |
| 598 | return ByteString(); |
| 599 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 600 | return ByteString( |
Henrique Nakashima | 0915087 | 2017-06-08 10:08:08 -0400 | [diff] [blame] | 601 | buf.str().c_str(), |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 602 | std::min(static_cast<size_t>(buf.tellp()), kMaxStringLength)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 603 | } |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 604 | |
| 605 | bool CPDF_StreamParser::PositionIsInBounds() const { |
Tom Sepez | 6058efd | 2018-04-06 23:48:24 +0000 | [diff] [blame] | 606 | return m_Pos < m_pBuf.size(); |
Dan Sinclair | 90c9005 | 2015-10-28 13:05:52 -0400 | [diff] [blame] | 607 | } |