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