gphoto2.ds: Set supported groups.
[wine.git] / dlls / setupapi / parser.c
blob5cc3ef58ced55b637f3a5e800eb321e2f5ff5a9e
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 broken_line; /* first line containing invalid data (if any) */
118 unsigned int error; /* error code */
119 unsigned int token_len; /* current token len */
120 WCHAR token[MAX_FIELD_LEN+1]; /* current token */
123 typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos );
125 /* parser state machine functions */
126 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos );
127 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos );
128 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos );
129 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos );
130 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos );
131 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos );
132 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos );
133 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos );
134 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos );
136 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
138 line_start_state, /* LINE_START */
139 section_name_state, /* SECTION_NAME */
140 key_name_state, /* KEY_NAME */
141 value_name_state, /* VALUE_NAME */
142 eol_backslash_state, /* EOL_BACKSLASH */
143 quotes_state, /* QUOTES */
144 leading_spaces_state, /* LEADING_SPACES */
145 trailing_spaces_state, /* TRAILING_SPACES */
146 comment_state /* COMMENT */
150 /* Unicode string constants */
151 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
152 static const WCHAR Signature[] = {'S','i','g','n','a','t','u','r','e',0};
153 static const WCHAR Chicago[] = {'$','C','h','i','c','a','g','o','$',0};
154 static const WCHAR WindowsNT[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
155 static const WCHAR Windows95[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
156 static const WCHAR LayoutFile[] = {'L','a','y','o','u','t','F','i','l','e',0};
158 /* extend an array, allocating more memory if necessary */
159 static void *grow_array( void *array, unsigned int *count, size_t elem )
161 void *new_array;
162 unsigned int new_count = *count + *count / 2;
163 if (new_count < 32) new_count = 32;
165 if (array)
166 new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem );
167 else
168 new_array = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * elem );
170 if (new_array)
171 *count = new_count;
172 else
173 HeapFree( GetProcessHeap(), 0, array );
174 return new_array;
178 /* get the directory of the inf file (as counted string, not null-terminated) */
179 static const WCHAR *get_inf_dir( const struct inf_file *file, unsigned int *len )
181 const WCHAR *p = strrchrW( file->filename, '\\' );
182 *len = p ? (p + 1 - file->filename) : 0;
183 return file->filename;
187 /* find a section by name */
188 static int find_section( const struct inf_file *file, const WCHAR *name )
190 unsigned int i;
192 for (i = 0; i < file->nb_sections; i++)
193 if (!strcmpiW( name, file->sections[i]->name )) return i;
194 return -1;
198 /* find a line by name */
199 static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name )
201 struct section *section;
202 struct line *line;
203 unsigned int i;
205 if (section_index < 0 || section_index >= file->nb_sections) return NULL;
206 section = file->sections[section_index];
207 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
209 if (line->key_field == -1) continue;
210 if (!strcmpiW( name, file->fields[line->key_field].text )) return line;
212 return NULL;
216 /* add a section to the file and return the section index */
217 static int add_section( struct inf_file *file, const WCHAR *name )
219 struct section *section;
221 if (file->nb_sections >= file->alloc_sections)
223 if (!(file->sections = grow_array( file->sections, &file->alloc_sections,
224 sizeof(file->sections[0]) ))) return -1;
226 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1;
227 section->name = name;
228 section->nb_lines = 0;
229 section->alloc_lines = sizeof(section->lines)/sizeof(section->lines[0]);
230 file->sections[file->nb_sections] = section;
231 return file->nb_sections++;
235 /* add a line to a given section */
236 static struct line *add_line( struct inf_file *file, int section_index )
238 struct section *section;
239 struct line *line;
241 assert( section_index >= 0 && section_index < file->nb_sections );
243 section = file->sections[section_index];
244 if (section->nb_lines == section->alloc_lines) /* need to grow the section */
246 int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line);
247 if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL;
248 section->alloc_lines *= 2;
249 file->sections[section_index] = section;
251 line = &section->lines[section->nb_lines++];
252 line->first_field = file->nb_fields;
253 line->nb_fields = 0;
254 line->key_field = -1;
255 return line;
259 /* retrieve a given line from section/line index */
260 static inline struct line *get_line( struct inf_file *file, unsigned int section_index,
261 unsigned int line_index )
263 struct section *section;
265 if (section_index >= file->nb_sections) return NULL;
266 section = file->sections[section_index];
267 if (line_index >= section->nb_lines) return NULL;
268 return &section->lines[line_index];
272 /* retrieve a given field from section/line/field index */
273 static struct field *get_field( struct inf_file *file, int section_index, int line_index,
274 int field_index )
276 struct line *line = get_line( file, section_index, line_index );
278 if (!line) return NULL;
279 if (!field_index) /* get the key */
281 if (line->key_field == -1) return NULL;
282 return &file->fields[line->key_field];
284 field_index--;
285 if (field_index >= line->nb_fields) return NULL;
286 return &file->fields[line->first_field + field_index];
290 /* allocate a new field, growing the array if necessary */
291 static struct field *add_field( struct inf_file *file, const WCHAR *text )
293 struct field *field;
295 if (file->nb_fields >= file->alloc_fields)
297 if (!(file->fields = grow_array( file->fields, &file->alloc_fields,
298 sizeof(file->fields[0]) ))) return NULL;
300 field = &file->fields[file->nb_fields++];
301 field->text = text;
302 return field;
306 /* retrieve the string substitution for a directory id */
307 static const WCHAR *get_dirid_subst( const struct inf_file *file, int dirid, unsigned int *len )
309 const WCHAR *ret;
311 if (dirid == DIRID_SRCPATH) return get_inf_dir( file, len );
312 ret = DIRID_get_string( dirid );
313 if (ret) *len = strlenW(ret);
314 return ret;
318 /* retrieve the string substitution for a given string, or NULL if not found */
319 /* if found, len is set to the substitution length */
320 static const WCHAR *get_string_subst( const struct inf_file *file, const WCHAR *str, unsigned int *len,
321 BOOL no_trailing_slash )
323 static const WCHAR percent = '%';
325 struct section *strings_section;
326 struct line *line;
327 struct field *field;
328 unsigned int i;
329 int dirid;
330 WCHAR *dirid_str, *end;
331 const WCHAR *ret = NULL;
333 if (!*len) /* empty string (%%) is replaced by single percent */
335 *len = 1;
336 return &percent;
338 if (file->strings_section == -1) goto not_found;
339 strings_section = file->sections[file->strings_section];
340 for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++)
342 if (line->key_field == -1) continue;
343 if (strncmpiW( str, file->fields[line->key_field].text, *len )) continue;
344 if (!file->fields[line->key_field].text[*len]) break;
346 if (i == strings_section->nb_lines || !line->nb_fields) goto not_found;
347 field = &file->fields[line->first_field];
348 *len = strlenW( field->text );
349 return field->text;
351 not_found: /* check for integer id */
352 if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) )))
354 memcpy( dirid_str, str, *len * sizeof(WCHAR) );
355 dirid_str[*len] = 0;
356 dirid = strtolW( dirid_str, &end, 10 );
357 if (!*end) ret = get_dirid_subst( file, dirid, len );
358 if (no_trailing_slash && ret && *len && ret[*len - 1] == '\\') *len -= 1;
359 HeapFree( GetProcessHeap(), 0, dirid_str );
360 return ret;
362 return NULL;
366 /* do string substitutions on the specified text */
367 /* the buffer is assumed to be large enough */
368 /* returns necessary length not including terminating null */
369 static unsigned int PARSER_string_substW( const struct inf_file *file, const WCHAR *text,
370 WCHAR *buffer, unsigned int size )
372 const WCHAR *start, *subst, *p;
373 unsigned int len, total = 0;
374 BOOL inside = FALSE;
376 if (!buffer) size = MAX_STRING_LEN + 1;
377 for (p = start = text; *p; p++)
379 if (*p != '%') continue;
380 inside = !inside;
381 if (inside) /* start of a %xx% string */
383 len = p - start;
384 if (len > size - 1) len = size - 1;
385 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
386 total += len;
387 size -= len;
388 start = p;
390 else /* end of the %xx% string, find substitution */
392 len = p - start - 1;
393 subst = get_string_subst( file, start + 1, &len, p[1] == '\\' );
394 if (!subst)
396 subst = start;
397 len = p - start + 1;
399 if (len > size - 1) len = size - 1;
400 if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
401 total += len;
402 size -= len;
403 start = p + 1;
407 if (start != p) /* unfinished string, copy it */
409 len = p - start;
410 if (len > size - 1) len = size - 1;
411 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
412 total += len;
414 if (buffer && size) buffer[total] = 0;
415 return total;
419 /* do string substitutions on the specified text */
420 /* the buffer is assumed to be large enough */
421 /* returns necessary length not including terminating null */
422 static unsigned int PARSER_string_substA( const struct inf_file *file, const WCHAR *text,
423 char *buffer, unsigned int size )
425 WCHAR buffW[MAX_STRING_LEN+1];
426 DWORD ret;
428 unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) );
429 if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
430 else
432 RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
433 buffer[ret] = 0;
435 return ret;
439 /* push some string data into the strings buffer */
440 static WCHAR *push_string( struct inf_file *file, const WCHAR *string )
442 WCHAR *ret = file->string_pos;
443 strcpyW( ret, string );
444 file->string_pos += strlenW( ret ) + 1;
445 return ret;
449 /* push the current state on the parser stack */
450 static inline void push_state( struct parser *parser, enum parser_state state )
452 assert( parser->stack_pos < sizeof(parser->stack)/sizeof(parser->stack[0]) );
453 parser->stack[parser->stack_pos++] = state;
457 /* pop the current state */
458 static inline void pop_state( struct parser *parser )
460 assert( parser->stack_pos );
461 parser->state = parser->stack[--parser->stack_pos];
465 /* set the parser state and return the previous one */
466 static inline enum parser_state set_state( struct parser *parser, enum parser_state state )
468 enum parser_state ret = parser->state;
469 parser->state = state;
470 return ret;
474 /* check if the pointer points to an end of file */
475 static inline BOOL is_eof( const struct parser *parser, const WCHAR *ptr )
477 return (ptr >= parser->end || *ptr == CONTROL_Z);
481 /* check if the pointer points to an end of line */
482 static inline BOOL is_eol( const struct parser *parser, const WCHAR *ptr )
484 return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n');
488 /* push data from current token start up to pos into the current token */
489 static int push_token( struct parser *parser, const WCHAR *pos )
491 int len = pos - parser->start;
492 const WCHAR *src = parser->start;
493 WCHAR *dst = parser->token + parser->token_len;
495 if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len;
497 parser->token_len += len;
498 for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' ';
499 *dst = 0;
500 parser->start = pos;
501 return 0;
505 /* add a section with the current token as name */
506 static int add_section_from_token( struct parser *parser )
508 int section_index;
510 if (parser->token_len > MAX_SECTION_NAME_LEN)
512 parser->error = ERROR_SECTION_NAME_TOO_LONG;
513 return -1;
515 if ((section_index = find_section( parser->file, parser->token )) == -1)
517 /* need to create a new one */
518 const WCHAR *name = push_string( parser->file, parser->token );
519 if ((section_index = add_section( parser->file, name )) == -1)
521 parser->error = ERROR_NOT_ENOUGH_MEMORY;
522 return -1;
525 parser->token_len = 0;
526 parser->cur_section = section_index;
527 return section_index;
531 /* add a field containing the current token to the current line */
532 static struct field *add_field_from_token( struct parser *parser, BOOL is_key )
534 struct field *field;
535 WCHAR *text;
537 if (!parser->line) /* need to start a new line */
539 if (parser->cur_section == -1) /* got a line before the first section */
541 parser->error = ERROR_EXPECTED_SECTION_NAME;
542 return NULL;
544 if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error;
546 else assert(!is_key);
548 text = push_string( parser->file, parser->token );
549 if ((field = add_field( parser->file, text )))
551 if (!is_key) parser->line->nb_fields++;
552 else
554 /* replace first field by key field */
555 parser->line->key_field = parser->line->first_field;
556 parser->line->first_field++;
558 parser->token_len = 0;
559 return field;
561 error:
562 parser->error = ERROR_NOT_ENOUGH_MEMORY;
563 return NULL;
567 /* close the current line and prepare for parsing a new one */
568 static void close_current_line( struct parser *parser )
570 struct line *cur_line = parser->line;
572 if (cur_line)
574 /* if line has a single field and no key, the field is the key too */
575 if (cur_line->nb_fields == 1 && cur_line->key_field == -1)
576 cur_line->key_field = cur_line->first_field;
578 parser->line = NULL;
582 /* handler for parser LINE_START state */
583 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
585 const WCHAR *p;
587 for (p = pos; !is_eof( parser, p ); p++)
589 switch(*p)
591 case '\n':
592 parser->line_pos++;
593 close_current_line( parser );
594 break;
595 case ';':
596 push_state( parser, LINE_START );
597 set_state( parser, COMMENT );
598 return p + 1;
599 case '[':
600 parser->start = p + 1;
601 set_state( parser, SECTION_NAME );
602 return p + 1;
603 default:
604 if (isspaceW(*p)) break;
605 if (parser->cur_section != -1)
607 parser->start = p;
608 set_state( parser, KEY_NAME );
609 return p;
611 if (!parser->broken_line)
612 parser->broken_line = parser->line_pos;
613 break;
616 close_current_line( parser );
617 return NULL;
621 /* handler for parser SECTION_NAME state */
622 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
624 const WCHAR *p;
626 for (p = pos; !is_eol( parser, p ); p++)
628 if (*p == ']')
630 push_token( parser, p );
631 if (add_section_from_token( parser ) == -1) return NULL;
632 push_state( parser, LINE_START );
633 set_state( parser, COMMENT ); /* ignore everything else on the line */
634 return p + 1;
637 parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */
638 return NULL;
642 /* handler for parser KEY_NAME state */
643 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos )
645 const WCHAR *p, *token_end = parser->start;
647 for (p = pos; !is_eol( parser, p ); p++)
649 if (*p == ',') break;
650 switch(*p)
653 case '=':
654 push_token( parser, token_end );
655 if (!add_field_from_token( parser, TRUE )) return NULL;
656 parser->start = p + 1;
657 push_state( parser, VALUE_NAME );
658 set_state( parser, LEADING_SPACES );
659 return p + 1;
660 case ';':
661 push_token( parser, token_end );
662 if (!add_field_from_token( parser, FALSE )) return NULL;
663 push_state( parser, LINE_START );
664 set_state( parser, COMMENT );
665 return p + 1;
666 case '"':
667 push_token( parser, p );
668 parser->start = p + 1;
669 push_state( parser, KEY_NAME );
670 set_state( parser, QUOTES );
671 return p + 1;
672 case '\\':
673 push_token( parser, token_end );
674 parser->start = p;
675 push_state( parser, KEY_NAME );
676 set_state( parser, EOL_BACKSLASH );
677 return p;
678 default:
679 if (!isspaceW(*p)) token_end = p + 1;
680 else
682 push_token( parser, p );
683 push_state( parser, KEY_NAME );
684 set_state( parser, TRAILING_SPACES );
685 return p;
687 break;
690 push_token( parser, token_end );
691 set_state( parser, VALUE_NAME );
692 return p;
696 /* handler for parser VALUE_NAME state */
697 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos )
699 const WCHAR *p, *token_end = parser->start;
701 for (p = pos; !is_eol( parser, p ); p++)
703 switch(*p)
705 case ';':
706 push_token( parser, token_end );
707 if (!add_field_from_token( parser, FALSE )) return NULL;
708 push_state( parser, LINE_START );
709 set_state( parser, COMMENT );
710 return p + 1;
711 case ',':
712 push_token( parser, token_end );
713 if (!add_field_from_token( parser, FALSE )) return NULL;
714 parser->start = p + 1;
715 push_state( parser, VALUE_NAME );
716 set_state( parser, LEADING_SPACES );
717 return p + 1;
718 case '"':
719 push_token( parser, p );
720 parser->start = p + 1;
721 push_state( parser, VALUE_NAME );
722 set_state( parser, QUOTES );
723 return p + 1;
724 case '\\':
725 push_token( parser, token_end );
726 parser->start = p;
727 push_state( parser, VALUE_NAME );
728 set_state( parser, EOL_BACKSLASH );
729 return p;
730 default:
731 if (!isspaceW(*p)) token_end = p + 1;
732 else
734 push_token( parser, p );
735 push_state( parser, VALUE_NAME );
736 set_state( parser, TRAILING_SPACES );
737 return p;
739 break;
742 push_token( parser, token_end );
743 if (!add_field_from_token( parser, FALSE )) return NULL;
744 set_state( parser, LINE_START );
745 return p;
749 /* handler for parser EOL_BACKSLASH state */
750 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos )
752 const WCHAR *p;
754 for (p = pos; !is_eof( parser, p ); p++)
756 switch(*p)
758 case '\n':
759 parser->line_pos++;
760 parser->start = p + 1;
761 set_state( parser, LEADING_SPACES );
762 return p + 1;
763 case '\\':
764 continue;
765 case ';':
766 push_state( parser, EOL_BACKSLASH );
767 set_state( parser, COMMENT );
768 return p + 1;
769 default:
770 if (isspaceW(*p)) continue;
771 push_token( parser, p );
772 pop_state( parser );
773 return p;
776 parser->start = p;
777 pop_state( parser );
778 return p;
782 /* handler for parser QUOTES state */
783 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos )
785 const WCHAR *p;
787 for (p = pos; !is_eol( parser, p ); p++)
789 if (*p == '"')
791 if (p+1 < parser->end && p[1] == '"') /* double quotes */
793 push_token( parser, p + 1 );
794 parser->start = p + 2;
795 p++;
797 else /* end of quotes */
799 push_token( parser, p );
800 parser->start = p + 1;
801 pop_state( parser );
802 return p + 1;
806 push_token( parser, p );
807 pop_state( parser );
808 return p;
812 /* handler for parser LEADING_SPACES state */
813 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos )
815 const WCHAR *p;
817 for (p = pos; !is_eol( parser, p ); p++)
819 if (*p == '\\')
821 parser->start = p;
822 set_state( parser, EOL_BACKSLASH );
823 return p;
825 if (!isspaceW(*p)) break;
827 parser->start = p;
828 pop_state( parser );
829 return p;
833 /* handler for parser TRAILING_SPACES state */
834 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos )
836 const WCHAR *p;
838 for (p = pos; !is_eol( parser, p ); p++)
840 if (*p == '\\')
842 set_state( parser, EOL_BACKSLASH );
843 return p;
845 if (!isspaceW(*p)) break;
847 pop_state( parser );
848 return p;
852 /* handler for parser COMMENT state */
853 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos )
855 const WCHAR *p = pos;
857 while (!is_eol( parser, p )) p++;
858 pop_state( parser );
859 return p;
863 static void free_inf_file( struct inf_file *file )
865 unsigned int i;
867 for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
868 HeapFree( GetProcessHeap(), 0, file->filename );
869 HeapFree( GetProcessHeap(), 0, file->sections );
870 HeapFree( GetProcessHeap(), 0, file->fields );
871 HeapFree( GetProcessHeap(), 0, file->strings );
872 HeapFree( GetProcessHeap(), 0, file );
876 /* parse a complete buffer */
877 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
878 UINT *error_line )
880 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
882 struct parser parser;
883 const WCHAR *pos = buffer;
885 parser.start = buffer;
886 parser.end = end;
887 parser.file = file;
888 parser.line = NULL;
889 parser.state = LINE_START;
890 parser.stack_pos = 0;
891 parser.cur_section = -1;
892 parser.line_pos = 1;
893 parser.broken_line = 0;
894 parser.error = 0;
895 parser.token_len = 0;
897 /* parser main loop */
898 while (pos) pos = (parser_funcs[parser.state])( &parser, pos );
900 /* trim excess buffer space */
901 if (file->alloc_sections > file->nb_sections)
903 file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections,
904 file->nb_sections * sizeof(file->sections[0]) );
905 file->alloc_sections = file->nb_sections;
907 if (file->alloc_fields > file->nb_fields)
909 file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields,
910 file->nb_fields * sizeof(file->fields[0]) );
911 file->alloc_fields = file->nb_fields;
913 file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings,
914 (file->string_pos - file->strings) * sizeof(WCHAR) );
916 if (parser.error)
918 if (error_line) *error_line = parser.line_pos;
919 return parser.error;
922 /* find the [strings] section */
923 file->strings_section = find_section( file, Strings );
925 if (file->strings_section == -1 && parser.broken_line)
927 if (error_line) *error_line = parser.broken_line;
928 return ERROR_EXPECTED_SECTION_NAME;
931 return 0;
935 /* append a child INF file to its parent list, in a thread-safe manner */
936 static void append_inf_file( struct inf_file *parent, struct inf_file *child )
938 struct inf_file **ppnext = &parent->next;
939 child->next = NULL;
941 for (;;)
943 struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
944 if (!next) return;
945 ppnext = &next->next;
950 /***********************************************************************
951 * parse_file
953 * parse an INF file.
955 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, DWORD style, UINT *error_line )
957 void *buffer;
958 DWORD err = 0;
959 struct inf_file *file;
961 DWORD size = GetFileSize( handle, NULL );
962 HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL );
963 if (!mapping) return NULL;
964 buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size );
965 NtClose( mapping );
966 if (!buffer) return NULL;
968 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
970 if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) )))
972 err = ERROR_NOT_ENOUGH_MEMORY;
973 goto done;
976 /* we won't need more strings space than the size of the file,
977 * so we can preallocate it here
979 if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
981 err = ERROR_NOT_ENOUGH_MEMORY;
982 goto done;
984 file->string_pos = file->strings;
985 file->strings_section = -1;
987 if (!RtlIsTextUnicode( buffer, size, NULL ))
989 static const BYTE utf8_bom[3] = { 0xef, 0xbb, 0xbf };
990 WCHAR *new_buff;
991 UINT codepage = CP_ACP;
992 UINT offset = 0;
994 if (size > sizeof(utf8_bom) && !memcmp( buffer, utf8_bom, sizeof(utf8_bom) ))
996 codepage = CP_UTF8;
997 offset = sizeof(utf8_bom);
1000 if ((new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
1002 DWORD len = MultiByteToWideChar( codepage, 0, (char *)buffer + offset,
1003 size - offset, new_buff, size );
1004 err = parse_buffer( file, new_buff, new_buff + len, error_line );
1005 HeapFree( GetProcessHeap(), 0, new_buff );
1008 else
1010 WCHAR *new_buff = buffer;
1011 /* UCS-16 files should start with the Unicode BOM; we should skip it */
1012 if (*new_buff == 0xfeff)
1013 new_buff++;
1014 err = parse_buffer( file, new_buff, (WCHAR *)((char *)buffer + size), error_line );
1017 if (!err) /* now check signature */
1019 int version_index = find_section( file, Version );
1020 if (version_index != -1)
1022 struct line *line = find_line( file, version_index, Signature );
1023 if (line && line->nb_fields > 0)
1025 struct field *field = file->fields + line->first_field;
1026 if (!strcmpiW( field->text, Chicago )) goto done;
1027 if (!strcmpiW( field->text, WindowsNT )) goto done;
1028 if (!strcmpiW( field->text, Windows95 )) goto done;
1031 if (error_line) *error_line = 0;
1032 if (style & INF_STYLE_WIN4) err = ERROR_WRONG_INF_STYLE;
1035 done:
1036 UnmapViewOfFile( buffer );
1037 if (err)
1039 if (file) free_inf_file( file );
1040 SetLastError( err );
1041 file = NULL;
1043 return file;
1047 /***********************************************************************
1048 * PARSER_get_inf_filename
1050 * Retrieve the filename of an inf file.
1052 const WCHAR *PARSER_get_inf_filename( HINF hinf )
1054 struct inf_file *file = hinf;
1055 return file->filename;
1059 /***********************************************************************
1060 * PARSER_get_src_root
1062 * Retrieve the source directory of an inf file.
1064 WCHAR *PARSER_get_src_root( HINF hinf )
1066 unsigned int len;
1067 const WCHAR *dir = get_inf_dir( hinf, &len );
1068 WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
1069 if (ret)
1071 memcpy( ret, dir, len * sizeof(WCHAR) );
1072 ret[len] = 0;
1074 return ret;
1078 /***********************************************************************
1079 * PARSER_get_dest_dir
1081 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1082 * returned buffer must be freed by caller.
1084 WCHAR *PARSER_get_dest_dir( INFCONTEXT *context )
1086 const WCHAR *dir;
1087 WCHAR *ptr, *ret;
1088 INT dirid;
1089 unsigned int len1;
1090 DWORD len2;
1092 if (!SetupGetIntField( context, 1, &dirid )) return NULL;
1093 if (!(dir = get_dirid_subst( context->Inf, dirid, &len1 ))) return NULL;
1094 if (!SetupGetStringFieldW( context, 2, NULL, 0, &len2 )) len2 = 0;
1095 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (len1+len2+1) * sizeof(WCHAR) ))) return NULL;
1096 memcpy( ret, dir, len1 * sizeof(WCHAR) );
1097 ptr = ret + len1;
1098 if (len2 && ptr > ret && ptr[-1] != '\\') *ptr++ = '\\';
1099 if (!SetupGetStringFieldW( context, 2, ptr, len2, NULL )) *ptr = 0;
1100 return ret;
1104 /***********************************************************************
1105 * SetupOpenInfFileA (SETUPAPI.@)
1107 HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error )
1109 UNICODE_STRING nameW, classW;
1110 HINF ret = INVALID_HANDLE_VALUE;
1112 classW.Buffer = NULL;
1113 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class ))
1115 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1116 return ret;
1118 if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
1120 ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
1121 RtlFreeUnicodeString( &nameW );
1123 RtlFreeUnicodeString( &classW );
1124 return ret;
1128 /***********************************************************************
1129 * SetupOpenInfFileW (SETUPAPI.@)
1131 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1133 struct inf_file *file = NULL;
1134 HANDLE handle;
1135 WCHAR *path, *p;
1136 UINT len;
1138 if (strchrW( name, '\\' ) || strchrW( name, '/' ))
1140 if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return INVALID_HANDLE_VALUE;
1141 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1143 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1144 return INVALID_HANDLE_VALUE;
1146 GetFullPathNameW( name, len, path, NULL );
1147 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1149 else /* try Windows directory */
1151 static const WCHAR Inf[] = {'\\','i','n','f','\\',0};
1152 static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1154 len = GetWindowsDirectoryW( NULL, 0 ) + strlenW(name) + 12;
1155 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1157 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1158 return INVALID_HANDLE_VALUE;
1160 GetWindowsDirectoryW( path, len );
1161 p = path + strlenW(path);
1162 strcpyW( p, Inf );
1163 strcatW( p, name );
1164 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1165 if (handle == INVALID_HANDLE_VALUE)
1167 strcpyW( p, System32 );
1168 strcatW( p, name );
1169 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1173 if (handle != INVALID_HANDLE_VALUE)
1175 file = parse_file( handle, class, style, error );
1176 CloseHandle( handle );
1178 if (!file)
1180 HeapFree( GetProcessHeap(), 0, path );
1181 return INVALID_HANDLE_VALUE;
1183 TRACE( "%s -> %p\n", debugstr_w(path), file );
1184 file->filename = path;
1185 SetLastError( 0 );
1186 return file;
1190 /***********************************************************************
1191 * SetupOpenAppendInfFileA (SETUPAPI.@)
1193 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
1195 HINF child_hinf;
1197 if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error );
1198 child_hinf = SetupOpenInfFileA( name, 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_a(name), child_hinf );
1202 return TRUE;
1206 /***********************************************************************
1207 * SetupOpenAppendInfFileW (SETUPAPI.@)
1209 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1211 HINF child_hinf;
1213 if (!name)
1215 INFCONTEXT context;
1216 WCHAR filename[MAX_PATH];
1217 int idx = 1;
1219 if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE;
1220 while (SetupGetStringFieldW( &context, idx++, filename,
1221 sizeof(filename)/sizeof(WCHAR), NULL ))
1223 child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error );
1224 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1225 append_inf_file( parent_hinf, child_hinf );
1226 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf );
1228 return TRUE;
1230 child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error );
1231 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1232 append_inf_file( parent_hinf, child_hinf );
1233 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf );
1234 return TRUE;
1238 /***********************************************************************
1239 * SetupOpenMasterInf (SETUPAPI.@)
1241 HINF WINAPI SetupOpenMasterInf( VOID )
1243 static const WCHAR Layout[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1244 WCHAR Buffer[MAX_PATH];
1246 GetWindowsDirectoryW( Buffer, MAX_PATH );
1247 strcatW( Buffer, Layout );
1248 return SetupOpenInfFileW( Buffer, NULL, INF_STYLE_WIN4, NULL);
1253 /***********************************************************************
1254 * SetupCloseInfFile (SETUPAPI.@)
1256 void WINAPI SetupCloseInfFile( HINF hinf )
1258 struct inf_file *file = hinf;
1260 if (!hinf || (hinf == INVALID_HANDLE_VALUE)) return;
1262 free_inf_file( file );
1266 /***********************************************************************
1267 * SetupEnumInfSectionsA (SETUPAPI.@)
1269 BOOL WINAPI SetupEnumInfSectionsA( HINF hinf, UINT index, PSTR buffer, DWORD size, DWORD *need )
1271 struct inf_file *file = hinf;
1273 for (file = hinf; file; file = file->next)
1275 if (index < file->nb_sections)
1277 DWORD len = WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1,
1278 NULL, 0, NULL, NULL );
1279 if (need) *need = len;
1280 if (!buffer)
1282 if (!size) return TRUE;
1283 SetLastError( ERROR_INVALID_USER_BUFFER );
1284 return FALSE;
1286 if (len > size)
1288 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1289 return FALSE;
1291 WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1, buffer, size, NULL, NULL );
1292 return TRUE;
1294 index -= file->nb_sections;
1296 SetLastError( ERROR_NO_MORE_ITEMS );
1297 return FALSE;
1301 /***********************************************************************
1302 * SetupEnumInfSectionsW (SETUPAPI.@)
1304 BOOL WINAPI SetupEnumInfSectionsW( HINF hinf, UINT index, PWSTR buffer, DWORD size, DWORD *need )
1306 struct inf_file *file = hinf;
1308 for (file = hinf; file; file = file->next)
1310 if (index < file->nb_sections)
1312 DWORD len = strlenW( file->sections[index]->name ) + 1;
1313 if (need) *need = len;
1314 if (!buffer)
1316 if (!size) return TRUE;
1317 SetLastError( ERROR_INVALID_USER_BUFFER );
1318 return FALSE;
1320 if (len > size)
1322 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1323 return FALSE;
1325 memcpy( buffer, file->sections[index]->name, len * sizeof(WCHAR) );
1326 return TRUE;
1328 index -= file->nb_sections;
1330 SetLastError( ERROR_NO_MORE_ITEMS );
1331 return FALSE;
1335 /***********************************************************************
1336 * SetupGetLineCountA (SETUPAPI.@)
1338 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1340 UNICODE_STRING sectionW;
1341 LONG ret = -1;
1343 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, name ))
1344 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1345 else
1347 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1348 RtlFreeUnicodeString( &sectionW );
1350 return ret;
1354 /***********************************************************************
1355 * SetupGetLineCountW (SETUPAPI.@)
1357 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1359 struct inf_file *file = hinf;
1360 int section_index;
1361 LONG ret = -1;
1363 for (file = hinf; file; file = file->next)
1365 if ((section_index = find_section( file, section )) == -1) continue;
1366 if (ret == -1) ret = 0;
1367 ret += file->sections[section_index]->nb_lines;
1369 TRACE( "(%p,%s) returning %d\n", hinf, debugstr_w(section), ret );
1370 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1371 return ret;
1375 /***********************************************************************
1376 * SetupGetLineByIndexA (SETUPAPI.@)
1378 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1380 UNICODE_STRING sectionW;
1381 BOOL ret = FALSE;
1383 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1384 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1385 else
1387 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1388 RtlFreeUnicodeString( &sectionW );
1390 return ret;
1394 /***********************************************************************
1395 * SetupGetLineByIndexW (SETUPAPI.@)
1397 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1399 struct inf_file *file = hinf;
1400 int section_index;
1402 for (file = hinf; file; file = file->next)
1404 if ((section_index = find_section( file, section )) == -1) continue;
1405 if (index < file->sections[section_index]->nb_lines)
1407 context->Inf = hinf;
1408 context->CurrentInf = file;
1409 context->Section = section_index;
1410 context->Line = index;
1411 SetLastError( 0 );
1412 TRACE( "(%p,%s): returning %d/%d\n",
1413 hinf, debugstr_w(section), section_index, index );
1414 return TRUE;
1416 index -= file->sections[section_index]->nb_lines;
1418 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1419 SetLastError( ERROR_LINE_NOT_FOUND );
1420 return FALSE;
1424 /***********************************************************************
1425 * SetupFindFirstLineA (SETUPAPI.@)
1427 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1429 UNICODE_STRING sectionW, keyW;
1430 BOOL ret = FALSE;
1432 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1434 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1435 return FALSE;
1438 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1439 else
1441 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1443 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1444 RtlFreeUnicodeString( &keyW );
1446 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1448 RtlFreeUnicodeString( &sectionW );
1449 return ret;
1453 /***********************************************************************
1454 * SetupFindFirstLineW (SETUPAPI.@)
1456 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1458 struct inf_file *file;
1459 int section_index;
1461 for (file = hinf; file; file = file->next)
1463 if ((section_index = find_section( file, section )) == -1) continue;
1464 if (key)
1466 INFCONTEXT ctx;
1467 ctx.Inf = hinf;
1468 ctx.CurrentInf = file;
1469 ctx.Section = section_index;
1470 ctx.Line = -1;
1471 return SetupFindNextMatchLineW( &ctx, key, context );
1473 if (file->sections[section_index]->nb_lines)
1475 context->Inf = hinf;
1476 context->CurrentInf = file;
1477 context->Section = section_index;
1478 context->Line = 0;
1479 SetLastError( 0 );
1480 TRACE( "(%p,%s,%s): returning %d/0\n",
1481 hinf, debugstr_w(section), debugstr_w(key), section_index );
1482 return TRUE;
1485 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1486 SetLastError( ERROR_LINE_NOT_FOUND );
1487 return FALSE;
1491 /***********************************************************************
1492 * SetupFindNextLine (SETUPAPI.@)
1494 BOOL WINAPI SetupFindNextLine( PINFCONTEXT context_in, PINFCONTEXT context_out )
1496 struct inf_file *file = context_in->CurrentInf;
1497 struct section *section;
1499 if (context_in->Section >= file->nb_sections) goto error;
1501 section = file->sections[context_in->Section];
1502 if (context_in->Line+1 < section->nb_lines)
1504 if (context_out != context_in) *context_out = *context_in;
1505 context_out->Line++;
1506 SetLastError( 0 );
1507 return TRUE;
1510 /* now search the appended files */
1512 for (file = file->next; file; file = file->next)
1514 int section_index = find_section( file, section->name );
1515 if (section_index == -1) continue;
1516 if (file->sections[section_index]->nb_lines)
1518 context_out->Inf = context_in->Inf;
1519 context_out->CurrentInf = file;
1520 context_out->Section = section_index;
1521 context_out->Line = 0;
1522 SetLastError( 0 );
1523 return TRUE;
1526 error:
1527 SetLastError( ERROR_LINE_NOT_FOUND );
1528 return FALSE;
1532 /***********************************************************************
1533 * SetupFindNextMatchLineA (SETUPAPI.@)
1535 BOOL WINAPI SetupFindNextMatchLineA( PINFCONTEXT context_in, PCSTR key,
1536 PINFCONTEXT context_out )
1538 UNICODE_STRING keyW;
1539 BOOL ret = FALSE;
1541 if (!key) return SetupFindNextLine( context_in, context_out );
1543 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1544 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1545 else
1547 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1548 RtlFreeUnicodeString( &keyW );
1550 return ret;
1554 /***********************************************************************
1555 * SetupFindNextMatchLineW (SETUPAPI.@)
1557 BOOL WINAPI SetupFindNextMatchLineW( PINFCONTEXT context_in, PCWSTR key,
1558 PINFCONTEXT context_out )
1560 struct inf_file *file = context_in->CurrentInf;
1561 struct section *section;
1562 struct line *line;
1563 unsigned int i;
1565 if (!key) return SetupFindNextLine( context_in, context_out );
1567 if (context_in->Section >= file->nb_sections) goto error;
1569 section = file->sections[context_in->Section];
1571 for (i = context_in->Line+1, line = &section->lines[i]; i < section->nb_lines; i++, line++)
1573 if (line->key_field == -1) continue;
1574 if (!strcmpiW( key, file->fields[line->key_field].text ))
1576 if (context_out != context_in) *context_out = *context_in;
1577 context_out->Line = i;
1578 SetLastError( 0 );
1579 TRACE( "(%p,%s,%s): returning %d\n",
1580 file, debugstr_w(section->name), debugstr_w(key), i );
1581 return TRUE;
1585 /* now search the appended files */
1587 for (file = file->next; file; file = file->next)
1589 int section_index = find_section( file, section->name );
1590 if (section_index == -1) continue;
1591 section = file->sections[section_index];
1592 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1594 if (line->key_field == -1) continue;
1595 if (!strcmpiW( key, file->fields[line->key_field].text ))
1597 context_out->Inf = context_in->Inf;
1598 context_out->CurrentInf = file;
1599 context_out->Section = section_index;
1600 context_out->Line = i;
1601 SetLastError( 0 );
1602 TRACE( "(%p,%s,%s): returning %d/%d\n",
1603 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1604 return TRUE;
1608 TRACE( "(%p,%s,%s): not found\n",
1609 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1610 error:
1611 SetLastError( ERROR_LINE_NOT_FOUND );
1612 return FALSE;
1616 /***********************************************************************
1617 * SetupGetLineTextW (SETUPAPI.@)
1619 BOOL WINAPI SetupGetLineTextW( PINFCONTEXT context, HINF hinf, PCWSTR section_name,
1620 PCWSTR key_name, PWSTR buffer, DWORD size, PDWORD required )
1622 struct inf_file *file;
1623 struct line *line;
1624 struct field *field;
1625 int i;
1626 DWORD total = 0;
1628 if (!context)
1630 INFCONTEXT new_context;
1631 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1632 file = new_context.CurrentInf;
1633 line = get_line( file, new_context.Section, new_context.Line );
1635 else
1637 file = context->CurrentInf;
1638 if (!(line = get_line( file, context->Section, context->Line )))
1640 SetLastError( ERROR_LINE_NOT_FOUND );
1641 return FALSE;
1645 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1646 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1648 if (required) *required = total;
1649 if (buffer)
1651 if (total > size)
1653 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1654 return FALSE;
1656 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1658 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1659 if (i+1 < line->nb_fields) buffer[len] = ',';
1660 buffer += len + 1;
1663 return TRUE;
1667 /***********************************************************************
1668 * SetupGetLineTextA (SETUPAPI.@)
1670 BOOL WINAPI SetupGetLineTextA( PINFCONTEXT context, HINF hinf, PCSTR section_name,
1671 PCSTR key_name, PSTR buffer, DWORD size, PDWORD required )
1673 struct inf_file *file;
1674 struct line *line;
1675 struct field *field;
1676 int i;
1677 DWORD total = 0;
1679 if (!context)
1681 INFCONTEXT new_context;
1682 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1683 file = new_context.CurrentInf;
1684 line = get_line( file, new_context.Section, new_context.Line );
1686 else
1688 file = context->CurrentInf;
1689 if (!(line = get_line( file, context->Section, context->Line )))
1691 SetLastError( ERROR_LINE_NOT_FOUND );
1692 return FALSE;
1696 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1697 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1699 if (required) *required = total;
1700 if (buffer)
1702 if (total > size)
1704 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1705 return FALSE;
1707 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1709 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1710 if (i+1 < line->nb_fields) buffer[len] = ',';
1711 buffer += len + 1;
1714 return TRUE;
1718 /***********************************************************************
1719 * SetupGetFieldCount (SETUPAPI.@)
1721 DWORD WINAPI SetupGetFieldCount( PINFCONTEXT context )
1723 struct inf_file *file = context->CurrentInf;
1724 struct line *line = get_line( file, context->Section, context->Line );
1726 if (!line) return 0;
1727 return line->nb_fields;
1731 /***********************************************************************
1732 * SetupGetStringFieldA (SETUPAPI.@)
1734 BOOL WINAPI SetupGetStringFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1735 DWORD size, PDWORD required )
1737 struct inf_file *file = context->CurrentInf;
1738 struct field *field = get_field( file, context->Section, context->Line, index );
1739 unsigned int len;
1741 SetLastError(0);
1742 if (!field) return FALSE;
1743 len = PARSER_string_substA( file, field->text, NULL, 0 );
1744 if (required) *required = len + 1;
1745 if (buffer)
1747 if (size <= len)
1749 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1750 return FALSE;
1752 PARSER_string_substA( file, field->text, buffer, size );
1754 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1755 context->Inf, context->CurrentInf, context->Section, context->Line,
1756 index, debugstr_a(buffer) );
1758 return TRUE;
1762 /***********************************************************************
1763 * SetupGetStringFieldW (SETUPAPI.@)
1765 BOOL WINAPI SetupGetStringFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1766 DWORD size, PDWORD required )
1768 struct inf_file *file = context->CurrentInf;
1769 struct field *field = get_field( file, context->Section, context->Line, index );
1770 unsigned int len;
1772 SetLastError(0);
1773 if (!field) return FALSE;
1774 len = PARSER_string_substW( file, field->text, NULL, 0 );
1775 if (required) *required = len + 1;
1776 if (buffer)
1778 if (size <= len)
1780 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1781 return FALSE;
1783 PARSER_string_substW( file, field->text, buffer, size );
1785 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1786 context->Inf, context->CurrentInf, context->Section, context->Line,
1787 index, debugstr_w(buffer) );
1789 return TRUE;
1793 /***********************************************************************
1794 * SetupGetIntField (SETUPAPI.@)
1796 BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result )
1798 char localbuff[20];
1799 char *end, *buffer = localbuff;
1800 DWORD required;
1801 INT res;
1802 BOOL ret;
1804 if (!(ret = SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required )))
1806 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1807 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1808 if (!(ret = SetupGetStringFieldA( context, index, buffer, required, NULL ))) goto done;
1810 /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */
1811 if (!*buffer) *result = 0;
1812 else
1814 res = strtol( buffer, &end, 0 );
1815 if (end != buffer && !*end) *result = res;
1816 else
1818 SetLastError( ERROR_INVALID_DATA );
1819 ret = FALSE;
1823 done:
1824 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1825 return ret;
1829 /***********************************************************************
1830 * SetupGetBinaryField (SETUPAPI.@)
1832 BOOL WINAPI SetupGetBinaryField( PINFCONTEXT context, DWORD index, BYTE *buffer,
1833 DWORD size, LPDWORD required )
1835 struct inf_file *file = context->CurrentInf;
1836 struct line *line = get_line( file, context->Section, context->Line );
1837 struct field *field;
1838 int i;
1840 if (!line)
1842 SetLastError( ERROR_LINE_NOT_FOUND );
1843 return FALSE;
1845 if (!index || index > line->nb_fields)
1847 SetLastError( ERROR_INVALID_PARAMETER );
1848 return FALSE;
1850 index--; /* fields start at 0 */
1851 if (required) *required = line->nb_fields - index;
1852 if (!buffer) return TRUE;
1853 if (size < line->nb_fields - index)
1855 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1856 return FALSE;
1858 field = &file->fields[line->first_field + index];
1859 for (i = index; i < line->nb_fields; i++, field++)
1861 const WCHAR *p;
1862 DWORD value = 0;
1863 for (p = field->text; *p && isxdigitW(*p); p++)
1865 if ((value <<= 4) > 255)
1867 SetLastError( ERROR_INVALID_DATA );
1868 return FALSE;
1870 if (*p <= '9') value |= (*p - '0');
1871 else value |= (tolowerW(*p) - 'a' + 10);
1873 buffer[i - index] = value;
1875 if (TRACE_ON(setupapi))
1877 TRACE( "%p/%p/%d/%d index %d returning",
1878 context->Inf, context->CurrentInf, context->Section, context->Line, index );
1879 for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] );
1880 TRACE( "\n" );
1882 return TRUE;
1886 /***********************************************************************
1887 * SetupGetMultiSzFieldA (SETUPAPI.@)
1889 BOOL WINAPI SetupGetMultiSzFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1890 DWORD size, LPDWORD required )
1892 struct inf_file *file = context->CurrentInf;
1893 struct line *line = get_line( file, context->Section, context->Line );
1894 struct field *field;
1895 unsigned int len;
1896 int i;
1897 DWORD total = 1;
1899 if (!line)
1901 SetLastError( ERROR_LINE_NOT_FOUND );
1902 return FALSE;
1904 if (!index || index > line->nb_fields)
1906 SetLastError( ERROR_INVALID_PARAMETER );
1907 return FALSE;
1909 index--; /* fields start at 0 */
1910 field = &file->fields[line->first_field + index];
1911 for (i = index; i < line->nb_fields; i++, field++)
1913 if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1914 total += len + 1;
1917 if (required) *required = total;
1918 if (!buffer) return TRUE;
1919 if (total > size)
1921 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1922 return FALSE;
1924 field = &file->fields[line->first_field + index];
1925 for (i = index; i < line->nb_fields; i++, field++)
1927 if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1928 buffer += len + 1;
1930 *buffer = 0; /* add final null */
1931 return TRUE;
1935 /***********************************************************************
1936 * SetupGetMultiSzFieldW (SETUPAPI.@)
1938 BOOL WINAPI SetupGetMultiSzFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1939 DWORD size, LPDWORD required )
1941 struct inf_file *file = context->CurrentInf;
1942 struct line *line = get_line( file, context->Section, context->Line );
1943 struct field *field;
1944 unsigned int len;
1945 int i;
1946 DWORD total = 1;
1948 if (!line)
1950 SetLastError( ERROR_LINE_NOT_FOUND );
1951 return FALSE;
1953 if (!index || index > line->nb_fields)
1955 SetLastError( ERROR_INVALID_PARAMETER );
1956 return FALSE;
1958 index--; /* fields start at 0 */
1959 field = &file->fields[line->first_field + index];
1960 for (i = index; i < line->nb_fields; i++, field++)
1962 if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1963 total += len + 1;
1966 if (required) *required = total;
1967 if (!buffer) return TRUE;
1968 if (total > size)
1970 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1971 return FALSE;
1973 field = &file->fields[line->first_field + index];
1974 for (i = index; i < line->nb_fields; i++, field++)
1976 if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1977 buffer += len + 1;
1979 *buffer = 0; /* add final null */
1980 return TRUE;
1983 /***********************************************************************
1984 * pSetupGetField (SETUPAPI.@)
1986 LPCWSTR WINAPI pSetupGetField( PINFCONTEXT context, DWORD index )
1988 struct inf_file *file = context->CurrentInf;
1989 struct field *field = get_field( file, context->Section, context->Line, index );
1991 if (!field)
1993 SetLastError( ERROR_INVALID_PARAMETER );
1994 return NULL;
1996 return field->text;