mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / setupapi / parser.c
blobc81a712ad840fff6b1db49ee40ae2d73960d9654
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 <assert.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "winerror.h"
35 #include "setupapi.h"
36 #include "setupapi_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
42 #define CONTROL_Z '\x1a'
43 #define MAX_SECTION_NAME_LEN 255
44 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
45 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
46 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
48 /* inf file structure definitions */
50 struct field
52 const WCHAR *text; /* field text */
55 struct line
57 int first_field; /* index of first field in field array */
58 int nb_fields; /* number of fields in line */
59 int key_field; /* index of field for key or -1 if no key */
62 struct section
64 const WCHAR *name; /* section name */
65 unsigned int nb_lines; /* number of used lines */
66 unsigned int alloc_lines; /* total number of allocated lines in array below */
67 struct line lines[16]; /* lines information (grown dynamically, 16 is initial size) */
70 struct inf_file
72 struct inf_file *next; /* next appended file */
73 WCHAR *strings; /* buffer for string data (section names and field values) */
74 WCHAR *string_pos; /* position of next available string in buffer */
75 unsigned int nb_sections; /* number of used sections */
76 unsigned int alloc_sections; /* total number of allocated section pointers */
77 struct section **sections; /* section pointers array */
78 unsigned int nb_fields;
79 unsigned int alloc_fields;
80 struct field *fields;
81 int strings_section; /* index of [Strings] section or -1 if none */
82 WCHAR *filename; /* filename of the INF */
85 /* parser definitions */
87 enum parser_state
89 LINE_START, /* at beginning of a line */
90 SECTION_NAME, /* parsing a section name */
91 KEY_NAME, /* parsing a key name */
92 VALUE_NAME, /* parsing a value name */
93 EOL_BACKSLASH, /* backslash at end of line */
94 QUOTES, /* inside quotes */
95 LEADING_SPACES, /* leading spaces */
96 TRAILING_SPACES, /* trailing spaces */
97 COMMENT, /* inside a comment */
98 NB_PARSER_STATES
101 struct parser
103 const WCHAR *start; /* start position of item being parsed */
104 const WCHAR *end; /* end of buffer */
105 struct inf_file *file; /* file being built */
106 enum parser_state state; /* current parser state */
107 enum parser_state stack[4]; /* state stack */
108 int stack_pos; /* current pos in stack */
110 int cur_section; /* index of section being parsed*/
111 struct line *line; /* current line */
112 unsigned int line_pos; /* current line position in file */
113 unsigned int broken_line; /* first line containing invalid data (if any) */
114 unsigned int error; /* error code */
115 unsigned int token_len; /* current token len */
116 WCHAR token[MAX_FIELD_LEN+1]; /* current token */
119 typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos );
121 /* parser state machine functions */
122 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos );
123 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos );
124 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos );
125 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos );
126 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos );
127 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos );
128 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos );
129 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos );
130 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos );
132 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
134 line_start_state, /* LINE_START */
135 section_name_state, /* SECTION_NAME */
136 key_name_state, /* KEY_NAME */
137 value_name_state, /* VALUE_NAME */
138 eol_backslash_state, /* EOL_BACKSLASH */
139 quotes_state, /* QUOTES */
140 leading_spaces_state, /* LEADING_SPACES */
141 trailing_spaces_state, /* TRAILING_SPACES */
142 comment_state /* COMMENT */
146 /* Unicode string constants */
147 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
148 static const WCHAR Signature[] = {'S','i','g','n','a','t','u','r','e',0};
149 static const WCHAR Chicago[] = {'$','C','h','i','c','a','g','o','$',0};
150 static const WCHAR WindowsNT[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
151 static const WCHAR Windows95[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
152 static const WCHAR LayoutFile[] = {'L','a','y','o','u','t','F','i','l','e',0};
154 /* extend an array, allocating more memory if necessary */
155 static void *grow_array( void *array, unsigned int *count, size_t elem )
157 void *new_array;
158 unsigned int new_count = *count + *count / 2;
159 if (new_count < 32) new_count = 32;
161 if (array)
162 new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem );
163 else
164 new_array = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * elem );
166 if (new_array)
167 *count = new_count;
168 else
169 HeapFree( GetProcessHeap(), 0, array );
170 return new_array;
174 /* get the directory of the inf file (as counted string, not null-terminated) */
175 static const WCHAR *get_inf_dir( const struct inf_file *file, unsigned int *len )
177 const WCHAR *p = wcsrchr( file->filename, '\\' );
178 *len = p ? (p + 1 - file->filename) : 0;
179 return file->filename;
183 /* find a section by name */
184 static int find_section( const struct inf_file *file, const WCHAR *name )
186 unsigned int i;
188 for (i = 0; i < file->nb_sections; i++)
189 if (!wcsicmp( name, file->sections[i]->name )) return i;
190 return -1;
194 /* find a line by name */
195 static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name )
197 struct section *section;
198 struct line *line;
199 unsigned int i;
201 if (section_index < 0 || section_index >= file->nb_sections) return NULL;
202 section = file->sections[section_index];
203 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
205 if (line->key_field == -1) continue;
206 if (!wcsicmp( name, file->fields[line->key_field].text )) return line;
208 return NULL;
212 /* add a section to the file and return the section index */
213 static int add_section( struct inf_file *file, const WCHAR *name )
215 struct section *section;
217 if (file->nb_sections >= file->alloc_sections)
219 if (!(file->sections = grow_array( file->sections, &file->alloc_sections,
220 sizeof(file->sections[0]) ))) return -1;
222 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1;
223 section->name = name;
224 section->nb_lines = 0;
225 section->alloc_lines = ARRAY_SIZE( section->lines );
226 file->sections[file->nb_sections] = section;
227 return file->nb_sections++;
231 /* add a line to a given section */
232 static struct line *add_line( struct inf_file *file, int section_index )
234 struct section *section;
235 struct line *line;
237 assert( section_index >= 0 && section_index < file->nb_sections );
239 section = file->sections[section_index];
240 if (section->nb_lines == section->alloc_lines) /* need to grow the section */
242 int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line);
243 if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL;
244 section->alloc_lines *= 2;
245 file->sections[section_index] = section;
247 line = &section->lines[section->nb_lines++];
248 line->first_field = file->nb_fields;
249 line->nb_fields = 0;
250 line->key_field = -1;
251 return line;
255 /* retrieve a given line from section/line index */
256 static inline struct line *get_line( struct inf_file *file, unsigned int section_index,
257 unsigned int line_index )
259 struct section *section;
261 if (section_index >= file->nb_sections) return NULL;
262 section = file->sections[section_index];
263 if (line_index >= section->nb_lines) return NULL;
264 return &section->lines[line_index];
268 /* retrieve a given field from section/line/field index */
269 static struct field *get_field( struct inf_file *file, int section_index, int line_index,
270 int field_index )
272 struct line *line = get_line( file, section_index, line_index );
274 if (!line) return NULL;
275 if (!field_index) /* get the key */
277 if (line->key_field == -1) return NULL;
278 return &file->fields[line->key_field];
280 field_index--;
281 if (field_index >= line->nb_fields) return NULL;
282 return &file->fields[line->first_field + field_index];
286 /* allocate a new field, growing the array if necessary */
287 static struct field *add_field( struct inf_file *file, const WCHAR *text )
289 struct field *field;
291 if (file->nb_fields >= file->alloc_fields)
293 if (!(file->fields = grow_array( file->fields, &file->alloc_fields,
294 sizeof(file->fields[0]) ))) return NULL;
296 field = &file->fields[file->nb_fields++];
297 field->text = text;
298 return field;
302 /* retrieve the string substitution for a directory id */
303 static const WCHAR *get_dirid_subst( const struct inf_file *file, int dirid, unsigned int *len )
305 const WCHAR *ret;
307 if (dirid == DIRID_SRCPATH) return get_inf_dir( file, len );
308 ret = DIRID_get_string( dirid );
309 if (ret) *len = lstrlenW(ret);
310 return ret;
314 /* retrieve the string substitution for a given string, or NULL if not found */
315 /* if found, len is set to the substitution length */
316 static const WCHAR *get_string_subst( const struct inf_file *file, const WCHAR *str, unsigned int *len,
317 BOOL no_trailing_slash )
319 static const WCHAR percent = '%';
321 struct section *strings_section;
322 struct line *line;
323 struct field *field;
324 unsigned int i;
325 int dirid;
326 WCHAR *dirid_str, *end;
327 const WCHAR *ret = NULL;
329 if (!*len) /* empty string (%%) is replaced by single percent */
331 *len = 1;
332 return &percent;
334 if (file->strings_section == -1) goto not_found;
335 strings_section = file->sections[file->strings_section];
336 for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++)
338 if (line->key_field == -1) continue;
339 if (wcsnicmp( str, file->fields[line->key_field].text, *len )) continue;
340 if (!file->fields[line->key_field].text[*len]) break;
342 if (i == strings_section->nb_lines || !line->nb_fields) goto not_found;
343 field = &file->fields[line->first_field];
344 *len = lstrlenW( field->text );
345 return field->text;
347 not_found: /* check for integer id */
348 if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) )))
350 memcpy( dirid_str, str, *len * sizeof(WCHAR) );
351 dirid_str[*len] = 0;
352 dirid = wcstol( dirid_str, &end, 10 );
353 if (!*end) ret = get_dirid_subst( file, dirid, len );
354 if (no_trailing_slash && ret && *len && ret[*len - 1] == '\\') *len -= 1;
355 HeapFree( GetProcessHeap(), 0, dirid_str );
356 return ret;
358 return NULL;
362 /* do string substitutions on the specified text */
363 /* the buffer is assumed to be large enough */
364 /* returns necessary length not including terminating null */
365 static unsigned int PARSER_string_substW( const struct inf_file *file, const WCHAR *text,
366 WCHAR *buffer, unsigned int size )
368 const WCHAR *start, *subst, *p;
369 unsigned int len, total = 0;
370 BOOL inside = FALSE;
372 if (!buffer) size = MAX_STRING_LEN + 1;
373 for (p = start = text; *p; p++)
375 if (*p != '%') continue;
376 inside = !inside;
377 if (inside) /* start of a %xx% string */
379 len = p - start;
380 if (len > size - 1) len = size - 1;
381 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
382 total += len;
383 size -= len;
384 start = p;
386 else /* end of the %xx% string, find substitution */
388 len = p - start - 1;
389 subst = get_string_subst( file, start + 1, &len, p[1] == '\\' );
390 if (!subst)
392 subst = start;
393 len = p - start + 1;
395 if (len > size - 1) len = size - 1;
396 if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
397 total += len;
398 size -= len;
399 start = p + 1;
403 if (start != p) /* unfinished string, copy it */
405 len = p - start;
406 if (len > size - 1) len = size - 1;
407 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
408 total += len;
410 if (buffer && size) buffer[total] = 0;
411 return total;
415 /* do string substitutions on the specified text */
416 /* the buffer is assumed to be large enough */
417 /* returns necessary length not including terminating null */
418 static unsigned int PARSER_string_substA( const struct inf_file *file, const WCHAR *text,
419 char *buffer, unsigned int size )
421 WCHAR buffW[MAX_STRING_LEN+1];
422 DWORD ret;
424 unsigned int len = PARSER_string_substW( file, text, buffW, ARRAY_SIZE( buffW ));
425 if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
426 else
428 RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
429 buffer[ret] = 0;
431 return ret;
435 /* push some string data into the strings buffer */
436 static WCHAR *push_string( struct inf_file *file, const WCHAR *string )
438 WCHAR *ret = file->string_pos;
439 lstrcpyW( ret, string );
440 file->string_pos += lstrlenW( ret ) + 1;
441 return ret;
445 /* push the current state on the parser stack */
446 static inline void push_state( struct parser *parser, enum parser_state state )
448 assert( parser->stack_pos < ARRAY_SIZE( parser->stack ));
449 parser->stack[parser->stack_pos++] = state;
453 /* pop the current state */
454 static inline void pop_state( struct parser *parser )
456 assert( parser->stack_pos );
457 parser->state = parser->stack[--parser->stack_pos];
461 /* set the parser state and return the previous one */
462 static inline enum parser_state set_state( struct parser *parser, enum parser_state state )
464 enum parser_state ret = parser->state;
465 parser->state = state;
466 return ret;
470 /* check if the pointer points to an end of file */
471 static inline BOOL is_eof( const struct parser *parser, const WCHAR *ptr )
473 return (ptr >= parser->end || *ptr == CONTROL_Z);
477 /* check if the pointer points to an end of line */
478 static inline BOOL is_eol( const struct parser *parser, const WCHAR *ptr )
480 return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n');
484 /* push data from current token start up to pos into the current token */
485 static int push_token( struct parser *parser, const WCHAR *pos )
487 int len = pos - parser->start;
488 const WCHAR *src = parser->start;
489 WCHAR *dst = parser->token + parser->token_len;
491 if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len;
493 parser->token_len += len;
494 for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' ';
495 *dst = 0;
496 parser->start = pos;
497 return 0;
501 /* add a section with the current token as name */
502 static int add_section_from_token( struct parser *parser )
504 int section_index;
506 if (parser->token_len > MAX_SECTION_NAME_LEN)
508 parser->error = ERROR_SECTION_NAME_TOO_LONG;
509 return -1;
511 if ((section_index = find_section( parser->file, parser->token )) == -1)
513 /* need to create a new one */
514 const WCHAR *name = push_string( parser->file, parser->token );
515 if ((section_index = add_section( parser->file, name )) == -1)
517 parser->error = ERROR_NOT_ENOUGH_MEMORY;
518 return -1;
521 parser->token_len = 0;
522 parser->cur_section = section_index;
523 return section_index;
527 /* add a field containing the current token to the current line */
528 static struct field *add_field_from_token( struct parser *parser, BOOL is_key )
530 struct field *field;
531 WCHAR *text;
533 if (!parser->line) /* need to start a new line */
535 if (parser->cur_section == -1) /* got a line before the first section */
537 parser->error = ERROR_EXPECTED_SECTION_NAME;
538 return NULL;
540 if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error;
542 else assert(!is_key);
544 text = push_string( parser->file, parser->token );
545 if ((field = add_field( parser->file, text )))
547 if (!is_key) parser->line->nb_fields++;
548 else
550 /* replace first field by key field */
551 parser->line->key_field = parser->line->first_field;
552 parser->line->first_field++;
554 parser->token_len = 0;
555 return field;
557 error:
558 parser->error = ERROR_NOT_ENOUGH_MEMORY;
559 return NULL;
563 /* close the current line and prepare for parsing a new one */
564 static void close_current_line( struct parser *parser )
566 struct line *cur_line = parser->line;
568 if (cur_line)
570 /* if line has a single field and no key, the field is the key too */
571 if (cur_line->nb_fields == 1 && cur_line->key_field == -1)
572 cur_line->key_field = cur_line->first_field;
574 parser->line = NULL;
578 /* handler for parser LINE_START state */
579 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
581 const WCHAR *p;
583 for (p = pos; !is_eof( parser, p ); p++)
585 switch(*p)
587 case '\n':
588 parser->line_pos++;
589 close_current_line( parser );
590 break;
591 case ';':
592 push_state( parser, LINE_START );
593 set_state( parser, COMMENT );
594 return p + 1;
595 case '[':
596 parser->start = p + 1;
597 set_state( parser, SECTION_NAME );
598 return p + 1;
599 default:
600 if (iswspace(*p)) break;
601 if (parser->cur_section != -1)
603 parser->start = p;
604 set_state( parser, KEY_NAME );
605 return p;
607 if (!parser->broken_line)
608 parser->broken_line = parser->line_pos;
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, TRUE )) 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, FALSE )) 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 (!iswspace(*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, FALSE )) 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, FALSE )) 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 (*p && !iswspace(*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, FALSE )) 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 (iswspace(*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 (!iswspace(*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 (*p && !iswspace(*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 static void free_inf_file( struct inf_file *file )
861 unsigned int i;
863 for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
864 HeapFree( GetProcessHeap(), 0, file->filename );
865 HeapFree( GetProcessHeap(), 0, file->sections );
866 HeapFree( GetProcessHeap(), 0, file->fields );
867 HeapFree( GetProcessHeap(), 0, file->strings );
868 HeapFree( GetProcessHeap(), 0, file );
872 /* parse a complete buffer */
873 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
874 UINT *error_line )
876 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
878 struct parser parser;
879 const WCHAR *pos = buffer;
881 parser.start = buffer;
882 parser.end = end;
883 parser.file = file;
884 parser.line = NULL;
885 parser.state = LINE_START;
886 parser.stack_pos = 0;
887 parser.cur_section = -1;
888 parser.line_pos = 1;
889 parser.broken_line = 0;
890 parser.error = 0;
891 parser.token_len = 0;
893 /* parser main loop */
894 while (pos) pos = (parser_funcs[parser.state])( &parser, pos );
896 /* trim excess buffer space */
897 if (file->alloc_sections > file->nb_sections)
899 file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections,
900 file->nb_sections * sizeof(file->sections[0]) );
901 file->alloc_sections = file->nb_sections;
903 if (file->alloc_fields > file->nb_fields)
905 file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields,
906 file->nb_fields * sizeof(file->fields[0]) );
907 file->alloc_fields = file->nb_fields;
909 file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings,
910 (file->string_pos - file->strings) * sizeof(WCHAR) );
912 if (parser.error)
914 if (error_line) *error_line = parser.line_pos;
915 return parser.error;
918 /* find the [strings] section */
919 file->strings_section = find_section( file, Strings );
921 if (file->strings_section == -1 && parser.broken_line)
923 if (error_line) *error_line = parser.broken_line;
924 return ERROR_EXPECTED_SECTION_NAME;
927 return 0;
931 /* append a child INF file to its parent list, in a thread-safe manner */
932 static void append_inf_file( struct inf_file *parent, struct inf_file *child )
934 struct inf_file **ppnext = &parent->next;
935 child->next = NULL;
937 for (;;)
939 struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
940 if (!next) return;
941 ppnext = &next->next;
946 /***********************************************************************
947 * parse_file
949 * parse an INF file.
951 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, DWORD style, UINT *error_line )
953 void *buffer;
954 DWORD err = 0;
955 struct inf_file *file;
957 DWORD size = GetFileSize( handle, NULL );
958 HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL );
959 if (!mapping) return NULL;
960 buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size );
961 NtClose( mapping );
962 if (!buffer) return NULL;
964 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
966 if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) )))
968 err = ERROR_NOT_ENOUGH_MEMORY;
969 goto done;
972 /* we won't need more strings space than the size of the file,
973 * so we can preallocate it here
975 if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
977 err = ERROR_NOT_ENOUGH_MEMORY;
978 goto done;
980 file->string_pos = file->strings;
981 file->strings_section = -1;
983 if (!RtlIsTextUnicode( buffer, size, NULL ))
985 static const BYTE utf8_bom[3] = { 0xef, 0xbb, 0xbf };
986 WCHAR *new_buff;
987 UINT codepage = CP_ACP;
988 UINT offset = 0;
990 if (size > sizeof(utf8_bom) && !memcmp( buffer, utf8_bom, sizeof(utf8_bom) ))
992 codepage = CP_UTF8;
993 offset = sizeof(utf8_bom);
996 if ((new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
998 DWORD len = MultiByteToWideChar( codepage, 0, (char *)buffer + offset,
999 size - offset, new_buff, size );
1000 err = parse_buffer( file, new_buff, new_buff + len, error_line );
1001 HeapFree( GetProcessHeap(), 0, new_buff );
1004 else
1006 WCHAR *new_buff = buffer;
1007 /* UCS-16 files should start with the Unicode BOM; we should skip it */
1008 if (*new_buff == 0xfeff)
1009 new_buff++;
1010 err = parse_buffer( file, new_buff, (WCHAR *)((char *)buffer + size), error_line );
1013 if (!err) /* now check signature */
1015 int version_index = find_section( file, Version );
1016 if (version_index != -1)
1018 struct line *line = find_line( file, version_index, Signature );
1019 if (line && line->nb_fields > 0)
1021 struct field *field = file->fields + line->first_field;
1022 if (!wcsicmp( field->text, Chicago )) goto done;
1023 if (!wcsicmp( field->text, WindowsNT )) goto done;
1024 if (!wcsicmp( field->text, Windows95 )) goto done;
1027 if (error_line) *error_line = 0;
1028 if (style & INF_STYLE_WIN4) err = ERROR_WRONG_INF_STYLE;
1031 done:
1032 UnmapViewOfFile( buffer );
1033 if (err)
1035 if (file) free_inf_file( file );
1036 SetLastError( err );
1037 file = NULL;
1039 return file;
1043 /***********************************************************************
1044 * PARSER_get_inf_filename
1046 * Retrieve the filename of an inf file.
1048 const WCHAR *PARSER_get_inf_filename( HINF hinf )
1050 struct inf_file *file = hinf;
1051 return file->filename;
1054 /***********************************************************************
1055 * PARSER_get_dest_dir
1057 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1058 * returned buffer must be freed by caller.
1060 WCHAR *PARSER_get_dest_dir( INFCONTEXT *context )
1062 const WCHAR *dir;
1063 WCHAR *ptr, *ret;
1064 INT dirid;
1065 unsigned int len1;
1066 DWORD len2;
1068 if (!SetupGetIntField( context, 1, &dirid )) return NULL;
1069 if (!(dir = get_dirid_subst( context->Inf, dirid, &len1 ))) return NULL;
1070 if (!SetupGetStringFieldW( context, 2, NULL, 0, &len2 )) len2 = 0;
1071 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (len1+len2+1) * sizeof(WCHAR) ))) return NULL;
1072 memcpy( ret, dir, len1 * sizeof(WCHAR) );
1073 ptr = ret + len1;
1074 if (len2 && ptr > ret && ptr[-1] != '\\') *ptr++ = '\\';
1075 if (!SetupGetStringFieldW( context, 2, ptr, len2, NULL )) *ptr = 0;
1076 return ret;
1080 /***********************************************************************
1081 * SetupOpenInfFileA (SETUPAPI.@)
1083 HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error )
1085 UNICODE_STRING nameW, classW;
1086 HINF ret = INVALID_HANDLE_VALUE;
1088 classW.Buffer = NULL;
1089 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class ))
1091 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1092 return ret;
1094 if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
1096 ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
1097 RtlFreeUnicodeString( &nameW );
1099 RtlFreeUnicodeString( &classW );
1100 return ret;
1104 /***********************************************************************
1105 * SetupOpenInfFileW (SETUPAPI.@)
1107 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1109 struct inf_file *file = NULL;
1110 HANDLE handle;
1111 WCHAR *path, *p;
1112 UINT len;
1114 if (wcschr( name, '\\' ) || wcschr( name, '/' ))
1116 if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return INVALID_HANDLE_VALUE;
1117 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1119 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1120 return INVALID_HANDLE_VALUE;
1122 GetFullPathNameW( name, len, path, NULL );
1123 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1125 else /* try Windows directory */
1127 static const WCHAR Inf[] = {'\\','i','n','f','\\',0};
1128 static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1130 len = GetWindowsDirectoryW( NULL, 0 ) + lstrlenW(name) + 12;
1131 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1133 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1134 return INVALID_HANDLE_VALUE;
1136 GetWindowsDirectoryW( path, len );
1137 p = path + lstrlenW(path);
1138 lstrcpyW( p, Inf );
1139 lstrcatW( p, name );
1140 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1141 if (handle == INVALID_HANDLE_VALUE)
1143 lstrcpyW( p, System32 );
1144 lstrcatW( p, name );
1145 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1149 if (handle != INVALID_HANDLE_VALUE)
1151 file = parse_file( handle, class, style, error );
1152 CloseHandle( handle );
1154 if (!file)
1156 HeapFree( GetProcessHeap(), 0, path );
1157 return INVALID_HANDLE_VALUE;
1159 TRACE( "%s -> %p\n", debugstr_w(path), file );
1160 file->filename = path;
1161 SetLastError( 0 );
1162 return file;
1166 /***********************************************************************
1167 * SetupOpenAppendInfFileA (SETUPAPI.@)
1169 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
1171 HINF child_hinf;
1173 if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error );
1174 child_hinf = SetupOpenInfFileA( name, NULL, INF_STYLE_WIN4, error );
1175 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1176 append_inf_file( parent_hinf, child_hinf );
1177 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_a(name), child_hinf );
1178 return TRUE;
1182 /***********************************************************************
1183 * SetupOpenAppendInfFileW (SETUPAPI.@)
1185 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1187 HINF child_hinf;
1189 if (!name)
1191 INFCONTEXT context;
1192 WCHAR filename[MAX_PATH];
1193 int idx = 1;
1195 if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE;
1196 while (SetupGetStringFieldW( &context, idx++, filename, ARRAY_SIZE( filename ), 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 lstrcatW( 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;
1235 if (!hinf || (hinf == INVALID_HANDLE_VALUE)) return;
1237 free_inf_file( file );
1241 /***********************************************************************
1242 * SetupEnumInfSectionsA (SETUPAPI.@)
1244 BOOL WINAPI SetupEnumInfSectionsA( HINF hinf, UINT index, PSTR buffer, DWORD size, DWORD *need )
1246 struct inf_file *file = hinf;
1248 for (file = hinf; file; file = file->next)
1250 if (index < file->nb_sections)
1252 DWORD len = WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1,
1253 NULL, 0, NULL, NULL );
1254 if (need) *need = len;
1255 if (!buffer)
1257 if (!size) return TRUE;
1258 SetLastError( ERROR_INVALID_USER_BUFFER );
1259 return FALSE;
1261 if (len > size)
1263 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1264 return FALSE;
1266 WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1, buffer, size, NULL, NULL );
1267 return TRUE;
1269 index -= file->nb_sections;
1271 SetLastError( ERROR_NO_MORE_ITEMS );
1272 return FALSE;
1276 /***********************************************************************
1277 * SetupEnumInfSectionsW (SETUPAPI.@)
1279 BOOL WINAPI SetupEnumInfSectionsW( HINF hinf, UINT index, PWSTR buffer, DWORD size, DWORD *need )
1281 struct inf_file *file = hinf;
1283 for (file = hinf; file; file = file->next)
1285 if (index < file->nb_sections)
1287 DWORD len = lstrlenW( file->sections[index]->name ) + 1;
1288 if (need) *need = len;
1289 if (!buffer)
1291 if (!size) return TRUE;
1292 SetLastError( ERROR_INVALID_USER_BUFFER );
1293 return FALSE;
1295 if (len > size)
1297 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1298 return FALSE;
1300 memcpy( buffer, file->sections[index]->name, len * sizeof(WCHAR) );
1301 return TRUE;
1303 index -= file->nb_sections;
1305 SetLastError( ERROR_NO_MORE_ITEMS );
1306 return FALSE;
1310 /***********************************************************************
1311 * SetupGetLineCountA (SETUPAPI.@)
1313 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1315 UNICODE_STRING sectionW;
1316 LONG ret = -1;
1318 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, name ))
1319 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1320 else
1322 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1323 RtlFreeUnicodeString( &sectionW );
1325 return ret;
1329 /***********************************************************************
1330 * SetupGetLineCountW (SETUPAPI.@)
1332 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1334 struct inf_file *file = hinf;
1335 int section_index;
1336 LONG ret = -1;
1338 for (file = hinf; file; file = file->next)
1340 if ((section_index = find_section( file, section )) == -1) continue;
1341 if (ret == -1) ret = 0;
1342 ret += file->sections[section_index]->nb_lines;
1344 TRACE( "(%p,%s) returning %d\n", hinf, debugstr_w(section), ret );
1345 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1346 return ret;
1350 /***********************************************************************
1351 * SetupGetLineByIndexA (SETUPAPI.@)
1353 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1355 UNICODE_STRING sectionW;
1356 BOOL ret = FALSE;
1358 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1359 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1360 else
1362 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1363 RtlFreeUnicodeString( &sectionW );
1365 return ret;
1369 /***********************************************************************
1370 * SetupGetLineByIndexW (SETUPAPI.@)
1372 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1374 struct inf_file *file = hinf;
1375 int section_index;
1377 for (file = hinf; file; file = file->next)
1379 if ((section_index = find_section( file, section )) == -1) continue;
1380 if (index < file->sections[section_index]->nb_lines)
1382 context->Inf = hinf;
1383 context->CurrentInf = file;
1384 context->Section = section_index;
1385 context->Line = index;
1386 SetLastError( 0 );
1387 TRACE( "(%p,%s): returning %d/%d\n",
1388 hinf, debugstr_w(section), section_index, index );
1389 return TRUE;
1391 index -= file->sections[section_index]->nb_lines;
1393 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1394 SetLastError( ERROR_LINE_NOT_FOUND );
1395 return FALSE;
1399 /***********************************************************************
1400 * SetupFindFirstLineA (SETUPAPI.@)
1402 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1404 UNICODE_STRING sectionW, keyW;
1405 BOOL ret = FALSE;
1407 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1409 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1410 return FALSE;
1413 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1414 else
1416 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1418 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1419 RtlFreeUnicodeString( &keyW );
1421 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1423 RtlFreeUnicodeString( &sectionW );
1424 return ret;
1428 /***********************************************************************
1429 * SetupFindFirstLineW (SETUPAPI.@)
1431 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1433 struct inf_file *file;
1434 int section_index;
1436 for (file = hinf; file; file = file->next)
1438 if ((section_index = find_section( file, section )) == -1) continue;
1439 if (key)
1441 INFCONTEXT ctx;
1442 ctx.Inf = hinf;
1443 ctx.CurrentInf = file;
1444 ctx.Section = section_index;
1445 ctx.Line = -1;
1446 return SetupFindNextMatchLineW( &ctx, key, context );
1448 if (file->sections[section_index]->nb_lines)
1450 context->Inf = hinf;
1451 context->CurrentInf = file;
1452 context->Section = section_index;
1453 context->Line = 0;
1454 SetLastError( 0 );
1455 TRACE( "(%p,%s,%s): returning %d/0\n",
1456 hinf, debugstr_w(section), debugstr_w(key), section_index );
1457 return TRUE;
1460 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1461 SetLastError( ERROR_LINE_NOT_FOUND );
1462 return FALSE;
1466 /***********************************************************************
1467 * SetupFindNextLine (SETUPAPI.@)
1469 BOOL WINAPI SetupFindNextLine( PINFCONTEXT context_in, PINFCONTEXT context_out )
1471 struct inf_file *file = context_in->CurrentInf;
1472 struct section *section;
1474 if (context_in->Section >= file->nb_sections) goto error;
1476 section = file->sections[context_in->Section];
1477 if (context_in->Line+1 < section->nb_lines)
1479 if (context_out != context_in) *context_out = *context_in;
1480 context_out->Line++;
1481 SetLastError( 0 );
1482 return TRUE;
1485 /* now search the appended files */
1487 for (file = file->next; file; file = file->next)
1489 int section_index = find_section( file, section->name );
1490 if (section_index == -1) continue;
1491 if (file->sections[section_index]->nb_lines)
1493 context_out->Inf = context_in->Inf;
1494 context_out->CurrentInf = file;
1495 context_out->Section = section_index;
1496 context_out->Line = 0;
1497 SetLastError( 0 );
1498 return TRUE;
1501 error:
1502 SetLastError( ERROR_LINE_NOT_FOUND );
1503 return FALSE;
1507 /***********************************************************************
1508 * SetupFindNextMatchLineA (SETUPAPI.@)
1510 BOOL WINAPI SetupFindNextMatchLineA( PINFCONTEXT context_in, PCSTR key,
1511 PINFCONTEXT context_out )
1513 UNICODE_STRING keyW;
1514 BOOL ret = FALSE;
1516 if (!key) return SetupFindNextLine( context_in, context_out );
1518 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1519 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1520 else
1522 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1523 RtlFreeUnicodeString( &keyW );
1525 return ret;
1529 /***********************************************************************
1530 * SetupFindNextMatchLineW (SETUPAPI.@)
1532 BOOL WINAPI SetupFindNextMatchLineW( PINFCONTEXT context_in, PCWSTR key,
1533 PINFCONTEXT context_out )
1535 struct inf_file *file = context_in->CurrentInf;
1536 WCHAR buffer[MAX_STRING_LEN + 1];
1537 struct section *section;
1538 struct line *line;
1539 unsigned int i;
1541 if (!key) return SetupFindNextLine( context_in, context_out );
1543 if (context_in->Section >= file->nb_sections) goto error;
1545 section = file->sections[context_in->Section];
1547 for (i = context_in->Line+1, line = &section->lines[i]; i < section->nb_lines; i++, line++)
1549 if (line->key_field == -1) continue;
1550 PARSER_string_substW( file, file->fields[line->key_field].text, buffer, ARRAY_SIZE(buffer) );
1551 if (!wcsicmp( key, buffer ))
1553 if (context_out != context_in) *context_out = *context_in;
1554 context_out->Line = i;
1555 SetLastError( 0 );
1556 TRACE( "(%p,%s,%s): returning %d\n",
1557 file, debugstr_w(section->name), debugstr_w(key), i );
1558 return TRUE;
1562 /* now search the appended files */
1564 for (file = file->next; file; file = file->next)
1566 int section_index = find_section( file, section->name );
1567 if (section_index == -1) continue;
1568 section = file->sections[section_index];
1569 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1571 if (line->key_field == -1) continue;
1572 if (!wcsicmp( key, file->fields[line->key_field].text ))
1574 context_out->Inf = context_in->Inf;
1575 context_out->CurrentInf = file;
1576 context_out->Section = section_index;
1577 context_out->Line = i;
1578 SetLastError( 0 );
1579 TRACE( "(%p,%s,%s): returning %d/%d\n",
1580 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1581 return TRUE;
1585 TRACE( "(%p,%s,%s): not found\n",
1586 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1587 error:
1588 SetLastError( ERROR_LINE_NOT_FOUND );
1589 return FALSE;
1593 /***********************************************************************
1594 * SetupGetLineTextW (SETUPAPI.@)
1596 BOOL WINAPI SetupGetLineTextW( PINFCONTEXT context, HINF hinf, PCWSTR section_name,
1597 PCWSTR key_name, PWSTR buffer, DWORD size, PDWORD required )
1599 struct inf_file *file;
1600 struct line *line;
1601 struct field *field;
1602 int i;
1603 DWORD total = 0;
1605 if (!context)
1607 INFCONTEXT new_context;
1608 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1609 file = new_context.CurrentInf;
1610 line = get_line( file, new_context.Section, new_context.Line );
1612 else
1614 file = context->CurrentInf;
1615 if (!(line = get_line( file, context->Section, context->Line )))
1617 SetLastError( ERROR_LINE_NOT_FOUND );
1618 return FALSE;
1622 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1623 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1625 if (required) *required = total;
1626 if (buffer)
1628 if (total > size)
1630 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1631 return FALSE;
1633 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1635 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1636 if (i+1 < line->nb_fields) buffer[len] = ',';
1637 buffer += len + 1;
1640 return TRUE;
1644 /***********************************************************************
1645 * SetupGetLineTextA (SETUPAPI.@)
1647 BOOL WINAPI SetupGetLineTextA( PINFCONTEXT context, HINF hinf, PCSTR section_name,
1648 PCSTR key_name, PSTR buffer, DWORD size, PDWORD required )
1650 struct inf_file *file;
1651 struct line *line;
1652 struct field *field;
1653 int i;
1654 DWORD total = 0;
1656 if (!context)
1658 INFCONTEXT new_context;
1659 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1660 file = new_context.CurrentInf;
1661 line = get_line( file, new_context.Section, new_context.Line );
1663 else
1665 file = context->CurrentInf;
1666 if (!(line = get_line( file, context->Section, context->Line )))
1668 SetLastError( ERROR_LINE_NOT_FOUND );
1669 return FALSE;
1673 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1674 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1676 if (required) *required = total;
1677 if (buffer)
1679 if (total > size)
1681 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1682 return FALSE;
1684 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1686 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1687 if (i+1 < line->nb_fields) buffer[len] = ',';
1688 buffer += len + 1;
1691 return TRUE;
1695 /***********************************************************************
1696 * SetupGetFieldCount (SETUPAPI.@)
1698 DWORD WINAPI SetupGetFieldCount( PINFCONTEXT context )
1700 struct inf_file *file = context->CurrentInf;
1701 struct line *line = get_line( file, context->Section, context->Line );
1703 if (!line) return 0;
1704 return line->nb_fields;
1708 /***********************************************************************
1709 * SetupGetStringFieldA (SETUPAPI.@)
1711 BOOL WINAPI SetupGetStringFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1712 DWORD size, PDWORD required )
1714 struct inf_file *file = context->CurrentInf;
1715 struct field *field = get_field( file, context->Section, context->Line, index );
1716 unsigned int len;
1718 SetLastError(0);
1719 if (!field) return FALSE;
1720 len = PARSER_string_substA( file, field->text, NULL, 0 );
1721 if (required) *required = len + 1;
1722 if (buffer)
1724 if (size <= len)
1726 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1727 return FALSE;
1729 PARSER_string_substA( file, field->text, buffer, size );
1731 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1732 context->Inf, context->CurrentInf, context->Section, context->Line,
1733 index, debugstr_a(buffer) );
1735 return TRUE;
1739 /***********************************************************************
1740 * SetupGetStringFieldW (SETUPAPI.@)
1742 BOOL WINAPI SetupGetStringFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1743 DWORD size, PDWORD required )
1745 struct inf_file *file = context->CurrentInf;
1746 struct field *field = get_field( file, context->Section, context->Line, index );
1747 unsigned int len;
1749 SetLastError(0);
1750 if (!field) return FALSE;
1751 len = PARSER_string_substW( file, field->text, NULL, 0 );
1752 if (required) *required = len + 1;
1753 if (buffer)
1755 if (size <= len)
1757 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1758 return FALSE;
1760 PARSER_string_substW( file, field->text, buffer, size );
1762 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1763 context->Inf, context->CurrentInf, context->Section, context->Line,
1764 index, debugstr_w(buffer) );
1766 return TRUE;
1770 /***********************************************************************
1771 * SetupGetIntField (SETUPAPI.@)
1773 BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result )
1775 char localbuff[20];
1776 char *end, *buffer = localbuff;
1777 DWORD required;
1778 INT res;
1779 BOOL ret;
1781 if (!(ret = SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required )))
1783 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1784 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1785 if (!(ret = SetupGetStringFieldA( context, index, buffer, required, NULL ))) goto done;
1787 /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */
1788 if (!*buffer) *result = 0;
1789 else
1791 res = strtol( buffer, &end, 0 );
1792 if (end != buffer && !*end) *result = res;
1793 else
1795 SetLastError( ERROR_INVALID_DATA );
1796 ret = FALSE;
1800 done:
1801 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1802 return ret;
1806 static int xdigit_to_int(WCHAR c)
1808 if ('0' <= c && c <= '9') return c - '0';
1809 if ('a' <= c && c <= 'f') return c - 'a' + 10;
1810 if ('A' <= c && c <= 'F') return c - 'A' + 10;
1811 return -1;
1815 /***********************************************************************
1816 * SetupGetBinaryField (SETUPAPI.@)
1818 BOOL WINAPI SetupGetBinaryField( PINFCONTEXT context, DWORD index, BYTE *buffer,
1819 DWORD size, LPDWORD required )
1821 struct inf_file *file = context->CurrentInf;
1822 struct line *line = get_line( file, context->Section, context->Line );
1823 struct field *field;
1824 int i;
1826 if (!line)
1828 SetLastError( ERROR_LINE_NOT_FOUND );
1829 return FALSE;
1831 if (!index || index > line->nb_fields)
1833 SetLastError( ERROR_INVALID_PARAMETER );
1834 return FALSE;
1836 index--; /* fields start at 0 */
1837 if (required) *required = line->nb_fields - index;
1838 if (!buffer) return TRUE;
1839 if (size < line->nb_fields - index)
1841 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1842 return FALSE;
1844 field = &file->fields[line->first_field + index];
1845 for (i = index; i < line->nb_fields; i++, field++)
1847 const WCHAR *p;
1848 DWORD value = 0;
1849 int d;
1850 for (p = field->text; *p && (d = xdigit_to_int(*p)) != -1; p++)
1852 if ((value <<= 4) > 255)
1854 SetLastError( ERROR_INVALID_DATA );
1855 return FALSE;
1857 value |= d;
1859 buffer[i - index] = value;
1861 TRACE( "%p/%p/%d/%d index %d\n",
1862 context->Inf, context->CurrentInf, context->Section, context->Line, index );
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;