Add constants for PDF 1.7 spec, table 3.4.

Add constants/stream_dict_common.h. The header lists all the constants
in the table in the same order. Constants that are not used at all are
commented out.

BUG=pdfium:1049

Change-Id: I6539090e0ad56319ea628883e388aeacef044e52
Reviewed-on: https://pdfium-review.googlesource.com/29090
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 5d69b8d..e78b2cf 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -192,6 +192,7 @@
   configs += [ ":pdfium_core_config" ]
 
   deps = [
+    ":constants",
     ":fdrm",
     ":formfiller",
     ":fpdfapi",
@@ -291,6 +292,12 @@
   "//:gn_visibility",
 ]
 
+source_set("constants") {
+  sources = [
+    "constants/stream_dict_common.h",
+  ]
+}
+
 jumbo_static_library("fdrm") {
   sources = [
     "core/fdrm/crypto/fx_crypt.cpp",
diff --git a/DEPS b/DEPS
index 38ff99d..476f404 100644
--- a/DEPS
+++ b/DEPS
@@ -138,6 +138,7 @@
 include_rules = [
   # Basic stuff that everyone can use.
   # Note: public is not here because core cannot depend on public.
+  '+constants',
   '+testing',
   '+third_party/base',
 ]
diff --git a/constants/stream_dict_common.h b/constants/stream_dict_common.h
new file mode 100644
index 0000000..fc12622
--- /dev/null
+++ b/constants/stream_dict_common.h
@@ -0,0 +1,27 @@
+// Copyright 2018 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONSTANTS_STREAM_DICT_COMMON_H_
+#define CONSTANTS_STREAM_DICT_COMMON_H_
+
+namespace pdfium {
+namespace stream {
+
+// PDF 1.7 spec, table 3.4.
+// Entries common to all stream dictionaries.
+
+// TODO(https://crbug.com/pdfium/1049): Examine all usages of "Length",
+// "Filter", and "F".
+constexpr char kLength[] = "Length";
+constexpr char kFilter[] = "Filter";
+constexpr char kDecodeParms[] = "DecodeParms";
+constexpr char kF[] = "F";
+// constexpr char kFFilter[] = "FFilter";
+// constexpr char kFDecodeParms[] = "FDecodeParms";
+constexpr char kDL[] = "DL";
+
+}  // namespace stream
+}  // namespace pdfium
+
+#endif  // CONSTANTS_STREAM_DICT_COMMON_H_
diff --git a/core/fpdfapi/edit/cpdf_flateencoder.cpp b/core/fpdfapi/edit/cpdf_flateencoder.cpp
index 9826932..f6125b8 100644
--- a/core/fpdfapi/edit/cpdf_flateencoder.cpp
+++ b/core/fpdfapi/edit/cpdf_flateencoder.cpp
@@ -8,6 +8,7 @@
 
 #include <memory>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/parser/cpdf_name.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
@@ -43,7 +44,7 @@
   m_pDict = ToDictionary(pStream->GetDict()->Clone());
   m_pDict->SetNewFor<CPDF_Number>("Length", static_cast<int>(m_dwSize));
   m_pDict->SetNewFor<CPDF_Name>("Filter", "FlateDecode");
-  m_pDict->RemoveFor("DecodeParms");
+  m_pDict->RemoveFor(pdfium::stream::kDecodeParms);
 }
 
 CPDF_FlateEncoder::~CPDF_FlateEncoder() {}
diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp
index ec826dc..68a6a32 100644
--- a/core/fpdfapi/page/cpdf_image.cpp
+++ b/core/fpdfapi/page/cpdf_image.cpp
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/cpdf_modulemgr.h"
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
@@ -105,7 +106,8 @@
   pDict->SetNewFor<CPDF_Number>("BitsPerComponent", bits);
   pDict->SetNewFor<CPDF_Name>("Filter", "DCTDecode");
   if (!color_trans) {
-    CPDF_Dictionary* pParms = pDict->SetNewFor<CPDF_Dictionary>("DecodeParms");
+    CPDF_Dictionary* pParms =
+        pDict->SetNewFor<CPDF_Dictionary>(pdfium::stream::kDecodeParms);
     pParms->SetNewFor<CPDF_Number>("ColorTransform", 0);
   }
   m_bIsMask = false;
diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp
index 82f31dd..b6658ee 100644
--- a/core/fpdfapi/page/cpdf_streamparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamparser.cpp
@@ -13,6 +13,7 @@
 #include <sstream>
 #include <utility>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/cpdf_modulemgr.h"
 #include "core/fpdfapi/page/cpdf_docpagedata.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
@@ -126,12 +127,12 @@
   if (pFilter) {
     if (CPDF_Array* pArray = pFilter->AsArray()) {
       Decoder = pArray->GetStringAt(0);
-      CPDF_Array* pParams = pDict->GetArrayFor("DecodeParms");
+      CPDF_Array* pParams = pDict->GetArrayFor(pdfium::stream::kDecodeParms);
       if (pParams)
         pParam = pParams->GetDictAt(0);
     } else {
       Decoder = pFilter->GetString();
-      pParam = pDict->GetDictFor("DecodeParms");
+      pParam = pDict->GetDictFor(pdfium::stream::kDecodeParms);
     }
   }
   uint32_t width = pDict->GetIntegerFor("Width");
diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp
index 4b16021..4780e87 100644
--- a/core/fpdfapi/parser/cpdf_object_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp
@@ -7,6 +7,7 @@
 #include <utility>
 #include <vector>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_boolean.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
@@ -790,21 +791,25 @@
   stream->InitStream(data.data(), data.size(),
                      pdfium::MakeUnique<CPDF_Dictionary>());
   EXPECT_EQ(static_cast<int>(data.size()),
-            stream->GetDict()->GetIntegerFor("Length"));
+            stream->GetDict()->GetIntegerFor(pdfium::stream::kLength));
 
