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_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
8 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf); | 8 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf); |
9 CFX_BinaryBuf::CFX_BinaryBuf() | 9 CFX_BinaryBuf::CFX_BinaryBuf() |
10 : m_AllocStep(0) | 10 : m_AllocStep(0) |
11 , m_pBuffer(NULL) | 11 , m_pBuffer(NULL) |
12 , m_DataSize(0) | 12 , m_DataSize(0) |
13 , m_AllocSize(0) | 13 , m_AllocSize(0) |
14 { | 14 { |
15 } | 15 } |
16 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) | 16 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) |
17 : m_AllocStep(0) | 17 : m_AllocStep(0) |
18 , m_DataSize(size) | 18 , m_DataSize(size) |
(...skipping 24 matching lines...) Expand all Loading... |
43 m_DataSize = 0; | 43 m_DataSize = 0; |
44 m_pBuffer = NULL; | 44 m_pBuffer = NULL; |
45 m_AllocSize = 0; | 45 m_AllocSize = 0; |
46 } | 46 } |
47 void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size) | 47 void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size) |
48 { | 48 { |
49 if (m_pBuffer) { | 49 if (m_pBuffer) { |
50 FX_Free(m_pBuffer); | 50 FX_Free(m_pBuffer); |
51 } | 51 } |
52 m_DataSize = size; | 52 m_DataSize = size; |
53 m_pBuffer = (FX_LPBYTE)buffer; | 53 m_pBuffer = (uint8_t*)buffer; |
54 m_AllocSize = size; | 54 m_AllocSize = size; |
55 } | 55 } |
56 void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other) | 56 void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other) |
57 { | 57 { |
58 AttachData(other.GetBuffer(), other.GetSize()); | 58 AttachData(other.GetBuffer(), other.GetSize()); |
59 other.DetachBuffer(); | 59 other.DetachBuffer(); |
60 } | 60 } |
61 void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) | 61 void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) |
62 { | 62 { |
63 m_AllocStep = step; | 63 m_AllocStep = step; |
(...skipping 11 matching lines...) Expand all Loading... |
75 int alloc_step; | 75 int alloc_step; |
76 if (m_AllocStep == 0) { | 76 if (m_AllocStep == 0) { |
77 alloc_step = m_AllocSize / 4; | 77 alloc_step = m_AllocSize / 4; |
78 if (alloc_step < 128 ) { | 78 if (alloc_step < 128 ) { |
79 alloc_step = 128; | 79 alloc_step = 128; |
80 } | 80 } |
81 } else { | 81 } else { |
82 alloc_step = m_AllocStep; | 82 alloc_step = m_AllocStep; |
83 } | 83 } |
84 new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step; | 84 new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step; |
85 FX_LPBYTE pNewBuffer = m_pBuffer; | 85 uint8_t* pNewBuffer = m_pBuffer; |
86 if (pNewBuffer) { | 86 if (pNewBuffer) { |
87 pNewBuffer = FX_Realloc(uint8_t, m_pBuffer, new_size); | 87 pNewBuffer = FX_Realloc(uint8_t, m_pBuffer, new_size); |
88 } else { | 88 } else { |
89 pNewBuffer = FX_Alloc(uint8_t, new_size); | 89 pNewBuffer = FX_Alloc(uint8_t, new_size); |
90 } | 90 } |
91 m_pBuffer = pNewBuffer; | 91 m_pBuffer = pNewBuffer; |
92 m_AllocSize = new_size; | 92 m_AllocSize = new_size; |
93 } | 93 } |
94 void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size) | 94 void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size) |
95 { | 95 { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 } | 195 } |
196 CFX_WideTextBuf& CFX_WideTextBuf::operator << (int i) | 196 CFX_WideTextBuf& CFX_WideTextBuf::operator << (int i) |
197 { | 197 { |
198 char buf[32]; | 198 char buf[32]; |
199 FXSYS_itoa(i, buf, 10); | 199 FXSYS_itoa(i, buf, 10); |
200 FX_STRSIZE len = FXSYS_strlen(buf); | 200 FX_STRSIZE len = FXSYS_strlen(buf); |
201 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { | 201 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { |
202 ExpandBuf(len * sizeof(FX_WCHAR)); | 202 ExpandBuf(len * sizeof(FX_WCHAR)); |
203 } | 203 } |
204 ASSERT(m_pBuffer != NULL); | 204 ASSERT(m_pBuffer != NULL); |
205 FX_LPWSTR str = (FX_WCHAR*)(m_pBuffer + m_DataSize); | 205 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); |
206 for (FX_STRSIZE j = 0; j < len; j ++) { | 206 for (FX_STRSIZE j = 0; j < len; j ++) { |
207 *str ++ = buf[j]; | 207 *str ++ = buf[j]; |
208 } | 208 } |
209 m_DataSize += len * sizeof(FX_WCHAR); | 209 m_DataSize += len * sizeof(FX_WCHAR); |
210 return *this; | 210 return *this; |
211 } | 211 } |
212 CFX_WideTextBuf& CFX_WideTextBuf::operator << (double f) | 212 CFX_WideTextBuf& CFX_WideTextBuf::operator << (double f) |
213 { | 213 { |
214 char buf[32]; | 214 char buf[32]; |
215 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); | 215 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); |
216 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { | 216 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { |
217 ExpandBuf(len * sizeof(FX_WCHAR)); | 217 ExpandBuf(len * sizeof(FX_WCHAR)); |
218 } | 218 } |
219 ASSERT(m_pBuffer != NULL); | 219 ASSERT(m_pBuffer != NULL); |
220 FX_LPWSTR str = (FX_WCHAR*)(m_pBuffer + m_DataSize); | 220 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); |
221 for (FX_STRSIZE i = 0; i < len; i ++) { | 221 for (FX_STRSIZE i = 0; i < len; i ++) { |
222 *str ++ = buf[i]; | 222 *str ++ = buf[i]; |
223 } | 223 } |
224 m_DataSize += len * sizeof(FX_WCHAR); | 224 m_DataSize += len * sizeof(FX_WCHAR); |
225 return *this; | 225 return *this; |
226 } | 226 } |
227 CFX_WideTextBuf& CFX_WideTextBuf::operator << (FX_LPCWSTR lpsz) | 227 CFX_WideTextBuf& CFX_WideTextBuf::operator << (const FX_WCHAR* lpsz) |
228 { | 228 { |
229 AppendBlock(lpsz, FXSYS_wcslen(lpsz)*sizeof(FX_WCHAR)); | 229 AppendBlock(lpsz, FXSYS_wcslen(lpsz)*sizeof(FX_WCHAR)); |
230 return *this; | 230 return *this; |
231 } | 231 } |
232 CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideTextBuf& buf) | 232 CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideTextBuf& buf) |
233 { | 233 { |
234 AppendBlock(buf.m_pBuffer, buf.m_DataSize); | 234 AppendBlock(buf.m_pBuffer, buf.m_DataSize); |
235 return *this; | 235 return *this; |
236 } | 236 } |
237 void CFX_WideTextBuf::operator =(FX_WSTR str) | 237 void CFX_WideTextBuf::operator =(FX_WSTR str) |
238 { | 238 { |
239 CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); | 239 CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); |
240 } | 240 } |
241 CFX_WideStringC CFX_WideTextBuf::GetWideString() const | 241 CFX_WideStringC CFX_WideTextBuf::GetWideString() const |
242 { | 242 { |
243 return CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_WCHAR))
; | 243 return CFX_WideStringC((const FX_WCHAR*)m_pBuffer, m_DataSize / sizeof(FX_WC
HAR)); |
244 } | 244 } |
245 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (uint8_t i) | 245 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (uint8_t i) |
246 { | 246 { |
247 if (m_pStream) { | 247 if (m_pStream) { |
248 m_pStream->WriteBlock(&i, 1); | 248 m_pStream->WriteBlock(&i, 1); |
249 } else { | 249 } else { |
250 m_SavingBuf.AppendByte(i); | 250 m_SavingBuf.AppendByte(i); |
251 } | 251 } |
252 return *this; | 252 return *this; |
253 } | 253 } |
(...skipping 29 matching lines...) Expand all Loading... |
283 int len = bstr.GetLength(); | 283 int len = bstr.GetLength(); |
284 if (m_pStream) { | 284 if (m_pStream) { |
285 m_pStream->WriteBlock(&len, sizeof(int)); | 285 m_pStream->WriteBlock(&len, sizeof(int)); |
286 m_pStream->WriteBlock(bstr.GetPtr(), len); | 286 m_pStream->WriteBlock(bstr.GetPtr(), len); |
287 } else { | 287 } else { |
288 m_SavingBuf.AppendBlock(&len, sizeof(int)); | 288 m_SavingBuf.AppendBlock(&len, sizeof(int)); |
289 m_SavingBuf.AppendBlock(bstr.GetPtr(), len); | 289 m_SavingBuf.AppendBlock(bstr.GetPtr(), len); |
290 } | 290 } |
291 return *this; | 291 return *this; |
292 } | 292 } |
293 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_LPCWSTR wstr) | 293 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (const FX_WCHAR* wstr) |
294 { | 294 { |
295 FX_STRSIZE len = FXSYS_wcslen(wstr); | 295 FX_STRSIZE len = FXSYS_wcslen(wstr); |
296 if (m_pStream) { | 296 if (m_pStream) { |
297 m_pStream->WriteBlock(&len, sizeof(int)); | 297 m_pStream->WriteBlock(&len, sizeof(int)); |
298 m_pStream->WriteBlock(wstr, len); | 298 m_pStream->WriteBlock(wstr, len); |
299 } else { | 299 } else { |
300 m_SavingBuf.AppendBlock(&len, sizeof(int)); | 300 m_SavingBuf.AppendBlock(&len, sizeof(int)); |
301 m_SavingBuf.AppendBlock(wstr, len); | 301 m_SavingBuf.AppendBlock(wstr, len); |
302 } | 302 } |
303 return *this; | 303 return *this; |
304 } | 304 } |
305 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (const CFX_WideString& wstr) | 305 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (const CFX_WideString& wstr) |
306 { | 306 { |
307 CFX_ByteString encoded = wstr.UTF16LE_Encode(); | 307 CFX_ByteString encoded = wstr.UTF16LE_Encode(); |
308 return operator << (encoded); | 308 return operator << (encoded); |
309 } | 309 } |
310 void CFX_ArchiveSaver::Write(const void* pData, FX_STRSIZE dwSize) | 310 void CFX_ArchiveSaver::Write(const void* pData, FX_STRSIZE dwSize) |
311 { | 311 { |
312 if (m_pStream) { | 312 if (m_pStream) { |
313 m_pStream->WriteBlock(pData, dwSize); | 313 m_pStream->WriteBlock(pData, dwSize); |
314 } else { | 314 } else { |
315 m_SavingBuf.AppendBlock(pData, dwSize); | 315 m_SavingBuf.AppendBlock(pData, dwSize); |
316 } | 316 } |
317 } | 317 } |
318 CFX_ArchiveLoader::CFX_ArchiveLoader(FX_LPCBYTE pData, FX_DWORD dwSize) | 318 CFX_ArchiveLoader::CFX_ArchiveLoader(const uint8_t* pData, FX_DWORD dwSize) |
319 { | 319 { |
320 m_pLoadingBuf = pData; | 320 m_pLoadingBuf = pData; |
321 m_LoadingPos = 0; | 321 m_LoadingPos = 0; |
322 m_LoadingSize = dwSize; | 322 m_LoadingSize = dwSize; |
323 } | 323 } |
324 FX_BOOL CFX_ArchiveLoader::IsEOF() | 324 FX_BOOL CFX_ArchiveLoader::IsEOF() |
325 { | 325 { |
326 return m_LoadingPos >= m_LoadingSize; | 326 return m_LoadingPos >= m_LoadingSize; |
327 } | 327 } |
328 CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (uint8_t& i) | 328 CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (uint8_t& i) |
(...skipping 23 matching lines...) Expand all Loading... |
352 { | 352 { |
353 if (m_LoadingPos + 4 > m_LoadingSize) { | 353 if (m_LoadingPos + 4 > m_LoadingSize) { |
354 return *this; | 354 return *this; |
355 } | 355 } |
356 int len; | 356 int len; |
357 operator >> (len); | 357 operator >> (len); |
358 str.Empty(); | 358 str.Empty(); |
359 if (len <= 0 || m_LoadingPos + len > m_LoadingSize) { | 359 if (len <= 0 || m_LoadingPos + len > m_LoadingSize) { |
360 return *this; | 360 return *this; |
361 } | 361 } |
362 FX_LPSTR buffer = str.GetBuffer(len); | 362 FX_CHAR* buffer = str.GetBuffer(len); |
363 FXSYS_memcpy32(buffer, m_pLoadingBuf + m_LoadingPos, len); | 363 FXSYS_memcpy32(buffer, m_pLoadingBuf + m_LoadingPos, len); |
364 str.ReleaseBuffer(len); | 364 str.ReleaseBuffer(len); |
365 m_LoadingPos += len; | 365 m_LoadingPos += len; |
366 return *this; | 366 return *this; |
367 } | 367 } |
368 CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (CFX_WideString& str) | 368 CFX_ArchiveLoader& CFX_ArchiveLoader::operator >> (CFX_WideString& str) |
369 { | 369 { |
370 CFX_ByteString encoded; | 370 CFX_ByteString encoded; |
371 operator >> (encoded); | 371 operator >> (encoded); |
372 str = CFX_WideString::FromUTF16LE((const unsigned short*)encoded.c_str(), en
coded.GetLength()); | 372 str = CFX_WideString::FromUTF16LE((const unsigned short*)encoded.c_str(), en
coded.GetLength()); |
373 return *this; | 373 return *this; |
374 } | 374 } |
375 FX_BOOL CFX_ArchiveLoader::Read(void* pBuf, FX_DWORD dwSize) | 375 FX_BOOL CFX_ArchiveLoader::Read(void* pBuf, FX_DWORD dwSize) |
376 { | 376 { |
377 if (m_LoadingPos + dwSize > m_LoadingSize) { | 377 if (m_LoadingPos + dwSize > m_LoadingSize) { |
378 return FALSE; | 378 return FALSE; |
379 } | 379 } |
380 FXSYS_memcpy32(pBuf, m_pLoadingBuf + m_LoadingPos, dwSize); | 380 FXSYS_memcpy32(pBuf, m_pLoadingBuf + m_LoadingPos, dwSize); |
381 m_LoadingPos += dwSize; | 381 m_LoadingPos += dwSize; |
382 return TRUE; | 382 return TRUE; |
383 } | 383 } |
384 void CFX_BitStream::Init(FX_LPCBYTE pData, FX_DWORD dwSize) | 384 void CFX_BitStream::Init(const uint8_t* pData, FX_DWORD dwSize) |
385 { | 385 { |
386 m_pData = pData; | 386 m_pData = pData; |
387 m_BitSize = dwSize * 8; | 387 m_BitSize = dwSize * 8; |
388 m_BitPos = 0; | 388 m_BitPos = 0; |
389 } | 389 } |
390 void CFX_BitStream::ByteAlign() | 390 void CFX_BitStream::ByteAlign() |
391 { | 391 { |
392 int mod = m_BitPos % 8; | 392 int mod = m_BitPos % 8; |
393 if (mod == 0) { | 393 if (mod == 0) { |
394 return; | 394 return; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 return bRet; | 448 return bRet; |
449 } | 449 } |
450 int32_t IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) | 450 int32_t IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) |
451 { | 451 { |
452 if (!pBuf || size < 1) { | 452 if (!pBuf || size < 1) { |
453 return 0; | 453 return 0; |
454 } | 454 } |
455 if (!m_pBuffer) { | 455 if (!m_pBuffer) { |
456 m_pBuffer = FX_Alloc(uint8_t, m_BufSize); | 456 m_pBuffer = FX_Alloc(uint8_t, m_BufSize); |
457 } | 457 } |
458 FX_LPBYTE buffer = (FX_LPBYTE)pBuf; | 458 uint8_t* buffer = (uint8_t*)pBuf; |
459 FX_STRSIZE temp_size = (FX_STRSIZE)size; | 459 FX_STRSIZE temp_size = (FX_STRSIZE)size; |
460 while (temp_size > 0) { | 460 while (temp_size > 0) { |
461 FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size
); | 461 FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size
); |
462 FXSYS_memcpy32(m_pBuffer + m_Length, buffer, buf_size); | 462 FXSYS_memcpy32(m_pBuffer + m_Length, buffer, buf_size); |
463 m_Length += buf_size; | 463 m_Length += buf_size; |
464 if (m_Length == m_BufSize) { | 464 if (m_Length == m_BufSize) { |
465 if (!Flush()) { | 465 if (!Flush()) { |
466 return -1; | 466 return -1; |
467 } | 467 } |
468 } | 468 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 if (!pFile) { | 509 if (!pFile) { |
510 return FALSE; | 510 return FALSE; |
511 } | 511 } |
512 if (m_pFile && m_bTakeover) { | 512 if (m_pFile && m_bTakeover) { |
513 m_pFile->Release(); | 513 m_pFile->Release(); |
514 } | 514 } |
515 m_pFile = pFile; | 515 m_pFile = pFile; |
516 m_bTakeover = bTakeover; | 516 m_bTakeover = bTakeover; |
517 return TRUE; | 517 return TRUE; |
518 } | 518 } |
519 FX_BOOL CFX_FileBufferArchive::AttachFile(FX_LPCWSTR filename) | 519 FX_BOOL CFX_FileBufferArchive::AttachFile(const FX_WCHAR* filename) |
520 { | 520 { |
521 if (!filename) { | 521 if (!filename) { |
522 return FALSE; | 522 return FALSE; |
523 } | 523 } |
524 if (m_pFile && m_bTakeover) { | 524 if (m_pFile && m_bTakeover) { |
525 m_pFile->Release(); | 525 m_pFile->Release(); |
526 } | 526 } |
527 m_pFile = FX_CreateFileWrite(filename); | 527 m_pFile = FX_CreateFileWrite(filename); |
528 if (!m_pFile) { | 528 if (!m_pFile) { |
529 return FALSE; | 529 return FALSE; |
530 } | 530 } |
531 m_bTakeover = TRUE; | 531 m_bTakeover = TRUE; |
532 return TRUE; | 532 return TRUE; |
533 } | 533 } |
534 FX_BOOL CFX_FileBufferArchive::AttachFile(FX_LPCSTR filename) | 534 FX_BOOL CFX_FileBufferArchive::AttachFile(const FX_CHAR* filename) |
535 { | 535 { |
536 if (!filename) { | 536 if (!filename) { |
537 return FALSE; | 537 return FALSE; |
538 } | 538 } |
539 if (m_pFile && m_bTakeover) { | 539 if (m_pFile && m_bTakeover) { |
540 m_pFile->Release(); | 540 m_pFile->Release(); |
541 } | 541 } |
542 m_pFile = FX_CreateFileWrite(filename); | 542 m_pFile = FX_CreateFileWrite(filename); |
543 if (!m_pFile) { | 543 if (!m_pFile) { |
544 return FALSE; | 544 return FALSE; |
545 } | 545 } |
546 m_bTakeover = TRUE; | 546 m_bTakeover = TRUE; |
547 return TRUE; | 547 return TRUE; |
548 } | 548 } |
549 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) | 549 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) |
550 { | 550 { |
551 if (!m_pFile) { | 551 if (!m_pFile) { |
552 return FALSE; | 552 return FALSE; |
553 } | 553 } |
554 if (!pBuf || size < 1) { | 554 if (!pBuf || size < 1) { |
555 return TRUE; | 555 return TRUE; |
556 } | 556 } |
557 return m_pFile->WriteBlock(pBuf, size); | 557 return m_pFile->WriteBlock(pBuf, size); |
558 } | 558 } |
OLD | NEW |