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
30 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
37 #define CONTROL_Z '\x1a'
38 #define MAX_SECTION_NAME_LEN 255
39 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
40 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
41 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
43 /* inf file structure definitions */
47 const WCHAR *text; /* field text */
52 int first_field; /* index of first field in field array */
53 int nb_fields; /* number of fields in line */
54 int key_field; /* index of field for key or -1 if no key */
59 const WCHAR *name; /* section name */
60 unsigned int nb_lines; /* number of used lines */
61 unsigned int alloc_lines; /* total number of allocated lines in array below */
62 struct line lines[16]; /* lines information (grown dynamically, 16 is initial size) */
67 struct inf_file *next; /* next appended file */
68 WCHAR *strings; /* buffer for string data (section names and field values) */
69 WCHAR *string_pos; /* position of next available string in buffer */
70 unsigned int nb_sections; /* number of used sections */
71 unsigned int alloc_sections; /* total number of allocated section pointers */
72 struct section **sections; /* section pointers array */
73 unsigned int nb_fields;
74 unsigned int alloc_fields;
76 int strings_section; /* index of [Strings] section or -1 if none */
77 WCHAR *src_root; /* source root directory */
80 /* parser definitions */
84 LINE_START, /* at beginning of a line */
85 SECTION_NAME, /* parsing a section name */
86 KEY_NAME, /* parsing a key name */
87 VALUE_NAME, /* parsing a value name */
88 EOL_BACKSLASH, /* backslash at end of line */
89 QUOTES, /* inside quotes */
90 LEADING_SPACES, /* leading spaces */
91 TRAILING_SPACES, /* trailing spaces */
92 COMMENT, /* inside a comment */
98 const WCHAR *start; /* start position of item being parsed */
99 const WCHAR *end; /* end of buffer */
100 struct inf_file *file; /* file being built */
101 enum parser_state state; /* current parser state */
102 enum parser_state stack[4]; /* state stack */
103 int stack_pos; /* current pos in stack */
105 int cur_section; /* index of section being parsed*/
106 struct line *line; /* current line */
107 unsigned int line_pos; /* current line position in file */
108 unsigned int error; /* error code */
109 unsigned int token_len; /* current token len */
110 WCHAR token[MAX_FIELD_LEN+1]; /* current token */
113 typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos );
115 /* parser state machine functions */
116 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos );
117 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos );
118 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos );
119 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos );
120 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos );
121 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos );
122 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos );
123 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos );
124 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos );
126 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
128 line_start_state, /* LINE_START */
129 section_name_state, /* SECTION_NAME */
130 key_name_state, /* KEY_NAME */
131 value_name_state, /* VALUE_NAME */
132 eol_backslash_state, /* EOL_BACKSLASH */
133 quotes_state, /* QUOTES */
134 leading_spaces_state, /* LEADING_SPACES */
135 trailing_spaces_state, /* TRAILING_SPACES */
136 comment_state /* COMMENT */
140 /* Unicode string constants */
141 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
142 static const WCHAR Signature[] = {'S','i','g','n','a','t','u','r','e',0};
143 static const WCHAR Chicago[] = {'$','C','h','i','c','a','g','o','$',0};
144 static const WCHAR WindowsNT[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
145 static const WCHAR Windows95[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
146 static const WCHAR LayoutFile[] = {'L','a','y','o','u','t','F','i','l','e',0};
148 /* extend an array, allocating more memory if necessary */
149 static void *grow_array( void *array, unsigned int *count, size_t elem )
152 unsigned int new_count = *count + *count / 2;
153 if (new_count < 32) new_count = 32;
154 if ((new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem )))
157 HeapFree( GetProcessHeap(), 0, array );
162 /* find a section by name */
163 static int find_section( struct inf_file *file, const WCHAR *name )
167 for (i = 0; i < file->nb_sections; i++)
168 if (!strcmpiW( name, file->sections[i]->name )) return i;
173 /* find a line by name */
174 static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name )
176 struct section *section;
180 if (section_index < 0 || section_index >= file->nb_sections) return NULL;
181 section = file->sections[section_index];
182 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
184 if (line->key_field == -1) continue;
185 if (!strcmpiW( name, file->fields[line->key_field].text )) return line;
191 /* add a section to the file and return the section index */
192 static int add_section( struct inf_file *file, const WCHAR *name )
194 struct section *section;
196 if (file->nb_sections >= file->alloc_sections)
198 if (!(file->sections = grow_array( file->sections, &file->alloc_sections,
199 sizeof(file->sections[0]) ))) return -1;
201 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1;
202 section->name = name;
203 section->nb_lines = 0;
204 section->alloc_lines = sizeof(section->lines)/sizeof(section->lines[0]);
205 file->sections[file->nb_sections] = section;
206 return file->nb_sections++;
210 /* add a line to a given section */
211 static struct line *add_line( struct inf_file *file, int section_index )
213 struct section *section;
216 assert( section_index >= 0 && section_index < file->nb_sections );
218 section = file->sections[section_index];
219 if (section->nb_lines == section->alloc_lines) /* need to grow the section */
221 int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line);
222 if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL;
223 section->alloc_lines *= 2;
224 file->sections[section_index] = section;
226 line = §ion->lines[section->nb_lines++];
227 line->first_field = file->nb_fields;
229 line->key_field = -1;
234 /* retrieve a given line from section/line index */
235 inline static struct line *get_line( struct inf_file *file, unsigned int section_index,
236 unsigned int line_index )
238 struct section *section;
240 if (section_index >= file->nb_sections) return NULL;
241 section = file->sections[section_index];
242 if (line_index >= section->nb_lines) return NULL;
243 return §ion->lines[line_index];
247 /* retrieve a given field from section/line/field index */
248 static struct field *get_field( struct inf_file *file, int section_index, int line_index,
251 struct line *line = get_line( file, section_index, line_index );
253 if (!line) return NULL;
254 if (!field_index) /* get the key */
256 if (line->key_field == -1) return NULL;
257 return &file->fields[line->key_field];
260 if (field_index >= line->nb_fields) return NULL;
261 return &file->fields[line->first_field + field_index];
265 /* allocate a new field, growing the array if necessary */
266 static struct field *add_field( struct inf_file *file, const WCHAR *text )
270 if (file->nb_fields >= file->alloc_fields)
272 if (!(file->fields = grow_array( file->fields, &file->alloc_fields,
273 sizeof(file->fields[0]) ))) return NULL;
275 field = &file->fields[file->nb_fields++];
281 /* retrieve the string substitution for a directory id */
282 static const WCHAR *get_dirid_subst( int dirid, unsigned int *len )
284 extern const WCHAR *DIRID_get_string( HINF hinf, int dirid );
285 const WCHAR *ret = DIRID_get_string( 0, dirid );
286 if (ret) *len = strlenW(ret);
291 /* retrieve the string substitution for a given string, or NULL if not found */
292 /* if found, len is set to the substitution length */
293 static const WCHAR *get_string_subst( struct inf_file *file, const WCHAR *str, unsigned int *len )
295 static const WCHAR percent = '%';
297 struct section *strings_section;
301 WCHAR *dirid_str, *end;
302 const WCHAR *ret = NULL;
304 if (!*len) /* empty string (%%) is replaced by single percent */
309 if (file->strings_section == -1) goto not_found;
310 strings_section = file->sections[file->strings_section];
311 for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++)
313 if (line->key_field == -1) continue;
314 if (strncmpiW( str, file->fields[line->key_field].text, *len )) continue;
315 if (!file->fields[line->key_field].text[*len]) break;
317 if (i == strings_section->nb_lines || !line->nb_fields) goto not_found;
318 field = &file->fields[line->first_field];
319 *len = strlenW( field->text );
322 not_found: /* check for integer id */
323 if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) )))
325 memcpy( dirid_str, str, *len * sizeof(WCHAR) );
327 dirid = wcstol( dirid_str, &end, 10 );
328 if (!*end) ret = get_dirid_subst( dirid, len );
329 HeapFree( GetProcessHeap(), 0, dirid_str );
336 /* do string substitutions on the specified text */
337 /* the buffer is assumed to be large enough */
338 /* returns necessary length not including terminating null */
339 unsigned int PARSER_string_substW( struct inf_file *file, const WCHAR *text, WCHAR *buffer,
342 const WCHAR *start, *subst, *p;
343 unsigned int len, total = 0;
346 if (!buffer) size = MAX_STRING_LEN + 1;
347 for (p = start = text; *p; p++)
349 if (*p != '%') continue;
351 if (inside) /* start of a %xx% string */
354 if (len > size - 1) len = size - 1;
355 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
360 else /* end of the %xx% string, find substitution */
363 subst = get_string_subst( file, start + 1, &len );
369 if (len > size - 1) len = size - 1;
370 if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
377 if (start != p) /* unfinished string, copy it */
380 if (len > size - 1) len = size - 1;
381 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
384 if (buffer && size) buffer[total] = 0;
389 /* do string substitutions on the specified text */
390 /* the buffer is assumed to be large enough */
391 /* returns necessary length not including terminating null */
392 unsigned int PARSER_string_substA( struct inf_file *file, const WCHAR *text, char *buffer,
395 WCHAR buffW[MAX_STRING_LEN+1];
398 unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) );
399 if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
402 RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
409 /* push some string data into the strings buffer */
410 static WCHAR *push_string( struct inf_file *file, const WCHAR *string )
412 WCHAR *ret = file->string_pos;
413 strcpyW( ret, string );
414 file->string_pos += strlenW( ret ) + 1;
419 /* push the current state on the parser stack */
420 inline static void push_state( struct parser *parser, enum parser_state state )
422 assert( parser->stack_pos < sizeof(parser->stack)/sizeof(parser->stack[0]) );
423 parser->stack[parser->stack_pos++] = state;
427 /* pop the current state */
428 inline static void pop_state( struct parser *parser )
430 assert( parser->stack_pos );
431 parser->state = parser->stack[--parser->stack_pos];
435 /* set the parser state and return the previous one */
436 inline static enum parser_state set_state( struct parser *parser, enum parser_state state )
438 enum parser_state ret = parser->state;
439 parser->state = state;
444 /* check if the pointer points to an end of file */
445 inline static int is_eof( struct parser *parser, const WCHAR *ptr )
447 return (ptr >= parser->end || *ptr == CONTROL_Z);
451 /* check if the pointer points to an end of line */
452 inline static int is_eol( struct parser *parser, const WCHAR *ptr )
454 return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n');
458 /* push data from current token start up to pos into the current token */
459 static int push_token( struct parser *parser, const WCHAR *pos )
461 int len = pos - parser->start;
462 const WCHAR *src = parser->start;
463 WCHAR *dst = parser->token + parser->token_len;
465 if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len;
467 parser->token_len += len;
468 for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' ';
475 /* add a section with the current token as name */
476 static int add_section_from_token( struct parser *parser )
480 if (parser->token_len > MAX_SECTION_NAME_LEN)
482 parser->error = ERROR_SECTION_NAME_TOO_LONG;
485 if ((section_index = find_section( parser->file, parser->token )) == -1)
487 /* need to create a new one */
488 const WCHAR *name = push_string( parser->file, parser->token );
489 if ((section_index = add_section( parser->file, name )) == -1)
491 parser->error = ERROR_NOT_ENOUGH_MEMORY;
495 parser->token_len = 0;
496 parser->cur_section = section_index;
497 return section_index;
501 /* add a field containing the current token to the current line */
502 static struct field *add_field_from_token( struct parser *parser, int is_key )
507 if (!parser->line) /* need to start a new line */
509 if (parser->cur_section == -1) /* got a line before the first section */
511 parser->error = ERROR_WRONG_INF_STYLE;
514 if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error;
516 else assert(!is_key);
518 text = push_string( parser->file, parser->token );
519 if ((field = add_field( parser->file, text )))
521 if (!is_key) parser->line->nb_fields++;
524 /* replace first field by key field */
525 parser->line->key_field = parser->line->first_field;
526 parser->line->first_field++;
528 parser->token_len = 0;
532 parser->error = ERROR_NOT_ENOUGH_MEMORY;
537 /* handler for parser LINE_START state */
538 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
542 for (p = pos; !is_eof( parser, p ); p++)
548 parser->line = NULL; /* start a new line */
551 push_state( parser, LINE_START );
552 set_state( parser, COMMENT );
555 parser->start = p + 1;
556 set_state( parser, SECTION_NAME );
562 set_state( parser, KEY_NAME );
572 /* handler for parser SECTION_NAME state */
573 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
577 for (p = pos; !is_eol( parser, p ); p++)
581 push_token( parser, p );
582 if (add_section_from_token( parser ) == -1) return NULL;
583 push_state( parser, LINE_START );
584 set_state( parser, COMMENT ); /* ignore everything else on the line */
588 parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */
593 /* handler for parser KEY_NAME state */
594 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos )
596 const WCHAR *p, *token_end = parser->start;
598 for (p = pos; !is_eol( parser, p ); p++)
600 if (*p == ',') break;
605 push_token( parser, token_end );
606 if (!add_field_from_token( parser, 1 )) return NULL;
607 parser->start = p + 1;
608 push_state( parser, VALUE_NAME );
609 set_state( parser, LEADING_SPACES );
612 push_token( parser, token_end );
613 if (!add_field_from_token( parser, 0 )) return NULL;
614 push_state( parser, LINE_START );
615 set_state( parser, COMMENT );
618 push_token( parser, token_end );
619 parser->start = p + 1;
620 push_state( parser, KEY_NAME );
621 set_state( parser, QUOTES );
624 push_token( parser, token_end );
626 push_state( parser, KEY_NAME );
627 set_state( parser, EOL_BACKSLASH );
630 if (!isspaceW(*p)) token_end = p + 1;
633 push_token( parser, p );
634 push_state( parser, KEY_NAME );
635 set_state( parser, TRAILING_SPACES );
641 push_token( parser, token_end );
642 set_state( parser, VALUE_NAME );
647 /* handler for parser VALUE_NAME state */
648 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos )
650 const WCHAR *p, *token_end = parser->start;
652 for (p = pos; !is_eol( parser, p ); p++)
657 push_token( parser, token_end );
658 if (!add_field_from_token( parser, 0 )) return NULL;
659 push_state( parser, LINE_START );
660 set_state( parser, COMMENT );
663 push_token( parser, token_end );
664 if (!add_field_from_token( parser, 0 )) return NULL;
665 parser->start = p + 1;
666 push_state( parser, VALUE_NAME );
667 set_state( parser, LEADING_SPACES );
670 push_token( parser, token_end );
671 parser->start = p + 1;
672 push_state( parser, VALUE_NAME );
673 set_state( parser, QUOTES );
676 push_token( parser, token_end );
678 push_state( parser, VALUE_NAME );
679 set_state( parser, EOL_BACKSLASH );
682 if (!isspaceW(*p)) token_end = p + 1;
685 push_token( parser, p );
686 push_state( parser, VALUE_NAME );
687 set_state( parser, TRAILING_SPACES );
693 push_token( parser, token_end );
694 if (!add_field_from_token( parser, 0 )) return NULL;
695 set_state( parser, LINE_START );
700 /* handler for parser EOL_BACKSLASH state */
701 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos )
705 for (p = pos; !is_eof( parser, p ); p++)
711 parser->start = p + 1;
712 set_state( parser, LEADING_SPACES );
717 push_state( parser, EOL_BACKSLASH );
718 set_state( parser, COMMENT );
721 if (isspaceW(*p)) continue;
722 push_token( parser, p );
733 /* handler for parser QUOTES state */
734 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos )
736 const WCHAR *p, *token_end = parser->start;
738 for (p = pos; !is_eol( parser, p ); p++)
742 if (p+1 < parser->end && p[1] == '"') /* double quotes */
744 push_token( parser, p + 1 );
745 parser->start = token_end = p + 2;
748 else /* end of quotes */
750 push_token( parser, p );
751 parser->start = p + 1;
757 push_token( parser, p );
763 /* handler for parser LEADING_SPACES state */
764 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos )
768 for (p = pos; !is_eol( parser, p ); p++)
773 set_state( parser, EOL_BACKSLASH );
776 if (!isspaceW(*p)) break;
784 /* handler for parser TRAILING_SPACES state */
785 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos )
789 for (p = pos; !is_eol( parser, p ); p++)
793 set_state( parser, EOL_BACKSLASH );
796 if (!isspaceW(*p)) break;
803 /* handler for parser COMMENT state */
804 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos )
806 const WCHAR *p = pos;
808 while (!is_eol( parser, p )) p++;
814 /* parse a complete buffer */
815 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
818 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
820 struct parser parser;
821 const WCHAR *pos = buffer;
823 parser.start = buffer;
827 parser.state = LINE_START;
828 parser.stack_pos = 0;
829 parser.cur_section = -1;
832 parser.token_len = 0;
834 /* parser main loop */
835 while (pos) pos = (parser_funcs[parser.state])( &parser, pos );
837 /* trim excess buffer space */
838 if (file->alloc_sections > file->nb_sections)
840 file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections,
841 file->nb_sections * sizeof(file->sections[0]) );
842 file->alloc_sections = file->nb_sections;
844 if (file->alloc_fields > file->nb_fields)
846 file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields,
847 file->nb_fields * sizeof(file->fields[0]) );
848 file->alloc_fields = file->nb_fields;
850 file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings,
851 (file->string_pos - file->strings) * sizeof(WCHAR) );
855 if (error_line) *error_line = parser.line_pos;
859 /* find the [strings] section */
860 file->strings_section = find_section( file, Strings );
865 /* append a child INF file to its parent list, in a thread-safe manner */
866 static void append_inf_file( struct inf_file *parent, struct inf_file *child )
868 struct inf_file **ppnext = &parent->next;
873 struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
875 ppnext = &next->next;
880 /***********************************************************************
885 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, UINT *error_line )
889 struct inf_file *file;
891 DWORD size = GetFileSize( handle, NULL );
892 HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL );
893 if (!mapping) return NULL;
894 buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size );
896 if (!buffer) return NULL;
898 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
900 if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) )))
902 err = ERROR_NOT_ENOUGH_MEMORY;
906 /* we won't need more strings space than the size of the file,
907 * so we can preallocate it here
909 if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
911 err = ERROR_NOT_ENOUGH_MEMORY;
914 file->string_pos = file->strings;
915 file->strings_section = -1;
917 if (!RtlIsTextUnicode( buffer, size, NULL ))
919 WCHAR *new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
922 DWORD len = MultiByteToWideChar( CP_ACP, 0, buffer, size, new_buff,
923 size * sizeof(WCHAR) );
924 err = parse_buffer( file, new_buff, new_buff + len, error_line );
925 HeapFree( GetProcessHeap(), 0, new_buff );
928 else err = parse_buffer( file, buffer, (WCHAR *)((char *)buffer + size), error_line );
930 if (!err) /* now check signature */
932 int version_index = find_section( file, Version );
933 if (version_index != -1)
935 struct line *line = find_line( file, version_index, Signature );
936 if (line && line->nb_fields > 0)
938 struct field *field = file->fields + line->first_field;
939 if (!strcmpiW( field->text, Chicago )) goto done;
940 if (!strcmpiW( field->text, WindowsNT )) goto done;
941 if (!strcmpiW( field->text, Windows95 )) goto done;
944 err = ERROR_WRONG_INF_STYLE;
948 UnmapViewOfFile( buffer );
951 HeapFree( GetProcessHeap(), 0, file );
959 /***********************************************************************
960 * PARSER_get_src_root
962 * Retrieve the source directory of an inf file.
964 const WCHAR *PARSER_get_src_root( HINF hinf )
966 struct inf_file *file = hinf;
967 return file->src_root;
971 /***********************************************************************
972 * SetupOpenInfFileA (SETUPAPI.@)
974 HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error )
976 UNICODE_STRING nameW, classW;
977 HINF ret = (HINF)INVALID_HANDLE_VALUE;
979 classW.Buffer = NULL;
980 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class ))
982 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
985 if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
987 ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
988 RtlFreeUnicodeString( &nameW );
990 RtlFreeUnicodeString( &classW );
995 /***********************************************************************
996 * SetupOpenInfFileW (SETUPAPI.@)
998 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1000 struct inf_file *file = NULL;
1005 if (strchrW( name, '\\' ) || strchrW( name, '/' ))
1007 if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return (HINF)INVALID_HANDLE_VALUE;
1008 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1010 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1011 return (HINF)INVALID_HANDLE_VALUE;
1013 GetFullPathNameW( name, len, path, NULL );
1014 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1016 else /* try Windows directory */
1018 static const WCHAR Inf[] = {'\\','i','n','f','\\',0};
1019 static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1021 len = GetWindowsDirectoryW( NULL, 0 ) + strlenW(name) + 12;
1022 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1024 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1025 return (HINF)INVALID_HANDLE_VALUE;
1027 GetWindowsDirectoryW( path, len );
1028 p = path + strlenW(path);
1031 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1032 if (handle == INVALID_HANDLE_VALUE)
1034 strcpyW( p, System32 );
1036 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1040 if (handle != INVALID_HANDLE_VALUE)
1042 file = parse_file( handle, class, error );
1043 CloseHandle( handle );
1047 HeapFree( GetProcessHeap(), 0, path );
1048 return (HINF)INVALID_HANDLE_VALUE;
1050 TRACE( "%s -> %p\n", debugstr_w(path), file );
1051 file->src_root = path;
1052 if ((p = strrchrW( path, '\\' ))) p[1] = 0; /* remove file name */
1058 /***********************************************************************
1059 * SetupOpenAppendInfFileA (SETUPAPI.@)
1061 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
1065 if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error );
1066 child_hinf = SetupOpenInfFileA( name, NULL, INF_STYLE_WIN4, error );
1067 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1068 append_inf_file( parent_hinf, child_hinf );
1069 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_a(name), child_hinf );
1074 /***********************************************************************
1075 * SetupOpenAppendInfFileW (SETUPAPI.@)
1077 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1084 WCHAR filename[MAX_PATH];
1087 if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE;
1088 while (SetupGetStringFieldW( &context, idx++, filename,
1089 sizeof(filename)/sizeof(WCHAR), NULL ))
1091 child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error );
1092 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1093 append_inf_file( parent_hinf, child_hinf );
1094 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf );
1098 child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error );
1099 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1100 append_inf_file( parent_hinf, child_hinf );
1101 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf );
1106 /***********************************************************************
1107 * SetupCloseInfFile (SETUPAPI.@)
1109 void WINAPI SetupCloseInfFile( HINF hinf )
1111 struct inf_file *file = hinf;
1114 for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
1115 HeapFree( GetProcessHeap(), 0, file->src_root );
1116 HeapFree( GetProcessHeap(), 0, file->sections );
1117 HeapFree( GetProcessHeap(), 0, file->fields );
1118 HeapFree( GetProcessHeap(), 0, file->strings );
1119 HeapFree( GetProcessHeap(), 0, file );
1123 /***********************************************************************
1124 * SetupGetLineCountA (SETUPAPI.@)
1126 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1128 UNICODE_STRING sectionW;
1131 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, name ))
1132 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1135 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1136 RtlFreeUnicodeString( §ionW );
1142 /***********************************************************************
1143 * SetupGetLineCountW (SETUPAPI.@)
1145 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1147 struct inf_file *file = hinf;
1151 for (file = hinf; file; file = file->next)
1153 if ((section_index = find_section( file, section )) == -1) continue;
1154 if (ret == -1) ret = 0;
1155 ret += file->sections[section_index]->nb_lines;
1157 TRACE( "(%p,%s) returning %ld\n", hinf, debugstr_w(section), ret );
1158 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1163 /***********************************************************************
1164 * SetupGetLineByIndexA (SETUPAPI.@)
1166 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1168 UNICODE_STRING sectionW;
1171 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
1172 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1175 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1176 RtlFreeUnicodeString( §ionW );
1182 /***********************************************************************
1183 * SetupGetLineByIndexW (SETUPAPI.@)
1185 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1187 struct inf_file *file = hinf;
1190 SetLastError( ERROR_SECTION_NOT_FOUND );
1191 for (file = hinf; file; file = file->next)
1193 if ((section_index = find_section( file, section )) == -1) continue;
1194 SetLastError( ERROR_LINE_NOT_FOUND );
1195 if (index < file->sections[section_index]->nb_lines)
1197 context->Inf = hinf;
1198 context->CurrentInf = file;
1199 context->Section = section_index;
1200 context->Line = index;
1202 TRACE( "(%p,%s): returning %d/%ld\n",
1203 hinf, debugstr_w(section), section_index, index );
1206 index -= file->sections[section_index]->nb_lines;
1208 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1213 /***********************************************************************
1214 * SetupFindFirstLineA (SETUPAPI.@)
1216 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1218 UNICODE_STRING sectionW, keyW;
1221 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
1223 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1227 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1230 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1232 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1233 RtlFreeUnicodeString( &keyW );
1235 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1237 RtlFreeUnicodeString( §ionW );
1242 /***********************************************************************
1243 * SetupFindFirstLineW (SETUPAPI.@)
1245 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1247 struct inf_file *file;
1250 SetLastError( ERROR_SECTION_NOT_FOUND );
1251 for (file = hinf; file; file = file->next)
1253 if ((section_index = find_section( file, section )) == -1) continue;
1258 ctx.CurrentInf = file;
1259 ctx.Section = section_index;
1261 return SetupFindNextMatchLineW( &ctx, key, context );
1263 SetLastError( ERROR_LINE_NOT_FOUND ); /* found at least one section */
1264 if (file->sections[section_index]->nb_lines)
1266 context->Inf = hinf;
1267 context->CurrentInf = file;
1268 context->Section = section_index;
1271 TRACE( "(%p,%s,%s): returning %d/0\n",
1272 hinf, debugstr_w(section), debugstr_w(key), section_index );
1276 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1281 /***********************************************************************
1282 * SetupFindNextLine (SETUPAPI.@)
1284 BOOL WINAPI SetupFindNextLine( const INFCONTEXT *context_in, INFCONTEXT *context_out )
1286 struct inf_file *file = context_in->CurrentInf;
1287 struct section *section;
1289 if (context_in->Section >= file->nb_sections) goto error;
1291 section = file->sections[context_in->Section];
1292 if (context_in->Line+1 < section->nb_lines)
1294 if (context_out != context_in) *context_out = *context_in;
1295 context_out->Line++;
1300 /* now search the appended files */
1302 for (file = file->next; file; file = file->next)
1304 int section_index = find_section( file, section->name );
1305 if (section_index == -1) continue;
1306 if (file->sections[section_index]->nb_lines)
1308 context_out->Inf = context_in->Inf;
1309 context_out->CurrentInf = file;
1310 context_out->Section = section_index;
1311 context_out->Line = 0;
1317 SetLastError( ERROR_LINE_NOT_FOUND );
1322 /***********************************************************************
1323 * SetupFindNextMatchLineA (SETUPAPI.@)
1325 BOOL WINAPI SetupFindNextMatchLineA( const INFCONTEXT *context_in, PCSTR key,
1326 INFCONTEXT *context_out )
1328 UNICODE_STRING keyW;
1331 if (!key) return SetupFindNextLine( context_in, context_out );
1333 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1334 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1337 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1338 RtlFreeUnicodeString( &keyW );
1344 /***********************************************************************
1345 * SetupFindNextMatchLineW (SETUPAPI.@)
1347 BOOL WINAPI SetupFindNextMatchLineW( const INFCONTEXT *context_in, PCWSTR key,
1348 INFCONTEXT *context_out )
1350 struct inf_file *file = context_in->CurrentInf;
1351 struct section *section;
1355 if (!key) return SetupFindNextLine( context_in, context_out );
1357 if (context_in->Section >= file->nb_sections) goto error;
1359 section = file->sections[context_in->Section];
1361 for (i = context_in->Line+1, line = §ion->lines[i]; i < section->nb_lines; i++, line++)
1363 if (line->key_field == -1) continue;
1364 if (!strcmpiW( key, file->fields[line->key_field].text ))
1366 if (context_out != context_in) *context_out = *context_in;
1367 context_out->Line = i;
1369 TRACE( "(%p,%s,%s): returning %d\n",
1370 file, debugstr_w(section->name), debugstr_w(key), i );
1375 /* now search the appended files */
1377 for (file = file->next; file; file = file->next)
1379 int section_index = find_section( file, section->name );
1380 if (section_index == -1) continue;
1381 section = file->sections[section_index];
1382 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1384 if (line->key_field == -1) continue;
1385 if (!strcmpiW( key, file->fields[line->key_field].text ))
1387 context_out->Inf = context_in->Inf;
1388 context_out->CurrentInf = file;
1389 context_out->Section = section_index;
1390 context_out->Line = i;
1392 TRACE( "(%p,%s,%s): returning %d/%d\n",
1393 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1398 TRACE( "(%p,%s,%s): not found\n",
1399 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1401 SetLastError( ERROR_LINE_NOT_FOUND );
1406 /***********************************************************************
1407 * SetupGetLineTextW (SETUPAPI.@)
1409 BOOL WINAPI SetupGetLineTextW( const INFCONTEXT *context, HINF hinf, PCWSTR section_name,
1410 PCWSTR key_name, PWSTR buffer, DWORD size, DWORD *required )
1412 struct inf_file *file;
1414 struct field *field;
1420 INFCONTEXT new_context;
1421 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1422 file = new_context.CurrentInf;
1423 line = get_line( file, new_context.Section, new_context.Line );
1427 file = context->CurrentInf;
1428 if (!(line = get_line( file, context->Section, context->Line )))
1430 SetLastError( ERROR_LINE_NOT_FOUND );
1435 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1436 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1438 if (required) *required = total;
1443 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1446 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1448 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1449 if (i+1 < line->nb_fields) buffer[len] = ',';
1457 /***********************************************************************
1458 * SetupGetLineTextA (SETUPAPI.@)
1460 BOOL WINAPI SetupGetLineTextA( const INFCONTEXT *context, HINF hinf, PCSTR section_name,
1461 PCSTR key_name, PSTR buffer, DWORD size, DWORD *required )
1463 struct inf_file *file;
1465 struct field *field;
1471 INFCONTEXT new_context;
1472 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1473 file = new_context.CurrentInf;
1474 line = get_line( file, new_context.Section, new_context.Line );
1478 file = context->CurrentInf;
1479 if (!(line = get_line( file, context->Section, context->Line )))
1481 SetLastError( ERROR_LINE_NOT_FOUND );
1486 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1487 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1489 if (required) *required = total;
1494 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1497 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1499 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1500 if (i+1 < line->nb_fields) buffer[len] = ',';
1508 /***********************************************************************
1509 * SetupGetFieldCount (SETUPAPI.@)
1511 DWORD WINAPI SetupGetFieldCount( const INFCONTEXT *context )
1513 struct inf_file *file = context->CurrentInf;
1514 struct line *line = get_line( file, context->Section, context->Line );
1516 if (!line) return 0;
1517 return line->nb_fields;
1521 /***********************************************************************
1522 * SetupGetStringFieldA (SETUPAPI.@)
1524 BOOL WINAPI SetupGetStringFieldA( const INFCONTEXT *context, DWORD index, PSTR buffer,
1525 DWORD size, DWORD *required )
1527 struct inf_file *file = context->CurrentInf;
1528 struct field *field = get_field( file, context->Section, context->Line, index );
1532 if (!field) return FALSE;
1533 len = PARSER_string_substA( file, field->text, NULL, 0 );
1534 if (required) *required = len + 1;
1539 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1542 PARSER_string_substA( file, field->text, buffer, size );
1544 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1545 context->Inf, context->CurrentInf, context->Section, context->Line,
1546 index, debugstr_a(buffer) );
1552 /***********************************************************************
1553 * SetupGetStringFieldW (SETUPAPI.@)
1555 BOOL WINAPI SetupGetStringFieldW( const INFCONTEXT *context, DWORD index, PWSTR buffer,
1556 DWORD size, DWORD *required )
1558 struct inf_file *file = context->CurrentInf;
1559 struct field *field = get_field( file, context->Section, context->Line, index );
1563 if (!field) return FALSE;
1564 len = PARSER_string_substW( file, field->text, NULL, 0 );
1565 if (required) *required = len + 1;
1570 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1573 PARSER_string_substW( file, field->text, buffer, size );
1575 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1576 context->Inf, context->CurrentInf, context->Section, context->Line,
1577 index, debugstr_w(buffer) );
1583 /***********************************************************************
1584 * SetupGetIntField (SETUPAPI.@)
1586 BOOL WINAPI SetupGetIntField( const INFCONTEXT *context, DWORD index, INT *result )
1589 char *end, *buffer = localbuff;
1594 if (!SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))
1596 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1597 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1598 if (!SetupGetStringFieldA( context, index, buffer, required, NULL )) goto done;
1600 res = strtol( buffer, &end, 0 );
1601 if (end != buffer && !*end)
1606 else SetLastError( ERROR_INVALID_DATA );
1609 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1614 /***********************************************************************
1615 * SetupGetBinaryField (SETUPAPI.@)
1617 BOOL WINAPI SetupGetBinaryField( const INFCONTEXT *context, DWORD index, BYTE *buffer,
1618 DWORD size, DWORD *required )
1620 struct inf_file *file = context->CurrentInf;
1621 struct line *line = get_line( file, context->Section, context->Line );
1622 struct field *field;
1627 SetLastError( ERROR_LINE_NOT_FOUND );
1630 if (!index || index >= line->nb_fields)
1632 SetLastError( ERROR_INVALID_PARAMETER );
1635 index--; /* fields start at 0 */
1636 if (required) *required = line->nb_fields - index;
1637 if (!buffer) return TRUE;
1638 if (size < line->nb_fields - index)
1640 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1643 field = &file->fields[line->first_field + index];
1644 for (i = index; i < line->nb_fields; i++, field++)
1648 for (p = field->text; *p && isxdigitW(*p); p++)
1650 if ((value <<= 8) > 255)
1652 SetLastError( ERROR_INVALID_DATA );
1655 if (*p <= '9') value |= (*p - '0');
1656 else value |= (tolowerW(*p) - 'a' + 10);
1658 buffer[i - index] = value;
1660 if (TRACE_ON(setupapi))
1662 TRACE( "%p/%p/%d/%d index %ld returning",
1663 context->Inf, context->CurrentInf, context->Section, context->Line, index );
1664 for (i = index; i < line->nb_fields; i++) DPRINTF( " %02x", buffer[i - index] );
1671 /***********************************************************************
1672 * SetupGetMultiSzFieldA (SETUPAPI.@)
1674 BOOL WINAPI SetupGetMultiSzFieldA( const INFCONTEXT *context, DWORD index, PSTR buffer,
1675 DWORD size, DWORD *required )
1677 struct inf_file *file = context->CurrentInf;
1678 struct line *line = get_line( file, context->Section, context->Line );
1679 struct field *field;
1686 SetLastError( ERROR_LINE_NOT_FOUND );
1689 if (!index || index >= line->nb_fields)
1691 SetLastError( ERROR_INVALID_PARAMETER );
1694 index--; /* fields start at 0 */
1695 field = &file->fields[line->first_field + index];
1696 for (i = index; i < line->nb_fields; i++, field++)
1698 if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1702 if (required) *required = total;
1703 if (!buffer) return TRUE;
1706 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1709 field = &file->fields[line->first_field + index];
1710 for (i = index; i < line->nb_fields; i++, field++)
1712 if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1715 *buffer = 0; /* add final null */
1720 /***********************************************************************
1721 * SetupGetMultiSzFieldW (SETUPAPI.@)
1723 BOOL WINAPI SetupGetMultiSzFieldW( const INFCONTEXT *context, DWORD index, PWSTR buffer,
1724 DWORD size, DWORD *required )
1726 struct inf_file *file = context->CurrentInf;
1727 struct line *line = get_line( file, context->Section, context->Line );
1728 struct field *field;
1735 SetLastError( ERROR_LINE_NOT_FOUND );
1738 if (!index || index >= line->nb_fields)
1740 SetLastError( ERROR_INVALID_PARAMETER );
1743 index--; /* fields start at 0 */
1744 field = &file->fields[line->first_field + index];
1745 for (i = index; i < line->nb_fields; i++, field++)
1747 if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1751 if (required) *required = total;
1752 if (!buffer) return TRUE;
1755 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1758 field = &file->fields[line->first_field + index];
1759 for (i = index; i < line->nb_fields; i++, field++)
1761 if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1764 *buffer = 0; /* add final null */