lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /************************************************************************
|
| 2 | * Id: buffer.c *
|
| 3 | * *
|
| 4 | * TR069 Project: A TR069 library in C *
|
| 5 | * Copyright (C) 2013-2014 netcwmp group *
|
| 6 | * *
|
| 7 | * *
|
| 8 | * Email: netcwmp ( & ) gmail dot com *
|
| 9 | * *
|
| 10 | ***********************************************************************/
|
| 11 | #include "cwmp/buffer.h"
|
| 12 | #include "cwmp/util.h"
|
| 13 | #include "cwmp/memory.h"
|
| 14 | #include "cwmp/log.h"
|
| 15 | //#include "cwmp_private.h"
|
| 16 |
|
| 17 | void cwmp_buffer_clear(void * data)
|
| 18 | {
|
| 19 | cwmp_buffer_t * b;
|
| 20 | //FUNCTION_TRACE();
|
| 21 | if (!data)
|
| 22 | return;
|
| 23 | b = (cwmp_buffer_t*)data;
|
| 24 | b->writed = 0;
|
| 25 | b->readed = 0;
|
| 26 | b->offset = b->string;
|
| 27 | b->size = DEFAULT_BUFFER_SIZE;
|
| 28 | b->next = 0;
|
| 29 | b->string[0] = 0;
|
| 30 | }
|
| 31 |
|
| 32 |
|
| 33 | int cwmp_buffer_create(cwmp_buffer_t **news, pool_t * pool)
|
| 34 | {
|
| 35 | (*news) = PMALLOC(sizeof(cwmp_buffer_t));
|
| 36 |
|
| 37 | if ((*news) == NULL)
|
| 38 | {
|
| 39 | return CWMP_ENOMEM;
|
| 40 | }
|
| 41 | (*news)->size = DEFAULT_BUFFER_SIZE;
|
| 42 | (*news)->writed = 0;
|
| 43 | (*news)->readed = 0;
|
| 44 | (*news)->offset = (*news)->string;
|
| 45 | (*news)->next = 0;
|
| 46 | (*news)->string[0] = 0;
|
| 47 |
|
| 48 | pool_cleanup_add(pool, cwmp_buffer_clear, *news);
|
| 49 |
|
| 50 | return CWMP_OK;
|
| 51 | }
|
| 52 |
|
| 53 | int cwmp_buffer_init(cwmp_buffer_t * b)
|
| 54 | {
|
| 55 | b->writed = 0;
|
| 56 | b->readed = 0;
|
| 57 | b->size = DEFAULT_BUFFER_SIZE;
|
| 58 | b->offset = b->string;
|
| 59 | b->next = 0;
|
| 60 | b->string[0] = 0;
|
| 61 | return CWMP_OK;
|
| 62 | }
|
| 63 |
|
| 64 | void cwmp_buffer_free(cwmp_buffer_t * b, pool_t * pool)
|
| 65 | {
|
| 66 | if (b == NULL)
|
| 67 | return;
|
| 68 |
|
| 69 | PFREE(b);
|
| 70 |
|
| 71 | }
|
| 72 |
|
| 73 |
|
| 74 | char * cwmp_buffer_current(cwmp_buffer_t * b)
|
| 75 | {
|
| 76 | return b->string + b->writed;
|
| 77 | }
|
| 78 |
|
| 79 | char * cwmp_buffer_string(cwmp_buffer_t * b)
|
| 80 | {
|
| 81 | return b->string;
|
| 82 | }
|
| 83 |
|
| 84 | size_t cwmp_buffer_length(cwmp_buffer_t * b)
|
| 85 | {
|
| 86 | return b == NULL ? 0: (b->writed - b->readed) ;
|
| 87 | }
|
| 88 |
|
| 89 | size_t cwmp_buffer_size(cwmp_buffer_t * b)
|
| 90 | {
|
| 91 | return b == NULL ? 0: b->size;
|
| 92 | }
|
| 93 |
|
| 94 | int cwmp_buffer_remain(cwmp_buffer_t * b)
|
| 95 | {
|
| 96 | return b == NULL ? 0: (b->size - b->writed);
|
| 97 | }
|
| 98 |
|
| 99 | size_t cwmp_chunk_length(cwmp_chunk_t * c)
|
| 100 | {
|
| 101 | return c == NULL? 0 : c->bytes;
|
| 102 | }
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 | void cwmp_buffer_write_position(cwmp_buffer_t * b, size_t pos, const void * val, cwmp_uint32_t len)
|
| 108 | {
|
| 109 | memcpy(b->string + pos, val, len);
|
| 110 | if (pos + len > b->writed)
|
| 111 | {
|
| 112 | /* b->writed = pos + len; */
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | void cwmp_buffer_write_bytes(cwmp_buffer_t * b, const void * val, size_t len)
|
| 117 | {
|
| 118 | if (len > (b->size - b->writed))
|
| 119 | return;
|
| 120 | memcpy(b->offset, val, len);
|
| 121 | b->offset += len;
|
| 122 | b->writed += len;
|
| 123 | }
|
| 124 |
|
| 125 | void cwmp_buffer_write_uint64(cwmp_buffer_t * b, cwmp_uint64_t val)
|
| 126 | {
|
| 127 | cwmp_buffer_write_bytes(b, &val, 8);
|
| 128 | }
|
| 129 |
|
| 130 | void cwmp_buffer_write_uint32(cwmp_buffer_t * b, cwmp_uint32_t val)
|
| 131 | {
|
| 132 | cwmp_buffer_write_bytes(b, &val, 4);
|
| 133 | }
|
| 134 |
|
| 135 | void cwmp_buffer_write_uint16(cwmp_buffer_t * b, cwmp_uint16_t val)
|
| 136 | {
|
| 137 | cwmp_buffer_write_bytes(b, &val, 2);
|
| 138 | }
|
| 139 |
|
| 140 | void cwmp_buffer_write_uint8(cwmp_buffer_t * b, cwmp_byte_t val)
|
| 141 | {
|
| 142 | cwmp_buffer_write_bytes(b, &val, 1);
|
| 143 | }
|
| 144 |
|
| 145 | void cwmp_buffer_write_format_string(cwmp_buffer_t * b, const char * fmt, ...)
|
| 146 | {
|
| 147 | va_list ap = {0};
|
| 148 | int len;
|
| 149 | va_start(ap, fmt);
|
| 150 | #ifdef WIN32
|
| 151 | len = _vsnprintf(cwmp_buffer_current(b), b->size - b->writed, fmt, ap);
|
| 152 | #else
|
| 153 | len =vsnprintf(cwmp_buffer_current(b), b->size - b->writed, fmt, ap);
|
| 154 | #endif
|
| 155 | b->offset += len;
|
| 156 | b->writed += len;
|
| 157 | va_end(ap);
|
| 158 | }
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 | void cwmp_buffer_write_string(cwmp_buffer_t * b, const char * str, size_t len)
|
| 163 | {
|
| 164 | cwmp_buffer_write_bytes(b, str, len);
|
| 165 | }
|
| 166 |
|
| 167 | void cwmp_buffer_clone(cwmp_buffer_t * b, const cwmp_buffer_t * cb)
|
| 168 | {
|
| 169 | size_t len = cb->writed;
|
| 170 | cwmp_buffer_write_bytes(b, cb->string, len);
|
| 171 | }
|
| 172 |
|
| 173 | int cwmp_buffer_read_bytes(void * val, cwmp_buffer_t * b, const cwmp_uint32_t len)
|
| 174 | {
|
| 175 | if (len > cwmp_buffer_length(b))
|
| 176 | {
|
| 177 | return CWMP_ERROR;
|
| 178 | }
|
| 179 | else
|
| 180 | {
|
| 181 | memcpy(val, b->string + b->readed, len);
|
| 182 | b->readed += len;
|
| 183 | return CWMP_OK;
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | int cwmp_buffer_read_uint8(cwmp_byte_t * val, cwmp_buffer_t * b)
|
| 188 | {
|
| 189 | return cwmp_buffer_read_bytes((void *)val,b,1);
|
| 190 | }
|
| 191 |
|
| 192 |
|
| 193 | int cwmp_buffer_read_uint16(cwmp_uint16_t * val, cwmp_buffer_t * b)
|
| 194 | {
|
| 195 | cwmp_uint16_t v;
|
| 196 | if (cwmp_buffer_read_bytes((void *)&v,b,2) != CWMP_OK)
|
| 197 | {
|
| 198 | return CWMP_ERROR;
|
| 199 | }
|
| 200 | else
|
| 201 | {
|
| 202 | (*val) = v;
|
| 203 | return CWMP_OK;
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | int cwmp_buffer_read_uint32(cwmp_uint32_t * val, cwmp_buffer_t * b)
|
| 208 | {
|
| 209 | cwmp_uint32_t v;
|
| 210 | if (cwmp_buffer_read_bytes((void *)&v,b,4) != CWMP_OK)
|
| 211 | {
|
| 212 | return CWMP_ERROR;
|
| 213 | }
|
| 214 | else
|
| 215 | {
|
| 216 | (*val) = v;
|
| 217 | return CWMP_OK;
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | int cwmp_chunk_create(cwmp_chunk_t ** news, pool_t * pool)
|
| 222 | {
|
| 223 | (*news) = PMALLOC(sizeof(cwmp_chunk_t));
|
| 224 | if (!(*news))
|
| 225 | {
|
| 226 | cwmp_log_error("create array buffer out of memory\n");
|
| 227 | return CWMP_ENOMEM;
|
| 228 | }
|
| 229 | (*news)->buffer = NULL;
|
| 230 | (*news)->current = NULL;
|
| 231 | (*news)->bytes = (*news)->count = (*news)->used = 0;
|
| 232 |
|
| 233 | /*
|
| 234 | if (cwmp_buffer_create(&buffer, pool) != CWMP_OK)
|
| 235 | {
|
| 236 | cwmp_log_error("create buffer out of memory\n");
|
| 237 | PFREE(*news);
|
| 238 | return CWMP_ENOMEM;
|
| 239 | }
|
| 240 | (*news)->buffer = (*news)->current = buffer;
|
| 241 | */
|
| 242 |
|
| 243 | pool_cleanup_add(pool, (pool_cleanup_handler)cwmp_chunk_clear, (*news));
|
| 244 | return CWMP_OK;
|
| 245 | }
|
| 246 |
|
| 247 | void cwmp_chunk_print(cwmp_chunk_t * cb)
|
| 248 | {
|
| 249 | cwmp_buffer_t * b = cb->buffer;
|
| 250 | FUNCTION_TRACE();
|
| 251 | while (b)
|
| 252 | {
|
| 253 | //fwrite(cwmp_buffer_string(b), 1, cwmp_buffer_length(b), out);
|
| 254 | cwmp_log_info("%s", cwmp_buffer_string(b));
|
| 255 | b = b->next;
|
| 256 | }
|
| 257 |
|
| 258 | }
|
| 259 |
|
| 260 |
|
| 261 | int cwmp_chunk_write_string(cwmp_chunk_t * cb, const char * str, size_t length, pool_t * pool)
|
| 262 | {
|
| 263 | cwmp_buffer_t *buffer;
|
| 264 | size_t len, leftbytes, writebytes, writed;
|
| 265 | //FUNCTION_TRACE();
|
| 266 | writed = 0;
|
| 267 | leftbytes = length;
|
| 268 |
|
| 269 | while (leftbytes>0)
|
| 270 | {
|
| 271 | len = cwmp_buffer_remain(cb->current);
|
| 272 |
|
| 273 | if (len <= 0)
|
| 274 | {
|
| 275 | if (cb->current && cb->current->next)
|
| 276 | {
|
| 277 | cb->current = cb->current->next;
|
| 278 | cwmp_buffer_clear(cb->current);
|
| 279 | }
|
| 280 | else
|
| 281 | {
|
| 282 | if (cwmp_buffer_create(&buffer, pool) != CWMP_OK)
|
| 283 | {
|
| 284 | cwmp_log_error("create buffer out of memory\n");
|
| 285 | return CWMP_ERROR;
|
| 286 | }
|
| 287 | if (cb->current != NULL)
|
| 288 | {
|
| 289 | cb->current->next = buffer;
|
| 290 | }
|
| 291 | else
|
| 292 | {
|
| 293 | cb->buffer = buffer;
|
| 294 | }
|
| 295 | cb->current = buffer;
|
| 296 | }
|
| 297 |
|
| 298 |
|
| 299 | len=cwmp_buffer_remain(cb->current);
|
| 300 |
|
| 301 | }
|
| 302 |
|
| 303 |
|
| 304 | writebytes = leftbytes > len ? len :leftbytes;
|
| 305 |
|
| 306 | cwmp_buffer_write_string(cb->current, str + writed, writebytes);
|
| 307 | writed += writebytes;
|
| 308 |
|
| 309 | cb->bytes += writebytes;
|
| 310 | leftbytes -= writebytes;
|
| 311 | /*if (leftbytes > 0)
|
| 312 | {
|
| 313 | if (cwmp_buffer_create(&buffer, pool) != CWMP_OK)
|
| 314 | {
|
| 315 | cwmp_log_error("create buffer out of memory\n");
|
| 316 | return CWMP_ERROR;
|
| 317 | }
|
| 318 | cb->current->next = buffer;
|
| 319 | cb->current = buffer;
|
| 320 | }
|
| 321 | else
|
| 322 | {
|
| 323 | break;
|
| 324 | }
|
| 325 | */
|
| 326 | }
|
| 327 |
|
| 328 | return writed;
|
| 329 | }
|
| 330 |
|
| 331 |
|
| 332 | int cwmp_chunk_copy(char * dest, const cwmp_chunk_t * cb, size_t max_length)
|
| 333 | {
|
| 334 | size_t bufleft, destwrited, writebytes;
|
| 335 | size_t b,d;
|
| 336 | char * ptr = dest;
|
| 337 | cwmp_buffer_t * buffer = cb->buffer;
|
| 338 | bufleft = cb->bytes;
|
| 339 | destwrited = 0;
|
| 340 | while ((destwrited <= max_length) && (bufleft > 0) && (buffer!=NULL))
|
| 341 | {
|
| 342 | b = bufleft > cwmp_buffer_length(buffer) ? cwmp_buffer_length(buffer) : bufleft;
|
| 343 | d = max_length - destwrited;
|
| 344 | writebytes = d > b ? b : d;
|
| 345 | strncpy(ptr, cwmp_buffer_string(buffer), writebytes);
|
| 346 | ptr += writebytes;
|
| 347 | destwrited += writebytes;
|
| 348 | bufleft -= writebytes;
|
| 349 | buffer = buffer->next;
|
| 350 | }
|
| 351 |
|
| 352 | dest[destwrited] = 0;
|
| 353 | return destwrited;
|
| 354 | }
|
| 355 |
|
| 356 | void cwmp_chunk_init(cwmp_chunk_t * cb)
|
| 357 | {
|
| 358 | FUNCTION_TRACE();
|
| 359 |
|
| 360 | cb->buffer = NULL;
|
| 361 | cb->bytes = 0;
|
| 362 | cb->count = 0;
|
| 363 | cb->used = 0;
|
| 364 | cb->current = cb->buffer;
|
| 365 | }
|
| 366 |
|
| 367 | void cwmp_chunk_clear(cwmp_chunk_t * cb)
|
| 368 | {
|
| 369 | //FUNCTION_TRACE();
|
| 370 |
|
| 371 | cb->buffer = NULL;
|
| 372 | cb->bytes = 0;
|
| 373 | cb->count = 0;
|
| 374 | cb->used = 0;
|
| 375 | cb->current = cb->buffer;
|
| 376 | }
|
| 377 |
|
| 378 |
|
| 379 | int cwmp_chunk_release(cwmp_chunk_t * cb, int remain, pool_t * pool)
|
| 380 | {
|
| 381 | int i;
|
| 382 | cwmp_buffer_t * next;
|
| 383 | cwmp_buffer_t * buffer = cb->buffer;
|
| 384 | for (i=0; i<remain && buffer; i++)
|
| 385 | {
|
| 386 | buffer = buffer->next;
|
| 387 | }
|
| 388 |
|
| 389 |
|
| 390 | while (buffer)
|
| 391 | {
|
| 392 | next = buffer->next;
|
| 393 | cwmp_buffer_free(buffer, pool);
|
| 394 | buffer = next;
|
| 395 | }
|
| 396 | return 0;
|
| 397 | }
|