4 * Copyright 2002 Alexandre Julliard for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
45 #define CONTROL_Z '\x1a'
46 #define MAX_SECTION_NAME_LEN 255
47 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
48 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
49 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
51 /* inf file structure definitions */
55 const WCHAR *text; /* field text */
60 int first_field; /* index of first field in field array */
61 int nb_fields; /* number of fields in line */
62 int key_field; /* index of field for key or -1 if no key */
67 const WCHAR *name; /* section name */
68 unsigned int nb_lines; /* number of used lines */
69 unsigned int alloc_lines; /* total number of allocated lines in array below */
70 struct line lines[16]; /* lines information (grown dynamically, 16 is initial size) */
75 struct inf_file *next; /* next appended file */
76 WCHAR *strings; /* buffer for string data (section names and field values) */
77 WCHAR *string_pos; /* position of next available string in buffer */
78 unsigned int nb_sections; /* number of used sections */
79 unsigned int alloc_sections; /* total number of allocated section pointers */
80 struct section **sections; /* section pointers array */
81 unsigned int nb_fields;
82 unsigned int alloc_fields;
84 int strings_section; /* index of [Strings] section or -1 if none */
85 WCHAR *src_root; /* source root directory */
88 /* parser definitions */
92 LINE_START, /* at beginning of a line */
93 SECTION_NAME, /* parsing a section name */
94 KEY_NAME, /* parsing a key name */
95 VALUE_NAME, /* parsing a value name */
96 EOL_BACKSLASH, /* backslash at end of line */
97 QUOTES, /* inside quotes */
98 LEADING_SPACES, /* leading spaces */
99 TRAILING_SPACES, /* trailing spaces */
100 COMMENT, /* inside a comment */
106 const WCHAR *start; /* start position of item being parsed */
107 const WCHAR *end; /* end of buffer */
108 struct inf_file *file; /* file being built */
109 enum parser_state state; /* current parser state */
110 enum parser_state stack[4]; /* state stack */
111 int stack_pos; /* current pos in stack */
113 int cur_section; /* index of section being parsed*/
114 struct line *line; /* current line */
115 unsigned int line_pos; /* current line position in file */
116 unsigned int error; /* error code */
117 unsigned int token_len; /* current token len */
118 WCHAR token[MAX_FIELD_LEN+1]; /* current token */
121 typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos );
123 /* parser state machine functions */
124 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos );
125 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos );
126 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos );
127 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos );
128 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos );
129 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos );
130 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos );
131 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos );
132 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos );
134 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
136 line_start_state, /* LINE_START */
137 section_name_state, /* SECTION_NAME */
138 key_name_state, /* KEY_NAME */
139 value_name_state, /* VALUE_NAME */
140 eol_backslash_state, /* EOL_BACKSLASH */
141 quotes_state, /* QUOTES */
142 leading_spaces_state, /* LEADING_SPACES */
143 trailing_spaces_state, /* TRAILING_SPACES */
144 comment_state /* COMMENT */
148 /* Unicode string constants */
149 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
150 static const WCHAR Signature[] = {'S','i','g','n','a','t','u','r','e',0};
151 static const WCHAR Chicago[] = {'$','C','h','i','c','a','g','o','$',0};
152 static const WCHAR WindowsNT[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
153 static const WCHAR Windows95[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
154 static const WCHAR LayoutFile[] = {'L','a','y','o','u','t','F','i','l','e',0};
156 /* extend an array, allocating more memory if necessary */
157 static void *grow_array( void *array, unsigned int *count, size_t elem )
160 unsigned int new_count = *count + *count / 2;
161 if (new_count < 32) new_count = 32;
162 if ((new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem )))
165 HeapFree( GetProcessHeap(), 0, array );
170 /* find a section by name */
171 static int find_section( struct inf_file *file, const WCHAR *name )
175 for (i = 0; i < file->nb_sections; i++)
176 if (!strcmpiW( name, file->sections[i]->name )) return i;
181 /* find a line by name */
182 static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name )
184 struct section *section;
188 if (section_index < 0 || section_index >= file->nb_sections) return NULL;
189 section = file->sections[section_index];
190 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
192 if (line->key_field == -1) continue;
193 if (!strcmpiW( name, file->fields[line->key_field].text )) return line;
199 /* add a section to the file and return the section index */
200 static int add_section( struct inf_file *file, const WCHAR *name )
202 struct section *section;
204 if (file->nb_sections >= file->alloc_sections)
206 if (!(file->sections = grow_array( file->sections, &file->alloc_sections,
207 sizeof(file->sections[0]) ))) return -1;
209 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1;
210 section->name = name;
211 section->nb_lines = 0;
212 section->alloc_lines = sizeof(section->lines)/sizeof(section->lines[0]);
213 file->sections[file->nb_sections] = section;
214 return file->nb_sections++;
218 /* add a line to a given section */
219 static struct line *add_line( struct inf_file *file, int section_index )
221 struct section *section;
224 assert( section_index >= 0 && section_index < file->nb_sections );
226 section = file->sections[section_index];
227 if (section->nb_lines == section->alloc_lines) /* need to grow the section */
229 int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line);
230 if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL;
231 section->alloc_lines *= 2;
232 file->sections[section_index] = section;
234 line = §ion->lines[section->nb_lines++];
235 line->first_field = file->nb_fields;
237 line->key_field = -1;
242 /* retrieve a given line from section/line index */
243 inline static struct line *get_line( struct inf_file *file, unsigned int section_index,
244 unsigned int line_index )
246 struct section *section;
248 if (section_index >= file->nb_sections) return NULL;
249 section = file->sections[section_index];
250 if (line_index >= section->nb_lines) return NULL;
251 return §ion->lines[line_index];
255 /* retrieve a given field from section/line/field index */
256 static struct field *get_field( struct inf_file *file, int section_index, int line_index,
259 struct line *line = get_line( file, section_index, line_index );
261 if (!line) return NULL;
262 if (!field_index) /* get the key */
264 if (line->key_field == -1) return NULL;
265 return &file->fields[line->key_field];
268 if (field_index >= line->nb_fields) return NULL;
269 return &file->fields[line->first_field + field_index];
273 /* allocate a new field, growing the array if necessary */
274 static struct field *add_field( struct inf_file *file, const WCHAR *text )
278 if (file->nb_fields >= file->alloc_fields)
280 if (!(file->fields = grow_array( file->fields, &file->alloc_fields,
281 sizeof(file->fields[0]) ))) return NULL;
283 field = &file->fields[file->nb_fields++];
289 /* retrieve the string substitution for a directory id */
290 static const WCHAR *get_dirid_subst( int dirid, unsigned int *len )
292 extern const WCHAR *DIRID_get_string( HINF hinf, int dirid );
293 const WCHAR *ret = DIRID_get_string( 0, dirid );
294 if (ret) *len = strlenW(ret);
299 /* retrieve the string substitution for a given string, or NULL if not found */
300 /* if found, len is set to the substitution length */
301 static const WCHAR *get_string_subst( struct inf_file *file, const WCHAR *str, unsigned int *len )
303 static const WCHAR percent = '%';
305 struct section *strings_section;
309 WCHAR *dirid_str, *end;
310 const WCHAR *ret = NULL;
312 if (!*len) /* empty string (%%) is replaced by single percent */
317 if (file->strings_section == -1) goto not_found;
318 strings_section = file->sections[file->strings_section];
319 for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++)
321 if (line->key_field == -1) continue;
322 if (strncmpiW( str, file->fields[line->key_field].text, *len )) continue;
323 if (!file->fields[line->key_field].text[*len]) break;
325 if (i == strings_section->nb_lines || !line->nb_fields) goto not_found;
326 field = &file->fields[line->first_field];
327 *len = strlenW( field->text );
330 not_found: /* check for integer id */
331 if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) )))
333 memcpy( dirid_str, str, *len * sizeof(WCHAR) );
335 dirid = strtolW( dirid_str, &end, 10 );
336 if (!*end) ret = get_dirid_subst( dirid, len );
337 HeapFree( GetProcessHeap(), 0, dirid_str );
344 /* do string substitutions on the specified text */
345 /* the buffer is assumed to be large enough */
346 /* returns necessary length not including terminating null */
347 unsigned int PARSER_string_substW( struct inf_file *file, const WCHAR *text, WCHAR *buffer,
350 const WCHAR *start, *subst, *p;
351 unsigned int len, total = 0;
354 if (!buffer) size = MAX_STRING_LEN + 1;
355 for (p = start = text; *p; p++)
357 if (*p != '%') continue;
359 if (inside) /* start of a %xx% string */
362 if (len > size - 1) len = size - 1;
363 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
368 else /* end of the %xx% string, find substitution */
371 subst = get_string_subst( file, start + 1, &len );
377 if (len > size - 1) len = size - 1;
378 if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
385 if (start != p) /* unfinished string, copy it */
388 if (len > size - 1) len = size - 1;
389 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
392 if (buffer && size) buffer[total] = 0;
397 /* do string substitutions on the specified text */
398 /* the buffer is assumed to be large enough */
399 /* returns necessary length not including terminating null */
400 unsigned int PARSER_string_substA( struct inf_file *file, const WCHAR *text, char *buffer,
403 WCHAR buffW[MAX_STRING_LEN+1];
406 unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) );
407 if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
410 RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
417 /* push some string data into the strings buffer */
418 static WCHAR *push_string( struct inf_file *file, const WCHAR *string )
420 WCHAR *ret = file->string_pos;
421 strcpyW( ret, string );
422 file->string_pos += strlenW( ret ) + 1;
427 /* push the current state on the parser stack */
428 inline static void push_state( struct parser *parser, enum parser_state state )
430 assert( parser->stack_pos < sizeof(parser->stack)/sizeof(parser->stack[0]) );
431 parser->stack[parser->stack_pos++] = state;
435 /* pop the current state */
436 inline static void pop_state( struct parser *parser )
438 assert( parser->stack_pos );
439 parser->state = parser->stack[--parser->stack_pos];
443 /* set the parser state and return the previous one */
444 inline static enum parser_state set_state( struct parser *parser, enum parser_state state )
446 enum parser_state ret = parser->state;
447 parser->state = state;
452 /* check if the pointer points to an end of file */
453 inline static int is_eof( struct parser *parser, const WCHAR *ptr )
455 return (ptr >= parser->end || *ptr == CONTROL_Z);
459 /* check if the pointer points to an end of line */
460 inline static int is_eol( struct parser *parser, const WCHAR *ptr )
462 return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n');
466 /* push data from current token start up to pos into the current token */
467 static int push_token( struct parser *parser, const WCHAR *pos )
469 int len = pos - parser->start;
470 const WCHAR *src = parser->start;
471 WCHAR *dst = parser->token + parser->token_len;
473 if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len;
475 parser->token_len += len;
476 for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' ';
483 /* add a section with the current token as name */
484 static int add_section_from_token( struct parser *parser )
488 if (parser->token_len > MAX_SECTION_NAME_LEN)
490 parser->error = ERROR_SECTION_NAME_TOO_LONG;
493 if ((section_index = find_section( parser->file, parser->token )) == -1)
495 /* need to create a new one */
496 const WCHAR *name = push_string( parser->file, parser->token );
497 if ((section_index = add_section( parser->file, name )) == -1)
499 parser->error = ERROR_NOT_ENOUGH_MEMORY;
503 parser->token_len = 0;
504 parser->cur_section = section_index;
505 return section_index;
509 /* add a field containing the current token to the current line */
510 static struct field *add_field_from_token( struct parser *parser, int is_key )
515 if (!parser->line) /* need to start a new line */
517 if (parser->cur_section == -1) /* got a line before the first section */
519 parser->error = ERROR_WRONG_INF_STYLE;
522 if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error;
524 else assert(!is_key);
526 text = push_string( parser->file, parser->token );
527 if ((field = add_field( parser->file, text )))
529 if (!is_key) parser->line->nb_fields++;
532 /* replace first field by key field */
533 parser->line->key_field = parser->line->first_field;
534 parser->line->first_field++;
536 parser->token_len = 0;
540 parser->error = ERROR_NOT_ENOUGH_MEMORY;
545 /* close the current line and prepare for parsing a new one */
546 static void close_current_line( struct parser *parser )
548 struct line *cur_line = parser->line;
552 /* if line has a single field and no key, the field is the key too */
553 if (cur_line->nb_fields == 1 && cur_line->key_field == -1)
554 cur_line->key_field = cur_line->first_field;
560 /* handler for parser LINE_START state */
561 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
565 for (p = pos; !is_eof( parser, p ); p++)
571 close_current_line( parser );
574 push_state( parser, LINE_START );
575 set_state( parser, COMMENT );
578 parser->start = p + 1;
579 set_state( parser, SECTION_NAME );
585 set_state( parser, KEY_NAME );
591 close_current_line( parser );
596 /* handler for parser SECTION_NAME state */
597 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
601 for (p = pos; !is_eol( parser, p ); p++)
605 push_token( parser, p );
606 if (add_section_from_token( parser ) == -1) return NULL;
607 push_state( parser, LINE_START );
608 set_state( parser, COMMENT ); /* ignore everything else on the line */
612 parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */
617 /* handler for parser KEY_NAME state */
618 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos )
620 const WCHAR *p, *token_end = parser->start;
622 for (p = pos; !is_eol( parser, p ); p++)
624 if (*p == ',') break;
629 push_token( parser, token_end );
630 if (!add_field_from_token( parser, 1 )) return NULL;
631 parser->start = p + 1;
632 push_state( parser, VALUE_NAME );
633 set_state( parser, LEADING_SPACES );
636 push_token( parser, token_end );
637 if (!add_field_from_token( parser, 0 )) return NULL;
638 push_state( parser, LINE_START );
639 set_state( parser, COMMENT );
642 push_token( parser, token_end );
643 parser->start = p + 1;
644 push_state( parser, KEY_NAME );
645 set_state( parser, QUOTES );
648 push_token( parser, token_end );
650 push_state( parser, KEY_NAME );
651 set_state( parser, EOL_BACKSLASH );
654 if (!isspaceW(*p)) token_end = p + 1;
657 push_token( parser, p );
658 push_state( parser, KEY_NAME );
659 set_state( parser, TRAILING_SPACES );
665 push_token( parser, token_end );
666 set_state( parser, VALUE_NAME );
671 /* handler for parser VALUE_NAME state */
672 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos )
674 const WCHAR *p, *token_end = parser->start;
676 for (p = pos; !is_eol( parser, p ); p++)
681 push_token( parser, token_end );
682 if (!add_field_from_token( parser, 0 )) return NULL;
683 push_state( parser, LINE_START );
684 set_state( parser, COMMENT );
687 push_token( parser, token_end );
688 if (!add_field_from_token( parser, 0 )) return NULL;
689 parser->start = p + 1;
690 push_state( parser, VALUE_NAME );
691 set_state( parser, LEADING_SPACES );
694 push_token( parser, token_end );
695 parser->start = p + 1;
696 push_state( parser, VALUE_NAME );
697 set_state( parser, QUOTES );
700 push_token( parser, token_end );
702 push_state( parser, VALUE_NAME );
703 set_state( parser, EOL_BACKSLASH );
706 if (!isspaceW(*p)) token_end = p + 1;
709 push_token( parser, p );
710 push_state( parser, VALUE_NAME );
711 set_state( parser, TRAILING_SPACES );
717 push_token( parser, token_end );
718 if (!add_field_from_token( parser, 0 )) return NULL;
719 set_state( parser, LINE_START );
724 /* handler for parser EOL_BACKSLASH state */
725 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos )
729 for (p = pos; !is_eof( parser, p ); p++)
735 parser->start = p + 1;
736 set_state( parser, LEADING_SPACES );
741 push_state( parser, EOL_BACKSLASH );
742 set_state( parser, COMMENT );
745 if (isspaceW(*p)) continue;
746 push_token( parser, p );
757 /* handler for parser QUOTES state */
758 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos )
760 const WCHAR *p, *token_end = parser->start;
762 for (p = pos; !is_eol( parser, p ); p++)
766 if (p+1 < parser->end && p[1] == '"') /* double quotes */
768 push_token( parser, p + 1 );
769 parser->start = token_end = p + 2;
772 else /* end of quotes */
774 push_token( parser, p );
775 parser->start = p + 1;
781 push_token( parser, p );
787 /* handler for parser LEADING_SPACES state */
788 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos )
792 for (p = pos; !is_eol( parser, p ); p++)
797 set_state( parser, EOL_BACKSLASH );
800 if (!isspaceW(*p)) break;
808 /* handler for parser TRAILING_SPACES state */
809 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos )
813 for (p = pos; !is_eol( parser, p ); p++)
817 set_state( parser, EOL_BACKSLASH );
820 if (!isspaceW(*p)) break;
827 /* handler for parser COMMENT state */
828 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos )
830 const WCHAR *p = pos;
832 while (!is_eol( parser, p )) p++;
838 /* parse a complete buffer */
839 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
842 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
844 struct parser parser;
845 const WCHAR *pos = buffer;
847 parser.start = buffer;
851 parser.state = LINE_START;
852 parser.stack_pos = 0;
853 parser.cur_section = -1;
856 parser.token_len = 0;
858 /* parser main loop */
859 while (pos) pos = (parser_funcs[parser.state])( &parser, pos );
861 /* trim excess buffer space */
862 if (file->alloc_sections > file->nb_sections)
864 file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections,
865 file->nb_sections * sizeof(file->sections[0]) );
866 file->alloc_sections = file->nb_sections;
868 if (file->alloc_fields > file->nb_fields)
870 file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields,
871 file->nb_fields * sizeof(file->fields[0]) );
872 file->alloc_fields = file->nb_fields;
874 file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings,
875 (file->string_pos - file->strings) * sizeof(WCHAR) );
879 if (error_line) *error_line = parser.line_pos;
883 /* find the [strings] section */
884 file->strings_section = find_section( file, Strings );
889 /* append a child INF file to its parent list, in a thread-safe manner */
890 static void append_inf_file( struct inf_file *parent, struct inf_file *child )
892 struct inf_file **ppnext = &parent->next;
897 struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
899 ppnext = &next->next;
904 /***********************************************************************
909 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, UINT *error_line )
913 struct inf_file *file;
915 DWORD size = GetFileSize( handle, NULL );
916 HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL );
917 if (!mapping) return NULL;
918 buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size );
920 if (!buffer) return NULL;
922 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
924 if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) )))
926 err = ERROR_NOT_ENOUGH_MEMORY;
930 /* we won't need more strings space than the size of the file,
931 * so we can preallocate it here
933 if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
935 err = ERROR_NOT_ENOUGH_MEMORY;
938 file->string_pos = file->strings;
939 file->strings_section = -1;
941 if (!RtlIsTextUnicode( buffer, size, NULL ))
943 WCHAR *new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
946 DWORD len = MultiByteToWideChar( CP_ACP, 0, buffer, size, new_buff,
947 size * sizeof(WCHAR) );
948 err = parse_buffer( file, new_buff, new_buff + len, error_line );
949 HeapFree( GetProcessHeap(), 0, new_buff );
952 else err = parse_buffer( file, buffer, (WCHAR *)((char *)buffer + size), error_line );
954 if (!err) /* now check signature */
956 int version_index = find_section( file, Version );
957 if (version_index != -1)
959 struct line *line = find_line( file, version_index, Signature );
960 if (line && line->nb_fields > 0)
962 struct field *field = file->fields + line->first_field;
963 if (!strcmpiW( field->text, Chicago )) goto done;
964 if (!strcmpiW( field->text, WindowsNT )) goto done;
965 if (!strcmpiW( field->text, Windows95 )) goto done;
968 err = ERROR_WRONG_INF_STYLE;
972 UnmapViewOfFile( buffer );
975 HeapFree( GetProcessHeap(), 0, file );
983 /***********************************************************************
984 * PARSER_get_src_root
986 * Retrieve the source directory of an inf file.
988 const WCHAR *PARSER_get_src_root( HINF hinf )
990 struct inf_file *file = hinf;
991 return file->src_root;
995 /***********************************************************************
996 * SetupOpenInfFileA (SETUPAPI.@)
998 HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error )
1000 UNICODE_STRING nameW, classW;
1001 HINF ret = (HINF)INVALID_HANDLE_VALUE;
1003 classW.Buffer = NULL;
1004 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class ))
1006 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1009 if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
1011 ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
1012 RtlFreeUnicodeString( &nameW );
1014 RtlFreeUnicodeString( &classW );
1019 /***********************************************************************
1020 * SetupOpenInfFileW (SETUPAPI.@)
1022 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1024 struct inf_file *file = NULL;
1029 if (strchrW( name, '\\' ) || strchrW( name, '/' ))
1031 if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return (HINF)INVALID_HANDLE_VALUE;
1032 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1034 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1035 return (HINF)INVALID_HANDLE_VALUE;
1037 GetFullPathNameW( name, len, path, NULL );
1038 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1040 else /* try Windows directory */
1042 static const WCHAR Inf[] = {'\\','i','n','f','\\',0};
1043 static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1045 len = GetWindowsDirectoryW( NULL, 0 ) + strlenW(name) + 12;
1046 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1048 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1049 return (HINF)INVALID_HANDLE_VALUE;
1051 GetWindowsDirectoryW( path, len );
1052 p = path + strlenW(path);
1055 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1056 if (handle == INVALID_HANDLE_VALUE)
1058 strcpyW( p, System32 );
1060 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1064 if (handle != INVALID_HANDLE_VALUE)
1066 file = parse_file( handle, class, error );
1067 CloseHandle( handle );
1071 HeapFree( GetProcessHeap(), 0, path );
1072 return (HINF)INVALID_HANDLE_VALUE;
1074 TRACE( "%s -> %p\n", debugstr_w(path), file );
1075 file->src_root = path;
1076 if ((p = strrchrW( path, '\\' ))) p[1] = 0; /* remove file name */
1082 /***********************************************************************
1083 * SetupOpenAppendInfFileA (SETUPAPI.@)
1085 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
1089 if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error );
1090 child_hinf = SetupOpenInfFileA( name, NULL, INF_STYLE_WIN4, error );
1091 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1092 append_inf_file( parent_hinf, child_hinf );
1093 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_a(name), child_hinf );
1098 /***********************************************************************
1099 * SetupOpenAppendInfFileW (SETUPAPI.@)
1101 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1108 WCHAR filename[MAX_PATH];
1111 if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE;
1112 while (SetupGetStringFieldW( &context, idx++, filename,
1113 sizeof(filename)/sizeof(WCHAR), NULL ))
1115 child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error );
1116 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1117 append_inf_file( parent_hinf, child_hinf );
1118 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf );
1122 child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error );
1123 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1124 append_inf_file( parent_hinf, child_hinf );
1125 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf );
1130 /***********************************************************************
1131 * SetupCloseInfFile (SETUPAPI.@)
1133 void WINAPI SetupCloseInfFile( HINF hinf )
1135 struct inf_file *file = hinf;
1138 for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
1139 HeapFree( GetProcessHeap(), 0, file->src_root );
1140 HeapFree( GetProcessHeap(), 0, file->sections );
1141 HeapFree( GetProcessHeap(), 0, file->fields );
1142 HeapFree( GetProcessHeap(), 0, file->strings );
1143 HeapFree( GetProcessHeap(), 0, file );
1147 /***********************************************************************
1148 * SetupGetLineCountA (SETUPAPI.@)
1150 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1152 UNICODE_STRING sectionW;
1155 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, name ))
1156 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1159 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1160 RtlFreeUnicodeString( §ionW );
1166 /***********************************************************************
1167 * SetupGetLineCountW (SETUPAPI.@)
1169 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1171 struct inf_file *file = hinf;
1175 for (file = hinf; file; file = file->next)
1177 if ((section_index = find_section( file, section )) == -1) continue;
1178 if (ret == -1) ret = 0;
1179 ret += file->sections[section_index]->nb_lines;
1181 TRACE( "(%p,%s) returning %ld\n", hinf, debugstr_w(section), ret );
1182 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1187 /***********************************************************************
1188 * SetupGetLineByIndexA (SETUPAPI.@)
1190 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1192 UNICODE_STRING sectionW;
1195 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
1196 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1199 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1200 RtlFreeUnicodeString( §ionW );
1206 /***********************************************************************
1207 * SetupGetLineByIndexW (SETUPAPI.@)
1209 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1211 struct inf_file *file = hinf;
1214 SetLastError( ERROR_SECTION_NOT_FOUND );
1215 for (file = hinf; file; file = file->next)
1217 if ((section_index = find_section( file, section )) == -1) continue;
1218 SetLastError( ERROR_LINE_NOT_FOUND );
1219 if (index < file->sections[section_index]->nb_lines)
1221 context->Inf = hinf;
1222 context->CurrentInf = file;
1223 context->Section = section_index;
1224 context->Line = index;
1226 TRACE( "(%p,%s): returning %d/%ld\n",
1227 hinf, debugstr_w(section), section_index, index );
1230 index -= file->sections[section_index]->nb_lines;
1232 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1237 /***********************************************************************
1238 * SetupFindFirstLineA (SETUPAPI.@)
1240 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1242 UNICODE_STRING sectionW, keyW;
1245 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
1247 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1251 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1254 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1256 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1257 RtlFreeUnicodeString( &keyW );
1259 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1261 RtlFreeUnicodeString( §ionW );
1266 /***********************************************************************
1267 * SetupFindFirstLineW (SETUPAPI.@)
1269 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1271 struct inf_file *file;
1274 SetLastError( ERROR_SECTION_NOT_FOUND );
1275 for (file = hinf; file; file = file->next)
1277 if ((section_index = find_section( file, section )) == -1) continue;
1282 ctx.CurrentInf = file;
1283 ctx.Section = section_index;
1285 return SetupFindNextMatchLineW( &ctx, key, context );
1287 SetLastError( ERROR_LINE_NOT_FOUND ); /* found at least one section */
1288 if (file->sections[section_index]->nb_lines)
1290 context->Inf = hinf;
1291 context->CurrentInf = file;
1292 context->Section = section_index;
1295 TRACE( "(%p,%s,%s): returning %d/0\n",
1296 hinf, debugstr_w(section), debugstr_w(key), section_index );
1300 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1305 /***********************************************************************
1306 * SetupFindNextLine (SETUPAPI.@)
1308 BOOL WINAPI SetupFindNextLine( const INFCONTEXT *context_in, INFCONTEXT *context_out )
1310 struct inf_file *file = context_in->CurrentInf;
1311 struct section *section;
1313 if (context_in->Section >= file->nb_sections) goto error;
1315 section = file->sections[context_in->Section];
1316 if (context_in->Line+1 < section->nb_lines)
1318 if (context_out != context_in) *context_out = *context_in;
1319 context_out->Line++;
1324 /* now search the appended files */
1326 for (file = file->next; file; file = file->next)
1328 int section_index = find_section( file, section->name );
1329 if (section_index == -1) continue;
1330 if (file->sections[section_index]->nb_lines)
1332 context_out->Inf = context_in->Inf;
1333 context_out->CurrentInf = file;
1334 context_out->Section = section_index;
1335 context_out->Line = 0;
1341 SetLastError( ERROR_LINE_NOT_FOUND );
1346 /***********************************************************************
1347 * SetupFindNextMatchLineA (SETUPAPI.@)
1349 BOOL WINAPI SetupFindNextMatchLineA( const INFCONTEXT *context_in, PCSTR key,
1350 INFCONTEXT *context_out )
1352 UNICODE_STRING keyW;
1355 if (!key) return SetupFindNextLine( context_in, context_out );
1357 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1358 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1361 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1362 RtlFreeUnicodeString( &keyW );
1368 /***********************************************************************
1369 * SetupFindNextMatchLineW (SETUPAPI.@)
1371 BOOL WINAPI SetupFindNextMatchLineW( const INFCONTEXT *context_in, PCWSTR key,
1372 INFCONTEXT *context_out )
1374 struct inf_file *file = context_in->CurrentInf;
1375 struct section *section;
1379 if (!key) return SetupFindNextLine( context_in, context_out );
1381 if (context_in->Section >= file->nb_sections) goto error;
1383 section = file->sections[context_in->Section];
1385 for (i = context_in->Line+1, line = §ion->lines[i]; i < section->nb_lines; i++, line++)
1387 if (line->key_field == -1) continue;
1388 if (!strcmpiW( key, file->fields[line->key_field].text ))
1390 if (context_out != context_in) *context_out = *context_in;
1391 context_out->Line = i;
1393 TRACE( "(%p,%s,%s): returning %d\n",
1394 file, debugstr_w(section->name), debugstr_w(key), i );
1399 /* now search the appended files */
1401 for (file = file->next; file; file = file->next)
1403 int section_index = find_section( file, section->name );
1404 if (section_index == -1) continue;
1405 section = file->sections[section_index];
1406 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1408 if (line->key_field == -1) continue;
1409 if (!strcmpiW( key, file->fields[line->key_field].text ))
1411 context_out->Inf = context_in->Inf;
1412 context_out->CurrentInf = file;
1413 context_out->Section = section_index;
1414 context_out->Line = i;
1416 TRACE( "(%p,%s,%s): returning %d/%d\n",
1417 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1422 TRACE( "(%p,%s,%s): not found\n",
1423 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1425 SetLastError( ERROR_LINE_NOT_FOUND );
1430 /***********************************************************************
1431 * SetupGetLineTextW (SETUPAPI.@)
1433 BOOL WINAPI SetupGetLineTextW( const INFCONTEXT *context, HINF hinf, PCWSTR section_name,
1434 PCWSTR key_name, PWSTR buffer, DWORD size, DWORD *required )
1436 struct inf_file *file;
1438 struct field *field;
1444 INFCONTEXT new_context;
1445 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1446 file = new_context.CurrentInf;
1447 line = get_line( file, new_context.Section, new_context.Line );
1451 file = context->CurrentInf;
1452 if (!(line = get_line( file, context->Section, context->Line )))
1454 SetLastError( ERROR_LINE_NOT_FOUND );
1459 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1460 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1462 if (required) *required = total;
1467 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1470 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1472 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1473 if (i+1 < line->nb_fields) buffer[len] = ',';
1481 /***********************************************************************
1482 * SetupGetLineTextA (SETUPAPI.@)
1484 BOOL WINAPI SetupGetLineTextA( const INFCONTEXT *context, HINF hinf, PCSTR section_name,
1485 PCSTR key_name, PSTR buffer, DWORD size, DWORD *required )
1487 struct inf_file *file;
1489 struct field *field;
1495 INFCONTEXT new_context;
1496 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1497 file = new_context.CurrentInf;
1498 line = get_line( file, new_context.Section, new_context.Line );
1502 file = context->CurrentInf;
1503 if (!(line = get_line( file, context->Section, context->Line )))
1505 SetLastError( ERROR_LINE_NOT_FOUND );
1510 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1511 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1513 if (required) *required = total;
1518 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1521 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1523 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1524 if (i+1 < line->nb_fields) buffer[len] = ',';
1532 /***********************************************************************
1533 * SetupGetFieldCount (SETUPAPI.@)
1535 DWORD WINAPI SetupGetFieldCount( const INFCONTEXT *context )
1537 struct inf_file *file = context->CurrentInf;
1538 struct line *line = get_line( file, context->Section, context->Line );
1540 if (!line) return 0;
1541 return line->nb_fields;
1545 /***********************************************************************
1546 * SetupGetStringFieldA (SETUPAPI.@)
1548 BOOL WINAPI SetupGetStringFieldA( const INFCONTEXT *context, DWORD index, PSTR buffer,
1549 DWORD size, DWORD *required )
1551 struct inf_file *file = context->CurrentInf;
1552 struct field *field = get_field( file, context->Section, context->Line, index );
1556 if (!field) return FALSE;
1557 len = PARSER_string_substA( file, field->text, NULL, 0 );
1558 if (required) *required = len + 1;
1563 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1566 PARSER_string_substA( file, field->text, buffer, size );
1568 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1569 context->Inf, context->CurrentInf, context->Section, context->Line,
1570 index, debugstr_a(buffer) );
1576 /***********************************************************************
1577 * SetupGetStringFieldW (SETUPAPI.@)
1579 BOOL WINAPI SetupGetStringFieldW( const INFCONTEXT *context, DWORD index, PWSTR buffer,
1580 DWORD size, DWORD *required )
1582 struct inf_file *file = context->CurrentInf;
1583 struct field *field = get_field( file, context->Section, context->Line, index );
1587 if (!field) return FALSE;
1588 len = PARSER_string_substW( file, field->text, NULL, 0 );
1589 if (required) *required = len + 1;
1594 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1597 PARSER_string_substW( file, field->text, buffer, size );
1599 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1600 context->Inf, context->CurrentInf, context->Section, context->Line,
1601 index, debugstr_w(buffer) );
1607 /***********************************************************************
1608 * SetupGetIntField (SETUPAPI.@)
1610 BOOL WINAPI SetupGetIntField( const INFCONTEXT *context, DWORD index, INT *result )
1613 char *end, *buffer = localbuff;
1618 if (!SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))
1620 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1621 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1622 if (!SetupGetStringFieldA( context, index, buffer, required, NULL )) goto done;
1624 res = strtol( buffer, &end, 0 );
1625 if (end != buffer && !*end)
1630 else SetLastError( ERROR_INVALID_DATA );
1633 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1638 /***********************************************************************
1639 * SetupGetBinaryField (SETUPAPI.@)
1641 BOOL WINAPI SetupGetBinaryField( const INFCONTEXT *context, DWORD index, BYTE *buffer,
1642 DWORD size, DWORD *required )
1644 struct inf_file *file = context->CurrentInf;
1645 struct line *line = get_line( file, context->Section, context->Line );
1646 struct field *field;
1651 SetLastError( ERROR_LINE_NOT_FOUND );
1654 if (!index || index >= line->nb_fields)
1656 SetLastError( ERROR_INVALID_PARAMETER );
1659 index--; /* fields start at 0 */
1660 if (required) *required = line->nb_fields - index;
1661 if (!buffer) return TRUE;
1662 if (size < line->nb_fields - index)
1664 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1667 field = &file->fields[line->first_field + index];
1668 for (i = index; i < line->nb_fields; i++, field++)
1672 for (p = field->text; *p && isxdigitW(*p); p++)
1674 if ((value <<= 4) > 255)
1676 SetLastError( ERROR_INVALID_DATA );
1679 if (*p <= '9') value |= (*p - '0');
1680 else value |= (tolowerW(*p) - 'a' + 10);
1682 buffer[i - index] = value;
1684 if (TRACE_ON(setupapi))
1686 TRACE( "%p/%p/%d/%d index %ld returning",
1687 context->Inf, context->CurrentInf, context->Section, context->Line, index );
1688 for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] );
1695 /***********************************************************************
1696 * SetupGetMultiSzFieldA (SETUPAPI.@)
1698 BOOL WINAPI SetupGetMultiSzFieldA( const INFCONTEXT *context, DWORD index, PSTR buffer,
1699 DWORD size, DWORD *required )
1701 struct inf_file *file = context->CurrentInf;
1702 struct line *line = get_line( file, context->Section, context->Line );
1703 struct field *field;
1710 SetLastError( ERROR_LINE_NOT_FOUND );
1713 if (!index || index >= line->nb_fields)
1715 SetLastError( ERROR_INVALID_PARAMETER );
1718 index--; /* fields start at 0 */
1719 field = &file->fields[line->first_field + index];
1720 for (i = index; i < line->nb_fields; i++, field++)
1722 if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1726 if (required) *required = total;
1727 if (!buffer) return TRUE;
1730 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1733 field = &file->fields[line->first_field + index];
1734 for (i = index; i < line->nb_fields; i++, field++)
1736 if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1739 *buffer = 0; /* add final null */
1744 /***********************************************************************
1745 * SetupGetMultiSzFieldW (SETUPAPI.@)
1747 BOOL WINAPI SetupGetMultiSzFieldW( const INFCONTEXT *context, DWORD index, PWSTR buffer,
1748 DWORD size, DWORD *required )
1750 struct inf_file *file = context->CurrentInf;
1751 struct line *line = get_line( file, context->Section, context->Line );
1752 struct field *field;
1759 SetLastError( ERROR_LINE_NOT_FOUND );
1762 if (!index || index >= line->nb_fields)
1764 SetLastError( ERROR_INVALID_PARAMETER );
1767 index--; /* fields start at 0 */
1768 field = &file->fields[line->first_field + index];
1769 for (i = index; i < line->nb_fields; i++, field++)
1771 if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1775 if (required) *required = total;
1776 if (!buffer) return TRUE;
1779 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1782 field = &file->fields[line->first_field + index];
1783 for (i = index; i < line->nb_fields; i++, field++)
1785 if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1788 *buffer = 0; /* add final null */