OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../../include/fxcrt/fx_ext.h" | 7 #include "../../../include/fxcrt/fx_ext.h" |
8 #include "../../../include/fpdfapi/fpdf_serial.h" | 8 #include "../../../include/fpdfapi/fpdf_serial.h" |
9 #include "../../../include/fpdfapi/fpdf_parser.h" | 9 #include "../../../include/fpdfapi/fpdf_parser.h" |
10 #include "editint.h" | 10 #include "editint.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } | 139 } |
140 } | 140 } |
141 } | 141 } |
142 if (pFile->AppendString(FX_BSTRC(">>")) < 0) { | 142 if (pFile->AppendString(FX_BSTRC(">>")) < 0) { |
143 return -1; | 143 return -1; |
144 } | 144 } |
145 offset += 2; | 145 offset += 2; |
146 break; | 146 break; |
147 } | 147 } |
148 case PDFOBJ_STREAM: { | 148 case PDFOBJ_STREAM: { |
149 CPDF_Stream* p = (CPDF_Stream*)pObj; | 149 const CPDF_Stream* p = pObj->AsStream(); |
150 if (PDF_CreatorAppendObject(p->GetDict(), pFile, offset) < 0) { | 150 if (PDF_CreatorAppendObject(p->GetDict(), pFile, offset) < 0) { |
151 return -1; | 151 return -1; |
152 } | 152 } |
153 if (pFile->AppendString(FX_BSTRC("stream\r\n")) < 0) { | 153 if (pFile->AppendString(FX_BSTRC("stream\r\n")) < 0) { |
154 return -1; | 154 return -1; |
155 } | 155 } |
156 offset += 8; | 156 offset += 8; |
157 CPDF_StreamAcc acc; | 157 CPDF_StreamAcc acc; |
158 acc.LoadAllData(p, TRUE); | 158 acc.LoadAllData(p, TRUE); |
159 if (pFile->AppendBlock(acc.GetData(), acc.GetSize()) < 0) { | 159 if (pFile->AppendBlock(acc.GetData(), acc.GetSize()) < 0) { |
(...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 return FALSE; | 922 return FALSE; |
923 } | 923 } |
924 int32_t iSize = pXRef->m_IndexArray.GetSize() / 2; | 924 int32_t iSize = pXRef->m_IndexArray.GetSize() / 2; |
925 int32_t iCount = 0; | 925 int32_t iCount = 0; |
926 for (int32_t i = 0; i < iSize; i++) { | 926 for (int32_t i = 0; i < iSize; i++) { |
927 iCount += pXRef->m_IndexArray.ElementAt(i * 2 + 1); | 927 iCount += pXRef->m_IndexArray.ElementAt(i * 2 + 1); |
928 } | 928 } |
929 return (iCount >= PDF_XREFSTREAM_MAXSIZE); | 929 return (iCount >= PDF_XREFSTREAM_MAXSIZE); |
930 } | 930 } |
931 int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { | 931 int32_t CPDF_Creator::WriteIndirectObjectToStream(const CPDF_Object* pObj) { |
932 if (!m_pXRefStream) { | 932 if (!m_pXRefStream) |
933 return 1; | 933 return 1; |
934 } | 934 |
935 FX_DWORD objnum = pObj->GetObjNum(); | 935 FX_DWORD objnum = pObj->GetObjNum(); |
936 if (m_pParser && m_pParser->m_ObjVersion.GetSize() > (int32_t)objnum && | 936 if (m_pParser && m_pParser->m_ObjVersion.GetSize() > (int32_t)objnum && |
937 m_pParser->m_ObjVersion[objnum] > 0) { | 937 m_pParser->m_ObjVersion[objnum] > 0) { |
938 return 1; | 938 return 1; |
939 } | 939 } |
940 | 940 |
941 if (pObj->IsNumber()) | 941 if (pObj->IsNumber()) |
942 return 1; | 942 return 1; |
943 | 943 |
944 CPDF_Dictionary* pDict = pObj->GetDict(); | 944 CPDF_Dictionary* pDict = pObj->GetDict(); |
945 if (pObj->GetType() == PDFOBJ_STREAM) { | 945 if (pObj->IsStream()) |
946 if (pDict && pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("XRef")) { | 946 if (pDict && pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("XRef")) |
947 return 0; | 947 return 0; |
948 } | |
949 return 1; | 948 return 1; |
| 949 |
| 950 if (pDict) { |
| 951 if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) |
| 952 return 1; |
| 953 if (IsSignatureDict(pDict)) |
| 954 return 1; |
| 955 if (pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("Page")) |
| 956 return 1; |
950 } | 957 } |
951 if (pDict) { | 958 |
952 if (pDict == m_pDocument->m_pRootDict || pDict == m_pEncryptDict) { | |
953 return 1; | |
954 } | |
955 if (IsSignatureDict(pDict)) { | |
956 return 1; | |
957 } | |
958 if (pDict->GetString(FX_BSTRC("Type")) == FX_BSTRC("Page")) { | |
959 return 1; | |
960 } | |
961 } | |
962 m_pXRefStream->AddObjectNumberToIndexArray(objnum); | 959 m_pXRefStream->AddObjectNumberToIndexArray(objnum); |
963 if (m_pXRefStream->CompressIndirectObject(objnum, pObj, this) < 0) { | 960 if (m_pXRefStream->CompressIndirectObject(objnum, pObj, this) < 0) |
964 return -1; | 961 return -1; |
965 } | 962 if (!_IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) |
966 if (!_IsXRefNeedEnd(m_pXRefStream, m_dwFlags)) { | |
967 return 0; | 963 return 0; |
968 } | 964 if (!m_pXRefStream->End(this)) |
969 if (!m_pXRefStream->End(this)) { | |
970 return -1; | 965 return -1; |
971 } | 966 if (!m_pXRefStream->Start()) |
972 if (!m_pXRefStream->Start()) { | |
973 return -1; | 967 return -1; |
974 } | |
975 return 0; | 968 return 0; |
976 } | 969 } |
977 int32_t CPDF_Creator::WriteIndirectObjectToStream(FX_DWORD objnum, | 970 int32_t CPDF_Creator::WriteIndirectObjectToStream(FX_DWORD objnum, |
978 const uint8_t* pBuffer, | 971 const uint8_t* pBuffer, |
979 FX_DWORD dwSize) { | 972 FX_DWORD dwSize) { |
980 if (!m_pXRefStream) { | 973 if (!m_pXRefStream) { |
981 return 1; | 974 return 1; |
982 } | 975 } |
983 m_pXRefStream->AddObjectNumberToIndexArray(objnum); | 976 m_pXRefStream->AddObjectNumberToIndexArray(objnum); |
984 int32_t iRet = | 977 int32_t iRet = |
(...skipping 25 matching lines...) Expand all Loading... |
1010 } | 1003 } |
1011 if (!m_pXRefStream->Start()) { | 1004 if (!m_pXRefStream->Start()) { |
1012 return -1; | 1005 return -1; |
1013 } | 1006 } |
1014 return 0; | 1007 return 0; |
1015 } | 1008 } |
1016 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, | 1009 int32_t CPDF_Creator::WriteStream(const CPDF_Object* pStream, |
1017 FX_DWORD objnum, | 1010 FX_DWORD objnum, |
1018 CPDF_CryptoHandler* pCrypto) { | 1011 CPDF_CryptoHandler* pCrypto) { |
1019 CPDF_FlateEncoder encoder; | 1012 CPDF_FlateEncoder encoder; |
1020 encoder.Initialize((CPDF_Stream*)pStream, | 1013 encoder.Initialize(const_cast<CPDF_Stream*>(pStream->AsStream()), |
1021 pStream == m_pMetadata ? FALSE : m_bCompress); | 1014 pStream == m_pMetadata ? FALSE : m_bCompress); |
1022 CPDF_Encryptor encryptor; | 1015 CPDF_Encryptor encryptor; |
1023 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, | 1016 if (!encryptor.Initialize(pCrypto, objnum, encoder.m_pData, |
1024 encoder.m_dwSize)) { | 1017 encoder.m_dwSize)) { |
1025 return -1; | 1018 return -1; |
1026 } | 1019 } |
1027 if ((FX_DWORD)encoder.m_pDict->GetInteger(FX_BSTRC("Length")) != | 1020 if ((FX_DWORD)encoder.m_pDict->GetInteger(FX_BSTRC("Length")) != |
1028 encryptor.m_dwSize) { | 1021 encryptor.m_dwSize) { |
1029 encoder.CloneDict(); | 1022 encoder.CloneDict(); |
1030 encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize); | 1023 encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize); |
(...skipping 12 matching lines...) Expand all Loading... |
1043 m_Offset += encryptor.m_dwSize; | 1036 m_Offset += encryptor.m_dwSize; |
1044 if ((len = m_File.AppendString(FX_BSTRC("\r\nendstream"))) < 0) { | 1037 if ((len = m_File.AppendString(FX_BSTRC("\r\nendstream"))) < 0) { |
1045 return -1; | 1038 return -1; |
1046 } | 1039 } |
1047 m_Offset += len; | 1040 m_Offset += len; |
1048 return 1; | 1041 return 1; |
1049 } | 1042 } |
1050 int32_t CPDF_Creator::WriteIndirectObj(FX_DWORD objnum, | 1043 int32_t CPDF_Creator::WriteIndirectObj(FX_DWORD objnum, |
1051 const CPDF_Object* pObj) { | 1044 const CPDF_Object* pObj) { |
1052 int32_t len = m_File.AppendDWord(objnum); | 1045 int32_t len = m_File.AppendDWord(objnum); |
1053 if (len < 0) { | 1046 if (len < 0) |
1054 return -1; | 1047 return -1; |
1055 } | 1048 |
1056 m_Offset += len; | 1049 m_Offset += len; |
1057 if ((len = m_File.AppendString(FX_BSTRC(" 0 obj\r\n"))) < 0) { | 1050 if ((len = m_File.AppendString(FX_BSTRC(" 0 obj\r\n"))) < 0) |
1058 return -1; | 1051 return -1; |
1059 } | 1052 |
1060 m_Offset += len; | 1053 m_Offset += len; |
1061 if (pObj->GetType() == PDFOBJ_STREAM) { | 1054 if (pObj->IsStream()) { |
1062 CPDF_CryptoHandler* pHandler = NULL; | 1055 CPDF_CryptoHandler* pHandler = nullptr; |
1063 pHandler = | 1056 pHandler = |
1064 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; | 1057 (pObj == m_pMetadata && !m_bEncryptMetadata) ? NULL : m_pCryptoHandler; |
1065 if (WriteStream(pObj, objnum, pHandler) < 0) { | 1058 if (WriteStream(pObj, objnum, pHandler) < 0) |
1066 return -1; | 1059 return -1; |
1067 } | |
1068 } else { | 1060 } else { |
1069 if (WriteDirectObj(objnum, pObj) < 0) { | 1061 if (WriteDirectObj(objnum, pObj) < 0) |
1070 return -1; | 1062 return -1; |
1071 } | |
1072 } | 1063 } |
1073 if ((len = m_File.AppendString(FX_BSTRC("\r\nendobj\r\n"))) < 0) { | 1064 if ((len = m_File.AppendString(FX_BSTRC("\r\nendobj\r\n"))) < 0) |
1074 return -1; | 1065 return -1; |
1075 } | 1066 |
1076 m_Offset += len; | 1067 m_Offset += len; |
1077 if (AppendObjectNumberToXRef(objnum) < 0) { | 1068 if (AppendObjectNumberToXRef(objnum) < 0) |
1078 return -1; | 1069 return -1; |
1079 } | |
1080 return 0; | 1070 return 0; |
1081 } | 1071 } |
1082 int32_t CPDF_Creator::WriteIndirectObj(const CPDF_Object* pObj) { | 1072 int32_t CPDF_Creator::WriteIndirectObj(const CPDF_Object* pObj) { |
1083 int32_t iRet = WriteIndirectObjectToStream(pObj); | 1073 int32_t iRet = WriteIndirectObjectToStream(pObj); |
1084 if (iRet < 1) { | 1074 if (iRet < 1) { |
1085 return iRet; | 1075 return iRet; |
1086 } | 1076 } |
1087 return WriteIndirectObj(pObj->GetObjNum(), pObj); | 1077 return WriteIndirectObj(pObj->GetObjNum(), pObj); |
1088 } | 1078 } |
1089 int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, | 1079 int32_t CPDF_Creator::WriteDirectObj(FX_DWORD objnum, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1132 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), | 1122 CFX_ByteString((const FX_CHAR*)encryptor.m_pData, encryptor.m_dwSize), |
1133 bHex); | 1123 bHex); |
1134 if ((len = m_File.AppendString(content)) < 0) { | 1124 if ((len = m_File.AppendString(content)) < 0) { |
1135 return -1; | 1125 return -1; |
1136 } | 1126 } |
1137 m_Offset += len; | 1127 m_Offset += len; |
1138 break; | 1128 break; |
1139 } | 1129 } |
1140 case PDFOBJ_STREAM: { | 1130 case PDFOBJ_STREAM: { |
1141 CPDF_FlateEncoder encoder; | 1131 CPDF_FlateEncoder encoder; |
1142 encoder.Initialize((CPDF_Stream*)pObj, m_bCompress); | 1132 encoder.Initialize(const_cast<CPDF_Stream*>(pObj->AsStream()), |
| 1133 m_bCompress); |
1143 CPDF_Encryptor encryptor; | 1134 CPDF_Encryptor encryptor; |
1144 CPDF_CryptoHandler* pHandler = m_pCryptoHandler; | 1135 CPDF_CryptoHandler* pHandler = m_pCryptoHandler; |
1145 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); | 1136 encryptor.Initialize(pHandler, objnum, encoder.m_pData, encoder.m_dwSize); |
1146 if ((FX_DWORD)encoder.m_pDict->GetInteger(FX_BSTRC("Length")) != | 1137 if ((FX_DWORD)encoder.m_pDict->GetInteger(FX_BSTRC("Length")) != |
1147 encryptor.m_dwSize) { | 1138 encryptor.m_dwSize) { |
1148 encoder.CloneDict(); | 1139 encoder.CloneDict(); |
1149 encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize); | 1140 encoder.m_pDict->SetAtInteger(FX_BSTRC("Length"), encryptor.m_dwSize); |
1150 } | 1141 } |
1151 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { | 1142 if (WriteDirectObj(objnum, encoder.m_pDict) < 0) { |
1152 return -1; | 1143 return -1; |
(...skipping 975 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2128 m_bNewCrypto = FALSE; | 2119 m_bNewCrypto = FALSE; |
2129 if (!m_bStandardSecurity) { | 2120 if (!m_bStandardSecurity) { |
2130 return; | 2121 return; |
2131 } | 2122 } |
2132 if (m_pEncryptDict) { | 2123 if (m_pEncryptDict) { |
2133 m_pEncryptDict->Release(); | 2124 m_pEncryptDict->Release(); |
2134 m_pEncryptDict = NULL; | 2125 m_pEncryptDict = NULL; |
2135 } | 2126 } |
2136 m_bStandardSecurity = FALSE; | 2127 m_bStandardSecurity = FALSE; |
2137 } | 2128 } |
OLD | NEW |