blob: 1817ad74d99a101c4881d5549bf4abe11f72e10e [file] [log] [blame]
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +05301/*
2Copyright (c) 2015, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <ctype.h>
34#include <limits.h>
35#include <math.h>
36#include <utils/Log.h>
37#include "ConfFileParser.h"
38
39//declaration of functions only specific to this file
40static char parse_line
41(
42 group_table *key_file,
43 const char *line,
44 char **cur_grp
45);
46
47static char parse_load_frm_fhandler
48(
49 group_table *key_file,
50 FILE *fp
51);
52
53static char line_is_grp
54(
55 group_table *key_file,
56 const char *str,
57 char **cur_grp
58);
59
60static void free_grp_list
61(
62 group *a
63);
64
65static void free_key_list
66(
67 key_value_pair_list *a
68);
69
70static char line_is_key_value_pair
71(
72 group_table *key_file,
73 const char *str,
74 const char *cur_grp
75);
76
77static char line_is_comment
78(
79 const char *str
80);
81
82static char grp_exist
83(
84 const group_table *key_file,
85 const char *new_grp
86);
87
88static char add_grp
89(
90 group_table *key_file,
91 const char *new_grp
92);
93
94static group *alloc_group
95(
96 void
97);
98
99static key_value_pair_list *alloc_key_value_pair
100(
101 void
102);
103
104static char add_key_value_pair
105(
106 group_table *key_file,
107 const char *cur_grp,
108 const char *key,
109 const char *val
110);
111
112
113//Definitions
114void free_strs
115(
116 char **str_array
117)
118{
119 char **str_array_cpy = str_array;
120 if(str_array != NULL) {
121 while(*str_array != NULL) {
122 free(*str_array);
123 str_array++;
124 }
125 }
126 free(str_array_cpy);
127}
128//ToDo: Come up with code hashing
129//function
130unsigned int get_hash_code
131(
132 const char *str
133)
134{
135
136 unsigned len = strlen(str);
137 unsigned int i;
138 unsigned int hash_code = 0;
139
140 for(i = 0; len > 0; len--, i++) {
141 hash_code += (int)((str[i] * pow(2, len))) % INT_MAX;
142 hash_code %= INT_MAX;
143 }
144 return hash_code;
145}
146
147static key_value_pair_list *alloc_key_value_pair
148(
149 void
150)
151{
152 key_value_pair_list *key_list = NULL;
153
154 key_list = (key_value_pair_list *)malloc(
155 sizeof(key_value_pair_list));
156 if(key_list != NULL) {
157 key_list->key = NULL;
158 key_list->next = NULL;
159 key_list->value = NULL;
160 }
161 return key_list;
162}
163
164static group * alloc_group
165(
166 void
167)
168{
169 group *grp = NULL;
170 unsigned int i;
171
172 grp = (group *)malloc(sizeof(group));
173 if(grp != NULL) {
174 grp->grp_name = NULL;
175 grp->grp_next = NULL;
176 grp->num_of_keys = 0;
177 grp->keys_hash_size = MAX_UNIQ_KEYS;
178 grp->list = (key_value_pair_list **)malloc
179 (sizeof(key_value_pair_list *) * grp->keys_hash_size);
180 if(grp->list == NULL) {
181 ALOGE("Could not alloc group\n");
182 free(grp);
183 grp = NULL;
184 }else {
185 for(i = 0; i < grp->keys_hash_size; i++) {
186 grp->list[i] = NULL;
187 }
188 }
189 }
190 return grp;
191}
192
193group_table *get_key_file
194(
195)
196{
197 group_table *t = NULL;
198 unsigned int i;
199
200 t = (group_table *)malloc(sizeof(group_table));
201 if (t != NULL) {
202 t->grps_hash_size = MAX_UNIQ_GRPS;
203 t->num_of_grps = 0;
204 t->grps_hash = (group **)malloc(sizeof(group *)
205 * t->grps_hash_size);
206 if (t->grps_hash == NULL) {
207 free(t);
208 return NULL;
209 }
210 for(i = 0; i < t->grps_hash_size; i++) {
211 t->grps_hash[i] = NULL;
212 }
213 }
214 return t;
215}
216
217void free_key_file(
218 group_table *key_file
219)
220{
221 unsigned int i;
222
223 if(key_file != NULL) {
224 if(key_file->grps_hash != NULL) {
225 for(i = 0; i < key_file->grps_hash_size; i++) {
226 free_grp_list(key_file->grps_hash[i]);
227 }
228 }
229 free(key_file->grps_hash);
230 free(key_file);
231 }
232}
233
234static void free_grp_list
235(
236 group *a
237)
238{
239 group *next;
240 unsigned int i;
241
242 while(a != NULL) {
243 next = a->grp_next;
244 if(a->list != NULL) {
245 for(i = 0; i < a->keys_hash_size; i++) {
246 free_key_list(a->list[i]);
247 }
248 }
249 free(a->grp_name);
250 free(a->list);
251 free(a);
252 a = next;
253 }
254}
255
256static void free_key_list
257(
258 key_value_pair_list *a
259)
260{
261 key_value_pair_list *next;
262
263 while(a != NULL) {
264 next = a->next;
265 free(a->key);
266 free(a->value);
267 free(a);
268 a = next;
269 }
270}
271//return all the groups
272//present in the file
273char **get_grps
274(
275 const group_table *key_file
276)
277{
278 char **grps = NULL;
279 unsigned int i = 0;
280 unsigned int j = 0;
281 unsigned int grp_len;
282 group *grp_list;
283
284 if((key_file == NULL)
285 || (key_file->grps_hash == NULL)
286 || (key_file->grps_hash_size == 0)
287 || (key_file->num_of_grps == 0)) {
288 return grps;
289 }
290 grps = (char **)calloc((key_file->num_of_grps + 1),
291 sizeof(char *));
292 if(grps == NULL) {
293 return grps;
294 }
295 for(i = 0; i < key_file->grps_hash_size; i++) {
296 grp_list = key_file->grps_hash[i];
297 while(grp_list != NULL) {
298 grp_len = strlen(grp_list->grp_name);
299 grps[j] = (char *)malloc(sizeof(char) *
300 (grp_len + 1));
301 if(grps[j] == NULL) {
302 free_strs(grps);
303 grps = NULL;
304 return grps;
305 }
306 memcpy(grps[j], grp_list->grp_name,
307 (grp_len + 1));
308 grp_list = grp_list->grp_next;
309 j++;
310 }
311 }
312 grps[j] = NULL;
313 return grps;
314}
315
316//returns the list of keys
317//associated with group name
318char **get_keys
319(
320 const group_table *key_file,
321 const char *grp_name
322)
323{
324 unsigned int grp_hash_code;
325 unsigned int grp_index;
326 unsigned int num_of_keys;
327 unsigned int i;
328 unsigned int j = 0;
329 unsigned int key_len;
330 group *grp;
331 key_value_pair_list *key_val_list;
332 char **keys = NULL;
333
334 if((key_file == NULL) || (grp_name == NULL)
335 || (key_file->num_of_grps == 0) ||
336 (key_file->grps_hash_size == 0) ||
337 (key_file->grps_hash == NULL) ||
338 (!strcmp(grp_name, ""))) {
339 return keys;
340 }
341 grp_hash_code = get_hash_code(grp_name);
342 grp_index = (grp_hash_code % key_file->grps_hash_size);
343 grp = key_file->grps_hash[grp_index];
344 while(grp != NULL) {
345 if(!strcmp(grp_name, grp->grp_name)) {
346 if((grp->num_of_keys == 0)
347 || (grp->keys_hash_size == 0)
348 || (grp->list == 0)) {
349 return keys;
350 }
351 keys = (char **)calloc((grp->num_of_keys + 1),
352 sizeof(char *));
353 if(keys == NULL) {
354 return keys;
355 }
356 for(i = 0; i < grp->keys_hash_size; i++) {
357 key_val_list = grp->list[i];
358 while(key_val_list != NULL) {
359 key_len = strlen(key_val_list->key);
360 keys[j] = (char *)malloc(sizeof(char) *
361 (key_len + 1));
362 if(keys[j] == NULL) {
363 free_strs(keys);
364 keys = NULL;
365 return keys;
366 }
367 memcpy(keys[j], key_val_list->key,
368 (key_len + 1));
369 j++;
370 key_val_list = key_val_list->next;
371 }
372 }
373 keys[j] = NULL;
374 return keys;
375 }
376 grp = grp->grp_next;
377 }
378 return keys;
379}
380
381char *get_value
382(
383 const group_table *key_file,
384 const char *grp_name,
385 const char *key
386)
387{
388 unsigned int grp_hash_code;
389 unsigned int key_hash_code;
390 unsigned int grp_index;
391 unsigned int key_index;
392 unsigned val_len;
393 char *val = NULL;
394 group *grp;
395 key_value_pair_list *list;
396
397 if((key_file == NULL) || (grp_name == NULL)
398 || (key == NULL) || (key_file->grps_hash == NULL)
399 || (key_file->grps_hash_size == 0) || !strcmp(grp_name, "")
400 ||(!strcmp(key, ""))) {
401 return NULL;
402 }
403 grp_hash_code = get_hash_code(grp_name);
404 key_hash_code = get_hash_code(key);
405 grp_index = (grp_hash_code % key_file->grps_hash_size);
406 grp = key_file->grps_hash[grp_index];
407 while(grp != NULL) {
408 if(!strcmp(grp_name, grp->grp_name) && grp->keys_hash_size
409 && grp->list) {
410 key_index = (key_hash_code % grp->keys_hash_size);
411 list = grp->list[key_index];
412 while((list != NULL) && (strcmp(list->key, key))) {
413 list = list->next;
414 }
415 if(list != NULL) {
416 val_len = strlen(list->value);
417 val = (char *)malloc(sizeof(char) * (val_len + 1));
418 if(val != NULL) {
419 memcpy(val, list->value, val_len);
420 val[val_len] = '\0';
421 }
422 }
423 return val;
424 }
425 grp = grp->grp_next;
426 }
427 return val;
428}
429//open the file,
430//read, parse and load
431//returns PARSE_SUCCESS if successfully
432//loaded else PARSE_FAILED
433char parse_load_file
434(
435 group_table *key_file,
436 const char *file
437)
438{
439 FILE *fp;
440 char ret = FALSE;
441
442 if((file == NULL) || !strcmp(file, "")) {
443 ALOGE("File name is null or empty \n");
444 return ret;
445 }
446
447 fp = fopen(file, "r");
448 if(fp == NULL) {
449 ALOGE("could not open file for read\n");
450 return ret;
451 }
452
453 ret = parse_load_frm_fhandler(key_file, fp);
454 fclose(fp);
455
456 return ret;
457}
458
459//Read block of data from file handler
460//extract each line, check kind of line(comment,
461//group, key value pair)
462static char parse_load_frm_fhandler
463(
464 group_table *key_file,
465 FILE *fp
466)
467{
468 char buf[MAX_LINE_LEN];
469 char ret = TRUE;
470 char *line = NULL;
471 void *new_line;
472 char *cur_grp = NULL;
473 unsigned line_len = 0;
474 unsigned line_allocated = 0;
475 unsigned int bytes_read = 0;
476 unsigned int i;
477 bool has_carriage_rtn = false;
478
479 while((bytes_read = fread(buf, 1, MAX_LINE_LEN, fp))) {
480 for(i = 0; i < bytes_read; i++) {
481 if(line_len == line_allocated) {
482 line_allocated += 25;
483 new_line = realloc(line, line_allocated);
484 if(new_line == NULL) {
485 ret = FALSE;
486 ALOGE("memory allocation failed for line\n");
487 break;
488 }
489 line = (char *)new_line;
490 }
491 if((buf[i] == '\n')) {
492 has_carriage_rtn = false;
493 line[line_len] = '\0';
494 ret = parse_line(key_file, line, &cur_grp);
495 line_len = 0;
496 if(ret == FALSE) {
497 ALOGE("could not parse the line, line not proper\n");
498 break;
499 }
500 }else if(buf[i] == '\r') {
501 ALOGE("File has carriage return\n");
502 has_carriage_rtn = true;
503 }else if(has_carriage_rtn) {
504 ALOGE("File format is not proper, no line character\
505 after carraige return\n");
506 ret = FALSE;
507 break;
508 }else {
509 line[line_len] = buf[i];
510 line_len++;
511 }
512 }
513 if (!ret) {
514 break;
515 }
516 }
517 free(line);
518 free(cur_grp);
519
520 return ret;
521}
522
523//checks whether a line is
524//comment or grp or key pair value
525//and accordingly adds to list
526static char parse_line
527(
528 group_table *key_file,
529 const char *line,
530 char **cur_grp
531)
532{
533 const char *line_begin;
534 char *grp_name;
535 unsigned int len;
536
537 if((line == NULL) || (key_file == NULL)) {
538 ALOGE("key file or line is null\n");
539 return FALSE;
540 }
541
542 for(line_begin = line; isspace(*line_begin);
543 line_begin++);
544
545 if(line_is_comment(line_begin)) {
546 ALOGE("line is comment\n");
547 return TRUE;
548 }else if(line_is_grp(key_file, line_begin, cur_grp)) {
549 ALOGE("line is grp\n");
550 return TRUE;
551 }else if(line_is_key_value_pair(key_file, line_begin, *cur_grp)) {
552 ALOGE("line is key value pair\n");
553 return TRUE;
554 }else {
555 ALOGE("line is neither comment, grp nor key value pair\n");
556 return FALSE;
557 }
558}
559
560static char line_is_comment
561(
562 const char *str
563)
564{
565 if(str == NULL) {
566 return FALSE;
567 }else if(((*str) == '#') || ((*str) == '\0')
568 || ((*str) == '\n')) {
569 return TRUE;
570 }else {
571 ALOGE("line is not comment\n");
572 return FALSE;
573 }
574}
575
576//return true if a group
577//name already exist
578//else false
579static char grp_exist
580(
581 const group_table *key_file,
582 const char *new_grp
583)
584{
585 unsigned hash_code;
586 unsigned int index;
587 group *grp;
588
589 if((key_file == NULL) || (new_grp == NULL)
590 || (!key_file->grps_hash_size)) {
591 return FALSE;
592 }else {
593 hash_code = get_hash_code(new_grp);
594 index = hash_code % key_file->grps_hash_size;
595 grp = key_file->grps_hash[index];
596 while(grp != NULL) {
597 if (!strcmp(grp->grp_name, new_grp))
598 return TRUE;
599 grp = grp->grp_next;
600 }
601 return FALSE;
602 }
603}
604
605//Add a group to group
606//table if it does not exist
607static char add_grp
608(
609 group_table *key_file,
610 const char *new_grp
611)
612{
613 unsigned int hash_code;
614 unsigned int index;
615 unsigned int grp_name_len;
616 group *grp;
617
618 if(!grp_exist(key_file, new_grp)) {
619 if((key_file == NULL) || (new_grp == NULL)
620 || !key_file->grps_hash_size) {
621 return FALSE;
622 }
623 hash_code = get_hash_code(new_grp);
624 ALOGE("group hash code is: %u\n", hash_code);
625 index = hash_code % key_file->grps_hash_size;
626 ALOGE("group index is: %u\n", index);
627 grp = alloc_group();
628 if(grp == NULL) {
629 return FALSE;
630 }
631 grp_name_len = strlen(new_grp);
632 grp->grp_name = (char *)malloc(
633 sizeof(char) * (grp_name_len + 1));
634 if(grp->grp_name == NULL) {
635 ALOGE("could not alloc memory for group name\n");
636 ALOGE("Add group failed\n");
637 free_grp_list(grp);
638 return FALSE;
639 }else {
640 memcpy(grp->grp_name, new_grp, (grp_name_len + 1));
641 }
642 grp->grp_next = key_file->grps_hash[index];
643 key_file->grps_hash[index] = grp;
644 key_file->num_of_grps++;
645 return TRUE;
646 }else {
647 return FALSE;
648 }
649}
650
651//checks validity of a group
652//a valid group is
653//inside [] group name must be
654//alphanumeric
655//Example: [grpName]
656static char line_is_grp
657(
658 group_table *key_file,
659 const char *str,
660 char **cur_grp
661)
662{
663 const char *g_start;
664 const char *g_end;
665 char *new_grp;
666 unsigned int grp_len;
667
668 if ((str == NULL) || (key_file == NULL)) {
669 ALOGE("str is null or key file is null\n");
670 return FALSE;
671 }
672 //checks start mark char ']'
673 if(((*str) != '[')) {
674 ALOGE("start mark is not '['\n");
675 return FALSE;
676 }else {
677 str++;
678 g_start = str;
679 }
680 //checks the end char '['
681 while((*str != '\0') && ((*str) != ']')) {
682 str++;
683 }
684 //if end mark group not found
685 if ((*str) != ']') {
686 ALOGE("grp end mark is not '['\n");
687 return FALSE;
688 }else {
689 g_end = (str - 1);
690 }
691
692 str++;
693 //if end mark found checks the rest chars as well
694 //rest chars should be space
695 while(((*str) == ' ') || ((*str) == '\t')) {
696 str++;
697 }
698 if(*str) {
699 ALOGE("after ']' there are some character\n");
700 return FALSE;
701 }
702
703 str = g_start;
704 while((*g_start != '\0') && (g_start != g_end)
705 && isalnum(*g_start)) {
706 g_start++;
707 }
708 if((g_start == g_end) && isalnum(*g_start)) {
709 //look up if already exist
710 //return false else insert the grp in grp table
711 grp_len = (g_end - str + 1);
712 new_grp = (char *)malloc(sizeof(char) * (grp_len + 1));
713 if (new_grp == NULL) {
714 ALOGE("could not alloc memory for new group\n");
715 return FALSE;
716 }
717 memcpy(new_grp, str, grp_len);
718 new_grp[grp_len] = '\0';
719
720 if(add_grp(key_file, new_grp)) {
721 free(*cur_grp);
722 *cur_grp = new_grp;
723 return TRUE;
724 }else {
725 ALOGE("could not add group to group table\n");
726 return FALSE;
727 }
728 }else {
729 return FALSE;
730 }
731}
732
733static char key_exist
734(
735 const group_table *key_file,
736 const char *cur_grp,
737 const char *key
738)
739{
740 unsigned int grp_hash_code;
741 unsigned int key_hash_code;
742 unsigned int grp_index;
743 unsigned int key_index;
744 group *grp = NULL;
745 key_value_pair_list *list = NULL;
746
747 if((key_file != NULL) && (cur_grp != NULL)
748 && (key != NULL) && ((key_file->grps_hash != NULL))
749 && (strcmp(key, ""))) {
750 grp_hash_code = get_hash_code(cur_grp);
751 grp_index = (grp_hash_code % key_file->grps_hash_size);
752 grp = key_file->grps_hash[grp_index];
753 key_hash_code = get_hash_code(key);
754 while((grp != NULL)) {
755 if(!strcmp(cur_grp, grp->grp_name)) {
756 key_index = (key_hash_code % grp->keys_hash_size);
757 if(grp->list)
758 list = grp->list[key_index];
759 while((list != NULL) && strcmp(key, list->key)) {
760 list = list->next;
761 }
762 if(list != NULL){
763 return TRUE;
764 }else{
765 return FALSE;
766 }
767 }
768 grp = grp->grp_next;
769 }
770 if(!grp) {
771 return TRUE;
772 }else {
773 return FALSE;
774 }
775 }else {
776 return FALSE;
777 }
778}
779
780//checks validity of key
781//a valid key must start in
782//a seperate line and key must
783//be alphanumeric and before '='
784//there must not be any space
785//Example: key=value
786static char line_is_key_value_pair
787(
788 group_table *key_file,
789 const char *str,
790 const char *cur_grp
791)
792{
793 char *equal_start;
Smriti Guptaee397202017-12-06 15:00:40 +0530794 char *key = NULL;
795 char *val = NULL;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530796 unsigned key_len;
797 unsigned val_len;
798
799 if((str == NULL) || (cur_grp == NULL) ||
800 !strcmp(cur_grp, "") || (key_file == NULL)) {
801 ALOGE("line is null or cur group or key file is null or empty\n");
802 return FALSE;
803 }
804 equal_start = strchr(str, '=');
805 key_len = (equal_start - str);
806 if((equal_start == NULL) || (equal_start == str)) {
807 ALOGE("line does not have '=' character or no key\n");
808 return FALSE;
809 }
810 while((str != equal_start) && isalnum(*str))
811 str++;
812 if((str == equal_start)) {
813 key = (char *)malloc(sizeof(char) * (key_len + 1));
814 if(key == NULL) {
815 ALOGE("could not alloc memory for new key\n");
816 return FALSE;
817 }
818 equal_start++;
819 val_len = strlen(equal_start);
820 val = (char *)malloc(sizeof(char) * (val_len + 1));
821 if(val == NULL) {
822 ALOGE("could not alloc memory for value\n");
Smriti Guptaee397202017-12-06 15:00:40 +0530823 if(key){
824 free(key);
825 key = NULL;
826 }
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530827 return FALSE;
828 }
829 memcpy(key, (str - key_len), key_len);
830 memcpy(val, equal_start, val_len);
831 key[key_len] = '\0';
832 val[val_len] = '\0';
833 ALOGE("Grp: %s, key: %s, value: %s\n", cur_grp, key, val);
834 return add_key_value_pair(key_file,
835 cur_grp, key, val);
836 }else {
837 ALOGE("key name doesnot have alpha numeric char\n");
838 return FALSE;
839 }
840}
841
842static char add_key_value_pair
843(
844 group_table *key_file,
845 const char *cur_grp,
846 const char *key,
847 const char *val
848)
849{
850 unsigned int grp_hash_code;
851 unsigned int key_hash_code;
852 unsigned int grp_index;
853 unsigned int key_index;
854 unsigned key_len, val_len;
855 group *grp = NULL;
856 key_value_pair_list *list = NULL;
857
858 if((key_file != NULL) && (cur_grp != NULL)
859 && (key != NULL) && ((key_file->grps_hash != NULL))
860 && (strcmp(key, ""))) {
861 grp_hash_code = get_hash_code(cur_grp);
862 ALOGE("grp hash code is %u\n", grp_hash_code);
863 grp_index = (grp_hash_code % key_file->grps_hash_size);
864 ALOGE("grp index is %u\n", grp_index);
865 grp = key_file->grps_hash[grp_index];
866 key_hash_code = get_hash_code(key);
867 while((grp != NULL)) {
868 if(!strcmp(cur_grp, grp->grp_name)) {
869 key_index = (key_hash_code % grp->keys_hash_size);
870 if(grp->list) {
871 list = grp->list[key_index];
872 }else {
873 ALOGE("group list is null\n");
Smriti Guptaee397202017-12-06 15:00:40 +0530874 goto err;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530875 }
876 while((list != NULL) && strcmp(key, list->key)) {
877 list = list->next;
878 }
879 if(list != NULL) {
880 ALOGE("group already contains the key\n");
Smriti Guptaee397202017-12-06 15:00:40 +0530881 goto err;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530882 }else{
883 list = alloc_key_value_pair();
884 if(list == NULL) {
885 ALOGE("add key value failed as could not alloc memory for key\
886 val pair\n");
Smriti Guptaee397202017-12-06 15:00:40 +0530887 goto err;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530888 }
889 key_len = strlen(key);
890 list->key = (char *)malloc(sizeof(char) *
891 (key_len + 1));
892 if(list->key == NULL) {
893 ALOGE("could not alloc memory for key\n");
894 free(list);
Smriti Guptaee397202017-12-06 15:00:40 +0530895 goto err;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530896 }
897 val_len = strlen(val);
898 list->value = (char *)malloc(sizeof(char) *
899 (val_len + 1));
900 if(!list->value) {
901 free(list->key);
902 free(list);
Smriti Guptaee397202017-12-06 15:00:40 +0530903 goto err;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530904 }
905 memcpy(list->key, key, key_len);
906 memcpy(list->value, val, val_len);
Smriti Guptaee397202017-12-06 15:00:40 +0530907 if (key) free((char*)key);
908 if (val) free((char*)val);
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530909 list->key[key_len] = '\0';
910 list->value[val_len] = '\0';
911 list->next = grp->list[key_index];
912 grp->list[key_index] = list;
913 grp->num_of_keys++;
914 return TRUE;
915 }
916 }
917 grp = grp->grp_next;
918 }
919 ALOGE("group does not exist\n");
Smriti Guptaee397202017-12-06 15:00:40 +0530920 goto err;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530921 }
Smriti Guptaee397202017-12-06 15:00:40 +0530922err:
923 if (key) free((char*)key);
924 if (val) free((char*)val);
925 return FALSE;
Venkateshwarlu Domakondaacdd9a42015-03-24 10:28:37 +0530926}