push 378fe7a60681a28e8b22f62dcfe122d585b92570
[wine/hacks.git] / dlls / setupapi / parser.c
blob65a6a037499e986de1e779345d597ef4514c7f46
1 /*
2 * INF file parsing
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winnls.h"
35 #include "winreg.h"
36 #include "winternl.h"
37 #include "winerror.h"
38 #include "setupapi.h"
39 #include "setupapi_private.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
46 #define CONTROL_Z '\x1a'
47 #define MAX_SECTION_NAME_LEN 255
48 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
49 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
50 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
52 /* inf file structure definitions */
54 struct field
56 const WCHAR *text; /* field text */
59 struct line
61 int first_field; /* index of first field in field array */
62 int nb_fields; /* number of fields in line */
63 int key_field; /* index of field for key or -1 if no key */
66 struct section
68 const WCHAR *name; /* section name */
69 unsigned int nb_lines; /* number of used lines */
70 unsigned int alloc_lines; /* total number of allocated lines in array below */
71 struct line lines[16]; /* lines information (grown dynamically, 16 is initial size) */
74 struct inf_file
76 struct inf_file *next; /* next appended file */
77 WCHAR *strings; /* buffer for string data (section names and field values) */
78 WCHAR *string_pos; /* position of next available string in buffer */
79 unsigned int nb_sections; /* number of used sections */
80 unsigned int alloc_sections; /* total number of allocated section pointers */
81 struct section **sections; /* section pointers array */
82 unsigned int nb_fields;
83 unsigned int alloc_fields;
84 struct field *fields;
85 int strings_section; /* index of [Strings] section or -1 if none */
86 WCHAR *filename; /* filename of the INF */
89 /* parser definitions */
91 enum parser_state
93 LINE_START, /* at beginning of a line */
94 SECTION_NAME, /* parsing a section name */
95 KEY_NAME, /* parsing a key name */
96 VALUE_NAME, /* parsing a value name */
97 EOL_BACKSLASH, /* backslash at end of line */
98 QUOTES, /* inside quotes */
99 LEADING_SPACES, /* leading spaces */
100 TRAILING_SPACES, /* trailing spaces */
101 COMMENT, /* inside a comment */
102 NB_PARSER_STATES
105 struct parser
107 const WCHAR *start; /* start position of item being parsed */
108 const WCHAR *end; /* end of buffer */
109 struct inf_file *file; /* file being built */
110 enum parser_state state; /* current parser state */
111 enum parser_state stack[4]; /* state stack */
112 int stack_pos; /* current pos in stack */
114 int cur_section; /* index of section being parsed*/
115 struct line *line; /* current line */
116 unsigned int line_pos; /* current line position in file */
117 unsigned int error; /* error code */
118 unsigned int token_len; /* current token len */
119 WCHAR token[MAX_FIELD_LEN+1]; /* current token */
122 typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos );
124 /* parser state machine functions */
125 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos );
126 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos );
127 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos );
128 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos );
129 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos );
130 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos );
131 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos );
132 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos );
133 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos );
135 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
137 line_start_state, /* LINE_START */
138 section_name_state, /* SECTION_NAME */
139 key_name_state, /* KEY_NAME */
140 value_name_state, /* VALUE_NAME */
141 eol_backslash_state, /* EOL_BACKSLASH */
142 quotes_state, /* QUOTES */
143 leading_spaces_state, /* LEADING_SPACES */
144 trailing_spaces_state, /* TRAILING_SPACES */
145 comment_state /* COMMENT */
149 /* Unicode string constants */
150 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
151 static const WCHAR Signature[] = {'S','i','g','n','a','t','u','r','e',0};
152 static const WCHAR Chicago[] = {'$','C','h','i','c','a','g','o','$',0};
153 static const WCHAR WindowsNT[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
154 static const WCHAR Windows95[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
155 static const WCHAR LayoutFile[] = {'L','a','y','o','u','t','F','i','l','e',0};
157 /* extend an array, allocating more memory if necessary */
158 static void *grow_array( void *array, unsigned int *count, size_t elem )
160 void *new_array;
161 unsigned int new_count = *count + *count / 2;
162 if (new_count < 32) new_count = 32;
164 if (array)
165 new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem );
166 else
167 new_array = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * elem );
169 if (new_array)
170 *count = new_count;
171 else
172 HeapFree( GetProcessHeap(), 0, array );
173 return new_array;
177 /* get the directory of the inf file (as counted string, not null-terminated) */
178 static const WCHAR *get_inf_dir( const struct inf_file *file, unsigned int *len )
180 const WCHAR *p = strrchrW( file->filename, '\\' );
181 *len = p ? (p + 1 - file->filename) : 0;
182 return file->filename;
186 /* find a section by name */
187 static int find_section( const struct inf_file *file, const WCHAR *name )
189 unsigned int i;
191 for (i = 0; i < file->nb_sections; i++)
192 if (!strcmpiW( name, file->sections[i]->name )) return i;
193 return -1;
197 /* find a line by name */
198 static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name )
200 struct section *section;
201 struct line *line;
202 unsigned int i;
204 if (section_index < 0 || section_index >= file->nb_sections) return NULL;
205 section = file->sections[section_index];
206 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
208 if (line->key_field == -1) continue;
209 if (!strcmpiW( name, file->fields[line->key_field].text )) return line;
211 return NULL;
215 /* add a section to the file and return the section index */
216 static int add_section( struct inf_file *file, const WCHAR *name )
218 struct section *section;
220 if (file->nb_sections >= file->alloc_sections)
222 if (!(file->sections = grow_array( file->sections, &file->alloc_sections,
223 sizeof(file->sections[0]) ))) return -1;
225 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1;
226 section->name = name;
227 section->nb_lines = 0;
228 section->alloc_lines = sizeof(section->lines)/sizeof(section->lines[0]);
229 file->sections[file->nb_sections] = section;
230 return file->nb_sections++;
234 /* add a line to a given section */
235 static struct line *add_line( struct inf_file *file, int section_index )
237 struct section *section;
238 struct line *line;
240 assert( section_index >= 0 && section_index < file->nb_sections );
242 section = file->sections[section_index];
243 if (section->nb_lines == section->alloc_lines) /* need to grow the section */
245 int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line);
246 if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL;
247 section->alloc_lines *= 2;
248 file->sections[section_index] = section;
250 line = &section->lines[section->nb_lines++];
251 line->first_field = file->nb_fields;
252 line->nb_fields = 0;
253 line->key_field = -1;
254 return line;
258 /* retrieve a given line from section/line index */
259 static inline struct line *get_line( struct inf_file *file, unsigned int section_index,
260 unsigned int line_index )
262 struct section *section;
264 if (section_index >= file->nb_sections) return NULL;
265 section = file->sections[section_index];
266 if (line_index >= section->nb_lines) return NULL;
267 return &section->lines[line_index];
271 /* retrieve a given field from section/line/field index */
272 static struct field *get_field( struct inf_file *file, int section_index, int line_index,
273 int field_index )
275 struct line *line = get_line( file, section_index, line_index );
277 if (!line) return NULL;
278 if (!field_index) /* get the key */
280 if (line->key_field == -1) return NULL;
281 return &file->fields[line->key_field];
283 field_index--;
284 if (field_index >= line->nb_fields) return NULL;
285 return &file->fields[line->first_field + field_index];
289 /* allocate a new field, growing the array if necessary */
290 static struct field *add_field( struct inf_file *file, const WCHAR *text )
292 struct field *field;
294 if (file->nb_fields >= file->alloc_fields)
296 if (!(file->fields = grow_array( file->fields, &file->alloc_fields,
297 sizeof(file->fields[0]) ))) return NULL;
299 field = &file->fields[file->nb_fields++];
300 field->text = text;
301 return field;
305 /* retrieve the string substitution for a directory id */
306 static const WCHAR *get_dirid_subst( const struct inf_file *file, int dirid, unsigned int *len )
308 const WCHAR *ret;
310 if (dirid == DIRID_SRCPATH) return get_inf_dir( file, len );
311 ret = DIRID_get_string( dirid );
312 if (ret) *len = strlenW(ret);
313 return ret;
317 /* retrieve the string substitution for a given string, or NULL if not found */
318 /* if found, len is set to the substitution length */
319 static const WCHAR *get_string_subst( const struct inf_file *file, const WCHAR *str, unsigned int *len,
320 BOOL no_trailing_slash )
322 static const WCHAR percent = '%';
324 struct section *strings_section;
325 struct line *line;
326 struct field *field;
327 unsigned int i;
328 int dirid;
329 WCHAR *dirid_str, *end;
330 const WCHAR *ret = NULL;
332 if (!*len) /* empty string (%%) is replaced by single percent */
334 *len = 1;
335 return &percent;
337 if (file->strings_section == -1) goto not_found;
338 strings_section = file->sections[file->strings_section];
339 for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++)
341 if (line->key_field == -1) continue;
342 if (strncmpiW( str, file->fields[line->key_field].text, *len )) continue;
343 if (!file->fields[line->key_field].text[*len]) break;
345 if (i == strings_section->nb_lines || !line->nb_fields) goto not_found;
346 field = &file->fields[line->first_field];
347 *len = strlenW( field->text );
348 return field->text;
350 not_found: /* check for integer id */
351 if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) )))
353 memcpy( dirid_str, str, *len * sizeof(WCHAR) );
354 dirid_str[*len] = 0;
355 dirid = strtolW( dirid_str, &end, 10 );
356 if (!*end) ret = get_dirid_subst( file, dirid, len );
357 if (no_trailing_slash && ret && *len && ret[*len - 1] == '\\') *len -= 1;
358 HeapFree( GetProcessHeap(), 0, dirid_str );
359 return ret;
361 return NULL;
365 /* do string substitutions on the specified text */
366 /* the buffer is assumed to be large enough */
367 /* returns necessary length not including terminating null */
368 static unsigned int PARSER_string_substW( const struct inf_file *file, const WCHAR *text,
369 WCHAR *buffer, unsigned int size )
371 const WCHAR *start, *subst, *p;
372 unsigned int len, total = 0;
373 int inside = 0;
375 if (!buffer) size = MAX_STRING_LEN + 1;
376 for (p = start = text; *p; p++)
378 if (*p != '%') continue;
379 inside = !inside;
380 if (inside) /* start of a %xx% string */
382 len = p - start;
383 if (len > size - 1) len = size - 1;
384 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
385 total += len;
386 size -= len;
387 start = p;
389 else /* end of the %xx% string, find substitution */
391 len = p - start - 1;
392 subst = get_string_subst( file, start + 1, &len, p[1] == '\\' );
393 if (!subst)
395 subst = start;
396 len = p - start + 1;
398 if (len > size - 1) len = size - 1;
399 if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
400 total += len;
401 size -= len;
402 start = p + 1;
406 if (start != p) /* unfinished string, copy it */
408 len = p - start;
409 if (len > size - 1) len = size - 1;
410 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
411 total += len;
413 if (buffer && size) buffer[total] = 0;
414 return total;
418 /* do string substitutions on the specified text */
419 /* the buffer is assumed to be large enough */
420 /* returns necessary length not including terminating null */
421 unsigned int PARSER_string_substA( const struct inf_file *file, const WCHAR *text, char *buffer,
422 unsigned int size )
424 WCHAR buffW[MAX_STRING_LEN+1];
425 DWORD ret;
427 unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) );
428 if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
429 else
431 RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
432 buffer[ret] = 0;
434 return ret;
438 /* push some string data into the strings buffer */
439 static WCHAR *push_string( struct inf_file *file, const WCHAR *string )
441 WCHAR *ret = file->string_pos;
442 strcpyW( ret, string );
443 file->string_pos += strlenW( ret ) + 1;
444 return ret;
448 /* push the current state on the parser stack */
449 static inline void push_state( struct parser *parser, enum parser_state state )
451 assert( parser->stack_pos < sizeof(parser->stack)/sizeof(parser->stack[0]) );
452 parser->stack[parser->stack_pos++] = state;
456 /* pop the current state */
457 static inline void pop_state( struct parser *parser )
459 assert( parser->stack_pos );
460 parser->state = parser->stack[--parser->stack_pos];
464 /* set the parser state and return the previous one */
465 static inline enum parser_state set_state( struct parser *parser, enum parser_state state )
467 enum parser_state ret = parser->state;
468 parser->state = state;
469 return ret;
473 /* check if the pointer points to an end of file */
474 static inline int is_eof( const struct parser *parser, const WCHAR *ptr )
476 return (ptr >= parser->end || *ptr == CONTROL_Z);
480 /* check if the pointer points to an end of line */
481 static inline int is_eol( const struct parser *parser, const WCHAR *ptr )
483 return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n');
487 /* push data from current token start up to pos into the current token */
488 static int push_token( struct parser *parser, const WCHAR *pos )
490 int len = pos - parser->start;
491 const WCHAR *src = parser->start;
492 WCHAR *dst = parser->token + parser->token_len;
494 if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len;
496 parser->token_len += len;
497 for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' ';
498 *dst = 0;
499 parser->start = pos;
500 return 0;
504 /* add a section with the current token as name */
505 static int add_section_from_token( struct parser *parser )
507 int section_index;
509 if (parser->token_len > MAX_SECTION_NAME_LEN)
511 parser->error = ERROR_SECTION_NAME_TOO_LONG;
512 return -1;
514 if ((section_index = find_section( parser->file, parser->token )) == -1)
516 /* need to create a new one */
517 const WCHAR *name = push_string( parser->file, parser->token );
518 if ((section_index = add_section( parser->file, name )) == -1)
520 parser->error = ERROR_NOT_ENOUGH_MEMORY;
521 return -1;
524 parser->token_len = 0;
525 parser->cur_section = section_index;
526 return section_index;
530 /* add a field containing the current token to the current line */
531 static struct field *add_field_from_token( struct parser *parser, int is_key )
533 struct field *field;
534 WCHAR *text;
536 if (!parser->line) /* need to start a new line */
538 if (parser->cur_section == -1) /* got a line before the first section */
540 parser->error = ERROR_EXPECTED_SECTION_NAME;
541 return NULL;
543 if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error;
545 else assert(!is_key);
547 text = push_string( parser->file, parser->token );
548 if ((field = add_field( parser->file, text )))
550 if (!is_key) parser->line->nb_fields++;
551 else
553 /* replace first field by key field */
554 parser->line->key_field = parser->line->first_field;
555 parser->line->first_field++;
557 parser->token_len = 0;
558 return field;
560 error:
561 parser->error = ERROR_NOT_ENOUGH_MEMORY;
562 return NULL;
566 /* close the current line and prepare for parsing a new one */
567 static void close_current_line( struct parser *parser )
569 struct line *cur_line = parser->line;
571 if (cur_line)
573 /* if line has a single field and no key, the field is the key too */
574 if (cur_line->nb_fields == 1 && cur_line->key_field == -1)
575 cur_line->key_field = cur_line->first_field;
577 parser->line = NULL;
581 /* handler for parser LINE_START state */
582 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
584 const WCHAR *p;
586 for (p = pos; !is_eof( parser, p ); p++)
588 switch(*p)
590 case '\n':
591 parser->line_pos++;
592 close_current_line( parser );
593 break;
594 case ';':
595 push_state( parser, LINE_START );
596 set_state( parser, COMMENT );
597 return p + 1;
598 case '[':
599 parser->start = p + 1;
600 set_state( parser, SECTION_NAME );
601 return p + 1;
602 default:
603 if (!isspaceW(*p))
605 parser->start = p;
606 set_state( parser, KEY_NAME );
607 return p;
609 break;
612 close_current_line( parser );
613 return NULL;
617 /* handler for parser SECTION_NAME state */
618 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
620 const WCHAR *p;
622 for (p = pos; !is_eol( parser, p ); p++)
624 if (*p == ']')
626 push_token( parser, p );
627 if (add_section_from_token( parser ) == -1) return NULL;
628 push_state( parser, LINE_START );
629 set_state( parser, COMMENT ); /* ignore everything else on the line */
630 return p + 1;
633 parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */
634 return NULL;
638 /* handler for parser KEY_NAME state */
639 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos )
641 const WCHAR *p, *token_end = parser->start;
643 for (p = pos; !is_eol( parser, p ); p++)
645 if (*p == ',') break;
646 switch(*p)
649 case '=':
650 push_token( parser, token_end );
651 if (!add_field_from_token( parser, 1 )) return NULL;
652 parser->start = p + 1;
653 push_state( parser, VALUE_NAME );
654 set_state( parser, LEADING_SPACES );
655 return p + 1;
656 case ';':
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 );
661 return p + 1;
662 case '"':
663 push_token( parser, p );
664 parser->start = p + 1;
665 push_state( parser, KEY_NAME );
666 set_state( parser, QUOTES );
667 return p + 1;
668 case '\\':
669 push_token( parser, token_end );
670 parser->start = p;
671 push_state( parser, KEY_NAME );
672 set_state( parser, EOL_BACKSLASH );
673 return p;
674 default:
675 if (!isspaceW(*p)) token_end = p + 1;
676 else
678 push_token( parser, p );
679 push_state( parser, KEY_NAME );
680 set_state( parser, TRAILING_SPACES );
681 return p;
683 break;
686 push_token( parser, token_end );
687 set_state( parser, VALUE_NAME );
688 return p;
692 /* handler for parser VALUE_NAME state */
693 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos )
695 const WCHAR *p, *token_end = parser->start;
697 for (p = pos; !is_eol( parser, p ); p++)
699 switch(*p)
701 case ';':
702 push_token( parser, token_end );
703 if (!add_field_from_token( parser, 0 )) return NULL;
704 push_state( parser, LINE_START );
705 set_state( parser, COMMENT );
706 return p + 1;
707 case ',':
708 push_token( parser, token_end );
709 if (!add_field_from_token( parser, 0 )) return NULL;
710 parser->start = p + 1;
711 push_state( parser, VALUE_NAME );
712 set_state( parser, LEADING_SPACES );
713 return p + 1;
714 case '"':
715 push_token( parser, p );
716 parser->start = p + 1;
717 push_state( parser, VALUE_NAME );
718 set_state( parser, QUOTES );
719 return p + 1;
720 case '\\':
721 push_token( parser, token_end );
722 parser->start = p;
723 push_state( parser, VALUE_NAME );
724 set_state( parser, EOL_BACKSLASH );
725 return p;
726 default:
727 if (!isspaceW(*p)) token_end = p + 1;
728 else
730 push_token( parser, p );
731 push_state( parser, VALUE_NAME );
732 set_state( parser, TRAILING_SPACES );
733 return p;
735 break;
738 push_token( parser, token_end );
739 if (!add_field_from_token( parser, 0 )) return NULL;
740 set_state( parser, LINE_START );
741 return p;
745 /* handler for parser EOL_BACKSLASH state */
746 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos )
748 const WCHAR *p;
750 for (p = pos; !is_eof( parser, p ); p++)
752 switch(*p)
754 case '\n':
755 parser->line_pos++;
756 parser->start = p + 1;
757 set_state( parser, LEADING_SPACES );
758 return p + 1;
759 case '\\':
760 continue;
761 case ';':
762 push_state( parser, EOL_BACKSLASH );
763 set_state( parser, COMMENT );
764 return p + 1;
765 default:
766 if (isspaceW(*p)) continue;
767 push_token( parser, p );
768 pop_state( parser );
769 return p;
772 parser->start = p;
773 pop_state( parser );
774 return p;
778 /* handler for parser QUOTES state */
779 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos )
781 const WCHAR *p;
783 for (p = pos; !is_eol( parser, p ); p++)
785 if (*p == '"')
787 if (p+1 < parser->end && p[1] == '"') /* double quotes */
789 push_token( parser, p + 1 );
790 parser->start = p + 2;
791 p++;
793 else /* end of quotes */
795 push_token( parser, p );
796 parser->start = p + 1;
797 pop_state( parser );
798 return p + 1;
802 push_token( parser, p );
803 pop_state( parser );
804 return p;
808 /* handler for parser LEADING_SPACES state */
809 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos )
811 const WCHAR *p;
813 for (p = pos; !is_eol( parser, p ); p++)
815 if (*p == '\\')
817 parser->start = p;
818 set_state( parser, EOL_BACKSLASH );
819 return p;
821 if (!isspaceW(*p)) break;
823 parser->start = p;
824 pop_state( parser );
825 return p;
829 /* handler for parser TRAILING_SPACES state */
830 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos )
832 const WCHAR *p;
834 for (p = pos; !is_eol( parser, p ); p++)
836 if (*p == '\\')
838 set_state( parser, EOL_BACKSLASH );
839 return p;
841 if (!isspaceW(*p)) break;
843 pop_state( parser );
844 return p;
848 /* handler for parser COMMENT state */
849 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos )
851 const WCHAR *p = pos;
853 while (!is_eol( parser, p )) p++;
854 pop_state( parser );
855 return p;
859 /* parse a complete buffer */
860 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
861 UINT *error_line )
863 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
865 struct parser parser;
866 const WCHAR *pos = buffer;
868 parser.start = buffer;
869 parser.end = end;
870 parser.file = file;
871 parser.line = NULL;
872 parser.state = LINE_START;
873 parser.stack_pos = 0;
874 parser.cur_section = -1;
875 parser.line_pos = 1;
876 parser.error = 0;
877 parser.token_len = 0;
879 /* parser main loop */
880 while (pos) pos = (parser_funcs[parser.state])( &parser, pos );
882 /* trim excess buffer space */
883 if (file->alloc_sections > file->nb_sections)
885 file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections,
886 file->nb_sections * sizeof(file->sections[0]) );
887 file->alloc_sections = file->nb_sections;
889 if (file->alloc_fields > file->nb_fields)
891 file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields,
892 file->nb_fields * sizeof(file->fields[0]) );
893 file->alloc_fields = file->nb_fields;
895 file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings,
896 (file->string_pos - file->strings) * sizeof(WCHAR) );
898 if (parser.error)
900 if (error_line) *error_line = parser.line_pos;
901 return parser.error;
904 /* find the [strings] section */
905 file->strings_section = find_section( file, Strings );
906 return 0;
910 /* append a child INF file to its parent list, in a thread-safe manner */
911 static void append_inf_file( struct inf_file *parent, struct inf_file *child )
913 struct inf_file **ppnext = &parent->next;
914 child->next = NULL;
916 for (;;)
918 struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
919 if (!next) return;
920 ppnext = &next->next;
925 /***********************************************************************
926 * parse_file
928 * parse an INF file.
930 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, DWORD style, UINT *error_line )
932 void *buffer;
933 DWORD err = 0;
934 struct inf_file *file;
936 DWORD size = GetFileSize( handle, NULL );
937 HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL );
938 if (!mapping) return NULL;
939 buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size );
940 NtClose( mapping );
941 if (!buffer) return NULL;
943 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
945 if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) )))
947 err = ERROR_NOT_ENOUGH_MEMORY;
948 goto done;
951 /* we won't need more strings space than the size of the file,
952 * so we can preallocate it here
954 if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
956 err = ERROR_NOT_ENOUGH_MEMORY;
957 goto done;
959 file->string_pos = file->strings;
960 file->strings_section = -1;
962 if (!RtlIsTextUnicode( buffer, size, NULL ))
964 static const BYTE utf8_bom[3] = { 0xef, 0xbb, 0xbf };
965 WCHAR *new_buff;
966 UINT codepage = CP_ACP;
967 UINT offset = 0;
969 if (size > sizeof(utf8_bom) && !memcmp( buffer, utf8_bom, sizeof(utf8_bom) ))
971 codepage = CP_UTF8;
972 offset = sizeof(utf8_bom);
975 if ((new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
977 DWORD len = MultiByteToWideChar( codepage, 0, (char *)buffer + offset,
978 size - offset, new_buff, size );
979 err = parse_buffer( file, new_buff, new_buff + len, error_line );
980 HeapFree( GetProcessHeap(), 0, new_buff );
983 else
985 WCHAR *new_buff = buffer;
986 /* UCS-16 files should start with the Unicode BOM; we should skip it */
987 if (*new_buff == 0xfeff)
988 new_buff++;
989 err = parse_buffer( file, new_buff, (WCHAR *)((char *)buffer + size), error_line );
992 if (!err) /* now check signature */
994 int version_index = find_section( file, Version );
995 if (version_index != -1)
997 struct line *line = find_line( file, version_index, Signature );
998 if (line && line->nb_fields > 0)
1000 struct field *field = file->fields + line->first_field;
1001 if (!strcmpiW( field->text, Chicago )) goto done;
1002 if (!strcmpiW( field->text, WindowsNT )) goto done;
1003 if (!strcmpiW( field->text, Windows95 )) goto done;
1006 if (error_line) *error_line = 0;
1007 if (style & INF_STYLE_WIN4) err = ERROR_WRONG_INF_STYLE;
1010 done:
1011 UnmapViewOfFile( buffer );
1012 if (err)
1014 HeapFree( GetProcessHeap(), 0, file );
1015 SetLastError( err );
1016 file = NULL;
1018 return file;
1022 /***********************************************************************
1023 * PARSER_get_inf_filename
1025 * Retrieve the filename of an inf file.
1027 const WCHAR *PARSER_get_inf_filename( HINF hinf )
1029 struct inf_file *file = hinf;
1030 return file->filename;
1034 /***********************************************************************
1035 * PARSER_get_src_root
1037 * Retrieve the source directory of an inf file.
1039 WCHAR *PARSER_get_src_root( HINF hinf )
1041 unsigned int len;
1042 const WCHAR *dir = get_inf_dir( hinf, &len );
1043 WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
1044 if (ret)
1046 memcpy( ret, dir, len * sizeof(WCHAR) );
1047 ret[len] = 0;
1049 return ret;
1053 /***********************************************************************
1054 * PARSER_get_dest_dir
1056 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1057 * returned buffer must be freed by caller.
1059 WCHAR *PARSER_get_dest_dir( INFCONTEXT *context )
1061 const WCHAR *dir;
1062 WCHAR *ptr, *ret;
1063 INT dirid;
1064 unsigned int len1;
1065 DWORD len2;
1067 if (!SetupGetIntField( context, 1, &dirid )) return NULL;
1068 if (!(dir = get_dirid_subst( context->Inf, dirid, &len1 ))) return NULL;
1069 if (!SetupGetStringFieldW( context, 2, NULL, 0, &len2 )) len2 = 0;
1070 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (len1+len2+1) * sizeof(WCHAR) ))) return NULL;
1071 memcpy( ret, dir, len1 * sizeof(WCHAR) );
1072 ptr = ret + len1;
1073 if (len2 && ptr > ret && ptr[-1] != '\\') *ptr++ = '\\';
1074 if (!SetupGetStringFieldW( context, 2, ptr, len2, NULL )) *ptr = 0;
1075 return ret;
1079 /***********************************************************************
1080 * SetupOpenInfFileA (SETUPAPI.@)
1082 HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error )
1084 UNICODE_STRING nameW, classW;
1085 HINF ret = INVALID_HANDLE_VALUE;
1087 classW.Buffer = NULL;
1088 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class ))
1090 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1091 return ret;
1093 if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
1095 ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
1096 RtlFreeUnicodeString( &nameW );
1098 RtlFreeUnicodeString( &classW );
1099 return ret;
1103 /***********************************************************************
1104 * SetupOpenInfFileW (SETUPAPI.@)
1106 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1108 struct inf_file *file = NULL;
1109 HANDLE handle;
1110 WCHAR *path, *p;
1111 UINT len;
1113 if (strchrW( name, '\\' ) || strchrW( name, '/' ))
1115 if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return INVALID_HANDLE_VALUE;
1116 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1118 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1119 return INVALID_HANDLE_VALUE;
1121 GetFullPathNameW( name, len, path, NULL );
1122 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1124 else /* try Windows directory */
1126 static const WCHAR Inf[] = {'\\','i','n','f','\\',0};
1127 static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1129 len = GetWindowsDirectoryW( NULL, 0 ) + strlenW(name) + 12;
1130 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1132 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1133 return INVALID_HANDLE_VALUE;
1135 GetWindowsDirectoryW( path, len );
1136 p = path + strlenW(path);
1137 strcpyW( p, Inf );
1138 strcatW( p, name );
1139 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1140 if (handle == INVALID_HANDLE_VALUE)
1142 strcpyW( p, System32 );
1143 strcatW( p, name );
1144 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1148 if (handle != INVALID_HANDLE_VALUE)
1150 file = parse_file( handle, class, style, error );
1151 CloseHandle( handle );
1153 if (!file)
1155 HeapFree( GetProcessHeap(), 0, path );
1156 return INVALID_HANDLE_VALUE;
1158 TRACE( "%s -> %p\n", debugstr_w(path), file );
1159 file->filename = path;
1160 SetLastError( 0 );
1161 return file;
1165 /***********************************************************************
1166 * SetupOpenAppendInfFileA (SETUPAPI.@)
1168 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
1170 HINF child_hinf;
1172 if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error );
1173 child_hinf = SetupOpenInfFileA( name, NULL, INF_STYLE_WIN4, error );
1174 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1175 append_inf_file( parent_hinf, child_hinf );
1176 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_a(name), child_hinf );
1177 return TRUE;
1181 /***********************************************************************
1182 * SetupOpenAppendInfFileW (SETUPAPI.@)
1184 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1186 HINF child_hinf;
1188 if (!name)
1190 INFCONTEXT context;
1191 WCHAR filename[MAX_PATH];
1192 int idx = 1;
1194 if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE;
1195 while (SetupGetStringFieldW( &context, idx++, filename,
1196 sizeof(filename)/sizeof(WCHAR), NULL ))
1198 child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error );
1199 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1200 append_inf_file( parent_hinf, child_hinf );
1201 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf );
1203 return TRUE;
1205 child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error );
1206 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1207 append_inf_file( parent_hinf, child_hinf );
1208 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf );
1209 return TRUE;
1213 /***********************************************************************
1214 * SetupOpenMasterInf (SETUPAPI.@)
1216 HINF WINAPI SetupOpenMasterInf( VOID )
1218 static const WCHAR Layout[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1219 WCHAR Buffer[MAX_PATH];
1221 GetWindowsDirectoryW( Buffer, MAX_PATH );
1222 strcatW( Buffer, Layout );
1223 return SetupOpenInfFileW( Buffer, NULL, INF_STYLE_WIN4, NULL);
1228 /***********************************************************************
1229 * SetupCloseInfFile (SETUPAPI.@)
1231 void WINAPI SetupCloseInfFile( HINF hinf )
1233 struct inf_file *file = hinf;
1234 unsigned int i;
1236 if (!hinf || (hinf == INVALID_HANDLE_VALUE)) return;
1238 for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
1239 HeapFree( GetProcessHeap(), 0, file->filename );
1240 HeapFree( GetProcessHeap(), 0, file->sections );
1241 HeapFree( GetProcessHeap(), 0, file->fields );
1242 HeapFree( GetProcessHeap(), 0, file->strings );
1243 HeapFree( GetProcessHeap(), 0, file );
1247 /***********************************************************************
1248 * SetupEnumInfSectionsA (SETUPAPI.@)
1250 BOOL WINAPI SetupEnumInfSectionsA( HINF hinf, UINT index, PSTR buffer, DWORD size, DWORD *need )
1252 struct inf_file *file = hinf;
1254 for (file = hinf; file; file = file->next)
1256 if (index < file->nb_sections)
1258 DWORD len = WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1,
1259 NULL, 0, NULL, NULL );
1260 if (need) *need = len;
1261 if (!buffer)
1263 if (!size) return TRUE;
1264 SetLastError( ERROR_INVALID_USER_BUFFER );
1265 return FALSE;
1267 if (len > size)
1269 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1270 return FALSE;
1272 WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1, buffer, size, NULL, NULL );
1273 return TRUE;
1275 index -= file->nb_sections;
1277 SetLastError( ERROR_NO_MORE_ITEMS );
1278 return FALSE;
1282 /***********************************************************************
1283 * SetupEnumInfSectionsW (SETUPAPI.@)
1285 BOOL WINAPI SetupEnumInfSectionsW( HINF hinf, UINT index, PWSTR buffer, DWORD size, DWORD *need )
1287 struct inf_file *file = hinf;
1289 for (file = hinf; file; file = file->next)
1291 if (index < file->nb_sections)
1293 DWORD len = strlenW( file->sections[index]->name ) + 1;
1294 if (need) *need = len;
1295 if (!buffer)
1297 if (!size) return TRUE;
1298 SetLastError( ERROR_INVALID_USER_BUFFER );
1299 return FALSE;
1301 if (len > size)
1303 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1304 return FALSE;
1306 memcpy( buffer, file->sections[index]->name, len * sizeof(WCHAR) );
1307 return TRUE;
1309 index -= file->nb_sections;
1311 SetLastError( ERROR_NO_MORE_ITEMS );
1312 return FALSE;
1316 /***********************************************************************
1317 * SetupGetLineCountA (SETUPAPI.@)
1319 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1321 UNICODE_STRING sectionW;
1322 LONG ret = -1;
1324 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, name ))
1325 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1326 else
1328 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1329 RtlFreeUnicodeString( &sectionW );
1331 return ret;
1335 /***********************************************************************
1336 * SetupGetLineCountW (SETUPAPI.@)
1338 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1340 struct inf_file *file = hinf;
1341 int section_index;
1342 LONG ret = -1;
1344 for (file = hinf; file; file = file->next)
1346 if ((section_index = find_section( file, section )) == -1) continue;
1347 if (ret == -1) ret = 0;
1348 ret += file->sections[section_index]->nb_lines;
1350 TRACE( "(%p,%s) returning %d\n", hinf, debugstr_w(section), ret );
1351 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1352 return ret;
1356 /***********************************************************************
1357 * SetupGetLineByIndexA (SETUPAPI.@)
1359 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1361 UNICODE_STRING sectionW;
1362 BOOL ret = FALSE;
1364 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1365 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1366 else
1368 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1369 RtlFreeUnicodeString( &sectionW );
1371 return ret;
1375 /***********************************************************************
1376 * SetupGetLineByIndexW (SETUPAPI.@)
1378 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1380 struct inf_file *file = hinf;
1381 int section_index;
1383 for (file = hinf; file; file = file->next)
1385 if ((section_index = find_section( file, section )) == -1) continue;
1386 if (index < file->sections[section_index]->nb_lines)
1388 context->Inf = hinf;
1389 context->CurrentInf = file;
1390 context->Section = section_index;
1391 context->Line = index;
1392 SetLastError( 0 );
1393 TRACE( "(%p,%s): returning %d/%d\n",
1394 hinf, debugstr_w(section), section_index, index );
1395 return TRUE;
1397 index -= file->sections[section_index]->nb_lines;
1399 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1400 SetLastError( ERROR_LINE_NOT_FOUND );
1401 return FALSE;
1405 /***********************************************************************
1406 * SetupFindFirstLineA (SETUPAPI.@)
1408 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1410 UNICODE_STRING sectionW, keyW;
1411 BOOL ret = FALSE;
1413 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1415 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1416 return FALSE;
1419 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1420 else
1422 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1424 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1425 RtlFreeUnicodeString( &keyW );
1427 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1429 RtlFreeUnicodeString( &sectionW );
1430 return ret;
1434 /***********************************************************************
1435 * SetupFindFirstLineW (SETUPAPI.@)
1437 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1439 struct inf_file *file;
1440 int section_index;
1442 for (file = hinf; file; file = file->next)
1444 if ((section_index = find_section( file, section )) == -1) continue;
1445 if (key)
1447 INFCONTEXT ctx;
1448 ctx.Inf = hinf;
1449 ctx.CurrentInf = file;
1450 ctx.Section = section_index;
1451 ctx.Line = -1;
1452 return SetupFindNextMatchLineW( &ctx, key, context );
1454 if (file->sections[section_index]->nb_lines)
1456 context->Inf = hinf;
1457 context->CurrentInf = file;
1458 context->Section = section_index;
1459 context->Line = 0;
1460 SetLastError( 0 );
1461 TRACE( "(%p,%s,%s): returning %d/0\n",
1462 hinf, debugstr_w(section), debugstr_w(key), section_index );
1463 return TRUE;
1466 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1467 SetLastError( ERROR_LINE_NOT_FOUND );
1468 return FALSE;
1472 /***********************************************************************
1473 * SetupFindNextLine (SETUPAPI.@)
1475 BOOL WINAPI SetupFindNextLine( PINFCONTEXT context_in, PINFCONTEXT context_out )
1477 struct inf_file *file = context_in->CurrentInf;
1478 struct section *section;
1480 if (context_in->Section >= file->nb_sections) goto error;
1482 section = file->sections[context_in->Section];
1483 if (context_in->Line+1 < section->nb_lines)
1485 if (context_out != context_in) *context_out = *context_in;
1486 context_out->Line++;
1487 SetLastError( 0 );
1488 return TRUE;
1491 /* now search the appended files */
1493 for (file = file->next; file; file = file->next)
1495 int section_index = find_section( file, section->name );
1496 if (section_index == -1) continue;
1497 if (file->sections[section_index]->nb_lines)
1499 context_out->Inf = context_in->Inf;
1500 context_out->CurrentInf = file;
1501 context_out->Section = section_index;
1502 context_out->Line = 0;
1503 SetLastError( 0 );
1504 return TRUE;
1507 error:
1508 SetLastError( ERROR_LINE_NOT_FOUND );
1509 return FALSE;
1513 /***********************************************************************
1514 * SetupFindNextMatchLineA (SETUPAPI.@)
1516 BOOL WINAPI SetupFindNextMatchLineA( PINFCONTEXT context_in, PCSTR key,
1517 PINFCONTEXT context_out )
1519 UNICODE_STRING keyW;
1520 BOOL ret = FALSE;
1522 if (!key) return SetupFindNextLine( context_in, context_out );
1524 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1525 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1526 else
1528 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1529 RtlFreeUnicodeString( &keyW );
1531 return ret;
1535 /***********************************************************************
1536 * SetupFindNextMatchLineW (SETUPAPI.@)
1538 BOOL WINAPI SetupFindNextMatchLineW( PINFCONTEXT context_in, PCWSTR key,
1539 PINFCONTEXT context_out )
1541 struct inf_file *file = context_in->CurrentInf;
1542 struct section *section;
1543 struct line *line;
1544 unsigned int i;
1546 if (!key) return SetupFindNextLine( context_in, context_out );
1548 if (context_in->Section >= file->nb_sections) goto error;
1550 section = file->sections[context_in->Section];
1552 for (i = context_in->Line+1, line = &section->lines[i]; i < section->nb_lines; i++, line++)
1554 if (line->key_field == -1) continue;
1555 if (!strcmpiW( key, file->fields[line->key_field].text ))
1557 if (context_out != context_in) *context_out = *context_in;
1558 context_out->Line = i;
1559 SetLastError( 0 );
1560 TRACE( "(%p,%s,%s): returning %d\n",
1561 file, debugstr_w(section->name), debugstr_w(key), i );
1562 return TRUE;
1566 /* now search the appended files */
1568 for (file = file->next; file; file = file->next)
1570 int section_index = find_section( file, section->name );
1571 if (section_index == -1) continue;
1572 section = file->sections[section_index];
1573 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1575 if (line->key_field == -1) continue;
1576 if (!strcmpiW( key, file->fields[line->key_field].text ))
1578 context_out->Inf = context_in->Inf;
1579 context_out->CurrentInf = file;
1580 context_out->Section = section_index;
1581 context_out->Line = i;
1582 SetLastError( 0 );
1583 TRACE( "(%p,%s,%s): returning %d/%d\n",
1584 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1585 return TRUE;
1589 TRACE( "(%p,%s,%s): not found\n",
1590 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1591 error:
1592 SetLastError( ERROR_LINE_NOT_FOUND );
1593 return FALSE;
1597 /***********************************************************************
1598 * SetupGetLineTextW (SETUPAPI.@)
1600 BOOL WINAPI SetupGetLineTextW( PINFCONTEXT context, HINF hinf, PCWSTR section_name,
1601 PCWSTR key_name, PWSTR buffer, DWORD size, PDWORD required )
1603 struct inf_file *file;
1604 struct line *line;
1605 struct field *field;
1606 int i;
1607 DWORD total = 0;
1609 if (!context)
1611 INFCONTEXT new_context;
1612 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1613 file = new_context.CurrentInf;
1614 line = get_line( file, new_context.Section, new_context.Line );
1616 else
1618 file = context->CurrentInf;
1619 if (!(line = get_line( file, context->Section, context->Line )))
1621 SetLastError( ERROR_LINE_NOT_FOUND );
1622 return FALSE;
1626 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1627 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1629 if (required) *required = total;
1630 if (buffer)
1632 if (total > size)
1634 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1635 return FALSE;
1637 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1639 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1640 if (i+1 < line->nb_fields) buffer[len] = ',';
1641 buffer += len + 1;
1644 return TRUE;
1648 /***********************************************************************
1649 * SetupGetLineTextA (SETUPAPI.@)
1651 BOOL WINAPI SetupGetLineTextA( PINFCONTEXT context, HINF hinf, PCSTR section_name,
1652 PCSTR key_name, PSTR buffer, DWORD size, PDWORD required )
1654 struct inf_file *file;
1655 struct line *line;
1656 struct field *field;
1657 int i;
1658 DWORD total = 0;
1660 if (!context)
1662 INFCONTEXT new_context;
1663 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1664 file = new_context.CurrentInf;
1665 line = get_line( file, new_context.Section, new_context.Line );
1667 else
1669 file = context->CurrentInf;
1670 if (!(line = get_line( file, context->Section, context->Line )))
1672 SetLastError( ERROR_LINE_NOT_FOUND );
1673 return FALSE;
1677 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1678 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1680 if (required) *required = total;
1681 if (buffer)
1683 if (total > size)
1685 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1686 return FALSE;
1688 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1690 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1691 if (i+1 < line->nb_fields) buffer[len] = ',';
1692 buffer += len + 1;
1695 return TRUE;
1699 /***********************************************************************
1700 * SetupGetFieldCount (SETUPAPI.@)
1702 DWORD WINAPI SetupGetFieldCount( PINFCONTEXT context )
1704 struct inf_file *file = context->CurrentInf;
1705 struct line *line = get_line( file, context->Section, context->Line );
1707 if (!line) return 0;
1708 return line->nb_fields;
1712 /***********************************************************************
1713 * SetupGetStringFieldA (SETUPAPI.@)
1715 BOOL WINAPI SetupGetStringFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1716 DWORD size, PDWORD required )
1718 struct inf_file *file = context->CurrentInf;
1719 struct field *field = get_field( file, context->Section, context->Line, index );
1720 unsigned int len;
1722 SetLastError(0);
1723 if (!field) return FALSE;
1724 len = PARSER_string_substA( file, field->text, NULL, 0 );
1725 if (required) *required = len + 1;
1726 if (buffer)
1728 if (size <= len)
1730 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1731 return FALSE;
1733 PARSER_string_substA( file, field->text, buffer, size );
1735 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1736 context->Inf, context->CurrentInf, context->Section, context->Line,
1737 index, debugstr_a(buffer) );
1739 return TRUE;
1743 /***********************************************************************
1744 * SetupGetStringFieldW (SETUPAPI.@)
1746 BOOL WINAPI SetupGetStringFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1747 DWORD size, PDWORD required )
1749 struct inf_file *file = context->CurrentInf;
1750 struct field *field = get_field( file, context->Section, context->Line, index );
1751 unsigned int len;
1753 SetLastError(0);
1754 if (!field) return FALSE;
1755 len = PARSER_string_substW( file, field->text, NULL, 0 );
1756 if (required) *required = len + 1;
1757 if (buffer)
1759 if (size <= len)
1761 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1762 return FALSE;
1764 PARSER_string_substW( file, field->text, buffer, size );
1766 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1767 context->Inf, context->CurrentInf, context->Section, context->Line,
1768 index, debugstr_w(buffer) );
1770 return TRUE;
1774 /***********************************************************************
1775 * SetupGetIntField (SETUPAPI.@)
1777 BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result )
1779 char localbuff[20];
1780 char *end, *buffer = localbuff;
1781 DWORD required;
1782 INT res;
1783 BOOL ret;
1785 if (!(ret = SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required )))
1787 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1788 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1789 if (!(ret = SetupGetStringFieldA( context, index, buffer, required, NULL ))) goto done;
1791 /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */
1792 if (!*buffer) *result = 0;
1793 else
1795 res = strtol( buffer, &end, 0 );
1796 if (end != buffer && !*end) *result = res;
1797 else
1799 SetLastError( ERROR_INVALID_DATA );
1800 ret = FALSE;
1804 done:
1805 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1806 return ret;
1810 /***********************************************************************
1811 * SetupGetBinaryField (SETUPAPI.@)
1813 BOOL WINAPI SetupGetBinaryField( PINFCONTEXT context, DWORD index, BYTE *buffer,
1814 DWORD size, LPDWORD required )
1816 struct inf_file *file = context->CurrentInf;
1817 struct line *line = get_line( file, context->Section, context->Line );
1818 struct field *field;
1819 int i;
1821 if (!line)
1823 SetLastError( ERROR_LINE_NOT_FOUND );
1824 return FALSE;
1826 if (!index || index > line->nb_fields)
1828 SetLastError( ERROR_INVALID_PARAMETER );
1829 return FALSE;
1831 index--; /* fields start at 0 */
1832 if (required) *required = line->nb_fields - index;
1833 if (!buffer) return TRUE;
1834 if (size < line->nb_fields - index)
1836 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1837 return FALSE;
1839 field = &file->fields[line->first_field + index];
1840 for (i = index; i < line->nb_fields; i++, field++)
1842 const WCHAR *p;
1843 DWORD value = 0;
1844 for (p = field->text; *p && isxdigitW(*p); p++)
1846 if ((value <<= 4) > 255)
1848 SetLastError( ERROR_INVALID_DATA );
1849 return FALSE;
1851 if (*p <= '9') value |= (*p - '0');
1852 else value |= (tolowerW(*p) - 'a' + 10);
1854 buffer[i - index] = value;
1856 if (TRACE_ON(setupapi))
1858 TRACE( "%p/%p/%d/%d index %d returning",
1859 context->Inf, context->CurrentInf, context->Section, context->Line, index );
1860 for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] );
1861 TRACE( "\n" );
1863 return TRUE;
1867 /***********************************************************************
1868 * SetupGetMultiSzFieldA (SETUPAPI.@)
1870 BOOL WINAPI SetupGetMultiSzFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1871 DWORD size, LPDWORD required )
1873 struct inf_file *file = context->CurrentInf;
1874 struct line *line = get_line( file, context->Section, context->Line );
1875 struct field *field;
1876 unsigned int len;
1877 int i;
1878 DWORD total = 1;
1880 if (!line)
1882 SetLastError( ERROR_LINE_NOT_FOUND );
1883 return FALSE;
1885 if (!index || index > line->nb_fields)
1887 SetLastError( ERROR_INVALID_PARAMETER );
1888 return FALSE;
1890 index--; /* fields start at 0 */
1891 field = &file->fields[line->first_field + index];
1892 for (i = index; i < line->nb_fields; i++, field++)
1894 if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1895 total += len + 1;
1898 if (required) *required = total;
1899 if (!buffer) return TRUE;
1900 if (total > size)
1902 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1903 return FALSE;
1905 field = &file->fields[line->first_field + index];
1906 for (i = index; i < line->nb_fields; i++, field++)
1908 if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1909 buffer += len + 1;
1911 *buffer = 0; /* add final null */
1912 return TRUE;
1916 /***********************************************************************
1917 * SetupGetMultiSzFieldW (SETUPAPI.@)
1919 BOOL WINAPI SetupGetMultiSzFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1920 DWORD size, LPDWORD required )
1922 struct inf_file *file = context->CurrentInf;
1923 struct line *line = get_line( file, context->Section, context->Line );
1924 struct field *field;
1925 unsigned int len;
1926 int i;
1927 DWORD total = 1;
1929 if (!line)
1931 SetLastError( ERROR_LINE_NOT_FOUND );
1932 return FALSE;
1934 if (!index || index > line->nb_fields)
1936 SetLastError( ERROR_INVALID_PARAMETER );
1937 return FALSE;
1939 index--; /* fields start at 0 */
1940 field = &file->fields[line->first_field + index];
1941 for (i = index; i < line->nb_fields; i++, field++)
1943 if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1944 total += len + 1;
1947 if (required) *required = total;
1948 if (!buffer) return TRUE;
1949 if (total > size)
1951 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1952 return FALSE;
1954 field = &file->fields[line->first_field + index];
1955 for (i = index; i < line->nb_fields; i++, field++)
1957 if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1958 buffer += len + 1;
1960 *buffer = 0; /* add final null */
1961 return TRUE;
1964 /***********************************************************************
1965 * pSetupGetField (SETUPAPI.@)
1967 LPCWSTR WINAPI pSetupGetField( PINFCONTEXT context, DWORD index )
1969 struct inf_file *file = context->CurrentInf;
1970 struct field *field = get_field( file, context->Section, context->Line, index );
1972 if (!field)
1974 SetLastError( ERROR_INVALID_PARAMETER );
1975 return NULL;
1977 return field->text;