-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathnvram-faker.c
136 lines (118 loc) · 2.92 KB
/
nvram-faker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "nvram-faker.h"
//include before ini.h to override ini.h defaults
#include "nvram-faker-internal.h"
#include "ini.h"
#define RED_ON "\033[22;31m"
#define RED_OFF "\033[22;00m"
#define DEFAULT_KV_PAIR_LEN 1024
static int kv_count=0;
static int key_value_pair_len=DEFAULT_KV_PAIR_LEN;
static char **key_value_pairs=NULL;
static int ini_handler(void *user, const char *section, const char *name,const char *value)
{
int old_kv_len;
char **kv;
char **new_kv;
int i;
if(NULL == user || NULL == section || NULL == name || NULL == value)
{
DEBUG_PRINTF("bad parameter to ini_handler\n");
return 0;
}
kv = *((char ***)user);
if(NULL == kv)
{
LOG_PRINTF("kv is NULL\n");
return 0;
}
DEBUG_PRINTF("kv_count: %d, key_value_pair_len: %d\n", kv_count,key_value_pair_len);
if(kv_count >= key_value_pair_len)
{
old_kv_len=key_value_pair_len;
key_value_pair_len=(key_value_pair_len * 2);
new_kv=(char **)malloc(key_value_pair_len * sizeof(char **));
if(NULL == kv)
{
LOG_PRINTF("Failed to reallocate key value array.\n");
return 0;
}
for(i=0;i<old_kv_len;i++)
{
new_kv[i]=kv[i];
}
free(*(char ***)user);
kv=new_kv;
*(char ***)user=kv;
}
DEBUG_PRINTF("Got %s:%s\n",name,value);
kv[kv_count++]=strdup(name);
kv[kv_count++]=strdup(value);
return 1;
}
void initialize_ini(void)
{
int ret;
DEBUG_PRINTF("Initializing.\n");
if (NULL == key_value_pairs)
{
key_value_pairs=malloc(key_value_pair_len * sizeof(char **));
}
if(NULL == key_value_pairs)
{
LOG_PRINTF("Failed to allocate memory for key value array. Terminating.\n");
exit(1);
}
ret = ini_parse(INI_FILE_PATH,ini_handler,(void *)&key_value_pairs);
if (0 != ret)
{
LOG_PRINTF("ret from ini_parse was: %d\n",ret);
LOG_PRINTF("INI parse failed. Terminating\n");
free(key_value_pairs);
key_value_pairs=NULL;
exit(1);
}else
{
DEBUG_PRINTF("ret from ini_parse was: %d\n",ret);
}
return;
}
void end(void)
{
int i;
for (i=0;i<kv_count;i++)
{
free(key_value_pairs[i]);
}
free(key_value_pairs);
key_value_pairs=NULL;
return;
}
char *nvram_get(const char *key)
{
int i;
int found=0;
char *value;
char *ret;
for(i=0;i<kv_count;i+=2)
{
if(strcmp(key,key_value_pairs[i]) == 0)
{
LOG_PRINTF("%s=%s\n",key,key_value_pairs[i+1]);
found = 1;
value=key_value_pairs[i+1];
break;
}
}
ret = NULL;
if(!found)
{
LOG_PRINTF( RED_ON"%s=Unknown\n"RED_OFF,key);
}else
{
ret=strdup(value);
}
return ret;
}