Mercurial > mplayer.hg
annotate loader/registry.c @ 21896:b2640a639ac7
Cosmetics
author | reimar |
---|---|
date | Sat, 13 Jan 2007 12:56:42 +0000 |
parents | a2e02e6b6379 |
children | 15ca243429f9 |
rev | line source |
---|---|
15166
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
9965
diff
changeset
|
1 /* |
18783 | 2 * Modified for use with MPlayer, detailed changelog at |
3 * http://svn.mplayerhq.hu/mplayer/trunk/ | |
15166
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
9965
diff
changeset
|
4 * $Id$ |
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
9965
diff
changeset
|
5 */ |
f5537cc95b02
Mark modified imported files as such to comply with GPL ¡ø2a.
diego
parents:
9965
diff
changeset
|
6 |
3465 | 7 #include "config.h" |
21261
a2e02e6b6379
Rename config.h --> debug.h and include config.h explicitly.
diego
parents:
18889
diff
changeset
|
8 #include "debug.h" |
1 | 9 |
10 #include <stdio.h> | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
11 #include <stdlib.h> |
1 | 12 #include <fcntl.h> |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
13 #include <unistd.h> |
1 | 14 #include <pwd.h> |
15 #include <sys/types.h> | |
16 | |
7386 | 17 #include "wine/winbase.h" |
18 #include "wine/winreg.h" | |
19 #include "wine/winnt.h" | |
20 #include "wine/winerror.h" | |
1 | 21 |
3465 | 22 #include "ext.h" |
23 #include "registry.h" | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
24 |
128 | 25 //#undef TRACE |
26 //#define TRACE printf | |
3128 | 27 |
18889
e60c8c7399d2
get_path as const, patch by Stefan Huehner, stefan AT huehner-org
reynaldo
parents:
18878
diff
changeset
|
28 extern char *get_path ( const char * ); |
3134 | 29 |
4384 | 30 // ...can be set before init_registry() call |
31 char* regpathname = NULL; | |
3134 | 32 |
4384 | 33 static char* localregpathname = NULL; |
3134 | 34 |
35 typedef struct reg_handle_s | |
36 { | |
37 int handle; | |
38 char* name; | |
39 struct reg_handle_s* next; | |
40 struct reg_handle_s* prev; | |
41 } reg_handle_t; | |
42 | |
1 | 43 struct reg_value |
44 { | |
45 int type; | |
46 char* name; | |
47 int len; | |
48 char* value; | |
49 }; | |
50 | |
3134 | 51 static struct reg_value* regs = NULL; |
52 static int reg_size; | |
53 static reg_handle_t* head = NULL; | |
1 | 54 |
55 #define DIR -25 | |
56 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
57 static void create_registry(void); |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
58 static void open_registry(void); |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
59 static void save_registry(void); |
3128 | 60 static void init_registry(void); |
1 | 61 |
62 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
63 static void create_registry(void){ |
1 | 64 if(regs) |
65 { | |
66 printf("Logic error: create_registry() called with existing registry\n"); | |
67 save_registry(); | |
68 return; | |
3128 | 69 } |
18878 | 70 regs=malloc(3*sizeof(struct reg_value)); |
1 | 71 regs[0].type=regs[1].type=DIR; |
18878 | 72 regs[0].name=malloc(5); |
1 | 73 strcpy(regs[0].name, "HKLM"); |
18878 | 74 regs[1].name=malloc(5); |
1 | 75 strcpy(regs[1].name, "HKCU"); |
76 regs[0].value=regs[1].value=NULL; | |
77 regs[0].len=regs[1].len=0; | |
78 reg_size=2; | |
3134 | 79 head = 0; |
1 | 80 save_registry(); |
81 } | |
3134 | 82 |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
83 static void open_registry(void) |
1 | 84 { |
85 int fd; | |
86 int i; | |
3128 | 87 unsigned int len; |
1 | 88 if(regs) |
89 { | |
90 printf("Multiple open_registry(>\n"); | |
91 return; | |
92 } | |
3134 | 93 fd = open(localregpathname, O_RDONLY); |
3128 | 94 if (fd == -1) |
1 | 95 { |
96 printf("Creating new registry\n"); | |
97 create_registry(); | |
98 return; | |
3128 | 99 } |
1 | 100 read(fd, ®_size, 4); |
18878 | 101 regs=malloc(reg_size*sizeof(struct reg_value)); |
3134 | 102 head = 0; |
1 | 103 for(i=0; i<reg_size; i++) |
104 { | |
105 read(fd,®s[i].type,4); | |
106 read(fd,&len,4); | |
18878 | 107 regs[i].name=malloc(len+1); |
1 | 108 if(regs[i].name==0) |
109 { | |
110 reg_size=i+1; | |
111 goto error; | |
112 } | |
113 read(fd, regs[i].name, len); | |
114 regs[i].name[len]=0; | |
115 read(fd,®s[i].len,4); | |
18878 | 116 regs[i].value=malloc(regs[i].len+1); |
1 | 117 if(regs[i].value==0) |
118 { | |
3134 | 119 free(regs[i].name); |
1 | 120 reg_size=i+1; |
121 goto error; | |
122 } | |
123 read(fd, regs[i].value, regs[i].len); | |
124 regs[i].value[regs[i].len]=0; | |
125 } | |
126 error: | |
127 close(fd); | |
128 return; | |
129 } | |
130 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
131 static void save_registry(void) |
1 | 132 { |
3128 | 133 int fd, i; |
134 if (!regs) | |
135 init_registry(); | |
3134 | 136 fd = open(localregpathname, O_WRONLY | O_CREAT, 00666); |
3128 | 137 if (fd == -1) |
1 | 138 { |
3128 | 139 printf("Failed to open registry file '%s' for writing.\n", |
3134 | 140 localregpathname); |
3128 | 141 return; |
1 | 142 } |
143 write(fd, ®_size, 4); | |
144 for(i=0; i<reg_size; i++) | |
145 { | |
3128 | 146 unsigned len=strlen(regs[i].name); |
1 | 147 write(fd, ®s[i].type, 4); |
148 write(fd, &len, 4); | |
149 write(fd, regs[i].name, len); | |
150 write(fd, ®s[i].len, 4); | |
151 write(fd, regs[i].value, regs[i].len); | |
152 } | |
153 close(fd); | |
154 } | |
3134 | 155 |
156 void free_registry(void) | |
157 { | |
158 reg_handle_t* t = head; | |
159 while (t) | |
160 { | |
161 reg_handle_t* f = t; | |
162 if (t->name) | |
163 free(t->name); | |
164 t=t->prev; | |
165 free(f); | |
166 } | |
167 head = 0; | |
168 if (regs) | |
169 { | |
170 int i; | |
171 for(i=0; i<reg_size; i++) | |
172 { | |
173 free(regs[i].name); | |
174 free(regs[i].value); | |
175 } | |
176 free(regs); | |
177 regs = 0; | |
178 } | |
3465 | 179 |
180 if (localregpathname && localregpathname != regpathname) | |
3134 | 181 free(localregpathname); |
3465 | 182 localregpathname = 0; |
3134 | 183 } |
184 | |
185 | |
1 | 186 static reg_handle_t* find_handle_by_name(const char* name) |
187 { | |
188 reg_handle_t* t; | |
189 for(t=head; t; t=t->prev) | |
190 { | |
191 if(!strcmp(t->name, name)) | |
192 { | |
193 return t; | |
194 } | |
195 } | |
196 return 0; | |
197 } | |
198 static struct reg_value* find_value_by_name(const char* name) | |
199 { | |
200 int i; | |
201 for(i=0; i<reg_size; i++) | |
202 if(!strcmp(regs[i].name, name)) | |
203 return regs+i; | |
204 return 0; | |
205 } | |
206 static reg_handle_t* find_handle(int handle) | |
207 { | |
208 reg_handle_t* t; | |
209 for(t=head; t; t=t->prev) | |
210 { | |
211 if(t->handle==handle) | |
212 { | |
213 return t; | |
214 } | |
215 } | |
216 return 0; | |
3128 | 217 } |
1 | 218 static int generate_handle() |
219 { | |
7386 | 220 static unsigned int zz=249; |
1 | 221 zz++; |
222 while((zz==HKEY_LOCAL_MACHINE) || (zz==HKEY_CURRENT_USER)) | |
223 zz++; | |
224 return zz; | |
225 } | |
226 | |
227 static reg_handle_t* insert_handle(long handle, const char* name) | |
228 { | |
229 reg_handle_t* t; | |
18878 | 230 t=malloc(sizeof(reg_handle_t)); |
1 | 231 if(head==0) |
232 { | |
233 t->prev=0; | |
234 } | |
235 else | |
236 { | |
237 head->next=t; | |
238 t->prev=head; | |
239 } | |
240 t->next=0; | |
18878 | 241 t->name=malloc(strlen(name)+1); |
1 | 242 strcpy(t->name, name); |
243 t->handle=handle; | |
244 head=t; | |
245 return t; | |
246 } | |
247 static char* build_keyname(long key, const char* subkey) | |
248 { | |
249 char* full_name; | |
250 reg_handle_t* t; | |
251 if((t=find_handle(key))==0) | |
252 { | |
253 TRACE("Invalid key\n"); | |
254 return NULL; | |
255 } | |
256 if(subkey==NULL) | |
257 subkey="<default>"; | |
18878 | 258 full_name=malloc(strlen(t->name)+strlen(subkey)+10); |
1 | 259 strcpy(full_name, t->name); |
260 strcat(full_name, "\\"); | |
261 strcat(full_name, subkey); | |
262 return full_name; | |
263 } | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
264 static struct reg_value* insert_reg_value(int handle, const char* name, int type, const void* value, int len) |
1 | 265 { |
266 reg_handle_t* t; | |
267 struct reg_value* v; | |
268 char* fullname; | |
269 if((fullname=build_keyname(handle, name))==NULL) | |
270 { | |
271 TRACE("Invalid handle\n"); | |
272 return NULL; | |
273 } | |
274 | |
275 if((v=find_value_by_name(fullname))==0) | |
276 //creating new value in registry | |
277 { | |
278 if(regs==0) | |
279 create_registry(); | |
280 regs=(struct reg_value*)realloc(regs, sizeof(struct reg_value)*(reg_size+1)); | |
3134 | 281 //regs=(struct reg_value*)my_realloc(regs, sizeof(struct reg_value)*(reg_size+1)); |
1 | 282 v=regs+reg_size; |
283 reg_size++; | |
284 } | |
285 else | |
286 //replacing old one | |
287 { | |
3134 | 288 free(v->value); |
289 free(v->name); | |
1 | 290 } |
7386 | 291 TRACE("RegInsert '%s' %p v:%d len:%d\n", name, value, *(int*)value, len); |
1 | 292 v->type=type; |
293 v->len=len; | |
18878 | 294 v->value=malloc(len); |
1 | 295 memcpy(v->value, value, len); |
18878 | 296 v->name=malloc(strlen(fullname)+1); |
1 | 297 strcpy(v->name, fullname); |
3134 | 298 free(fullname); |
1 | 299 save_registry(); |
300 return v; | |
301 } | |
302 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
340
diff
changeset
|
303 static void init_registry(void) |
1 | 304 { |
3128 | 305 TRACE("Initializing registry\n"); |
306 // can't be free-ed - it's static and probably thread | |
307 // unsafe structure which is stored in glibc | |
308 | |
3465 | 309 #ifdef MPLAYER |
310 regpathname = get_path("registry"); | |
311 localregpathname = regpathname; | |
3128 | 312 #else |
3465 | 313 // regpathname is an external pointer |
314 // | |
315 // registry.c is holding it's own internal pointer | |
316 // localregpathname - which is being allocate/deallocated | |
317 | |
3134 | 318 if (localregpathname == 0) |
3128 | 319 { |
3134 | 320 const char* pthn = regpathname; |
321 if (!regpathname) | |
322 { | |
3465 | 323 // avifile - for now reading data from user's home |
3134 | 324 struct passwd* pwent; |
325 pwent = getpwuid(geteuid()); | |
326 pthn = pwent->pw_dir; | |
327 } | |
328 | |
18878 | 329 localregpathname = malloc(strlen(pthn)+20); |
3134 | 330 strcpy(localregpathname, pthn); |
331 strcat(localregpathname, "/.registry"); | |
3128 | 332 } |
340 | 333 #endif |
3128 | 334 |
1 | 335 open_registry(); |
336 insert_handle(HKEY_LOCAL_MACHINE, "HKLM"); | |
337 insert_handle(HKEY_CURRENT_USER, "HKCU"); | |
338 } | |
3128 | 339 |
1 | 340 static reg_handle_t* find_handle_2(long key, const char* subkey) |
341 { | |
342 char* full_name; | |
343 reg_handle_t* t; | |
344 if((t=find_handle(key))==0) | |
345 { | |
346 TRACE("Invalid key\n"); | |
347 return (reg_handle_t*)-1; | |
348 } | |
349 if(subkey==NULL) | |
350 return t; | |
18878 | 351 full_name=malloc(strlen(t->name)+strlen(subkey)+10); |
1 | 352 strcpy(full_name, t->name); |
353 strcat(full_name, "\\"); | |
354 strcat(full_name, subkey); | |
355 t=find_handle_by_name(full_name); | |
356 free(full_name); | |
357 return t; | |
358 } | |
359 | |
9965 | 360 long __stdcall RegOpenKeyExA(long key, const char* subkey, long reserved, long access, int* newkey) |
1 | 361 { |
362 char* full_name; | |
363 reg_handle_t* t; | |
364 struct reg_value* v; | |
365 TRACE("Opening key %s\n", subkey); | |
3128 | 366 |
1 | 367 if(!regs) |
368 init_registry() | |
3128 | 369 ; |
1 | 370 /* t=find_handle_2(key, subkey); |
3128 | 371 |
1 | 372 if(t==0) |
373 return -1; | |
374 | |
375 if(t==(reg_handle_t*)-1) | |
376 return -1; | |
3128 | 377 */ |
378 full_name=build_keyname(key, subkey); | |
1 | 379 if(!full_name) |
380 return -1; | |
3128 | 381 TRACE("Opening key Fullname %s\n", full_name); |
382 v=find_value_by_name(full_name); | |
1 | 383 |
384 t=insert_handle(generate_handle(), full_name); | |
385 *newkey=t->handle; | |
386 free(full_name); | |
3128 | 387 |
1 | 388 return 0; |
3128 | 389 } |
9965 | 390 long __stdcall RegCloseKey(long key) |
1 | 391 { |
7386 | 392 reg_handle_t *handle; |
393 if(key==(long)HKEY_LOCAL_MACHINE) | |
1 | 394 return 0; |
7386 | 395 if(key==(long)HKEY_CURRENT_USER) |
1 | 396 return 0; |
397 handle=find_handle(key); | |
398 if(handle==0) | |
399 return 0; | |
400 if(handle->prev) | |
401 handle->prev->next=handle->next; | |
402 if(handle->next) | |
403 handle->next->prev=handle->prev; | |
404 if(handle->name) | |
405 free(handle->name); | |
406 if(handle==head) | |
407 head=head->prev; | |
408 free(handle); | |
409 return 1; | |
3128 | 410 } |
411 | |
9965 | 412 long __stdcall RegQueryValueExA(long key, const char* value, int* reserved, int* type, int* data, int* count) |
1 | 413 { |
7386 | 414 struct reg_value* t; |
415 char* c; | |
416 TRACE("Querying value %s\n", value); | |
417 if(!regs) | |
418 init_registry(); | |
3465 | 419 |
7386 | 420 c=build_keyname(key, value); |
421 if (!c) | |
422 return 1; | |
423 t=find_value_by_name(c); | |
424 free(c); | |
425 if (t==0) | |
426 return 2; | |
427 if (type) | |
428 *type=t->type; | |
429 if (data) | |
430 { | |
431 memcpy(data, t->value, (t->len<*count)?t->len:*count); | |
432 TRACE("returning %d bytes: %d\n", t->len, *(int*)data); | |
433 } | |
434 if(*count<t->len) | |
435 { | |
436 *count=t->len; | |
437 return ERROR_MORE_DATA; | |
1 | 438 } |
439 else | |
440 { | |
7386 | 441 *count=t->len; |
442 } | |
1 | 443 return 0; |
3128 | 444 } |
9965 | 445 long __stdcall RegCreateKeyExA(long key, const char* name, long reserved, |
3128 | 446 void* classs, long options, long security, |
447 void* sec_attr, int* newkey, int* status) | |
1 | 448 { |
7386 | 449 reg_handle_t* t; |
450 char* fullname; | |
451 struct reg_value* v; | |
452 // TRACE("Creating/Opening key %s\n", name); | |
453 if(!regs) | |
454 init_registry(); | |
3465 | 455 |
7386 | 456 fullname=build_keyname(key, name); |
457 if (!fullname) | |
458 return 1; | |
459 TRACE("Creating/Opening key %s\n", fullname); | |
460 v=find_value_by_name(fullname); | |
461 if(v==0) | |
462 { | |
463 int qw=45708; | |
464 v=insert_reg_value(key, name, DIR, &qw, 4); | |
465 if (status) *status=REG_CREATED_NEW_KEY; | |
466 // return 0; | |
467 } | |
1 | 468 |
7386 | 469 t=insert_handle(generate_handle(), fullname); |
470 *newkey=t->handle; | |
471 free(fullname); | |
472 return 0; | |
1 | 473 } |
1416 | 474 |
3128 | 475 /* |
476 LONG RegEnumValue( | |
477 HKEY hKey, // handle to key to query | |
478 DWORD dwIndex, // index of value to query | |
479 LPTSTR lpValueName, // address of buffer for value string | |
480 LPDWORD lpcbValueName, // address for size of value buffer | |
481 LPDWORD lpReserved, // reserved | |
482 LPDWORD lpType, // address of buffer for type code | |
483 LPBYTE lpData, // address of buffer for value data | |
484 LPDWORD lpcbData // address for size of data buffer | |
485 ); | |
486 */ | |
487 | |
9965 | 488 long __stdcall RegEnumValueA(HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count, |
1416 | 489 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count) |
490 { | |
3128 | 491 // currenly just made to support MSZH & ZLIB |
492 //printf("Reg Enum 0x%x %d %s %d data: %p %d %d >%s<\n", hkey, index, | |
493 // value, *val_count, data, *count, reg_size, data); | |
494 reg_handle_t* t = find_handle(hkey); | |
495 if (t && index < 10) | |
1416 | 496 { |
3128 | 497 struct reg_value* v=find_value_by_name(t->name); |
498 if (v) | |
1416 | 499 { |
3128 | 500 memcpy(data, v->value, (v->len < *count) ? v->len : *count); |
501 if(*count < v->len) | |
502 *count = v->len; | |
503 if (type) | |
1416 | 504 *type = v->type; |
505 //printf("Found handle %s\n", v->name); | |
3128 | 506 return 0; |
1416 | 507 } |
508 } | |
3128 | 509 return ERROR_NO_MORE_ITEMS; |
1416 | 510 } |
511 | |
9965 | 512 long __stdcall RegSetValueExA(long key, const char* name, long v1, long v2, const void* data, long size) |
1 | 513 { |
514 struct reg_value* t; | |
515 char* c; | |
7386 | 516 TRACE("Request to set value %s %d\n", name, *(const int*)data); |
1 | 517 if(!regs) |
3465 | 518 init_registry(); |
519 | |
1 | 520 c=build_keyname(key, name); |
521 if(c==NULL) | |
522 return 1; | |
523 insert_reg_value(key, name, v2, data, size); | |
524 free(c); | |
525 return 0; | |
3128 | 526 } |
3465 | 527 |
9965 | 528 long __stdcall RegEnumKeyExA(HKEY hKey, DWORD dwIndex, LPSTR lpName, LPDWORD lpcbName, |
3465 | 529 LPDWORD lpReserved, LPSTR lpClass, LPDWORD lpcbClass, |
530 LPFILETIME lpftLastWriteTime) | |
531 { | |
532 return ERROR_NO_MORE_ITEMS; | |
533 } |