fix a buffer overflow
[libpicofe.git] / config_file.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2011-2012
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  *  - MAME license.
9  * See the COPYING file in the top-level directory.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "input.h"
16 #include "menu.h"
17 #include "config_file.h"
18 #include "lprintf.h"
19
20 #define array_size(x) (sizeof(x) / sizeof(x[0]))
21
22 static char *mystrip(char *str)
23 {
24         int i, len;
25
26         len = strlen(str);
27         for (i = 0; i < len; i++)
28                 if (str[i] != ' ') break;
29         if (i > 0) memmove(str, str + i, len - i + 1);
30
31         len = strlen(str);
32         for (i = len - 1; i >= 0; i--)
33                 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
34         str[i+1] = 0;
35
36         return str;
37 }
38
39 static void get_line(char *d, size_t size, const char *s)
40 {
41         const char *pe;
42         size_t len;
43
44         for (pe = s; *pe != '\r' && *pe != '\n' && *pe != 0; pe++)
45                 ;
46         len = pe - s;
47         if (len > size - 1)
48                 len = size - 1;
49         strncpy(d, s, len);
50         d[len] = 0;
51
52         mystrip(d);
53 }
54
55 void config_write_keys(FILE *f)
56 {
57         int d;
58
59         for (d = 0; d < IN_MAX_DEVS; d++)
60         {
61                 const int *binds = in_get_dev_binds(d);
62                 const char *name = in_get_dev_name(d, 0, 0);
63                 int k, count = 0;
64
65                 if (binds == NULL || name == NULL)
66                         continue;
67
68                 fprintf(f, "binddev = %s\n", name);
69                 in_get_config(d, IN_CFG_BIND_COUNT, &count);
70
71                 for (k = 0; k < count; k++)
72                 {
73                         int i, kbinds, mask;
74                         char act[32];
75
76                         act[0] = act[31] = 0;
77                         name = in_get_key_name(d, k);
78
79                         kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)];
80                         for (i = 0; kbinds && me_ctrl_actions[i].name != NULL; i++) {
81                                 mask = me_ctrl_actions[i].mask;
82                                 if (mask & kbinds) {
83                                         strncpy(act, me_ctrl_actions[i].name, 31);
84                                         fprintf(f, "bind %s = player1 %s\n", name, mystrip(act));
85                                         kbinds &= ~mask;
86                                 }
87                                 mask = me_ctrl_actions[i].mask << 16;
88                                 if (mask & kbinds) {
89                                         strncpy(act, me_ctrl_actions[i].name, 31);
90                                         fprintf(f, "bind %s = player2 %s\n", name, mystrip(act));
91                                         kbinds &= ~mask;
92                                 }
93                         }
94
95                         kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)];
96                         for (i = 0; kbinds && emuctrl_actions[i].name != NULL; i++) {
97                                 mask = emuctrl_actions[i].mask;
98                                 if (mask & kbinds) {
99                                         strncpy(act, emuctrl_actions[i].name, 31);
100                                         fprintf(f, "bind %s = %s\n", name, mystrip(act));
101                                         kbinds &= ~mask;
102                                 }
103                         }
104                 }
105
106 #ifdef ANALOG_BINDS
107                 for (k = 0; k < array_size(in_adev); k++)
108                 {
109                         if (in_adev[k] == d)
110                                 fprintf(f, "bind_analog = %d\n", k);
111                 }
112 #endif
113         }
114 }
115
116 static int parse_bind_val(const char *val, int *type)
117 {
118         int i;
119
120         *type = IN_BINDTYPE_NONE;
121         if (val[0] == 0)
122                 return 0;
123         
124         if (strncasecmp(val, "player", 6) == 0)
125         {
126                 int player, shift = 0;
127                 player = atoi(val + 6) - 1;
128
129                 if ((unsigned int)player > 1)
130                         return -1;
131                 if (player == 1)
132                         shift = 16;
133
134                 *type = IN_BINDTYPE_PLAYER12;
135                 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
136                         if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
137                                 return me_ctrl_actions[i].mask << shift;
138                 }
139         }
140         for (i = 0; emuctrl_actions[i].name != NULL; i++) {
141                 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
142                         *type = IN_BINDTYPE_EMU;
143                         return emuctrl_actions[i].mask;
144                 }
145         }
146
147         return -1;
148 }
149
150 void config_read_keys(const char *cfg_content)
151 {
152         char dev[256], key[128], *act;
153         const char *p;
154         int bind, bindtype;
155         int dev_id;
156
157         p = cfg_content;
158         while (p != NULL && (p = strstr(p, "binddev = ")) != NULL) {
159                 p += 10;
160
161                 get_line(dev, sizeof(dev), p);
162                 dev_id = in_config_parse_dev(dev);
163                 if (dev_id < 0) {
164                         printf("input: can't handle dev: %s\n", dev);
165                         continue;
166                 }
167
168                 in_unbind_all(dev_id, -1, -1);
169                 while ((p = strstr(p, "bind"))) {
170                         if (strncmp(p, "binddev = ", 10) == 0)
171                                 break;
172
173 #ifdef ANALOG_BINDS
174                         if (strncmp(p, "bind_analog", 11) == 0) {
175                                 int ret = sscanf(p, "bind_analog = %d", &bind);
176                                 p += 11;
177                                 if (ret != 1) {
178                                         printf("input: parse error: %16s..\n", p);
179                                         continue;
180                                 }
181                                 if ((unsigned int)bind >= array_size(in_adev)) {
182                                         printf("input: analog id %d out of range\n", bind);
183                                         continue;
184                                 }
185                                 in_adev[bind] = dev_id;
186                                 continue;
187                         }
188 #endif
189
190                         p += 4;
191                         if (*p != ' ') {
192                                 printf("input: parse error: %16s..\n", p);
193                                 continue;
194                         }
195
196                         get_line(key, sizeof(key), p);
197                         act = strchr(key, '=');
198                         if (act == NULL) {
199                                 printf("parse failed: %16s..\n", p);
200                                 continue;
201                         }
202                         *act = 0;
203                         act++;
204                         mystrip(key);
205                         mystrip(act);
206
207                         bind = parse_bind_val(act, &bindtype);
208                         if (bind != -1 && bind != 0) {
209                                 //printf("bind #%d '%s' %08x (%s)\n", dev_id, key, bind, act);
210                                 in_config_bind_key(dev_id, key, bind, bindtype);
211                         }
212                         else
213                                 lprintf("config: unhandled action \"%s\"\n", act);
214                 }
215         }
216         in_clean_binds();
217 }