4 * Copyright 2002 Alexandre Julliard for CodeWeavers
5 * 2005-2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "setupapi_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
26 /* Unicode constants */
27 static const WCHAR BackSlash
[] = {'\\',0};
28 static const WCHAR Class
[] = {'C','l','a','s','s',0};
29 static const WCHAR ClassGUID
[] = {'C','l','a','s','s','G','U','I','D',0};
30 static const WCHAR InfDirectory
[] = {'i','n','f','\\',0};
31 static const WCHAR InfFileSpecification
[] = {'*','.','i','n','f',0};
33 #define CONTROL_Z '\x1a'
34 #define MAX_SECTION_NAME_LEN 255
35 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
36 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
37 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
39 /* inf file structure definitions */
43 const WCHAR
*text
; /* field text */
48 int first_field
; /* index of first field in field array */
49 int nb_fields
; /* number of fields in line */
50 int key_field
; /* index of field for key or -1 if no key */
55 const WCHAR
*name
; /* section name */
56 unsigned int nb_lines
; /* number of used lines */
57 unsigned int alloc_lines
; /* total number of allocated lines in array below */
58 struct line lines
[16]; /* lines information (grown dynamically, 16 is initial size) */
63 struct inf_file
*next
; /* next appended file */
64 WCHAR
*strings
; /* buffer for string data (section names and field values) */
65 WCHAR
*string_pos
; /* position of next available string in buffer */
66 unsigned int nb_sections
; /* number of used sections */
67 unsigned int alloc_sections
; /* total number of allocated section pointers */
68 struct section
**sections
; /* section pointers array */
69 unsigned int nb_fields
;
70 unsigned int alloc_fields
;
72 int strings_section
; /* index of [Strings] section or -1 if none */
73 WCHAR
*filename
; /* filename of the INF */
76 /* parser definitions */
80 LINE_START
, /* at beginning of a line */
81 SECTION_NAME
, /* parsing a section name */
82 KEY_NAME
, /* parsing a key name */
83 VALUE_NAME
, /* parsing a value name */
84 EOL_BACKSLASH
, /* backslash at end of line */
85 QUOTES
, /* inside quotes */
86 LEADING_SPACES
, /* leading spaces */
87 TRAILING_SPACES
, /* trailing spaces */
88 COMMENT
, /* inside a comment */
94 const WCHAR
*start
; /* start position of item being parsed */
95 const WCHAR
*end
; /* end of buffer */
96 struct inf_file
*file
; /* file being built */
97 enum parser_state state
; /* current parser state */
98 enum parser_state stack
[4]; /* state stack */
99 int stack_pos
; /* current pos in stack */
101 int cur_section
; /* index of section being parsed*/
102 struct line
*line
; /* current line */
103 unsigned int line_pos
; /* current line position in file */
104 unsigned int error
; /* error code */
105 unsigned int token_len
; /* current token len */
106 WCHAR token
[MAX_FIELD_LEN
+1]; /* current token */
109 typedef const WCHAR
* (*parser_state_func
)( struct parser
*parser
, const WCHAR
*pos
);
111 /* parser state machine functions */
112 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
);
113 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
);
114 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
);
115 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
);
116 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
);
117 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
);
118 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
119 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
120 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
);
122 static const parser_state_func parser_funcs
[NB_PARSER_STATES
] =
124 line_start_state
, /* LINE_START */
125 section_name_state
, /* SECTION_NAME */
126 key_name_state
, /* KEY_NAME */
127 value_name_state
, /* VALUE_NAME */
128 eol_backslash_state
, /* EOL_BACKSLASH */
129 quotes_state
, /* QUOTES */
130 leading_spaces_state
, /* LEADING_SPACES */
131 trailing_spaces_state
, /* TRAILING_SPACES */
132 comment_state
/* COMMENT */
136 /* Unicode string constants */
137 static const WCHAR Version
[] = {'V','e','r','s','i','o','n',0};
138 static const WCHAR Signature
[] = {'S','i','g','n','a','t','u','r','e',0};
139 static const WCHAR Chicago
[] = {'$','C','h','i','c','a','g','o','$',0};
140 static const WCHAR WindowsNT
[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
141 static const WCHAR Windows95
[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
142 static const WCHAR LayoutFile
[] = {'L','a','y','o','u','t','F','i','l','e',0};
144 /* extend an array, allocating more memory if necessary */
145 static void *grow_array( void *array
, unsigned int *count
, size_t elem
)
148 unsigned int new_count
= *count
+ *count
/ 2;
149 if (new_count
< 32) new_count
= 32;
152 new_array
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, array
, new_count
* elem
);
154 new_array
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* elem
);
159 HeapFree( GetProcessHeap(), 0, array
);
164 /* get the directory of the inf file (as counted string, not null-terminated) */
165 static const WCHAR
*get_inf_dir( const struct inf_file
*file
, unsigned int *len
)
167 const WCHAR
*p
= strrchrW( file
->filename
, '\\' );
168 *len
= p
? (p
+ 1 - file
->filename
) : 0;
169 return file
->filename
;
173 /* find a section by name */
174 static int find_section( const struct inf_file
*file
, const WCHAR
*name
)
178 for (i
= 0; i
< file
->nb_sections
; i
++)
179 if (!strcmpiW( name
, file
->sections
[i
]->name
)) return i
;
184 /* find a line by name */
185 static struct line
*find_line( struct inf_file
*file
, int section_index
, const WCHAR
*name
)
187 struct section
*section
;
191 if (section_index
< 0 || section_index
>= file
->nb_sections
) return NULL
;
192 section
= file
->sections
[section_index
];
193 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
195 if (line
->key_field
== -1) continue;
196 if (!strcmpiW( name
, file
->fields
[line
->key_field
].text
)) return line
;
202 /* add a section to the file and return the section index */
203 static int add_section( struct inf_file
*file
, const WCHAR
*name
)
205 struct section
*section
;
207 if (file
->nb_sections
>= file
->alloc_sections
)
209 if (!(file
->sections
= grow_array( file
->sections
, &file
->alloc_sections
,
210 sizeof(file
->sections
[0]) ))) return -1;
212 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) ))) return -1;
213 section
->name
= name
;
214 section
->nb_lines
= 0;
215 section
->alloc_lines
= sizeof(section
->lines
)/sizeof(section
->lines
[0]);
216 file
->sections
[file
->nb_sections
] = section
;
217 return file
->nb_sections
++;
221 /* add a line to a given section */
222 static struct line
*add_line( struct inf_file
*file
, int section_index
)
224 struct section
*section
;
227 ASSERT( section_index
>= 0 && section_index
< file
->nb_sections
);
229 section
= file
->sections
[section_index
];
230 if (section
->nb_lines
== section
->alloc_lines
) /* need to grow the section */
232 int size
= sizeof(*section
) - sizeof(section
->lines
) + 2*section
->alloc_lines
*sizeof(*line
);
233 if (!(section
= HeapReAlloc( GetProcessHeap(), 0, section
, size
))) return NULL
;
234 section
->alloc_lines
*= 2;
235 file
->sections
[section_index
] = section
;
237 line
= §ion
->lines
[section
->nb_lines
++];
238 line
->first_field
= file
->nb_fields
;
240 line
->key_field
= -1;
245 /* retrieve a given line from section/line index */
246 static inline struct line
*get_line( struct inf_file
*file
, unsigned int section_index
,
247 unsigned int line_index
)
249 struct section
*section
;
251 if (section_index
>= file
->nb_sections
) return NULL
;
252 section
= file
->sections
[section_index
];
253 if (line_index
>= section
->nb_lines
) return NULL
;
254 return §ion
->lines
[line_index
];
258 /* retrieve a given field from section/line/field index */
259 static struct field
*get_field( struct inf_file
*file
, int section_index
, int line_index
,
262 struct line
*line
= get_line( file
, section_index
, line_index
);
264 if (!line
) return NULL
;
265 if (!field_index
) /* get the key */
267 if (line
->key_field
== -1) return NULL
;
268 return &file
->fields
[line
->key_field
];
271 if (field_index
>= line
->nb_fields
) return NULL
;
272 return &file
->fields
[line
->first_field
+ field_index
];
276 /* allocate a new field, growing the array if necessary */
277 static struct field
*add_field( struct inf_file
*file
, const WCHAR
*text
)
281 if (file
->nb_fields
>= file
->alloc_fields
)
283 if (!(file
->fields
= grow_array( file
->fields
, &file
->alloc_fields
,
284 sizeof(file
->fields
[0]) ))) return NULL
;
286 field
= &file
->fields
[file
->nb_fields
++];
292 /* retrieve the string substitution for a directory id */
293 static const WCHAR
*get_dirid_subst( const struct inf_file
*file
, int dirid
, unsigned int *len
)
297 if (dirid
== DIRID_SRCPATH
) return get_inf_dir( file
, len
);
298 ret
= DIRID_get_string( dirid
);
299 if (ret
) *len
= strlenW(ret
);
304 /* retrieve the string substitution for a given string, or NULL if not found */
305 /* if found, len is set to the substitution length */
306 static const WCHAR
*get_string_subst( const struct inf_file
*file
, const WCHAR
*str
, unsigned int *len
,
307 BOOL no_trailing_slash
)
309 static const WCHAR percent
= '%';
311 struct section
*strings_section
;
316 WCHAR
*dirid_str
, *end
;
317 const WCHAR
*ret
= NULL
;
318 WCHAR StringLangId
[13] = {'S','t','r','i','n','g','s','.',0};
321 if (!*len
) /* empty string (%%) is replaced by single percent */
326 if (file
->strings_section
== -1) goto not_found
;
327 strings_section
= file
->sections
[file
->strings_section
];
328 for (j
= 0, line
= strings_section
->lines
; j
< strings_section
->nb_lines
; j
++, line
++)
330 if (line
->key_field
== -1) continue;
331 if (strncmpiW( str
, file
->fields
[line
->key_field
].text
, *len
)) continue;
332 if (!file
->fields
[line
->key_field
].text
[*len
]) break;
334 if (j
== strings_section
->nb_lines
|| !line
->nb_fields
) goto not_found
;
335 field
= &file
->fields
[line
->first_field
];
336 GetLocaleInfo(LOCALE_SYSTEM_DEFAULT
, LOCALE_ILANGUAGE
, Lang
, sizeof(Lang
)/sizeof(TCHAR
)); // get the current system locale for translated strings
337 strcatW(StringLangId
, Lang
); // append the Language identifier from GetLocaleInfo
338 // now you have e.g. Strings.0407 for german translations
339 for (i
= 0; i
< file
->nb_sections
; i
++) // search in all sections
341 if (!strcmpiW(file
->sections
[i
]->name
,StringLangId
)) // if the section is a Strings.* section
343 strings_section
= file
->sections
[i
]; // select this section for further use
344 for (j
= 0, line
= strings_section
->lines
; j
< strings_section
->nb_lines
; j
++, line
++) // process all lines in this section
346 if (line
->key_field
== -1) continue; // if no key then skip
347 if (strncmpiW( str
, file
->fields
[line
->key_field
].text
, *len
)) continue; // if wrong key name, then skip
348 if (!file
->fields
[line
->key_field
].text
[*len
]) // if value exist
350 field
= &file
->fields
[line
->first_field
]; // then extract value and
351 break; // no more search necessary
356 *len
= strlenW( field
->text
); // set length
357 ret
= field
->text
; // return the english or translated string
361 not_found
: /* check for integer id */
362 if ((dirid_str
= HeapAlloc( GetProcessHeap(), 0, (*len
+1) * sizeof(WCHAR
) )))
364 memcpy( dirid_str
, str
, *len
* sizeof(WCHAR
) );
366 dirid
= strtolW( dirid_str
, &end
, 10 );
367 if (!*end
) ret
= get_dirid_subst( file
, dirid
, len
);
368 if (no_trailing_slash
&& ret
&& *len
&& ret
[*len
- 1] == '\\') *len
-= 1;
369 HeapFree( GetProcessHeap(), 0, dirid_str
);
376 /* do string substitutions on the specified text */
377 /* the buffer is assumed to be large enough */
378 /* returns necessary length not including terminating null */
379 unsigned int PARSER_string_substW( const struct inf_file
*file
, const WCHAR
*text
, WCHAR
*buffer
,
382 const WCHAR
*start
, *subst
, *p
;
383 unsigned int len
, total
= 0;
386 if (!buffer
) size
= MAX_STRING_LEN
+ 1;
387 for (p
= start
= text
; *p
; p
++)
389 if (*p
!= '%') continue;
391 if (inside
) /* start of a %xx% string */
394 if (len
> size
- 1) len
= size
- 1;
395 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
400 else /* end of the %xx% string, find substitution */
403 subst
= get_string_subst( file
, start
+ 1, &len
, p
[1] == '\\' );
409 if (len
> size
- 1) len
= size
- 1;
410 if (buffer
) memcpy( buffer
+ total
, subst
, len
* sizeof(WCHAR
) );
417 if (start
!= p
) /* unfinished string, copy it */
420 if (len
> size
- 1) len
= size
- 1;
421 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
424 if (buffer
&& size
) buffer
[total
] = 0;
429 /* do string substitutions on the specified text */
430 /* the buffer is assumed to be large enough */
431 /* returns necessary length not including terminating null */
432 unsigned int PARSER_string_substA( const struct inf_file
*file
, const WCHAR
*text
, char *buffer
,
435 WCHAR buffW
[MAX_STRING_LEN
+1];
438 unsigned int len
= PARSER_string_substW( file
, text
, buffW
, sizeof(buffW
)/sizeof(WCHAR
) );
439 if (!buffer
) RtlUnicodeToMultiByteSize( &ret
, buffW
, len
* sizeof(WCHAR
) );
442 RtlUnicodeToMultiByteN( buffer
, size
-1, &ret
, buffW
, len
* sizeof(WCHAR
) );
449 /* push some string data into the strings buffer */
450 static WCHAR
*push_string( struct inf_file
*file
, const WCHAR
*string
)
452 WCHAR
*ret
= file
->string_pos
;
453 strcpyW( ret
, string
);
454 file
->string_pos
+= strlenW( ret
) + 1;
459 /* push the current state on the parser stack */
460 static inline void push_state( struct parser
*parser
, enum parser_state state
)
462 ASSERT( parser
->stack_pos
< sizeof(parser
->stack
)/sizeof(parser
->stack
[0]) );
463 parser
->stack
[parser
->stack_pos
++] = state
;
467 /* pop the current state */
468 static inline void pop_state( struct parser
*parser
)
470 ASSERT( parser
->stack_pos
);
471 parser
->state
= parser
->stack
[--parser
->stack_pos
];
475 /* set the parser state and return the previous one */
476 static inline enum parser_state
set_state( struct parser
*parser
, enum parser_state state
)
478 enum parser_state ret
= parser
->state
;
479 parser
->state
= state
;
484 /* check if the pointer points to an end of file */
485 static inline int is_eof( const struct parser
*parser
, const WCHAR
*ptr
)
487 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
);
491 /* check if the pointer points to an end of line */
492 static inline int is_eol( const struct parser
*parser
, const WCHAR
*ptr
)
494 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
|| *ptr
== '\n');
498 /* push data from current token start up to pos into the current token */
499 static int push_token( struct parser
*parser
, const WCHAR
*pos
)
501 int len
= pos
- parser
->start
;
502 const WCHAR
*src
= parser
->start
;
503 WCHAR
*dst
= parser
->token
+ parser
->token_len
;
505 if (len
> MAX_FIELD_LEN
- parser
->token_len
) len
= MAX_FIELD_LEN
- parser
->token_len
;
507 parser
->token_len
+= len
;
508 for ( ; len
> 0; len
--, dst
++, src
++) *dst
= *src
? *src
: ' ';
515 /* add a section with the current token as name */
516 static int add_section_from_token( struct parser
*parser
)
520 if (parser
->token_len
> MAX_SECTION_NAME_LEN
)
522 parser
->error
= ERROR_SECTION_NAME_TOO_LONG
;
525 if ((section_index
= find_section( parser
->file
, parser
->token
)) == -1)
527 /* need to create a new one */
528 const WCHAR
*name
= push_string( parser
->file
, parser
->token
);
529 if ((section_index
= add_section( parser
->file
, name
)) == -1)
531 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
535 parser
->token_len
= 0;
536 parser
->cur_section
= section_index
;
537 return section_index
;
541 /* add a field containing the current token to the current line */
542 static struct field
*add_field_from_token( struct parser
*parser
, int is_key
)
547 if (!parser
->line
) /* need to start a new line */
549 if (parser
->cur_section
== -1) /* got a line before the first section */
551 parser
->error
= ERROR_EXPECTED_SECTION_NAME
;
554 if (!(parser
->line
= add_line( parser
->file
, parser
->cur_section
))) goto error
;
556 else ASSERT(!is_key
);
558 text
= push_string( parser
->file
, parser
->token
);
559 if ((field
= add_field( parser
->file
, text
)))
561 if (!is_key
) parser
->line
->nb_fields
++;
564 /* replace first field by key field */
565 parser
->line
->key_field
= parser
->line
->first_field
;
566 parser
->line
->first_field
++;
568 parser
->token_len
= 0;
572 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
577 /* close the current line and prepare for parsing a new one */
578 static void close_current_line( struct parser
*parser
)
580 struct line
*cur_line
= parser
->line
;
584 /* if line has a single field and no key, the field is the key too */
585 if (cur_line
->nb_fields
== 1 && cur_line
->key_field
== -1)
586 cur_line
->key_field
= cur_line
->first_field
;
592 /* handler for parser LINE_START state */
593 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
)
597 for (p
= pos
; !is_eof( parser
, p
); p
++)
603 close_current_line( parser
);
606 push_state( parser
, LINE_START
);
607 set_state( parser
, COMMENT
);
610 parser
->start
= p
+ 1;
611 set_state( parser
, SECTION_NAME
);
617 set_state( parser
, KEY_NAME
);
623 close_current_line( parser
);
628 /* handler for parser SECTION_NAME state */
629 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
)
633 for (p
= pos
; !is_eol( parser
, p
); p
++)
637 push_token( parser
, p
);
638 if (add_section_from_token( parser
) == -1) return NULL
;
639 push_state( parser
, LINE_START
);
640 set_state( parser
, COMMENT
); /* ignore everything else on the line */
644 parser
->error
= ERROR_BAD_SECTION_NAME_LINE
; /* unfinished section name */
649 /* handler for parser KEY_NAME state */
650 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
)
652 const WCHAR
*p
, *token_end
= parser
->start
;
654 for (p
= pos
; !is_eol( parser
, p
); p
++)
656 if (*p
== ',') break;
661 push_token( parser
, token_end
);
662 if (!add_field_from_token( parser
, 1 )) return NULL
;
663 parser
->start
= p
+ 1;
664 push_state( parser
, VALUE_NAME
);
665 set_state( parser
, LEADING_SPACES
);
668 push_token( parser
, token_end
);
669 if (!add_field_from_token( parser
, 0 )) return NULL
;
670 push_state( parser
, LINE_START
);
671 set_state( parser
, COMMENT
);
674 push_token( parser
, p
);
675 parser
->start
= p
+ 1;
676 push_state( parser
, KEY_NAME
);
677 set_state( parser
, QUOTES
);
680 push_token( parser
, token_end
);
682 push_state( parser
, KEY_NAME
);
683 set_state( parser
, EOL_BACKSLASH
);
686 if (!isspaceW(*p
)) token_end
= p
+ 1;
689 push_token( parser
, p
);
690 push_state( parser
, KEY_NAME
);
691 set_state( parser
, TRAILING_SPACES
);
697 push_token( parser
, token_end
);
698 set_state( parser
, VALUE_NAME
);
703 /* handler for parser VALUE_NAME state */
704 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
)
706 const WCHAR
*p
, *token_end
= parser
->start
;
708 for (p
= pos
; !is_eol( parser
, p
); p
++)
713 push_token( parser
, token_end
);
714 if (!add_field_from_token( parser
, 0 )) return NULL
;
715 push_state( parser
, LINE_START
);
716 set_state( parser
, COMMENT
);
719 push_token( parser
, token_end
);
720 if (!add_field_from_token( parser
, 0 )) return NULL
;
721 parser
->start
= p
+ 1;
722 push_state( parser
, VALUE_NAME
);
723 set_state( parser
, LEADING_SPACES
);
726 push_token( parser
, p
);
727 parser
->start
= p
+ 1;
728 push_state( parser
, VALUE_NAME
);
729 set_state( parser
, QUOTES
);
732 push_token( parser
, token_end
);
734 push_state( parser
, VALUE_NAME
);
735 set_state( parser
, EOL_BACKSLASH
);
738 if (!isspaceW(*p
)) token_end
= p
+ 1;
741 push_token( parser
, p
);
742 push_state( parser
, VALUE_NAME
);
743 set_state( parser
, TRAILING_SPACES
);
749 push_token( parser
, token_end
);
750 if (!add_field_from_token( parser
, 0 )) return NULL
;
751 set_state( parser
, LINE_START
);
756 /* handler for parser EOL_BACKSLASH state */
757 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
)
761 for (p
= pos
; !is_eof( parser
, p
); p
++)
767 parser
->start
= p
+ 1;
768 set_state( parser
, LEADING_SPACES
);
773 push_state( parser
, EOL_BACKSLASH
);
774 set_state( parser
, COMMENT
);
777 if (isspaceW(*p
)) continue;
778 push_token( parser
, p
);
789 /* handler for parser QUOTES state */
790 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
)
792 const WCHAR
*p
, *token_end
= parser
->start
;
794 for (p
= pos
; !is_eol( parser
, p
); p
++)
798 if (p
+1 < parser
->end
&& p
[1] == '"') /* double quotes */
800 push_token( parser
, p
+ 1 );
801 parser
->start
= token_end
= p
+ 2;
804 else /* end of quotes */
806 push_token( parser
, p
);
807 parser
->start
= p
+ 1;
813 push_token( parser
, p
);
819 /* handler for parser LEADING_SPACES state */
820 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
824 for (p
= pos
; !is_eol( parser
, p
); p
++)
829 set_state( parser
, EOL_BACKSLASH
);
832 if (!isspaceW(*p
)) break;
840 /* handler for parser TRAILING_SPACES state */
841 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
845 for (p
= pos
; !is_eol( parser
, p
); p
++)
849 set_state( parser
, EOL_BACKSLASH
);
852 if (!isspaceW(*p
)) break;
859 /* handler for parser COMMENT state */
860 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
)
862 const WCHAR
*p
= pos
;
864 while (!is_eol( parser
, p
)) p
++;
870 /* parse a complete buffer */
871 static DWORD
parse_buffer( struct inf_file
*file
, const WCHAR
*buffer
, const WCHAR
*end
,
874 static const WCHAR Strings
[] = {'S','t','r','i','n','g','s',0};
876 struct parser parser
;
877 const WCHAR
*pos
= buffer
;
879 parser
.start
= buffer
;
883 parser
.state
= LINE_START
;
884 parser
.stack_pos
= 0;
885 parser
.cur_section
= -1;
888 parser
.token_len
= 0;
890 /* parser main loop */
891 while (pos
) pos
= (parser_funcs
[parser
.state
])( &parser
, pos
);
893 /* trim excess buffer space */
894 if (file
->alloc_sections
> file
->nb_sections
)
896 file
->sections
= HeapReAlloc( GetProcessHeap(), 0, file
->sections
,
897 file
->nb_sections
* sizeof(file
->sections
[0]) );
898 file
->alloc_sections
= file
->nb_sections
;
900 if (file
->alloc_fields
> file
->nb_fields
)
902 file
->fields
= HeapReAlloc( GetProcessHeap(), 0, file
->fields
,
903 file
->nb_fields
* sizeof(file
->fields
[0]) );
904 file
->alloc_fields
= file
->nb_fields
;
906 file
->strings
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, file
->strings
,
907 (file
->string_pos
- file
->strings
) * sizeof(WCHAR
) );
911 if (error_line
) *error_line
= parser
.line_pos
;
915 /* find the [strings] section */
916 file
->strings_section
= find_section( file
, Strings
);
921 /* append a child INF file to its parent list, in a thread-safe manner */
922 static void append_inf_file( struct inf_file
*parent
, struct inf_file
*child
)
924 struct inf_file
**ppnext
= &parent
->next
;
929 struct inf_file
*next
= InterlockedCompareExchangePointer( (void **)ppnext
, child
, NULL
);
931 ppnext
= &next
->next
;
936 /***********************************************************************
941 static struct inf_file
*parse_file( HANDLE handle
, UINT
*error_line
, DWORD style
)
945 struct inf_file
*file
;
947 DWORD size
= GetFileSize( handle
, NULL
);
948 HANDLE mapping
= CreateFileMappingW( handle
, NULL
, PAGE_READONLY
, 0, size
, NULL
);
949 if (!mapping
) return NULL
;
950 buffer
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, size
);
952 if (!buffer
) return NULL
;
954 if (!(file
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*file
) )))
956 err
= ERROR_NOT_ENOUGH_MEMORY
;
960 /* we won't need more strings space than the size of the file,
961 * so we can preallocate it here
963 if (!(file
->strings
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
965 err
= ERROR_NOT_ENOUGH_MEMORY
;
968 file
->string_pos
= file
->strings
;
969 file
->strings_section
= -1;
971 if (!RtlIsTextUnicode( buffer
, size
, NULL
))
973 static const BYTE utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
975 UINT codepage
= CP_ACP
;
978 if (size
> sizeof(utf8_bom
) && !memcmp( buffer
, utf8_bom
, sizeof(utf8_bom
) ))
981 offset
= sizeof(utf8_bom
);
984 if ((new_buff
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
986 DWORD len
= MultiByteToWideChar( codepage
, 0, (char *)buffer
+ offset
,
987 size
- offset
, new_buff
, size
);
988 err
= parse_buffer( file
, new_buff
, new_buff
+ len
, error_line
);
989 HeapFree( GetProcessHeap(), 0, new_buff
);
994 WCHAR
*new_buff
= (WCHAR
*)buffer
;
995 /* UCS-16 files should start with the Unicode BOM; we should skip it */
996 if (*new_buff
== 0xfeff)
998 err
= parse_buffer( file
, new_buff
, (WCHAR
*)((char *)buffer
+ size
), error_line
);
1001 if (!err
) /* now check signature */
1003 int version_index
= find_section( file
, Version
);
1004 if (version_index
!= -1)
1006 struct line
*line
= find_line( file
, version_index
, Signature
);
1007 if (line
&& line
->nb_fields
> 0)
1009 struct field
*field
= file
->fields
+ line
->first_field
;
1010 if (!strcmpiW( field
->text
, Chicago
)) goto done
;
1011 if (!strcmpiW( field
->text
, WindowsNT
)) goto done
;
1012 if (!strcmpiW( field
->text
, Windows95
)) goto done
;
1015 if (error_line
) *error_line
= 0;
1016 if (style
& INF_STYLE_WIN4
) err
= ERROR_WRONG_INF_STYLE
;
1020 UnmapViewOfFile( buffer
);
1023 HeapFree( GetProcessHeap(), 0, file
);
1024 SetLastError( err
);
1031 /***********************************************************************
1032 * PARSER_get_inf_filename
1034 * Retrieve the filename of an inf file.
1036 const WCHAR
*PARSER_get_inf_filename( HINF hinf
)
1038 struct inf_file
*file
= hinf
;
1039 return file
->filename
;
1043 /***********************************************************************
1044 * PARSER_get_src_root
1046 * Retrieve the source directory of an inf file.
1048 WCHAR
*PARSER_get_src_root( HINF hinf
)
1051 const WCHAR
*dir
= get_inf_dir( hinf
, &len
);
1052 WCHAR
*ret
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
1055 memcpy( ret
, dir
, len
* sizeof(WCHAR
) );
1062 /***********************************************************************
1063 * PARSER_get_dest_dir
1065 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1066 * returned buffer must be freed by caller.
1068 WCHAR
*PARSER_get_dest_dir( INFCONTEXT
*context
)
1076 if (!SetupGetIntField( context
, 1, &dirid
)) return NULL
;
1077 if (!(dir
= get_dirid_subst( context
->Inf
, dirid
, &len1
))) return NULL
;
1078 if (!SetupGetStringFieldW( context
, 2, NULL
, 0, &len2
)) len2
= 0;
1079 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, (len1
+len2
+1) * sizeof(WCHAR
) ))) return NULL
;
1080 memcpy( ret
, dir
, len1
* sizeof(WCHAR
) );
1082 if (len2
&& ptr
> ret
&& ptr
[-1] != '\\') *ptr
++ = '\\';
1083 if (!SetupGetStringFieldW( context
, 2, ptr
, len2
, NULL
)) *ptr
= 0;
1088 /***********************************************************************
1089 * SetupOpenInfFileA (SETUPAPI.@)
1091 HINF WINAPI
SetupOpenInfFileA( PCSTR name
, PCSTR
class, DWORD style
, UINT
*error
)
1093 UNICODE_STRING nameW
, classW
;
1094 HINF ret
= INVALID_HANDLE_VALUE
;
1096 classW
.Buffer
= NULL
;
1097 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW
, class ))
1099 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1102 if (RtlCreateUnicodeStringFromAsciiz( &nameW
, name
))
1104 ret
= SetupOpenInfFileW( nameW
.Buffer
, classW
.Buffer
, style
, error
);
1105 RtlFreeUnicodeString( &nameW
);
1107 RtlFreeUnicodeString( &classW
);
1113 PARSER_GetInfClassW(
1115 OUT LPGUID ClassGuid
,
1116 OUT PWSTR ClassName
,
1117 IN DWORD ClassNameSize
,
1118 OUT PDWORD RequiredSize OPTIONAL
)
1121 WCHAR guidW
[MAX_GUID_STRING_LEN
+ 1];
1124 /* Read class Guid */
1125 if (!SetupGetLineTextW(NULL
, hInf
, Version
, ClassGUID
, guidW
, sizeof(guidW
), NULL
))
1127 guidW
[37] = '\0'; /* Replace the } by a NULL character */
1128 if (UuidFromStringW(&guidW
[1], ClassGuid
) != RPC_S_OK
)
1131 /* Read class name */
1132 ret
= SetupGetLineTextW(NULL
, hInf
, Version
, Class
, ClassName
, ClassNameSize
, &requiredSize
);
1133 if (ret
&& ClassName
== NULL
&& ClassNameSize
== 0)
1136 *RequiredSize
= requiredSize
;
1137 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1143 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1146 *RequiredSize
= requiredSize
;
1149 else if (!SetupDiClassNameFromGuidW(ClassGuid
, ClassName
, ClassNameSize
, &requiredSize
))
1151 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1154 *RequiredSize
= requiredSize
;
1157 /* Return a NULL class name */
1160 if (ClassNameSize
< 1)
1162 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1165 memcpy(ClassGuid
, &GUID_NULL
, sizeof(GUID
));
1166 *ClassName
= UNICODE_NULL
;
1173 TRACE("Returning %d\n", ret
);
1178 /***********************************************************************
1179 * SetupOpenInfFileW (SETUPAPI.@)
1181 HINF WINAPI
SetupOpenInfFileW( PCWSTR name
, PCWSTR
class, DWORD style
, UINT
*error
)
1183 struct inf_file
*file
= NULL
;
1188 TRACE("%s %s %lx %p\n", debugstr_w(name
), debugstr_w(class), style
, error
);
1190 if (style
& ~(INF_STYLE_OLDNT
| INF_STYLE_WIN4
))
1192 SetLastError(ERROR_INVALID_PARAMETER
);
1193 return (HINF
)INVALID_HANDLE_VALUE
;
1196 if (strchrW( name
, '\\' ) || strchrW( name
, '/' ))
1198 if (!(len
= GetFullPathNameW( name
, 0, NULL
, NULL
))) return INVALID_HANDLE_VALUE
;
1199 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1201 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1202 return INVALID_HANDLE_VALUE
;
1204 GetFullPathNameW( name
, len
, path
, NULL
);
1205 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1207 else /* try Windows directory */
1209 static const WCHAR Inf
[] = {'\\','i','n','f','\\',0};
1210 static const WCHAR System32
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1212 len
= GetWindowsDirectoryW( NULL
, 0 ) + strlenW(name
) + 12;
1213 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1215 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1216 return INVALID_HANDLE_VALUE
;
1218 GetWindowsDirectoryW( path
, len
);
1219 p
= path
+ strlenW(path
);
1222 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1223 if (handle
== INVALID_HANDLE_VALUE
)
1225 strcpyW( p
, System32
);
1227 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1231 if (handle
!= INVALID_HANDLE_VALUE
)
1233 file
= parse_file( handle
, error
, style
);
1234 CloseHandle( handle
);
1238 HeapFree( GetProcessHeap(), 0, path
);
1239 return INVALID_HANDLE_VALUE
;
1241 TRACE( "%s -> %p\n", debugstr_w(path
), file
);
1242 file
->filename
= path
;
1247 LPWSTR ClassName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(class) + 1) * sizeof(WCHAR
));
1250 /* Not enough memory */
1251 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1252 SetupCloseInfFile((HINF
)file
);
1253 return INVALID_HANDLE_VALUE
;
1255 else if (!PARSER_GetInfClassW((HINF
)file
, &ClassGuid
, ClassName
, strlenW(class) + 1, NULL
))
1257 /* Unable to get class name in .inf file */
1258 HeapFree(GetProcessHeap(), 0, ClassName
);
1259 SetLastError(ERROR_CLASS_MISMATCH
);
1260 SetupCloseInfFile((HINF
)file
);
1261 return INVALID_HANDLE_VALUE
;
1263 else if (strcmpW(class, ClassName
) != 0)
1265 /* Provided name name is not the expected one */
1266 HeapFree(GetProcessHeap(), 0, ClassName
);
1267 SetLastError(ERROR_CLASS_MISMATCH
);
1268 SetupCloseInfFile((HINF
)file
);
1269 return INVALID_HANDLE_VALUE
;
1271 HeapFree(GetProcessHeap(), 0, ClassName
);
1279 /***********************************************************************
1280 * SetupOpenAppendInfFileA (SETUPAPI.@)
1282 BOOL WINAPI
SetupOpenAppendInfFileA( PCSTR name
, HINF parent_hinf
, UINT
*error
)
1286 if (!name
) return SetupOpenAppendInfFileW( NULL
, parent_hinf
, error
);
1287 child_hinf
= SetupOpenInfFileA( name
, NULL
, INF_STYLE_WIN4
, error
);
1288 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1289 append_inf_file( parent_hinf
, child_hinf
);
1290 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_a(name
), child_hinf
);
1295 /***********************************************************************
1296 * SetupOpenAppendInfFileW (SETUPAPI.@)
1298 BOOL WINAPI
SetupOpenAppendInfFileW( PCWSTR name
, HINF parent_hinf
, UINT
*error
)
1305 WCHAR filename
[MAX_PATH
];
1308 if (!SetupFindFirstLineW( parent_hinf
, Version
, LayoutFile
, &context
)) return FALSE
;
1309 while (SetupGetStringFieldW( &context
, idx
++, filename
,
1310 sizeof(filename
)/sizeof(WCHAR
), NULL
))
1312 child_hinf
= SetupOpenInfFileW( filename
, NULL
, INF_STYLE_WIN4
, error
);
1313 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1314 append_inf_file( parent_hinf
, child_hinf
);
1315 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(filename
), child_hinf
);
1319 child_hinf
= SetupOpenInfFileW( name
, NULL
, INF_STYLE_WIN4
, error
);
1320 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1321 append_inf_file( parent_hinf
, child_hinf
);
1322 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(name
), child_hinf
);
1327 /***********************************************************************
1328 * SetupOpenMasterInf (SETUPAPI.@)
1330 HINF WINAPI
SetupOpenMasterInf( VOID
)
1332 static const WCHAR Layout
[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1333 WCHAR Buffer
[MAX_PATH
];
1335 GetWindowsDirectoryW( Buffer
, MAX_PATH
);
1336 strcatW( Buffer
, Layout
);
1337 return SetupOpenInfFileW( Buffer
, NULL
, INF_STYLE_WIN4
, NULL
);
1342 /***********************************************************************
1343 * SetupCloseInfFile (SETUPAPI.@)
1345 void WINAPI
SetupCloseInfFile( HINF hinf
)
1347 struct inf_file
*file
= hinf
;
1350 if (!hinf
|| (hinf
== INVALID_HANDLE_VALUE
)) return;
1352 for (i
= 0; i
< file
->nb_sections
; i
++) HeapFree( GetProcessHeap(), 0, file
->sections
[i
] );
1353 HeapFree( GetProcessHeap(), 0, file
->filename
);
1354 HeapFree( GetProcessHeap(), 0, file
->sections
);
1355 HeapFree( GetProcessHeap(), 0, file
->fields
);
1356 HeapFree( GetProcessHeap(), 0, file
->strings
);
1357 HeapFree( GetProcessHeap(), 0, file
);
1361 /***********************************************************************
1362 * SetupGetLineCountA (SETUPAPI.@)
1364 LONG WINAPI
SetupGetLineCountA( HINF hinf
, PCSTR name
)
1366 UNICODE_STRING sectionW
;
1369 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, name
))
1370 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1373 ret
= SetupGetLineCountW( hinf
, sectionW
.Buffer
);
1374 RtlFreeUnicodeString( §ionW
);
1380 /***********************************************************************
1381 * SetupGetLineCountW (SETUPAPI.@)
1383 LONG WINAPI
SetupGetLineCountW( HINF hinf
, PCWSTR section
)
1385 struct inf_file
*file
= hinf
;
1389 for (file
= hinf
; file
; file
= file
->next
)
1391 if ((section_index
= find_section( file
, section
)) == -1) continue;
1392 if (ret
== -1) ret
= 0;
1393 ret
+= file
->sections
[section_index
]->nb_lines
;
1395 TRACE( "(%p,%s) returning %d\n", hinf
, debugstr_w(section
), ret
);
1396 SetLastError( (ret
== -1) ? ERROR_SECTION_NOT_FOUND
: 0 );
1401 /***********************************************************************
1402 * SetupGetLineByIndexA (SETUPAPI.@)
1404 BOOL WINAPI
SetupGetLineByIndexA( HINF hinf
, PCSTR section
, DWORD index
, INFCONTEXT
*context
)
1406 UNICODE_STRING sectionW
;
1409 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1410 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1413 ret
= SetupGetLineByIndexW( hinf
, sectionW
.Buffer
, index
, context
);
1414 RtlFreeUnicodeString( §ionW
);
1420 /***********************************************************************
1421 * SetupGetLineByIndexW (SETUPAPI.@)
1423 BOOL WINAPI
SetupGetLineByIndexW( HINF hinf
, PCWSTR section
, DWORD index
, INFCONTEXT
*context
)
1425 struct inf_file
*file
= hinf
;
1428 SetLastError( ERROR_SECTION_NOT_FOUND
);
1429 for (file
= hinf
; file
; file
= file
->next
)
1431 if ((section_index
= find_section( file
, section
)) == -1) continue;
1432 SetLastError( ERROR_LINE_NOT_FOUND
);
1433 if (index
< file
->sections
[section_index
]->nb_lines
)
1435 context
->Inf
= hinf
;
1436 context
->CurrentInf
= file
;
1437 context
->Section
= section_index
;
1438 context
->Line
= index
;
1440 TRACE( "(%p,%s): returning %d/%d\n",
1441 hinf
, debugstr_w(section
), section_index
, index
);
1444 index
-= file
->sections
[section_index
]->nb_lines
;
1446 TRACE( "(%p,%s) not found\n", hinf
, debugstr_w(section
) );
1451 /***********************************************************************
1452 * SetupFindFirstLineA (SETUPAPI.@)
1454 BOOL WINAPI
SetupFindFirstLineA( HINF hinf
, PCSTR section
, PCSTR key
, INFCONTEXT
*context
)
1456 UNICODE_STRING sectionW
, keyW
;
1459 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1461 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1465 if (!key
) ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, NULL
, context
);
1468 if (RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1470 ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, keyW
.Buffer
, context
);
1471 RtlFreeUnicodeString( &keyW
);
1473 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1475 RtlFreeUnicodeString( §ionW
);
1480 /***********************************************************************
1481 * SetupFindFirstLineW (SETUPAPI.@)
1483 BOOL WINAPI
SetupFindFirstLineW( HINF hinf
, PCWSTR section
, PCWSTR key
, INFCONTEXT
*context
)
1485 struct inf_file
*file
;
1488 SetLastError( ERROR_SECTION_NOT_FOUND
);
1489 for (file
= hinf
; file
; file
= file
->next
)
1491 if ((section_index
= find_section( file
, section
)) == -1) continue;
1496 ctx
.CurrentInf
= file
;
1497 ctx
.Section
= section_index
;
1499 return SetupFindNextMatchLineW( &ctx
, key
, context
);
1501 SetLastError( ERROR_LINE_NOT_FOUND
); /* found at least one section */
1502 if (file
->sections
[section_index
]->nb_lines
)
1504 context
->Inf
= hinf
;
1505 context
->CurrentInf
= file
;
1506 context
->Section
= section_index
;
1509 TRACE( "(%p,%s,%s): returning %d/0\n",
1510 hinf
, debugstr_w(section
), debugstr_w(key
), section_index
);
1514 TRACE( "(%p,%s,%s): not found\n", hinf
, debugstr_w(section
), debugstr_w(key
) );
1519 /***********************************************************************
1520 * SetupFindNextLine (SETUPAPI.@)
1522 BOOL WINAPI
SetupFindNextLine( PINFCONTEXT context_in
, PINFCONTEXT context_out
)
1524 struct inf_file
*file
= context_in
->CurrentInf
;
1525 struct section
*section
;
1527 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1529 section
= file
->sections
[context_in
->Section
];
1530 if (context_in
->Line
+1 < section
->nb_lines
)
1532 if (context_out
!= context_in
) *context_out
= *context_in
;
1533 context_out
->Line
++;
1538 /* now search the appended files */
1540 for (file
= file
->next
; file
; file
= file
->next
)
1542 int section_index
= find_section( file
, section
->name
);
1543 if (section_index
== -1) continue;
1544 if (file
->sections
[section_index
]->nb_lines
)
1546 context_out
->Inf
= context_in
->Inf
;
1547 context_out
->CurrentInf
= file
;
1548 context_out
->Section
= section_index
;
1549 context_out
->Line
= 0;
1555 SetLastError( ERROR_LINE_NOT_FOUND
);
1560 /***********************************************************************
1561 * SetupFindNextMatchLineA (SETUPAPI.@)
1563 BOOL WINAPI
SetupFindNextMatchLineA( PINFCONTEXT context_in
, PCSTR key
,
1564 PINFCONTEXT context_out
)
1566 UNICODE_STRING keyW
;
1569 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1571 if (!RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1572 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1575 ret
= SetupFindNextMatchLineW( context_in
, keyW
.Buffer
, context_out
);
1576 RtlFreeUnicodeString( &keyW
);
1582 /***********************************************************************
1583 * SetupFindNextMatchLineW (SETUPAPI.@)
1585 BOOL WINAPI
SetupFindNextMatchLineW( PINFCONTEXT context_in
, PCWSTR key
,
1586 PINFCONTEXT context_out
)
1588 struct inf_file
*file
= context_in
->CurrentInf
;
1589 struct section
*section
;
1593 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1595 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1597 section
= file
->sections
[context_in
->Section
];
1599 for (i
= context_in
->Line
+1, line
= §ion
->lines
[i
]; i
< section
->nb_lines
; i
++, line
++)
1601 if (line
->key_field
== -1) continue;
1602 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1604 if (context_out
!= context_in
) *context_out
= *context_in
;
1605 context_out
->Line
= i
;
1607 TRACE( "(%p,%s,%s): returning %d\n",
1608 file
, debugstr_w(section
->name
), debugstr_w(key
), i
);
1613 /* now search the appended files */
1615 for (file
= file
->next
; file
; file
= file
->next
)
1617 int section_index
= find_section( file
, section
->name
);
1618 if (section_index
== -1) continue;
1619 section
= file
->sections
[section_index
];
1620 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
1622 if (line
->key_field
== -1) continue;
1623 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1625 context_out
->Inf
= context_in
->Inf
;
1626 context_out
->CurrentInf
= file
;
1627 context_out
->Section
= section_index
;
1628 context_out
->Line
= i
;
1630 TRACE( "(%p,%s,%s): returning %d/%d\n",
1631 file
, debugstr_w(section
->name
), debugstr_w(key
), section_index
, i
);
1636 TRACE( "(%p,%s,%s): not found\n",
1637 context_in
->CurrentInf
, debugstr_w(section
->name
), debugstr_w(key
) );
1639 SetLastError( ERROR_LINE_NOT_FOUND
);
1644 /***********************************************************************
1645 * SetupGetLineTextW (SETUPAPI.@)
1647 BOOL WINAPI
SetupGetLineTextW( PINFCONTEXT context
, HINF hinf
, PCWSTR section_name
,
1648 PCWSTR key_name
, PWSTR buffer
, DWORD size
, PDWORD required
)
1650 struct inf_file
*file
;
1652 struct field
*field
;
1658 INFCONTEXT new_context
;
1659 if (!SetupFindFirstLineW( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1660 file
= new_context
.CurrentInf
;
1661 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1665 file
= context
->CurrentInf
;
1666 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1668 SetLastError( ERROR_LINE_NOT_FOUND
);
1673 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1674 total
+= PARSER_string_substW( file
, field
->text
, NULL
, 0 ) + 1;
1676 if (required
) *required
= total
;
1681 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1684 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1686 unsigned int len
= PARSER_string_substW( file
, field
->text
, buffer
, size
);
1687 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1695 /***********************************************************************
1696 * SetupGetLineTextA (SETUPAPI.@)
1698 BOOL WINAPI
SetupGetLineTextA( PINFCONTEXT context
, HINF hinf
, PCSTR section_name
,
1699 PCSTR key_name
, PSTR buffer
, DWORD size
, PDWORD required
)
1701 struct inf_file
*file
;
1703 struct field
*field
;
1709 INFCONTEXT new_context
;
1710 if (!SetupFindFirstLineA( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1711 file
= new_context
.CurrentInf
;
1712 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1716 file
= context
->CurrentInf
;
1717 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1719 SetLastError( ERROR_LINE_NOT_FOUND
);
1724 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1725 total
+= PARSER_string_substA( file
, field
->text
, NULL
, 0 ) + 1;
1727 if (required
) *required
= total
;
1732 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1735 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1737 unsigned int len
= PARSER_string_substA( file
, field
->text
, buffer
, size
);
1738 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1746 /***********************************************************************
1747 * SetupGetFieldCount (SETUPAPI.@)
1749 DWORD WINAPI
SetupGetFieldCount( PINFCONTEXT context
)
1751 struct inf_file
*file
= context
->CurrentInf
;
1752 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1754 if (!line
) return 0;
1755 return line
->nb_fields
;
1759 /***********************************************************************
1760 * SetupGetStringFieldA (SETUPAPI.@)
1762 BOOL WINAPI
SetupGetStringFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1763 DWORD size
, PDWORD required
)
1765 struct inf_file
*file
= context
->CurrentInf
;
1766 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1770 if (!field
) { SetLastError(ERROR_INVALID_PARAMETER
); return FALSE
; }
1771 len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 );
1772 if (required
) *required
= len
+ 1;
1777 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1780 PARSER_string_substA( file
, field
->text
, buffer
, size
);
1782 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1783 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1784 index
, debugstr_a(buffer
) );
1790 /***********************************************************************
1791 * SetupGetStringFieldW (SETUPAPI.@)
1793 BOOL WINAPI
SetupGetStringFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1794 DWORD size
, PDWORD required
)
1796 struct inf_file
*file
= context
->CurrentInf
;
1797 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1801 if (!field
) { SetLastError(ERROR_INVALID_PARAMETER
); return FALSE
; }
1802 len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 );
1803 if (required
) *required
= len
+ 1;
1808 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1811 PARSER_string_substW( file
, field
->text
, buffer
, size
);
1813 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1814 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1815 index
, debugstr_w(buffer
) );
1821 /***********************************************************************
1822 * SetupGetIntField (SETUPAPI.@)
1824 BOOL WINAPI
SetupGetIntField( PINFCONTEXT context
, DWORD index
, PINT result
)
1827 char *end
, *buffer
= localbuff
;
1832 if (!SetupGetStringFieldA( context
, index
, localbuff
, sizeof(localbuff
), &required
))
1834 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
1835 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
))) return FALSE
;
1836 if (!SetupGetStringFieldA( context
, index
, buffer
, required
, NULL
)) goto done
;
1838 res
= strtol( buffer
, &end
, 0 );
1839 if (end
!= buffer
&& !*end
)
1844 else SetLastError( ERROR_INVALID_DATA
);
1847 if (buffer
!= localbuff
) HeapFree( GetProcessHeap(), 0, buffer
);
1852 /***********************************************************************
1853 * SetupGetBinaryField (SETUPAPI.@)
1855 BOOL WINAPI
SetupGetBinaryField( PINFCONTEXT context
, DWORD index
, BYTE
*buffer
,
1856 DWORD size
, LPDWORD required
)
1858 struct inf_file
*file
= context
->CurrentInf
;
1859 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1860 struct field
*field
;
1865 SetLastError( ERROR_LINE_NOT_FOUND
);
1868 if (!index
|| index
> line
->nb_fields
)
1870 SetLastError( ERROR_INVALID_PARAMETER
);
1873 index
--; /* fields start at 0 */
1874 if (required
) *required
= line
->nb_fields
- index
;
1875 if (!buffer
) return TRUE
;
1876 if (size
< line
->nb_fields
- index
)
1878 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1881 field
= &file
->fields
[line
->first_field
+ index
];
1882 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1886 for (p
= field
->text
; *p
&& isxdigitW(*p
); p
++)
1888 if ((value
<<= 4) > 255)
1890 SetLastError( ERROR_INVALID_DATA
);
1893 if (*p
<= '9') value
|= (*p
- '0');
1894 else value
|= (tolowerW(*p
) - 'a' + 10);
1896 buffer
[i
- index
] = value
;
1898 if (TRACE_ON(setupapi
))
1900 TRACE( "%p/%p/%d/%d index %d returning",
1901 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
, index
);
1902 for (i
= index
; i
< line
->nb_fields
; i
++) TRACE( " %02x", buffer
[i
- index
] );
1909 /***********************************************************************
1910 * SetupGetMultiSzFieldA (SETUPAPI.@)
1912 BOOL WINAPI
SetupGetMultiSzFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1913 DWORD size
, LPDWORD required
)
1915 struct inf_file
*file
= context
->CurrentInf
;
1916 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1917 struct field
*field
;
1924 SetLastError( ERROR_LINE_NOT_FOUND
);
1927 if (!index
|| index
> line
->nb_fields
)
1929 SetLastError( ERROR_INVALID_PARAMETER
);
1932 index
--; /* fields start at 0 */
1933 field
= &file
->fields
[line
->first_field
+ index
];
1934 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1936 if (!(len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 ))) break;
1940 if (required
) *required
= total
;
1941 if (!buffer
) return TRUE
;
1944 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1947 field
= &file
->fields
[line
->first_field
+ index
];
1948 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1950 if (!(len
= PARSER_string_substA( file
, field
->text
, buffer
, size
))) break;
1953 *buffer
= 0; /* add final null */
1958 /***********************************************************************
1959 * SetupGetMultiSzFieldW (SETUPAPI.@)
1961 BOOL WINAPI
SetupGetMultiSzFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1962 DWORD size
, LPDWORD required
)
1964 struct inf_file
*file
= context
->CurrentInf
;
1965 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1966 struct field
*field
;
1973 SetLastError( ERROR_LINE_NOT_FOUND
);
1976 if (!index
|| index
> line
->nb_fields
)
1978 SetLastError( ERROR_INVALID_PARAMETER
);
1981 index
--; /* fields start at 0 */
1982 field
= &file
->fields
[line
->first_field
+ index
];
1983 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1985 if (!(len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 ))) break;
1989 if (required
) *required
= total
;
1990 if (!buffer
) return TRUE
;
1993 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1996 field
= &file
->fields
[line
->first_field
+ index
];
1997 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1999 if (!(len
= PARSER_string_substW( file
, field
->text
, buffer
, size
))) break;
2002 *buffer
= 0; /* add final null */
2006 /***********************************************************************
2007 * pSetupGetField (SETUPAPI.@)
2009 LPCWSTR WINAPI
pSetupGetField( PINFCONTEXT context
, DWORD index
)
2011 struct inf_file
*file
= context
->CurrentInf
;
2012 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
2016 SetLastError( ERROR_INVALID_PARAMETER
);
2022 /***********************************************************************
2023 * SetupGetInfFileListW (SETUPAPI.@)
2026 SetupGetInfFileListW(
2027 IN PCWSTR DirectoryPath OPTIONAL
,
2029 IN OUT PWSTR ReturnBuffer OPTIONAL
,
2030 IN DWORD ReturnBufferSize OPTIONAL
,
2031 OUT PDWORD RequiredSize OPTIONAL
)
2034 LPWSTR pFullFileName
= NULL
;
2035 LPWSTR pFileName
; /* Pointer into pFullFileName buffer */
2036 LPWSTR pBuffer
= ReturnBuffer
;
2037 WIN32_FIND_DATAW wfdFileInfo
;
2039 DWORD requiredSize
= 0;
2042 TRACE("%s %lx %p %ld %p\n", debugstr_w(DirectoryPath
), InfStyle
,
2043 ReturnBuffer
, ReturnBufferSize
, RequiredSize
);
2045 if (InfStyle
& ~(INF_STYLE_OLDNT
| INF_STYLE_WIN4
))
2047 TRACE("Unknown flags: 0x%08lx\n", InfStyle
& ~(INF_STYLE_OLDNT
| INF_STYLE_WIN4
));
2048 SetLastError(ERROR_INVALID_PARAMETER
);
2051 else if (ReturnBufferSize
== 0 && ReturnBuffer
!= NULL
)
2053 SetLastError(ERROR_INVALID_PARAMETER
);
2056 else if (ReturnBufferSize
> 0 && ReturnBuffer
== NULL
)
2058 SetLastError(ERROR_INVALID_PARAMETER
);
2062 /* Allocate memory for file filter */
2063 if (DirectoryPath
!= NULL
)
2064 /* "DirectoryPath\" form */
2065 len
= strlenW(DirectoryPath
) + 1 + 1;
2067 /* "%SYSTEMROOT%\Inf\" form */
2068 len
= MAX_PATH
+ 1 + strlenW(InfDirectory
) + 1;
2069 len
+= MAX_PATH
; /* To contain file name or "*.inf" string */
2070 pFullFileName
= MyMalloc(len
* sizeof(WCHAR
));
2071 if (pFullFileName
== NULL
)
2073 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2077 /* Fill file filter buffer */
2080 strcpyW(pFullFileName
, DirectoryPath
);
2081 if (*pFullFileName
&& pFullFileName
[strlenW(pFullFileName
) - 1] != '\\')
2082 strcatW(pFullFileName
, BackSlash
);
2086 len
= GetSystemWindowsDirectoryW(pFullFileName
, MAX_PATH
);
2087 if (len
== 0 || len
> MAX_PATH
)
2089 if (pFullFileName
[strlenW(pFullFileName
) - 1] != '\\')
2090 strcatW(pFullFileName
, BackSlash
);
2091 strcatW(pFullFileName
, InfDirectory
);
2093 pFileName
= &pFullFileName
[strlenW(pFullFileName
)];
2095 /* Search for the first file */
2096 strcpyW(pFileName
, InfFileSpecification
);
2097 hSearch
= FindFirstFileW(pFullFileName
, &wfdFileInfo
);
2098 if (hSearch
== INVALID_HANDLE_VALUE
)
2100 TRACE("No file returned by %s\n", debugstr_w(pFullFileName
));
2108 strcpyW(pFileName
, wfdFileInfo
.cFileName
);
2109 hInf
= SetupOpenInfFileW(
2111 NULL
, /* Inf class */
2113 NULL
/* Error line */);
2114 if (hInf
== INVALID_HANDLE_VALUE
)
2116 if (GetLastError() == ERROR_CLASS_MISMATCH
)
2118 /* InfStyle was not correct. Skip this file */
2121 TRACE("Invalid .inf file %s\n", debugstr_w(pFullFileName
));
2125 len
= strlenW(wfdFileInfo
.cFileName
) + 1;
2126 requiredSize
+= (DWORD
)(len
* sizeof(WCHAR
));
2127 if (requiredSize
<= ReturnBufferSize
)
2129 strcpyW(pBuffer
, wfdFileInfo
.cFileName
);
2130 pBuffer
= &pBuffer
[len
];
2132 SetupCloseInfFile(hInf
);
2133 } while (FindNextFileW(hSearch
, &wfdFileInfo
));
2136 requiredSize
+= sizeof(WCHAR
); /* Final NULL char */
2137 if (requiredSize
<= ReturnBufferSize
)
2144 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2148 *RequiredSize
= requiredSize
;
2151 MyFree(pFullFileName
);
2155 /***********************************************************************
2156 * SetupGetInfFileListA (SETUPAPI.@)
2159 SetupGetInfFileListA(
2160 IN PCSTR DirectoryPath OPTIONAL
,
2162 IN OUT PSTR ReturnBuffer OPTIONAL
,
2163 IN DWORD ReturnBufferSize OPTIONAL
,
2164 OUT PDWORD RequiredSize OPTIONAL
)
2166 PWSTR DirectoryPathW
= NULL
;
2167 PWSTR ReturnBufferW
= NULL
;
2170 TRACE("%s %lx %p %ld %p\n", debugstr_a(DirectoryPath
), InfStyle
,
2171 ReturnBuffer
, ReturnBufferSize
, RequiredSize
);
2173 if (DirectoryPath
!= NULL
)
2175 DirectoryPathW
= MultiByteToUnicode(DirectoryPath
, CP_ACP
);
2176 if (DirectoryPathW
== NULL
) goto Cleanup
;
2179 if (ReturnBuffer
!= NULL
&& ReturnBufferSize
!= 0)
2181 ReturnBufferW
= MyMalloc(ReturnBufferSize
* sizeof(WCHAR
));
2182 if (ReturnBufferW
== NULL
) goto Cleanup
;
2185 ret
= SetupGetInfFileListW(DirectoryPathW
, InfStyle
, ReturnBufferW
, ReturnBufferSize
, RequiredSize
);
2187 if (ret
&& ReturnBufferW
!= NULL
)
2189 ret
= WideCharToMultiByte(CP_ACP
, 0, ReturnBufferW
, -1, ReturnBuffer
, ReturnBufferSize
, NULL
, NULL
) != 0;
2193 MyFree(DirectoryPathW
);
2194 MyFree(ReturnBufferW
);
2199 /***********************************************************************
2200 * SetupDiGetINFClassW (SETUPAPI.@)
2203 SetupDiGetINFClassW(
2205 OUT LPGUID ClassGuid
,
2206 OUT PWSTR ClassName
,
2207 IN DWORD ClassNameSize
,
2208 OUT PDWORD RequiredSize OPTIONAL
)
2210 HINF hInf
= INVALID_HANDLE_VALUE
;
2213 TRACE("%s %p %p %ld %p\n", debugstr_w(InfName
), ClassGuid
,
2214 ClassName
, ClassNameSize
, RequiredSize
);
2216 /* Open .inf file */
2217 hInf
= SetupOpenInfFileW(InfName
, NULL
, INF_STYLE_WIN4
, NULL
);
2218 if (hInf
== INVALID_HANDLE_VALUE
)
2221 ret
= PARSER_GetInfClassW(hInf
, ClassGuid
, ClassName
, ClassNameSize
, RequiredSize
);
2224 if (hInf
!= INVALID_HANDLE_VALUE
)
2225 SetupCloseInfFile(hInf
);
2227 TRACE("Returning %d\n", ret
);
2231 /***********************************************************************
2232 * SetupDiGetINFClassA (SETUPAPI.@)
2234 BOOL WINAPI
SetupDiGetINFClassA(
2236 OUT LPGUID ClassGuid
,
2238 IN DWORD ClassNameSize
,
2239 OUT PDWORD RequiredSize OPTIONAL
)
2241 PWSTR InfNameW
= NULL
;
2242 PWSTR ClassNameW
= NULL
;
2245 TRACE("%s %p %p %ld %p\n", debugstr_a(InfName
), ClassGuid
,
2246 ClassName
, ClassNameSize
, RequiredSize
);
2248 if (InfName
!= NULL
)
2250 InfNameW
= MultiByteToUnicode(InfName
, CP_ACP
);
2251 if (InfNameW
== NULL
) goto Cleanup
;
2254 if (ClassName
!= NULL
&& ClassNameSize
!= 0)
2256 ClassNameW
= MyMalloc(ClassNameSize
* sizeof(WCHAR
));
2257 if (ClassNameW
== NULL
) goto Cleanup
;
2260 ret
= SetupDiGetINFClassW(InfNameW
, ClassGuid
, ClassNameW
, ClassNameSize
, RequiredSize
);
2262 if (ret
&& ClassNameW
!= NULL
)
2264 ret
= WideCharToMultiByte(CP_ACP
, 0, ClassNameW
, -1, ClassName
, ClassNameSize
, NULL
, NULL
) != 0;
2274 BOOL
EnumerateSectionsStartingWith(
2277 IN FIND_CALLBACK Callback
,
2280 struct inf_file
*file
= (struct inf_file
*)hInf
;
2281 size_t len
= strlenW(pStr
);
2284 for (i
= 0; i
< file
->nb_sections
; i
++)
2285 if (strncmpiW(pStr
, file
->sections
[i
]->name
, len
) == 0)
2287 if (!Callback(file
->sections
[i
]->name
, Context
))