| 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 <stddef.h> // For offsetof(). |
| 8 |
| 7 #include "../../include/fxcrt/fx_basic.h" | 9 #include "../../include/fxcrt/fx_basic.h" |
| 8 #include "../../../third_party/base/numerics/safe_math.h" | 10 #include "../../../third_party/base/numerics/safe_math.h" |
| 9 | 11 |
| 10 static CFX_StringDataW* FX_AllocStringW(int nLen) | 12 static CFX_StringDataW* FX_AllocStringW(int nLen) |
| 11 { | 13 { |
| 12 // TODO(palmer): |nLen| should really be declared as |size_t|, or | 14 // TODO(palmer): |nLen| should really be declared as |size_t|, or |
| 13 // at least unsigned. | 15 // at least unsigned. |
| 14 if (nLen == 0 || nLen < 0) { | 16 if (nLen == 0 || nLen < 0) { |
| 15 return NULL; | 17 return NULL; |
| 16 } | 18 } |
| 17 | 19 |
| 18 int overhead = 3 * sizeof(long) + sizeof(FX_WCHAR); // +WCHAR is for NUL. | 20 // Fixed portion of header plus a NUL wide char not in m_nAllocLength. |
| 21 int overhead = offsetof(CFX_StringDataW, m_String) + sizeof(FX_WCHAR); |
| 19 pdfium::base::CheckedNumeric<int> iSize = nLen; | 22 pdfium::base::CheckedNumeric<int> iSize = nLen; |
| 20 iSize *= sizeof(FX_WCHAR); | 23 iSize *= sizeof(FX_WCHAR); |
| 21 iSize += overhead; | 24 iSize += overhead; |
| 22 | 25 |
| 23 // Now round to an 8-byte boundary. We'd expect that this is the minimum | 26 // Now round to an 8-byte boundary. We'd expect that this is the minimum |
| 24 // granularity of any of the underlying allocators, so there may be cases | 27 // granularity of any of the underlying allocators, so there may be cases |
| 25 // where we can save a re-alloc when adding a few characters to a string | 28 // where we can save a re-alloc when adding a few characters to a string |
| 26 // by using this otherwise wasted space. | 29 // by using this otherwise wasted space. |
| 27 iSize += 7; | 30 iSize += 7; |
| 28 int totalSize = iSize.ValueOrDie() & ~7; | 31 int totalSize = iSize.ValueOrDie() & ~7; |
| (...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1116 return (CFX_CharMap*)&g_DefaultJISMapper; | 1119 return (CFX_CharMap*)&g_DefaultJISMapper; |
| 1117 case 936: | 1120 case 936: |
| 1118 return (CFX_CharMap*)&g_DefaultGBKMapper; | 1121 return (CFX_CharMap*)&g_DefaultGBKMapper; |
| 1119 case 949: | 1122 case 949: |
| 1120 return (CFX_CharMap*)&g_DefaultUHCMapper; | 1123 return (CFX_CharMap*)&g_DefaultUHCMapper; |
| 1121 case 950: | 1124 case 950: |
| 1122 return (CFX_CharMap*)&g_DefaultBig5Mapper; | 1125 return (CFX_CharMap*)&g_DefaultBig5Mapper; |
| 1123 } | 1126 } |
| 1124 return NULL; | 1127 return NULL; |
| 1125 } | 1128 } |
| OLD | NEW |