1
|
1 #include <config.h>
|
|
2
|
|
3 #include <stdio.h>
|
|
4 #include <fcntl.h>
|
|
5 #include <pwd.h>
|
|
6 #include <sys/types.h>
|
|
7
|
|
8 #include <wine/winbase.h>
|
|
9 #include <wine/winreg.h>
|
|
10 #include <wine/winnt.h>
|
|
11 #include <wine/winerror.h>
|
|
12
|
|
13 #include <registry.h>
|
128
|
14 //#undef TRACE
|
|
15 //#define TRACE printf
|
1
|
16 struct reg_value
|
|
17 {
|
|
18 int type;
|
|
19 char* name;
|
|
20 int len;
|
|
21 char* value;
|
|
22 };
|
|
23
|
|
24 static int reg_size=0;
|
|
25
|
|
26 static struct reg_value* regs=0;
|
|
27
|
|
28 struct reg_handle_s;
|
|
29 typedef struct reg_handle_s
|
|
30 {
|
|
31 int handle;
|
|
32 char* name;
|
|
33 struct reg_handle_s* next;
|
|
34 struct reg_handle_s* prev;
|
|
35 } reg_handle_t;
|
|
36
|
|
37 static reg_handle_t* head=0;
|
|
38
|
|
39 #define DIR -25
|
|
40
|
178
|
41 extern char *get_path(char *);
|
|
42
|
1
|
43 static void create_registry();
|
|
44 static void open_registry();
|
|
45 static void save_registry();
|
|
46
|
|
47
|
|
48
|
|
49
|
|
50 static void create_registry(){
|
|
51 if(regs)
|
|
52 {
|
|
53 printf("Logic error: create_registry() called with existing registry\n");
|
|
54 save_registry();
|
|
55 return;
|
|
56 }
|
|
57 regs=(struct reg_value*)malloc(3*sizeof(struct reg_value));
|
|
58 regs[0].type=regs[1].type=DIR;
|
|
59 regs[0].name=(char*)malloc(5);
|
|
60 strcpy(regs[0].name, "HKLM");
|
|
61 regs[1].name=(char*)malloc(5);
|
|
62 strcpy(regs[1].name, "HKCU");
|
|
63 regs[0].value=regs[1].value=NULL;
|
|
64 regs[0].len=regs[1].len=0;
|
|
65 reg_size=2;
|
|
66 save_registry();
|
|
67 }
|
|
68 static void open_registry()
|
|
69 {
|
|
70 int fd;
|
|
71 int i;
|
|
72 int len;
|
178
|
73 // struct passwd* pwent;
|
1
|
74 char* pathname;
|
|
75 if(regs)
|
|
76 {
|
|
77 printf("Multiple open_registry(>\n");
|
|
78 return;
|
|
79 }
|
178
|
80 // pwent=getpwuid(getuid());
|
|
81 // pathname=(char*)malloc(strlen(pwent->pw_dir)+20);
|
|
82 // strcpy(pathname, pwent->pw_dir);
|
|
83 // strcat(pathname, "/.mplayer/registry");
|
|
84 pathname = get_path("registry");
|
1
|
85 fd=open(pathname, O_RDONLY);
|
|
86 free(pathname);
|
|
87 if(fd==-1)
|
|
88 {
|
|
89 printf("Creating new registry\n");
|
|
90 create_registry();
|
|
91 return;
|
|
92 }
|
|
93 read(fd, ®_size, 4);
|
|
94 regs=(struct reg_value*)malloc(reg_size*sizeof(struct reg_value));
|
|
95 for(i=0; i<reg_size; i++)
|
|
96 {
|
|
97 read(fd,®s[i].type,4);
|
|
98 read(fd,&len,4);
|
|
99 regs[i].name=(char*)malloc(len+1);
|
|
100 if(regs[i].name==0)
|
|
101 {
|
|
102 reg_size=i+1;
|
|
103 goto error;
|
|
104 }
|
|
105 read(fd, regs[i].name, len);
|
|
106 regs[i].name[len]=0;
|
|
107 read(fd,®s[i].len,4);
|
|
108 regs[i].value=(char*)malloc(regs[i].len+1);
|
|
109 if(regs[i].value==0)
|
|
110 {
|
|
111 free(regs[i].name);
|
|
112 reg_size=i+1;
|
|
113 goto error;
|
|
114 }
|
|
115 read(fd, regs[i].value, regs[i].len);
|
|
116 regs[i].value[regs[i].len]=0;
|
|
117 }
|
|
118 error:
|
|
119 close(fd);
|
|
120 return;
|
|
121 }
|
|
122
|
|
123 static void save_registry()
|
|
124 {
|
|
125 int fd, i, len;
|
178
|
126 // struct passwd* pwent;
|
1
|
127 char* pathname;
|
178
|
128 // pwent=getpwuid(getuid());
|
|
129 // pathname=(char*)malloc(strlen(pwent->pw_dir)+20);
|
|
130 // strcpy(pathname, pwent->pw_dir);
|
|
131 // strcat(pathname, "/.mplayer/registry");
|
|
132 pathname = get_path("registry");
|
1
|
133 fd=open(pathname, O_WRONLY | O_CREAT, 00777);
|
|
134 free(pathname);
|
|
135 if(fd==-1)
|
|
136 {
|
|
137 printf("Failed to open registry file for writing.\n");
|
|
138 return;
|
|
139 }
|
|
140 write(fd, ®_size, 4);
|
|
141 for(i=0; i<reg_size; i++)
|
|
142 {
|
|
143 write(fd, ®s[i].type, 4);
|
|
144 len=strlen(regs[i].name);
|
|
145 write(fd, &len, 4);
|
|
146 write(fd, regs[i].name, len);
|
|
147 write(fd, ®s[i].len, 4);
|
|
148 write(fd, regs[i].value, regs[i].len);
|
|
149 }
|
|
150 close(fd);
|
|
151 }
|
|
152 static reg_handle_t* find_handle_by_name(const char* name)
|
|
153 {
|
|
154 reg_handle_t* t;
|
|
155 for(t=head; t; t=t->prev)
|
|
156 {
|
|
157 if(!strcmp(t->name, name))
|
|
158 {
|
|
159 return t;
|
|
160 }
|
|
161 }
|
|
162 return 0;
|
|
163 }
|
|
164 static struct reg_value* find_value_by_name(const char* name)
|
|
165 {
|
|
166 int i;
|
|
167 for(i=0; i<reg_size; i++)
|
|
168 if(!strcmp(regs[i].name, name))
|
|
169 return regs+i;
|
|
170 return 0;
|
|
171 }
|
|
172 static reg_handle_t* find_handle(int handle)
|
|
173 {
|
|
174 reg_handle_t* t;
|
|
175 for(t=head; t; t=t->prev)
|
|
176 {
|
|
177 if(t->handle==handle)
|
|
178 {
|
|
179 return t;
|
|
180 }
|
|
181 }
|
|
182 return 0;
|
|
183 }
|
|
184 static int generate_handle()
|
|
185 {
|
|
186 static int zz=249;
|
|
187 zz++;
|
|
188 while((zz==HKEY_LOCAL_MACHINE) || (zz==HKEY_CURRENT_USER))
|
|
189 zz++;
|
|
190 return zz;
|
|
191 }
|
|
192
|
|
193 static reg_handle_t* insert_handle(long handle, const char* name)
|
|
194 {
|
|
195 reg_handle_t* t;
|
|
196 t=(reg_handle_t*)malloc(sizeof(reg_handle_t));
|
|
197 if(head==0)
|
|
198 {
|
|
199 t->prev=0;
|
|
200 }
|
|
201 else
|
|
202 {
|
|
203 head->next=t;
|
|
204 t->prev=head;
|
|
205 }
|
|
206 t->next=0;
|
|
207 t->name=(char*)malloc(strlen(name)+1);
|
|
208 strcpy(t->name, name);
|
|
209 t->handle=handle;
|
|
210 head=t;
|
|
211 return t;
|
|
212 }
|
|
213 static char* build_keyname(long key, const char* subkey)
|
|
214 {
|
|
215 char* full_name;
|
|
216 reg_handle_t* t;
|
|
217 if((t=find_handle(key))==0)
|
|
218 {
|
|
219 TRACE("Invalid key\n");
|
|
220 return NULL;
|
|
221 }
|
|
222 if(subkey==NULL)
|
|
223 subkey="<default>";
|
|
224 full_name=(char*)malloc(strlen(t->name)+strlen(subkey)+10);
|
|
225 strcpy(full_name, t->name);
|
|
226 strcat(full_name, "\\");
|
|
227 strcat(full_name, subkey);
|
|
228 return full_name;
|
|
229 }
|
|
230 struct reg_value* insert_reg_value(int handle, const char* name, int type, const void* value, int len)
|
|
231 {
|
|
232 reg_handle_t* t;
|
|
233 struct reg_value* v;
|
|
234 char* fullname;
|
|
235 if((fullname=build_keyname(handle, name))==NULL)
|
|
236 {
|
|
237 TRACE("Invalid handle\n");
|
|
238 return NULL;
|
|
239 }
|
|
240
|
|
241 if((v=find_value_by_name(fullname))==0)
|
|
242 //creating new value in registry
|
|
243 {
|
|
244 if(regs==0)
|
|
245 create_registry();
|
|
246 regs=(struct reg_value*)realloc(regs, sizeof(struct reg_value)*(reg_size+1));
|
|
247 v=regs+reg_size;
|
|
248 reg_size++;
|
|
249 }
|
|
250 else
|
|
251 //replacing old one
|
|
252 {
|
|
253 free(v->value);
|
|
254 free(v->name);
|
|
255 }
|
|
256 v->type=type;
|
|
257 v->len=len;
|
|
258 v->value=(char*)malloc(len);
|
|
259 memcpy(v->value, value, len);
|
|
260 v->name=(char*)malloc(strlen(fullname)+1);
|
|
261 strcpy(v->name, fullname);
|
|
262 save_registry();
|
|
263 return v;
|
|
264 }
|
|
265
|
|
266 static void init_registry()
|
|
267 {
|
340
|
268 #ifdef DETAILED_OUT
|
128
|
269 printf("Initializing registry\n");
|
340
|
270 #endif
|
1
|
271 open_registry();
|
|
272 insert_handle(HKEY_LOCAL_MACHINE, "HKLM");
|
|
273 insert_handle(HKEY_CURRENT_USER, "HKCU");
|
|
274 }
|
|
275 static reg_handle_t* find_handle_2(long key, const char* subkey)
|
|
276 {
|
|
277 char* full_name;
|
|
278 reg_handle_t* t;
|
|
279 if((t=find_handle(key))==0)
|
|
280 {
|
|
281 TRACE("Invalid key\n");
|
|
282 return (reg_handle_t*)-1;
|
|
283 }
|
|
284 if(subkey==NULL)
|
|
285 return t;
|
|
286 full_name=(char*)malloc(strlen(t->name)+strlen(subkey)+10);
|
|
287 strcpy(full_name, t->name);
|
|
288 strcat(full_name, "\\");
|
|
289 strcat(full_name, subkey);
|
|
290 t=find_handle_by_name(full_name);
|
|
291 free(full_name);
|
|
292 return t;
|
|
293 }
|
|
294
|
|
295 long RegOpenKeyExA(long key, const char* subkey, long reserved, long access, int* newkey)
|
|
296 {
|
|
297 char* full_name;
|
|
298 reg_handle_t* t;
|
|
299 struct reg_value* v;
|
|
300 TRACE("Opening key %s\n", subkey);
|
|
301
|
|
302 if(!regs)
|
|
303 init_registry()
|
|
304 ;
|
|
305 /* t=find_handle_2(key, subkey);
|
|
306
|
|
307 if(t==0)
|
|
308 return -1;
|
|
309
|
|
310 if(t==(reg_handle_t*)-1)
|
|
311 return -1;
|
|
312
|
|
313 */ full_name=build_keyname(key, subkey);
|
|
314 if(!full_name)
|
|
315 return -1;
|
|
316 v=find_value_by_name(full_name);
|
|
317
|
|
318 t=insert_handle(generate_handle(), full_name);
|
|
319 *newkey=t->handle;
|
|
320 free(full_name);
|
|
321
|
|
322 return 0;
|
|
323 }
|
|
324 long RegCloseKey(long key)
|
|
325 {
|
|
326 reg_handle_t *handle;
|
|
327 if(key==HKEY_LOCAL_MACHINE)
|
|
328 return 0;
|
|
329 if(key==HKEY_CURRENT_USER)
|
|
330 return 0;
|
|
331 handle=find_handle(key);
|
|
332 if(handle==0)
|
|
333 return 0;
|
|
334 if(handle->prev)
|
|
335 handle->prev->next=handle->next;
|
|
336 if(handle->next)
|
|
337 handle->next->prev=handle->prev;
|
|
338 if(handle->name)
|
|
339 free(handle->name);
|
|
340 if(handle==head)
|
|
341 head=head->prev;
|
|
342 free(handle);
|
|
343 return 1;
|
|
344 }
|
|
345 long RegQueryValueExA(long key, const char* value, int* reserved, int* type, int* data, int* count)
|
|
346 {
|
|
347 struct reg_value* t;
|
|
348 char* c;
|
|
349 TRACE("Querying value %s\n", value);
|
|
350 if(!regs)
|
|
351 init_registry()
|
|
352 ;
|
|
353 c=build_keyname(key, value);
|
|
354 if(c==NULL)
|
|
355 return 1;
|
|
356 if((t=find_value_by_name(c))==0)
|
|
357 {
|
|
358 free(c);
|
|
359 return 2;
|
|
360 }
|
|
361 free(c);
|
|
362 if(type)
|
|
363 *type=t->type;
|
|
364 if(data)
|
|
365 {
|
|
366 memcpy(data, t->value, (t->len<*count)?t->len:*count);
|
|
367 TRACE("returning %d bytes: %d\n", t->len, *(int*)data);
|
|
368 }
|
|
369 if(*count<t->len)
|
|
370 {
|
|
371 *count=t->len;
|
|
372 return ERROR_MORE_DATA;
|
|
373 }
|
|
374 else
|
|
375 {
|
|
376 *count=t->len;
|
|
377 }
|
|
378 return 0;
|
|
379 }
|
|
380 long RegCreateKeyExA(long key, const char* name, long reserved,
|
|
381 void* classs, long options, long security,
|
|
382 void* sec_attr, int* newkey, int* status)
|
|
383 {
|
|
384 reg_handle_t* t;
|
|
385 char* fullname;
|
|
386 struct reg_value* v;
|
|
387 // TRACE("Creating/Opening key %s\n", name);
|
|
388 TRACE("Creating/Opening key %s\n", name);
|
|
389 if(!regs)
|
|
390 init_registry()
|
|
391 ;
|
|
392 fullname=build_keyname(key, name);
|
|
393 if(fullname==NULL)
|
|
394 return 1;
|
|
395 v=find_value_by_name(fullname);
|
|
396 if(v==0)
|
|
397 {
|
|
398 int qw=45708;
|
|
399 v=insert_reg_value(key, name, DIR, &qw, 4);
|
128
|
400 if (status) *status=REG_CREATED_NEW_KEY;
|
1
|
401 // return 0;
|
|
402 }
|
|
403 else
|
128
|
404 if (status) *status=REG_OPENED_EXISTING_KEY;
|
1
|
405
|
|
406 t=insert_handle(generate_handle(), fullname);
|
|
407 *newkey=t->handle;
|
|
408 free(fullname);
|
|
409 return 0;
|
|
410 }
|
|
411 long RegSetValueExA(long key, const char* name, long v1, long v2, const void* data, long size)
|
|
412 {
|
|
413 struct reg_value* t;
|
|
414 char* c;
|
|
415 TRACE("Request to set value %s\n", name);
|
|
416 if(!regs)
|
|
417 init_registry()
|
|
418 ;
|
|
419 c=build_keyname(key, name);
|
|
420 if(c==NULL)
|
|
421 return 1;
|
|
422 insert_reg_value(key, name, v2, data, size);
|
|
423 free(c);
|
|
424 return 0;
|
|
425 }
|