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