| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include <errno.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include "simplebuffer.h" |
| 28 |
#include "ref_array.h" |
| 29 |
#include "ini_configobj.h" |
| 30 |
#include "ini_config_priv.h" |
| 31 |
#include "ini_comment.h" |
| 32 |
#include "ini_defines.h" |
| 33 |
#include "ini_valueobj.h" |
| 34 |
#include "trace.h" |
| 35 |
|
| 36 |
struct value_obj { |
| 37 |
struct ref_array *raw_lines; |
| 38 |
struct ref_array *raw_lengths; |
| 39 |
struct simplebuffer *unfolded; |
| 40 |
uint32_t origin; |
| 41 |
uint32_t line; |
| 42 |
uint32_t keylen; |
| 43 |
uint32_t boundary; |
| 44 |
struct ini_comment *ic; |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
#define INI_FOLDING_OVERHEAD 3 |
| 49 |
|
| 50 |
|
| 51 |
#define INI_ARRAY_GROW 2 |
| 52 |
|
| 53 |
|
| 54 |
#define INI_EQUAL_SIGN " = " |
| 55 |
#define INI_OPEN_BR "[" |
| 56 |
#define INI_CLOSE_BR "]" |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static int value_unfold(struct ref_array *raw_lines, |
| 61 |
struct ref_array *raw_lengths, |
| 62 |
struct simplebuffer **unfolded) |
| 63 |
{ |
| 64 |
int error; |
| 65 |
struct simplebuffer *oneline = NULL; |
| 66 |
uint32_t len = 0; |
| 67 |
char *ptr = NULL; |
| 68 |
uint32_t i = 0; |
| 69 |
char *part = NULL; |
| 70 |
|
| 71 |
TRACE_FLOW_ENTRY(); |
| 72 |
|
| 73 |
error = simplebuffer_alloc(&oneline); |
| 74 |
if (error) { |
| 75 |
TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
| 76 |
return error; |
| 77 |
} |
| 78 |
|
| 79 |
for (;;) { |
| 80 |
|
| 81 |
ptr = ref_array_get(raw_lines, i, NULL); |
| 82 |
if (ptr) { |
| 83 |
|
| 84 |
ref_array_get(raw_lengths, i, (void *)&len); |
| 85 |
|
| 86 |
part = *((char **)(ptr)); |
| 87 |
|
| 88 |
TRACE_INFO_STRING("Value:", part); |
| 89 |
TRACE_INFO_NUMBER("Lenght:", len); |
| 90 |
|
| 91 |
error = simplebuffer_add_raw(oneline, |
| 92 |
part, |
| 93 |
len, |
| 94 |
INI_VALUE_BLOCK); |
| 95 |
if (error) { |
| 96 |
TRACE_ERROR_NUMBER("Failed to add string", error); |
| 97 |
simplebuffer_free(oneline); |
| 98 |
return error; |
| 99 |
} |
| 100 |
|
| 101 |
i++; |
| 102 |
} |
| 103 |
else break; |
| 104 |
} |
| 105 |
|
| 106 |
*unfolded = oneline; |
| 107 |
|
| 108 |
TRACE_FLOW_EXIT(); |
| 109 |
return error; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
static int save_portion(struct ref_array *raw_lines, |
| 114 |
struct ref_array *raw_lengths, |
| 115 |
const char* buf, |
| 116 |
uint32_t len) |
| 117 |
{ |
| 118 |
int error = EOK; |
| 119 |
char *copy = NULL; |
| 120 |
uint32_t adj = 0; |
| 121 |
|
| 122 |
TRACE_FLOW_ENTRY(); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if ((buf[0] != ' ') && |
| 131 |
(buf[0] != '\t') && |
| 132 |
(len != 0) && |
| 133 |
(ref_array_len(raw_lines) != 0)) adj = 1; |
| 134 |
|
| 135 |
copy = malloc(len + adj + 1); |
| 136 |
if (!copy) { |
| 137 |
TRACE_ERROR_NUMBER("Failed to allocate memory", ENOMEM); |
| 138 |
return ENOMEM; |
| 139 |
} |
| 140 |
|
| 141 |
memcpy(copy + adj, buf, len); |
| 142 |
len += adj; |
| 143 |
copy[len] = 0; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (adj) copy[0] = ' '; |
| 149 |
|
| 150 |
error = ref_array_append(raw_lines, (void *)(©)); |
| 151 |
if (error) { |
| 152 |
TRACE_ERROR_NUMBER("Failed to append line", |
| 153 |
error); |
| 154 |
free(copy); |
| 155 |
return error; |
| 156 |
} |
| 157 |
|
| 158 |
error = ref_array_append(raw_lengths, (void *)(&len)); |
| 159 |
if (error) { |
| 160 |
TRACE_ERROR_NUMBER("Failed to append length", |
| 161 |
error); |
| 162 |
return error; |
| 163 |
} |
| 164 |
|
| 165 |
TRACE_INFO_STRING("Added string:", (char *)copy); |
| 166 |
TRACE_INFO_NUMBER("Added number:", len); |
| 167 |
|
| 168 |
|
| 169 |
TRACE_FLOW_EXIT(); |
| 170 |
return EOK; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
static int value_fold(struct simplebuffer *unfolded, |
| 175 |
uint32_t key_len, |
| 176 |
uint32_t fold_bound, |
| 177 |
struct ref_array *raw_lines, |
| 178 |
struct ref_array *raw_lengths) |
| 179 |
{ |
| 180 |
int error = EOK; |
| 181 |
const char *buf; |
| 182 |
uint32_t len = 0; |
| 183 |
uint32_t fold_place = 0; |
| 184 |
uint32_t best_place = 0; |
| 185 |
uint32_t next_place = 0; |
| 186 |
uint32_t fold_len = 0; |
| 187 |
uint32_t idx = 0; |
| 188 |
uint32_t i = 0; |
| 189 |
uint32_t resume_place = 0; |
| 190 |
uint32_t start_place = 0; |
| 191 |
int done = 0; |
| 192 |
|
| 193 |
TRACE_FLOW_ENTRY(); |
| 194 |
|
| 195 |
|
| 196 |
ref_array_reset(raw_lines); |
| 197 |
ref_array_reset(raw_lengths); |
| 198 |
|
| 199 |
|
| 200 |
len = simplebuffer_get_len(unfolded); |
| 201 |
if (!len) { |
| 202 |
|
| 203 |
TRACE_FLOW_EXIT(); |
| 204 |
return EOK; |
| 205 |
} |
| 206 |
|
| 207 |
buf = (const char *)simplebuffer_get_buf(unfolded); |
| 208 |
|
| 209 |
TRACE_INFO_STRING("Unfolded value:", buf); |
| 210 |
|
| 211 |
|
| 212 |
if (fold_bound == 0) fold_bound++; |
| 213 |
|
| 214 |
while (!done) { |
| 215 |
|
| 216 |
if (idx == 0) { |
| 217 |
if (fold_bound > (key_len + INI_FOLDING_OVERHEAD)) { |
| 218 |
best_place = fold_bound - key_len - INI_FOLDING_OVERHEAD; |
| 219 |
} |
| 220 |
else best_place = 0; |
| 221 |
} |
| 222 |
else { |
| 223 |
best_place = fold_bound; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if ((buf[start_place] != ' ') && |
| 230 |
(buf[start_place] != '\t')) best_place--; |
| 231 |
} |
| 232 |
|
| 233 |
TRACE_INFO_NUMBER("Best place", best_place); |
| 234 |
|
| 235 |
fold_place = start_place; |
| 236 |
next_place = start_place; |
| 237 |
best_place += start_place; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
for (i = resume_place; i <= len; i++) { |
| 242 |
|
| 243 |
|
| 244 |
if (i == len) { |
| 245 |
next_place = i; |
| 246 |
done = 1; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
else if (((!((best_place > 0) && (idx == 0) && (i == 0))) && |
| 253 |
((buf[i] == ' ') || (buf[i] == '\t'))) || |
| 254 |
((best_place == 0) && (i == 0))) { |
| 255 |
next_place = i; |
| 256 |
TRACE_INFO_NUMBER("Next place:", next_place); |
| 257 |
} |
| 258 |
else continue; |
| 259 |
|
| 260 |
if ((next_place > best_place) || (next_place == 0)) { |
| 261 |
if ((fold_place == start_place) && |
| 262 |
(next_place != 0)) { |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
fold_len = next_place - start_place; |
| 268 |
|
| 269 |
} |
| 270 |
else { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
fold_len = fold_place - start_place; |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
TRACE_INFO_NUMBER("Fold len:", fold_len); |
| 279 |
|
| 280 |
error = save_portion(raw_lines, |
| 281 |
raw_lengths, |
| 282 |
buf + start_place, |
| 283 |
fold_len); |
| 284 |
if (error) { |
| 285 |
TRACE_ERROR_NUMBER("Failed to save", error); |
| 286 |
return error; |
| 287 |
} |
| 288 |
|
| 289 |
start_place += fold_len; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
resume_place = next_place; |
| 298 |
if (fold_len == 0) resume_place++; |
| 299 |
idx++; |
| 300 |
break; |
| 301 |
} |
| 302 |
else { |
| 303 |
fold_place = next_place; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
if (done) { |
| 309 |
if (next_place - start_place) { |
| 310 |
error = save_portion(raw_lines, |
| 311 |
raw_lengths, |
| 312 |
buf + start_place, |
| 313 |
next_place - start_place); |
| 314 |
if (error) { |
| 315 |
TRACE_ERROR_NUMBER("Failed to save last chunk", error); |
| 316 |
return error; |
| 317 |
} |
| 318 |
idx++; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
TRACE_FLOW_EXIT(); |
| 324 |
return error; |
| 325 |
} |
| 326 |
|
| 327 |
static int trim_last(struct value_obj *vo) |
| 328 |
{ |
| 329 |
int error = EOK; |
| 330 |
uint32_t last = 0; |
| 331 |
uint32_t len = 0; |
| 332 |
uint32_t idx = 0; |
| 333 |
char *ptr = NULL; |
| 334 |
char *part = NULL; |
| 335 |
|
| 336 |
TRACE_FLOW_ENTRY(); |
| 337 |
|
| 338 |
last = ref_array_len(vo->raw_lengths); |
| 339 |
if (last) { |
| 340 |
last--; |
| 341 |
ref_array_get(vo->raw_lengths, last, (void *)&len); |
| 342 |
if (len) { |
| 343 |
ptr = ref_array_get(vo->raw_lines, last, NULL); |
| 344 |
if (ptr) { |
| 345 |
part = *((char **)ptr); |
| 346 |
|
| 347 |
TRACE_INFO_STRING("Value", part); |
| 348 |
TRACE_INFO_NUMBER("Length", len); |
| 349 |
|
| 350 |
idx = len - 1; |
| 351 |
|
| 352 |
TRACE_INFO_NUMBER("Start index", idx); |
| 353 |
|
| 354 |
while((idx) && (isspace(part[idx]))) idx--; |
| 355 |
if (idx != len - 1) { |
| 356 |
TRACE_INFO_NUMBER("End index", idx); |
| 357 |
len = idx + 1; |
| 358 |
error = ref_array_replace(vo->raw_lengths, last, (void *)&len); |
| 359 |
if (error) { |
| 360 |
TRACE_ERROR_NUMBER("Failed to update length", error); |
| 361 |
return error; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
TRACE_FLOW_EXIT(); |
| 368 |
return error; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
int value_create_from_refarray(struct ref_array *raw_lines, |
| 373 |
struct ref_array *raw_lengths, |
| 374 |
uint32_t line, |
| 375 |
uint32_t origin, |
| 376 |
uint32_t key_len, |
| 377 |
uint32_t boundary, |
| 378 |
struct ini_comment *ic, |
| 379 |
struct value_obj **vo) |
| 380 |
{ |
| 381 |
int error = EOK; |
| 382 |
struct value_obj *new_vo = NULL; |
| 383 |
|
| 384 |
TRACE_FLOW_ENTRY(); |
| 385 |
|
| 386 |
if ((!raw_lines) || (!raw_lengths) || (!vo)) { |
| 387 |
TRACE_ERROR_NUMBER("Invalid argument", EINVAL); |
| 388 |
return EINVAL; |
| 389 |
} |
| 390 |
|
| 391 |
new_vo = malloc(sizeof(struct value_obj)); |
| 392 |
if (!new_vo) { |
| 393 |
TRACE_ERROR_NUMBER("No memory", ENOMEM); |
| 394 |
return ENOMEM; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
new_vo->raw_lines = raw_lines; |
| 405 |
new_vo->raw_lengths = raw_lengths; |
| 406 |
new_vo->origin = origin; |
| 407 |
new_vo->line = line; |
| 408 |
new_vo->keylen = key_len; |
| 409 |
new_vo->boundary = boundary; |
| 410 |
new_vo->ic = ic; |
| 411 |
|
| 412 |
|
| 413 |
error = trim_last(new_vo); |
| 414 |
if (error) { |
| 415 |
TRACE_ERROR_NUMBER("Failed to trim last", error); |
| 416 |
value_destroy(new_vo); |
| 417 |
return error; |
| 418 |
} |
| 419 |
|
| 420 |
error = value_unfold(new_vo->raw_lines, |
| 421 |
new_vo->raw_lengths, |
| 422 |
&(new_vo->unfolded)); |
| 423 |
if (error) { |
| 424 |
TRACE_ERROR_NUMBER("Failed to unfold", error); |
| 425 |
value_destroy(new_vo); |
| 426 |
return error; |
| 427 |
} |
| 428 |
|
| 429 |
TRACE_INFO_STRING("Unfolded:", |
| 430 |
(const char *)simplebuffer_get_buf(new_vo->unfolded)); |
| 431 |
*vo = new_vo; |
| 432 |
|
| 433 |
TRACE_FLOW_EXIT(); |
| 434 |
|
| 435 |
return error; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
void value_lines_cleanup_cb(void *elem, |
| 440 |
ref_array_del_enum type, |
| 441 |
void *data) |
| 442 |
{ |
| 443 |
char *part; |
| 444 |
|
| 445 |
TRACE_FLOW_ENTRY(); |
| 446 |
|
| 447 |
part = *((char **)(elem)); |
| 448 |
|
| 449 |
TRACE_INFO_STRING("Freeing:", part); |
| 450 |
|
| 451 |
free(part); |
| 452 |
|
| 453 |
TRACE_FLOW_EXIT(); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
int value_create_arrays(struct ref_array **raw_lines, |
| 458 |
struct ref_array **raw_lengths) |
| 459 |
{ |
| 460 |
int error = EOK; |
| 461 |
struct ref_array *new_lines = NULL; |
| 462 |
struct ref_array *new_lengths = NULL; |
| 463 |
|
| 464 |
TRACE_FLOW_ENTRY(); |
| 465 |
|
| 466 |
error = ref_array_create(&new_lines, |
| 467 |
sizeof(char *), |
| 468 |
INI_ARRAY_GROW, |
| 469 |
value_lines_cleanup_cb, |
| 470 |
NULL); |
| 471 |
if (error) { |
| 472 |
TRACE_ERROR_NUMBER("Failed to create lines array", error); |
| 473 |
return error; |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
error = ref_array_create(&new_lengths, |
| 478 |
sizeof(uint32_t), |
| 479 |
INI_ARRAY_GROW, |
| 480 |
NULL, |
| 481 |
NULL); |
| 482 |
if (error) { |
| 483 |
TRACE_ERROR_NUMBER("Failed to create lengths array", error); |
| 484 |
ref_array_destroy(new_lines); |
| 485 |
return error; |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
*raw_lines = new_lines; |
| 490 |
*raw_lengths = new_lengths; |
| 491 |
|
| 492 |
TRACE_FLOW_EXIT(); |
| 493 |
return EOK; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
int value_add_to_arrays(const char *strvalue, |
| 498 |
uint32_t len, |
| 499 |
struct ref_array *raw_lines, |
| 500 |
struct ref_array *raw_lengths) |
| 501 |
{ |
| 502 |
int error = EOK; |
| 503 |
|
| 504 |
TRACE_FLOW_ENTRY(); |
| 505 |
|
| 506 |
error = ref_array_append(raw_lines, (void *)(&strvalue)); |
| 507 |
if (error) { |
| 508 |
TRACE_ERROR_NUMBER("Failed to add to lines array", error); |
| 509 |
return error; |
| 510 |
|
| 511 |
} |
| 512 |
|
| 513 |
error = ref_array_append(raw_lengths, (void *)(&len)); |
| 514 |
if (error) { |
| 515 |
TRACE_ERROR_NUMBER("Failed to add to lengths array", error); |
| 516 |
return error; |
| 517 |
|
| 518 |
} |
| 519 |
|
| 520 |
TRACE_FLOW_EXIT(); |
| 521 |
return error; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
void value_destroy_arrays(struct ref_array *raw_lines, |
| 527 |
struct ref_array *raw_lengths) |
| 528 |
{ |
| 529 |
TRACE_FLOW_ENTRY(); |
| 530 |
|
| 531 |
|
| 532 |
ref_array_destroy(raw_lines); |
| 533 |
|
| 534 |
ref_array_destroy(raw_lengths); |
| 535 |
|
| 536 |
TRACE_FLOW_EXIT(); |
| 537 |
|
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
void value_destroy(struct value_obj *vo) |
| 542 |
{ |
| 543 |
TRACE_FLOW_ENTRY(); |
| 544 |
|
| 545 |
if (vo) { |
| 546 |
|
| 547 |
value_destroy_arrays(vo->raw_lines, |
| 548 |
vo->raw_lengths); |
| 549 |
|
| 550 |
simplebuffer_free(vo->unfolded); |
| 551 |
|
| 552 |
ini_comment_destroy(vo->ic); |
| 553 |
free(vo); |
| 554 |
} |
| 555 |
|
| 556 |
TRACE_FLOW_EXIT(); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
int value_create_new(const char *strvalue, |
| 561 |
uint32_t length, |
| 562 |
uint32_t origin, |
| 563 |
uint32_t key_len, |
| 564 |
uint32_t boundary, |
| 565 |
struct ini_comment *ic, |
| 566 |
struct value_obj **vo) |
| 567 |
{ |
| 568 |
int error = EOK; |
| 569 |
struct value_obj *new_vo = NULL; |
| 570 |
struct simplebuffer *oneline = NULL; |
| 571 |
|
| 572 |
TRACE_FLOW_ENTRY(); |
| 573 |
|
| 574 |
if ((!strvalue) || (!vo)) { |
| 575 |
TRACE_ERROR_NUMBER("Invalid argument", EINVAL); |
| 576 |
return EINVAL; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
error = simplebuffer_alloc(&oneline); |
| 581 |
if (error) { |
| 582 |
TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
| 583 |
return error; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
error = simplebuffer_add_str(oneline, |
| 588 |
strvalue, |
| 589 |
length, |
| 590 |
INI_VALUE_BLOCK); |
| 591 |
if (error) { |
| 592 |
TRACE_ERROR_NUMBER("Failed to add string", error); |
| 593 |
simplebuffer_free(oneline); |
| 594 |
return error; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
new_vo = malloc(sizeof(struct value_obj)); |
| 599 |
if (!new_vo) { |
| 600 |
TRACE_ERROR_NUMBER("No memory", ENOMEM); |
| 601 |
simplebuffer_free(oneline); |
| 602 |
return ENOMEM; |
| 603 |
} |
| 604 |
|
| 605 |
new_vo->origin = origin; |
| 606 |
|
| 607 |
new_vo->line = 0; |
| 608 |
new_vo->ic = ic; |
| 609 |
new_vo->unfolded = oneline; |
| 610 |
new_vo->keylen = key_len; |
| 611 |
new_vo->boundary = boundary; |
| 612 |
new_vo->raw_lines = NULL; |
| 613 |
new_vo->raw_lengths = NULL; |
| 614 |
|
| 615 |
error = value_create_arrays(&(new_vo->raw_lines), |
| 616 |
&(new_vo->raw_lengths)); |
| 617 |
|
| 618 |
if (error) { |
| 619 |
TRACE_ERROR_NUMBER("Failed to fold", error); |
| 620 |
value_destroy(new_vo); |
| 621 |
return error; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
error = value_fold(new_vo->unfolded, |
| 626 |
new_vo->keylen, |
| 627 |
new_vo->boundary, |
| 628 |
new_vo->raw_lines, |
| 629 |
new_vo->raw_lengths); |
| 630 |
if (error) { |
| 631 |
TRACE_ERROR_NUMBER("Failed to fold", error); |
| 632 |
value_destroy(new_vo); |
| 633 |
return error; |
| 634 |
} |
| 635 |
|
| 636 |
*vo = new_vo; |
| 637 |
|
| 638 |
TRACE_FLOW_EXIT(); |
| 639 |
|
| 640 |
return error; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
int value_copy(struct value_obj *vo, |
| 645 |
struct value_obj **copy_vo) |
| 646 |
{ |
| 647 |
|
| 648 |
int error = EOK; |
| 649 |
struct value_obj *new_vo = NULL; |
| 650 |
struct simplebuffer *oneline = NULL; |
| 651 |
|
| 652 |
TRACE_FLOW_ENTRY(); |
| 653 |
|
| 654 |
if ((!copy_vo) || (!vo)) { |
| 655 |
TRACE_ERROR_NUMBER("Invalid argument", EINVAL); |
| 656 |
return EINVAL; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
error = simplebuffer_alloc(&oneline); |
| 661 |
if (error) { |
| 662 |
TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
| 663 |
return error; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
error = simplebuffer_add_str(oneline, |
| 668 |
(const char *)simplebuffer_get_buf(vo->unfolded), |
| 669 |
simplebuffer_get_len(vo->unfolded), |
| 670 |
INI_VALUE_BLOCK); |
| 671 |
if (error) { |
| 672 |
TRACE_ERROR_NUMBER("Failed to add string", error); |
| 673 |
simplebuffer_free(oneline); |
| 674 |
return error; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
new_vo = malloc(sizeof(struct value_obj)); |
| 679 |
if (!new_vo) { |
| 680 |
TRACE_ERROR_NUMBER("No memory", ENOMEM); |
| 681 |
simplebuffer_free(oneline); |
| 682 |
return ENOMEM; |
| 683 |
} |
| 684 |
|
| 685 |
new_vo->origin = vo->origin; |
| 686 |
new_vo->line = vo->line; |
| 687 |
new_vo->unfolded = oneline; |
| 688 |
new_vo->keylen = vo->keylen; |
| 689 |
new_vo->boundary = vo->boundary; |
| 690 |
new_vo->raw_lines = NULL; |
| 691 |
new_vo->raw_lengths = NULL; |
| 692 |
new_vo->ic = NULL; |
| 693 |
|
| 694 |
error = value_create_arrays(&(new_vo->raw_lines), |
| 695 |
&(new_vo->raw_lengths)); |
| 696 |
|
| 697 |
if (error) { |
| 698 |
TRACE_ERROR_NUMBER("Failed to fold", error); |
| 699 |
value_destroy(new_vo); |
| 700 |
return error; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
error = value_fold(new_vo->unfolded, |
| 705 |
new_vo->keylen, |
| 706 |
new_vo->boundary, |
| 707 |
new_vo->raw_lines, |
| 708 |
new_vo->raw_lengths); |
| 709 |
if (error) { |
| 710 |
TRACE_ERROR_NUMBER("Failed to fold", error); |
| 711 |
value_destroy(new_vo); |
| 712 |
return error; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
if (vo->ic) { |
| 717 |
error = ini_comment_copy(vo->ic, &new_vo->ic); |
| 718 |
if (error) { |
| 719 |
TRACE_ERROR_NUMBER("Failed to copy comment", error); |
| 720 |
value_destroy(new_vo); |
| 721 |
return error; |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
*copy_vo = new_vo; |
| 726 |
|
| 727 |
TRACE_INFO_STRING("Orig value:", |
| 728 |
(const char *)simplebuffer_get_buf(vo->unfolded)); |
| 729 |
TRACE_INFO_STRING("Copy value:", |
| 730 |
(const char *)simplebuffer_get_buf(new_vo->unfolded)); |
| 731 |
|
| 732 |
TRACE_INFO_NUMBER("Orig value num lines:", |
| 733 |
ref_array_len(vo->raw_lengths)); |
| 734 |
TRACE_INFO_NUMBER("Copy value num lines:", |
| 735 |
ref_array_len(new_vo->raw_lengths)); |
| 736 |
|
| 737 |
TRACE_FLOW_EXIT(); |
| 738 |
return error; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
int value_get_concatenated(struct value_obj *vo, |
| 743 |
const char **fullstr) |
| 744 |
{ |
| 745 |
TRACE_FLOW_ENTRY(); |
| 746 |
|
| 747 |
if (!vo) { |
| 748 |
TRACE_ERROR_NUMBER("Invalid object", EINVAL); |
| 749 |
return EINVAL; |
| 750 |
} |
| 751 |
|
| 752 |
if (!fullstr) |
| 753 |
{ |
| 754 |
TRACE_ERROR_NUMBER("Invalid output value", EINVAL); |
| 755 |
return EINVAL; |
| 756 |
} |
| 757 |
|
| 758 |
*fullstr = (const char *)simplebuffer_get_buf(vo->unfolded); |
| 759 |
|
| 760 |
TRACE_FLOW_EXIT(); |
| 761 |
return EOK; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
int value_get_concatenated_len(struct value_obj *vo, |
| 766 |
uint32_t *len) |
| 767 |
{ |
| 768 |
TRACE_FLOW_ENTRY(); |
| 769 |
|
| 770 |
if (!vo) { |
| 771 |
TRACE_ERROR_NUMBER("Invalid object", EINVAL); |
| 772 |
return EINVAL; |
| 773 |
} |
| 774 |
|
| 775 |
if (!len) |
| 776 |
{ |
| 777 |
TRACE_ERROR_NUMBER("Invalid output value", EINVAL); |
| 778 |
return EINVAL; |
| 779 |
} |
| 780 |
|
| 781 |
*len = simplebuffer_get_len(vo->unfolded); |
| 782 |
|
| 783 |
TRACE_FLOW_EXIT(); |
| 784 |
return EOK; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
int value_get_origin(struct value_obj *vo, uint32_t *origin) |
| 790 |
{ |
| 791 |
TRACE_FLOW_ENTRY(); |
| 792 |
|
| 793 |
if (!vo) { |
| 794 |
TRACE_ERROR_NUMBER("Invalid object", EINVAL); |
| 795 |
return EINVAL; |
| 796 |
} |
| 797 |
|
| 798 |
if (!origin) |
| 799 |
{ |
| 800 |
TRACE_ERROR_NUMBER("Invalid output value", EINVAL); |
| 801 |
return EINVAL; |
| 802 |
} |
| 803 |
|
| 804 |
*origin = vo->origin; |
| 805 |
|
| 806 |
TRACE_FLOW_EXIT(); |
| 807 |
return EOK; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
int value_get_line(struct value_obj *vo, uint32_t *line) |
| 812 |
{ |
| 813 |
TRACE_FLOW_ENTRY(); |
| 814 |
|
| 815 |
if (!vo) { |
| 816 |
TRACE_ERROR_NUMBER("Invalid object", EINVAL); |
| 817 |
return EINVAL; |
| 818 |
} |
| 819 |
|
| 820 |
if (!line) |
| 821 |
{ |
| 822 |
TRACE_ERROR_NUMBER("Invalid output value", EINVAL); |
| 823 |
return EINVAL; |
| 824 |
} |
| 825 |
|
| 826 |
*line = vo->line; |
| 827 |
|
| 828 |
TRACE_FLOW_EXIT(); |
| 829 |
return EOK; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
int value_set_keylen(struct value_obj *vo, uint32_t key_len) |
| 834 |
{ |
| 835 |
int error = EOK; |
| 836 |
TRACE_FLOW_ENTRY(); |
| 837 |
|
| 838 |
if (!vo) { |
| 839 |
TRACE_ERROR_NUMBER("Invalid object", EINVAL); |
| 840 |
return EINVAL; |
| 841 |
} |
| 842 |
|
| 843 |
vo->keylen = key_len; |
| 844 |
|
| 845 |
|
| 846 |
error = value_fold(vo->unfolded, |
| 847 |
vo->keylen, |
| 848 |
vo->boundary, |
| 849 |
vo->raw_lines, |
| 850 |
vo->raw_lengths); |
| 851 |
if (error) { |
| 852 |
TRACE_ERROR_NUMBER("Failed to fold", error); |
| 853 |
|
| 854 |
|
| 855 |
return error; |
| 856 |
} |
| 857 |
|
| 858 |
TRACE_FLOW_EXIT(); |
| 859 |
return EOK; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
int value_set_boundary(struct value_obj *vo, uint32_t boundary) |
| 864 |
{ |
| 865 |
int error = EOK; |
| 866 |
TRACE_FLOW_ENTRY(); |
| 867 |
|
| 868 |
if (!vo) { |
| 869 |
TRACE_ERROR_NUMBER("Invalid object", EINVAL); |
| 870 |
return EINVAL; |
| 871 |
} |
| 872 |
|
| 873 |
vo->boundary = boundary; |
| 874 |
|
| 875 |
|
| 876 |
error = value_fold(vo->unfolded, |
| 877 |
vo->keylen, |
| 878 |
vo->boundary, |
| 879 |
vo->raw_lines, |
| 880 |
vo->raw_lengths); |
| 881 |
if (error) { |
| 882 |
TRACE_ERROR_NUMBER("Failed to fold", error); |
| 883 |
|
| 884 |
|
| 885 |
return error; |
| 886 |
} |
| 887 |
|
| 888 |
TRACE_FLOW_EXIT(); |
| 889 |
return EOK; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
int value_update(struct value_obj *vo, |
| 894 |
const char *value, |
| 895 |
uint32_t length, |
| 896 |
uint32_t origin, |
| 897 |
uint32_t boundary) |
| 898 |
{ |
| 899 |
int error = EOK; |
| 900 |
struct simplebuffer *oneline = NULL; |
| 901 |
|
| 902 |
if ((!value) || (!vo)) { |
| 903 |
TRACE_ERROR_NUMBER("Invalid argument", EINVAL); |
| 904 |
return EINVAL; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
error = simplebuffer_alloc(&oneline); |
| 909 |
if (error) { |
| 910 |
TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
| 911 |
return error; |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
error = simplebuffer_add_str(oneline, |
| 916 |
value, |
| 917 |
length, |
| 918 |
INI_VALUE_BLOCK); |
| 919 |
if (error) { |
| 920 |
TRACE_ERROR_NUMBER("Failed to add string", error); |
| 921 |
simplebuffer_free(oneline); |
| 922 |
return error; |
| 923 |
} |
| 924 |
|
| 925 |
simplebuffer_free(vo->unfolded); |
| 926 |
|
| 927 |
vo->origin = origin; |
| 928 |
vo->unfolded = oneline; |
| 929 |
vo->boundary = boundary; |
| 930 |
|
| 931 |
|
| 932 |
error = value_fold(vo->unfolded, |
| 933 |
vo->keylen, |
| 934 |
vo->boundary, |
| 935 |
vo->raw_lines, |
| 936 |
vo->raw_lengths); |
| 937 |
if (error) { |
| 938 |
TRACE_ERROR_NUMBER("Failed to fold", error); |
| 939 |
|
| 940 |
|
| 941 |
return error; |
| 942 |
} |
| 943 |
|
| 944 |
TRACE_FLOW_EXIT(); |
| 945 |
|
| 946 |
return error; |
| 947 |
|
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
int value_extract_comment(struct value_obj *vo, struct ini_comment **ic) |
| 952 |
{ |
| 953 |
int error = EOK; |
| 954 |
|
| 955 |
TRACE_FLOW_ENTRY(); |
| 956 |
|
| 957 |
if ((!vo) || (!ic)) { |
| 958 |
TRACE_ERROR_NUMBER("Invalid input parameter", EINVAL); |
| 959 |
return EINVAL; |
| 960 |
} |
| 961 |
|
| 962 |
*ic = vo->ic; |
| 963 |
vo->ic = NULL; |
| 964 |
|
| 965 |
TRACE_FLOW_EXIT(); |
| 966 |
return error; |
| 967 |
|
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
int value_put_comment(struct value_obj *vo, struct ini_comment *ic) |
| 972 |
{ |
| 973 |
int error = EOK; |
| 974 |
|
| 975 |
TRACE_FLOW_ENTRY(); |
| 976 |
|
| 977 |
if (!vo) { |
| 978 |
TRACE_ERROR_NUMBER("Invalid input parameter", EINVAL); |
| 979 |
return EINVAL; |
| 980 |
} |
| 981 |
|
| 982 |
if (vo->ic != ic) { |
| 983 |
|
| 984 |
ini_comment_destroy(vo->ic); |
| 985 |
} |
| 986 |
|
| 987 |
vo->ic = ic; |
| 988 |
|
| 989 |
TRACE_FLOW_EXIT(); |
| 990 |
return error; |
| 991 |
|
| 992 |
} |
| 993 |
|
| 994 |
|
| 995 |
int value_serialize(struct value_obj *vo, |
| 996 |
const char *key, |
| 997 |
struct simplebuffer *sbobj) |
| 998 |
{ |
| 999 |
int error = EOK; |
| 1000 |
uint32_t i = 0; |
| 1001 |
uint32_t len = 0; |
| 1002 |
char *ptr = NULL; |
| 1003 |
char *part = NULL; |
| 1004 |
int sec = 0; |
| 1005 |
uint32_t vln = 0; |
| 1006 |
|
| 1007 |
TRACE_FLOW_ENTRY(); |
| 1008 |
TRACE_INFO_STRING("Serializing key:", key); |
| 1009 |
|
| 1010 |
if (!vo) { |
| 1011 |
TRACE_ERROR_NUMBER("Invalid input parameter", EINVAL); |
| 1012 |
return EINVAL; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
if (vo->ic) { |
| 1017 |
error = ini_comment_serialize(vo->ic, sbobj); |
| 1018 |
if (error) { |
| 1019 |
TRACE_ERROR_NUMBER("Failed serialize comment", error); |
| 1020 |
return error; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
if (strncmp(key, |
| 1026 |
INI_SECTION_KEY, |
| 1027 |
sizeof(INI_SECTION_KEY)) == 0) sec = 1; |
| 1028 |
|
| 1029 |
if (sec) { |
| 1030 |
error = simplebuffer_add_str(sbobj, |
| 1031 |
INI_OPEN_BR, |
| 1032 |
sizeof(INI_OPEN_BR) - 1, |
| 1033 |
INI_VALUE_BLOCK); |
| 1034 |
if (error) { |
| 1035 |
TRACE_ERROR_NUMBER("Failed to add opening section bracket", error); |
| 1036 |
return error; |
| 1037 |
|
| 1038 |
} |
| 1039 |
} |
| 1040 |
else { |
| 1041 |
|
| 1042 |
error = simplebuffer_add_str(sbobj, |
| 1043 |
key, |
| 1044 |
vo->keylen, |
| 1045 |
INI_VALUE_BLOCK); |
| 1046 |
if (error) { |
| 1047 |
TRACE_ERROR_NUMBER("Failed to add key", error); |
| 1048 |
return error; |
| 1049 |
} |
| 1050 |
|
| 1051 |
error = simplebuffer_add_str(sbobj, |
| 1052 |
INI_EQUAL_SIGN, |
| 1053 |
sizeof(INI_EQUAL_SIGN) - 1, |
| 1054 |
INI_VALUE_BLOCK); |
| 1055 |
if (error) { |
| 1056 |
TRACE_ERROR_NUMBER("Failed to add equal sign", error); |
| 1057 |
return error; |
| 1058 |
} |
| 1059 |
|
| 1060 |
} |
| 1061 |
|
| 1062 |
if (vo->raw_lines) { |
| 1063 |
|
| 1064 |
vln = ref_array_len(vo->raw_lines); |
| 1065 |
TRACE_INFO_NUMBER("Number of lines:", vln); |
| 1066 |
|
| 1067 |
#ifdef HAVE_TRACE |
| 1068 |
|
| 1069 |
ref_array_debug(vo->raw_lines, 0); |
| 1070 |
ref_array_debug(vo->raw_lengths, 1); |
| 1071 |
#endif |
| 1072 |
|
| 1073 |
for (i = 0; i < vln; i++) { |
| 1074 |
|
| 1075 |
ptr = ref_array_get(vo->raw_lines, i, NULL); |
| 1076 |
|
| 1077 |
if (ptr) { |
| 1078 |
|
| 1079 |
len = 0; |
| 1080 |
ref_array_get(vo->raw_lengths, i, (void *)&len); |
| 1081 |
|
| 1082 |
part = *((char **)(ptr)); |
| 1083 |
|
| 1084 |
TRACE_INFO_STRING("Value:", part); |
| 1085 |
TRACE_INFO_NUMBER("Lenght:", len); |
| 1086 |
|
| 1087 |
error = simplebuffer_add_raw(sbobj, |
| 1088 |
part, |
| 1089 |
len, |
| 1090 |
INI_VALUE_BLOCK); |
| 1091 |
if (error) { |
| 1092 |
TRACE_ERROR_NUMBER("Failed to add value", error); |
| 1093 |
return error; |
| 1094 |
} |
| 1095 |
|
| 1096 |
} |
| 1097 |
if (!sec) { |
| 1098 |
error = simplebuffer_add_cr(sbobj); |
| 1099 |
if (error) { |
| 1100 |
TRACE_ERROR_NUMBER("Failed to add CR", error); |
| 1101 |
return error; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
if ((!vln) && (!sec)) { |
| 1107 |
error = simplebuffer_add_cr(sbobj); |
| 1108 |
if (error) { |
| 1109 |
TRACE_ERROR_NUMBER("Failed to add CR", error); |
| 1110 |
return error; |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (sec) { |
| 1116 |
error = simplebuffer_add_str(sbobj, |
| 1117 |
INI_CLOSE_BR, |
| 1118 |
sizeof(INI_CLOSE_BR) - 1, |
| 1119 |
INI_VALUE_BLOCK); |
| 1120 |
if (error) { |
| 1121 |
TRACE_ERROR_NUMBER("Failed to add closing bracket", error); |
| 1122 |
return error; |
| 1123 |
|
| 1124 |
} |
| 1125 |
|
| 1126 |
error = simplebuffer_add_cr(sbobj); |
| 1127 |
if (error) { |
| 1128 |
TRACE_ERROR_NUMBER("Failed to add CR", error); |
| 1129 |
return error; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
TRACE_INFO_STRING("Buffer:", (const char *)simplebuffer_get_buf(sbobj)); |
| 1134 |
TRACE_FLOW_EXIT(); |
| 1135 |
return error; |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
int value_merge_comment(struct value_obj *vo_donor, |
| 1140 |
struct value_obj *vo) |
| 1141 |
{ |
| 1142 |
int error = EOK; |
| 1143 |
|
| 1144 |
TRACE_FLOW_ENTRY(); |
| 1145 |
|
| 1146 |
if ((!vo) || (!vo_donor)) { |
| 1147 |
TRACE_ERROR_NUMBER("Invalid input parameter", EINVAL); |
| 1148 |
return EINVAL; |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (vo_donor->ic) { |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
if (vo->ic) { |
| 1156 |
|
| 1157 |
|
| 1158 |
error = ini_comment_add(vo_donor->ic, vo->ic); |
| 1159 |
if (error) { |
| 1160 |
TRACE_ERROR_NUMBER("Failed to merge the comment", error); |
| 1161 |
return error; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
else { |
| 1165 |
|
| 1166 |
|
| 1167 |
error = ini_comment_copy(vo_donor->ic, &(vo->ic)); |
| 1168 |
if (error) { |
| 1169 |
TRACE_ERROR_NUMBER("Failed to merge the comment", error); |
| 1170 |
return error; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
TRACE_FLOW_EXIT(); |
| 1176 |
return error; |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
void value_print(const char *key, struct value_obj *vo) |
| 1182 |
{ |
| 1183 |
|
| 1184 |
int error = EOK; |
| 1185 |
struct simplebuffer *sbobj = NULL; |
| 1186 |
|
| 1187 |
TRACE_FLOW_ENTRY(); |
| 1188 |
|
| 1189 |
error = simplebuffer_alloc(&sbobj); |
| 1190 |
if (error) { |
| 1191 |
printf("Failed to allocate dynamic string %d.\n", error); |
| 1192 |
return; |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
error = value_serialize(vo, key, sbobj); |
| 1197 |
if (error) { |
| 1198 |
printf("Failed to serialize a value object %d.\n", error); |
| 1199 |
simplebuffer_free(sbobj); |
| 1200 |
return; |
| 1201 |
} |
| 1202 |
|
| 1203 |
printf("%s", simplebuffer_get_buf(sbobj)); |
| 1204 |
simplebuffer_free(sbobj); |
| 1205 |
|
| 1206 |
TRACE_FLOW_EXIT(); |
| 1207 |
} |