Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: fpdfsdk/javascript/JS_GlobalData.cpp

Issue 1876203002: Pass CFX_ByteStrings rather than raw ptrs to JS_GlobalData. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "fpdfsdk/javascript/JS_GlobalData.h" 7 #include "fpdfsdk/javascript/JS_GlobalData.h"
8 8
9 #include "core/fdrm/crypto/include/fx_crypt.h" 9 #include "core/fdrm/crypto/include/fx_crypt.h"
10 #include "third_party/base/stl_util.h" 10 #include "third_party/base/stl_util.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51, 87 0xb4, 0xcf, 0xd7, 0x77, 0x67, 0xf9, 0x75, 0x9f, 0xf0, 0xe0, 0x1e, 0x51,
88 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16, 88 0xee, 0x46, 0xfd, 0x0b, 0xc9, 0x93, 0x25, 0x55, 0x4a, 0xee, 0xe0, 0x16,
89 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22, 89 0xd0, 0xdf, 0x8c, 0xfa, 0x2a, 0xa9, 0x49, 0xfd, 0x97, 0x1c, 0x0e, 0x22,
90 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b, 90 0x13, 0x28, 0x7c, 0xaf, 0xc4, 0xfc, 0x9c, 0x12, 0x65, 0x8c, 0x4e, 0x5b,
91 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a, 91 0x04, 0x75, 0x89, 0xc9, 0xb1, 0xed, 0x50, 0xca, 0x96, 0x6f, 0x1a, 0x7a,
92 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00, 92 0xfe, 0x58, 0x5d, 0xec, 0x19, 0x4a, 0xf6, 0x35, 0x6a, 0x97, 0x14, 0x00,
93 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0, 93 0x0e, 0xd0, 0x6b, 0xbb, 0xd5, 0x75, 0x55, 0x8b, 0x6e, 0x6b, 0x19, 0xa0,
94 0xf8, 0x77, 0xd5, 0xa3}; 94 0xf8, 0x77, 0xd5, 0xa3};
95 95
96 // Returns true if non-empty, setting sPropName 96 // Returns true if non-empty, setting sPropName
97 static bool TrimPropName(const char* propname, CFX_ByteString* sPropName) { 97 static bool TrimPropName(CFX_ByteString* sPropName) {
98 ASSERT(propname);
99 *sPropName = propname;
100 sPropName->TrimLeft(); 98 sPropName->TrimLeft();
Tom Sepez 2016/04/11 17:51:06 Note: these are now no-touch if possible, so TrimP
101 sPropName->TrimRight(); 99 sPropName->TrimRight();
102 return sPropName->GetLength() != 0; 100 return sPropName->GetLength() != 0;
103 } 101 }
104 102
105 CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr; 103 CJS_GlobalData* CJS_GlobalData::g_Instance = nullptr;
106 104
107 // static 105 // static
108 CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) { 106 CJS_GlobalData* CJS_GlobalData::GetRetainedInstance(CPDFDoc_Environment* pApp) {
109 if (!g_Instance) { 107 if (!g_Instance) {
110 g_Instance = new CJS_GlobalData(); 108 g_Instance = new CJS_GlobalData();
(...skipping 12 matching lines...) Expand all
123 CJS_GlobalData::CJS_GlobalData() 121 CJS_GlobalData::CJS_GlobalData()
124 : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) { 122 : m_RefCount(0), m_sFilePath(SDK_JS_GLOBALDATA_FILENAME) {
125 LoadGlobalPersistentVariables(); 123 LoadGlobalPersistentVariables();
126 } 124 }
127 125
128 CJS_GlobalData::~CJS_GlobalData() { 126 CJS_GlobalData::~CJS_GlobalData() {
129 SaveGlobalPersisitentVariables(); 127 SaveGlobalPersisitentVariables();
130 } 128 }
131 129
132 CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( 130 CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable(
133 const FX_CHAR* propname) { 131 const CFX_ByteString& propname) {
134 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); 132 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
135 ++it) { 133 ++it) {
136 if ((*it)->data.sKey == propname) 134 if ((*it)->data.sKey == propname)
137 return it; 135 return it;
138 } 136 }
139 return m_arrayGlobalData.end(); 137 return m_arrayGlobalData.end();
140 } 138 }
141 139
142 CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( 140 CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable(
143 const FX_CHAR* propname) const { 141 const CFX_ByteString& propname) const {
144 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); 142 for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end();
145 ++it) { 143 ++it) {
146 if ((*it)->data.sKey == propname) 144 if ((*it)->data.sKey == propname)
147 return it; 145 return it;
148 } 146 }
149 return m_arrayGlobalData.end(); 147 return m_arrayGlobalData.end();
150 } 148 }
151 149
152 CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( 150 CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable(
153 const FX_CHAR* propname) { 151 const CFX_ByteString& propname) {
154 auto iter = FindGlobalVariable(propname); 152 auto iter = FindGlobalVariable(propname);
155 return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; 153 return iter != m_arrayGlobalData.end() ? iter->get() : nullptr;
156 } 154 }
157 155
158 void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname, 156 void CJS_GlobalData::SetGlobalVariableNumber(CFX_ByteString sPropName,
159 double dData) { 157 double dData) {
160 CFX_ByteString sPropName; 158 if (!TrimPropName(&sPropName))
161 if (!TrimPropName(propname, &sPropName))
162 return; 159 return;
163 160
164 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 161 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
165 pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; 162 pData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
166 pData->data.dData = dData; 163 pData->data.dData = dData;
167 return; 164 return;
168 } 165 }
169 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); 166 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
170 pNewData->data.sKey = sPropName; 167 pNewData->data.sKey = sPropName;
171 pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER; 168 pNewData->data.nType = JS_GLOBALDATA_TYPE_NUMBER;
172 pNewData->data.dData = dData; 169 pNewData->data.dData = dData;
173 m_arrayGlobalData.push_back(std::move(pNewData)); 170 m_arrayGlobalData.push_back(std::move(pNewData));
174 } 171 }
175 172
176 void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname, 173 void CJS_GlobalData::SetGlobalVariableBoolean(CFX_ByteString sPropName,
177 bool bData) { 174 bool bData) {
178 CFX_ByteString sPropName; 175 if (!TrimPropName(&sPropName))
179 if (!TrimPropName(propname, &sPropName))
180 return; 176 return;
181 177
182 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 178 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
183 pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; 179 pData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
184 pData->data.bData = bData; 180 pData->data.bData = bData;
185 return; 181 return;
186 } 182 }
187 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); 183 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
188 pNewData->data.sKey = sPropName; 184 pNewData->data.sKey = sPropName;
189 pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN; 185 pNewData->data.nType = JS_GLOBALDATA_TYPE_BOOLEAN;
190 pNewData->data.bData = bData; 186 pNewData->data.bData = bData;
191 m_arrayGlobalData.push_back(std::move(pNewData)); 187 m_arrayGlobalData.push_back(std::move(pNewData));
192 } 188 }
193 189
194 void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname, 190 void CJS_GlobalData::SetGlobalVariableString(CFX_ByteString sPropName,
195 const CFX_ByteString& sData) { 191 const CFX_ByteString& sData) {
196 CFX_ByteString sPropName; 192 if (!TrimPropName(&sPropName))
197 if (!TrimPropName(propname, &sPropName))
198 return; 193 return;
199 194
200 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 195 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
201 pData->data.nType = JS_GLOBALDATA_TYPE_STRING; 196 pData->data.nType = JS_GLOBALDATA_TYPE_STRING;
202 pData->data.sData = sData; 197 pData->data.sData = sData;
203 return; 198 return;
204 } 199 }
205 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); 200 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
206 pNewData->data.sKey = sPropName; 201 pNewData->data.sKey = sPropName;
207 pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING; 202 pNewData->data.nType = JS_GLOBALDATA_TYPE_STRING;
208 pNewData->data.sData = sData; 203 pNewData->data.sData = sData;
209 m_arrayGlobalData.push_back(std::move(pNewData)); 204 m_arrayGlobalData.push_back(std::move(pNewData));
210 } 205 }
211 206
212 void CJS_GlobalData::SetGlobalVariableObject( 207 void CJS_GlobalData::SetGlobalVariableObject(
213 const FX_CHAR* propname, 208 CFX_ByteString sPropName,
214 const CJS_GlobalVariableArray& array) { 209 const CJS_GlobalVariableArray& array) {
215 CFX_ByteString sPropName; 210 if (!TrimPropName(&sPropName))
216 if (!TrimPropName(propname, &sPropName))
217 return; 211 return;
218 212
219 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 213 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
220 pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; 214 pData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
221 pData->data.objData.Copy(array); 215 pData->data.objData.Copy(array);
222 return; 216 return;
223 } 217 }
224 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); 218 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
225 pNewData->data.sKey = sPropName; 219 pNewData->data.sKey = sPropName;
226 pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT; 220 pNewData->data.nType = JS_GLOBALDATA_TYPE_OBJECT;
227 pNewData->data.objData.Copy(array); 221 pNewData->data.objData.Copy(array);
228 m_arrayGlobalData.push_back(std::move(pNewData)); 222 m_arrayGlobalData.push_back(std::move(pNewData));
229 } 223 }
230 224
231 void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) { 225 void CJS_GlobalData::SetGlobalVariableNull(CFX_ByteString sPropName) {
232 CFX_ByteString sPropName; 226 if (!TrimPropName(&sPropName))
233 if (!TrimPropName(propname, &sPropName))
234 return; 227 return;
235 228
236 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { 229 if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) {
237 pData->data.nType = JS_GLOBALDATA_TYPE_NULL; 230 pData->data.nType = JS_GLOBALDATA_TYPE_NULL;
238 return; 231 return;
239 } 232 }
240 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element); 233 std::unique_ptr<CJS_GlobalData_Element> pNewData(new CJS_GlobalData_Element);
241 pNewData->data.sKey = sPropName; 234 pNewData->data.sKey = sPropName;
242 pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL; 235 pNewData->data.nType = JS_GLOBALDATA_TYPE_NULL;
243 m_arrayGlobalData.push_back(std::move(pNewData)); 236 m_arrayGlobalData.push_back(std::move(pNewData));
244 } 237 }
245 238
246 FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname, 239 FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(CFX_ByteString sPropName,
247 FX_BOOL bPersistent) { 240 FX_BOOL bPersistent) {
248 CFX_ByteString sPropName; 241 if (!TrimPropName(&sPropName))
249 if (!TrimPropName(propname, &sPropName))
250 return FALSE; 242 return FALSE;
251 243
252 CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); 244 CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName);
253 if (!pData) 245 if (!pData)
254 return FALSE; 246 return FALSE;
255 247
256 pData->bPersistent = bPersistent; 248 pData->bPersistent = bPersistent;
257 return TRUE; 249 return TRUE;
258 } 250 }
259 251
260 FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) { 252 FX_BOOL CJS_GlobalData::DeleteGlobalVariable(CFX_ByteString sPropName) {
261 CFX_ByteString sPropName; 253 if (!TrimPropName(&sPropName))
262 if (!TrimPropName(propname, &sPropName))
263 return FALSE; 254 return FALSE;
264 255
265 auto iter = FindGlobalVariable(sPropName); 256 auto iter = FindGlobalVariable(sPropName);
266 if (iter == m_arrayGlobalData.end()) 257 if (iter == m_arrayGlobalData.end())
267 return FALSE; 258 return FALSE;
268 259
269 m_arrayGlobalData.erase(iter); 260 m_arrayGlobalData.erase(iter);
270 return TRUE; 261 return TRUE;
271 } 262 }
272 263
(...skipping 12 matching lines...) Expand all
285 int32_t nLength = 0; 276 int32_t nLength = 0;
286 277
287 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); 278 LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength);
288 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); 279 CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY));
289 280
290 if (pBuffer) { 281 if (pBuffer) {
291 uint8_t* p = pBuffer; 282 uint8_t* p = pBuffer;
292 uint16_t wType = *((uint16_t*)p); 283 uint16_t wType = *((uint16_t*)p);
293 p += sizeof(uint16_t); 284 p += sizeof(uint16_t);
294 285
295 // uint16_t wTemp = (uint16_t)(('X' << 8) | 'F');
296
297 if (wType == (uint16_t)(('X' << 8) | 'F')) { 286 if (wType == (uint16_t)(('X' << 8) | 'F')) {
298 uint16_t wVersion = *((uint16_t*)p); 287 uint16_t wVersion = *((uint16_t*)p);
299 p += sizeof(uint16_t); 288 p += sizeof(uint16_t);
300 289
301 ASSERT(wVersion <= 2); 290 ASSERT(wVersion <= 2);
302 291
303 uint32_t dwCount = *((uint32_t*)p); 292 uint32_t dwCount = *((uint32_t*)p);
304 p += sizeof(uint32_t); 293 p += sizeof(uint32_t);
305 294
306 uint32_t dwSize = *((uint32_t*)p); 295 uint32_t dwSize = *((uint32_t*)p);
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 case JS_GLOBALDATA_TYPE_NULL: { 438 case JS_GLOBALDATA_TYPE_NULL: {
450 uint32_t dwNameLen = (uint32_t)name.GetLength(); 439 uint32_t dwNameLen = (uint32_t)name.GetLength();
451 sData.AppendBlock(&dwNameLen, sizeof(uint32_t)); 440 sData.AppendBlock(&dwNameLen, sizeof(uint32_t));
452 sData.AppendString(name); 441 sData.AppendString(name);
453 sData.AppendBlock(&wType, sizeof(uint32_t)); 442 sData.AppendBlock(&wType, sizeof(uint32_t));
454 } break; 443 } break;
455 default: 444 default:
456 break; 445 break;
457 } 446 }
458 } 447 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698