-  stream->GetDict()->SetNewFor<CPDF_String>("Filter", L"SomeFilter");
-  stream->GetDict()->SetNewFor<CPDF_String>("DecodeParms", L"SomeParams");
+  stream->GetDict()->SetNewFor<CPDF_String>(pdfium::stream::kFilter,
+                                            L"SomeFilter");
+  stream->GetDict()->SetNewFor<CPDF_String>(pdfium::stream::kDecodeParms,
+                                            L"SomeParams");
 
   std::vector<uint8_t> new_data(data.size() * 2);
   stream->SetData(new_data.data(), new_data.size());
 
   // The "Length" field should be updated for new data size.
   EXPECT_EQ(static_cast<int>(new_data.size()),
-            stream->GetDict()->GetIntegerFor("Length"));
+            stream->GetDict()->GetIntegerFor(pdfium::stream::kLength));
 
   // The "Filter" and "DecodeParms" fields should not be changed.
-  EXPECT_EQ(stream->GetDict()->GetUnicodeTextFor("Filter"), L"SomeFilter");
-  EXPECT_EQ(stream->GetDict()->GetUnicodeTextFor("DecodeParms"), L"SomeParams");
+  EXPECT_EQ(stream->GetDict()->GetUnicodeTextFor(pdfium::stream::kFilter),
+            L"SomeFilter");
+  EXPECT_EQ(stream->GetDict()->GetUnicodeTextFor(pdfium::stream::kDecodeParms),
+            L"SomeParams");
 }
 
 TEST(PDFStreamTest, SetDataAndRemoveFilter) {
@@ -813,20 +818,22 @@
   stream->InitStream(data.data(), data.size(),
                      pdfium::MakeUnique<CPDF_Dictionary>());
   EXPECT_EQ(static_cast<int>(data.size()),
-            stream->GetDict()->GetIntegerFor("Length"));
+            stream->GetDict()->GetIntegerFor(pdfium::stream::kLength));
 
-  stream->GetDict()->SetNewFor<CPDF_String>("Filter", L"SomeFilter");
-  stream->GetDict()->SetNewFor<CPDF_String>("DecodeParms", L"SomeParams");
+  stream->GetDict()->SetNewFor<CPDF_String>(pdfium::stream::kFilter,
+                                            L"SomeFilter");
+  stream->GetDict()->SetNewFor<CPDF_String>(pdfium::stream::kDecodeParms,
+                                            L"SomeParams");
 
   std::vector<uint8_t> new_data(data.size() * 2);
   stream->SetDataAndRemoveFilter(new_data.data(), new_data.size());
   // The "Length" field should be updated for new data size.
   EXPECT_EQ(static_cast<int>(new_data.size()),
-            stream->GetDict()->GetIntegerFor("Length"));
+            stream->GetDict()->GetIntegerFor(pdfium::stream::kLength));
 
   // The "Filter" and "DecodeParms" should be removed.
