blob: dc06fc1fffcada07b971a29cfac5fd0d1ddaf130 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*==============================================================================
2
3 ds_ASBuffer.cpp
4
5GENERAL DESCRIPTION
6 A buffer class with utility functions for parsing raw bytes.
7
8EXTERNALIZED FUNCTIONS
9
10INITIALIZATION AND SEQUENCING REQUIREMENTS
11 None
12
13 Copyright (c) 2014 by Qualcomm Technologies Incorporated. All Rights Reserved.
14==============================================================================*/
15
16/*==============================================================================
17 EDIT HISTORY FOR MODULE
18
19This section contains comments describing changes made to the module.
20Notice that changes are listed in reverse chronological order.
21
22when who what, where, why
23-------- --- ----------------------------------------------------------
2406/03/15 ml Remove memory allocation on default constructor
2505/20/15 ml Use memory from modem heap
2604/21/14 ml Created file/Initial version.
27==============================================================================*/
28#include "ds_ASBuffer.h"
29
30//#include "ds_appsrv_mem.h"
31#include <string.h>
32#include <mbtk_type.h>
33#include <stdlib.h>
34
35#include <cctype> // isspace
36
37
38// temp
39#include "ds_ASString.h"
40
41#define DEFAULT_BUFFER_SIZE 256
42
43// static const uint8 EMPTY_STRING[] = "";
44
45void memscpy(void* dest, size_t destLen, const void* src, size_t srcLen) {
46 if (srcLen <= destLen)
47 memcpy(dest, src, srcLen);
48}
49
50
51ASBuffer::ASBuffer()
52: buffer(NULL), buffer_size(0), buffer_capacity(DEFAULT_BUFFER_SIZE), error_status(false)
53{ }
54
55
56
57ASBuffer::ASBuffer(uint32 capacity)
58: buffer(NULL), buffer_size(0), buffer_capacity(capacity), error_status(false)
59{ }
60
61
62
63ASBuffer::ASBuffer(ASBuffer &buf)
64: buffer(NULL), buffer_size(0), buffer_capacity(buf.buffer_capacity), error_status(false)
65{
66 init();
67 append(buf);
68}
69
70
71
72ASBuffer::ASBuffer(const ASBuffer &buf)
73: buffer(NULL), buffer_size(0), buffer_capacity(buf.buffer_capacity), error_status(false)
74{
75 init();
76 append(buf);
77}
78
79
80
81ASBuffer::ASBuffer(const uint8* buf, uint32 buf_size)
82: buffer(NULL), buffer_size(0), buffer_capacity(buf_size), error_status(false)
83{
84 init();
85 append(buf, buf_size);
86}
87
88
89
90ASBuffer::~ASBuffer()
91{
92 if(NULL != buffer)
93 {
94 free(buffer);
95 buffer = NULL;
96 }
97}
98
99
100
101ASBuffer& ASBuffer::operator=(const ASBuffer &rhs)
102{
103 clear();
104 append(rhs);
105 return *this;
106}
107
108
109
110
111void ASBuffer::init()
112{
113 if(0 == buffer_capacity)
114 {
115 error_status = true;
116 }
117 else if(NULL == buffer)
118 {
119 buffer = (uint8*)malloc(sizeof(uint8) * (buffer_capacity+1));
120 error_status = (NULL == buffer);
121 }
122}
123
124
125
126// temp name
127#define DO_NOT_DOUBLE_IF_LARGER 2147483647
128bool ASBuffer::increment_buffer_capacity(const uint32 req_size)
129{
130 uint32 new_capacity = buffer_capacity;
131 uint8* tmp = NULL;
132
133 while(req_size > new_capacity)
134 {
135 // will cause overflow if doubled
136 if(DO_NOT_DOUBLE_IF_LARGER < new_capacity)
137 return false;
138
139 new_capacity *= 2;
140 }
141
142 tmp = (uint8*)malloc(sizeof(uint8) * new_capacity+1);
143 if(NULL == tmp)
144 return false;
145
146 memset(tmp, 0, new_capacity+1);
147 memscpy(tmp, new_capacity, buffer, buffer_size);
148 free(buffer);
149 buffer = NULL;
150
151 buffer = tmp;
152 buffer_capacity = new_capacity;
153
154 return true;
155}
156
157
158
159uint8* ASBuffer::non_const_content()
160{
161 return buffer;
162}
163
164
165const uint8* ASBuffer::content() const
166{
167 return buffer;
168}
169
170
171const char* ASBuffer::c_str() const
172{
173 if(NULL == buffer)
174 return "";
175 else
176 return (const char*)buffer;
177}
178
179
180
181uint32 ASBuffer::size() const
182{
183 return buffer_size;
184}
185
186
187
188bool ASBuffer::empty() const
189{
190 return (0 == buffer_size);
191}
192
193
194
195bool ASBuffer::error() const
196{
197 return error_status;
198}
199
200
201
202void ASBuffer::clear()
203{
204 if(NULL == buffer)
205 {
206 init();
207 }
208 else
209 {
210 memset(buffer, 0, buffer_capacity+1);
211 }
212 buffer_size = 0;
213}
214
215
216
217bool ASBuffer::append(const uint8* append_buffer, const uint32 append_size)
218{
219 if(NULL == append_buffer || 0 == append_size)
220 return true;
221
222 if(NULL == buffer)
223 init();
224
225 if(error_status) // do nothing if in error state
226 return false;
227
228 uint32 new_size = buffer_size + append_size;
229 if(new_size > buffer_capacity)
230 {
231 if(!increment_buffer_capacity(new_size))
232 {
233 error_status = true;
234 return false;
235 }
236 }
237
238 memscpy(
239 buffer + buffer_size,
240 buffer_capacity - buffer_size,
241 append_buffer,
242 append_size
243 );
244 buffer_size = new_size;
245
246 return true;
247}
248
249
250
251bool ASBuffer::append(const char* append_buffer)
252{
253 if(NULL == append_buffer)
254 return false;
255 else
256 return append((uint8*)append_buffer, strlen(append_buffer));
257}
258
259
260bool ASBuffer::append(const char append_buffer)
261{
262 char buffer[1];
263 buffer[0] = append_buffer;
264
265 return append((uint8*)buffer, 1);
266}
267
268
269bool ASBuffer::append(const char* append_buffer, const uint32 size)
270{
271 if(NULL == append_buffer)
272 return false;
273 else
274 return append((uint8*)append_buffer, size);
275}
276
277
278
279bool ASBuffer::append(const ASBuffer& append_buffer)
280{
281 if(append_buffer.empty())
282 return false;
283 else
284 return append(append_buffer.content(), append_buffer.size());
285}
286
287
288
289bool ASBuffer::append(const ASString& append_buffer)
290{
291 if(append_buffer.empty())
292 return false;
293 else
294 return append((uint8*)append_buffer.c_str(), append_buffer.size());
295}
296
297