Mercurial > mplayer.hg
annotate loader/registry.c @ 23884:b341311b8b21
Removing global variables from tv://
Step 3: removing references to old globals from tvi_bsdbt848.c
author | voroshil |
---|---|
date | Sun, 29 Jul 2007 10:20:05 +0000 |
parents | c45f009ce3a7 |
children | 13dcc81c0013 |
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 regpathname = get_path("registry"); |
310 localregpathname = regpathname; | |
3128 | 311 |
1 | 312 open_registry(); |
313 insert_handle(HKEY_LOCAL_MACHINE, "HKLM"); | |
314 insert_handle(HKEY_CURRENT_USER, "HKCU"); | |
315 } | |
3128 | 316 |
1 | 317 static reg_handle_t* find_handle_2(long key, const char* subkey) |
318 { | |
319 char* full_name; | |
320 reg_handle_t* t; | |
321 if((t=find_handle(key))==0) | |
322 { | |
323 TRACE("Invalid key\n"); | |
324 return (reg_handle_t*)-1; | |
325 } | |
326 if(subkey==NULL) | |
327 return t; | |
18878 | 328 full_name=malloc(strlen(t->name)+strlen(subkey)+10); |
1 | 329 strcpy(full_name, t->name); |
330 strcat(full_name, "\\"); | |
331 strcat(full_name, subkey); | |
332 t=find_handle_by_name(full_name); | |
333 free(full_name); | |
334 return t; | |
335 } | |
336 | |
9965 | 337 long __stdcall RegOpenKeyExA(long key, const char* subkey, long reserved, long access, int* newkey) |
1 | 338 { |
339 char* full_name; | |
340 reg_handle_t* t; | |
341 struct reg_value* v; | |
342 TRACE("Opening key %s\n", subkey); | |
3128 | 343 |
1 | 344 if(!regs) |
345 init_registry() | |
3128 | 346 ; |
1 | 347 /* t=find_handle_2(key, subkey); |
3128 | 348 |
1 | 349 if(t==0) |
350 return -1; | |
351 | |
352 if(t==(reg_handle_t*)-1) | |
353 return -1; | |
3128 | 354 */ |
355 full_name=build_keyname(key, subkey); | |
1 | 356 if(!full_name) |
357 return -1; | |
3128 | 358 TRACE("Opening key Fullname %s\n", full_name); |
359 v=find_value_by_name(full_name); | |
1 | 360 |
361 t=insert_handle(generate_handle(), full_name); | |
362 *newkey=t->handle; | |
363 free(full_name); | |
3128 | 364 |
1 | 365 return 0; |
3128 | 366 } |
9965 | 367 long __stdcall RegCloseKey(long key) |
1 | 368 { |
7386 | 369 reg_handle_t *handle; |
370 if(key==(long)HKEY_LOCAL_MACHINE) | |
1 | 371 return 0; |
7386 | 372 if(key==(long)HKEY_CURRENT_USER) |
1 | 373 return 0; |
374 handle=find_handle(key); | |
375 if(handle==0) | |
376 return 0; | |
377 if(handle->prev) | |
378 handle->prev->next=handle->next; | |
379 if(handle->next) | |
380 handle->next->prev=handle->prev; | |
381 if(handle->name) | |
382 free(handle->name); | |
383 if(handle==head) | |
384 head=head->prev; | |
385 free(handle); | |
386 return 1; | |
3128 | 387 } |
388 | |
9965 | 389 long __stdcall RegQueryValueExA(long key, const char* value, int* reserved, int* type, int* data, int* count) |
1 | 390 { |
7386 | 391 struct reg_value* t; |
392 char* c; | |
393 TRACE("Querying value %s\n", value); | |
394 if(!regs) | |
395 init_registry(); | |
3465 | 396 |
7386 | 397 c=build_keyname(key, value); |
398 if (!c) | |
399 return 1; | |
400 t=find_value_by_name(c); | |
401 free(c); | |
402 if (t==0) | |
403 return 2; | |
404 if (type) | |
405 *type=t->type; | |
406 if (data) | |
407 { | |
408 memcpy(data, t->value, (t->len<*count)?t->len:*count); | |
409 TRACE("returning %d bytes: %d\n", t->len, *(int*)data); | |
410 } | |
411 if(*count<t->len) | |
412 { | |
413 *count=t->len; | |
414 return ERROR_MORE_DATA; | |
1 | 415 } |
416 else | |
417 { | |
7386 | 418 *count=t->len; |
419 } | |
1 | 420 return 0; |
3128 | 421 } |
9965 | 422 long __stdcall RegCreateKeyExA(long key, const char* name, long reserved, |
3128 | 423 void* classs, long options, long security, |
424 void* sec_attr, int* newkey, int* status) | |
1 | 425 { |
7386 | 426 reg_handle_t* t; |
427 char* fullname; | |
428 struct reg_value* v; | |
429 // TRACE("Creating/Opening key %s\n", name); | |
430 if(!regs) | |
431 init_registry(); | |
3465 | 432 |
7386 | 433 fullname=build_keyname(key, name); |
434 if (!fullname) | |
435 return 1; | |
436 TRACE("Creating/Opening key %s\n", fullname); | |
437 v=find_value_by_name(fullname); | |
438 if(v==0) | |
439 { | |
440 int qw=45708; | |
441 v=insert_reg_value(key, name, DIR, &qw, 4); | |
442 if (status) *status=REG_CREATED_NEW_KEY; | |
443 // return 0; | |
444 } | |
1 | 445 |
7386 | 446 t=insert_handle(generate_handle(), fullname); |
447 *newkey=t->handle; | |
448 free(fullname); | |
449 return 0; | |
1 | 450 } |
1416 | 451 |
3128 | 452 /* |
453 LONG RegEnumValue( | |
454 HKEY hKey, // handle to key to query | |
455 DWORD dwIndex, // index of value to query | |
456 LPTSTR lpValueName, // address of buffer for value string | |
457 LPDWORD lpcbValueName, // address for size of value buffer | |
458 LPDWORD lpReserved, // reserved | |
459 LPDWORD lpType, // address of buffer for type code | |
460 LPBYTE lpData, // address of buffer for value data | |
461 LPDWORD lpcbData // address for size of data buffer | |
462 ); | |
463 */ | |
464 | |
9965 | 465 long __stdcall RegEnumValueA(HKEY hkey, DWORD index, LPSTR value, LPDWORD val_count, |
1416 | 466 LPDWORD reserved, LPDWORD type, LPBYTE data, LPDWORD count) |
467 { | |
3128 | 468 // currenly just made to support MSZH & ZLIB |
469 //printf("Reg Enum 0x%x %d %s %d data: %p %d %d >%s<\n", hkey, index, | |
470 // value, *val_count, data, *count, reg_size, data); | |
471 reg_handle_t* t = find_handle(hkey); | |
472 if (t && index < 10) | |
1416 | 473 { |
3128 | 474 struct reg_value* v=find_value_by_name(t->name); |
475 if (v) | |
1416 | 476 { |
3128 | 477 memcpy(data, v->value, (v->len < *count) ? v->len : *count); |
478 if(*count < v->len) | |
479 *count = v->len; | |
480 if (type) | |
1416 | 481 *type = v->type; |
482 //printf("Found handle %s\n", v->name); | |
3128 | 483 return 0; |
1416 | 484 } |
485 } | |
3128 | 486 return ERROR_NO_MORE_ITEMS; |
1416 | 487 } |
488 | |
9965 | 489 long __stdcall RegSetValueExA(long key, const char* name, long v1, long v2, const void* data, long size) |
1 | 490 { |
491 struct reg_value* t; | |
492 char* c; | |
7386 | 493 TRACE("Request to set value %s %d\n", name, *(const int*)data); |
1 | 494 if(!regs) |
3465 | 495 init_registry(); |
496 | |
1 | 497 c=build_keyname(key, name); |
498 if(c==NULL) | |
499 return 1; | |
500 insert_reg_value(key, name, v2, data, size); | |
501 free(c); | |
502 return 0; | |
3128 | 503 } |
3465 | 504 |
9965 | 505 long __stdcall RegEnumKeyExA(HKEY hKey, DWORD dwIndex, LPSTR lpName, LPDWORD lpcbName, |
3465 | 506 LPDWORD lpReserved, LPSTR lpClass, LPDWORD lpcbClass, |
507 LPFILETIME lpftLastWriteTime) | |
508 { | |
509 return ERROR_NO_MORE_ITEMS; | |
510 } |