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

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

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