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

Side by Side Diff: core/include/fxcrt/fx_basic.h

Issue 1719493002: Revert "Use safe arithmentic in CFX_BinaryBuf::ExpandBuf." (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « no previous file | core/include/fxcrt/fx_safe_types.h » ('j') | 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 #ifndef CORE_INCLUDE_FXCRT_FX_BASIC_H_ 7 #ifndef CORE_INCLUDE_FXCRT_FX_BASIC_H_
8 #define CORE_INCLUDE_FXCRT_FX_BASIC_H_ 8 #define CORE_INCLUDE_FXCRT_FX_BASIC_H_
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <memory>
12 11
13 #include "core/include/fxcrt/fx_memory.h" 12 #include "core/include/fxcrt/fx_memory.h"
14 #include "core/include/fxcrt/fx_stream.h" 13 #include "core/include/fxcrt/fx_stream.h"
15 #include "core/include/fxcrt/fx_string.h" 14 #include "core/include/fxcrt/fx_string.h"
16 #include "core/include/fxcrt/fx_system.h" 15 #include "core/include/fxcrt/fx_system.h"
17 16
18 // The FX_ArraySize(arr) macro returns the # of elements in an array arr. 17 // The FX_ArraySize(arr) macro returns the # of elements in an array arr.
19 // The expression is a compile-time constant, and therefore can be 18 // The expression is a compile-time constant, and therefore can be
20 // used in defining new arrays, for example. If you use FX_ArraySize on 19 // used in defining new arrays, for example. If you use FX_ArraySize on
21 // a pointer by mistake, you will get a compile-time error. 20 // a pointer by mistake, you will get a compile-time error.
22 // 21 //
23 // One caveat is that FX_ArraySize() doesn't accept any array of an 22 // One caveat is that FX_ArraySize() doesn't accept any array of an
24 // anonymous type or a type defined inside a function. 23 // anonymous type or a type defined inside a function.
25 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array))) 24 #define FX_ArraySize(array) (sizeof(ArraySizeHelper(array)))
26 25
27 // This template function declaration is used in defining FX_ArraySize. 26 // This template function declaration is used in defining FX_ArraySize.
28 // Note that the function doesn't need an implementation, as we only 27 // Note that the function doesn't need an implementation, as we only
29 // use its type. 28 // use its type.
30 template <typename T, size_t N> 29 template <typename T, size_t N>
31 char(&ArraySizeHelper(T(&array)[N]))[N]; 30 char(&ArraySizeHelper(T(&array)[N]))[N];
32 31
33 // Used with std::unique_ptr to FX_Free raw memory.
34 struct FxFreeDeleter {
35 inline void operator()(void* ptr) const { FX_Free(ptr); }
36 };
37
38 // Used with std::unique_ptr to Release() objects that can't be deleted.
39 template <class T>
40 struct ReleaseDeleter {
41 inline void operator()(T* ptr) const { ptr->Release(); }
42 };
43
44 class CFX_BinaryBuf { 32 class CFX_BinaryBuf {
45 public: 33 public:
46 CFX_BinaryBuf(); 34 CFX_BinaryBuf();
47 explicit CFX_BinaryBuf(FX_STRSIZE size); 35 CFX_BinaryBuf(FX_STRSIZE size);
48 36
49 uint8_t* GetBuffer() const { return m_pBuffer.get(); } 37 ~CFX_BinaryBuf();
50 FX_STRSIZE GetSize() const { return m_DataSize; }
51 38
52 void Clear(); 39 void Clear();
40
53 void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0); 41 void EstimateSize(FX_STRSIZE size, FX_STRSIZE alloc_step = 0);
42
54 void AppendBlock(const void* pBuf, FX_STRSIZE size); 43 void AppendBlock(const void* pBuf, FX_STRSIZE size);
44
45 void AppendFill(uint8_t byte, FX_STRSIZE count);
46
55 void AppendString(const CFX_ByteStringC& str) { 47 void AppendString(const CFX_ByteStringC& str) {
56 AppendBlock(str.GetPtr(), str.GetLength()); 48 AppendBlock(str.GetPtr(), str.GetLength());
57 } 49 }
58 50
59 void AppendByte(uint8_t byte) { 51 inline void AppendByte(uint8_t byte) {
60 ExpandBuf(1); 52 if (m_AllocSize <= m_DataSize) {
61 m_pBuffer.get()[m_DataSize++] = byte; 53 ExpandBuf(1);
54 }
55 m_pBuffer[m_DataSize++] = byte;
62 } 56 }
63 57
64 void InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size); 58 void InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE size);
59
60 void AttachData(void* pBuf, FX_STRSIZE size);
61
62 void CopyData(const void* pBuf, FX_STRSIZE size);
63
64 void TakeOver(CFX_BinaryBuf& other);
65
65 void Delete(int start_index, int count); 66 void Delete(int start_index, int count);
66 67
67 // Takes ownership of |pBuf|. 68 uint8_t* GetBuffer() const { return m_pBuffer; }
68 void AttachData(uint8_t* pBuf, FX_STRSIZE size);
69 69
70 // Releases ownership of |m_pBuffer| and returns it. 70 FX_STRSIZE GetSize() const { return m_DataSize; }
71 uint8_t* DetachBuffer(); 71
72 CFX_ByteStringC GetByteString() const;
73
74 void DetachBuffer();
72 75
73 protected: 76 protected:
77 FX_STRSIZE m_AllocStep;
78
79 uint8_t* m_pBuffer;
80
81 FX_STRSIZE m_DataSize;
82
83 FX_STRSIZE m_AllocSize;
84
74 void ExpandBuf(FX_STRSIZE size); 85 void ExpandBuf(FX_STRSIZE size);
75
76 FX_STRSIZE m_AllocStep;
77 FX_STRSIZE m_AllocSize;
78 FX_STRSIZE m_DataSize;
79 std::unique_ptr<uint8_t, FxFreeDeleter> m_pBuffer;
80 }; 86 };
81
82 class CFX_ByteTextBuf : public CFX_BinaryBuf { 87 class CFX_ByteTextBuf : public CFX_BinaryBuf {
83 public: 88 public:
89 void operator=(const CFX_ByteStringC& str);
90
84 void AppendChar(int ch) { AppendByte((uint8_t)ch); } 91 void AppendChar(int ch) { AppendByte((uint8_t)ch); }
85 FX_STRSIZE GetLength() const { return m_DataSize; }
86 CFX_ByteStringC GetByteString() const;
87 92
88 CFX_ByteTextBuf& operator<<(int i); 93 CFX_ByteTextBuf& operator<<(int i);
94
89 CFX_ByteTextBuf& operator<<(FX_DWORD i); 95 CFX_ByteTextBuf& operator<<(FX_DWORD i);
96
90 CFX_ByteTextBuf& operator<<(double f); 97 CFX_ByteTextBuf& operator<<(double f);
98
91 CFX_ByteTextBuf& operator<<(const CFX_ByteStringC& lpsz); 99 CFX_ByteTextBuf& operator<<(const CFX_ByteStringC& lpsz);
100
92 CFX_ByteTextBuf& operator<<(const CFX_ByteTextBuf& buf); 101 CFX_ByteTextBuf& operator<<(const CFX_ByteTextBuf& buf);
102
103 FX_STRSIZE GetLength() const { return m_DataSize; }
93 }; 104 };
94
95 class CFX_WideTextBuf : public CFX_BinaryBuf { 105 class CFX_WideTextBuf : public CFX_BinaryBuf {
96 public: 106 public:
107 void operator=(const FX_WCHAR* lpsz);
108
109 void operator=(const CFX_WideStringC& str);
110
97 void AppendChar(FX_WCHAR wch); 111 void AppendChar(FX_WCHAR wch);
112
113 CFX_WideTextBuf& operator<<(int i);
114
115 CFX_WideTextBuf& operator<<(double f);
116
117 CFX_WideTextBuf& operator<<(const FX_WCHAR* lpsz);
118
119 CFX_WideTextBuf& operator<<(const CFX_WideStringC& str);
120 CFX_WideTextBuf& operator<<(const CFX_WideString& str);
121
122 CFX_WideTextBuf& operator<<(const CFX_WideTextBuf& buf);
123
98 FX_STRSIZE GetLength() const { return m_DataSize / sizeof(FX_WCHAR); } 124 FX_STRSIZE GetLength() const { return m_DataSize / sizeof(FX_WCHAR); }
99 FX_WCHAR* GetBuffer() const { 125
100 return reinterpret_cast<FX_WCHAR*>(m_pBuffer.get()); 126 FX_WCHAR* GetBuffer() const { return (FX_WCHAR*)m_pBuffer; }
101 }
102 CFX_WideStringC GetWideString() const;
103 127
104 void Delete(int start_index, int count) { 128 void Delete(int start_index, int count) {
105 CFX_BinaryBuf::Delete(start_index * sizeof(FX_WCHAR), 129 CFX_BinaryBuf::Delete(start_index * sizeof(FX_WCHAR),
106 count * sizeof(FX_WCHAR)); 130 count * sizeof(FX_WCHAR));
107 } 131 }
108 132
109 CFX_WideTextBuf& operator<<(int i); 133 CFX_WideStringC GetWideString() const;
110 CFX_WideTextBuf& operator<<(double f);
111 CFX_WideTextBuf& operator<<(const FX_WCHAR* lpsz);
112 CFX_WideTextBuf& operator<<(const CFX_WideStringC& str);
113 CFX_WideTextBuf& operator<<(const CFX_WideString& str);
114 CFX_WideTextBuf& operator<<(const CFX_WideTextBuf& buf);
115 }; 134 };
116
117 #ifdef PDF_ENABLE_XFA 135 #ifdef PDF_ENABLE_XFA
118 class CFX_ArchiveSaver { 136 class CFX_ArchiveSaver {
119 public: 137 public:
120 CFX_ArchiveSaver() : m_pStream(NULL) {} 138 CFX_ArchiveSaver() : m_pStream(NULL) {}
121 139
122 CFX_ArchiveSaver& operator<<(uint8_t i); 140 CFX_ArchiveSaver& operator<<(uint8_t i);
123 141
124 CFX_ArchiveSaver& operator<<(int i); 142 CFX_ArchiveSaver& operator<<(int i);
125 143
126 CFX_ArchiveSaver& operator<<(FX_DWORD i); 144 CFX_ArchiveSaver& operator<<(FX_DWORD i);
(...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 public: 1024 public:
1007 explicit CFX_AutoRestorer(T* location) 1025 explicit CFX_AutoRestorer(T* location)
1008 : m_Location(location), m_OldValue(*location) {} 1026 : m_Location(location), m_OldValue(*location) {}
1009 ~CFX_AutoRestorer() { *m_Location = m_OldValue; } 1027 ~CFX_AutoRestorer() { *m_Location = m_OldValue; }
1010 1028
1011 private: 1029 private:
1012 T* const m_Location; 1030 T* const m_Location;
1013 const T m_OldValue; 1031 const T m_OldValue;
1014 }; 1032 };
1015 1033
1034 struct FxFreeDeleter {
1035 inline void operator()(void* ptr) const { FX_Free(ptr); }
1036 };
1037
1038 // Used with std::unique_ptr to Release() objects that can't be deleted.
1039 template <class T>
1040 struct ReleaseDeleter {
1041 inline void operator()(T* ptr) const { ptr->Release(); }
1042 };
1043
1016 #define FX_DATALIST_LENGTH 1024 1044 #define FX_DATALIST_LENGTH 1024
1017 template <size_t unit> 1045 template <size_t unit>
1018 class CFX_SortListArray { 1046 class CFX_SortListArray {
1019 protected: 1047 protected:
1020 struct DataList { 1048 struct DataList {
1021 int32_t start; 1049 int32_t start;
1022 1050
1023 int32_t count; 1051 int32_t count;
1024 uint8_t* data; 1052 uint8_t* data;
1025 }; 1053 };
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
1191 FX_FLOAT c; 1219 FX_FLOAT c;
1192 FX_FLOAT d; 1220 FX_FLOAT d;
1193 FX_FLOAT e; 1221 FX_FLOAT e;
1194 FX_FLOAT f; 1222 FX_FLOAT f;
1195 FX_FLOAT g; 1223 FX_FLOAT g;
1196 FX_FLOAT h; 1224 FX_FLOAT h;
1197 FX_FLOAT i; 1225 FX_FLOAT i;
1198 }; 1226 };
1199 1227
1200 #endif // CORE_INCLUDE_FXCRT_FX_BASIC_H_ 1228 #endif // CORE_INCLUDE_FXCRT_FX_BASIC_H_
OLDNEW
« no previous file with comments | « no previous file | core/include/fxcrt/fx_safe_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698