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_CHAR* 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), m_pBuffer(NULL), m_DataSize(0), m_AllocSize(0) {} |
11 , m_pBuffer(NULL) | |
12 , m_DataSize(0) | |
13 , m_AllocSize(0) | |
14 { | |
15 } | |
16 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) | 11 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size) |
17 : m_AllocStep(0) | 12 : m_AllocStep(0), m_DataSize(size), m_AllocSize(size) { |
18 , m_DataSize(size) | 13 m_pBuffer = FX_Alloc(uint8_t, size); |
19 , m_AllocSize(size) | 14 } |
20 { | 15 CFX_BinaryBuf::~CFX_BinaryBuf() { |
21 m_pBuffer = FX_Alloc(uint8_t, size); | 16 if (m_pBuffer) { |
22 } | 17 FX_Free(m_pBuffer); |
23 CFX_BinaryBuf::~CFX_BinaryBuf() | 18 } |
24 { | 19 } |
25 if (m_pBuffer) { | 20 void CFX_BinaryBuf::Delete(int start_index, int count) { |
26 FX_Free(m_pBuffer); | 21 if (!m_pBuffer || start_index < 0 || start_index + count > m_DataSize) { |
| 22 return; |
| 23 } |
| 24 FXSYS_memmove(m_pBuffer + start_index, m_pBuffer + start_index + count, |
| 25 m_DataSize - start_index - count); |
| 26 m_DataSize -= count; |
| 27 } |
| 28 void CFX_BinaryBuf::Clear() { |
| 29 m_DataSize = 0; |
| 30 } |
| 31 void CFX_BinaryBuf::DetachBuffer() { |
| 32 m_DataSize = 0; |
| 33 m_pBuffer = NULL; |
| 34 m_AllocSize = 0; |
| 35 } |
| 36 void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size) { |
| 37 if (m_pBuffer) { |
| 38 FX_Free(m_pBuffer); |
| 39 } |
| 40 m_DataSize = size; |
| 41 m_pBuffer = (uint8_t*)buffer; |
| 42 m_AllocSize = size; |
| 43 } |
| 44 void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other) { |
| 45 AttachData(other.GetBuffer(), other.GetSize()); |
| 46 other.DetachBuffer(); |
| 47 } |
| 48 void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) { |
| 49 m_AllocStep = step; |
| 50 if (m_AllocSize >= size) { |
| 51 return; |
| 52 } |
| 53 ExpandBuf(size - m_DataSize); |
| 54 } |
| 55 void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) { |
| 56 FX_STRSIZE new_size = add_size + m_DataSize; |
| 57 if (m_AllocSize >= new_size) { |
| 58 return; |
| 59 } |
| 60 int alloc_step; |
| 61 if (m_AllocStep == 0) { |
| 62 alloc_step = m_AllocSize / 4; |
| 63 if (alloc_step < 128) { |
| 64 alloc_step = 128; |
27 } | 65 } |
28 } | 66 } else { |
29 void CFX_BinaryBuf::Delete(int start_index, int count) | 67 alloc_step = m_AllocStep; |
30 { | 68 } |
31 if (!m_pBuffer || start_index < 0 || start_index + count > m_DataSize) { | 69 new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step; |
32 return; | 70 uint8_t* pNewBuffer = m_pBuffer; |
| 71 if (pNewBuffer) { |
| 72 pNewBuffer = FX_Realloc(uint8_t, m_pBuffer, new_size); |
| 73 } else { |
| 74 pNewBuffer = FX_Alloc(uint8_t, new_size); |
| 75 } |
| 76 m_pBuffer = pNewBuffer; |
| 77 m_AllocSize = new_size; |
| 78 } |
| 79 void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size) { |
| 80 if (size == 0) { |
| 81 m_DataSize = 0; |
| 82 return; |
| 83 } |
| 84 if (m_AllocSize < size) { |
| 85 ExpandBuf(size - m_DataSize); |
| 86 } |
| 87 if (!m_pBuffer) { |
| 88 return; |
| 89 } |
| 90 FXSYS_memcpy(m_pBuffer, pStr, size); |
| 91 m_DataSize = size; |
| 92 } |
| 93 void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) { |
| 94 ExpandBuf(size); |
| 95 if (pBuf && m_pBuffer) { |
| 96 FXSYS_memcpy(m_pBuffer + m_DataSize, pBuf, size); |
| 97 } |
| 98 m_DataSize += size; |
| 99 } |
| 100 void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, |
| 101 const void* pBuf, |
| 102 FX_STRSIZE size) { |
| 103 ExpandBuf(size); |
| 104 if (!m_pBuffer) { |
| 105 return; |
| 106 } |
| 107 FXSYS_memmove(m_pBuffer + pos + size, m_pBuffer + pos, m_DataSize - pos); |
| 108 if (pBuf) { |
| 109 FXSYS_memcpy(m_pBuffer + pos, pBuf, size); |
| 110 } |
| 111 m_DataSize += size; |
| 112 } |
| 113 void CFX_BinaryBuf::AppendFill(uint8_t byte, FX_STRSIZE count) { |
| 114 ExpandBuf(count); |
| 115 if (!m_pBuffer) { |
| 116 return; |
| 117 } |
| 118 FXSYS_memset(m_pBuffer + m_DataSize, byte, count); |
| 119 m_DataSize += count; |
| 120 } |
| 121 CFX_ByteStringC CFX_BinaryBuf::GetByteString() const { |
| 122 return CFX_ByteStringC(m_pBuffer, m_DataSize); |
| 123 } |
| 124 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteStringC& lpsz) { |
| 125 AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); |
| 126 return *this; |
| 127 } |
| 128 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(int i) { |
| 129 char buf[32]; |
| 130 FXSYS_itoa(i, buf, 10); |
| 131 AppendBlock(buf, FXSYS_strlen(buf)); |
| 132 return *this; |
| 133 } |
| 134 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(FX_DWORD i) { |
| 135 char buf[32]; |
| 136 FXSYS_itoa(i, buf, 10); |
| 137 AppendBlock(buf, FXSYS_strlen(buf)); |
| 138 return *this; |
| 139 } |
| 140 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(double f) { |
| 141 char buf[32]; |
| 142 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); |
| 143 AppendBlock(buf, len); |
| 144 return *this; |
| 145 } |
| 146 CFX_ByteTextBuf& CFX_ByteTextBuf::operator<<(const CFX_ByteTextBuf& buf) { |
| 147 AppendBlock(buf.m_pBuffer, buf.m_DataSize); |
| 148 return *this; |
| 149 } |
| 150 void CFX_ByteTextBuf::operator=(const CFX_ByteStringC& str) { |
| 151 CopyData(str.GetPtr(), str.GetLength()); |
| 152 } |
| 153 void CFX_WideTextBuf::AppendChar(FX_WCHAR ch) { |
| 154 if (m_AllocSize < m_DataSize + (FX_STRSIZE)sizeof(FX_WCHAR)) { |
| 155 ExpandBuf(sizeof(FX_WCHAR)); |
| 156 } |
| 157 ASSERT(m_pBuffer != NULL); |
| 158 *(FX_WCHAR*)(m_pBuffer + m_DataSize) = ch; |
| 159 m_DataSize += sizeof(FX_WCHAR); |
| 160 } |
| 161 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideStringC& str) { |
| 162 AppendBlock(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); |
| 163 return *this; |
| 164 } |
| 165 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideString& str) { |
| 166 AppendBlock(str.c_str(), str.GetLength() * sizeof(FX_WCHAR)); |
| 167 return *this; |
| 168 } |
| 169 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(int i) { |
| 170 char buf[32]; |
| 171 FXSYS_itoa(i, buf, 10); |
| 172 FX_STRSIZE len = FXSYS_strlen(buf); |
| 173 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { |
| 174 ExpandBuf(len * sizeof(FX_WCHAR)); |
| 175 } |
| 176 ASSERT(m_pBuffer != NULL); |
| 177 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); |
| 178 for (FX_STRSIZE j = 0; j < len; j++) { |
| 179 *str++ = buf[j]; |
| 180 } |
| 181 m_DataSize += len * sizeof(FX_WCHAR); |
| 182 return *this; |
| 183 } |
| 184 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(double f) { |
| 185 char buf[32]; |
| 186 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); |
| 187 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { |
| 188 ExpandBuf(len * sizeof(FX_WCHAR)); |
| 189 } |
| 190 ASSERT(m_pBuffer != NULL); |
| 191 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); |
| 192 for (FX_STRSIZE i = 0; i < len; i++) { |
| 193 *str++ = buf[i]; |
| 194 } |
| 195 m_DataSize += len * sizeof(FX_WCHAR); |
| 196 return *this; |
| 197 } |
| 198 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const FX_WCHAR* lpsz) { |
| 199 AppendBlock(lpsz, FXSYS_wcslen(lpsz) * sizeof(FX_WCHAR)); |
| 200 return *this; |
| 201 } |
| 202 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(const CFX_WideTextBuf& buf) { |
| 203 AppendBlock(buf.m_pBuffer, buf.m_DataSize); |
| 204 return *this; |
| 205 } |
| 206 void CFX_WideTextBuf::operator=(const CFX_WideStringC& str) { |
| 207 CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); |
| 208 } |
| 209 CFX_WideStringC CFX_WideTextBuf::GetWideString() const { |
| 210 return CFX_WideStringC((const FX_WCHAR*)m_pBuffer, |
| 211 m_DataSize / sizeof(FX_WCHAR)); |
| 212 } |
| 213 void CFX_BitStream::Init(const uint8_t* pData, FX_DWORD dwSize) { |
| 214 m_pData = pData; |
| 215 m_BitSize = dwSize * 8; |
| 216 m_BitPos = 0; |
| 217 } |
| 218 void CFX_BitStream::ByteAlign() { |
| 219 int mod = m_BitPos % 8; |
| 220 if (mod == 0) { |
| 221 return; |
| 222 } |
| 223 m_BitPos += 8 - mod; |
| 224 } |
| 225 FX_DWORD CFX_BitStream::GetBits(FX_DWORD nBits) { |
| 226 if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize) { |
| 227 return 0; |
| 228 } |
| 229 if (nBits == 1) { |
| 230 int bit = (m_pData[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0; |
| 231 m_BitPos++; |
| 232 return bit; |
| 233 } |
| 234 FX_DWORD byte_pos = m_BitPos / 8; |
| 235 FX_DWORD bit_pos = m_BitPos % 8, bit_left = nBits; |
| 236 FX_DWORD result = 0; |
| 237 if (bit_pos) { |
| 238 if (8 - bit_pos >= bit_left) { |
| 239 result = |
| 240 (m_pData[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - bit_left); |
| 241 m_BitPos += bit_left; |
| 242 return result; |
33 } | 243 } |
34 FXSYS_memmove(m_pBuffer + start_index, m_pBuffer + start_index + count, m_Da
taSize - start_index - count); | 244 bit_left -= 8 - bit_pos; |
35 m_DataSize -= count; | 245 result = (m_pData[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; |
36 } | 246 } |
37 void CFX_BinaryBuf::Clear() | 247 while (bit_left >= 8) { |
38 { | 248 bit_left -= 8; |
39 m_DataSize = 0; | 249 result |= m_pData[byte_pos++] << bit_left; |
40 } | 250 } |
41 void CFX_BinaryBuf::DetachBuffer() | 251 if (bit_left) { |
42 { | 252 result |= m_pData[byte_pos] >> (8 - bit_left); |
43 m_DataSize = 0; | 253 } |
| 254 m_BitPos += nBits; |
| 255 return result; |
| 256 } |
| 257 IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size) |
| 258 : m_BufSize(size), m_pBuffer(NULL), m_Length(0) {} |
| 259 void IFX_BufferArchive::Clear() { |
| 260 m_Length = 0; |
| 261 if (m_pBuffer) { |
| 262 FX_Free(m_pBuffer); |
44 m_pBuffer = NULL; | 263 m_pBuffer = NULL; |
45 m_AllocSize = 0; | 264 } |
46 } | 265 } |
47 void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size) | 266 FX_BOOL IFX_BufferArchive::Flush() { |
48 { | 267 FX_BOOL bRet = DoWork(m_pBuffer, m_Length); |
49 if (m_pBuffer) { | 268 m_Length = 0; |
50 FX_Free(m_pBuffer); | 269 return bRet; |
| 270 } |
| 271 int32_t IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) { |
| 272 if (!pBuf || size < 1) { |
| 273 return 0; |
| 274 } |
| 275 if (!m_pBuffer) { |
| 276 m_pBuffer = FX_Alloc(uint8_t, m_BufSize); |
| 277 } |
| 278 uint8_t* buffer = (uint8_t*)pBuf; |
| 279 FX_STRSIZE temp_size = (FX_STRSIZE)size; |
| 280 while (temp_size > 0) { |
| 281 FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size); |
| 282 FXSYS_memcpy(m_pBuffer + m_Length, buffer, buf_size); |
| 283 m_Length += buf_size; |
| 284 if (m_Length == m_BufSize) { |
| 285 if (!Flush()) { |
| 286 return -1; |
| 287 } |
51 } | 288 } |
52 m_DataSize = size; | 289 temp_size -= buf_size; |
53 m_pBuffer = (uint8_t*)buffer; | 290 buffer += buf_size; |
54 m_AllocSize = size; | 291 } |
55 } | 292 return (int32_t)size; |
56 void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other) | 293 } |
57 { | 294 int32_t IFX_BufferArchive::AppendByte(uint8_t byte) { |
58 AttachData(other.GetBuffer(), other.GetSize()); | 295 return AppendBlock(&byte, 1); |
59 other.DetachBuffer(); | 296 } |
60 } | 297 int32_t IFX_BufferArchive::AppendDWord(FX_DWORD i) { |
61 void CFX_BinaryBuf::EstimateSize(FX_STRSIZE size, FX_STRSIZE step) | 298 char buf[32]; |
62 { | 299 FXSYS_itoa(i, buf, 10); |
63 m_AllocStep = step; | 300 return AppendBlock(buf, (size_t)FXSYS_strlen(buf)); |
64 if (m_AllocSize >= size) { | 301 } |
65 return; | 302 int32_t IFX_BufferArchive::AppendString(const CFX_ByteStringC& lpsz) { |
66 } | 303 return AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); |
67 ExpandBuf(size - m_DataSize); | |
68 } | |
69 void CFX_BinaryBuf::ExpandBuf(FX_STRSIZE add_size) | |
70 { | |
71 FX_STRSIZE new_size = add_size + m_DataSize; | |
72 if (m_AllocSize >= new_size) { | |
73 return; | |
74 } | |
75 int alloc_step; | |
76 if (m_AllocStep == 0) { | |
77 alloc_step = m_AllocSize / 4; | |
78 if (alloc_step < 128 ) { | |
79 alloc_step = 128; | |
80 } | |
81 } else { | |
82 alloc_step = m_AllocStep; | |
83 } | |
84 new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step; | |
85 uint8_t* pNewBuffer = m_pBuffer; | |
86 if (pNewBuffer) { | |
87 pNewBuffer = FX_Realloc(uint8_t, m_pBuffer, new_size); | |
88 } else { | |
89 pNewBuffer = FX_Alloc(uint8_t, new_size); | |
90 } | |
91 m_pBuffer = pNewBuffer; | |
92 m_AllocSize = new_size; | |
93 } | |
94 void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size) | |
95 { | |
96 if (size == 0) { | |
97 m_DataSize = 0; | |
98 return; | |
99 } | |
100 if (m_AllocSize < size) { | |
101 ExpandBuf(size - m_DataSize); | |
102 } | |
103 if (!m_pBuffer) { | |
104 return; | |
105 } | |
106 FXSYS_memcpy(m_pBuffer, pStr, size); | |
107 m_DataSize = size; | |
108 } | |
109 void CFX_BinaryBuf::AppendBlock(const void* pBuf, FX_STRSIZE size) | |
110 { | |
111 ExpandBuf(size); | |
112 if (pBuf && m_pBuffer) { | |
113 FXSYS_memcpy(m_pBuffer + m_DataSize, pBuf, size); | |
114 } | |
115 m_DataSize += size; | |
116 } | |
117 void CFX_BinaryBuf::InsertBlock(FX_STRSIZE pos, const void* pBuf, FX_STRSIZE siz
e) | |
118 { | |
119 ExpandBuf(size); | |
120 if (!m_pBuffer) { | |
121 return; | |
122 } | |
123 FXSYS_memmove(m_pBuffer + pos + size, m_pBuffer + pos, m_DataSize - pos); | |
124 if (pBuf) { | |
125 FXSYS_memcpy(m_pBuffer + pos, pBuf, size); | |
126 } | |
127 m_DataSize += size; | |
128 } | |
129 void CFX_BinaryBuf::AppendFill(uint8_t byte, FX_STRSIZE count) | |
130 { | |
131 ExpandBuf(count); | |
132 if (!m_pBuffer) { | |
133 return; | |
134 } | |
135 FXSYS_memset(m_pBuffer + m_DataSize, byte, count); | |
136 m_DataSize += count; | |
137 } | |
138 CFX_ByteStringC CFX_BinaryBuf::GetByteString() const | |
139 { | |
140 return CFX_ByteStringC(m_pBuffer, m_DataSize); | |
141 } | |
142 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (const CFX_ByteStringC& lpsz) | |
143 { | |
144 AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); | |
145 return *this; | |
146 } | |
147 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (int i) | |
148 { | |
149 char buf[32]; | |
150 FXSYS_itoa(i, buf, 10); | |
151 AppendBlock(buf, FXSYS_strlen(buf)); | |
152 return *this; | |
153 } | |
154 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (FX_DWORD i) | |
155 { | |
156 char buf[32]; | |
157 FXSYS_itoa(i, buf, 10); | |
158 AppendBlock(buf, FXSYS_strlen(buf)); | |
159 return *this; | |
160 } | |
161 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (double f) | |
162 { | |
163 char buf[32]; | |
164 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); | |
165 AppendBlock(buf, len); | |
166 return *this; | |
167 } | |
168 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (const CFX_ByteTextBuf& buf) | |
169 { | |
170 AppendBlock(buf.m_pBuffer, buf.m_DataSize); | |
171 return *this; | |
172 } | |
173 void CFX_ByteTextBuf::operator =(const CFX_ByteStringC& str) | |
174 { | |
175 CopyData(str.GetPtr(), str.GetLength()); | |
176 } | |
177 void CFX_WideTextBuf::AppendChar(FX_WCHAR ch) | |
178 { | |
179 if (m_AllocSize < m_DataSize + (FX_STRSIZE)sizeof(FX_WCHAR)) { | |
180 ExpandBuf(sizeof(FX_WCHAR)); | |
181 } | |
182 ASSERT(m_pBuffer != NULL); | |
183 *(FX_WCHAR*)(m_pBuffer + m_DataSize) = ch; | |
184 m_DataSize += sizeof(FX_WCHAR); | |
185 } | |
186 CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideStringC& str) | |
187 { | |
188 AppendBlock(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); | |
189 return *this; | |
190 } | |
191 CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideString &str) | |
192 { | |
193 AppendBlock(str.c_str(), str.GetLength() * sizeof(FX_WCHAR)); | |
194 return *this; | |
195 } | |
196 CFX_WideTextBuf& CFX_WideTextBuf::operator << (int i) | |
197 { | |
198 char buf[32]; | |
199 FXSYS_itoa(i, buf, 10); | |
200 FX_STRSIZE len = FXSYS_strlen(buf); | |
201 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { | |
202 ExpandBuf(len * sizeof(FX_WCHAR)); | |
203 } | |
204 ASSERT(m_pBuffer != NULL); | |
205 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); | |
206 for (FX_STRSIZE j = 0; j < len; j ++) { | |
207 *str ++ = buf[j]; | |
208 } | |
209 m_DataSize += len * sizeof(FX_WCHAR); | |
210 return *this; | |
211 } | |
212 CFX_WideTextBuf& CFX_WideTextBuf::operator << (double f) | |
213 { | |
214 char buf[32]; | |
215 FX_STRSIZE len = FX_ftoa((FX_FLOAT)f, buf); | |
216 if (m_AllocSize < m_DataSize + (FX_STRSIZE)(len * sizeof(FX_WCHAR))) { | |
217 ExpandBuf(len * sizeof(FX_WCHAR)); | |
218 } | |
219 ASSERT(m_pBuffer != NULL); | |
220 FX_WCHAR* str = (FX_WCHAR*)(m_pBuffer + m_DataSize); | |
221 for (FX_STRSIZE i = 0; i < len; i ++) { | |
222 *str ++ = buf[i]; | |
223 } | |
224 m_DataSize += len * sizeof(FX_WCHAR); | |
225 return *this; | |
226 } | |
227 CFX_WideTextBuf& CFX_WideTextBuf::operator << (const FX_WCHAR* lpsz) | |
228 { | |
229 AppendBlock(lpsz, FXSYS_wcslen(lpsz)*sizeof(FX_WCHAR)); | |
230 return *this; | |
231 } | |
232 CFX_WideTextBuf& CFX_WideTextBuf::operator << (const CFX_WideTextBuf& buf) | |
233 { | |
234 AppendBlock(buf.m_pBuffer, buf.m_DataSize); | |
235 return *this; | |
236 } | |
237 void CFX_WideTextBuf::operator =(const CFX_WideStringC& str) | |
238 { | |
239 CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); | |
240 } | |
241 CFX_WideStringC CFX_WideTextBuf::GetWideString() const | |
242 { | |
243 return CFX_WideStringC((const FX_WCHAR*)m_pBuffer, m_DataSize / sizeof(FX_WC
HAR)); | |
244 } | |
245 void CFX_BitStream::Init(const uint8_t* pData, FX_DWORD dwSize) | |
246 { | |
247 m_pData = pData; | |
248 m_BitSize = dwSize * 8; | |
249 m_BitPos = 0; | |
250 } | |
251 void CFX_BitStream::ByteAlign() | |
252 { | |
253 int mod = m_BitPos % 8; | |
254 if (mod == 0) { | |
255 return; | |
256 } | |
257 m_BitPos += 8 - mod; | |
258 } | |
259 FX_DWORD CFX_BitStream::GetBits(FX_DWORD nBits) | |
260 { | |
261 if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize) { | |
262 return 0; | |
263 } | |
264 if (nBits == 1) { | |
265 int bit = (m_pData[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0; | |
266 m_BitPos ++; | |
267 return bit; | |
268 } | |
269 FX_DWORD byte_pos = m_BitPos / 8; | |
270 FX_DWORD bit_pos = m_BitPos % 8, bit_left = nBits; | |
271 FX_DWORD result = 0; | |
272 if (bit_pos) { | |
273 if (8 - bit_pos >= bit_left) { | |
274 result = (m_pData[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - b
it_left); | |
275 m_BitPos += bit_left; | |
276 return result; | |
277 } | |
278 bit_left -= 8 - bit_pos; | |
279 result = (m_pData[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; | |
280 } | |
281 while (bit_left >= 8) { | |
282 bit_left -= 8; | |
283 result |= m_pData[byte_pos++] << bit_left; | |
284 } | |
285 if (bit_left) { | |
286 result |= m_pData[byte_pos] >> (8 - bit_left); | |
287 } | |
288 m_BitPos += nBits; | |
289 return result; | |
290 } | |
291 IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size) | |
292 : m_BufSize(size) | |
293 , m_pBuffer(NULL) | |
294 , m_Length(0) | |
295 { | |
296 } | |
297 void IFX_BufferArchive::Clear() | |
298 { | |
299 m_Length = 0; | |
300 if (m_pBuffer) { | |
301 FX_Free(m_pBuffer); | |
302 m_pBuffer = NULL; | |
303 } | |
304 } | |
305 FX_BOOL IFX_BufferArchive::Flush() | |
306 { | |
307 FX_BOOL bRet = DoWork(m_pBuffer, m_Length); | |
308 m_Length = 0; | |
309 return bRet; | |
310 } | |
311 int32_t IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) | |
312 { | |
313 if (!pBuf || size < 1) { | |
314 return 0; | |
315 } | |
316 if (!m_pBuffer) { | |
317 m_pBuffer = FX_Alloc(uint8_t, m_BufSize); | |
318 } | |
319 uint8_t* buffer = (uint8_t*)pBuf; | |
320 FX_STRSIZE temp_size = (FX_STRSIZE)size; | |
321 while (temp_size > 0) { | |
322 FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size
); | |
323 FXSYS_memcpy(m_pBuffer + m_Length, buffer, buf_size); | |
324 m_Length += buf_size; | |
325 if (m_Length == m_BufSize) { | |
326 if (!Flush()) { | |
327 return -1; | |
328 } | |
329 } | |
330 temp_size -= buf_size; | |
331 buffer += buf_size; | |
332 } | |
333 return (int32_t)size; | |
334 } | |
335 int32_t IFX_BufferArchive::AppendByte(uint8_t byte) | |
336 { | |
337 return AppendBlock(&byte, 1); | |
338 } | |
339 int32_t IFX_BufferArchive::AppendDWord(FX_DWORD i) | |
340 { | |
341 char buf[32]; | |
342 FXSYS_itoa(i, buf, 10); | |
343 return AppendBlock(buf, (size_t)FXSYS_strlen(buf)); | |
344 } | |
345 int32_t IFX_BufferArchive::AppendString(const CFX_ByteStringC& lpsz) | |
346 { | |
347 return AppendBlock(lpsz.GetPtr(), lpsz.GetLength()); | |
348 } | 304 } |
349 CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size) | 305 CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size) |
350 : IFX_BufferArchive(size) | 306 : IFX_BufferArchive(size), m_pFile(NULL), m_bTakeover(FALSE) {} |
351 , m_pFile(NULL) | 307 CFX_FileBufferArchive::~CFX_FileBufferArchive() { |
352 , m_bTakeover(FALSE) | 308 Clear(); |
353 { | 309 } |
354 } | 310 void CFX_FileBufferArchive::Clear() { |
355 CFX_FileBufferArchive::~CFX_FileBufferArchive() | 311 if (m_pFile && m_bTakeover) { |
356 { | 312 m_pFile->Release(); |
357 Clear(); | 313 } |
358 } | 314 m_pFile = NULL; |
359 void CFX_FileBufferArchive::Clear() | 315 m_bTakeover = FALSE; |
360 { | 316 IFX_BufferArchive::Clear(); |
361 if (m_pFile && m_bTakeover) { | 317 } |
362 m_pFile->Release(); | 318 FX_BOOL CFX_FileBufferArchive::AttachFile(IFX_StreamWrite* pFile, |
363 } | 319 FX_BOOL bTakeover) { |
364 m_pFile = NULL; | 320 if (!pFile) { |
365 m_bTakeover = FALSE; | 321 return FALSE; |
366 IFX_BufferArchive::Clear(); | 322 } |
367 } | 323 if (m_pFile && m_bTakeover) { |
368 FX_BOOL CFX_FileBufferArchive::AttachFile(IFX_StreamWrite *pFile, FX_BOOL bTakeo
ver ) | 324 m_pFile->Release(); |
369 { | 325 } |
370 if (!pFile) { | 326 m_pFile = pFile; |
371 return FALSE; | 327 m_bTakeover = bTakeover; |
372 } | 328 return TRUE; |
373 if (m_pFile && m_bTakeover) { | 329 } |
374 m_pFile->Release(); | 330 FX_BOOL CFX_FileBufferArchive::AttachFile(const FX_WCHAR* filename) { |
375 } | 331 if (!filename) { |
376 m_pFile = pFile; | 332 return FALSE; |
377 m_bTakeover = bTakeover; | 333 } |
| 334 if (m_pFile && m_bTakeover) { |
| 335 m_pFile->Release(); |
| 336 } |
| 337 m_pFile = FX_CreateFileWrite(filename); |
| 338 if (!m_pFile) { |
| 339 return FALSE; |
| 340 } |
| 341 m_bTakeover = TRUE; |
| 342 return TRUE; |
| 343 } |
| 344 FX_BOOL CFX_FileBufferArchive::AttachFile(const FX_CHAR* filename) { |
| 345 if (!filename) { |
| 346 return FALSE; |
| 347 } |
| 348 if (m_pFile && m_bTakeover) { |
| 349 m_pFile->Release(); |
| 350 } |
| 351 m_pFile = FX_CreateFileWrite(filename); |
| 352 if (!m_pFile) { |
| 353 return FALSE; |
| 354 } |
| 355 m_bTakeover = TRUE; |
| 356 return TRUE; |
| 357 } |
| 358 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) { |
| 359 if (!m_pFile) { |
| 360 return FALSE; |
| 361 } |
| 362 if (!pBuf || size < 1) { |
378 return TRUE; | 363 return TRUE; |
379 } | 364 } |
380 FX_BOOL CFX_FileBufferArchive::AttachFile(const FX_WCHAR* filename) | 365 return m_pFile->WriteBlock(pBuf, size); |
381 { | 366 } |
382 if (!filename) { | |
383 return FALSE; | |
384 } | |
385 if (m_pFile && m_bTakeover) { | |
386 m_pFile->Release(); | |
387 } | |
388 m_pFile = FX_CreateFileWrite(filename); | |
389 if (!m_pFile) { | |
390 return FALSE; | |
391 } | |
392 m_bTakeover = TRUE; | |
393 return TRUE; | |
394 } | |
395 FX_BOOL CFX_FileBufferArchive::AttachFile(const FX_CHAR* filename) | |
396 { | |
397 if (!filename) { | |
398 return FALSE; | |
399 } | |
400 if (m_pFile && m_bTakeover) { | |
401 m_pFile->Release(); | |
402 } | |
403 m_pFile = FX_CreateFileWrite(filename); | |
404 if (!m_pFile) { | |
405 return FALSE; | |
406 } | |
407 m_bTakeover = TRUE; | |
408 return TRUE; | |
409 } | |
410 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) | |
411 { | |
412 if (!m_pFile) { | |
413 return FALSE; | |
414 } | |
415 if (!pBuf || size < 1) { | |
416 return TRUE; | |
417 } | |
418 return m_pFile->WriteBlock(pBuf, size); | |
419 } | |
OLD | NEW |