setupapi: Add stubs for SetupQueryInfOriginalFileInformationA/W.
[wine] / dlls / setupapi / query.c
1 /*
2  * setupapi query functions
3  *
4  * Copyright 2006 James Hawkins
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "winver.h"
28 #include "setupapi.h"
29 #include "advpub.h"
30 #include "winnls.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "setupapi_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
36
37 #ifdef __i386__
38 static const WCHAR source_disks_names_platform[] =
39     {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s','.','x','8','6',0};
40 static const WCHAR source_disks_files_platform[] =
41     {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s','.','x','8','6',0};
42 #elif defined(__x86_64)
43 static const WCHAR source_disks_names_platform[] =
44     {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s','.','a','m','d','6','4',0};
45 static const WCHAR source_disks_files_platform[] =
46     {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s','.','a','m','d','6','4',0};
47 #else  /* FIXME: other platforms */
48 static const WCHAR source_disks_names_platform[] =
49     {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s',0};
50 static const WCHAR source_disks_files_platform[] =
51     {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s',0};
52 #endif
53 static const WCHAR source_disks_names[] =
54     {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s',0};
55 static const WCHAR source_disks_files[] =
56     {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s',0};
57
58 /* fills the PSP_INF_INFORMATION struct fill_info is TRUE
59  * always returns the required size of the information
60  */
61 static BOOL fill_inf_info(HINF inf, PSP_INF_INFORMATION buffer, DWORD size, DWORD *required)
62 {
63     LPCWSTR filename = PARSER_get_inf_filename(inf);
64     DWORD total_size = FIELD_OFFSET(SP_INF_INFORMATION, VersionData)
65                         + (lstrlenW(filename) + 1) * sizeof(WCHAR);
66
67     if (required) *required = total_size;
68
69     /* FIXME: we need to parse the INF file to find the correct version info */
70     if (buffer)
71     {
72         if (size < total_size)
73         {
74             SetLastError(ERROR_INSUFFICIENT_BUFFER);
75             return FALSE;
76         }
77         buffer->InfStyle = INF_STYLE_WIN4;
78         buffer->InfCount = 1;
79         /* put the filename in buffer->VersionData */
80         lstrcpyW((LPWSTR)&buffer->VersionData[0], filename);
81     }
82     return TRUE;
83 }
84
85 static HINF search_for_inf(LPCVOID InfSpec, DWORD SearchControl)
86 {
87     HINF hInf = INVALID_HANDLE_VALUE;
88     WCHAR inf_path[MAX_PATH];
89
90     static const WCHAR infW[] = {'\\','i','n','f','\\',0};
91     static const WCHAR system32W[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
92
93     if (SearchControl == INFINFO_REVERSE_DEFAULT_SEARCH)
94     {
95         GetWindowsDirectoryW(inf_path, MAX_PATH);
96         lstrcatW(inf_path, system32W);
97         lstrcatW(inf_path, InfSpec);
98
99         hInf = SetupOpenInfFileW(inf_path, NULL,
100                                  INF_STYLE_OLDNT | INF_STYLE_WIN4, NULL);
101         if (hInf != INVALID_HANDLE_VALUE)
102             return hInf;
103
104         GetWindowsDirectoryW(inf_path, MAX_PATH);
105         lstrcpyW(inf_path, infW);
106         lstrcatW(inf_path, InfSpec);
107
108         return SetupOpenInfFileW(inf_path, NULL,
109                                  INF_STYLE_OLDNT | INF_STYLE_WIN4, NULL);
110     }
111
112     return INVALID_HANDLE_VALUE;
113 }
114
115 /***********************************************************************
116  *      SetupGetInfInformationA    (SETUPAPI.@)
117  *
118  */
119 BOOL WINAPI SetupGetInfInformationA(LPCVOID InfSpec, DWORD SearchControl,
120                                     PSP_INF_INFORMATION ReturnBuffer,
121                                     DWORD ReturnBufferSize, PDWORD RequiredSize)
122 {
123     LPWSTR inf = (LPWSTR)InfSpec;
124     DWORD len;
125     BOOL ret;
126
127     if (InfSpec && SearchControl >= INFINFO_INF_NAME_IS_ABSOLUTE)
128     {
129         len = lstrlenA(InfSpec) + 1;
130         inf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
131         MultiByteToWideChar(CP_ACP, 0, InfSpec, -1, inf, len);
132     }
133
134     ret = SetupGetInfInformationW(inf, SearchControl, ReturnBuffer,
135                                   ReturnBufferSize, RequiredSize);
136
137     if (SearchControl >= INFINFO_INF_NAME_IS_ABSOLUTE)
138         HeapFree(GetProcessHeap(), 0, inf);
139
140     return ret;
141 }
142
143 /***********************************************************************
144  *      SetupGetInfInformationW    (SETUPAPI.@)
145  * 
146  * BUGS
147  *   Only handles the case when InfSpec is an INF handle.
148  */
149 BOOL WINAPI SetupGetInfInformationW(LPCVOID InfSpec, DWORD SearchControl,
150                                      PSP_INF_INFORMATION ReturnBuffer,
151                                      DWORD ReturnBufferSize, PDWORD RequiredSize)
152 {
153     HINF inf;
154     BOOL ret;
155     DWORD infSize;
156
157     TRACE("(%p, %d, %p, %d, %p)\n", InfSpec, SearchControl, ReturnBuffer,
158            ReturnBufferSize, RequiredSize);
159
160     if (!InfSpec)
161     {
162         if (SearchControl == INFINFO_INF_SPEC_IS_HINF)
163             SetLastError(ERROR_INVALID_HANDLE);
164         else
165             SetLastError(ERROR_INVALID_PARAMETER);
166
167         return FALSE;
168     }
169
170     switch (SearchControl)
171     {
172         case INFINFO_INF_SPEC_IS_HINF:
173             inf = (HINF)InfSpec;
174             break;
175         case INFINFO_INF_NAME_IS_ABSOLUTE:
176         case INFINFO_DEFAULT_SEARCH:
177             inf = SetupOpenInfFileW(InfSpec, NULL,
178                                     INF_STYLE_OLDNT | INF_STYLE_WIN4, NULL);
179             break;
180         case INFINFO_REVERSE_DEFAULT_SEARCH:
181             inf = search_for_inf(InfSpec, SearchControl);
182             break;
183         case INFINFO_INF_PATH_LIST_SEARCH:
184             FIXME("Unhandled search control: %d\n", SearchControl);
185
186             if (RequiredSize)
187                 *RequiredSize = 0;
188
189             return FALSE;
190         default:
191             SetLastError(ERROR_INVALID_PARAMETER);
192             return FALSE;
193     }
194
195     if (inf == INVALID_HANDLE_VALUE)
196     {
197         SetLastError(ERROR_FILE_NOT_FOUND);
198         return FALSE;
199     }
200
201     ret = fill_inf_info(inf, ReturnBuffer, ReturnBufferSize, &infSize);
202     if (!ReturnBuffer && (ReturnBufferSize >= infSize))
203     {
204         SetLastError(ERROR_INVALID_PARAMETER);
205         ret = FALSE;
206     }
207     if (RequiredSize) *RequiredSize = infSize;
208
209     if (SearchControl >= INFINFO_INF_NAME_IS_ABSOLUTE)
210         SetupCloseInfFile(inf);
211
212     return ret;
213 }
214
215 /***********************************************************************
216  *      SetupQueryInfFileInformationA    (SETUPAPI.@)
217  */
218 BOOL WINAPI SetupQueryInfFileInformationA(PSP_INF_INFORMATION InfInformation,
219                                           UINT InfIndex, PSTR ReturnBuffer,
220                                           DWORD ReturnBufferSize, PDWORD RequiredSize)
221 {
222     LPWSTR filenameW;
223     DWORD size;
224     BOOL ret;
225
226     ret = SetupQueryInfFileInformationW(InfInformation, InfIndex, NULL, 0, &size);
227     if (!ret)
228         return FALSE;
229
230     filenameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
231
232     ret = SetupQueryInfFileInformationW(InfInformation, InfIndex,
233                                         filenameW, size, &size);
234     if (!ret)
235     {
236         HeapFree(GetProcessHeap(), 0, filenameW);
237         return FALSE;
238     }
239
240     if (RequiredSize)
241         *RequiredSize = size;
242
243     if (!ReturnBuffer)
244     {
245         if (ReturnBufferSize)
246         {
247             SetLastError(ERROR_INVALID_PARAMETER);
248             return FALSE;
249         }
250
251         return TRUE;
252     }
253
254     if (size > ReturnBufferSize)
255     {
256         SetLastError(ERROR_INSUFFICIENT_BUFFER);
257         return FALSE;
258     }
259
260     WideCharToMultiByte(CP_ACP, 0, filenameW, -1, ReturnBuffer, size, NULL, NULL);
261     HeapFree(GetProcessHeap(), 0, filenameW);
262
263     return ret;
264 }
265
266 /***********************************************************************
267  *      SetupQueryInfFileInformationW    (SETUPAPI.@)
268  */
269 BOOL WINAPI SetupQueryInfFileInformationW(PSP_INF_INFORMATION InfInformation,
270                                           UINT InfIndex, PWSTR ReturnBuffer,
271                                           DWORD ReturnBufferSize, PDWORD RequiredSize) 
272 {
273     DWORD len;
274     LPWSTR ptr;
275
276     TRACE("(%p, %u, %p, %d, %p) Stub!\n", InfInformation, InfIndex,
277           ReturnBuffer, ReturnBufferSize, RequiredSize);
278
279     if (!InfInformation)
280     {
281         SetLastError(ERROR_INVALID_PARAMETER);
282         return FALSE;
283     }
284
285     if (InfIndex != 0)
286         FIXME("Appended INF files are not handled\n");
287
288     ptr = (LPWSTR)&InfInformation->VersionData[0];
289     len = lstrlenW(ptr);
290
291     if (RequiredSize)
292         *RequiredSize = len + 1;
293
294     if (!ReturnBuffer)
295         return TRUE;
296
297     if (ReturnBufferSize < len)
298     {
299         SetLastError(ERROR_INSUFFICIENT_BUFFER);
300         return FALSE;
301     }
302
303     lstrcpyW(ReturnBuffer, ptr);
304     return TRUE;
305 }
306
307 /***********************************************************************
308  *            SetupGetSourceFileLocationA   (SETUPAPI.@)
309  */
310
311 BOOL WINAPI SetupGetSourceFileLocationA( HINF hinf, PINFCONTEXT context, PCSTR filename,
312                                          PUINT source_id, PSTR buffer, DWORD buffer_size,
313                                          PDWORD required_size )
314 {
315     BOOL ret = FALSE;
316     WCHAR *filenameW = NULL, *bufferW = NULL;
317     DWORD required;
318     INT size;
319
320     TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf, context, debugstr_a(filename), source_id,
321           buffer, buffer_size, required_size);
322
323     if (filename && !(filenameW = strdupAtoW( filename )))
324         return FALSE;
325
326     if (!SetupGetSourceFileLocationW( hinf, context, filenameW, source_id, NULL, 0, &required ))
327         goto done;
328
329     if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, required * sizeof(WCHAR) )))
330         goto done;
331
332     if (!SetupGetSourceFileLocationW( hinf, context, filenameW, source_id, bufferW, required, NULL ))
333         goto done;
334
335     size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
336     if (required_size) *required_size = size;
337
338     if (buffer)
339     {
340         if (buffer_size >= size)
341             WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, buffer_size, NULL, NULL );
342         else
343         {
344             SetLastError( ERROR_INSUFFICIENT_BUFFER );
345             goto done;
346         }
347     }
348     ret = TRUE;
349
350  done:
351     HeapFree( GetProcessHeap(), 0, filenameW );
352     HeapFree( GetProcessHeap(), 0, bufferW );
353     return ret;
354 }
355
356 static LPWSTR get_source_id( HINF hinf, PINFCONTEXT context, PCWSTR filename )
357 {
358     DWORD size;
359     LPWSTR source_id;
360
361     if (!SetupFindFirstLineW( hinf, source_disks_files_platform, filename, context ) &&
362         !SetupFindFirstLineW( hinf, source_disks_files, filename, context ))
363         return NULL;
364
365     if (!SetupGetStringFieldW( context, 1, NULL, 0, &size ))
366         return NULL;
367
368     if (!(source_id = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
369         return NULL;
370
371     if (!SetupGetStringFieldW( context, 1, source_id, size, NULL ))
372     {
373         HeapFree( GetProcessHeap(), 0, source_id );
374         return NULL;
375     }
376
377     if (!SetupFindFirstLineW( hinf, source_disks_names_platform, source_id, context ) &&
378         !SetupFindFirstLineW( hinf, source_disks_names, source_id, context ))
379     {
380         HeapFree( GetProcessHeap(), 0, source_id );
381         return NULL;
382     }
383     return source_id;
384 }
385
386 /***********************************************************************
387  *            SetupGetSourceFileLocationW   (SETUPAPI.@)
388  */
389
390 BOOL WINAPI SetupGetSourceFileLocationW( HINF hinf, PINFCONTEXT context, PCWSTR filename,
391                                          PUINT source_id, PWSTR buffer, DWORD buffer_size,
392                                          PDWORD required_size )
393 {
394     INFCONTEXT ctx;
395     WCHAR *end, *source_id_str;
396
397     TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf, context, debugstr_w(filename), source_id,
398           buffer, buffer_size, required_size);
399
400     if (!context) context = &ctx;
401
402     if (!(source_id_str = get_source_id( hinf, context, filename )))
403         return FALSE;
404
405     *source_id = strtolW( source_id_str, &end, 10 );
406     if (end == source_id_str || *end)
407     {
408         HeapFree( GetProcessHeap(), 0, source_id_str );
409         return FALSE;
410     }
411     HeapFree( GetProcessHeap(), 0, source_id_str );
412
413     if (SetupGetStringFieldW( context, 4, buffer, buffer_size, required_size ))
414         return TRUE;
415
416     if (required_size) *required_size = 1;
417     if (buffer)
418     {
419         if (buffer_size >= 1) buffer[0] = 0;
420         else
421         {
422             SetLastError( ERROR_INSUFFICIENT_BUFFER );
423             return FALSE;
424         }
425     }
426     return TRUE;
427 }
428
429 /***********************************************************************
430  *            SetupGetSourceInfoA  (SETUPAPI.@)
431  */
432
433 BOOL WINAPI SetupGetSourceInfoA( HINF hinf, UINT source_id, UINT info,
434                                  PSTR buffer, DWORD buffer_size, LPDWORD required_size )
435 {
436     BOOL ret = FALSE;
437     WCHAR *bufferW = NULL;
438     DWORD required;
439     INT size;
440
441     TRACE("%p, %d, %d, %p, %d, %p\n", hinf, source_id, info, buffer, buffer_size,
442           required_size);
443
444     if (!SetupGetSourceInfoW( hinf, source_id, info, NULL, 0, &required ))
445         return FALSE;
446
447     if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, required * sizeof(WCHAR) )))
448         return FALSE;
449
450     if (!SetupGetSourceInfoW( hinf, source_id, info, bufferW, required, NULL ))
451         goto done;
452
453     size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
454     if (required_size) *required_size = size;
455
456     if (buffer)
457     {
458         if (buffer_size >= size)
459             WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, buffer_size, NULL, NULL );
460         else
461         {
462             SetLastError( ERROR_INSUFFICIENT_BUFFER );
463             goto done;
464         }
465     }
466     ret = TRUE;
467
468  done:
469     HeapFree( GetProcessHeap(), 0, bufferW );
470     return ret;
471 }
472
473 /***********************************************************************
474  *            SetupGetSourceInfoW  (SETUPAPI.@)
475  */
476
477 BOOL WINAPI SetupGetSourceInfoW( HINF hinf, UINT source_id, UINT info,
478                                  PWSTR buffer, DWORD buffer_size, LPDWORD required_size )
479 {
480     INFCONTEXT ctx;
481     WCHAR source_id_str[11];
482     static const WCHAR fmt[] = {'%','d',0};
483     DWORD index;
484
485     TRACE("%p, %d, %d, %p, %d, %p\n", hinf, source_id, info, buffer, buffer_size,
486           required_size);
487
488     sprintfW( source_id_str, fmt, source_id );
489
490     if (!SetupFindFirstLineW( hinf, source_disks_names_platform, source_id_str, &ctx ) &&
491         !SetupFindFirstLineW( hinf, source_disks_names, source_id_str, &ctx ))
492         return FALSE;
493
494     switch (info)
495     {
496     case SRCINFO_PATH:          index = 4; break;
497     case SRCINFO_TAGFILE:       index = 2; break;
498     case SRCINFO_DESCRIPTION:   index = 1; break;
499     default:
500         WARN("unknown info level: %d\n", info);
501         return FALSE;
502     }
503
504     if (SetupGetStringFieldW( &ctx, index, buffer, buffer_size, required_size ))
505         return TRUE;
506
507     if (required_size) *required_size = 1;
508     if (buffer)
509     {
510         if (buffer_size >= 1) buffer[0] = 0;
511         else
512         {
513             SetLastError( ERROR_INSUFFICIENT_BUFFER );
514             return FALSE;
515         }
516     }
517     return TRUE;
518 }
519
520 /***********************************************************************
521  *            SetupGetTargetPathA   (SETUPAPI.@)
522  */
523
524 BOOL WINAPI SetupGetTargetPathA( HINF hinf, PINFCONTEXT context, PCSTR section, PSTR buffer,
525                                  DWORD buffer_size, PDWORD required_size )
526 {
527     BOOL ret = FALSE;
528     WCHAR *sectionW = NULL, *bufferW = NULL;
529     DWORD required;
530     INT size;
531
532     TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf, context, debugstr_a(section), buffer,
533           buffer_size, required_size);
534
535     if (section && !(sectionW = strdupAtoW( section )))
536         return FALSE;
537
538     if (!SetupGetTargetPathW( hinf, context, sectionW, NULL, 0, &required ))
539         goto done;
540
541     if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, required * sizeof(WCHAR) )))
542         goto done;
543
544     if (!SetupGetTargetPathW( hinf, context, sectionW, bufferW, required, NULL ))
545         goto done;
546
547     size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
548     if (required_size) *required_size = size;
549
550     if (buffer)
551     {
552         if (buffer_size >= size)
553             WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, buffer_size, NULL, NULL );
554         else
555         {
556             SetLastError( ERROR_INSUFFICIENT_BUFFER );
557             goto done;
558         }
559     }
560     ret = TRUE;
561
562  done:
563     HeapFree( GetProcessHeap(), 0, sectionW );
564     HeapFree( GetProcessHeap(), 0, bufferW );
565     return ret;
566 }
567
568 /***********************************************************************
569  *            SetupGetTargetPathW   (SETUPAPI.@)
570  */
571
572 BOOL WINAPI SetupGetTargetPathW( HINF hinf, PINFCONTEXT context, PCWSTR section, PWSTR buffer,
573                                  DWORD buffer_size, PDWORD required_size )
574 {
575     static const WCHAR destination_dirs[] =
576         {'D','e','s','t','i','n','a','t','i','o','n','D','i','r','s',0};
577     static const WCHAR default_dest_dir[]  =
578         {'D','e','f','a','u','l','t','D','e','s','t','D','i','r',0};
579
580     INFCONTEXT ctx;
581     WCHAR *dir;
582     INT size;
583
584     TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf, context, debugstr_w(section), buffer,
585           buffer_size, required_size);
586
587     if (context && !SetupFindFirstLineW( hinf, destination_dirs, NULL, context )) return FALSE;
588     else if (section && !SetupFindFirstLineW( hinf, section, NULL, &ctx )) return FALSE;
589     else if (!SetupFindFirstLineW( hinf, destination_dirs, default_dest_dir, &ctx )) return FALSE;
590
591     if (!(dir = PARSER_get_dest_dir( context ? context : &ctx ))) return FALSE;
592
593     size = lstrlenW( dir ) + 1;
594     if (required_size) *required_size = size;
595
596     if (buffer)
597     {
598         if (buffer_size >= size)
599             lstrcpyW( buffer, dir );
600         else
601         {
602             SetLastError( ERROR_INSUFFICIENT_BUFFER );
603             HeapFree( GetProcessHeap(), 0, dir );
604             return FALSE;
605         }
606     }
607     HeapFree( GetProcessHeap(), 0, dir );
608     return TRUE;
609 }
610
611 /***********************************************************************
612  *            SetupQueryInfOriginalFileInformationA   (SETUPAPI.@)
613  */
614 BOOL WINAPI SetupQueryInfOriginalFileInformationA(
615     PSP_INF_INFORMATION InfInformation, UINT InfIndex,
616     PSP_ALTPLATFORM_INFO AlternativePlatformInfo,
617     PSP_ORIGINAL_FILE_INFO_A OriginalFileInfo)
618 {
619     FIXME("(%p, %d, %p, %p): stub\n", InfInformation, InfIndex,
620         AlternativePlatformInfo, OriginalFileInfo);
621     return FALSE;
622 }
623
624 /***********************************************************************
625  *            SetupQueryInfOriginalFileInformationW   (SETUPAPI.@)
626  */
627 BOOL WINAPI SetupQueryInfOriginalFileInformationW(
628     PSP_INF_INFORMATION InfInformation, UINT InfIndex,
629     PSP_ALTPLATFORM_INFO AlternativePlatformInfo,
630     PSP_ORIGINAL_FILE_INFO_W OriginalFileInfo)
631 {
632     FIXME("(%p, %d, %p, %p): stub\n", InfInformation, InfIndex,
633         AlternativePlatformInfo, OriginalFileInfo);
634     return FALSE;
635 }