4 * Copyright 2002 Alexandre Julliard for CodeWeavers
5 * 2005 Hervé Poussineau (hpoussin@reactos.org)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
41 #include "setupapi_private.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
48 /* Unicode constants */
49 static const WCHAR BackSlash
[] = {'\\',0};
50 static const WCHAR InfDirectory
[] = {'i','n','f','\\',0};
51 static const WCHAR InfFileSpecification
[] = {'*','.','i','n','f',0};
53 #define CONTROL_Z '\x1a'
54 #define MAX_SECTION_NAME_LEN 255
55 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
56 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
57 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
59 /* inf file structure definitions */
63 const WCHAR
*text
; /* field text */
68 int first_field
; /* index of first field in field array */
69 int nb_fields
; /* number of fields in line */
70 int key_field
; /* index of field for key or -1 if no key */
75 const WCHAR
*name
; /* section name */
76 unsigned int nb_lines
; /* number of used lines */
77 unsigned int alloc_lines
; /* total number of allocated lines in array below */
78 struct line lines
[16]; /* lines information (grown dynamically, 16 is initial size) */
83 struct inf_file
*next
; /* next appended file */
84 WCHAR
*strings
; /* buffer for string data (section names and field values) */
85 WCHAR
*string_pos
; /* position of next available string in buffer */
86 unsigned int nb_sections
; /* number of used sections */
87 unsigned int alloc_sections
; /* total number of allocated section pointers */
88 struct section
**sections
; /* section pointers array */
89 unsigned int nb_fields
;
90 unsigned int alloc_fields
;
92 int strings_section
; /* index of [Strings] section or -1 if none */
93 WCHAR
*src_root
; /* source root directory */
96 /* parser definitions */
100 LINE_START
, /* at beginning of a line */
101 SECTION_NAME
, /* parsing a section name */
102 KEY_NAME
, /* parsing a key name */
103 VALUE_NAME
, /* parsing a value name */
104 EOL_BACKSLASH
, /* backslash at end of line */
105 QUOTES
, /* inside quotes */
106 LEADING_SPACES
, /* leading spaces */
107 TRAILING_SPACES
, /* trailing spaces */
108 COMMENT
, /* inside a comment */
114 const WCHAR
*start
; /* start position of item being parsed */
115 const WCHAR
*end
; /* end of buffer */
116 struct inf_file
*file
; /* file being built */
117 enum parser_state state
; /* current parser state */
118 enum parser_state stack
[4]; /* state stack */
119 int stack_pos
; /* current pos in stack */
121 int cur_section
; /* index of section being parsed*/
122 struct line
*line
; /* current line */
123 unsigned int line_pos
; /* current line position in file */
124 unsigned int error
; /* error code */
125 unsigned int token_len
; /* current token len */
126 WCHAR token
[MAX_FIELD_LEN
+1]; /* current token */
129 typedef const WCHAR
* (*parser_state_func
)( struct parser
*parser
, const WCHAR
*pos
);
131 /* parser state machine functions */
132 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
);
133 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
);
134 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
);
135 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
);
136 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
);
137 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
);
138 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
139 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
140 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
);
142 static const parser_state_func parser_funcs
[NB_PARSER_STATES
] =
144 line_start_state
, /* LINE_START */
145 section_name_state
, /* SECTION_NAME */
146 key_name_state
, /* KEY_NAME */
147 value_name_state
, /* VALUE_NAME */
148 eol_backslash_state
, /* EOL_BACKSLASH */
149 quotes_state
, /* QUOTES */
150 leading_spaces_state
, /* LEADING_SPACES */
151 trailing_spaces_state
, /* TRAILING_SPACES */
152 comment_state
/* COMMENT */
156 /* Unicode string constants */
157 static const WCHAR Version
[] = {'V','e','r','s','i','o','n',0};
158 static const WCHAR Signature
[] = {'S','i','g','n','a','t','u','r','e',0};
159 static const WCHAR Chicago
[] = {'$','C','h','i','c','a','g','o','$',0};
160 static const WCHAR WindowsNT
[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
161 static const WCHAR Windows95
[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
162 static const WCHAR LayoutFile
[] = {'L','a','y','o','u','t','F','i','l','e',0};
164 /* extend an array, allocating more memory if necessary */
165 static void *grow_array( void *array
, unsigned int *count
, size_t elem
)
168 unsigned int new_count
= *count
+ *count
/ 2;
169 if (new_count
< 32) new_count
= 32;
172 new_array
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, array
, new_count
* elem
);
174 new_array
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* elem
);
179 HeapFree( GetProcessHeap(), 0, array
);
184 /* find a section by name */
185 static int find_section( struct inf_file
*file
, const WCHAR
*name
)
189 for (i
= 0; i
< file
->nb_sections
; i
++)
190 if (!strcmpiW( name
, file
->sections
[i
]->name
)) return i
;
195 /* find a line by name */
196 static struct line
*find_line( struct inf_file
*file
, int section_index
, const WCHAR
*name
)
198 struct section
*section
;
202 if (section_index
< 0 || section_index
>= file
->nb_sections
) return NULL
;
203 section
= file
->sections
[section_index
];
204 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
206 if (line
->key_field
== -1) continue;
207 if (!strcmpiW( name
, file
->fields
[line
->key_field
].text
)) return line
;
213 /* add a section to the file and return the section index */
214 static int add_section( struct inf_file
*file
, const WCHAR
*name
)
216 struct section
*section
;
218 if (file
->nb_sections
>= file
->alloc_sections
)
220 if (!(file
->sections
= grow_array( file
->sections
, &file
->alloc_sections
,
221 sizeof(file
->sections
[0]) ))) return -1;
223 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) ))) return -1;
224 section
->name
= name
;
225 section
->nb_lines
= 0;
226 section
->alloc_lines
= sizeof(section
->lines
)/sizeof(section
->lines
[0]);
227 file
->sections
[file
->nb_sections
] = section
;
228 return file
->nb_sections
++;
232 /* add a line to a given section */
233 static struct line
*add_line( struct inf_file
*file
, int section_index
)
235 struct section
*section
;
238 assert( section_index
>= 0 && section_index
< file
->nb_sections
);
240 section
= file
->sections
[section_index
];
241 if (section
->nb_lines
== section
->alloc_lines
) /* need to grow the section */
243 int size
= sizeof(*section
) - sizeof(section
->lines
) + 2*section
->alloc_lines
*sizeof(*line
);
244 if (!(section
= HeapReAlloc( GetProcessHeap(), 0, section
, size
))) return NULL
;
245 section
->alloc_lines
*= 2;
246 file
->sections
[section_index
] = section
;
248 line
= §ion
->lines
[section
->nb_lines
++];
249 line
->first_field
= file
->nb_fields
;
251 line
->key_field
= -1;
256 /* retrieve a given line from section/line index */
257 inline static struct line
*get_line( struct inf_file
*file
, unsigned int section_index
,
258 unsigned int line_index
)
260 struct section
*section
;
262 if (section_index
>= file
->nb_sections
) return NULL
;
263 section
= file
->sections
[section_index
];
264 if (line_index
>= section
->nb_lines
) return NULL
;
265 return §ion
->lines
[line_index
];
269 /* retrieve a given field from section/line/field index */
270 static struct field
*get_field( struct inf_file
*file
, int section_index
, int line_index
,
273 struct line
*line
= get_line( file
, section_index
, line_index
);
275 if (!line
) return NULL
;
276 if (!field_index
) /* get the key */
278 if (line
->key_field
== -1) return NULL
;
279 return &file
->fields
[line
->key_field
];
282 if (field_index
>= line
->nb_fields
) return NULL
;
283 return &file
->fields
[line
->first_field
+ field_index
];
287 /* allocate a new field, growing the array if necessary */
288 static struct field
*add_field( struct inf_file
*file
, const WCHAR
*text
)
292 if (file
->nb_fields
>= file
->alloc_fields
)
294 if (!(file
->fields
= grow_array( file
->fields
, &file
->alloc_fields
,
295 sizeof(file
->fields
[0]) ))) return NULL
;
297 field
= &file
->fields
[file
->nb_fields
++];
303 /* retrieve the string substitution for a directory id */
304 static const WCHAR
*get_dirid_subst( int dirid
, unsigned int *len
)
306 extern const WCHAR
*DIRID_get_string( HINF hinf
, int dirid
);
307 const WCHAR
*ret
= DIRID_get_string( 0, dirid
);
308 if (ret
) *len
= strlenW(ret
);
313 /* retrieve the string substitution for a given string, or NULL if not found */
314 /* if found, len is set to the substitution length */
315 static const WCHAR
*get_string_subst( struct inf_file
*file
, const WCHAR
*str
, unsigned int *len
)
317 static const WCHAR percent
= '%';
319 struct section
*strings_section
;
324 WCHAR
*dirid_str
, *end
;
325 const WCHAR
*ret
= NULL
;
327 if (!*len
) /* empty string (%%) is replaced by single percent */
332 if (file
->strings_section
== -1) goto not_found
;
333 strings_section
= file
->sections
[file
->strings_section
];
334 for (i
= 0, line
= strings_section
->lines
; i
< strings_section
->nb_lines
; i
++, line
++)
336 if (line
->key_field
== -1) continue;
337 if (strncmpiW( str
, file
->fields
[line
->key_field
].text
, *len
)) continue;
338 if (!file
->fields
[line
->key_field
].text
[*len
]) break;
340 if (i
== strings_section
->nb_lines
|| !line
->nb_fields
) goto not_found
;
341 field
= &file
->fields
[line
->first_field
];
342 *len
= strlenW( field
->text
);
345 not_found
: /* check for integer id */
346 if ((dirid_str
= HeapAlloc( GetProcessHeap(), 0, (*len
+1) * sizeof(WCHAR
) )))
348 memcpy( dirid_str
, str
, *len
* sizeof(WCHAR
) );
350 dirid
= strtolW( dirid_str
, &end
, 10 );
351 if (!*end
) ret
= get_dirid_subst( dirid
, len
);
352 HeapFree( GetProcessHeap(), 0, dirid_str
);
359 /* do string substitutions on the specified text */
360 /* the buffer is assumed to be large enough */
361 /* returns necessary length not including terminating null */
362 unsigned int PARSER_string_substW( struct inf_file
*file
, const WCHAR
*text
, WCHAR
*buffer
,
365 const WCHAR
*start
, *subst
, *p
;
366 unsigned int len
, total
= 0;
369 if (!buffer
) size
= MAX_STRING_LEN
+ 1;
370 for (p
= start
= text
; *p
; p
++)
372 if (*p
!= '%') continue;
374 if (inside
) /* start of a %xx% string */
377 if (len
> size
- 1) len
= size
- 1;
378 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
383 else /* end of the %xx% string, find substitution */
386 subst
= get_string_subst( file
, start
+ 1, &len
);
392 if (len
> size
- 1) len
= size
- 1;
393 if (buffer
) memcpy( buffer
+ total
, subst
, len
* sizeof(WCHAR
) );
400 if (start
!= p
) /* unfinished string, copy it */
403 if (len
> size
- 1) len
= size
- 1;
404 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
407 if (buffer
&& size
) buffer
[total
] = 0;
412 /* do string substitutions on the specified text */
413 /* the buffer is assumed to be large enough */
414 /* returns necessary length not including terminating null */
415 unsigned int PARSER_string_substA( struct inf_file
*file
, const WCHAR
*text
, char *buffer
,
418 WCHAR buffW
[MAX_STRING_LEN
+1];
421 unsigned int len
= PARSER_string_substW( file
, text
, buffW
, sizeof(buffW
)/sizeof(WCHAR
) );
422 if (!buffer
) RtlUnicodeToMultiByteSize( &ret
, buffW
, len
* sizeof(WCHAR
) );
425 RtlUnicodeToMultiByteN( buffer
, size
-1, &ret
, buffW
, len
* sizeof(WCHAR
) );
432 /* push some string data into the strings buffer */
433 static WCHAR
*push_string( struct inf_file
*file
, const WCHAR
*string
)
435 WCHAR
*ret
= file
->string_pos
;
436 strcpyW( ret
, string
);
437 file
->string_pos
+= strlenW( ret
) + 1;
442 /* push the current state on the parser stack */
443 inline static void push_state( struct parser
*parser
, enum parser_state state
)
445 assert( parser
->stack_pos
< sizeof(parser
->stack
)/sizeof(parser
->stack
[0]) );
446 parser
->stack
[parser
->stack_pos
++] = state
;
450 /* pop the current state */
451 inline static void pop_state( struct parser
*parser
)
453 assert( parser
->stack_pos
);
454 parser
->state
= parser
->stack
[--parser
->stack_pos
];
458 /* set the parser state and return the previous one */
459 inline static enum parser_state
set_state( struct parser
*parser
, enum parser_state state
)
461 enum parser_state ret
= parser
->state
;
462 parser
->state
= state
;
467 /* check if the pointer points to an end of file */
468 inline static int is_eof( struct parser
*parser
, const WCHAR
*ptr
)
470 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
);
474 /* check if the pointer points to an end of line */
475 inline static int is_eol( struct parser
*parser
, const WCHAR
*ptr
)
477 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
|| *ptr
== '\n');
481 /* push data from current token start up to pos into the current token */
482 static int push_token( struct parser
*parser
, const WCHAR
*pos
)
484 int len
= pos
- parser
->start
;
485 const WCHAR
*src
= parser
->start
;
486 WCHAR
*dst
= parser
->token
+ parser
->token_len
;
488 if (len
> MAX_FIELD_LEN
- parser
->token_len
) len
= MAX_FIELD_LEN
- parser
->token_len
;
490 parser
->token_len
+= len
;
491 for ( ; len
> 0; len
--, dst
++, src
++) *dst
= *src
? *src
: ' ';
498 /* add a section with the current token as name */
499 static int add_section_from_token( struct parser
*parser
)
503 if (parser
->token_len
> MAX_SECTION_NAME_LEN
)
505 parser
->error
= ERROR_SECTION_NAME_TOO_LONG
;
508 if ((section_index
= find_section( parser
->file
, parser
->token
)) == -1)
510 /* need to create a new one */
511 const WCHAR
*name
= push_string( parser
->file
, parser
->token
);
512 if ((section_index
= add_section( parser
->file
, name
)) == -1)
514 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
518 parser
->token_len
= 0;
519 parser
->cur_section
= section_index
;
520 return section_index
;
524 /* add a field containing the current token to the current line */
525 static struct field
*add_field_from_token( struct parser
*parser
, int is_key
)
530 if (!parser
->line
) /* need to start a new line */
532 if (parser
->cur_section
== -1) /* got a line before the first section */
534 parser
->error
= ERROR_WRONG_INF_STYLE
;
537 if (!(parser
->line
= add_line( parser
->file
, parser
->cur_section
))) goto error
;
539 else assert(!is_key
);
541 text
= push_string( parser
->file
, parser
->token
);
542 if ((field
= add_field( parser
->file
, text
)))
544 if (!is_key
) parser
->line
->nb_fields
++;
547 /* replace first field by key field */
548 parser
->line
->key_field
= parser
->line
->first_field
;
549 parser
->line
->first_field
++;
551 parser
->token_len
= 0;
555 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
560 /* close the current line and prepare for parsing a new one */
561 static void close_current_line( struct parser
*parser
)
563 struct line
*cur_line
= parser
->line
;
567 /* if line has a single field and no key, the field is the key too */
568 if (cur_line
->nb_fields
== 1 && cur_line
->key_field
== -1)
569 cur_line
->key_field
= cur_line
->first_field
;
575 /* handler for parser LINE_START state */
576 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
)
580 for (p
= pos
; !is_eof( parser
, p
); p
++)
586 close_current_line( parser
);
589 push_state( parser
, LINE_START
);
590 set_state( parser
, COMMENT
);
593 parser
->start
= p
+ 1;
594 set_state( parser
, SECTION_NAME
);
600 set_state( parser
, KEY_NAME
);
606 close_current_line( parser
);
611 /* handler for parser SECTION_NAME state */
612 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
)
616 for (p
= pos
; !is_eol( parser
, p
); p
++)
620 push_token( parser
, p
);
621 if (add_section_from_token( parser
) == -1) return NULL
;
622 push_state( parser
, LINE_START
);
623 set_state( parser
, COMMENT
); /* ignore everything else on the line */
627 parser
->error
= ERROR_BAD_SECTION_NAME_LINE
; /* unfinished section name */
632 /* handler for parser KEY_NAME state */
633 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
)
635 const WCHAR
*p
, *token_end
= parser
->start
;
637 for (p
= pos
; !is_eol( parser
, p
); p
++)
639 if (*p
== ',') break;
644 push_token( parser
, token_end
);
645 if (!add_field_from_token( parser
, 1 )) return NULL
;
646 parser
->start
= p
+ 1;
647 push_state( parser
, VALUE_NAME
);
648 set_state( parser
, LEADING_SPACES
);
651 push_token( parser
, token_end
);
652 if (!add_field_from_token( parser
, 0 )) return NULL
;
653 push_state( parser
, LINE_START
);
654 set_state( parser
, COMMENT
);
657 push_token( parser
, p
);
658 parser
->start
= p
+ 1;
659 push_state( parser
, KEY_NAME
);
660 set_state( parser
, QUOTES
);
663 push_token( parser
, token_end
);
665 push_state( parser
, KEY_NAME
);
666 set_state( parser
, EOL_BACKSLASH
);
669 if (!isspaceW(*p
)) token_end
= p
+ 1;
672 push_token( parser
, p
);
673 push_state( parser
, KEY_NAME
);
674 set_state( parser
, TRAILING_SPACES
);
680 push_token( parser
, token_end
);
681 set_state( parser
, VALUE_NAME
);
686 /* handler for parser VALUE_NAME state */
687 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
)
689 const WCHAR
*p
, *token_end
= parser
->start
;
691 for (p
= pos
; !is_eol( parser
, p
); p
++)
696 push_token( parser
, token_end
);
697 if (!add_field_from_token( parser
, 0 )) return NULL
;
698 push_state( parser
, LINE_START
);
699 set_state( parser
, COMMENT
);
702 push_token( parser
, token_end
);
703 if (!add_field_from_token( parser
, 0 )) return NULL
;
704 parser
->start
= p
+ 1;
705 push_state( parser
, VALUE_NAME
);
706 set_state( parser
, LEADING_SPACES
);
709 push_token( parser
, p
);
710 parser
->start
= p
+ 1;
711 push_state( parser
, VALUE_NAME
);
712 set_state( parser
, QUOTES
);
715 push_token( parser
, token_end
);
717 push_state( parser
, VALUE_NAME
);
718 set_state( parser
, EOL_BACKSLASH
);
721 if (!isspaceW(*p
)) token_end
= p
+ 1;
724 push_token( parser
, p
);
725 push_state( parser
, VALUE_NAME
);
726 set_state( parser
, TRAILING_SPACES
);
732 push_token( parser
, token_end
);
733 if (!add_field_from_token( parser
, 0 )) return NULL
;
734 set_state( parser
, LINE_START
);
739 /* handler for parser EOL_BACKSLASH state */
740 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
)
744 for (p
= pos
; !is_eof( parser
, p
); p
++)
750 parser
->start
= p
+ 1;
751 set_state( parser
, LEADING_SPACES
);
756 push_state( parser
, EOL_BACKSLASH
);
757 set_state( parser
, COMMENT
);
760 if (isspaceW(*p
)) continue;
761 push_token( parser
, p
);
772 /* handler for parser QUOTES state */
773 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
)
775 const WCHAR
*p
, *token_end
= parser
->start
;
777 for (p
= pos
; !is_eol( parser
, p
); p
++)
781 if (p
+1 < parser
->end
&& p
[1] == '"') /* double quotes */
783 push_token( parser
, p
+ 1 );
784 parser
->start
= token_end
= p
+ 2;
787 else /* end of quotes */
789 push_token( parser
, p
);
790 parser
->start
= p
+ 1;
796 push_token( parser
, p
);
802 /* handler for parser LEADING_SPACES state */
803 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
807 for (p
= pos
; !is_eol( parser
, p
); p
++)
812 set_state( parser
, EOL_BACKSLASH
);
815 if (!isspaceW(*p
)) break;
823 /* handler for parser TRAILING_SPACES state */
824 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
828 for (p
= pos
; !is_eol( parser
, p
); p
++)
832 set_state( parser
, EOL_BACKSLASH
);
835 if (!isspaceW(*p
)) break;
842 /* handler for parser COMMENT state */
843 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
)
845 const WCHAR
*p
= pos
;
847 while (!is_eol( parser
, p
)) p
++;
853 /* parse a complete buffer */
854 static DWORD
parse_buffer( struct inf_file
*file
, const WCHAR
*buffer
, const WCHAR
*end
,
857 static const WCHAR Strings
[] = {'S','t','r','i','n','g','s',0};
859 struct parser parser
;
860 const WCHAR
*pos
= buffer
;
862 parser
.start
= buffer
;
866 parser
.state
= LINE_START
;
867 parser
.stack_pos
= 0;
868 parser
.cur_section
= -1;
871 parser
.token_len
= 0;
873 /* parser main loop */
874 while (pos
) pos
= (parser_funcs
[parser
.state
])( &parser
, pos
);
876 /* trim excess buffer space */
877 if (file
->alloc_sections
> file
->nb_sections
)
879 file
->sections
= HeapReAlloc( GetProcessHeap(), 0, file
->sections
,
880 file
->nb_sections
* sizeof(file
->sections
[0]) );
881 file
->alloc_sections
= file
->nb_sections
;
883 if (file
->alloc_fields
> file
->nb_fields
)
885 file
->fields
= HeapReAlloc( GetProcessHeap(), 0, file
->fields
,
886 file
->nb_fields
* sizeof(file
->fields
[0]) );
887 file
->alloc_fields
= file
->nb_fields
;
889 file
->strings
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, file
->strings
,
890 (file
->string_pos
- file
->strings
) * sizeof(WCHAR
) );
894 if (error_line
) *error_line
= parser
.line_pos
;
898 /* find the [strings] section */
899 file
->strings_section
= find_section( file
, Strings
);
904 /* append a child INF file to its parent list, in a thread-safe manner */
905 static void append_inf_file( struct inf_file
*parent
, struct inf_file
*child
)
907 struct inf_file
**ppnext
= &parent
->next
;
912 struct inf_file
*next
= InterlockedCompareExchangePointer( (void **)ppnext
, child
, NULL
);
914 ppnext
= &next
->next
;
919 /***********************************************************************
924 static struct inf_file
*parse_file( HANDLE handle
, const WCHAR
*class, UINT
*error_line
)
928 struct inf_file
*file
;
930 DWORD size
= GetFileSize( handle
, NULL
);
931 HANDLE mapping
= CreateFileMappingW( handle
, NULL
, PAGE_READONLY
, 0, size
, NULL
);
932 if (!mapping
) return NULL
;
933 buffer
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, size
);
935 if (!buffer
) return NULL
;
937 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
939 if (!(file
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*file
) )))
941 err
= ERROR_NOT_ENOUGH_MEMORY
;
945 /* we won't need more strings space than the size of the file,
946 * so we can preallocate it here
948 if (!(file
->strings
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
950 err
= ERROR_NOT_ENOUGH_MEMORY
;
953 file
->string_pos
= file
->strings
;
954 file
->strings_section
= -1;
956 if (!RtlIsTextUnicode( buffer
, size
, NULL
))
958 WCHAR
*new_buff
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) );
961 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, new_buff
, size
);
962 err
= parse_buffer( file
, new_buff
, new_buff
+ len
, error_line
);
963 HeapFree( GetProcessHeap(), 0, new_buff
);
968 WCHAR
*new_buff
= (WCHAR
*)buffer
;
969 /* Some UNICODE files may start with the UNICODE marker */
970 if (*new_buff
== 0xfeff)
972 err
= parse_buffer( file
, new_buff
, (WCHAR
*)((char *)new_buff
+ size
), error_line
);
975 if (!err
) /* now check signature */
977 int version_index
= find_section( file
, Version
);
978 if (version_index
!= -1)
980 struct line
*line
= find_line( file
, version_index
, Signature
);
981 if (line
&& line
->nb_fields
> 0)
983 struct field
*field
= file
->fields
+ line
->first_field
;
984 if (!strcmpiW( field
->text
, Chicago
)) goto done
;
985 if (!strcmpiW( field
->text
, WindowsNT
)) goto done
;
986 if (!strcmpiW( field
->text
, Windows95
)) goto done
;
989 err
= ERROR_WRONG_INF_STYLE
;
993 UnmapViewOfFile( buffer
);
996 HeapFree( GetProcessHeap(), 0, file
);
1004 /***********************************************************************
1005 * PARSER_get_src_root
1007 * Retrieve the source directory of an inf file.
1009 const WCHAR
*PARSER_get_src_root( HINF hinf
)
1011 struct inf_file
*file
= hinf
;
1012 return file
->src_root
;
1016 /***********************************************************************
1017 * PARSER_get_dest_dir
1019 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1020 * returned buffer must be freed by caller.
1022 WCHAR
*PARSER_get_dest_dir( INFCONTEXT
*context
)
1029 if (!SetupGetIntField( context
, 1, &dirid
)) return NULL
;
1030 if (!(dir
= DIRID_get_string( context
->Inf
, dirid
))) return NULL
;
1031 len1
= strlenW(dir
) + 1;
1032 if (!SetupGetStringFieldW( context
, 2, NULL
, 0, &len2
)) len2
= 0;
1033 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, (len1
+len2
) * sizeof(WCHAR
) ))) return NULL
;
1034 strcpyW( ret
, dir
);
1035 ptr
= ret
+ strlenW(ret
);
1036 if (len2
&& ptr
> ret
&& ptr
[-1] != '\\') *ptr
++ = '\\';
1037 if (!SetupGetStringFieldW( context
, 2, ptr
, len2
, NULL
)) *ptr
= 0;
1042 /***********************************************************************
1043 * SetupOpenInfFileA (SETUPAPI.@)
1045 HINF WINAPI
SetupOpenInfFileA( PCSTR name
, PCSTR
class, DWORD style
, UINT
*error
)
1047 UNICODE_STRING nameW
, classW
;
1048 HINF ret
= (HINF
)INVALID_HANDLE_VALUE
;
1050 classW
.Buffer
= NULL
;
1051 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW
, class ))
1053 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1056 if (RtlCreateUnicodeStringFromAsciiz( &nameW
, name
))
1058 ret
= SetupOpenInfFileW( nameW
.Buffer
, classW
.Buffer
, style
, error
);
1059 RtlFreeUnicodeString( &nameW
);
1061 RtlFreeUnicodeString( &classW
);
1066 /***********************************************************************
1067 * SetupOpenInfFileW (SETUPAPI.@)
1069 HINF WINAPI
SetupOpenInfFileW( PCWSTR name
, PCWSTR
class, DWORD style
, UINT
*error
)
1071 struct inf_file
*file
= NULL
;
1076 TRACE("%S %S %lx %p\n", name
, class, style
, error
);
1078 if (strchrW( name
, '\\' ) || strchrW( name
, '/' ))
1080 if (!(len
= GetFullPathNameW( name
, 0, NULL
, NULL
))) return (HINF
)INVALID_HANDLE_VALUE
;
1081 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1083 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1084 return (HINF
)INVALID_HANDLE_VALUE
;
1086 GetFullPathNameW( name
, len
, path
, NULL
);
1087 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1089 else /* try Windows directory */
1091 static const WCHAR Inf
[] = {'\\','i','n','f','\\',0};
1092 static const WCHAR System32
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1094 len
= GetWindowsDirectoryW( NULL
, 0 ) + strlenW(name
) + 12;
1095 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1097 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1098 return (HINF
)INVALID_HANDLE_VALUE
;
1100 GetWindowsDirectoryW( path
, len
);
1101 p
= path
+ strlenW(path
);
1104 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1105 if (handle
== INVALID_HANDLE_VALUE
)
1107 strcpyW( p
, System32
);
1109 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1113 if (handle
!= INVALID_HANDLE_VALUE
)
1115 file
= parse_file( handle
, class, error
);
1116 CloseHandle( handle
);
1120 HeapFree( GetProcessHeap(), 0, path
);
1121 return (HINF
)INVALID_HANDLE_VALUE
;
1123 TRACE( "%s -> %p\n", debugstr_w(path
), file
);
1124 file
->src_root
= path
;
1125 if ((p
= strrchrW( path
, '\\' ))) p
[1] = 0; /* remove file name */
1131 /***********************************************************************
1132 * SetupOpenAppendInfFileA (SETUPAPI.@)
1134 BOOL WINAPI
SetupOpenAppendInfFileA( PCSTR name
, HINF parent_hinf
, UINT
*error
)
1138 if (!name
) return SetupOpenAppendInfFileW( NULL
, parent_hinf
, error
);
1139 child_hinf
= SetupOpenInfFileA( name
, NULL
, INF_STYLE_WIN4
, error
);
1140 if (child_hinf
== (HINF
)INVALID_HANDLE_VALUE
) return FALSE
;
1141 append_inf_file( parent_hinf
, child_hinf
);
1142 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_a(name
), child_hinf
);
1147 /***********************************************************************
1148 * SetupOpenAppendInfFileW (SETUPAPI.@)
1150 BOOL WINAPI
SetupOpenAppendInfFileW( PCWSTR name
, HINF parent_hinf
, UINT
*error
)
1157 WCHAR filename
[MAX_PATH
];
1160 if (!SetupFindFirstLineW( parent_hinf
, Version
, LayoutFile
, &context
)) return FALSE
;
1161 while (SetupGetStringFieldW( &context
, idx
++, filename
,
1162 sizeof(filename
)/sizeof(WCHAR
), NULL
))
1164 child_hinf
= SetupOpenInfFileW( filename
, NULL
, INF_STYLE_WIN4
, error
);
1165 if (child_hinf
== (HINF
)INVALID_HANDLE_VALUE
) return FALSE
;
1166 append_inf_file( parent_hinf
, child_hinf
);
1167 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(filename
), child_hinf
);
1171 child_hinf
= SetupOpenInfFileW( name
, NULL
, INF_STYLE_WIN4
, error
);
1172 if (child_hinf
== (HINF
)INVALID_HANDLE_VALUE
) return FALSE
;
1173 append_inf_file( parent_hinf
, child_hinf
);
1174 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(name
), child_hinf
);
1179 /***********************************************************************
1180 * SetupOpenMasterInf (SETUPAPI.@)
1182 HINF WINAPI
SetupOpenMasterInf( VOID
)
1184 static const WCHAR Layout
[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1185 WCHAR Buffer
[MAX_PATH
];
1187 GetWindowsDirectoryW( Buffer
, MAX_PATH
);
1188 strcatW( Buffer
, Layout
);
1189 return SetupOpenInfFileW( Buffer
, NULL
, INF_STYLE_WIN4
, NULL
);
1194 /***********************************************************************
1195 * SetupCloseInfFile (SETUPAPI.@)
1197 void WINAPI
SetupCloseInfFile( HINF hinf
)
1199 struct inf_file
*file
= hinf
;
1202 for (i
= 0; i
< file
->nb_sections
; i
++) HeapFree( GetProcessHeap(), 0, file
->sections
[i
] );
1203 HeapFree( GetProcessHeap(), 0, file
->src_root
);
1204 HeapFree( GetProcessHeap(), 0, file
->sections
);
1205 HeapFree( GetProcessHeap(), 0, file
->fields
);
1206 HeapFree( GetProcessHeap(), 0, file
->strings
);
1207 HeapFree( GetProcessHeap(), 0, file
);
1211 /***********************************************************************
1212 * SetupGetLineCountA (SETUPAPI.@)
1214 LONG WINAPI
SetupGetLineCountA( HINF hinf
, PCSTR name
)
1216 UNICODE_STRING sectionW
;
1219 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, name
))
1220 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1223 ret
= SetupGetLineCountW( hinf
, sectionW
.Buffer
);
1224 RtlFreeUnicodeString( §ionW
);
1230 /***********************************************************************
1231 * SetupGetLineCountW (SETUPAPI.@)
1233 LONG WINAPI
SetupGetLineCountW( HINF hinf
, PCWSTR section
)
1235 struct inf_file
*file
= hinf
;
1239 for (file
= hinf
; file
; file
= file
->next
)
1241 if ((section_index
= find_section( file
, section
)) == -1) continue;
1242 if (ret
== -1) ret
= 0;
1243 ret
+= file
->sections
[section_index
]->nb_lines
;
1245 TRACE( "(%p,%s) returning %ld\n", hinf
, debugstr_w(section
), ret
);
1246 SetLastError( (ret
== -1) ? ERROR_SECTION_NOT_FOUND
: 0 );
1251 /***********************************************************************
1252 * SetupGetLineByIndexA (SETUPAPI.@)
1254 BOOL WINAPI
SetupGetLineByIndexA( HINF hinf
, PCSTR section
, DWORD index
, INFCONTEXT
*context
)
1256 UNICODE_STRING sectionW
;
1259 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1260 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1263 ret
= SetupGetLineByIndexW( hinf
, sectionW
.Buffer
, index
, context
);
1264 RtlFreeUnicodeString( §ionW
);
1270 /***********************************************************************
1271 * SetupGetLineByIndexW (SETUPAPI.@)
1273 BOOL WINAPI
SetupGetLineByIndexW( HINF hinf
, PCWSTR section
, DWORD index
, INFCONTEXT
*context
)
1275 struct inf_file
*file
= hinf
;
1278 SetLastError( ERROR_SECTION_NOT_FOUND
);
1279 for (file
= hinf
; file
; file
= file
->next
)
1281 if ((section_index
= find_section( file
, section
)) == -1) continue;
1282 SetLastError( ERROR_LINE_NOT_FOUND
);
1283 if (index
< file
->sections
[section_index
]->nb_lines
)
1285 context
->Inf
= hinf
;
1286 context
->CurrentInf
= file
;
1287 context
->Section
= section_index
;
1288 context
->Line
= index
;
1290 TRACE( "(%p,%s): returning %d/%ld\n",
1291 hinf
, debugstr_w(section
), section_index
, index
);
1294 index
-= file
->sections
[section_index
]->nb_lines
;
1296 TRACE( "(%p,%s) not found\n", hinf
, debugstr_w(section
) );
1301 /***********************************************************************
1302 * SetupFindFirstLineA (SETUPAPI.@)
1304 BOOL WINAPI
SetupFindFirstLineA( HINF hinf
, PCSTR section
, PCSTR key
, INFCONTEXT
*context
)
1306 UNICODE_STRING sectionW
, keyW
;
1309 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1311 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1315 if (!key
) ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, NULL
, context
);
1318 if (RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1320 ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, keyW
.Buffer
, context
);
1321 RtlFreeUnicodeString( &keyW
);
1323 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1325 RtlFreeUnicodeString( §ionW
);
1330 /***********************************************************************
1331 * SetupFindFirstLineW (SETUPAPI.@)
1333 BOOL WINAPI
SetupFindFirstLineW( HINF hinf
, PCWSTR section
, PCWSTR key
, INFCONTEXT
*context
)
1335 struct inf_file
*file
;
1338 SetLastError( ERROR_SECTION_NOT_FOUND
);
1339 for (file
= hinf
; file
; file
= file
->next
)
1341 if ((section_index
= find_section( file
, section
)) == -1) continue;
1346 ctx
.CurrentInf
= file
;
1347 ctx
.Section
= section_index
;
1349 return SetupFindNextMatchLineW( &ctx
, key
, context
);
1351 SetLastError( ERROR_LINE_NOT_FOUND
); /* found at least one section */
1352 if (file
->sections
[section_index
]->nb_lines
)
1354 context
->Inf
= hinf
;
1355 context
->CurrentInf
= file
;
1356 context
->Section
= section_index
;
1359 TRACE( "(%p,%s,%s): returning %d/0\n",
1360 hinf
, debugstr_w(section
), debugstr_w(key
), section_index
);
1364 TRACE( "(%p,%s,%s): not found\n", hinf
, debugstr_w(section
), debugstr_w(key
) );
1369 /***********************************************************************
1370 * SetupFindNextLine (SETUPAPI.@)
1372 BOOL WINAPI
SetupFindNextLine( PINFCONTEXT context_in
, PINFCONTEXT context_out
)
1374 struct inf_file
*file
= context_in
->CurrentInf
;
1375 struct section
*section
;
1377 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1379 section
= file
->sections
[context_in
->Section
];
1380 if (context_in
->Line
+1 < section
->nb_lines
)
1382 if (context_out
!= context_in
) *context_out
= *context_in
;
1383 context_out
->Line
++;
1388 /* now search the appended files */
1390 for (file
= file
->next
; file
; file
= file
->next
)
1392 int section_index
= find_section( file
, section
->name
);
1393 if (section_index
== -1) continue;
1394 if (file
->sections
[section_index
]->nb_lines
)
1396 context_out
->Inf
= context_in
->Inf
;
1397 context_out
->CurrentInf
= file
;
1398 context_out
->Section
= section_index
;
1399 context_out
->Line
= 0;
1405 SetLastError( ERROR_LINE_NOT_FOUND
);
1410 /***********************************************************************
1411 * SetupFindNextMatchLineA (SETUPAPI.@)
1413 BOOL WINAPI
SetupFindNextMatchLineA( PINFCONTEXT context_in
, PCSTR key
,
1414 PINFCONTEXT context_out
)
1416 UNICODE_STRING keyW
;
1419 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1421 if (!RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1422 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1425 ret
= SetupFindNextMatchLineW( context_in
, keyW
.Buffer
, context_out
);
1426 RtlFreeUnicodeString( &keyW
);
1432 /***********************************************************************
1433 * SetupFindNextMatchLineW (SETUPAPI.@)
1435 BOOL WINAPI
SetupFindNextMatchLineW( PINFCONTEXT context_in
, PCWSTR key
,
1436 PINFCONTEXT context_out
)
1438 struct inf_file
*file
= context_in
->CurrentInf
;
1439 struct section
*section
;
1443 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1445 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1447 section
= file
->sections
[context_in
->Section
];
1449 for (i
= context_in
->Line
+1, line
= §ion
->lines
[i
]; i
< section
->nb_lines
; i
++, line
++)
1451 if (line
->key_field
== -1) continue;
1452 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1454 if (context_out
!= context_in
) *context_out
= *context_in
;
1455 context_out
->Line
= i
;
1457 TRACE( "(%p,%s,%s): returning %d\n",
1458 file
, debugstr_w(section
->name
), debugstr_w(key
), i
);
1463 /* now search the appended files */
1465 for (file
= file
->next
; file
; file
= file
->next
)
1467 int section_index
= find_section( file
, section
->name
);
1468 if (section_index
== -1) continue;
1469 section
= file
->sections
[section_index
];
1470 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
1472 if (line
->key_field
== -1) continue;
1473 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1475 context_out
->Inf
= context_in
->Inf
;
1476 context_out
->CurrentInf
= file
;
1477 context_out
->Section
= section_index
;
1478 context_out
->Line
= i
;
1480 TRACE( "(%p,%s,%s): returning %d/%d\n",
1481 file
, debugstr_w(section
->name
), debugstr_w(key
), section_index
, i
);
1486 TRACE( "(%p,%s,%s): not found\n",
1487 context_in
->CurrentInf
, debugstr_w(section
->name
), debugstr_w(key
) );
1489 SetLastError( ERROR_LINE_NOT_FOUND
);
1494 /***********************************************************************
1495 * SetupGetLineTextW (SETUPAPI.@)
1497 BOOL WINAPI
SetupGetLineTextW( PINFCONTEXT context
, HINF hinf
, PCWSTR section_name
,
1498 PCWSTR key_name
, PWSTR buffer
, DWORD size
, PDWORD required
)
1500 struct inf_file
*file
;
1502 struct field
*field
;
1508 INFCONTEXT new_context
;
1509 if (!SetupFindFirstLineW( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1510 file
= new_context
.CurrentInf
;
1511 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1515 file
= context
->CurrentInf
;
1516 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1518 SetLastError( ERROR_LINE_NOT_FOUND
);
1523 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1524 total
+= PARSER_string_substW( file
, field
->text
, NULL
, 0 ) + 1;
1526 if (required
) *required
= total
;
1531 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1534 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1536 unsigned int len
= PARSER_string_substW( file
, field
->text
, buffer
, size
);
1537 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1545 /***********************************************************************
1546 * SetupGetLineTextA (SETUPAPI.@)
1548 BOOL WINAPI
SetupGetLineTextA( PINFCONTEXT context
, HINF hinf
, PCSTR section_name
,
1549 PCSTR key_name
, PSTR buffer
, DWORD size
, PDWORD required
)
1551 struct inf_file
*file
;
1553 struct field
*field
;
1559 INFCONTEXT new_context
;
1560 if (!SetupFindFirstLineA( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1561 file
= new_context
.CurrentInf
;
1562 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1566 file
= context
->CurrentInf
;
1567 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1569 SetLastError( ERROR_LINE_NOT_FOUND
);
1574 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1575 total
+= PARSER_string_substA( file
, field
->text
, NULL
, 0 ) + 1;
1577 if (required
) *required
= total
;
1582 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1585 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1587 unsigned int len
= PARSER_string_substA( file
, field
->text
, buffer
, size
);
1588 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1596 /***********************************************************************
1597 * SetupGetFieldCount (SETUPAPI.@)
1599 DWORD WINAPI
SetupGetFieldCount( PINFCONTEXT context
)
1601 struct inf_file
*file
= context
->CurrentInf
;
1602 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1604 if (!line
) return 0;
1605 return line
->nb_fields
;
1609 /***********************************************************************
1610 * SetupGetStringFieldA (SETUPAPI.@)
1612 BOOL WINAPI
SetupGetStringFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1613 DWORD size
, PDWORD required
)
1615 struct inf_file
*file
= context
->CurrentInf
;
1616 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1620 if (!field
) return FALSE
;
1621 len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 );
1622 if (required
) *required
= len
+ 1;
1627 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1630 PARSER_string_substA( file
, field
->text
, buffer
, size
);
1632 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1633 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1634 index
, debugstr_a(buffer
) );
1640 /***********************************************************************
1641 * SetupGetStringFieldW (SETUPAPI.@)
1643 BOOL WINAPI
SetupGetStringFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1644 DWORD size
, PDWORD required
)
1646 struct inf_file
*file
= context
->CurrentInf
;
1647 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1651 if (!field
) return FALSE
;
1652 len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 );
1653 if (required
) *required
= len
+ 1;
1658 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1661 PARSER_string_substW( file
, field
->text
, buffer
, size
);
1663 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1664 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1665 index
, debugstr_w(buffer
) );
1671 /***********************************************************************
1672 * SetupGetIntField (SETUPAPI.@)
1674 BOOL WINAPI
SetupGetIntField( PINFCONTEXT context
, DWORD index
, PINT result
)
1677 char *end
, *buffer
= localbuff
;
1682 if (!SetupGetStringFieldA( context
, index
, localbuff
, sizeof(localbuff
), &required
))
1684 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
1685 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
))) return FALSE
;
1686 if (!SetupGetStringFieldA( context
, index
, buffer
, required
, NULL
)) goto done
;
1688 res
= strtol( buffer
, &end
, 0 );
1689 if (end
!= buffer
&& !*end
)
1694 else SetLastError( ERROR_INVALID_DATA
);
1697 if (buffer
!= localbuff
) HeapFree( GetProcessHeap(), 0, buffer
);
1702 /***********************************************************************
1703 * SetupGetBinaryField (SETUPAPI.@)
1705 BOOL WINAPI
SetupGetBinaryField( PINFCONTEXT context
, DWORD index
, BYTE
*buffer
,
1706 DWORD size
, LPDWORD required
)
1708 struct inf_file
*file
= context
->CurrentInf
;
1709 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1710 struct field
*field
;
1715 SetLastError( ERROR_LINE_NOT_FOUND
);
1718 if (!index
|| index
> line
->nb_fields
)
1720 SetLastError( ERROR_INVALID_PARAMETER
);
1723 index
--; /* fields start at 0 */
1724 if (required
) *required
= line
->nb_fields
- index
;
1725 if (!buffer
) return TRUE
;
1726 if (size
< line
->nb_fields
- index
)
1728 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1731 field
= &file
->fields
[line
->first_field
+ index
];
1732 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1736 for (p
= field
->text
; *p
&& isxdigitW(*p
); p
++)
1738 if ((value
<<= 4) > 255)
1740 SetLastError( ERROR_INVALID_DATA
);
1743 if (*p
<= '9') value
|= (*p
- '0');
1744 else value
|= (tolowerW(*p
) - 'a' + 10);
1746 buffer
[i
- index
] = value
;
1748 if (TRACE_ON(setupapi
))
1750 TRACE( "%p/%p/%d/%d index %ld returning",
1751 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
, index
);
1752 for (i
= index
; i
< line
->nb_fields
; i
++) TRACE( " %02x", buffer
[i
- index
] );
1759 /***********************************************************************
1760 * SetupGetMultiSzFieldA (SETUPAPI.@)
1762 BOOL WINAPI
SetupGetMultiSzFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1763 DWORD size
, LPDWORD required
)
1765 struct inf_file
*file
= context
->CurrentInf
;
1766 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1767 struct field
*field
;
1774 SetLastError( ERROR_LINE_NOT_FOUND
);
1777 if (!index
|| index
> line
->nb_fields
)
1779 SetLastError( ERROR_INVALID_PARAMETER
);
1782 index
--; /* fields start at 0 */
1783 field
= &file
->fields
[line
->first_field
+ index
];
1784 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1786 if (!(len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 ))) break;
1790 if (required
) *required
= total
;
1791 if (!buffer
) return TRUE
;
1794 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1797 field
= &file
->fields
[line
->first_field
+ index
];
1798 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1800 if (!(len
= PARSER_string_substA( file
, field
->text
, buffer
, size
))) break;
1803 *buffer
= 0; /* add final null */
1808 /***********************************************************************
1809 * SetupGetMultiSzFieldW (SETUPAPI.@)
1811 BOOL WINAPI
SetupGetMultiSzFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1812 DWORD size
, LPDWORD required
)
1814 struct inf_file
*file
= context
->CurrentInf
;
1815 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1816 struct field
*field
;
1823 SetLastError( ERROR_LINE_NOT_FOUND
);
1826 if (!index
|| index
> line
->nb_fields
)
1828 SetLastError( ERROR_INVALID_PARAMETER
);
1831 index
--; /* fields start at 0 */
1832 field
= &file
->fields
[line
->first_field
+ index
];
1833 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1835 if (!(len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 ))) break;
1839 if (required
) *required
= total
;
1840 if (!buffer
) return TRUE
;
1843 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1846 field
= &file
->fields
[line
->first_field
+ index
];
1847 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1849 if (!(len
= PARSER_string_substW( file
, field
->text
, buffer
, size
))) break;
1852 *buffer
= 0; /* add final null */
1856 /***********************************************************************
1857 * SetupGetInfInformationW (SETUPAPI.@)
1860 SetupGetInfInformationW(
1862 IN DWORD SearchControl
,
1863 IN PSP_INF_INFORMATION ReturnBuffer
,
1864 IN DWORD ReturnBufferSize
,
1865 IN PDWORD RequiredSize
)
1871 TRACE("%p %lx %p %ld %p\n", InfSpec
, SearchControl
, ReturnBuffer
,
1872 ReturnBufferSize
, RequiredSize
);
1874 if (SearchControl
!= INFINFO_INF_SPEC_IS_HINF
1875 && SearchControl
!= INFINFO_INF_NAME_IS_ABSOLUTE
1876 && SearchControl
!= INFINFO_DEFAULT_SEARCH
1877 && SearchControl
!= INFINFO_REVERSE_DEFAULT_SEARCH
1878 && SearchControl
!= INFINFO_INF_PATH_LIST_SEARCH
)
1880 SetLastError(ERROR_INVALID_PARAMETER
);
1884 if (SearchControl
== INFINFO_INF_SPEC_IS_HINF
)
1885 hInf
= (HINF
)InfSpec
;
1888 /* open .inf file and put its handle to hInf */
1889 FIXME("SearchControl 0x%lx not implemented\n", SearchControl
);
1890 SetLastError(ERROR_GEN_FAILURE
);
1894 /* FIXME: add size of [Version] section */
1895 requiredSize
= sizeof(SP_INF_INFORMATION
);
1897 if (requiredSize
<= ReturnBufferSize
)
1899 ReturnBuffer
->InfStyle
= INF_STYLE_WIN4
; /* FIXME */
1900 ReturnBuffer
->InfCount
= 1; /* FIXME */
1901 /* FIXME: memcpy(ReturnBuffer->VersionData, ...); */
1905 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1908 *RequiredSize
= requiredSize
;
1910 if (SearchControl
!= INFINFO_INF_SPEC_IS_HINF
)
1911 SetupCloseInfFile(hInf
);
1915 /***********************************************************************
1916 * SetupGetInfFileListW (SETUPAPI.@)
1919 SetupGetInfFileListW(
1920 IN PCWSTR DirectoryPath OPTIONAL
,
1922 IN OUT PWSTR ReturnBuffer OPTIONAL
,
1923 IN DWORD ReturnBufferSize OPTIONAL
,
1924 OUT PDWORD RequiredSize OPTIONAL
)
1927 LPWSTR pFileSpecification
, pFileName
;
1928 LPWSTR pBuffer
= ReturnBuffer
;
1929 WIN32_FIND_DATAW wfdFileInfo
;
1930 PVOID Buffer
= NULL
;
1932 DWORD requiredSizeInfo
;
1933 DWORD requiredSize
= 0;
1936 TRACE("%S %lx %p %ld %p\n", DirectoryPath
, InfStyle
,
1937 ReturnBuffer
, ReturnBufferSize
, RequiredSize
);
1939 if (InfStyle
& ~(INF_STYLE_OLDNT
| INF_STYLE_WIN4
))
1941 TRACE("Unknown flags: 0x%08lx\n", InfStyle
& ~(INF_STYLE_OLDNT
| INF_STYLE_WIN4
));
1942 SetLastError(ERROR_INVALID_PARAMETER
);
1948 len
= wcslen(DirectoryPath
);
1949 pFileSpecification
= HeapAlloc(
1950 GetProcessHeap(), 0,
1951 (len
+ MAX_PATH
+ 2) * sizeof(WCHAR
));
1952 if (!pFileSpecification
)
1954 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1957 wcscpy(pFileSpecification
, DirectoryPath
);
1958 if (DirectoryPath
[len
] == '\\')
1960 pFileName
= &pFileSpecification
[len
];
1964 wcscat(pFileSpecification
, BackSlash
);
1965 pFileName
= &pFileSpecification
[len
+ 1];
1970 WCHAR windir
[MAX_PATH
];
1971 if (GetSystemWindowsDirectoryW(windir
, MAX_PATH
) == 0)
1973 len
= wcslen(windir
);
1974 pFileSpecification
= HeapAlloc(
1975 GetProcessHeap(), 0,
1976 (len
+ MAX_PATH
+ 6) * sizeof(WCHAR
));
1977 if (!pFileSpecification
)
1979 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1982 wcscpy(pFileSpecification
, windir
);
1983 if (windir
[len
] != '\\')
1984 wcscat(pFileSpecification
, BackSlash
);
1985 wcscat(pFileSpecification
, InfDirectory
);
1986 pFileName
= &pFileSpecification
[wcslen(pFileSpecification
)];
1988 wcscpy(pFileName
, InfFileSpecification
);
1989 hSearch
= FindFirstFileW(pFileSpecification
, &wfdFileInfo
);
1990 if (hSearch
== INVALID_HANDLE_VALUE
)
1992 HeapFree(GetProcessHeap(), 0, pFileSpecification
);
2001 wcscpy(pFileName
, wfdFileInfo
.cFileName
);
2002 hInf
= SetupOpenInfFileW(
2004 NULL
, /* Inf class */
2006 NULL
/* Error line */);
2007 if (hInf
== INVALID_HANDLE_VALUE
)
2009 if (GetLastError() == ERROR_CLASS_MISMATCH
)
2011 /* InfStyle was not correct. Skip this file */
2014 TRACE("Invalid .inf file %S\n", pFileSpecification
);
2018 ret
= SetupGetInfInformationW(
2020 INFINFO_INF_SPEC_IS_HINF
,
2023 if (!ret
&& GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
2028 Buffer
= HeapAlloc(GetProcessHeap(), 0, requiredSizeInfo
);
2031 SetupCloseInfFile(hInf
);
2033 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2037 ret
= SetupGetInfInformationW(
2039 INFINFO_INF_SPEC_IS_HINF
,
2040 Buffer
, requiredSizeInfo
,
2046 len
= wcslen(wfdFileInfo
.cFileName
) + 1;
2047 requiredSize
+= (DWORD
)(len
* sizeof(WCHAR
));
2048 if (requiredSize
<= ReturnBufferSize
)
2050 wcscpy(pBuffer
, wfdFileInfo
.cFileName
);
2051 pBuffer
= &pBuffer
[len
];
2053 HeapFree(GetProcessHeap(), 0, Buffer
);
2054 SetupCloseInfFile(hInf
);
2056 } while (FindNextFileW(hSearch
, &wfdFileInfo
));
2061 requiredSize
+= sizeof(WCHAR
); /* Final NULL char */
2062 if (requiredSize
<= ReturnBufferSize
)
2066 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2070 *RequiredSize
= requiredSize
;
2073 HeapFree(GetProcessHeap(), 0, pFileSpecification
);
2077 /***********************************************************************
2078 * SetupGetInfFileListA (SETUPAPI.@)
2081 SetupGetInfFileListA(
2082 IN PCSTR DirectoryPath OPTIONAL
,
2084 IN OUT PSTR ReturnBuffer OPTIONAL
,
2085 IN DWORD ReturnBufferSize OPTIONAL
,
2086 OUT PDWORD RequiredSize OPTIONAL
)
2088 PWSTR DirectoryPathW
= NULL
;
2089 PWSTR ReturnBufferW
= NULL
;
2092 TRACE("%s %lx %p %ld %p\n", debugstr_a(DirectoryPath
), InfStyle
,
2093 ReturnBuffer
, ReturnBufferSize
, RequiredSize
);
2095 if (DirectoryPath
!= NULL
)
2097 DirectoryPathW
= MultiByteToUnicode(DirectoryPath
, CP_ACP
);
2098 if (DirectoryPathW
== NULL
) goto Cleanup
;
2101 if (ReturnBuffer
!= NULL
&& ReturnBufferSize
!= 0)
2103 ReturnBufferW
= MyMalloc(ReturnBufferSize
* sizeof(WCHAR
));
2104 if (ReturnBufferW
== NULL
) goto Cleanup
;
2107 ret
= SetupGetInfFileListW(DirectoryPathW
, InfStyle
, ReturnBufferW
, ReturnBufferSize
, RequiredSize
);
2109 if (ret
&& ReturnBufferW
!= NULL
)
2111 ret
= WideCharToMultiByte(CP_ACP
, 0, ReturnBufferW
, -1, ReturnBuffer
, ReturnBufferSize
, NULL
, NULL
) != 0;
2115 if (DirectoryPathW
!= NULL
)
2116 MyFree(DirectoryPathW
);
2117 if (ReturnBufferW
!= NULL
)
2118 MyFree(ReturnBufferW
);
2123 /***********************************************************************
2124 * SetupDiGetINFClassW (SETUPAPI.@)
2127 SetupDiGetINFClassW(
2129 OUT LPGUID ClassGuid
,
2130 OUT PWSTR ClassName
,
2131 IN DWORD ClassNameSize
,
2132 OUT PDWORD RequiredSize OPTIONAL
)
2134 HINF hInf
= INVALID_HANDLE_VALUE
;
2136 WCHAR guidW
[MAX_GUID_STRING_LEN
+ 1];
2139 TRACE("%S %p %p %ld %p\n", InfName
, ClassGuid
,
2140 ClassName
, ClassNameSize
, RequiredSize
);
2142 /* Open .inf file */
2143 hInf
= SetupOpenInfFileW(InfName
, NULL
, INF_STYLE_WIN4
, NULL
);
2144 if (hInf
== INVALID_HANDLE_VALUE
)
2147 /* Read class Guid */
2148 if (!SetupGetLineTextW(NULL
, hInf
, L
"Version", L
"ClassGUID", guidW
, sizeof(guidW
), NULL
))
2150 guidW
[37] = '\0'; /* Replace the } by a NULL character */
2151 if (UuidFromStringW(&guidW
[1], ClassGuid
) != RPC_S_OK
)
2154 /* Read class name */
2155 ret
= SetupGetLineTextW(NULL
, hInf
, L
"Version", L
"Class", ClassName
, ClassNameSize
, &requiredSize
);
2156 if (ret
&& ClassName
== NULL
&& ClassNameSize
== 0)
2159 *RequiredSize
= requiredSize
;
2160 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2166 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2169 *RequiredSize
= requiredSize
;
2172 else if (!SetupDiClassNameFromGuidW(ClassGuid
, ClassName
, ClassNameSize
, &requiredSize
))
2174 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2177 *RequiredSize
= requiredSize
;
2180 /* Return a NULL class name */
2183 if (ClassNameSize
< 1)
2185 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2188 memcpy(ClassGuid
, &GUID_NULL
, sizeof(GUID
));
2189 *ClassName
= UNICODE_NULL
;
2196 if (hInf
!= INVALID_HANDLE_VALUE
)
2197 SetupCloseInfFile(hInf
);
2199 TRACE("Returning %d\n", ret
);
2203 /***********************************************************************
2204 * SetupDiGetINFClassA (SETUPAPI.@)
2206 BOOL WINAPI
SetupDiGetINFClassA(
2208 OUT LPGUID ClassGuid
,
2210 IN DWORD ClassNameSize
,
2211 OUT PDWORD RequiredSize OPTIONAL
)
2213 PWSTR InfNameW
= NULL
;
2214 PWSTR ClassNameW
= NULL
;
2217 TRACE("%s %p %p %ld %p\n", debugstr_a(InfName
), ClassGuid
,
2218 ClassName
, ClassNameSize
, RequiredSize
);
2220 if (InfName
!= NULL
)
2222 InfNameW
= MultiByteToUnicode(InfName
, CP_ACP
);
2223 if (InfNameW
== NULL
) goto Cleanup
;
2226 if (ClassName
!= NULL
&& ClassNameSize
!= 0)
2228 ClassNameW
= MyMalloc(ClassNameSize
* sizeof(WCHAR
));
2229 if (ClassNameW
== NULL
) goto Cleanup
;
2232 ret
= SetupDiGetINFClassW(InfNameW
, ClassGuid
, ClassNameW
, ClassNameSize
, RequiredSize
);
2234 if (ret
&& ClassNameW
!= NULL
)
2236 ret
= WideCharToMultiByte(CP_ACP
, 0, ClassNameW
, -1, ClassName
, ClassNameSize
, NULL
, NULL
) != 0;
2240 if (InfNameW
!= NULL
)
2242 if (ClassNameW
!= NULL
)