blob: ae8b15eeb3d204fe93d1c2f3ed721d9d3a292ab3 [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;
325 unsigned int num_of_keys;
326 unsigned int i;
327 unsigned int j = 0;
328 unsigned int key_len;
329 group *grp;
330 key_value_pair_list *key_val_list;
331 char **keys = NULL;
332
333 if((key_file == NULL) || (grp_name == NULL)
334 || (key_file->num_of_grps == 0) ||
335 (key_file->grps_hash_size == 0) ||
336 (key_file->grps_hash == NULL) ||
337 (!strcmp(grp_name, ""))) {
338 return keys;
339 }
340 grp_hash_code = get_hash_code(grp_name);
341 grp_index = (grp_hash_code % key_file->grps_hash_size);
342 grp = key_file->grps_hash[grp_index];
343 while(grp != NULL) {
344 if(!strcmp(grp_name, grp->grp_name)) {
345 if((grp->num_of_keys == 0)
346 || (grp->keys_hash_size == 0)
347 || (grp->list == 0)) {
348 return keys;
349 }
350 keys = (char **)calloc((grp->num_of_keys + 1),
351 sizeof(char *));
352 if(keys == NULL) {
353 return keys;
354 }
355 for(i = 0; i < grp->keys_hash_size; i++) {
356 key_val_list = grp->list[i];
357 while(key_val_list != NULL) {
358 key_len = strlen(key_val_list->key);
359 keys[j] = (char *)malloc(sizeof(char) *
360 (key_len + 1));
361 if(keys[j] == NULL) {
362 free_strs(keys);
363 keys = NULL;
364 return keys;
365 }
366 memcpy(keys[j], key_val_list->key,
367 (key_len + 1));
368 j++;
369 key_val_list = key_val_list->next;
370 }
371 }
372 keys[j] = NULL;
373 return keys;
374 }
375 grp = grp->grp_next;
376 }
377 return keys;
378}
379
380char *get_value
381(
382 const group_table *key_file,
383 const char *grp_name,
384 const char *key
385)
386{
387 unsigned int grp_hash_code;
388 unsigned int key_hash_code;
389 unsigned int grp_index;
390 unsigned int key_index;
391 unsigned val_len;
392 char *val = NULL;
393 group *grp;
394 key_value_pair_list *list;
395
396 if((key_file == NULL) || (grp_name == NULL)
397 || (key == NULL) || (key_file->grps_hash == NULL)
398 || (key_file->grps_hash_size == 0) || !strcmp(grp_name, "")
399 ||(!strcmp(key, ""))) {
400 return NULL;
401 }
402 grp_hash_code = get_hash_code(grp_name);
403 key_hash_code = get_hash_code(key);
404 grp_index = (grp_hash_code % key_file->grps_hash_size);
405 grp = key_file->grps_hash[grp_index];
406 while(grp != NULL) {
407 if(!strcmp(grp_name, grp->grp_name) && grp->keys_hash_size
408 && grp->list) {
409 key_index = (key_hash_code % grp->keys_hash_size);
410 list = grp->list[key_index];
411 while((list != NULL) && (strcmp(list->key, key))) {
412 list = list->next;
413 }
414 if(list != NULL) {
415 val_len = strlen(list->value);
416 val = (char *)malloc(sizeof(char) * (val_len + 1));
417 if(val != NULL) {
418 memcpy(val, list->value, val_len);
419 val[val_len] = '\0';
420 }
421 }
422 return val;
423 }
424 grp = grp->grp_next;
425 }
426 return val;
427}
428//open the file,
429//read, parse and load
430//returns PARSE_SUCCESS if successfully
431//loaded else PARSE_FAILED
432char parse_load_file
433(
434 group_table *key_file,
435 const char *file
436)
437{
438 FILE *fp;
439 char ret = FALSE;
440
441 if((file == NULL) || !strcmp(file, "")) {
442 ALOGE("File name is null or empty \n");
443 return ret;
444 }
445
446 fp = fopen(file, "r");
447 if(fp == NULL) {
448 ALOGE("could not open file for read\n");
449 return ret;
450 }
451
452 ret = parse_load_frm_fhandler(key_file, fp);
453 fclose(fp);
454
455 return ret;
456}
457
458//Read block of data from file handler
459//extract each line, check kind of line(comment,
460//group, key value pair)
461static char parse_load_frm_fhandler
462(
463 group_table *key_file,
464 FILE *fp
465)
466{
467 char buf[MAX_LINE_LEN];
468 char ret = TRUE;
469 char *line = NULL;
470 void *new_line;
471 char *cur_grp = NULL;
472 unsigned line_len = 0;
473 unsigned line_allocated = 0;
474 unsigned int bytes_read = 0;
475 unsigned int i;
476 bool has_carriage_rtn = false;
477
478 while((bytes_read = fread(buf, 1, MAX_LINE_LEN, fp))) {
479 for(i = 0; i < bytes_read; i++) {
480 if(line_len == line_allocated) {
481 line_allocated += 25;
482 new_line = realloc(line, line_allocated);
483 if(new_line == NULL) {
484 ret = FALSE;
485 ALOGE("memory allocation failed for line\n");
486 break;
487 }
488 line = (char *)new_line;
489 }
490 if((buf[i] == '\n')) {
491 has_carriage_rtn = false;
492 line[line_len] = '\0';
493 ret = parse_line(key_file, line, &cur_grp);
494 line_len = 0;
495 if(ret == FALSE) {
496 ALOGE("could not parse the line, line not proper\n");
497 break;
498 }
499 }else if(buf[i] == '\r') {
500 ALOGE("File has carriage return\n");
501 has_carriage_rtn = true;
502 }else if(has_carriage_rtn) {
503 ALOGE("File format is not proper, no line character\
504 after carraige return\n");
505 ret = FALSE;
506 break;
507 }else {
508 line[line_len] = buf[i];
509 line_len++;
510 }
511 }
512 if (!ret) {
513 break;
514 }
515 }
516 free(line);
517 free(cur_grp);
518
519 return ret;
520}
521
522//checks whether a line is
523//comment or grp or key pair value
524//and accordingly adds to list
525static char parse_line
526(
527 group_table *key_file,
528 const char *line,
529 char **cur_grp
530)
531{
532 const char *line_begin;
533 char *grp_name;
534 unsigned int len;
535
536 if((line == NULL) || (key_file == NULL)) {
537 ALOGE("key file or line is null\n");
538 return FALSE;
539 }
540
541 for(line_begin = line; isspace(*line_begin);
542 line_begin++);
543
544 if(line_is_comment(line_begin)) {
545 ALOGE("line is comment\n");
546 return TRUE;
547 }else if(line_is_grp(key_file, line_begin, cur_grp)) {
548 ALOGE("line is grp\n");
549 return TRUE;
550 }else if(line_is_key_value_pair(key_file, line_begin, *cur_grp)) {
551 ALOGE("line is key value pair\n");
552 return TRUE;
553 }else {
554 ALOGE("line is neither comment, grp nor key value pair\n");
555 return FALSE;
556 }
557}
558
559static char line_is_comment
560(
561 const char *str
562)
563{
564 if(str == NULL) {
565 return FALSE;
566 }else if(((*str) == '#') || ((*str) == '\0')
567 || ((*str) == '\n')) {
568 return TRUE;
569 }else {
570 ALOGE("line is not comment\n");
571 return FALSE;
572 }
573}
574
575//return true if a group
576//name already exist
577//else false
578static char grp_exist
579(
580 const group_table *key_file,
581 const char *new_grp
582)
583{
584 unsigned hash_code;
585 unsigned int index;
586 group *grp;
587
588 if((key_file == NULL) || (new_grp == NULL)
589 || (!key_file->grps_hash_size)) {
590 return FALSE;
591 }else {
592 hash_code = get_hash_code(new_grp);
593 index = hash_code % key_file->grps_hash_size;
594 grp = key_file->grps_hash[index];
595 while(grp != NULL) {
596 if (!strcmp(grp->grp_name, new_grp))
597 return TRUE;
598 grp = grp->grp_next;
599 }
600 return FALSE;
601 }
602}
603
604//Add a group to group
605//table if it does not exist
606static char add_grp
607(
608 group_table *key_file,
609 const char *new_grp
610)
611{
612 unsigned int hash_code;
613 unsigned int index;
614 unsigned int grp_name_len;
615 group *grp;
616
617 if(!grp_exist(key_file, new_grp)) {
618 if((key_file == NULL) || (new_grp == NULL)
619 || !key_file->grps_hash_size) {
620 return FALSE;
621 }
622 hash_code = get_hash_code(new_grp);
623 ALOGE("group hash code is: %u\n", hash_code);
624 index = hash_code % key_file->grps_hash_size;
625 ALOGE("group index is: %u\n", index);
626 grp = alloc_group();
627 if(grp == NULL) {
628 return FALSE;
629 }
630 grp_name_len = strlen(new_grp);
631 grp->grp_name = (char *)malloc(
632 sizeof(char) * (grp_name_len + 1));
633 if(grp->grp_name == NULL) {
634 ALOGE("could not alloc memory for group name\n");
635 ALOGE("Add group failed\n");
636 free_grp_list(grp);
637 return FALSE;
638 }else {
639 memcpy(grp->grp_name, new_grp, (grp_name_len + 1));
640 }
641 grp->grp_next = key_file->grps_hash[index];
642 key_file->grps_hash[index] = grp;
643 key_file->num_of_grps++;
644 return TRUE;
645 }else {
646 return FALSE;
647 }
648}
649
650//checks validity of a group
651//a valid group is
652//inside [] group name must be
653//alphanumeric
654//Example: [grpName]
655static char line_is_grp
656(
657 group_table *key_file,
658 const char *str,
659 char **cur_grp
660)
661{
662 const char *g_start;
663 const char *g_end;
664 char *new_grp;
665 unsigned int grp_len;
666
667 if ((str == NULL) || (key_file == NULL)) {
668 ALOGE("str is null or key file is null\n");
669 return FALSE;
670 }
671 //checks start mark char ']'
672 if(((*str) != '[')) {
673 ALOGE("start mark is not '['\n");
674 return FALSE;
675 }else {
676 str++;
677 g_start = str;
678 }
679 //checks the end char '['
680 while((*str != '\0') && ((*str) != ']')) {
681 str++;
682 }
683 //if end mark group not found
684 if ((*str) != ']') {
685 ALOGE("grp end mark is not '['\n");
686 return FALSE;
687 }else {
688 g_end = (str - 1);
689 }
690
691 str++;
692 //if end mark found checks the rest chars as well
693 //rest chars should be space
694 while(((*str) == ' ') || ((*str) == '\t')) {
695 str++;
696 }
697 if(*str) {
698 ALOGE("after ']' there are some character\n");
699 return FALSE;
700 }
701
702 str = g_start;
703 while((*g_start != '\0') && (g_start != g_end)
704 && isalnum(*g_start)) {
705 g_start++;
706 }
707 if((g_start == g_end) && isalnum(*g_start)) {
708 //look up if already exist
709 //return false else insert the grp in grp table
710 grp_len = (g_end - str + 1);
711 new_grp = (char *)malloc(sizeof(char) * (grp_len + 1));
712 if (new_grp == NULL) {
713 ALOGE("could not alloc memory for new group\n");
714 return FALSE;
715 }
716 memcpy(new_grp, str, grp_len);
717 new_grp[grp_len] = '\0';
718
719 if(add_grp(key_file, new_grp)) {
720 free(*cur_grp);
721 *cur_grp = new_grp;
722 return TRUE;
723 }else {
724 ALOGE("could not add group to group table\n");
725 return FALSE;
726 }
727 }else {
728 return FALSE;
729 }
730}
731
732static char key_exist
733(
734 const group_table *key_file,
735 const char *cur_grp,
736 const char *key
737)
738{
739 unsigned int grp_hash_code;
740 unsigned int key_hash_code;
741 unsigned int grp_index;
742 unsigned int key_index;
743 group *grp = NULL;
744 key_value_pair_list *list = NULL;
745
746 if((key_file != NULL) && (cur_grp != NULL)
747 && (key != NULL) && ((key_file->grps_hash != NULL))
748 && (strcmp(key, ""))) {
749 grp_hash_code = get_hash_code(cur_grp);
750 grp_index = (grp_hash_code % key_file->grps_hash_size);
751 grp = key_file->grps_hash[grp_index];
752 key_hash_code = get_hash_code(key);
753 while((grp != NULL)) {
754 if(!strcmp(cur_grp, grp->grp_name)) {
755 key_index = (key_hash_code % grp->keys_hash_size);
Mahesh Kumar Sharmaca15ddb2015-08-02 11:02:19 -0700756 if(grp->list)
Venkateshwarlu Domakonda1c2c4352014-05-26 14:35:12 +0530757 list = grp->list[key_index];
758 while((list != NULL) && strcmp(key, list->key)) {
759 list = list->next;
760 }
761 if(list != NULL){
762 return TRUE;
763 }else{
764 return FALSE;
765 }
766 }
767 grp = grp->grp_next;
768 }
769 if(!grp) {
770 return TRUE;
771 }else {
772 return FALSE;
773 }
774 }else {
775 return FALSE;
776 }
777}
778
779//checks validity of key
780//a valid key must start in
781//a seperate line and key must
782//be alphanumeric and before '='
783//there must not be any space
784//Example: key=value
785static char line_is_key_value_pair
786(
787 group_table *key_file,
788 const char *str,
789 const char *cur_grp
790)
791{
792 char *equal_start;
793 char *key;
794 char *val;
795 unsigned key_len;
796 unsigned val_len;
797
798 if((str == NULL) || (cur_grp == NULL) ||
799 !strcmp(cur_grp, "") || (key_file == NULL)) {
800 ALOGE("line is null or cur group or key file is null or empty\n");
801 return FALSE;
802 }
803 equal_start = strchr(str, '=');
804 key_len = (equal_start - str);
805 if((equal_start == NULL) || (equal_start == str)) {
806 ALOGE("line does not have '=' character or no key\n");
807 return FALSE;
808 }
809 while((str != equal_start) && isalnum(*str))
810 str++;
811 if((str == equal_start)) {
812 key = (char *)malloc(sizeof(char) * (key_len + 1));
813 if(key == NULL) {
814 ALOGE("could not alloc memory for new key\n");
815 return FALSE;
816 }
817 equal_start++;
818 val_len = strlen(equal_start);
819 val = (char *)malloc(sizeof(char) * (val_len + 1));
820 if(val == NULL) {
821 ALOGE("could not alloc memory for value\n");
822 return FALSE;
823 }
824 memcpy(key, (str - key_len), key_len);
825 memcpy(val, equal_start, val_len);
826 key[key_len] = '\0';
827 val[val_len] = '\0';
828 ALOGE("Grp: %s, key: %s, value: %s\n", cur_grp, key, val);
829 return add_key_value_pair(key_file,
830 cur_grp, key, val);
831 }else {
832 ALOGE("key name doesnot have alpha numeric char\n");
833 return FALSE;
834 }
835}
836
837static char add_key_value_pair
838(
839 group_table *key_file,
840 const char *cur_grp,
841 const char *key,
842 const char *val
843)
844{
845 unsigned int grp_hash_code;
846 unsigned int key_hash_code;
847 unsigned int grp_index;
848 unsigned int key_index;
849 unsigned key_len, val_len;
850 group *grp = NULL;
851 key_value_pair_list *list = NULL;
852
853 if((key_file != NULL) && (cur_grp != NULL)
854 && (key != NULL) && ((key_file->grps_hash != NULL))
855 && (strcmp(key, ""))) {
856 grp_hash_code = get_hash_code(cur_grp);
857 ALOGE("grp hash code is %u\n", grp_hash_code);
858 grp_index = (grp_hash_code % key_file->grps_hash_size);
859 ALOGE("grp index is %u\n", grp_index);
860 grp = key_file->grps_hash[grp_index];
861 key_hash_code = get_hash_code(key);
862 while((grp != NULL)) {
863 if(!strcmp(cur_grp, grp->grp_name)) {
864 key_index = (key_hash_code % grp->keys_hash_size);
865 if(grp->list) {
866 list = grp->list[key_index];
867 }else {
868 ALOGE("group list is null\n");
869 return FALSE;
870 }
871 while((list != NULL) && strcmp(key, list->key)) {
872 list = list->next;
873 }
874 if(list != NULL) {
875 ALOGE("group already contains the key\n");
876 return FALSE;
877 }else{
878 list = alloc_key_value_pair();
879 if(list == NULL) {
880 ALOGE("add key value failed as could not alloc memory for key\
881 val pair\n");
882 return FALSE;
883 }
884 key_len = strlen(key);
885 list->key = (char *)malloc(sizeof(char) *
886 (key_len + 1));
887 if(list->key == NULL) {
888 ALOGE("could not alloc memory for key\n");
889 free(list);
890 return FALSE;
891 }
892 val_len = strlen(val);
893 list->value = (char *)malloc(sizeof(char) *
894 (val_len + 1));
895 if(!list->value) {
896 free(list->key);
897 free(list);
898 return FALSE;
899 }
900 memcpy(list->key, key, key_len);
901 memcpy(list->value, val, val_len);
902 list->key[key_len] = '\0';
903 list->value[val_len] = '\0';
904 list->next = grp->list[key_index];
905 grp->list[key_index] = list;
906 grp->num_of_keys++;
907 return TRUE;
908 }
909 }
910 grp = grp->grp_next;
911 }
912 ALOGE("group does not exist\n");
913 return FALSE;
914 }else {
915 return FALSE;
916 }
917}