Debian Codegraph @

ini/ini_get_valueobj.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Value interpretation functions for single values
5 and corresponding memory cleanup functions.
6
7 Copyright (C) Dmitri Pal <[email protected]> 2012
8
9 INI Library is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 INI Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
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 /* Macro co convert to HEX value */
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 /* If section is not defined it is different */
48 if (ini_config->section == NULL) {
49 TRACE_FLOW_RETURN(0);
50 return 0;
51 }
52
53 len = strlen(section);
54
55 /* If values are same this is the same section */
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 /* Otherwise the values are different */
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 /* If name is not defined it is different */
74 if (ini_config->name == NULL) {
75 TRACE_FLOW_RETURN(0);
76 return 0;
77 }
78
79 /* If values are same this is the same value */
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 /* Otherwise the values are different */
87 TRACE_FLOW_RETURN(0);
88 return 0;
89 }
90
91
92 /* Function to get value object from the configuration handle */
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 /* Do we have the accepting memory ? */
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 /* Do we have a name ? */
130 if (name == NULL) {
131 TRACE_ERROR_NUMBER("Name is NULL it will not be found.", EINVAL);
132 return EINVAL;
133 }
134
135 /* Empty section means look for the default one */
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 /* Make sure we start over if this is the first value */
143 if (mode == INI_GET_FIRST_VALUE) ini_config_clean_state(ini_config);
144
145 /* Are we looking in the same section ? */
146 if (!is_same_section(ini_config, to_find)) {
147
148 /* This is a different section */
149 ini_config_clean_state(ini_config);
150
151 /* Get Subcollection */
152 error = col_get_collection_reference(ini_config->cfg, &section_handle, to_find);
153 /* Check error */
154 if (error && (error != ENOENT)) {
155 TRACE_ERROR_NUMBER("Failed to get section", error);
156 return error;
157 }
158
159 /* Did we find a section */
160 if ((error == ENOENT) || (section_handle == NULL)) {
161 /* We have not found section - return success */
162 TRACE_FLOW_EXIT();
163 return EOK;
164 }
165
166 /* Create an iterator */
167 error = col_bind_iterator(&(ini_config->iterator),
168 section_handle,
169 COL_TRAVERSE_ONELEVEL);
170 /* Make sure we free the section we found */
171 col_destroy_collection(section_handle);
172 /* Check error */
173 if (error) {
174 TRACE_ERROR_NUMBER("Failed to bind to section", error);
175 return error;
176 }
177
178 /* Save section */
179 ini_config->section_len = strlen(to_find);
180 ini_config->section = strndup(to_find, ini_config->section_len);
181 /* Check error */
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 /* Check if this is the same name */
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 /* Iterate through the section */
204 do {
205
206 /* Loop through a collection */
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 /* Are we done ? */
215 if (item == NULL) {
216 /* We looped to the end and found last value */
217 if ((mode == INI_GET_LAST_VALUE) && (last_found)) {
218 item = last_found;
219 break;
220 }
221 /* There is nothing left to look for */
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 /* Save name */
241 ini_config->name_len = name_len;
242 ini_config->name = strndup(name, name_len);
243 /* Check error */
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 /* Get long long value from config value object */
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 /* Do we have the vo ? */
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 /* Get value - no error checking as we checked it above
280 * and there is no other reson the function could fail.
281 */
282 value_get_concatenated(vo, &str);
283
284 /* Try to parse the value */
285 errno = 0;
286 val = strtoll(str, &endptr, 10);
287 err = errno;
288
289 /* Check for various possible errors */
290 if (err != 0) {
291 TRACE_ERROR_NUMBER("Conversion failed", err);
292 if (error) *error = err;
293 return def;
294 }
295
296 /* Other error cases */
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 /* Get unsigned long long value from config value object */
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 /* Do we have the vo ? */
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 /* Get value - no error checking as we checked it above
330 * and there is no other reson the function could fail.
331 */
332 value_get_concatenated(vo, &str);
333
334 errno = 0;
335 val = strtoull(str, &endptr, 10);
336 err = errno;
337
338 /* Check for various possible errors */
339 if (err != 0) {
340 TRACE_ERROR_NUMBER("Conversion failed", err);
341 if (error) *error = err;
342 return def;
343 }
344
345 /* Other error cases */
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 /* Get integer value from config value */
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 /* Get unsigned integer value from config value object */
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 /* Get long value from config value object */
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 /* Get unsigned long value from config value object */
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 /* Get int32_t value from config value object */
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 /* Get uint32_t value from config value object */
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 /* Get int64_t value from config value ovject */
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 /* Get uint64_t value from config value object */
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 /* Get double value */
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 /* Do we have the vo ? */
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 /* Get value - no error checking as we checked it above
592 * and there is no other reason the function could fail.
593 */
594 value_get_concatenated(vo, &str);
595
596 errno = 0;
597 val = strtod(str, &endptr);
598
599 /* Check for various possible errors */
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 /* Get boolean value */
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 /* Do we have the vo ? */
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 /* Get value - no error checking as we checked it above
637 * and there is no other reson the function could fail.
638 */
639 value_get_concatenated(vo, &str);
640 value_get_concatenated_len(vo, &len);
641
642 /* Try to parse the value */
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 /* Return a string out of the value */
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 /* Do we have the vo ? */
669 if (vo == NULL) {
670 TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
671 if (error) *error = EINVAL;
672 return NULL;
673 }
674
675 /* Get value - no error checking as we checked it above
676 * and there is no other reson the function could fail.
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 /* Get string from the value object */
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 /* Do we have the vo ? */
702 if (vo == NULL) {
703 TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
704 if (error) *error = EINVAL;
705 return NULL;
706 }
707
708 /* Get value - no error checking as we checked it above
709 * and there is no other reson the function could fail.
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 /* A special hex format is assumed.
720 * The string should be taken in single quotes
721 * and consist of hex encoded value two hex digits per byte.
722 * Example: '0A2BFECC'
723 * Case does not matter.
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 /* Do we have the vo ? */
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 /* Get value - no error checking as we checked it above
747 * and there is no other reson the function could fail.
748 */
749 value_get_concatenated(vo, &str);
750
751 /* Check the length */
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 /* Is the format correct ? */
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 /* Check that all the symbols are ok */
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 /* The value is good so we can allocate memory for it */
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 /* Convert the value */
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 /* Function to free binary configuration value */
799 void ini_free_bin_config_value(char *value)
800 {
801 free(value);
802 }