-  EXPECT_FALSE(stream->GetDict()->KeyExist("Filter"));
-  EXPECT_FALSE(stream->GetDict()->KeyExist("DecodeParms"));
+  EXPECT_FALSE(stream->GetDict()->KeyExist(pdfium::stream::kFilter));
+  EXPECT_FALSE(stream->GetDict()->KeyExist(pdfium::stream::kDecodeParms));
 }
 
 TEST(PDFStreamTest, LengthInDictionaryOnCreate) {
@@ -838,18 +845,18 @@
     auto stream = pdfium::MakeUnique<CPDF_Stream>(
         std::move(data), kBufSize, pdfium::MakeUnique<CPDF_Dictionary>());
     EXPECT_EQ(static_cast<int>(kBufSize),
-              stream->GetDict()->GetIntegerFor("Length"));
+              stream->GetDict()->GetIntegerFor(pdfium::stream::kLength));
   }
   // The length field should be corrected on stream create.
   {
     std::unique_ptr<uint8_t, FxFreeDeleter> data;
     data.reset(FX_Alloc(uint8_t, kBufSize));
     auto dict = pdfium::MakeUnique<CPDF_Dictionary>();
-    dict->SetNewFor<CPDF_Number>("Length", 30000);
+    dict->SetNewFor<CPDF_Number>(pdfium::stream::kLength, 30000);
     auto stream = pdfium::MakeUnique<CPDF_Stream>(std::move(data), kBufSize,
                                                   std::move(dict));
     EXPECT_EQ(static_cast<int>(kBufSize),
-              stream->GetDict()->GetIntegerFor("Length"));
+              stream->GetDict()->GetIntegerFor(pdfium::stream::kLength));
   }
 }
 
diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp
index 5ff7484..45d2ebb 100644
--- a/core/fpdfapi/parser/cpdf_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_stream.cpp
@@ -8,6 +8,7 @@
 
 #include <utility>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
