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