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