@@ -96,7 +97,7 @@
 void CPDF_Stream::SetDataAndRemoveFilter(const uint8_t* pData, uint32_t size) {
   SetData(pData, size);
   m_pDict->RemoveFor("Filter");
-  m_pDict->RemoveFor("DecodeParms");
+  m_pDict->RemoveFor(pdfium::stream::kDecodeParms);
 }
 
 void CPDF_Stream::SetDataAndRemoveFilter(std::ostringstream* stream) {
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index 90dca2e..e879615 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -13,6 +13,7 @@
 #include <utility>
 #include <vector>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/cpdf_modulemgr.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
@@ -344,7 +345,7 @@
     return false;
 
   CPDF_Object* pParams =
-      pDict ? pDict->GetDirectObjectFor("DecodeParms") : nullptr;
+      pDict ? pDict->GetDirectObjectFor(pdfium::stream::kDecodeParms) : nullptr;
 
   std::vector<std::pair<ByteString, CPDF_Object*>> DecoderArray;
   if (CPDF_Array* pDecoders = pDecoder->AsArray()) {
diff --git a/core/fpdfdoc/cpdf_action.cpp b/core/fpdfdoc/cpdf_action.cpp
index 383f328..4ac86fe 100644
--- a/core/fpdfdoc/cpdf_action.cpp
+++ b/core/fpdfdoc/cpdf_action.cpp
@@ -6,6 +6,7 @@
 
 #include "core/fpdfdoc/cpdf_action.h"
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
 #include "core/fpdfdoc/cpdf_filespec.h"
@@ -70,14 +71,15 @@
     return WideString();
   }
 
-  CPDF_Object* pFile = m_pDict->GetDirectObjectFor("F");
+  CPDF_Object* pFile = m_pDict->GetDirectObjectFor(pdfium::stream::kF);
   if (pFile)
     return CPDF_FileSpec(pFile).GetFileName();
 
   if (type == "Launch") {
     CPDF_Dictionary* pWinDict = m_pDict->GetDictFor("Win");
     if (pWinDict) {
-      return WideString::FromLocal(pWinDict->GetStringFor("F").AsStringView());
+      return WideString::FromLocal(
+          pWinDict->GetStringFor(pdfium::stream::kF).AsStringView());
     }
   }
   return WideString();
diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp
index 2f999bc..e741ac6 100644
--- a/core/fpdfdoc/cpdf_filespec.cpp
+++ b/core/fpdfdoc/cpdf_filespec.cpp
@@ -8,6 +8,7 @@
 
 #include <vector>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_name.h"
 #include "core/fpdfapi/parser/cpdf_object.h"
@@ -94,8 +95,8 @@
   if (CPDF_Dictionary* pDict = m_pObj->AsDictionary()) {
     csFileName = pDict->GetUnicodeTextFor("UF");
     if (csFileName.IsEmpty()) {
-      csFileName =
-          WideString::FromLocal(pDict->GetStringFor("F").AsStringView());
+      csFileName = WideString::FromLocal(
+          pDict->GetStringFor(pdfium::stream::kF).AsStringView());
     }
     if (pDict->GetStringFor("FS") == "URL")
       return csFileName;
@@ -187,7 +188,8 @@
   if (m_pObj->IsString()) {
     m_pObj->SetString(ByteString::FromUnicode(wsStr));
   } else if (CPDF_Dictionary* pDict = m_pObj->AsDictionary()) {
-    pDict->SetNewFor<CPDF_String>("F", ByteString::FromUnicode(wsStr), false);
+    pDict->SetNewFor<CPDF_String>(pdfium::stream::kF,
+                                  ByteString::FromUnicode(wsStr), false);
     pDict->SetNewFor<CPDF_String>("UF", PDF_EncodeText(wsStr), false);
   }
 }
diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp
index 6c53c97..6bf72fd 100644
--- a/core/fpdfdoc/cpdf_interform.cpp
+++ b/core/fpdfdoc/cpdf_interform.cpp
@@ -9,6 +9,7 @@
 #include <utility>
 #include <vector>
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/font/cpdf_font.h"
 #include "core/fpdfapi/font/cpdf_fontencoding.h"
 #include "core/fpdfapi/page/cpdf_page.h"
@@ -1117,7 +1118,7 @@
     if (bSimpleFileSpec) {
       WideString wsFilePath = CPDF_FileSpec::EncodeFileName(pdf_path);
       pMainDict->SetNewFor<CPDF_String>(
-          "F", ByteString::FromUnicode(wsFilePath), false);
+          pdfium::stream::kF, ByteString::FromUnicode(wsFilePath), false);
       pMainDict->SetNewFor<CPDF_String>("UF", PDF_EncodeText(wsFilePath),
                                         false);
     } else {
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index 16e7efc..9cf4c44 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -6,6 +6,7 @@
 
 #include "fpdfsdk/cpdfsdk_helpers.h"
 
+#include "constants/stream_dict_common.h"
 #include "core/fpdfapi/cpdf_modulemgr.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
@@ -350,7 +351,8 @@
     uint32_t decoded_len = 0;
     ByteString dummy_last_decoder;
     CPDF_Dictionary* dummy_last_param;
-    if (PDF_DataDecode(data, len, dict, dict->GetIntegerFor("DL"), false,
+    if (PDF_DataDecode(data, len, dict,
+                       dict->GetIntegerFor(pdfium::stream::kDL), false,
                        &decoded_data, &decoded_len, &dummy_last_decoder,
                        &dummy_last_param)) {
       if (buffer && buflen >= decoded_len)
diff --git a/fpdfsdk/fpdf_attachment.cpp b/fpdfsdk/fpdf_attachment.cpp
index 0bb9364..2595717 100644
--- a/fpdfsdk/fpdf_attachment.cpp
+++ b/fpdfsdk/fpdf_attachment.cpp
@@ -7,6 +7,7 @@
 #include <memory>
 #include <utility>
 
+#include "constants/stream_dict_common.h"
 #include "core/fdrm/crypto/fx_crypt.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
@@ -90,7 +91,7 @@
   CPDF_Dictionary* pFile = pDoc->NewIndirect<CPDF_Dictionary>();
   pFile->SetNewFor<CPDF_Name>("Type", "Filespec");
   pFile->SetNewFor<CPDF_String>("UF", wsName);
-  pFile->SetNewFor<CPDF_String>("F", wsName);
+  pFile->SetNewFor<CPDF_String>(pdfium::stream::kF, wsName);
 
   // Add the new attachment name and filespec into the document's EmbeddedFiles.
   CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
@@ -230,7 +231,8 @@
       pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params");
 
   // Set the size of the new file in the dictionary.
-  pFileStreamDict->SetNewFor<CPDF_Number>("DL", static_cast<int>(len));
+  pFileStreamDict->SetNewFor<CPDF_Number>(pdfium::stream::kDL,
+                                          static_cast<int>(len));
   pParamsDict->SetNewFor<CPDF_Number>("Size", static_cast<int>(len));
 
   // Set the creation date of the new attachment in the dictionary.