richedit: Don't put cursor in the table row start paragraph.
[wine/hacks.git] / dlls / msi / source.c
blob5a714b127148f8b72879cc1c7ebf02a2e8174127
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msipriv.h"
35 #include "wincrypt.h"
36 #include "winver.h"
37 #include "winuser.h"
38 #include "wine/unicode.h"
39 #include "sddl.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 * These apis are defined in MSI 3.0
47 typedef struct tagMediaInfo
49 struct list entry;
50 LPWSTR path;
51 WCHAR szIndex[10];
52 DWORD index;
53 } media_info;
55 static UINT OpenSourceKey(LPCWSTR szProduct, HKEY* key, DWORD dwOptions,
56 MSIINSTALLCONTEXT context, BOOL create)
58 HKEY rootkey = 0;
59 UINT rc = ERROR_FUNCTION_FAILED;
60 static const WCHAR szSourceList[] = {'S','o','u','r','c','e','L','i','s','t',0};
62 if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
64 if (dwOptions & MSICODE_PATCH)
65 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
66 else
67 rc = MSIREG_OpenUserProductsKey(szProduct, &rootkey, create);
69 else if (context == MSIINSTALLCONTEXT_USERMANAGED)
71 if (dwOptions & MSICODE_PATCH)
72 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
73 else
74 rc = MSIREG_OpenLocalManagedProductKey(szProduct, &rootkey, create);
76 else if (context == MSIINSTALLCONTEXT_MACHINE)
78 if (dwOptions & MSICODE_PATCH)
79 rc = MSIREG_OpenPatchesKey(szProduct, &rootkey, create);
80 else
81 rc = MSIREG_OpenLocalClassesProductKey(szProduct, &rootkey, create);
84 if (rc != ERROR_SUCCESS)
86 if (dwOptions & MSICODE_PATCH)
87 return ERROR_UNKNOWN_PATCH;
88 else
89 return ERROR_UNKNOWN_PRODUCT;
92 if (create)
93 rc = RegCreateKeyW(rootkey, szSourceList, key);
94 else
96 rc = RegOpenKeyW(rootkey,szSourceList, key);
97 if (rc != ERROR_SUCCESS)
98 rc = ERROR_BAD_CONFIGURATION;
101 return rc;
104 static UINT OpenMediaSubkey(HKEY rootkey, HKEY *key, BOOL create)
106 UINT rc;
107 static const WCHAR media[] = {'M','e','d','i','a',0};
109 if (create)
110 rc = RegCreateKeyW(rootkey, media, key);
111 else
112 rc = RegOpenKeyW(rootkey,media, key);
114 return rc;
117 static UINT OpenNetworkSubkey(HKEY rootkey, HKEY *key, BOOL create)
119 UINT rc;
120 static const WCHAR net[] = {'N','e','t',0};
122 if (create)
123 rc = RegCreateKeyW(rootkey, net, key);
124 else
125 rc = RegOpenKeyW(rootkey, net, key);
127 return rc;
130 static UINT OpenURLSubkey(HKEY rootkey, HKEY *key, BOOL create)
132 UINT rc;
133 static const WCHAR URL[] = {'U','R','L',0};
135 if (create)
136 rc = RegCreateKeyW(rootkey, URL, key);
137 else
138 rc = RegOpenKeyW(rootkey, URL, key);
140 return rc;
143 /******************************************************************
144 * MsiSourceListEnumMediaDisksA (MSI.@)
146 UINT WINAPI MsiSourceListEnumMediaDisksA(LPCSTR szProductCodeOrPatchCode,
147 LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
148 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
149 LPSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
150 LPSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
152 LPWSTR product = NULL;
153 LPWSTR usersid = NULL;
154 LPWSTR volume = NULL;
155 LPWSTR prompt = NULL;
156 UINT r = ERROR_INVALID_PARAMETER;
158 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n", debugstr_a(szProductCodeOrPatchCode),
159 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, pdwDiskId,
160 szVolumeLabel, pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
162 if (szDiskPrompt && !pcchDiskPrompt)
163 return ERROR_INVALID_PARAMETER;
165 if (szProductCodeOrPatchCode) product = strdupAtoW(szProductCodeOrPatchCode);
166 if (szUserSid) usersid = strdupAtoW(szUserSid);
168 /* FIXME: add tests for an invalid format */
170 if (pcchVolumeLabel)
171 volume = msi_alloc(*pcchVolumeLabel * sizeof(WCHAR));
173 if (pcchDiskPrompt)
174 prompt = msi_alloc(*pcchDiskPrompt * sizeof(WCHAR));
176 if (volume) *volume = '\0';
177 if (prompt) *prompt = '\0';
178 r = MsiSourceListEnumMediaDisksW(product, usersid, dwContext, dwOptions,
179 dwIndex, pdwDiskId, volume, pcchVolumeLabel,
180 prompt, pcchDiskPrompt);
181 if (r != ERROR_SUCCESS)
182 goto done;
184 if (szVolumeLabel && pcchVolumeLabel)
185 WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
186 *pcchVolumeLabel + 1, NULL, NULL);
188 if (szDiskPrompt)
189 WideCharToMultiByte(CP_ACP, 0, prompt, -1, szDiskPrompt,
190 *pcchDiskPrompt + 1, NULL, NULL);
192 done:
193 msi_free(product);
194 msi_free(usersid);
195 msi_free(volume);
196 msi_free(prompt);
198 return r;
201 /******************************************************************
202 * MsiSourceListEnumMediaDisksW (MSI.@)
204 UINT WINAPI MsiSourceListEnumMediaDisksW(LPCWSTR szProductCodeOrPatchCode,
205 LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
206 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
207 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
208 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
210 WCHAR squished_pc[GUID_SIZE];
211 WCHAR convert[11];
212 LPWSTR value = NULL;
213 LPWSTR data = NULL;
214 LPWSTR ptr, ptr2;
215 HKEY source, media;
216 DWORD valuesz, datasz = 0;
217 DWORD type;
218 DWORD numvals, size;
219 LONG res;
220 UINT r;
221 static int index = 0;
223 static const WCHAR fmt[] = {'#','%','d',0};
225 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
226 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
227 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
229 if (!szProductCodeOrPatchCode ||
230 !squash_guid(szProductCodeOrPatchCode, squished_pc))
231 return ERROR_INVALID_PARAMETER;
233 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
234 return ERROR_INVALID_PARAMETER;
236 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
237 return ERROR_INVALID_PARAMETER;
239 if (szDiskPrompt && !pcchDiskPrompt)
240 return ERROR_INVALID_PARAMETER;
242 if (dwIndex == 0)
243 index = 0;
245 if (dwIndex != index)
246 return ERROR_INVALID_PARAMETER;
248 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
249 dwOptions, dwContext, FALSE);
250 if (r != ERROR_SUCCESS)
251 return r;
253 r = OpenMediaSubkey(source, &media, FALSE);
254 if (r != ERROR_SUCCESS)
256 RegCloseKey(source);
257 return ERROR_NO_MORE_ITEMS;
260 if (!pcchVolumeLabel && !pcchDiskPrompt)
262 r = RegEnumValueW(media, dwIndex, NULL, NULL, NULL,
263 &type, NULL, NULL);
264 goto done;
267 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
268 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
269 if (res != ERROR_SUCCESS)
271 r = ERROR_BAD_CONFIGURATION;
272 goto done;
275 value = msi_alloc(++valuesz * sizeof(WCHAR));
276 data = msi_alloc(++datasz * sizeof(WCHAR));
277 if (!value || !data)
279 r = ERROR_OUTOFMEMORY;
280 goto done;
283 r = RegEnumValueW(media, dwIndex, value, &valuesz,
284 NULL, &type, (LPBYTE)data, &datasz);
285 if (r != ERROR_SUCCESS)
286 goto done;
288 if (pdwDiskId)
289 *pdwDiskId = atolW(value);
291 ptr2 = data;
292 ptr = strchrW(data, ';');
293 if (!ptr)
294 ptr = data;
295 else
296 *ptr = '\0';
298 if (pcchVolumeLabel)
300 if (type == REG_DWORD)
302 sprintfW(convert, fmt, *data);
303 size = lstrlenW(convert);
304 ptr2 = convert;
306 else
307 size = lstrlenW(data);
309 if (size >= *pcchVolumeLabel)
310 r = ERROR_MORE_DATA;
311 else if (szVolumeLabel)
312 lstrcpyW(szVolumeLabel, ptr2);
314 *pcchVolumeLabel = size;
317 if (pcchDiskPrompt)
319 if (!*ptr)
320 ptr++;
322 if (type == REG_DWORD)
324 sprintfW(convert, fmt, *ptr);
325 size = lstrlenW(convert);
326 ptr = convert;
328 else
329 size = lstrlenW(ptr);
331 size = lstrlenW(ptr);
332 if (size >= *pcchDiskPrompt)
333 r = ERROR_MORE_DATA;
334 else if (szDiskPrompt)
335 lstrcpyW(szDiskPrompt, ptr);
337 *pcchDiskPrompt = size;
340 index++;
342 done:
343 msi_free(value);
344 msi_free(data);
345 RegCloseKey(source);
347 return r;
350 /******************************************************************
351 * MsiSourceListEnumSourcesA (MSI.@)
353 UINT WINAPI MsiSourceListEnumSourcesA(LPCSTR szProductCodeOrPatch, LPCSTR szUserSid,
354 MSIINSTALLCONTEXT dwContext,
355 DWORD dwOptions, DWORD dwIndex,
356 LPSTR szSource, LPDWORD pcchSource)
358 LPWSTR product = NULL;
359 LPWSTR usersid = NULL;
360 LPWSTR source = NULL;
361 DWORD len = 0;
362 UINT r = ERROR_INVALID_PARAMETER;
363 static int index = 0;
365 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_a(szProductCodeOrPatch),
366 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
368 if (dwIndex == 0)
369 index = 0;
371 if (szSource && !pcchSource)
372 goto done;
374 if (dwIndex != index)
375 goto done;
377 if (szProductCodeOrPatch) product = strdupAtoW(szProductCodeOrPatch);
378 if (szUserSid) usersid = strdupAtoW(szUserSid);
380 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
381 dwIndex, NULL, &len);
382 if (r != ERROR_SUCCESS)
383 goto done;
385 source = msi_alloc(++len * sizeof(WCHAR));
386 if (!source)
388 r = ERROR_OUTOFMEMORY;
389 goto done;
392 *source = '\0';
393 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
394 dwIndex, source, &len);
395 if (r != ERROR_SUCCESS)
396 goto done;
398 len = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL);
399 if (pcchSource && *pcchSource >= len)
400 WideCharToMultiByte(CP_ACP, 0, source, -1, szSource, len, NULL, NULL);
401 else if (szSource)
402 r = ERROR_MORE_DATA;
404 if (pcchSource)
405 *pcchSource = len - 1;
407 done:
408 msi_free(product);
409 msi_free(usersid);
410 msi_free(source);
412 if (r == ERROR_SUCCESS)
414 if (szSource || !pcchSource) index++;
416 else if (dwIndex > index)
417 index = 0;
419 return r;
422 /******************************************************************
423 * MsiSourceListEnumSourcesW (MSI.@)
425 UINT WINAPI MsiSourceListEnumSourcesW(LPCWSTR szProductCodeOrPatch, LPCWSTR szUserSid,
426 MSIINSTALLCONTEXT dwContext,
427 DWORD dwOptions, DWORD dwIndex,
428 LPWSTR szSource, LPDWORD pcchSource)
430 WCHAR squished_pc[GUID_SIZE];
431 WCHAR name[32];
432 HKEY source = NULL;
433 HKEY subkey = NULL;
434 LONG res;
435 UINT r = ERROR_INVALID_PARAMETER;
436 static int index = 0;
438 static const WCHAR format[] = {'%','d',0};
440 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_w(szProductCodeOrPatch),
441 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
443 if (dwIndex == 0)
444 index = 0;
446 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
447 goto done;
449 if (szSource && !pcchSource)
450 goto done;
452 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
453 goto done;
455 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
456 goto done;
458 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
459 goto done;
461 if (dwIndex != index)
462 goto done;
464 r = OpenSourceKey(szProductCodeOrPatch, &source,
465 dwOptions, dwContext, FALSE);
466 if (r != ERROR_SUCCESS)
467 goto done;
469 if (dwOptions & MSISOURCETYPE_NETWORK)
470 r = OpenNetworkSubkey(source, &subkey, FALSE);
471 else if (dwOptions & MSISOURCETYPE_URL)
472 r = OpenURLSubkey(source, &subkey, FALSE);
474 if (r != ERROR_SUCCESS)
476 r = ERROR_NO_MORE_ITEMS;
477 goto done;
480 sprintfW(name, format, dwIndex + 1);
482 res = RegQueryValueExW(subkey, name, 0, 0, (LPBYTE)szSource, pcchSource);
483 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
484 r = ERROR_NO_MORE_ITEMS;
486 done:
487 RegCloseKey(subkey);
488 RegCloseKey(source);
490 if (r == ERROR_SUCCESS)
492 if (szSource || !pcchSource) index++;
494 else if (dwIndex > index)
495 index = 0;
497 return r;
500 /******************************************************************
501 * MsiSourceListGetInfoA (MSI.@)
503 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
504 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
505 LPCSTR szProperty, LPSTR szValue,
506 LPDWORD pcchValue)
508 UINT ret;
509 LPWSTR product = NULL;
510 LPWSTR usersid = NULL;
511 LPWSTR property = NULL;
512 LPWSTR value = NULL;
513 DWORD len = 0;
515 if (szValue && !pcchValue)
516 return ERROR_INVALID_PARAMETER;
518 if (szProduct) product = strdupAtoW(szProduct);
519 if (szUserSid) usersid = strdupAtoW(szUserSid);
520 if (szProperty) property = strdupAtoW(szProperty);
522 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
523 property, NULL, &len);
524 if (ret != ERROR_SUCCESS)
525 goto done;
527 value = msi_alloc(++len * sizeof(WCHAR));
528 if (!value)
529 return ERROR_OUTOFMEMORY;
531 *value = '\0';
532 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
533 property, value, &len);
534 if (ret != ERROR_SUCCESS)
535 goto done;
537 len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
538 if (*pcchValue >= len)
539 WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
540 else if (szValue)
541 ret = ERROR_MORE_DATA;
543 *pcchValue = len - 1;
545 done:
546 msi_free(product);
547 msi_free(usersid);
548 msi_free(property);
549 msi_free(value);
550 return ret;
553 /******************************************************************
554 * MsiSourceListGetInfoW (MSI.@)
556 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
557 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
558 LPCWSTR szProperty, LPWSTR szValue,
559 LPDWORD pcchValue)
561 WCHAR squished_pc[GUID_SIZE];
562 HKEY sourcekey, media;
563 LPWSTR source, ptr;
564 DWORD size;
565 UINT rc;
567 static const WCHAR mediapack[] = {
568 'M','e','d','i','a','P','a','c','k','a','g','e',0};
570 TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szProperty));
572 if (!szProduct || !squash_guid(szProduct, squished_pc))
573 return ERROR_INVALID_PARAMETER;
575 if (szValue && !pcchValue)
576 return ERROR_INVALID_PARAMETER;
578 if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
579 dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
580 dwContext != MSIINSTALLCONTEXT_MACHINE)
581 return ERROR_INVALID_PARAMETER;
583 if (!szProperty)
584 return ERROR_INVALID_PARAMETER;
586 if (szUserSid)
587 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
589 if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED)
590 FIXME("Unhandled context %d\n", dwContext);
592 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
593 if (rc != ERROR_SUCCESS)
594 return rc;
596 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
597 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
599 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
600 if (rc != ERROR_SUCCESS)
602 RegCloseKey(sourcekey);
603 return ERROR_SUCCESS;
606 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
607 szProperty = mediapack;
609 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
610 RegCloseKey(media);
612 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) ||
613 !lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
615 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
616 0, 0, NULL, &size);
617 if (rc != ERROR_SUCCESS)
619 RegCloseKey(sourcekey);
620 return ERROR_SUCCESS;
623 source = msi_alloc(size);
624 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
625 0, 0, (LPBYTE)source, &size);
627 if (!*source)
629 msi_free(source);
630 RegCloseKey(sourcekey);
631 return ERROR_SUCCESS;
634 if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
636 if (*source != 'n' && *source != 'u' && *source != 'm')
638 msi_free(source);
639 RegCloseKey(sourcekey);
640 return ERROR_SUCCESS;
643 ptr = source;
644 source[1] = '\0';
646 else
648 ptr = strrchrW(source, ';');
649 if (!ptr)
650 ptr = source;
651 else
652 ptr++;
655 if (szValue)
657 if (lstrlenW(ptr) < *pcchValue)
658 lstrcpyW(szValue, ptr);
659 else
660 rc = ERROR_MORE_DATA;
663 *pcchValue = lstrlenW(ptr);
664 msi_free(source);
666 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
668 *pcchValue = *pcchValue * sizeof(WCHAR);
669 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
670 (LPBYTE)szValue, pcchValue);
671 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
673 *pcchValue = 0;
674 rc = ERROR_SUCCESS;
676 else
678 if (*pcchValue)
679 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
680 if (szValue)
681 szValue[*pcchValue] = '\0';
684 else
686 FIXME("Unknown property %s\n",debugstr_w(szProperty));
687 rc = ERROR_UNKNOWN_PROPERTY;
690 RegCloseKey(sourcekey);
691 return rc;
694 /******************************************************************
695 * MsiSourceListSetInfoA (MSI.@)
697 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
698 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
699 LPCSTR szProperty, LPCSTR szValue)
701 UINT ret;
702 LPWSTR product = NULL;
703 LPWSTR usersid = NULL;
704 LPWSTR property = NULL;
705 LPWSTR value = NULL;
707 if (szProduct) product = strdupAtoW(szProduct);
708 if (szUserSid) usersid = strdupAtoW(szUserSid);
709 if (szProperty) property = strdupAtoW(szProperty);
710 if (szValue) value = strdupAtoW(szValue);
712 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
713 property, value);
715 msi_free(product);
716 msi_free(usersid);
717 msi_free(property);
718 msi_free(value);
720 return ret;
723 UINT msi_set_last_used_source(LPCWSTR product, LPCWSTR usersid,
724 MSIINSTALLCONTEXT context, DWORD options,
725 LPCWSTR value)
727 HKEY source;
728 LPWSTR buffer;
729 WCHAR typechar;
730 DWORD size;
731 UINT r;
732 int index = 1;
734 static const WCHAR format[] = {'%','c',';','%','i',';','%','s',0};
736 if (options & MSISOURCETYPE_NETWORK)
737 typechar = 'n';
738 else if (options & MSISOURCETYPE_URL)
739 typechar = 'u';
740 else if (options & MSISOURCETYPE_MEDIA)
741 typechar = 'm';
742 else
743 return ERROR_INVALID_PARAMETER;
745 if (!(options & MSISOURCETYPE_MEDIA))
747 r = MsiSourceListAddSourceExW(product, usersid, context,
748 options, value, 0);
749 if (r != ERROR_SUCCESS)
750 return r;
752 index = 0;
753 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
754 index, NULL, NULL)) == ERROR_SUCCESS)
755 index++;
757 if (r != ERROR_NO_MORE_ITEMS)
758 return r;
761 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
762 buffer = msi_alloc(size);
763 if (!buffer)
764 return ERROR_OUTOFMEMORY;
766 r = OpenSourceKey(product, &source, MSICODE_PRODUCT, context, FALSE);
767 if (r != ERROR_SUCCESS)
768 return r;
770 sprintfW(buffer, format, typechar, index, value);
772 size = (lstrlenW(buffer) + 1) * sizeof(WCHAR);
773 r = RegSetValueExW(source, INSTALLPROPERTY_LASTUSEDSOURCEW, 0,
774 REG_SZ, (LPBYTE)buffer, size);
775 msi_free(buffer);
777 RegCloseKey(source);
778 return r;
781 /******************************************************************
782 * MsiSourceListSetInfoW (MSI.@)
784 UINT WINAPI MsiSourceListSetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
785 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
786 LPCWSTR szProperty, LPCWSTR szValue)
788 WCHAR squished_pc[GUID_SIZE];
789 HKEY sourcekey, media;
790 LPCWSTR property;
791 UINT rc;
793 static const WCHAR media_package[] = {
794 'M','e','d','i','a','P','a','c','k','a','g','e',0
797 TRACE("%s %s %x %x %s %s\n", debugstr_w(szProduct), debugstr_w(szUserSid),
798 dwContext, dwOptions, debugstr_w(szProperty), debugstr_w(szValue));
800 if (!szProduct || !squash_guid(szProduct, squished_pc))
801 return ERROR_INVALID_PARAMETER;
803 if (!szProperty)
804 return ERROR_INVALID_PARAMETER;
806 if (!szValue)
807 return ERROR_UNKNOWN_PROPERTY;
809 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
810 return ERROR_INVALID_PARAMETER;
812 if (dwOptions & MSICODE_PATCH)
814 FIXME("Unhandled options MSICODE_PATCH\n");
815 return ERROR_UNKNOWN_PATCH;
818 property = szProperty;
819 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
820 property = media_package;
822 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
823 if (rc != ERROR_SUCCESS)
824 return rc;
826 if (lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) &&
827 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
829 RegCloseKey(sourcekey);
830 return ERROR_INVALID_PARAMETER;
833 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
834 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
836 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
837 if (rc == ERROR_SUCCESS)
839 rc = msi_reg_set_val_str(media, property, szValue);
840 RegCloseKey(media);
843 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
845 DWORD size = (lstrlenW(szValue) + 1) * sizeof(WCHAR);
846 rc = RegSetValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0,
847 REG_SZ, (const BYTE *)szValue, size);
848 if (rc != ERROR_SUCCESS)
849 rc = ERROR_UNKNOWN_PROPERTY;
851 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW))
853 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
854 rc = ERROR_INVALID_PARAMETER;
855 else
856 rc = msi_set_last_used_source(szProduct, szUserSid, dwContext,
857 dwOptions, szValue);
859 else
860 rc = ERROR_UNKNOWN_PROPERTY;
862 RegCloseKey(sourcekey);
863 return rc;
866 /******************************************************************
867 * MsiSourceListAddSourceW (MSI.@)
869 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
870 DWORD dwReserved, LPCWSTR szSource)
872 WCHAR squished_pc[GUID_SIZE];
873 INT ret;
874 LPWSTR sidstr = NULL;
875 DWORD sidsize = 0;
876 DWORD domsize = 0;
877 DWORD context;
878 HKEY hkey = 0;
879 UINT r;
881 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
883 if (!szSource || !*szSource)
884 return ERROR_INVALID_PARAMETER;
886 if (dwReserved != 0)
887 return ERROR_INVALID_PARAMETER;
889 if (!szProduct || !squash_guid(szProduct, squished_pc))
890 return ERROR_INVALID_PARAMETER;
892 if (!szUserName || !*szUserName)
893 context = MSIINSTALLCONTEXT_MACHINE;
894 else
896 if (LookupAccountNameW(NULL, szUserName, NULL, &sidsize, NULL, &domsize, NULL))
898 PSID psid = msi_alloc(sidsize);
900 if (LookupAccountNameW(NULL, szUserName, psid, &sidsize, NULL, &domsize, NULL))
901 ConvertSidToStringSidW(psid, &sidstr);
903 msi_free(psid);
906 r = MSIREG_OpenLocalManagedProductKey(szProduct, &hkey, FALSE);
907 if (r == ERROR_SUCCESS)
908 context = MSIINSTALLCONTEXT_USERMANAGED;
909 else
911 r = MSIREG_OpenUserProductsKey(szProduct, &hkey, FALSE);
912 if (r != ERROR_SUCCESS)
913 return ERROR_UNKNOWN_PRODUCT;
915 context = MSIINSTALLCONTEXT_USERUNMANAGED;
918 RegCloseKey(hkey);
921 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
922 context, MSISOURCETYPE_NETWORK, szSource, 0);
924 if (sidstr)
925 LocalFree(sidstr);
927 return ret;
930 /******************************************************************
931 * MsiSourceListAddSourceA (MSI.@)
933 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
934 DWORD dwReserved, LPCSTR szSource)
936 INT ret;
937 LPWSTR szwproduct;
938 LPWSTR szwusername;
939 LPWSTR szwsource;
941 szwproduct = strdupAtoW( szProduct );
942 szwusername = strdupAtoW( szUserName );
943 szwsource = strdupAtoW( szSource );
945 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
947 msi_free(szwproduct);
948 msi_free(szwusername);
949 msi_free(szwsource);
951 return ret;
954 /******************************************************************
955 * MsiSourceListAddSourceExA (MSI.@)
957 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
958 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
960 UINT ret;
961 LPWSTR product, usersid, source;
963 product = strdupAtoW(szProduct);
964 usersid = strdupAtoW(szUserSid);
965 source = strdupAtoW(szSource);
967 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
968 dwOptions, source, dwIndex);
970 msi_free(product);
971 msi_free(usersid);
972 msi_free(source);
974 return ret;
977 static void free_source_list(struct list *sourcelist)
979 while (!list_empty(sourcelist))
981 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
982 list_remove(&info->entry);
983 msi_free(info->path);
984 msi_free(info);
988 static void add_source_to_list(struct list *sourcelist, media_info *info,
989 DWORD *index)
991 media_info *iter;
992 BOOL found = FALSE;
993 static const WCHAR fmt[] = {'%','i',0};
995 if (index) *index = 0;
997 if (list_empty(sourcelist))
999 list_add_head(sourcelist, &info->entry);
1000 return;
1003 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
1005 if (!found && info->index < iter->index)
1007 found = TRUE;
1008 list_add_before(&iter->entry, &info->entry);
1011 /* update the rest of the list */
1012 if (found)
1013 sprintfW(iter->szIndex, fmt, ++iter->index);
1014 else if (index)
1015 (*index)++;
1018 if (!found)
1019 list_add_after(&iter->entry, &info->entry);
1022 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1024 UINT r = ERROR_SUCCESS;
1025 DWORD index = 0;
1026 WCHAR name[10];
1027 DWORD size, val_size;
1028 media_info *entry;
1030 *count = 0;
1032 while (r == ERROR_SUCCESS)
1034 size = sizeof(name) / sizeof(name[0]);
1035 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1036 if (r != ERROR_SUCCESS)
1037 return r;
1039 entry = msi_alloc(sizeof(media_info));
1040 if (!entry)
1041 goto error;
1043 entry->path = msi_alloc(val_size);
1044 if (!entry->path)
1046 msi_free(entry);
1047 goto error;
1050 lstrcpyW(entry->szIndex, name);
1051 entry->index = atoiW(name);
1053 size++;
1054 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1055 NULL, (LPBYTE)entry->path, &val_size);
1056 if (r != ERROR_SUCCESS)
1058 msi_free(entry->path);
1059 msi_free(entry);
1060 goto error;
1063 index = ++(*count);
1064 add_source_to_list(sourcelist, entry, NULL);
1067 error:
1068 *count = -1;
1069 free_source_list(sourcelist);
1070 return ERROR_OUTOFMEMORY;
1073 /******************************************************************
1074 * MsiSourceListAddSourceExW (MSI.@)
1076 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1077 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1078 DWORD dwIndex)
1080 HKEY sourcekey;
1081 HKEY typekey;
1082 UINT rc;
1083 struct list sourcelist;
1084 media_info *info;
1085 WCHAR squished_pc[GUID_SIZE];
1086 WCHAR name[10];
1087 LPWSTR source;
1088 LPCWSTR postfix;
1089 DWORD size, count;
1090 DWORD index;
1092 static const WCHAR fmt[] = {'%','i',0};
1093 static const WCHAR one[] = {'1',0};
1094 static const WCHAR backslash[] = {'\\',0};
1095 static const WCHAR forwardslash[] = {'/',0};
1097 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1098 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1100 if (!szProduct || !squash_guid(szProduct, squished_pc))
1101 return ERROR_INVALID_PARAMETER;
1103 if (!szSource || !*szSource)
1104 return ERROR_INVALID_PARAMETER;
1106 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1107 return ERROR_INVALID_PARAMETER;
1109 if (dwOptions & MSICODE_PATCH)
1111 FIXME("Unhandled options MSICODE_PATCH\n");
1112 return ERROR_FUNCTION_FAILED;
1115 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1116 return ERROR_INVALID_PARAMETER;
1118 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1119 if (rc != ERROR_SUCCESS)
1120 return rc;
1122 if (dwOptions & MSISOURCETYPE_NETWORK)
1123 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1124 else if (dwOptions & MSISOURCETYPE_URL)
1125 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1126 else if (dwOptions & MSISOURCETYPE_MEDIA)
1127 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1128 else
1130 ERR("unknown media type: %08x\n", dwOptions);
1131 RegCloseKey(sourcekey);
1132 return ERROR_FUNCTION_FAILED;
1135 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? backslash : forwardslash;
1136 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1137 source = strdupW(szSource);
1138 else
1140 size = lstrlenW(szSource) + 2;
1141 source = msi_alloc(size * sizeof(WCHAR));
1142 lstrcpyW(source, szSource);
1143 lstrcatW(source, postfix);
1146 list_init(&sourcelist);
1147 rc = fill_source_list(&sourcelist, typekey, &count);
1148 if (rc != ERROR_NO_MORE_ITEMS)
1149 return rc;
1151 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1153 if (count == 0)
1155 rc = RegSetValueExW(typekey, one, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1156 goto done;
1158 else if (dwIndex > count || dwIndex == 0)
1160 sprintfW(name, fmt, count + 1);
1161 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1162 goto done;
1164 else
1166 sprintfW(name, fmt, dwIndex);
1167 info = msi_alloc(sizeof(media_info));
1168 if (!info)
1170 rc = ERROR_OUTOFMEMORY;
1171 goto done;
1174 info->path = strdupW(source);
1175 lstrcpyW(info->szIndex, name);
1176 info->index = dwIndex;
1177 add_source_to_list(&sourcelist, info, &index);
1179 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1181 if (info->index < index)
1182 continue;
1184 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1185 rc = RegSetValueExW(typekey, info->szIndex, 0,
1186 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1187 if (rc != ERROR_SUCCESS)
1188 goto done;
1192 done:
1193 free_source_list(&sourcelist);
1194 msi_free(source);
1195 RegCloseKey(typekey);
1196 RegCloseKey(sourcekey);
1197 return rc;
1200 /******************************************************************
1201 * MsiSourceListAddMediaDiskA (MSI.@)
1203 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1204 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1205 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1207 UINT r;
1208 LPWSTR product = NULL;
1209 LPWSTR usersid = NULL;
1210 LPWSTR volume = NULL;
1211 LPWSTR prompt = NULL;
1213 if (szProduct) product = strdupAtoW(szProduct);
1214 if (szUserSid) usersid = strdupAtoW(szUserSid);
1215 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1216 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1218 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1219 dwDiskId, volume, prompt);
1221 msi_free(product);
1222 msi_free(usersid);
1223 msi_free(volume);
1224 msi_free(prompt);
1226 return r;
1229 /******************************************************************
1230 * MsiSourceListAddMediaDiskW (MSI.@)
1232 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1233 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1234 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1236 HKEY sourcekey;
1237 HKEY mediakey;
1238 UINT rc;
1239 WCHAR szIndex[10];
1240 WCHAR squished_pc[GUID_SIZE];
1241 LPWSTR buffer;
1242 DWORD size;
1244 static const WCHAR fmt[] = {'%','i',0};
1245 static const WCHAR semicolon[] = {';',0};
1247 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1248 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1249 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1251 if (!szProduct || !squash_guid(szProduct, squished_pc))
1252 return ERROR_INVALID_PARAMETER;
1254 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1255 return ERROR_INVALID_PARAMETER;
1257 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1258 return ERROR_INVALID_PARAMETER;
1260 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1261 return ERROR_INVALID_PARAMETER;
1263 if (dwOptions & MSICODE_PATCH)
1265 FIXME("Unhandled options MSICODE_PATCH\n");
1266 return ERROR_FUNCTION_FAILED;
1269 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1270 if (rc != ERROR_SUCCESS)
1271 return rc;
1273 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1275 sprintfW(szIndex, fmt, dwDiskId);
1277 size = 2;
1278 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1279 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1281 size *= sizeof(WCHAR);
1282 buffer = msi_alloc(size);
1283 *buffer = '\0';
1285 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1286 lstrcatW(buffer, semicolon);
1287 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1289 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1290 msi_free(buffer);
1292 RegCloseKey(sourcekey);
1293 RegCloseKey(mediakey);
1295 return ERROR_SUCCESS;
1298 /******************************************************************
1299 * MsiSourceListClearAllA (MSI.@)
1301 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1303 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1304 return ERROR_SUCCESS;
1307 /******************************************************************
1308 * MsiSourceListClearAllW (MSI.@)
1310 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1312 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1313 return ERROR_SUCCESS;