1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
|
/*
INI LIBRARY
Value interpretation functions for single values
and corresponding memory cleanup functions.
Copyright (C) Dmitri Pal <dpal@redhat.com> 2012
INI Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
INI Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with INI Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "trace.h"
#include "collection.h"
#include "collection_tools.h"
#include "ini_defines.h"
#include "ini_configobj.h"
#include "ini_config_priv.h"
#include "ini_valueobj.h"
/* Macro co convert to HEX value */
#define HEXVAL(c) (isdigit(c) ? (c - '0') : (tolower(c) - 'a') + 10)
static int is_same_section(struct ini_cfgobj *ini_config,
const char *section)
{
int len = 0;
TRACE_FLOW_ENTRY();
/* If section is not defined it is different */
if (ini_config->section == NULL) {
TRACE_FLOW_RETURN(0);
return 0;
}
len = strlen(section);
/* If values are same this is the same section */
if ((strncasecmp(ini_config->section, section, ini_config->section_len) == 0) &&
(ini_config->section_len == len)) {
TRACE_FLOW_RETURN(1);
return 1;
}
/* Otherwise the values are different */
TRACE_FLOW_RETURN(0);
return 0;
}
static int is_same_name(struct ini_cfgobj *ini_config,
const char *name,
int name_len)
{
TRACE_FLOW_ENTRY();
/* If name is not defined it is different */
if (ini_config->name == NULL) {
TRACE_FLOW_RETURN(0);
return 0;
}
/* If values are same this is the same value */
if ((strncasecmp(ini_config->name, name, ini_config->name_len) == 0) &&
(ini_config->name_len == name_len)) {
TRACE_FLOW_RETURN(1);
return 1;
}
/* Otherwise the values are different */
TRACE_FLOW_RETURN(0);
return 0;
}
/* Function to get value object from the configuration handle */
int ini_get_config_valueobj(const char *section,
const char *name,
struct ini_cfgobj *ini_config,
int mode,
struct value_obj **vo)
{
int error = EOK;
struct collection_item *section_handle = NULL;
struct collection_item *item = NULL;
struct collection_item *last_found = NULL;
const char *to_find;
char default_section[] = INI_DEFAULT_SECTION;
uint64_t hash = 0;
int len = 0, name_len = 0;
TRACE_FLOW_ENTRY();
/* Do we have the accepting memory ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument vo.", EINVAL);
return EINVAL;
}
*vo = NULL;
if (ini_config == NULL) {
TRACE_ERROR_NUMBER("Invalid argument ini_config.", EINVAL);
return EINVAL;
}
if ((mode < INI_GET_FIRST_VALUE) ||
(mode > INI_GET_LAST_VALUE)) {
TRACE_ERROR_NUMBER("Invalid argument mode:", mode);
return EINVAL;
}
/* Do we have a name ? */
if (name == NULL) {
TRACE_ERROR_NUMBER("Name is NULL it will not be found.", EINVAL);
return EINVAL;
}
/* Empty section means look for the default one */
if (section == NULL) to_find = default_section;
else to_find = section;
TRACE_INFO_STRING("Getting Name:", name);
TRACE_INFO_STRING("In Section:", to_find);
/* Make sure we start over if this is the first value */
if (mode == INI_GET_FIRST_VALUE) ini_config_clean_state(ini_config);
/* Are we looking in the same section ? */
if (!is_same_section(ini_config, to_find)) {
/* This is a different section */
ini_config_clean_state(ini_config);
/* Get Subcollection */
error = col_get_collection_reference(ini_config->cfg, §ion_handle, to_find);
/* Check error */
if (error && (error != ENOENT)) {
TRACE_ERROR_NUMBER("Failed to get section", error);
return error;
}
/* Did we find a section */
if ((error == ENOENT) || (section_handle == NULL)) {
/* We have not found section - return success */
TRACE_FLOW_EXIT();
return EOK;
}
/* Create an iterator */
error = col_bind_iterator(&(ini_config->iterator),
section_handle,
COL_TRAVERSE_ONELEVEL);
/* Make sure we free the section we found */
col_destroy_collection(section_handle);
/* Check error */
if (error) {
TRACE_ERROR_NUMBER("Failed to bind to section", error);
return error;
}
/* Save section */
ini_config->section_len = strlen(to_find);
ini_config->section = strndup(to_find, ini_config->section_len);
/* Check error */
if (ini_config->section == NULL) {
TRACE_ERROR_NUMBER("Failed to save section name ", ENOMEM);
ini_config_clean_state(ini_config);
return ENOMEM;
}
}
hash = col_make_hash(name, 0, &name_len);
/* Check if this is the same name */
if (!is_same_name(ini_config, name, name_len)) {
TRACE_INFO_STRING("Saved name:", ini_config->name);
TRACE_INFO_STRING("Passed name:", name);
TRACE_INFO_NUMBER("Length of the saved name", ini_config->name_len);
TRACE_INFO_NUMBER("Length of the passed name", name_len);
col_rewind_iterator(ini_config->iterator);
free(ini_config->name);
ini_config->name = NULL;
ini_config->name_len = 0;
}
/* Iterate through the section */
do {
/* Loop through a collection */
error = col_iterate_collection(ini_config->iterator, &item);
if (error) {
TRACE_ERROR_NUMBER("Failed to iterate", error);
ini_config_clean_state(ini_config);
return error;
}
/* Are we done ? */
if (item == NULL) {
/* We looped to the end and found last value */
if ((mode == INI_GET_LAST_VALUE) && (last_found)) {
item = last_found;
break;
}
/* There is nothing left to look for */
ini_config_clean_state(ini_config);
TRACE_FLOW_EXIT();
return EOK;
}
if ((hash == col_get_item_hash(item)) &&
(col_get_item_type(item) == COL_TYPE_BINARY) &&
(strncasecmp(col_get_item_property(item, &len), name, name_len) == 0) &&
(len == name_len)) {
TRACE_INFO_STRING("Item is found", name);
last_found = item;
if (mode == INI_GET_LAST_VALUE) continue;
break;
}
}
while(1);
if (!is_same_name(ini_config, name, name_len)) {
/* Save name */
ini_config->name_len = name_len;
ini_config->name = strndup(name, name_len);
/* Check error */
if (ini_config->name == NULL) {
TRACE_ERROR_NUMBER("Failed to save key name ", ENOMEM);
ini_config_clean_state(ini_config);
return ENOMEM;
}
}
*vo = *((struct value_obj **)(col_get_item_data(item)));
TRACE_FLOW_EXIT();
return error;
}
/* Get long long value from config value object */
static long long ini_get_llong_config_value(struct value_obj *vo,
int strict,
long long def,
int *error)
{
int err;
const char *str;
char *endptr;
long long val = 0;
TRACE_FLOW_ENTRY();
/* Do we have the vo ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
if (error) *error = EINVAL;
return def;
}
if (error) *error = EOK;
/* Get value - no error checking as we checked it above
* and there is no other reson the function could fail.
*/
value_get_concatenated(vo, &str);
/* Try to parse the value */
errno = 0;
val = strtoll(str, &endptr, 10);
err = errno;
/* Check for various possible errors */
if (err != 0) {
TRACE_ERROR_NUMBER("Conversion failed", err);
if (error) *error = err;
return def;
}
/* Other error cases */
if ((endptr == str) || (strict && (*endptr != '\0'))) {
TRACE_ERROR_NUMBER("More characters or nothing processed", EIO);
if (error) *error = EIO;
return def;
}
TRACE_FLOW_NUMBER("ini_get_llong_config_value returning", (long)val);
return val;
}
/* Get unsigned long long value from config value object */
static unsigned long long ini_get_ullong_config_value(struct value_obj *vo,
int strict,
unsigned long long def,
int *error)
{
int err;
const char *str;
char *endptr;
unsigned long long val = 0;
TRACE_FLOW_ENTRY();
/* Do we have the vo ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
if (error) *error = EINVAL;
return def;
}
if (error) *error = EOK;
/* Get value - no error checking as we checked it above
* and there is no other reson the function could fail.
*/
value_get_concatenated(vo, &str);
errno = 0;
val = strtoull(str, &endptr, 10);
err = errno;
/* Check for various possible errors */
if (err != 0) {
TRACE_ERROR_NUMBER("Conversion failed", err);
if (error) *error = err;
return def;
}
/* Other error cases */
if ((endptr == str) || (strict && (*endptr != '\0'))) {
TRACE_ERROR_NUMBER("More characters or nothing processed", EIO);
if (error) *error = EIO;
return def;
}
TRACE_FLOW_NUMBER("ini_get_ullong_config_value returning", val);
return val;
}
/* Get integer value from config value */
int ini_get_int_config_value(struct value_obj *vo,
int strict,
int def,
int *error)
{
long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_llong_config_value(vo, strict, def, &err);
if (err == 0) {
if ((val > INT_MAX) || (val < INT_MIN)) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_int_config_value returning", (int)val);
return (int)val;
}
/* Get unsigned integer value from config value object */
unsigned ini_get_unsigned_config_value(struct value_obj *vo,
int strict,
unsigned def,
int *error)
{
unsigned long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_ullong_config_value(vo, strict, def, &err);
if (err == 0) {
if (val > UINT_MAX) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_unsigned_config_value returning",
(unsigned)val);
return (unsigned)val;
}
/* Get long value from config value object */
long ini_get_long_config_value(struct value_obj *vo,
int strict,
long def,
int *error)
{
long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_llong_config_value(vo, strict, def, &err);
if (err == 0) {
if ((val > LONG_MAX) || (val < LONG_MIN)) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_long_config_value returning",
(long)val);
return (long)val;
}
/* Get unsigned long value from config value object */
unsigned long ini_get_ulong_config_value(struct value_obj *vo,
int strict,
unsigned long def,
int *error)
{
unsigned long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_ullong_config_value(vo, strict, def, &err);
if (err == 0) {
if (val > ULONG_MAX) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_ulong_config_value returning",
(unsigned long)val);
return (unsigned long)val;
}
/* Get int32_t value from config value object */
int32_t ini_get_int32_config_value(struct value_obj *vo,
int strict,
int32_t def,
int *error)
{
long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_llong_config_value(vo, strict, def, &err);
if (err == 0) {
if ((val > INT32_MAX) || (val < INT32_MIN)) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_int32_config_value returning",
(int32_t)val);
return (int32_t)val;
}
/* Get uint32_t value from config value object */
uint32_t ini_get_uint32_config_value(struct value_obj *vo,
int strict,
uint32_t def,
int *error)
{
unsigned long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_ullong_config_value(vo, strict, def, &err);
if (err == 0) {
if (val > UINT32_MAX) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_uint32_config_value returning",
(uint32_t)val);
return (uint32_t)val;
}
/* Get int64_t value from config value ovject */
int64_t ini_get_int64_config_value(struct value_obj *vo,
int strict,
int64_t def,
int *error)
{
long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_llong_config_value(vo, strict, def, &err);
if (err == 0) {
if ((val > INT64_MAX) || (val < INT64_MIN)) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_int64_config_value returning",
(int64_t)val);
return (int64_t)val;
}
/* Get uint64_t value from config value object */
uint64_t ini_get_uint64_config_value(struct value_obj *vo,
int strict,
uint64_t def,
int *error)
{
unsigned long long val = 0;
int err = 0;
TRACE_FLOW_ENTRY();
val = ini_get_ullong_config_value(vo, strict, def, &err);
if (err == 0) {
if (val > UINT64_MAX) {
TRACE_ERROR_NUMBER("Value is out of range", ERANGE);
val = def;
err = ERANGE;
}
}
if (error) *error = err;
TRACE_FLOW_NUMBER("ini_get_uint64_config_value returning",
(uint64_t)val);
return (uint64_t)val;
}
/* Get double value */
double ini_get_double_config_value(struct value_obj *vo,
int strict, double def, int *error)
{
const char *str;
char *endptr;
double val = 0;
TRACE_FLOW_ENTRY();
/* Do we have the vo ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
if (error) *error = EINVAL;
return def;
}
if (error) *error = EOK;
/* Get value - no error checking as we checked it above
* and there is no other reason the function could fail.
*/
value_get_concatenated(vo, &str);
errno = 0;
val = strtod(str, &endptr);
/* Check for various possible errors */
if ((errno == ERANGE) ||
((errno != 0) && (val == 0)) ||
(endptr == str)) {
TRACE_ERROR_NUMBER("Conversion failed", EIO);
if (error) *error = EIO;
return def;
}
if (strict && (*endptr != '\0')) {
TRACE_ERROR_NUMBER("More characters than expected", EIO);
if (error) *error = EIO;
val = def;
}
TRACE_FLOW_DOUBLE("ini_get_double_config_value returning", val);
return val;
}
/* Get boolean value */
unsigned char ini_get_bool_config_value(struct value_obj *vo,
unsigned char def, int *error)
{
const char *str;
uint32_t len = 0;
TRACE_FLOW_ENTRY();
/* Do we have the vo ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
if (error) *error = EINVAL;
return def;
}
if (error) *error = EOK;
/* Get value - no error checking as we checked it above
* and there is no other reson the function could fail.
*/
value_get_concatenated(vo, &str);
value_get_concatenated_len(vo, &len);
/* Try to parse the value */
if ((strncasecmp(str, "true", len) == 0) ||
(strncasecmp(str, "yes", len) == 0)) {
TRACE_FLOW_STRING("Returning", "true");
return '\1';
}
else if ((strncasecmp(str, "false", len) == 0) ||
(strncasecmp(str, "no", len) == 0)) {
TRACE_FLOW_STRING("Returning", "false");
return '\0';
}
TRACE_ERROR_STRING("Returning", "error");
if (error) *error = EIO;
return def;
}
/* Return a string out of the value */
char *ini_get_string_config_value(struct value_obj *vo,
int *error)
{
const char *str = NULL;
char *ret_str = NULL;
TRACE_FLOW_ENTRY();
/* Do we have the vo ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
if (error) *error = EINVAL;
return NULL;
}
/* Get value - no error checking as we checked it above
* and there is no other reson the function could fail.
*/
value_get_concatenated(vo, &str);
ret_str = strdup(str);
if (ret_str == NULL) {
TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
if (error) *error = ENOMEM;
return NULL;
}
if (error) *error = EOK;
TRACE_FLOW_STRING("ini_get_string_config_value returning", str);
return ret_str;
}
/* Get string from the value object */
const char *ini_get_const_string_config_value(struct value_obj *vo,
int *error)
{
const char *str;
TRACE_FLOW_ENTRY();
/* Do we have the vo ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
if (error) *error = EINVAL;
return NULL;
}
/* Get value - no error checking as we checked it above
* and there is no other reson the function could fail.
*/
value_get_concatenated(vo, &str);
if (error) *error = EOK;
TRACE_FLOW_STRING("ini_get_const_string_config_value returning", str);
return str;
}
/* A special hex format is assumed.
* The string should be taken in single quotes
* and consist of hex encoded value two hex digits per byte.
* Example: '0A2BFECC'
* Case does not matter.
*/
char *ini_get_bin_config_value(struct value_obj *vo,
int *length, int *error)
{
int i;
char *value = NULL;
const char *buff;
int size = 0;
uint32_t len = 0;
const char *str;
TRACE_FLOW_ENTRY();
/* Do we have the vo ? */
if (vo == NULL) {
TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
if (error) *error = EINVAL;
return NULL;
}
if (error) *error = EOK;
/* Get value - no error checking as we checked it above
* and there is no other reson the function could fail.
*/
value_get_concatenated(vo, &str);
/* Check the length */
value_get_concatenated_len(vo, &len);
if ((len%2) != 0) {
TRACE_ERROR_STRING("Invalid length for binary data", "");
if (error) *error = EINVAL;
return NULL;
}
/* Is the format correct ? */
if ((*str != '\'') ||
(str[len -1] != '\'')) {
TRACE_ERROR_STRING("String is not escaped","");
if (error) *error = EIO;
return NULL;
}
/* Check that all the symbols are ok */
buff = str + 1;
len -= 2;
for (i = 0; i < len; i++) {
if (!isxdigit(buff[i])) {
TRACE_ERROR_STRING("Invalid encoding for binary data", buff);
if (error) *error = EIO;
return NULL;
}
}
/* The value is good so we can allocate memory for it */
value = malloc(len / 2);
if (value == NULL) {
TRACE_ERROR_NUMBER("Failed to allocate memory.", ENOMEM);
if (error) *error = ENOMEM;
return NULL;
}
/* Convert the value */
for (i = 0; i < len; i += 2) {
value[size] = (char)(16 * HEXVAL(buff[i]) + HEXVAL(buff[i+1]));
size++;
}
if (error) *error = EOK;
if (length) *length = size;
TRACE_FLOW_STRING("ini_get_bin_config_value", "Exit");
return value;
}
/* Function to free binary configuration value */
void ini_free_bin_config_value(char *value)
{
free(value);
}
|