1 /*
2  *  A program to modify a registry file.
3  *
4  *  Copyright (C) 2007 Alan Nisota
5  *
6  * This file is part of MPlayer.
7  *
8  * MPlayer is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * MPlayer is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <unistd.h>
24 #include <getopt.h>
25 #include "loader/registry.c"
26 
27 #include "mp_msg.h"
28 #ifdef __GNUC__
29 #define mp_msg(t, l, m, args...) fprintf(stderr, m, ##args)
30 #else
31 #define mp_msg(t, l, ...) fprintf(stderr, __VA_ARGS__)
32 #endif
33 
34 #include "path.c"
35 
remove_key(long handle,const char * name)36 static void remove_key(long handle, const char* name) {
37     int i, len;
38     char *fullname;
39 
40     fullname = build_keyname(handle, name);
41     len = strlen(fullname);
42     for (i=0; i < reg_size;) {
43         if (!strncmp(regs[i].name, fullname, len)) {
44             free(regs[i].value);
45             free(regs[i].name);
46             memmove(&regs[i], &regs[i+1], --reg_size*sizeof(struct reg_value));
47         } else {
48             i++;
49         }
50     }
51     free(fullname);
52     save_registry();
53 }
54 
parse_key(char * raw,HKEY * root,char * path,char * key)55 static void parse_key(char *raw, HKEY *root, char *path, char *key) {
56     char *tmpkey, *start;
57     tmpkey = strrchr(raw, '\\');
58     if (tmpkey == raw || tmpkey == NULL) {
59         printf("Couldn't process key \"%s\"\n", raw);
60         return;
61     }
62     start = strchr(raw, '\\');
63     if (start == raw || start == NULL) {
64         printf("Couldn't process key \"%s\"\n", raw);
65         return;
66     }
67     if (strncmp(raw, "HKEY_CURRENT_USER\\", 18) == 0 ||
68             strncmp(raw, "HKCU\\", 5) == 0) {
69         *root = HKEY_CURRENT_USER;
70     } else if (strncmp(raw, "HKEY_LOCAL_MACHINE\\", 19) == 0 ||
71             strncmp(raw, "HKLM\\", 5) == 0) {
72         *root = HKEY_LOCAL_MACHINE;
73     } else {
74         printf("Couldn't process key \"%s\"\n", raw);
75         return;
76     }
77     strncpy(key, tmpkey + 1, strlen(tmpkey)-1);
78     key[strlen(tmpkey)-1] = 0;
79     while(*start == '\\')
80         start++;
81     while(*(tmpkey-1) == '\\')
82         tmpkey--;
83     strncpy(path, start, tmpkey - start);
84     path[tmpkey - start] = 0;
85 }
86 
main(int argc,char * argv[])87 int main(int argc, char *argv[]) {
88     int i;
89     long type = REG_SZ;
90     char c, path[256], key[256], *value = NULL;
91     HKEY root = 0;
92     int Option_Index;
93     int list = 0, del = 0;
94     int newkey, status;
95     static struct option Long_Options[] = {
96         {"registry", 1, 0, 'r'},
97         {"list", 0, 0, 'l'},
98         {"key", 1, 0, 'k'},
99         {"value", 1, 0, 'v'},
100         {"type", 1, 0, 't'},
101         {"del", 0, 0, 'd'},
102     };
103     while(1) {
104         c = getopt_long(argc, argv, "r:lk:v:t:id", Long_Options, &Option_Index);
105         if (c == EOF)
106             break;
107         switch(c) {
108           case 'r':
109             localregpathname = optarg;
110             break;
111           case 'l':
112             list = 1;
113             break;
114           case 'k':
115             parse_key(optarg, &root, path, key);
116             break;
117           case 'v':
118             value = optarg;
119             break;
120           case 't':
121             if (!strcmp(optarg, "string"))
122                 type = REG_SZ;
123             else if (!strcmp(optarg, "dword"))
124                 type = REG_DWORD;
125             break;
126           case 'd':
127             del = 1;
128             break;
129         }
130     }
131     if (localregpathname == NULL || (! list && ! root)) {
132         printf("Must specify '-r' and either '-k' or '-l'\n");
133         return 1;
134     }
135     if (del && (list || value)) {
136         printf("Can't specify '-d' along with '-l' or '-v'\n");
137         return 1;
138     }
139     open_registry();
140     insert_handle(HKEY_LOCAL_MACHINE, "HKLM");
141     insert_handle(HKEY_CURRENT_USER, "HKCU");
142 
143     if (del) {
144         char tmpname[256];
145         sprintf(tmpname, "%s\\%s", path, key);
146         remove_key(root, tmpname);
147         return 0;
148     }
149 
150     if (list) {
151         for (i=0; i < reg_size; i++) {
152             if (regs[i].type == DIR) {
153                 printf("Directory:  %s\n", regs[i].name);
154             } else if (regs[i].type == REG_DWORD) {
155                 DWORD v = *(DWORD *)regs[i].value;
156                 printf("%s :: %08x  type: DWORD\n", regs[i].name, v);
157             } else if (regs[i].type == REG_SZ) {
158                 printf("%s :: '%s' len: %d type: String\n",
159                         regs[i].name, regs[i].value, regs[i].len);
160             } else {
161                 printf("%s :: '%s' len: %d type: %08x\n",
162                         regs[i].name, regs[i].value, regs[i].len, regs[i].type);
163             }
164         }
165     }
166     if (root) {
167         RegCreateKeyExA(root, path, 0, 0, 0, 0, 0, &newkey, &status);
168         if (value != NULL) {
169             int len;
170             DWORD v;
171             if (type == REG_DWORD) {
172                 len = sizeof(DWORD);
173                 v = strtoul(value, NULL, 0);
174                 value = (char *)&v;
175             } else
176                 len = strlen(value)+1;
177             printf("%08x -- %d\n", *value, len);
178             RegSetValueExA(newkey, key, 0, type, value, len);
179         }
180     }
181     return 0;
182 }
183