comctl32: Add NULL checks to SetWindowSubclass (Valgrind).
[wine.git] / dlls / setupapi / parser.c
blobbc5dba0454d2373dc2579fb022c1af43740599e0
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 = ARRAY_SIZE( section->lines );
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, ARRAY_SIZE( buffW ));
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 < ARRAY_SIZE( parser->stack ));
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, ARRAY_SIZE( filename ), NULL ))
1222 child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error );
1223 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1224 append_inf_file( parent_hinf, child_hinf );
1225 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf );
1227 return TRUE;
1229 child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error );
1230 if (child_hinf == INVALID_HANDLE_VALUE) return FALSE;
1231 append_inf_file( parent_hinf, child_hinf );
1232 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf );
1233 return TRUE;
1237 /***********************************************************************
1238 * SetupOpenMasterInf (SETUPAPI.@)
1240 HINF WINAPI SetupOpenMasterInf( VOID )
1242 static const WCHAR Layout[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1243 WCHAR Buffer[MAX_PATH];
1245 GetWindowsDirectoryW( Buffer, MAX_PATH );
1246 strcatW( Buffer, Layout );
1247 return SetupOpenInfFileW( Buffer, NULL, INF_STYLE_WIN4, NULL);
1252 /***********************************************************************
1253 * SetupCloseInfFile (SETUPAPI.@)
1255 void WINAPI SetupCloseInfFile( HINF hinf )
1257 struct inf_file *file = hinf;
1259 if (!hinf || (hinf == INVALID_HANDLE_VALUE)) return;
1261 free_inf_file( file );
1265 /***********************************************************************
1266 * SetupEnumInfSectionsA (SETUPAPI.@)
1268 BOOL WINAPI SetupEnumInfSectionsA( HINF hinf, UINT index, PSTR buffer, DWORD size, DWORD *need )
1270 struct inf_file *file = hinf;
1272 for (file = hinf; file; file = file->next)
1274 if (index < file->nb_sections)
1276 DWORD len = WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1,
1277 NULL, 0, NULL, NULL );
1278 if (need) *need = len;
1279 if (!buffer)
1281 if (!size) return TRUE;
1282 SetLastError( ERROR_INVALID_USER_BUFFER );
1283 return FALSE;
1285 if (len > size)
1287 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1288 return FALSE;
1290 WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1, buffer, size, NULL, NULL );
1291 return TRUE;
1293 index -= file->nb_sections;
1295 SetLastError( ERROR_NO_MORE_ITEMS );
1296 return FALSE;
1300 /***********************************************************************
1301 * SetupEnumInfSectionsW (SETUPAPI.@)
1303 BOOL WINAPI SetupEnumInfSectionsW( HINF hinf, UINT index, PWSTR buffer, DWORD size, DWORD *need )
1305 struct inf_file *file = hinf;
1307 for (file = hinf; file; file = file->next)
1309 if (index < file->nb_sections)
1311 DWORD len = strlenW( file->sections[index]->name ) + 1;
1312 if (need) *need = len;
1313 if (!buffer)
1315 if (!size) return TRUE;
1316 SetLastError( ERROR_INVALID_USER_BUFFER );
1317 return FALSE;
1319 if (len > size)
1321 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1322 return FALSE;
1324 memcpy( buffer, file->sections[index]->name, len * sizeof(WCHAR) );
1325 return TRUE;
1327 index -= file->nb_sections;
1329 SetLastError( ERROR_NO_MORE_ITEMS );
1330 return FALSE;
1334 /***********************************************************************
1335 * SetupGetLineCountA (SETUPAPI.@)
1337 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1339 UNICODE_STRING sectionW;
1340 LONG ret = -1;
1342 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, name ))
1343 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1344 else
1346 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1347 RtlFreeUnicodeString( &sectionW );
1349 return ret;
1353 /***********************************************************************
1354 * SetupGetLineCountW (SETUPAPI.@)
1356 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1358 struct inf_file *file = hinf;
1359 int section_index;
1360 LONG ret = -1;
1362 for (file = hinf; file; file = file->next)
1364 if ((section_index = find_section( file, section )) == -1) continue;
1365 if (ret == -1) ret = 0;
1366 ret += file->sections[section_index]->nb_lines;
1368 TRACE( "(%p,%s) returning %d\n", hinf, debugstr_w(section), ret );
1369 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1370 return ret;
1374 /***********************************************************************
1375 * SetupGetLineByIndexA (SETUPAPI.@)
1377 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1379 UNICODE_STRING sectionW;
1380 BOOL ret = FALSE;
1382 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1383 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1384 else
1386 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1387 RtlFreeUnicodeString( &sectionW );
1389 return ret;
1393 /***********************************************************************
1394 * SetupGetLineByIndexW (SETUPAPI.@)
1396 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1398 struct inf_file *file = hinf;
1399 int section_index;
1401 for (file = hinf; file; file = file->next)
1403 if ((section_index = find_section( file, section )) == -1) continue;
1404 if (index < file->sections[section_index]->nb_lines)
1406 context->Inf = hinf;
1407 context->CurrentInf = file;
1408 context->Section = section_index;
1409 context->Line = index;
1410 SetLastError( 0 );
1411 TRACE( "(%p,%s): returning %d/%d\n",
1412 hinf, debugstr_w(section), section_index, index );
1413 return TRUE;
1415 index -= file->sections[section_index]->nb_lines;
1417 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1418 SetLastError( ERROR_LINE_NOT_FOUND );
1419 return FALSE;
1423 /***********************************************************************
1424 * SetupFindFirstLineA (SETUPAPI.@)
1426 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1428 UNICODE_STRING sectionW, keyW;
1429 BOOL ret = FALSE;
1431 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1433 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1434 return FALSE;
1437 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1438 else
1440 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1442 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1443 RtlFreeUnicodeString( &keyW );
1445 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1447 RtlFreeUnicodeString( &sectionW );
1448 return ret;
1452 /***********************************************************************
1453 * SetupFindFirstLineW (SETUPAPI.@)
1455 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1457 struct inf_file *file;
1458 int section_index;
1460 for (file = hinf; file; file = file->next)
1462 if ((section_index = find_section( file, section )) == -1) continue;
1463 if (key)
1465 INFCONTEXT ctx;
1466 ctx.Inf = hinf;
1467 ctx.CurrentInf = file;
1468 ctx.Section = section_index;
1469 ctx.Line = -1;
1470 return SetupFindNextMatchLineW( &ctx, key, context );
1472 if (file->sections[section_index]->nb_lines)
1474 context->Inf = hinf;
1475 context->CurrentInf = file;
1476 context->Section = section_index;
1477 context->Line = 0;
1478 SetLastError( 0 );
1479 TRACE( "(%p,%s,%s): returning %d/0\n",
1480 hinf, debugstr_w(section), debugstr_w(key), section_index );
1481 return TRUE;
1484 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1485 SetLastError( ERROR_LINE_NOT_FOUND );
1486 return FALSE;
1490 /***********************************************************************
1491 * SetupFindNextLine (SETUPAPI.@)
1493 BOOL WINAPI SetupFindNextLine( PINFCONTEXT context_in, PINFCONTEXT context_out )
1495 struct inf_file *file = context_in->CurrentInf;
1496 struct section *section;
1498 if (context_in->Section >= file->nb_sections) goto error;
1500 section = file->sections[context_in->Section];
1501 if (context_in->Line+1 < section->nb_lines)
1503 if (context_out != context_in) *context_out = *context_in;
1504 context_out->Line++;
1505 SetLastError( 0 );
1506 return TRUE;
1509 /* now search the appended files */
1511 for (file = file->next; file; file = file->next)
1513 int section_index = find_section( file, section->name );
1514 if (section_index == -1) continue;
1515 if (file->sections[section_index]->nb_lines)
1517 context_out->Inf = context_in->Inf;
1518 context_out->CurrentInf = file;
1519 context_out->Section = section_index;
1520 context_out->Line = 0;
1521 SetLastError( 0 );
1522 return TRUE;
1525 error:
1526 SetLastError( ERROR_LINE_NOT_FOUND );
1527 return FALSE;
1531 /***********************************************************************
1532 * SetupFindNextMatchLineA (SETUPAPI.@)
1534 BOOL WINAPI SetupFindNextMatchLineA( PINFCONTEXT context_in, PCSTR key,
1535 PINFCONTEXT context_out )
1537 UNICODE_STRING keyW;
1538 BOOL ret = FALSE;
1540 if (!key) return SetupFindNextLine( context_in, context_out );
1542 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1543 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1544 else
1546 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1547 RtlFreeUnicodeString( &keyW );
1549 return ret;
1553 /***********************************************************************
1554 * SetupFindNextMatchLineW (SETUPAPI.@)
1556 BOOL WINAPI SetupFindNextMatchLineW( PINFCONTEXT context_in, PCWSTR key,
1557 PINFCONTEXT context_out )
1559 struct inf_file *file = context_in->CurrentInf;
1560 struct section *section;
1561 struct line *line;
1562 unsigned int i;
1564 if (!key) return SetupFindNextLine( context_in, context_out );
1566 if (context_in->Section >= file->nb_sections) goto error;
1568 section = file->sections[context_in->Section];
1570 for (i = context_in->Line+1, line = &section->lines[i]; i < section->nb_lines; i++, line++)
1572 if (line->key_field == -1) continue;
1573 if (!strcmpiW( key, file->fields[line->key_field].text ))
1575 if (context_out != context_in) *context_out = *context_in;
1576 context_out->Line = i;
1577 SetLastError( 0 );
1578 TRACE( "(%p,%s,%s): returning %d\n",
1579 file, debugstr_w(section->name), debugstr_w(key), i );
1580 return TRUE;
1584 /* now search the appended files */
1586 for (file = file->next; file; file = file->next)
1588 int section_index = find_section( file, section->name );
1589 if (section_index == -1) continue;
1590 section = file->sections[section_index];
1591 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1593 if (line->key_field == -1) continue;
1594 if (!strcmpiW( key, file->fields[line->key_field].text ))
1596 context_out->Inf = context_in->Inf;
1597 context_out->CurrentInf = file;
1598 context_out->Section = section_index;
1599 context_out->Line = i;
1600 SetLastError( 0 );
1601 TRACE( "(%p,%s,%s): returning %d/%d\n",
1602 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1603 return TRUE;
1607 TRACE( "(%p,%s,%s): not found\n",
1608 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1609 error:
1610 SetLastError( ERROR_LINE_NOT_FOUND );
1611 return FALSE;
1615 /***********************************************************************
1616 * SetupGetLineTextW (SETUPAPI.@)
1618 BOOL WINAPI SetupGetLineTextW( PINFCONTEXT context, HINF hinf, PCWSTR section_name,
1619 PCWSTR key_name, PWSTR buffer, DWORD size, PDWORD required )
1621 struct inf_file *file;
1622 struct line *line;
1623 struct field *field;
1624 int i;
1625 DWORD total = 0;
1627 if (!context)
1629 INFCONTEXT new_context;
1630 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1631 file = new_context.CurrentInf;
1632 line = get_line( file, new_context.Section, new_context.Line );
1634 else
1636 file = context->CurrentInf;
1637 if (!(line = get_line( file, context->Section, context->Line )))
1639 SetLastError( ERROR_LINE_NOT_FOUND );
1640 return FALSE;
1644 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1645 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1647 if (required) *required = total;
1648 if (buffer)
1650 if (total > size)
1652 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1653 return FALSE;
1655 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1657 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1658 if (i+1 < line->nb_fields) buffer[len] = ',';
1659 buffer += len + 1;
1662 return TRUE;
1666 /***********************************************************************
1667 * SetupGetLineTextA (SETUPAPI.@)
1669 BOOL WINAPI SetupGetLineTextA( PINFCONTEXT context, HINF hinf, PCSTR section_name,
1670 PCSTR key_name, PSTR buffer, DWORD size, PDWORD required )
1672 struct inf_file *file;
1673 struct line *line;
1674 struct field *field;
1675 int i;
1676 DWORD total = 0;
1678 if (!context)
1680 INFCONTEXT new_context;
1681 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1682 file = new_context.CurrentInf;
1683 line = get_line( file, new_context.Section, new_context.Line );
1685 else
1687 file = context->CurrentInf;
1688 if (!(line = get_line( file, context->Section, context->Line )))
1690 SetLastError( ERROR_LINE_NOT_FOUND );
1691 return FALSE;
1695 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1696 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1698 if (required) *required = total;
1699 if (buffer)
1701 if (total > size)
1703 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1704 return FALSE;
1706 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1708 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1709 if (i+1 < line->nb_fields) buffer[len] = ',';
1710 buffer += len + 1;
1713 return TRUE;
1717 /***********************************************************************
1718 * SetupGetFieldCount (SETUPAPI.@)
1720 DWORD WINAPI SetupGetFieldCount( PINFCONTEXT context )
1722 struct inf_file *file = context->CurrentInf;
1723 struct line *line = get_line( file, context->Section, context->Line );
1725 if (!line) return 0;
1726 return line->nb_fields;
1730 /***********************************************************************
1731 * SetupGetStringFieldA (SETUPAPI.@)
1733 BOOL WINAPI SetupGetStringFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1734 DWORD size, PDWORD required )
1736 struct inf_file *file = context->CurrentInf;
1737 struct field *field = get_field( file, context->Section, context->Line, index );
1738 unsigned int len;
1740 SetLastError(0);
1741 if (!field) return FALSE;
1742 len = PARSER_string_substA( file, field->text, NULL, 0 );
1743 if (required) *required = len + 1;
1744 if (buffer)
1746 if (size <= len)
1748 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1749 return FALSE;
1751 PARSER_string_substA( file, field->text, buffer, size );
1753 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1754 context->Inf, context->CurrentInf, context->Section, context->Line,
1755 index, debugstr_a(buffer) );
1757 return TRUE;
1761 /***********************************************************************
1762 * SetupGetStringFieldW (SETUPAPI.@)
1764 BOOL WINAPI SetupGetStringFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1765 DWORD size, PDWORD required )
1767 struct inf_file *file = context->CurrentInf;
1768 struct field *field = get_field( file, context->Section, context->Line, index );
1769 unsigned int len;
1771 SetLastError(0);
1772 if (!field) return FALSE;
1773 len = PARSER_string_substW( file, field->text, NULL, 0 );
1774 if (required) *required = len + 1;
1775 if (buffer)
1777 if (size <= len)
1779 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1780 return FALSE;
1782 PARSER_string_substW( file, field->text, buffer, size );
1784 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1785 context->Inf, context->CurrentInf, context->Section, context->Line,
1786 index, debugstr_w(buffer) );
1788 return TRUE;
1792 /***********************************************************************
1793 * SetupGetIntField (SETUPAPI.@)
1795 BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result )
1797 char localbuff[20];
1798 char *end, *buffer = localbuff;
1799 DWORD required;
1800 INT res;
1801 BOOL ret;
1803 if (!(ret = SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required )))
1805 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1806 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1807 if (!(ret = SetupGetStringFieldA( context, index, buffer, required, NULL ))) goto done;
1809 /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */
1810 if (!*buffer) *result = 0;
1811 else
1813 res = strtol( buffer, &end, 0 );
1814 if (end != buffer && !*end) *result = res;
1815 else
1817 SetLastError( ERROR_INVALID_DATA );
1818 ret = FALSE;
1822 done:
1823 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1824 return ret;
1828 /***********************************************************************
1829 * SetupGetBinaryField (SETUPAPI.@)
1831 BOOL WINAPI SetupGetBinaryField( PINFCONTEXT context, DWORD index, BYTE *buffer,
1832 DWORD size, LPDWORD required )
1834 struct inf_file *file = context->CurrentInf;
1835 struct line *line = get_line( file, context->Section, context->Line );
1836 struct field *field;
1837 int i;
1839 if (!line)
1841 SetLastError( ERROR_LINE_NOT_FOUND );
1842 return FALSE;
1844 if (!index || index > line->nb_fields)
1846 SetLastError( ERROR_INVALID_PARAMETER );
1847 return FALSE;
1849 index--; /* fields start at 0 */
1850 if (required) *required = line->nb_fields - index;
1851 if (!buffer) return TRUE;
1852 if (size < line->nb_fields - index)
1854 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1855 return FALSE;
1857 field = &file->fields[line->first_field + index];
1858 for (i = index; i < line->nb_fields; i++, field++)
1860 const WCHAR *p;
1861 DWORD value = 0;
1862 for (p = field->text; *p && isxdigitW(*p); p++)
1864 if ((value <<= 4) > 255)
1866 SetLastError( ERROR_INVALID_DATA );
1867 return FALSE;
1869 if (*p <= '9') value |= (*p - '0');
1870 else value |= (tolowerW(*p) - 'a' + 10);
1872 buffer[i - index] = value;
1874 if (TRACE_ON(setupapi))
1876 TRACE( "%p/%p/%d/%d index %d returning",
1877 context->Inf, context->CurrentInf, context->Section, context->Line, index );
1878 for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] );
1879 TRACE( "\n" );
1881 return TRUE;
1885 /***********************************************************************
1886 * SetupGetMultiSzFieldA (SETUPAPI.@)
1888 BOOL WINAPI SetupGetMultiSzFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1889 DWORD size, LPDWORD required )
1891 struct inf_file *file = context->CurrentInf;
1892 struct line *line = get_line( file, context->Section, context->Line );
1893 struct field *field;
1894 unsigned int len;
1895 int i;
1896 DWORD total = 1;
1898 if (!line)
1900 SetLastError( ERROR_LINE_NOT_FOUND );
1901 return FALSE;
1903 if (!index || index > line->nb_fields)
1905 SetLastError( ERROR_INVALID_PARAMETER );
1906 return FALSE;
1908 index--; /* fields start at 0 */
1909 field = &file->fields[line->first_field + index];
1910 for (i = index; i < line->nb_fields; i++, field++)
1912 if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1913 total += len + 1;
1916 if (required) *required = total;
1917 if (!buffer) return TRUE;
1918 if (total > size)
1920 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1921 return FALSE;
1923 field = &file->fields[line->first_field + index];
1924 for (i = index; i < line->nb_fields; i++, field++)
1926 if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1927 buffer += len + 1;
1929 *buffer = 0; /* add final null */
1930 return TRUE;
1934 /***********************************************************************
1935 * SetupGetMultiSzFieldW (SETUPAPI.@)
1937 BOOL WINAPI SetupGetMultiSzFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1938 DWORD size, LPDWORD required )
1940 struct inf_file *file = context->CurrentInf;
1941 struct line *line = get_line( file, context->Section, context->Line );
1942 struct field *field;
1943 unsigned int len;
1944 int i;
1945 DWORD total = 1;
1947 if (!line)
1949 SetLastError( ERROR_LINE_NOT_FOUND );
1950 return FALSE;
1952 if (!index || index > line->nb_fields)
1954 SetLastError( ERROR_INVALID_PARAMETER );
1955 return FALSE;
1957 index--; /* fields start at 0 */
1958 field = &file->fields[line->first_field + index];
1959 for (i = index; i < line->nb_fields; i++, field++)
1961 if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1962 total += len + 1;
1965 if (required) *required = total;
1966 if (!buffer) return TRUE;
1967 if (total > size)
1969 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1970 return FALSE;
1972 field = &file->fields[line->first_field + index];
1973 for (i = index; i < line->nb_fields; i++, field++)
1975 if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1976 buffer += len + 1;
1978 *buffer = 0; /* add final null */
1979 return TRUE;
1982 /***********************************************************************
1983 * pSetupGetField (SETUPAPI.@)
1985 LPCWSTR WINAPI pSetupGetField( PINFCONTEXT context, DWORD index )
1987 struct inf_file *file = context->CurrentInf;
1988 struct field *field = get_field( file, context->Section, context->Line, index );
1990 if (!field)
1992 SetLastError( ERROR_INVALID_PARAMETER );
1993 return NULL;
1995 return field->text;