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

Side by Side Diff: core/src/fxcrt/fx_basic_maps.cpp

Issue 1534953002: Fix a sentinel length value used in CFX_CMapByteStringToPtr::SetAt (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: type Created 5 years 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "core/include/fxcrt/fx_basic.h" 7 #include "core/include/fxcrt/fx_basic.h"
8 #include "plex.h" 8 #include "plex.h"
9 9
10 namespace {
11
12 const uint8_t kFreeLength = 0xfe;
13 const uint8_t kHasAllocatedBufferLength = 0xff;
14
15 } // namespace
16
10 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize) 17 CFX_MapPtrToPtr::CFX_MapPtrToPtr(int nBlockSize)
11 : m_pHashTable(NULL), 18 : m_pHashTable(NULL),
12 m_nHashTableSize(17), 19 m_nHashTableSize(17),
13 m_nCount(0), 20 m_nCount(0),
14 m_pFreeList(NULL), 21 m_pFreeList(NULL),
15 m_pBlocks(NULL), 22 m_pBlocks(NULL),
16 m_nBlockSize(nBlockSize) { 23 m_nBlockSize(nBlockSize) {
17 ASSERT(m_nBlockSize > 0); 24 ASSERT(m_nBlockSize > 0);
18 } 25 }
19 void CFX_MapPtrToPtr::RemoveAll() { 26 void CFX_MapPtrToPtr::RemoveAll() {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } 165 }
159 } 166 }
160 struct _CompactString { 167 struct _CompactString {
161 uint8_t m_CompactLen; 168 uint8_t m_CompactLen;
162 uint8_t m_LenHigh; 169 uint8_t m_LenHigh;
163 uint8_t m_LenLow; 170 uint8_t m_LenLow;
164 uint8_t m_Unused; 171 uint8_t m_Unused;
165 uint8_t* m_pBuffer; 172 uint8_t* m_pBuffer;
166 }; 173 };
167 static void _CompactStringRelease(_CompactString* pCompact) { 174 static void _CompactStringRelease(_CompactString* pCompact) {
168 if (pCompact->m_CompactLen == 0xff) { 175 if (pCompact->m_CompactLen == kHasAllocatedBufferLength) {
169 FX_Free(pCompact->m_pBuffer); 176 FX_Free(pCompact->m_pBuffer);
170 } 177 }
171 } 178 }
172 static FX_BOOL _CompactStringSame(_CompactString* pCompact, 179 static FX_BOOL _CompactStringSame(_CompactString* pCompact,
173 const uint8_t* pStr, 180 const uint8_t* pStr,
174 int len) { 181 int len) {
175 if (len < sizeof(_CompactString)) { 182 if (len < sizeof(_CompactString)) {
176 if (pCompact->m_CompactLen != len) { 183 if (pCompact->m_CompactLen != len) {
177 return FALSE; 184 return FALSE;
178 } 185 }
179 return FXSYS_memcmp(&pCompact->m_LenHigh, pStr, len) == 0; 186 return FXSYS_memcmp(&pCompact->m_LenHigh, pStr, len) == 0;
180 } 187 }
181 if (pCompact->m_CompactLen != 0xff || 188 if (pCompact->m_CompactLen != kHasAllocatedBufferLength ||
182 pCompact->m_LenHigh * 256 + pCompact->m_LenLow != len) { 189 pCompact->m_LenHigh * 256 + pCompact->m_LenLow != len) {
183 return FALSE; 190 return FALSE;
184 } 191 }
185 return FXSYS_memcmp(pCompact->m_pBuffer, pStr, len) == 0; 192 return FXSYS_memcmp(pCompact->m_pBuffer, pStr, len) == 0;
186 } 193 }
187 static void _CompactStringStore(_CompactString* pCompact, 194 static void _CompactStringStore(_CompactString* pCompact,
188 const uint8_t* pStr, 195 const uint8_t* pStr,
189 int len) { 196 int len) {
190 if (len < (int)sizeof(_CompactString)) { 197 if (len < (int)sizeof(_CompactString)) {
191 pCompact->m_CompactLen = (uint8_t)len; 198 pCompact->m_CompactLen = (uint8_t)len;
192 FXSYS_memcpy(&pCompact->m_LenHigh, pStr, len); 199 FXSYS_memcpy(&pCompact->m_LenHigh, pStr, len);
193 return; 200 return;
194 } 201 }
195 pCompact->m_CompactLen = 0xff; 202 pCompact->m_CompactLen = kHasAllocatedBufferLength;
196 pCompact->m_LenHigh = len / 256; 203 pCompact->m_LenHigh = len / 256;
197 pCompact->m_LenLow = len % 256; 204 pCompact->m_LenLow = len % 256;
198 pCompact->m_pBuffer = FX_Alloc(uint8_t, len); 205 pCompact->m_pBuffer = FX_Alloc(uint8_t, len);
199 FXSYS_memcpy(pCompact->m_pBuffer, pStr, len); 206 FXSYS_memcpy(pCompact->m_pBuffer, pStr, len);
200 } 207 }
201 static CFX_ByteStringC _CompactStringGet(_CompactString* pCompact) { 208 static CFX_ByteStringC _CompactStringGet(_CompactString* pCompact) {
202 if (pCompact->m_CompactLen == 0xff) { 209 if (pCompact->m_CompactLen == kHasAllocatedBufferLength) {
203 return CFX_ByteStringC(pCompact->m_pBuffer, 210 return CFX_ByteStringC(pCompact->m_pBuffer,
204 pCompact->m_LenHigh * 256 + pCompact->m_LenLow); 211 pCompact->m_LenHigh * 256 + pCompact->m_LenLow);
205 } 212 }
206 if (pCompact->m_CompactLen == 0xfe) { 213 if (pCompact->m_CompactLen == kFreeLength) {
207 return CFX_ByteStringC(); 214 return CFX_ByteStringC();
208 } 215 }
209 return CFX_ByteStringC(&pCompact->m_LenHigh, pCompact->m_CompactLen); 216 return CFX_ByteStringC(&pCompact->m_LenHigh, pCompact->m_CompactLen);
210 } 217 }
211 #define CMAP_ALLOC_STEP 8 218 #define CMAP_ALLOC_STEP 8
212 #define CMAP_INDEX_SIZE 8 219 #define CMAP_INDEX_SIZE 8
213 CFX_CMapByteStringToPtr::CFX_CMapByteStringToPtr() 220 CFX_CMapByteStringToPtr::CFX_CMapByteStringToPtr()
214 : m_Buffer(sizeof(_CompactString) + sizeof(void*), 221 : m_Buffer(sizeof(_CompactString) + sizeof(void*),
215 CMAP_ALLOC_STEP, 222 CMAP_ALLOC_STEP,
216 CMAP_INDEX_SIZE) {} 223 CMAP_INDEX_SIZE) {}
217 CFX_CMapByteStringToPtr::~CFX_CMapByteStringToPtr() { 224 CFX_CMapByteStringToPtr::~CFX_CMapByteStringToPtr() {
218 RemoveAll(); 225 RemoveAll();
219 } 226 }
220 void CFX_CMapByteStringToPtr::RemoveAll() { 227 void CFX_CMapByteStringToPtr::RemoveAll() {
221 int size = m_Buffer.GetSize(); 228 int size = m_Buffer.GetSize();
222 for (int i = 0; i < size; i++) { 229 for (int i = 0; i < size; i++) {
223 _CompactStringRelease((_CompactString*)m_Buffer.GetAt(i)); 230 _CompactStringRelease((_CompactString*)m_Buffer.GetAt(i));
224 } 231 }
225 m_Buffer.RemoveAll(); 232 m_Buffer.RemoveAll();
226 } 233 }
227 FX_POSITION CFX_CMapByteStringToPtr::GetStartPosition() const { 234 FX_POSITION CFX_CMapByteStringToPtr::GetStartPosition() const {
228 int size = m_Buffer.GetSize(); 235 int size = m_Buffer.GetSize();
229 for (int i = 0; i < size; i++) { 236 for (int i = 0; i < size; i++) {
230 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); 237 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i);
231 if (pKey->m_CompactLen != 0xfe) { 238 if (pKey->m_CompactLen != kFreeLength) {
232 return (FX_POSITION)(uintptr_t)(i + 1); 239 return (FX_POSITION)(uintptr_t)(i + 1);
233 } 240 }
234 } 241 }
235 return NULL; 242 return NULL;
236 } 243 }
237 void CFX_CMapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition, 244 void CFX_CMapByteStringToPtr::GetNextAssoc(FX_POSITION& rNextPosition,
238 CFX_ByteString& rKey, 245 CFX_ByteString& rKey,
239 void*& rValue) const { 246 void*& rValue) const {
240 if (!rNextPosition) { 247 if (!rNextPosition) {
241 return; 248 return;
242 } 249 }
243 int index = (int)(uintptr_t)rNextPosition - 1; 250 int index = (int)(uintptr_t)rNextPosition - 1;
244 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); 251 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
245 rKey = _CompactStringGet(pKey); 252 rKey = _CompactStringGet(pKey);
246 rValue = *(void**)(pKey + 1); 253 rValue = *(void**)(pKey + 1);
247 index++; 254 index++;
248 int size = m_Buffer.GetSize(); 255 int size = m_Buffer.GetSize();
249 while (index < size) { 256 while (index < size) {
250 pKey = (_CompactString*)m_Buffer.GetAt(index); 257 pKey = (_CompactString*)m_Buffer.GetAt(index);
251 if (pKey->m_CompactLen != 0xfe) { 258 if (pKey->m_CompactLen != kFreeLength) {
252 rNextPosition = (FX_POSITION)(uintptr_t)(index + 1); 259 rNextPosition = (FX_POSITION)(uintptr_t)(index + 1);
253 return; 260 return;
254 } 261 }
255 index++; 262 index++;
256 } 263 }
257 rNextPosition = NULL; 264 rNextPosition = NULL;
258 } 265 }
259 void* CFX_CMapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const { 266 void* CFX_CMapByteStringToPtr::GetNextValue(FX_POSITION& rNextPosition) const {
260 if (!rNextPosition) { 267 if (!rNextPosition) {
261 return NULL; 268 return NULL;
262 } 269 }
263 int index = (int)(uintptr_t)rNextPosition - 1; 270 int index = (int)(uintptr_t)rNextPosition - 1;
264 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); 271 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
265 void* rValue = *(void**)(pKey + 1); 272 void* rValue = *(void**)(pKey + 1);
266 index++; 273 index++;
267 int size = m_Buffer.GetSize(); 274 int size = m_Buffer.GetSize();
268 while (index < size) { 275 while (index < size) {
269 pKey = (_CompactString*)m_Buffer.GetAt(index); 276 pKey = (_CompactString*)m_Buffer.GetAt(index);
270 if (pKey->m_CompactLen != 0xfe) { 277 if (pKey->m_CompactLen != kFreeLength) {
271 rNextPosition = (FX_POSITION)(uintptr_t)(index + 1); 278 rNextPosition = (FX_POSITION)(uintptr_t)(index + 1);
272 return rValue; 279 return rValue;
273 } 280 }
274 index++; 281 index++;
275 } 282 }
276 rNextPosition = NULL; 283 rNextPosition = NULL;
277 return rValue; 284 return rValue;
278 } 285 }
279 FX_BOOL _CMapLookupCallback(void* param, void* pData) { 286 FX_BOOL _CMapLookupCallback(void* param, void* pData) {
280 return !_CompactStringSame((_CompactString*)pData, 287 return !_CompactStringSame((_CompactString*)pData,
(...skipping 16 matching lines...) Expand all
297 for (int index = 0; index < size; index++) { 304 for (int index = 0; index < size; index++) {
298 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); 305 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
299 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) { 306 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) {
300 continue; 307 continue;
301 } 308 }
302 *(void**)(pKey + 1) = value; 309 *(void**)(pKey + 1) = value;
303 return; 310 return;
304 } 311 }
305 for (int index = 0; index < size; index++) { 312 for (int index = 0; index < size; index++) {
306 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); 313 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
307 if (pKey->m_CompactLen) { 314 if (pKey->m_CompactLen != kFreeLength) {
308 continue; 315 continue;
309 } 316 }
310 _CompactStringStore(pKey, key.GetPtr(), key_len); 317 _CompactStringStore(pKey, key.GetPtr(), key_len);
311 *(void**)(pKey + 1) = value; 318 *(void**)(pKey + 1) = value;
312 return; 319 return;
313 } 320 }
314 _CompactString* pKey = (_CompactString*)m_Buffer.Add(); 321 _CompactString* pKey = (_CompactString*)m_Buffer.Add();
315 _CompactStringStore(pKey, key.GetPtr(), key_len); 322 _CompactStringStore(pKey, key.GetPtr(), key_len);
316 *(void**)(pKey + 1) = value; 323 *(void**)(pKey + 1) = value;
317 } 324 }
318 void CFX_CMapByteStringToPtr::AddValue(const CFX_ByteStringC& key, 325 void CFX_CMapByteStringToPtr::AddValue(const CFX_ByteStringC& key,
319 void* value) { 326 void* value) {
320 ASSERT(value); 327 ASSERT(value);
321 _CompactString* pKey = (_CompactString*)m_Buffer.Add(); 328 _CompactString* pKey = (_CompactString*)m_Buffer.Add();
322 _CompactStringStore(pKey, key.GetPtr(), key.GetLength()); 329 _CompactStringStore(pKey, key.GetPtr(), key.GetLength());
323 *(void**)(pKey + 1) = value; 330 *(void**)(pKey + 1) = value;
324 } 331 }
325 void CFX_CMapByteStringToPtr::RemoveKey(const CFX_ByteStringC& key) { 332 void CFX_CMapByteStringToPtr::RemoveKey(const CFX_ByteStringC& key) {
326 int key_len = key.GetLength(); 333 int key_len = key.GetLength();
327 int size = m_Buffer.GetSize(); 334 int size = m_Buffer.GetSize();
328 for (int index = 0; index < size; index++) { 335 for (int index = 0; index < size; index++) {
329 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index); 336 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(index);
330 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) { 337 if (!_CompactStringSame(pKey, key.GetPtr(), key_len)) {
331 continue; 338 continue;
332 } 339 }
333 _CompactStringRelease(pKey); 340 _CompactStringRelease(pKey);
334 pKey->m_CompactLen = 0xfe; 341 pKey->m_CompactLen = kFreeLength;
335 return; 342 return;
336 } 343 }
337 } 344 }
338 int CFX_CMapByteStringToPtr::GetCount() const { 345 int CFX_CMapByteStringToPtr::GetCount() const {
339 int count = 0; 346 int count = 0;
340 int size = m_Buffer.GetSize(); 347 int size = m_Buffer.GetSize();
341 for (int i = 0; i < size; i++) { 348 for (int i = 0; i < size; i++) {
342 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i); 349 _CompactString* pKey = (_CompactString*)m_Buffer.GetAt(i);
343 if (pKey->m_CompactLen != 0xfe) { 350 if (pKey->m_CompactLen != kFreeLength) {
344 count++; 351 count++;
345 } 352 }
346 } 353 }
347 return count; 354 return count;
348 } 355 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698