Allocate correct amount of memory.
[wine.git] / dlls / setupapi / parser.c
blob9c6d4fc41df0f4bb1e34b316bacce6327e852802
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 *src_root; /* source root directory */
89 /* parser definitions */
91 enum parser_state
93 LINE_START, /* at beginning of a line */
94 SECTION_NAME, /* parsing a section name */
95 KEY_NAME, /* parsing a key name */
96 VALUE_NAME, /* parsing a value name */
97 EOL_BACKSLASH, /* backslash at end of line */
98 QUOTES, /* inside quotes */
99 LEADING_SPACES, /* leading spaces */
100 TRAILING_SPACES, /* trailing spaces */
101 COMMENT, /* inside a comment */
102 NB_PARSER_STATES
105 struct parser
107 const WCHAR *start; /* start position of item being parsed */
108 const WCHAR *end; /* end of buffer */
109 struct inf_file *file; /* file being built */
110 enum parser_state state; /* current parser state */
111 enum parser_state stack[4]; /* state stack */
112 int stack_pos; /* current pos in stack */
114 int cur_section; /* index of section being parsed*/
115 struct line *line; /* current line */
116 unsigned int line_pos; /* current line position in file */
117 unsigned int error; /* error code */
118 unsigned int token_len; /* current token len */
119 WCHAR token[MAX_FIELD_LEN+1]; /* current token */
122 typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos );
124 /* parser state machine functions */
125 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos );
126 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos );
127 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos );
128 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos );
129 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos );
130 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos );
131 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos );
132 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos );
133 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos );
135 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
137 line_start_state, /* LINE_START */
138 section_name_state, /* SECTION_NAME */
139 key_name_state, /* KEY_NAME */
140 value_name_state, /* VALUE_NAME */
141 eol_backslash_state, /* EOL_BACKSLASH */
142 quotes_state, /* QUOTES */
143 leading_spaces_state, /* LEADING_SPACES */
144 trailing_spaces_state, /* TRAILING_SPACES */
145 comment_state /* COMMENT */
149 /* Unicode string constants */
150 static const WCHAR Version[] = {'V','e','r','s','i','o','n',0};
151 static const WCHAR Signature[] = {'S','i','g','n','a','t','u','r','e',0};
152 static const WCHAR Chicago[] = {'$','C','h','i','c','a','g','o','$',0};
153 static const WCHAR WindowsNT[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
154 static const WCHAR Windows95[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
155 static const WCHAR LayoutFile[] = {'L','a','y','o','u','t','F','i','l','e',0};
157 /* extend an array, allocating more memory if necessary */
158 static void *grow_array( void *array, unsigned int *count, size_t elem )
160 void *new_array;
161 unsigned int new_count = *count + *count / 2;
162 if (new_count < 32) new_count = 32;
164 if (array)
165 new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem );
166 else
167 new_array = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * elem );
169 if (new_array)
170 *count = new_count;
171 else
172 if (array)
173 HeapFree( GetProcessHeap(), 0, array );
174 return new_array;
178 /* find a section by name */
179 static int find_section( struct inf_file *file, const WCHAR *name )
181 unsigned int i;
183 for (i = 0; i < file->nb_sections; i++)
184 if (!strcmpiW( name, file->sections[i]->name )) return i;
185 return -1;
189 /* find a line by name */
190 static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name )
192 struct section *section;
193 struct line *line;
194 int i;
196 if (section_index < 0 || section_index >= file->nb_sections) return NULL;
197 section = file->sections[section_index];
198 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
200 if (line->key_field == -1) continue;
201 if (!strcmpiW( name, file->fields[line->key_field].text )) return line;
203 return NULL;
207 /* add a section to the file and return the section index */
208 static int add_section( struct inf_file *file, const WCHAR *name )
210 struct section *section;
212 if (file->nb_sections >= file->alloc_sections)
214 if (!(file->sections = grow_array( file->sections, &file->alloc_sections,
215 sizeof(file->sections[0]) ))) return -1;
217 if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1;
218 section->name = name;
219 section->nb_lines = 0;
220 section->alloc_lines = sizeof(section->lines)/sizeof(section->lines[0]);
221 file->sections[file->nb_sections] = section;
222 return file->nb_sections++;
226 /* add a line to a given section */
227 static struct line *add_line( struct inf_file *file, int section_index )
229 struct section *section;
230 struct line *line;
232 assert( section_index >= 0 && section_index < file->nb_sections );
234 section = file->sections[section_index];
235 if (section->nb_lines == section->alloc_lines) /* need to grow the section */
237 int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line);
238 if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL;
239 section->alloc_lines *= 2;
240 file->sections[section_index] = section;
242 line = &section->lines[section->nb_lines++];
243 line->first_field = file->nb_fields;
244 line->nb_fields = 0;
245 line->key_field = -1;
246 return line;
250 /* retrieve a given line from section/line index */
251 inline static struct line *get_line( struct inf_file *file, unsigned int section_index,
252 unsigned int line_index )
254 struct section *section;
256 if (section_index >= file->nb_sections) return NULL;
257 section = file->sections[section_index];
258 if (line_index >= section->nb_lines) return NULL;
259 return &section->lines[line_index];
263 /* retrieve a given field from section/line/field index */
264 static struct field *get_field( struct inf_file *file, int section_index, int line_index,
265 int field_index )
267 struct line *line = get_line( file, section_index, line_index );
269 if (!line) return NULL;
270 if (!field_index) /* get the key */
272 if (line->key_field == -1) return NULL;
273 return &file->fields[line->key_field];
275 field_index--;
276 if (field_index >= line->nb_fields) return NULL;
277 return &file->fields[line->first_field + field_index];
281 /* allocate a new field, growing the array if necessary */
282 static struct field *add_field( struct inf_file *file, const WCHAR *text )
284 struct field *field;
286 if (file->nb_fields >= file->alloc_fields)
288 if (!(file->fields = grow_array( file->fields, &file->alloc_fields,
289 sizeof(file->fields[0]) ))) return NULL;
291 field = &file->fields[file->nb_fields++];
292 field->text = text;
293 return field;
297 /* retrieve the string substitution for a directory id */
298 static const WCHAR *get_dirid_subst( int dirid, unsigned int *len )
300 extern const WCHAR *DIRID_get_string( HINF hinf, int dirid );
301 const WCHAR *ret = DIRID_get_string( 0, dirid );
302 if (ret) *len = strlenW(ret);
303 return ret;
307 /* retrieve the string substitution for a given string, or NULL if not found */
308 /* if found, len is set to the substitution length */
309 static const WCHAR *get_string_subst( struct inf_file *file, const WCHAR *str, unsigned int *len )
311 static const WCHAR percent = '%';
313 struct section *strings_section;
314 struct line *line;
315 struct field *field;
316 unsigned int i;
317 int dirid;
318 WCHAR *dirid_str, *end;
319 const WCHAR *ret = NULL;
321 if (!*len) /* empty string (%%) is replaced by single percent */
323 *len = 1;
324 return &percent;
326 if (file->strings_section == -1) goto not_found;
327 strings_section = file->sections[file->strings_section];
328 for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++)
330 if (line->key_field == -1) continue;
331 if (strncmpiW( str, file->fields[line->key_field].text, *len )) continue;
332 if (!file->fields[line->key_field].text[*len]) break;
334 if (i == strings_section->nb_lines || !line->nb_fields) goto not_found;
335 field = &file->fields[line->first_field];
336 *len = strlenW( field->text );
337 return field->text;
339 not_found: /* check for integer id */
340 if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) )))
342 memcpy( dirid_str, str, *len * sizeof(WCHAR) );
343 dirid_str[*len] = 0;
344 dirid = strtolW( dirid_str, &end, 10 );
345 if (!*end) ret = get_dirid_subst( dirid, len );
346 HeapFree( GetProcessHeap(), 0, dirid_str );
347 return ret;
349 return NULL;
353 /* do string substitutions on the specified text */
354 /* the buffer is assumed to be large enough */
355 /* returns necessary length not including terminating null */
356 unsigned int PARSER_string_substW( struct inf_file *file, const WCHAR *text, WCHAR *buffer,
357 unsigned int size )
359 const WCHAR *start, *subst, *p;
360 unsigned int len, total = 0;
361 int inside = 0;
363 if (!buffer) size = MAX_STRING_LEN + 1;
364 for (p = start = text; *p; p++)
366 if (*p != '%') continue;
367 inside = !inside;
368 if (inside) /* start of a %xx% string */
370 len = p - start;
371 if (len > size - 1) len = size - 1;
372 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
373 total += len;
374 size -= len;
375 start = p;
377 else /* end of the %xx% string, find substitution */
379 len = p - start - 1;
380 subst = get_string_subst( file, start + 1, &len );
381 if (!subst)
383 subst = start;
384 len = p - start + 1;
386 if (len > size - 1) len = size - 1;
387 if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
388 total += len;
389 size -= len;
390 start = p + 1;
394 if (start != p) /* unfinished string, copy it */
396 len = p - start;
397 if (len > size - 1) len = size - 1;
398 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
399 total += len;
401 if (buffer && size) buffer[total] = 0;
402 return total;
406 /* do string substitutions on the specified text */
407 /* the buffer is assumed to be large enough */
408 /* returns necessary length not including terminating null */
409 unsigned int PARSER_string_substA( struct inf_file *file, const WCHAR *text, char *buffer,
410 unsigned int size )
412 WCHAR buffW[MAX_STRING_LEN+1];
413 DWORD ret;
415 unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) );
416 if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
417 else
419 RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
420 buffer[ret] = 0;
422 return ret;
426 /* push some string data into the strings buffer */
427 static WCHAR *push_string( struct inf_file *file, const WCHAR *string )
429 WCHAR *ret = file->string_pos;
430 strcpyW( ret, string );
431 file->string_pos += strlenW( ret ) + 1;
432 return ret;
436 /* push the current state on the parser stack */
437 inline static void push_state( struct parser *parser, enum parser_state state )
439 assert( parser->stack_pos < sizeof(parser->stack)/sizeof(parser->stack[0]) );
440 parser->stack[parser->stack_pos++] = state;
444 /* pop the current state */
445 inline static void pop_state( struct parser *parser )
447 assert( parser->stack_pos );
448 parser->state = parser->stack[--parser->stack_pos];
452 /* set the parser state and return the previous one */
453 inline static enum parser_state set_state( struct parser *parser, enum parser_state state )
455 enum parser_state ret = parser->state;
456 parser->state = state;
457 return ret;
461 /* check if the pointer points to an end of file */
462 inline static int is_eof( struct parser *parser, const WCHAR *ptr )
464 return (ptr >= parser->end || *ptr == CONTROL_Z);
468 /* check if the pointer points to an end of line */
469 inline static int is_eol( struct parser *parser, const WCHAR *ptr )
471 return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n');
475 /* push data from current token start up to pos into the current token */
476 static int push_token( struct parser *parser, const WCHAR *pos )
478 int len = pos - parser->start;
479 const WCHAR *src = parser->start;
480 WCHAR *dst = parser->token + parser->token_len;
482 if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len;
484 parser->token_len += len;
485 for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' ';
486 *dst = 0;
487 parser->start = pos;
488 return 0;
492 /* add a section with the current token as name */
493 static int add_section_from_token( struct parser *parser )
495 int section_index;
497 if (parser->token_len > MAX_SECTION_NAME_LEN)
499 parser->error = ERROR_SECTION_NAME_TOO_LONG;
500 return -1;
502 if ((section_index = find_section( parser->file, parser->token )) == -1)
504 /* need to create a new one */
505 const WCHAR *name = push_string( parser->file, parser->token );
506 if ((section_index = add_section( parser->file, name )) == -1)
508 parser->error = ERROR_NOT_ENOUGH_MEMORY;
509 return -1;
512 parser->token_len = 0;
513 parser->cur_section = section_index;
514 return section_index;
518 /* add a field containing the current token to the current line */
519 static struct field *add_field_from_token( struct parser *parser, int is_key )
521 struct field *field;
522 WCHAR *text;
524 if (!parser->line) /* need to start a new line */
526 if (parser->cur_section == -1) /* got a line before the first section */
528 parser->error = ERROR_WRONG_INF_STYLE;
529 return NULL;
531 if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error;
533 else assert(!is_key);
535 text = push_string( parser->file, parser->token );
536 if ((field = add_field( parser->file, text )))
538 if (!is_key) parser->line->nb_fields++;
539 else
541 /* replace first field by key field */
542 parser->line->key_field = parser->line->first_field;
543 parser->line->first_field++;
545 parser->token_len = 0;
546 return field;
548 error:
549 parser->error = ERROR_NOT_ENOUGH_MEMORY;
550 return NULL;
554 /* close the current line and prepare for parsing a new one */
555 static void close_current_line( struct parser *parser )
557 struct line *cur_line = parser->line;
559 if (cur_line)
561 /* if line has a single field and no key, the field is the key too */
562 if (cur_line->nb_fields == 1 && cur_line->key_field == -1)
563 cur_line->key_field = cur_line->first_field;
565 parser->line = NULL;
569 /* handler for parser LINE_START state */
570 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
572 const WCHAR *p;
574 for (p = pos; !is_eof( parser, p ); p++)
576 switch(*p)
578 case '\n':
579 parser->line_pos++;
580 close_current_line( parser );
581 break;
582 case ';':
583 push_state( parser, LINE_START );
584 set_state( parser, COMMENT );
585 return p + 1;
586 case '[':
587 parser->start = p + 1;
588 set_state( parser, SECTION_NAME );
589 return p + 1;
590 default:
591 if (!isspaceW(*p))
593 parser->start = p;
594 set_state( parser, KEY_NAME );
595 return p;
597 break;
600 close_current_line( parser );
601 return NULL;
605 /* handler for parser SECTION_NAME state */
606 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
608 const WCHAR *p;
610 for (p = pos; !is_eol( parser, p ); p++)
612 if (*p == ']')
614 push_token( parser, p );
615 if (add_section_from_token( parser ) == -1) return NULL;
616 push_state( parser, LINE_START );
617 set_state( parser, COMMENT ); /* ignore everything else on the line */
618 return p + 1;
621 parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */
622 return NULL;
626 /* handler for parser KEY_NAME state */
627 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos )
629 const WCHAR *p, *token_end = parser->start;
631 for (p = pos; !is_eol( parser, p ); p++)
633 if (*p == ',') break;
634 switch(*p)
637 case '=':
638 push_token( parser, token_end );
639 if (!add_field_from_token( parser, 1 )) return NULL;
640 parser->start = p + 1;
641 push_state( parser, VALUE_NAME );
642 set_state( parser, LEADING_SPACES );
643 return p + 1;
644 case ';':
645 push_token( parser, token_end );
646 if (!add_field_from_token( parser, 0 )) return NULL;
647 push_state( parser, LINE_START );
648 set_state( parser, COMMENT );
649 return p + 1;
650 case '"':
651 push_token( parser, token_end );
652 parser->start = p + 1;
653 push_state( parser, KEY_NAME );
654 set_state( parser, QUOTES );
655 return p + 1;
656 case '\\':
657 push_token( parser, token_end );
658 parser->start = p;
659 push_state( parser, KEY_NAME );
660 set_state( parser, EOL_BACKSLASH );
661 return p;
662 default:
663 if (!isspaceW(*p)) token_end = p + 1;
664 else
666 push_token( parser, p );
667 push_state( parser, KEY_NAME );
668 set_state( parser, TRAILING_SPACES );
669 return p;
671 break;
674 push_token( parser, token_end );
675 set_state( parser, VALUE_NAME );
676 return p;
680 /* handler for parser VALUE_NAME state */
681 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos )
683 const WCHAR *p, *token_end = parser->start;
685 for (p = pos; !is_eol( parser, p ); p++)
687 switch(*p)
689 case ';':
690 push_token( parser, token_end );
691 if (!add_field_from_token( parser, 0 )) return NULL;
692 push_state( parser, LINE_START );
693 set_state( parser, COMMENT );
694 return p + 1;
695 case ',':
696 push_token( parser, token_end );
697 if (!add_field_from_token( parser, 0 )) return NULL;
698 parser->start = p + 1;
699 push_state( parser, VALUE_NAME );
700 set_state( parser, LEADING_SPACES );
701 return p + 1;
702 case '"':
703 push_token( parser, token_end );
704 parser->start = p + 1;
705 push_state( parser, VALUE_NAME );
706 set_state( parser, QUOTES );
707 return p + 1;
708 case '\\':
709 push_token( parser, token_end );
710 parser->start = p;
711 push_state( parser, VALUE_NAME );
712 set_state( parser, EOL_BACKSLASH );
713 return p;
714 default:
715 if (!isspaceW(*p)) token_end = p + 1;
716 else
718 push_token( parser, p );
719 push_state( parser, VALUE_NAME );
720 set_state( parser, TRAILING_SPACES );
721 return p;
723 break;
726 push_token( parser, token_end );
727 if (!add_field_from_token( parser, 0 )) return NULL;
728 set_state( parser, LINE_START );
729 return p;
733 /* handler for parser EOL_BACKSLASH state */
734 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos )
736 const WCHAR *p;
738 for (p = pos; !is_eof( parser, p ); p++)
740 switch(*p)
742 case '\n':
743 parser->line_pos++;
744 parser->start = p + 1;
745 set_state( parser, LEADING_SPACES );
746 return p + 1;
747 case '\\':
748 continue;
749 case ';':
750 push_state( parser, EOL_BACKSLASH );
751 set_state( parser, COMMENT );
752 return p + 1;
753 default:
754 if (isspaceW(*p)) continue;
755 push_token( parser, p );
756 pop_state( parser );
757 return p;
760 parser->start = p;
761 pop_state( parser );
762 return p;
766 /* handler for parser QUOTES state */
767 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos )
769 const WCHAR *p, *token_end = parser->start;
771 for (p = pos; !is_eol( parser, p ); p++)
773 if (*p == '"')
775 if (p+1 < parser->end && p[1] == '"') /* double quotes */
777 push_token( parser, p + 1 );
778 parser->start = token_end = p + 2;
779 p++;
781 else /* end of quotes */
783 push_token( parser, p );
784 parser->start = p + 1;
785 pop_state( parser );
786 return p + 1;
790 push_token( parser, p );
791 pop_state( parser );
792 return p;
796 /* handler for parser LEADING_SPACES state */
797 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos )
799 const WCHAR *p;
801 for (p = pos; !is_eol( parser, p ); p++)
803 if (*p == '\\')
805 parser->start = p;
806 set_state( parser, EOL_BACKSLASH );
807 return p;
809 if (!isspaceW(*p)) break;
811 parser->start = p;
812 pop_state( parser );
813 return p;
817 /* handler for parser TRAILING_SPACES state */
818 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos )
820 const WCHAR *p;
822 for (p = pos; !is_eol( parser, p ); p++)
824 if (*p == '\\')
826 set_state( parser, EOL_BACKSLASH );
827 return p;
829 if (!isspaceW(*p)) break;
831 pop_state( parser );
832 return p;
836 /* handler for parser COMMENT state */
837 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos )
839 const WCHAR *p = pos;
841 while (!is_eol( parser, p )) p++;
842 pop_state( parser );
843 return p;
847 /* parse a complete buffer */
848 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
849 UINT *error_line )
851 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
853 struct parser parser;
854 const WCHAR *pos = buffer;
856 parser.start = buffer;
857 parser.end = end;
858 parser.file = file;
859 parser.line = NULL;
860 parser.state = LINE_START;
861 parser.stack_pos = 0;
862 parser.cur_section = -1;
863 parser.line_pos = 1;
864 parser.error = 0;
865 parser.token_len = 0;
867 /* parser main loop */
868 while (pos) pos = (parser_funcs[parser.state])( &parser, pos );
870 /* trim excess buffer space */
871 if (file->alloc_sections > file->nb_sections)
873 file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections,
874 file->nb_sections * sizeof(file->sections[0]) );
875 file->alloc_sections = file->nb_sections;
877 if (file->alloc_fields > file->nb_fields)
879 file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields,
880 file->nb_fields * sizeof(file->fields[0]) );
881 file->alloc_fields = file->nb_fields;
883 file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings,
884 (file->string_pos - file->strings) * sizeof(WCHAR) );
886 if (parser.error)
888 if (error_line) *error_line = parser.line_pos;
889 return parser.error;
892 /* find the [strings] section */
893 file->strings_section = find_section( file, Strings );
894 return 0;
898 /* append a child INF file to its parent list, in a thread-safe manner */
899 static void append_inf_file( struct inf_file *parent, struct inf_file *child )
901 struct inf_file **ppnext = &parent->next;
902 child->next = NULL;
904 for (;;)
906 struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
907 if (!next) return;
908 ppnext = &next->next;
913 /***********************************************************************
914 * parse_file
916 * parse an INF file.
918 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, UINT *error_line )
920 void *buffer;
921 DWORD err = 0;
922 struct inf_file *file;
924 DWORD size = GetFileSize( handle, NULL );
925 HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL );
926 if (!mapping) return NULL;
927 buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size );
928 NtClose( mapping );
929 if (!buffer) return NULL;
931 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
933 if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) )))
935 err = ERROR_NOT_ENOUGH_MEMORY;
936 goto done;
939 /* we won't need more strings space than the size of the file,
940 * so we can preallocate it here
942 if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
944 err = ERROR_NOT_ENOUGH_MEMORY;
945 goto done;
947 file->string_pos = file->strings;
948 file->strings_section = -1;
950 if (!RtlIsTextUnicode( buffer, size, NULL ))
952 WCHAR *new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
953 if (new_buff)
955 DWORD len = MultiByteToWideChar( CP_ACP, 0, buffer, size, new_buff,
956 size * sizeof(WCHAR) );
957 err = parse_buffer( file, new_buff, new_buff + len, error_line );
958 HeapFree( GetProcessHeap(), 0, new_buff );
961 else err = parse_buffer( file, buffer, (WCHAR *)((char *)buffer + size), error_line );
963 if (!err) /* now check signature */
965 int version_index = find_section( file, Version );
966 if (version_index != -1)
968 struct line *line = find_line( file, version_index, Signature );
969 if (line && line->nb_fields > 0)
971 struct field *field = file->fields + line->first_field;
972 if (!strcmpiW( field->text, Chicago )) goto done;
973 if (!strcmpiW( field->text, WindowsNT )) goto done;
974 if (!strcmpiW( field->text, Windows95 )) goto done;
977 err = ERROR_WRONG_INF_STYLE;
980 done:
981 UnmapViewOfFile( buffer );
982 if (err)
984 HeapFree( GetProcessHeap(), 0, file );
985 SetLastError( err );
986 file = NULL;
988 return file;
992 /***********************************************************************
993 * PARSER_get_src_root
995 * Retrieve the source directory of an inf file.
997 const WCHAR *PARSER_get_src_root( HINF hinf )
999 struct inf_file *file = hinf;
1000 return file->src_root;
1004 /***********************************************************************
1005 * PARSER_get_dest_dir
1007 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1008 * returned buffer must be freed by caller.
1010 WCHAR *PARSER_get_dest_dir( INFCONTEXT *context )
1012 const WCHAR *dir;
1013 WCHAR *ptr, *ret;
1014 INT dirid;
1015 DWORD len1, len2;
1017 if (!SetupGetIntField( context, 1, &dirid )) return NULL;
1018 if (!(dir = DIRID_get_string( context->Inf, dirid ))) return NULL;
1019 len1 = strlenW(dir) + 1;
1020 if (!SetupGetStringFieldW( context, 2, NULL, 0, &len2 )) len2 = 0;
1021 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (len1+len2) * sizeof(WCHAR) ))) return NULL;
1022 strcpyW( ret, dir );
1023 ptr = ret + strlenW(ret);
1024 if (len2 && ptr > ret && ptr[-1] != '\\') *ptr++ = '\\';
1025 if (!SetupGetStringFieldW( context, 2, ptr, len2, NULL )) *ptr = 0;
1026 return ret;
1030 /***********************************************************************
1031 * SetupOpenInfFileA (SETUPAPI.@)
1033 HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error )
1035 UNICODE_STRING nameW, classW;
1036 HINF ret = (HINF)INVALID_HANDLE_VALUE;
1038 classW.Buffer = NULL;
1039 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class ))
1041 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1042 return ret;
1044 if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
1046 ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
1047 RtlFreeUnicodeString( &nameW );
1049 RtlFreeUnicodeString( &classW );
1050 return ret;
1054 /***********************************************************************
1055 * SetupOpenInfFileW (SETUPAPI.@)
1057 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1059 struct inf_file *file = NULL;
1060 HANDLE handle;
1061 WCHAR *path, *p;
1062 UINT len;
1064 if (strchrW( name, '\\' ) || strchrW( name, '/' ))
1066 if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return (HINF)INVALID_HANDLE_VALUE;
1067 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1069 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1070 return (HINF)INVALID_HANDLE_VALUE;
1072 GetFullPathNameW( name, len, path, NULL );
1073 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1075 else /* try Windows directory */
1077 static const WCHAR Inf[] = {'\\','i','n','f','\\',0};
1078 static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1080 len = GetWindowsDirectoryW( NULL, 0 ) + strlenW(name) + 12;
1081 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1083 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1084 return (HINF)INVALID_HANDLE_VALUE;
1086 GetWindowsDirectoryW( path, len );
1087 p = path + strlenW(path);
1088 strcpyW( p, Inf );
1089 strcatW( p, name );
1090 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1091 if (handle == INVALID_HANDLE_VALUE)
1093 strcpyW( p, System32 );
1094 strcatW( p, name );
1095 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1099 if (handle != INVALID_HANDLE_VALUE)
1101 file = parse_file( handle, class, error );
1102 CloseHandle( handle );
1104 if (!file)
1106 HeapFree( GetProcessHeap(), 0, path );
1107 return (HINF)INVALID_HANDLE_VALUE;
1109 TRACE( "%s -> %p\n", debugstr_w(path), file );
1110 file->src_root = path;
1111 if ((p = strrchrW( path, '\\' ))) p[1] = 0; /* remove file name */
1112 SetLastError( 0 );
1113 return (HINF)file;
1117 /***********************************************************************
1118 * SetupOpenAppendInfFileA (SETUPAPI.@)
1120 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
1122 HINF child_hinf;
1124 if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error );
1125 child_hinf = SetupOpenInfFileA( name, NULL, INF_STYLE_WIN4, error );
1126 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1127 append_inf_file( parent_hinf, child_hinf );
1128 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_a(name), child_hinf );
1129 return TRUE;
1133 /***********************************************************************
1134 * SetupOpenAppendInfFileW (SETUPAPI.@)
1136 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1138 HINF child_hinf;
1140 if (!name)
1142 INFCONTEXT context;
1143 WCHAR filename[MAX_PATH];
1144 int idx = 1;
1146 if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE;
1147 while (SetupGetStringFieldW( &context, idx++, filename,
1148 sizeof(filename)/sizeof(WCHAR), NULL ))
1150 child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error );
1151 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1152 append_inf_file( parent_hinf, child_hinf );
1153 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf );
1155 return TRUE;
1157 child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error );
1158 if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1159 append_inf_file( parent_hinf, child_hinf );
1160 TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf );
1161 return TRUE;
1165 /***********************************************************************
1166 * SetupOpenMasterInf (SETUPAPI.@)
1168 HINF WINAPI SetupOpenMasterInf( VOID )
1170 static const WCHAR Layout[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1171 WCHAR Buffer[MAX_PATH];
1173 GetWindowsDirectoryW( Buffer, MAX_PATH );
1174 strcatW( Buffer, Layout );
1175 return SetupOpenInfFileW( Buffer, NULL, INF_STYLE_WIN4, NULL);
1180 /***********************************************************************
1181 * SetupCloseInfFile (SETUPAPI.@)
1183 void WINAPI SetupCloseInfFile( HINF hinf )
1185 struct inf_file *file = hinf;
1186 unsigned int i;
1188 for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
1189 HeapFree( GetProcessHeap(), 0, file->src_root );
1190 HeapFree( GetProcessHeap(), 0, file->sections );
1191 HeapFree( GetProcessHeap(), 0, file->fields );
1192 HeapFree( GetProcessHeap(), 0, file->strings );
1193 HeapFree( GetProcessHeap(), 0, file );
1197 /***********************************************************************
1198 * SetupGetLineCountA (SETUPAPI.@)
1200 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1202 UNICODE_STRING sectionW;
1203 LONG ret = -1;
1205 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, name ))
1206 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1207 else
1209 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1210 RtlFreeUnicodeString( &sectionW );
1212 return ret;
1216 /***********************************************************************
1217 * SetupGetLineCountW (SETUPAPI.@)
1219 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1221 struct inf_file *file = hinf;
1222 int section_index;
1223 LONG ret = -1;
1225 for (file = hinf; file; file = file->next)
1227 if ((section_index = find_section( file, section )) == -1) continue;
1228 if (ret == -1) ret = 0;
1229 ret += file->sections[section_index]->nb_lines;
1231 TRACE( "(%p,%s) returning %ld\n", hinf, debugstr_w(section), ret );
1232 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1233 return ret;
1237 /***********************************************************************
1238 * SetupGetLineByIndexA (SETUPAPI.@)
1240 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1242 UNICODE_STRING sectionW;
1243 BOOL ret = FALSE;
1245 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1246 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1247 else
1249 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1250 RtlFreeUnicodeString( &sectionW );
1252 return ret;
1256 /***********************************************************************
1257 * SetupGetLineByIndexW (SETUPAPI.@)
1259 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1261 struct inf_file *file = hinf;
1262 int section_index;
1264 SetLastError( ERROR_SECTION_NOT_FOUND );
1265 for (file = hinf; file; file = file->next)
1267 if ((section_index = find_section( file, section )) == -1) continue;
1268 SetLastError( ERROR_LINE_NOT_FOUND );
1269 if (index < file->sections[section_index]->nb_lines)
1271 context->Inf = hinf;
1272 context->CurrentInf = file;
1273 context->Section = section_index;
1274 context->Line = index;
1275 SetLastError( 0 );
1276 TRACE( "(%p,%s): returning %d/%ld\n",
1277 hinf, debugstr_w(section), section_index, index );
1278 return TRUE;
1280 index -= file->sections[section_index]->nb_lines;
1282 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1283 return FALSE;
1287 /***********************************************************************
1288 * SetupFindFirstLineA (SETUPAPI.@)
1290 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1292 UNICODE_STRING sectionW, keyW;
1293 BOOL ret = FALSE;
1295 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1297 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1298 return FALSE;
1301 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1302 else
1304 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1306 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1307 RtlFreeUnicodeString( &keyW );
1309 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1311 RtlFreeUnicodeString( &sectionW );
1312 return ret;
1316 /***********************************************************************
1317 * SetupFindFirstLineW (SETUPAPI.@)
1319 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1321 struct inf_file *file;
1322 int section_index;
1324 SetLastError( ERROR_SECTION_NOT_FOUND );
1325 for (file = hinf; file; file = file->next)
1327 if ((section_index = find_section( file, section )) == -1) continue;
1328 if (key)
1330 INFCONTEXT ctx;
1331 ctx.Inf = hinf;
1332 ctx.CurrentInf = file;
1333 ctx.Section = section_index;
1334 ctx.Line = -1;
1335 return SetupFindNextMatchLineW( &ctx, key, context );
1337 SetLastError( ERROR_LINE_NOT_FOUND ); /* found at least one section */
1338 if (file->sections[section_index]->nb_lines)
1340 context->Inf = hinf;
1341 context->CurrentInf = file;
1342 context->Section = section_index;
1343 context->Line = 0;
1344 SetLastError( 0 );
1345 TRACE( "(%p,%s,%s): returning %d/0\n",
1346 hinf, debugstr_w(section), debugstr_w(key), section_index );
1347 return TRUE;
1350 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1351 return FALSE;
1355 /***********************************************************************
1356 * SetupFindNextLine (SETUPAPI.@)
1358 BOOL WINAPI SetupFindNextLine( PINFCONTEXT context_in, PINFCONTEXT context_out )
1360 struct inf_file *file = context_in->CurrentInf;
1361 struct section *section;
1363 if (context_in->Section >= file->nb_sections) goto error;
1365 section = file->sections[context_in->Section];
1366 if (context_in->Line+1 < section->nb_lines)
1368 if (context_out != context_in) *context_out = *context_in;
1369 context_out->Line++;
1370 SetLastError( 0 );
1371 return TRUE;
1374 /* now search the appended files */
1376 for (file = file->next; file; file = file->next)
1378 int section_index = find_section( file, section->name );
1379 if (section_index == -1) continue;
1380 if (file->sections[section_index]->nb_lines)
1382 context_out->Inf = context_in->Inf;
1383 context_out->CurrentInf = file;
1384 context_out->Section = section_index;
1385 context_out->Line = 0;
1386 SetLastError( 0 );
1387 return TRUE;
1390 error:
1391 SetLastError( ERROR_LINE_NOT_FOUND );
1392 return FALSE;
1396 /***********************************************************************
1397 * SetupFindNextMatchLineA (SETUPAPI.@)
1399 BOOL WINAPI SetupFindNextMatchLineA( PINFCONTEXT context_in, PCSTR key,
1400 PINFCONTEXT context_out )
1402 UNICODE_STRING keyW;
1403 BOOL ret = FALSE;
1405 if (!key) return SetupFindNextLine( context_in, context_out );
1407 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1408 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1409 else
1411 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1412 RtlFreeUnicodeString( &keyW );
1414 return ret;
1418 /***********************************************************************
1419 * SetupFindNextMatchLineW (SETUPAPI.@)
1421 BOOL WINAPI SetupFindNextMatchLineW( PINFCONTEXT context_in, PCWSTR key,
1422 PINFCONTEXT context_out )
1424 struct inf_file *file = context_in->CurrentInf;
1425 struct section *section;
1426 struct line *line;
1427 unsigned int i;
1429 if (!key) return SetupFindNextLine( context_in, context_out );
1431 if (context_in->Section >= file->nb_sections) goto error;
1433 section = file->sections[context_in->Section];
1435 for (i = context_in->Line+1, line = &section->lines[i]; i < section->nb_lines; i++, line++)
1437 if (line->key_field == -1) continue;
1438 if (!strcmpiW( key, file->fields[line->key_field].text ))
1440 if (context_out != context_in) *context_out = *context_in;
1441 context_out->Line = i;
1442 SetLastError( 0 );
1443 TRACE( "(%p,%s,%s): returning %d\n",
1444 file, debugstr_w(section->name), debugstr_w(key), i );
1445 return TRUE;
1449 /* now search the appended files */
1451 for (file = file->next; file; file = file->next)
1453 int section_index = find_section( file, section->name );
1454 if (section_index == -1) continue;
1455 section = file->sections[section_index];
1456 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1458 if (line->key_field == -1) continue;
1459 if (!strcmpiW( key, file->fields[line->key_field].text ))
1461 context_out->Inf = context_in->Inf;
1462 context_out->CurrentInf = file;
1463 context_out->Section = section_index;
1464 context_out->Line = i;
1465 SetLastError( 0 );
1466 TRACE( "(%p,%s,%s): returning %d/%d\n",
1467 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1468 return TRUE;
1472 TRACE( "(%p,%s,%s): not found\n",
1473 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1474 error:
1475 SetLastError( ERROR_LINE_NOT_FOUND );
1476 return FALSE;
1480 /***********************************************************************
1481 * SetupGetLineTextW (SETUPAPI.@)
1483 BOOL WINAPI SetupGetLineTextW( PINFCONTEXT context, HINF hinf, PCWSTR section_name,
1484 PCWSTR key_name, PWSTR buffer, DWORD size, PDWORD required )
1486 struct inf_file *file;
1487 struct line *line;
1488 struct field *field;
1489 int i;
1490 DWORD total = 0;
1492 if (!context)
1494 INFCONTEXT new_context;
1495 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1496 file = new_context.CurrentInf;
1497 line = get_line( file, new_context.Section, new_context.Line );
1499 else
1501 file = context->CurrentInf;
1502 if (!(line = get_line( file, context->Section, context->Line )))
1504 SetLastError( ERROR_LINE_NOT_FOUND );
1505 return FALSE;
1509 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1510 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1512 if (required) *required = total;
1513 if (buffer)
1515 if (total > size)
1517 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1518 return FALSE;
1520 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1522 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1523 if (i+1 < line->nb_fields) buffer[len] = ',';
1524 buffer += len + 1;
1527 return TRUE;
1531 /***********************************************************************
1532 * SetupGetLineTextA (SETUPAPI.@)
1534 BOOL WINAPI SetupGetLineTextA( PINFCONTEXT context, HINF hinf, PCSTR section_name,
1535 PCSTR key_name, PSTR buffer, DWORD size, PDWORD required )
1537 struct inf_file *file;
1538 struct line *line;
1539 struct field *field;
1540 int i;
1541 DWORD total = 0;
1543 if (!context)
1545 INFCONTEXT new_context;
1546 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1547 file = new_context.CurrentInf;
1548 line = get_line( file, new_context.Section, new_context.Line );
1550 else
1552 file = context->CurrentInf;
1553 if (!(line = get_line( file, context->Section, context->Line )))
1555 SetLastError( ERROR_LINE_NOT_FOUND );
1556 return FALSE;
1560 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1561 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1563 if (required) *required = total;
1564 if (buffer)
1566 if (total > size)
1568 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1569 return FALSE;
1571 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1573 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1574 if (i+1 < line->nb_fields) buffer[len] = ',';
1575 buffer += len + 1;
1578 return TRUE;
1582 /***********************************************************************
1583 * SetupGetFieldCount (SETUPAPI.@)
1585 DWORD WINAPI SetupGetFieldCount( PINFCONTEXT context )
1587 struct inf_file *file = context->CurrentInf;
1588 struct line *line = get_line( file, context->Section, context->Line );
1590 if (!line) return 0;
1591 return line->nb_fields;
1595 /***********************************************************************
1596 * SetupGetStringFieldA (SETUPAPI.@)
1598 BOOL WINAPI SetupGetStringFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1599 DWORD size, PDWORD required )
1601 struct inf_file *file = context->CurrentInf;
1602 struct field *field = get_field( file, context->Section, context->Line, index );
1603 unsigned int len;
1605 SetLastError(0);
1606 if (!field) return FALSE;
1607 len = PARSER_string_substA( file, field->text, NULL, 0 );
1608 if (required) *required = len + 1;
1609 if (buffer)
1611 if (size <= len)
1613 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1614 return FALSE;
1616 PARSER_string_substA( file, field->text, buffer, size );
1618 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1619 context->Inf, context->CurrentInf, context->Section, context->Line,
1620 index, debugstr_a(buffer) );
1622 return TRUE;
1626 /***********************************************************************
1627 * SetupGetStringFieldW (SETUPAPI.@)
1629 BOOL WINAPI SetupGetStringFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1630 DWORD size, PDWORD required )
1632 struct inf_file *file = context->CurrentInf;
1633 struct field *field = get_field( file, context->Section, context->Line, index );
1634 unsigned int len;
1636 SetLastError(0);
1637 if (!field) return FALSE;
1638 len = PARSER_string_substW( file, field->text, NULL, 0 );
1639 if (required) *required = len + 1;
1640 if (buffer)
1642 if (size <= len)
1644 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1645 return FALSE;
1647 PARSER_string_substW( file, field->text, buffer, size );
1649 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1650 context->Inf, context->CurrentInf, context->Section, context->Line,
1651 index, debugstr_w(buffer) );
1653 return TRUE;
1657 /***********************************************************************
1658 * SetupGetIntField (SETUPAPI.@)
1660 BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result )
1662 char localbuff[20];
1663 char *end, *buffer = localbuff;
1664 DWORD required;
1665 INT res;
1666 BOOL ret = FALSE;
1668 if (!SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))
1670 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1671 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1672 if (!SetupGetStringFieldA( context, index, buffer, required, NULL )) goto done;
1674 res = strtol( buffer, &end, 0 );
1675 if (end != buffer && !*end)
1677 *result = res;
1678 ret = TRUE;
1680 else SetLastError( ERROR_INVALID_DATA );
1682 done:
1683 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1684 return ret;
1688 /***********************************************************************
1689 * SetupGetBinaryField (SETUPAPI.@)
1691 BOOL WINAPI SetupGetBinaryField( PINFCONTEXT context, DWORD index, BYTE *buffer,
1692 DWORD size, LPDWORD required )
1694 struct inf_file *file = context->CurrentInf;
1695 struct line *line = get_line( file, context->Section, context->Line );
1696 struct field *field;
1697 int i;
1699 if (!line)
1701 SetLastError( ERROR_LINE_NOT_FOUND );
1702 return FALSE;
1704 if (!index || index >= line->nb_fields)
1706 SetLastError( ERROR_INVALID_PARAMETER );
1707 return FALSE;
1709 index--; /* fields start at 0 */
1710 if (required) *required = line->nb_fields - index;
1711 if (!buffer) return TRUE;
1712 if (size < line->nb_fields - index)
1714 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1715 return FALSE;
1717 field = &file->fields[line->first_field + index];
1718 for (i = index; i < line->nb_fields; i++, field++)
1720 const WCHAR *p;
1721 DWORD value = 0;
1722 for (p = field->text; *p && isxdigitW(*p); p++)
1724 if ((value <<= 4) > 255)
1726 SetLastError( ERROR_INVALID_DATA );
1727 return FALSE;
1729 if (*p <= '9') value |= (*p - '0');
1730 else value |= (tolowerW(*p) - 'a' + 10);
1732 buffer[i - index] = value;
1734 if (TRACE_ON(setupapi))
1736 TRACE( "%p/%p/%d/%d index %ld returning",
1737 context->Inf, context->CurrentInf, context->Section, context->Line, index );
1738 for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] );
1739 TRACE( "\n" );
1741 return TRUE;
1745 /***********************************************************************
1746 * SetupGetMultiSzFieldA (SETUPAPI.@)
1748 BOOL WINAPI SetupGetMultiSzFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1749 DWORD size, LPDWORD required )
1751 struct inf_file *file = context->CurrentInf;
1752 struct line *line = get_line( file, context->Section, context->Line );
1753 struct field *field;
1754 unsigned int len;
1755 int i;
1756 DWORD total = 1;
1758 if (!line)
1760 SetLastError( ERROR_LINE_NOT_FOUND );
1761 return FALSE;
1763 if (!index || index >= line->nb_fields)
1765 SetLastError( ERROR_INVALID_PARAMETER );
1766 return FALSE;
1768 index--; /* fields start at 0 */
1769 field = &file->fields[line->first_field + index];
1770 for (i = index; i < line->nb_fields; i++, field++)
1772 if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1773 total += len + 1;
1776 if (required) *required = total;
1777 if (!buffer) return TRUE;
1778 if (total > size)
1780 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1781 return FALSE;
1783 field = &file->fields[line->first_field + index];
1784 for (i = index; i < line->nb_fields; i++, field++)
1786 if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1787 buffer += len + 1;
1789 *buffer = 0; /* add final null */
1790 return TRUE;
1794 /***********************************************************************
1795 * SetupGetMultiSzFieldW (SETUPAPI.@)
1797 BOOL WINAPI SetupGetMultiSzFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1798 DWORD size, LPDWORD required )
1800 struct inf_file *file = context->CurrentInf;
1801 struct line *line = get_line( file, context->Section, context->Line );
1802 struct field *field;
1803 unsigned int len;
1804 int i;
1805 DWORD total = 1;
1807 if (!line)
1809 SetLastError( ERROR_LINE_NOT_FOUND );
1810 return FALSE;
1812 if (!index || index >= line->nb_fields)
1814 SetLastError( ERROR_INVALID_PARAMETER );
1815 return FALSE;
1817 index--; /* fields start at 0 */
1818 field = &file->fields[line->first_field + index];
1819 for (i = index; i < line->nb_fields; i++, field++)
1821 if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1822 total += len + 1;
1825 if (required) *required = total;
1826 if (!buffer) return TRUE;
1827 if (total > size)
1829 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1830 return FALSE;
1832 field = &file->fields[line->first_field + index];
1833 for (i = index; i < line->nb_fields; i++, field++)
1835 if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1836 buffer += len + 1;
1838 *buffer = 0; /* add final null */
1839 return TRUE;