HeapReAlloc doesn't allocate memory.
[wine.git] / dlls / setupapi / install.c
blob75c3007e2681a8a4e85feace44f7d7e5aaf19784
1 /*
2 * Setupapi install routines
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "winternl.h"
27 #include "winerror.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "setupapi.h"
32 #include "setupapi_private.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
38 /* info passed to callback functions dealing with files */
39 struct files_callback_info
41 HSPFILEQ queue;
42 PCWSTR src_root;
43 UINT copy_flags;
44 HINF layout;
47 /* info passed to callback functions dealing with the registry */
48 struct registry_callback_info
50 HKEY default_root;
51 BOOL delete;
54 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
56 /* Unicode constants */
57 static const WCHAR CopyFiles[] = {'C','o','p','y','F','i','l','e','s',0};
58 static const WCHAR DelFiles[] = {'D','e','l','F','i','l','e','s',0};
59 static const WCHAR RenFiles[] = {'R','e','n','F','i','l','e','s',0};
60 static const WCHAR Ini2Reg[] = {'I','n','i','2','R','e','g',0};
61 static const WCHAR LogConf[] = {'L','o','g','C','o','n','f',0};
62 static const WCHAR AddReg[] = {'A','d','d','R','e','g',0};
63 static const WCHAR DelReg[] = {'D','e','l','R','e','g',0};
64 static const WCHAR UpdateInis[] = {'U','p','d','a','t','e','I','n','i','s',0};
65 static const WCHAR UpdateIniFields[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
68 /***********************************************************************
69 * get_field_string
71 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
73 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
74 WCHAR *static_buffer, DWORD *size )
76 DWORD required;
78 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
79 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
81 /* now grow the buffer */
82 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
83 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
84 *size = required;
85 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
87 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
88 return NULL;
92 /***********************************************************************
93 * copy_files_callback
95 * Called once for each CopyFiles entry in a given section.
97 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
99 struct files_callback_info *info = arg;
101 if (field[0] == '@') /* special case: copy single file */
102 SetupQueueDefaultCopyW( info->queue, info->layout, info->src_root, NULL, field, info->copy_flags );
103 else
104 SetupQueueCopySectionW( info->queue, info->src_root, info->layout, hinf, field, info->copy_flags );
105 return TRUE;
109 /***********************************************************************
110 * delete_files_callback
112 * Called once for each DelFiles entry in a given section.
114 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
116 struct files_callback_info *info = arg;
117 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
118 return TRUE;
122 /***********************************************************************
123 * rename_files_callback
125 * Called once for each RenFiles entry in a given section.
127 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
129 struct files_callback_info *info = arg;
130 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
131 return TRUE;
135 /***********************************************************************
136 * get_root_key
138 * Retrieve the registry root key from its name.
140 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
142 static const WCHAR HKCR[] = {'H','K','C','R',0};
143 static const WCHAR HKCU[] = {'H','K','C','U',0};
144 static const WCHAR HKLM[] = {'H','K','L','M',0};
145 static const WCHAR HKU[] = {'H','K','U',0};
146 static const WCHAR HKR[] = {'H','K','R',0};
148 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
149 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
150 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
151 if (!strcmpiW( name, HKU )) return HKEY_USERS;
152 if (!strcmpiW( name, HKR )) return def_root;
153 return 0;
157 /***********************************************************************
158 * append_multi_sz_value
160 * Append a multisz string to a multisz registry value.
162 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
163 DWORD str_size )
165 DWORD size, type, total;
166 WCHAR *buffer, *p;
168 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
169 if (type != REG_MULTI_SZ) return;
171 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
172 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
174 /* compare each string against all the existing ones */
175 total = size;
176 while (*strings)
178 int len = strlenW(strings) + 1;
180 for (p = buffer; *p; p += strlenW(p) + 1)
181 if (!strcmpiW( p, strings )) break;
183 if (!*p) /* not found, need to append it */
185 memcpy( p, strings, len * sizeof(WCHAR) );
186 p[len] = 0;
187 total += len;
189 strings += len;
191 if (total != size)
193 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
194 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
196 done:
197 HeapFree( GetProcessHeap(), 0, buffer );
201 /***********************************************************************
202 * delete_multi_sz_value
204 * Remove a string from a multisz registry value.
206 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
208 DWORD size, type;
209 WCHAR *buffer, *src, *dst;
211 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
212 if (type != REG_MULTI_SZ) return;
213 /* allocate double the size, one for value before and one for after */
214 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
215 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
216 src = buffer;
217 dst = buffer + size;
218 while (*src)
220 int len = strlenW(src) + 1;
221 if (strcmpiW( src, string ))
223 memcpy( dst, src, len * sizeof(WCHAR) );
224 dst += len;
226 src += len;
228 *dst++ = 0;
229 if (dst != buffer + 2*size) /* did we remove something? */
231 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
232 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
233 (BYTE *)(buffer + size), dst - (buffer + size) );
235 done:
236 HeapFree( GetProcessHeap(), 0, buffer );
240 /***********************************************************************
241 * do_reg_operation
243 * Perform an add/delete registry operation depending on the flags.
245 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
247 DWORD type, size;
249 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
251 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
253 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
255 WCHAR *str;
257 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
258 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
259 SetupGetStringFieldW( context, 5, str, size, NULL );
260 delete_multi_sz_value( hkey, value, str );
261 HeapFree( GetProcessHeap(), 0, str );
263 else RegDeleteValueW( hkey, value );
265 else RegDeleteKeyW( hkey, NULL );
266 return TRUE;
269 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
271 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
273 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
274 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
275 if (!exists & (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
278 switch(flags & FLG_ADDREG_TYPE_MASK)
280 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
281 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
282 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
283 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
284 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
285 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
286 default: type = flags >> 16; break;
289 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
290 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
292 static const WCHAR empty;
293 WCHAR *str = NULL;
295 if (type == REG_MULTI_SZ)
297 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
298 if (size)
300 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
301 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
303 if (flags & FLG_ADDREG_APPEND)
305 if (!str) return TRUE;
306 append_multi_sz_value( hkey, value, str, size );
307 HeapFree( GetProcessHeap(), 0, str );
308 return TRUE;
310 /* else fall through to normal string handling */
312 else
314 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
315 if (size)
317 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
318 SetupGetStringFieldW( context, 5, str, size, NULL );
322 if (type == REG_DWORD)
324 DWORD dw = str ? strtoulW( str, NULL, 16 ) : 0;
325 TRACE( "setting dword %s to %lx\n", debugstr_w(value), dw );
326 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
328 else
330 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
331 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
332 else RegSetValueExW( hkey, value, 0, type, (BYTE *)&empty, sizeof(WCHAR) );
334 HeapFree( GetProcessHeap(), 0, str );
335 return TRUE;
337 else /* get the binary data */
339 BYTE *data = NULL;
341 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
342 if (size)
344 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
345 TRACE( "setting binary data %s len %ld\n", debugstr_w(value), size );
346 SetupGetBinaryField( context, 5, data, size, NULL );
348 RegSetValueExW( hkey, value, 0, type, data, size );
349 HeapFree( GetProcessHeap(), 0, data );
350 return TRUE;
355 /***********************************************************************
356 * registry_callback
358 * Called once for each AddReg and DelReg entry in a given section.
360 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
362 struct registry_callback_info *info = arg;
363 INFCONTEXT context;
364 HKEY root_key, hkey;
366 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
368 for (; ok; ok = SetupFindNextLine( &context, &context ))
370 WCHAR buffer[MAX_INF_STRING_LENGTH];
371 INT flags;
373 /* get root */
374 if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
375 continue;
376 if (!(root_key = get_root_key( buffer, info->default_root )))
377 continue;
379 /* get key */
380 if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
381 *buffer = 0;
383 /* get flags */
384 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
386 if (!info->delete)
388 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
390 else
392 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
393 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
396 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
398 if (RegOpenKeyW( root_key, buffer, &hkey )) continue; /* ignore if it doesn't exist */
400 else if (RegCreateKeyW( root_key, buffer, &hkey ))
402 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
403 continue;
405 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
407 /* get value name */
408 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
409 *buffer = 0;
411 /* and now do it */
412 if (!do_reg_operation( hkey, buffer, &context, flags ))
414 RegCloseKey( hkey );
415 return FALSE;
417 RegCloseKey( hkey );
419 return TRUE;
423 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
425 INFCONTEXT context;
427 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
429 for (; ok; ok = SetupFindNextLine( &context, &context ))
431 WCHAR buffer[MAX_INF_STRING_LENGTH];
432 WCHAR filename[MAX_INF_STRING_LENGTH];
433 WCHAR section[MAX_INF_STRING_LENGTH];
434 WCHAR entry[MAX_INF_STRING_LENGTH];
435 WCHAR string[MAX_INF_STRING_LENGTH];
436 LPWSTR divider;
438 if (!SetupGetStringFieldW( &context, 1, filename,
439 sizeof(filename)/sizeof(WCHAR), NULL ))
440 continue;
442 if (!SetupGetStringFieldW( &context, 2, section,
443 sizeof(section)/sizeof(WCHAR), NULL ))
444 continue;
446 if (!SetupGetStringFieldW( &context, 4, buffer,
447 sizeof(buffer)/sizeof(WCHAR), NULL ))
448 continue;
450 divider = strchrW(buffer,'=');
451 if (divider)
453 *divider = 0;
454 strcpyW(entry,buffer);
455 divider++;
456 strcpyW(string,divider);
458 else
460 strcpyW(entry,buffer);
461 string[0]=0;
464 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
465 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
466 WritePrivateProfileStringW(section,entry,string,filename);
469 return TRUE;
472 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
474 FIXME( "should update ini fields %s\n", debugstr_w(field) );
475 return TRUE;
478 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
480 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
481 return TRUE;
484 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
486 FIXME( "should do logconf %s\n", debugstr_w(field) );
487 return TRUE;
491 /***********************************************************************
492 * iterate_section_fields
494 * Iterate over all fields of a certain key of a certain section
496 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
497 iterate_fields_func callback, void *arg )
499 WCHAR static_buffer[200];
500 WCHAR *buffer = static_buffer;
501 DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
502 INFCONTEXT context;
503 BOOL ret = FALSE;
505 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
506 while (ok)
508 UINT i, count = SetupGetFieldCount( &context );
509 for (i = 1; i <= count; i++)
511 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
512 goto done;
513 if (!callback( hinf, buffer, arg ))
515 ERR("callback failed for %s %s\n", debugstr_w(section), debugstr_w(buffer) );
516 goto done;
519 ok = SetupFindNextMatchLineW( &context, key, &context );
521 ret = TRUE;
522 done:
523 if (buffer && buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
524 return ret;
528 /***********************************************************************
529 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
531 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
532 PCSTR section, PCSTR src_root, UINT flags )
534 UNICODE_STRING sectionW;
535 BOOL ret = FALSE;
537 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
539 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
540 return FALSE;
542 if (!src_root)
543 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
544 NULL, flags );
545 else
547 UNICODE_STRING srcW;
548 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
550 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
551 srcW.Buffer, flags );
552 RtlFreeUnicodeString( &srcW );
554 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
556 RtlFreeUnicodeString( &sectionW );
557 return ret;
561 /***********************************************************************
562 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
564 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
565 PCWSTR section, PCWSTR src_root, UINT flags )
567 struct files_callback_info info;
569 info.queue = queue;
570 info.src_root = src_root;
571 info.copy_flags = flags;
572 info.layout = hlayout;
573 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
577 /***********************************************************************
578 * SetupInstallFromInfSectionA (SETUPAPI.@)
580 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
581 HKEY key_root, PCSTR src_root, UINT copy_flags,
582 PSP_FILE_CALLBACK_A callback, PVOID context,
583 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
585 UNICODE_STRING sectionW, src_rootW;
586 struct callback_WtoA_context ctx;
587 BOOL ret = FALSE;
589 src_rootW.Buffer = NULL;
590 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
592 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
593 return FALSE;
596 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
598 ctx.orig_context = context;
599 ctx.orig_handler = callback;
600 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
601 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
602 &ctx, devinfo, devinfo_data );
603 RtlFreeUnicodeString( &sectionW );
605 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
607 RtlFreeUnicodeString( &src_rootW );
608 return ret;
612 /***********************************************************************
613 * SetupInstallFromInfSectionW (SETUPAPI.@)
615 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
616 HKEY key_root, PCWSTR src_root, UINT copy_flags,
617 PSP_FILE_CALLBACK_W callback, PVOID context,
618 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
620 if (flags & SPINST_FILES)
622 struct files_callback_info info;
623 HSPFILEQ queue;
624 BOOL ret;
626 if (!(queue = SetupOpenFileQueue())) return FALSE;
627 info.queue = queue;
628 info.src_root = src_root;
629 info.copy_flags = copy_flags;
630 info.layout = hinf;
631 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
632 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
633 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
634 SetupCommitFileQueueW( owner, queue, callback, context ));
635 SetupCloseFileQueue( queue );
636 if (!ret) return FALSE;
638 if (flags & SPINST_INIFILES)
640 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
641 !iterate_section_fields( hinf, section, UpdateIniFields,
642 update_ini_fields_callback, NULL ))
643 return FALSE;
645 if (flags & SPINST_INI2REG)
647 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
648 return FALSE;
651 if (flags & SPINST_LOGCONFIG)
653 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
654 return FALSE;
657 if (flags & SPINST_REGISTRY)
659 struct registry_callback_info info;
661 info.default_root = key_root;
662 info.delete = TRUE;
663 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
664 return FALSE;
665 info.delete = FALSE;
666 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
667 return FALSE;
669 if (flags & (SPINST_BITREG|SPINST_REGSVR|SPINST_UNREGSVR|SPINST_PROFILEITEMS|SPINST_COPYINF))
670 FIXME( "unsupported flags %x\n", flags );
671 return TRUE;