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
22 #include "wine/port.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 */
56 const WCHAR *text; /* field text */
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 */
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) */
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;
85 int strings_section; /* index of [Strings] section or -1 if none */
86 WCHAR *src_root; /* source root directory */
89 /* parser definitions */
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 */
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 )
161 unsigned int new_count = *count + *count / 2;
162 if (new_count < 32) new_count = 32;
165 new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem );
167 new_array = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * elem );
173 HeapFree( GetProcessHeap(), 0, array );
178 /* find a section by name */
179 static int find_section( struct inf_file *file, const WCHAR *name )
183 for (i = 0; i < file->nb_sections; i++)
184 if (!strcmpiW( name, file->sections[i]->name )) return i;
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;
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;
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;
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 = §ion->lines[section->nb_lines++];
243 line->first_field = file->nb_fields;
245 line->key_field = -1;
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 §ion->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,
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];
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 )
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++];
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);
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;
318 WCHAR *dirid_str, *end;
319 const WCHAR *ret = NULL;
321 if (!*len) /* empty string (%%) is replaced by single 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 );
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) );
344 dirid = strtolW( dirid_str, &end, 10 );
345 if (!*end) ret = get_dirid_subst( dirid, len );
346 HeapFree( GetProcessHeap(), 0, dirid_str );
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,
359 const WCHAR *start, *subst, *p;
360 unsigned int len, total = 0;
363 if (!buffer) size = MAX_STRING_LEN + 1;
364 for (p = start = text; *p; p++)
366 if (*p != '%') continue;
368 if (inside) /* start of a %xx% string */
371 if (len > size - 1) len = size - 1;
372 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
377 else /* end of the %xx% string, find substitution */
380 subst = get_string_subst( file, start + 1, &len );
386 if (len > size - 1) len = size - 1;
387 if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
394 if (start != p) /* unfinished string, copy it */
397 if (len > size - 1) len = size - 1;
398 if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
401 if (buffer && size) buffer[total] = 0;
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,
412 WCHAR buffW[MAX_STRING_LEN+1];
415 unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) );
416 if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
419 RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
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;
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;
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 : ' ';
492 /* add a section with the current token as name */
493 static int add_section_from_token( struct parser *parser )
497 if (parser->token_len > MAX_SECTION_NAME_LEN)
499 parser->error = ERROR_SECTION_NAME_TOO_LONG;
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;
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 )
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;
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++;
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;
549 parser->error = ERROR_NOT_ENOUGH_MEMORY;
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;
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;
569 /* handler for parser LINE_START state */
570 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
574 for (p = pos; !is_eof( parser, p ); p++)
580 close_current_line( parser );
583 push_state( parser, LINE_START );
584 set_state( parser, COMMENT );
587 parser->start = p + 1;
588 set_state( parser, SECTION_NAME );
594 set_state( parser, KEY_NAME );
600 close_current_line( parser );
605 /* handler for parser SECTION_NAME state */
606 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
610 for (p = pos; !is_eol( parser, p ); 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 */
621 parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */
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;
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 );
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 );
651 push_token( parser, token_end );
652 parser->start = p + 1;
653 push_state( parser, KEY_NAME );
654 set_state( parser, QUOTES );
657 push_token( parser, token_end );
659 push_state( parser, KEY_NAME );
660 set_state( parser, EOL_BACKSLASH );
663 if (!isspaceW(*p)) token_end = p + 1;
666 push_token( parser, p );
667 push_state( parser, KEY_NAME );
668 set_state( parser, TRAILING_SPACES );
674 push_token( parser, token_end );
675 set_state( parser, VALUE_NAME );
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++)
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 );
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 );
703 push_token( parser, token_end );
704 parser->start = p + 1;
705 push_state( parser, VALUE_NAME );
706 set_state( parser, QUOTES );
709 push_token( parser, token_end );
711 push_state( parser, VALUE_NAME );
712 set_state( parser, EOL_BACKSLASH );
715 if (!isspaceW(*p)) token_end = p + 1;
718 push_token( parser, p );
719 push_state( parser, VALUE_NAME );
720 set_state( parser, TRAILING_SPACES );
726 push_token( parser, token_end );
727 if (!add_field_from_token( parser, 0 )) return NULL;
728 set_state( parser, LINE_START );
733 /* handler for parser EOL_BACKSLASH state */
734 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos )
738 for (p = pos; !is_eof( parser, p ); p++)
744 parser->start = p + 1;
745 set_state( parser, LEADING_SPACES );
750 push_state( parser, EOL_BACKSLASH );
751 set_state( parser, COMMENT );
754 if (isspaceW(*p)) continue;
755 push_token( parser, 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++)
775 if (p+1 < parser->end && p[1] == '"') /* double quotes */
777 push_token( parser, p + 1 );
778 parser->start = token_end = p + 2;
781 else /* end of quotes */
783 push_token( parser, p );
784 parser->start = p + 1;
790 push_token( parser, p );
796 /* handler for parser LEADING_SPACES state */
797 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos )
801 for (p = pos; !is_eol( parser, p ); p++)
806 set_state( parser, EOL_BACKSLASH );
809 if (!isspaceW(*p)) break;
817 /* handler for parser TRAILING_SPACES state */
818 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos )
822 for (p = pos; !is_eol( parser, p ); p++)
826 set_state( parser, EOL_BACKSLASH );
829 if (!isspaceW(*p)) break;
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++;
847 /* parse a complete buffer */
848 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
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;
860 parser.state = LINE_START;
861 parser.stack_pos = 0;
862 parser.cur_section = -1;
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) );
888 if (error_line) *error_line = parser.line_pos;
892 /* find the [strings] section */
893 file->strings_section = find_section( file, Strings );
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;
906 struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
908 ppnext = &next->next;
913 /***********************************************************************
918 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, UINT *error_line )
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 );
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;
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;
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) );
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;
981 UnmapViewOfFile( buffer );
984 HeapFree( GetProcessHeap(), 0, 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 )
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;
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 );
1044 if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
1046 ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
1047 RtlFreeUnicodeString( &nameW );
1049 RtlFreeUnicodeString( &classW );
1054 /***********************************************************************
1055 * SetupOpenInfFileW (SETUPAPI.@)
1057 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1059 struct inf_file *file = NULL;
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);
1090 handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1091 if (handle == INVALID_HANDLE_VALUE)
1093 strcpyW( p, System32 );
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 );
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 */
1117 /***********************************************************************
1118 * SetupOpenAppendInfFileA (SETUPAPI.@)
1120 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
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 );
1133 /***********************************************************************
1134 * SetupOpenAppendInfFileW (SETUPAPI.@)
1136 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1143 WCHAR filename[MAX_PATH];
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 );
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 );
1165 /***********************************************************************
1166 * SetupCloseInfFile (SETUPAPI.@)
1168 void WINAPI SetupCloseInfFile( HINF hinf )
1170 struct inf_file *file = hinf;
1173 for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
1174 HeapFree( GetProcessHeap(), 0, file->src_root );
1175 HeapFree( GetProcessHeap(), 0, file->sections );
1176 HeapFree( GetProcessHeap(), 0, file->fields );
1177 HeapFree( GetProcessHeap(), 0, file->strings );
1178 HeapFree( GetProcessHeap(), 0, file );
1182 /***********************************************************************
1183 * SetupGetLineCountA (SETUPAPI.@)
1185 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1187 UNICODE_STRING sectionW;
1190 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, name ))
1191 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1194 ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1195 RtlFreeUnicodeString( §ionW );
1201 /***********************************************************************
1202 * SetupGetLineCountW (SETUPAPI.@)
1204 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1206 struct inf_file *file = hinf;
1210 for (file = hinf; file; file = file->next)
1212 if ((section_index = find_section( file, section )) == -1) continue;
1213 if (ret == -1) ret = 0;
1214 ret += file->sections[section_index]->nb_lines;
1216 TRACE( "(%p,%s) returning %ld\n", hinf, debugstr_w(section), ret );
1217 SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1222 /***********************************************************************
1223 * SetupGetLineByIndexA (SETUPAPI.@)
1225 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1227 UNICODE_STRING sectionW;
1230 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
1231 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1234 ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1235 RtlFreeUnicodeString( §ionW );
1241 /***********************************************************************
1242 * SetupGetLineByIndexW (SETUPAPI.@)
1244 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1246 struct inf_file *file = hinf;
1249 SetLastError( ERROR_SECTION_NOT_FOUND );
1250 for (file = hinf; file; file = file->next)
1252 if ((section_index = find_section( file, section )) == -1) continue;
1253 SetLastError( ERROR_LINE_NOT_FOUND );
1254 if (index < file->sections[section_index]->nb_lines)
1256 context->Inf = hinf;
1257 context->CurrentInf = file;
1258 context->Section = section_index;
1259 context->Line = index;
1261 TRACE( "(%p,%s): returning %d/%ld\n",
1262 hinf, debugstr_w(section), section_index, index );
1265 index -= file->sections[section_index]->nb_lines;
1267 TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1272 /***********************************************************************
1273 * SetupFindFirstLineA (SETUPAPI.@)
1275 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1277 UNICODE_STRING sectionW, keyW;
1280 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
1282 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1286 if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1289 if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1291 ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1292 RtlFreeUnicodeString( &keyW );
1294 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1296 RtlFreeUnicodeString( §ionW );
1301 /***********************************************************************
1302 * SetupFindFirstLineW (SETUPAPI.@)
1304 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1306 struct inf_file *file;
1309 SetLastError( ERROR_SECTION_NOT_FOUND );
1310 for (file = hinf; file; file = file->next)
1312 if ((section_index = find_section( file, section )) == -1) continue;
1317 ctx.CurrentInf = file;
1318 ctx.Section = section_index;
1320 return SetupFindNextMatchLineW( &ctx, key, context );
1322 SetLastError( ERROR_LINE_NOT_FOUND ); /* found at least one section */
1323 if (file->sections[section_index]->nb_lines)
1325 context->Inf = hinf;
1326 context->CurrentInf = file;
1327 context->Section = section_index;
1330 TRACE( "(%p,%s,%s): returning %d/0\n",
1331 hinf, debugstr_w(section), debugstr_w(key), section_index );
1335 TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1340 /***********************************************************************
1341 * SetupFindNextLine (SETUPAPI.@)
1343 BOOL WINAPI SetupFindNextLine( PINFCONTEXT context_in, PINFCONTEXT context_out )
1345 struct inf_file *file = context_in->CurrentInf;
1346 struct section *section;
1348 if (context_in->Section >= file->nb_sections) goto error;
1350 section = file->sections[context_in->Section];
1351 if (context_in->Line+1 < section->nb_lines)
1353 if (context_out != context_in) *context_out = *context_in;
1354 context_out->Line++;
1359 /* now search the appended files */
1361 for (file = file->next; file; file = file->next)
1363 int section_index = find_section( file, section->name );
1364 if (section_index == -1) continue;
1365 if (file->sections[section_index]->nb_lines)
1367 context_out->Inf = context_in->Inf;
1368 context_out->CurrentInf = file;
1369 context_out->Section = section_index;
1370 context_out->Line = 0;
1376 SetLastError( ERROR_LINE_NOT_FOUND );
1381 /***********************************************************************
1382 * SetupFindNextMatchLineA (SETUPAPI.@)
1384 BOOL WINAPI SetupFindNextMatchLineA( PINFCONTEXT context_in, PCSTR key,
1385 PINFCONTEXT context_out )
1387 UNICODE_STRING keyW;
1390 if (!key) return SetupFindNextLine( context_in, context_out );
1392 if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1393 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1396 ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1397 RtlFreeUnicodeString( &keyW );
1403 /***********************************************************************
1404 * SetupFindNextMatchLineW (SETUPAPI.@)
1406 BOOL WINAPI SetupFindNextMatchLineW( PINFCONTEXT context_in, PCWSTR key,
1407 PINFCONTEXT context_out )
1409 struct inf_file *file = context_in->CurrentInf;
1410 struct section *section;
1414 if (!key) return SetupFindNextLine( context_in, context_out );
1416 if (context_in->Section >= file->nb_sections) goto error;
1418 section = file->sections[context_in->Section];
1420 for (i = context_in->Line+1, line = §ion->lines[i]; i < section->nb_lines; i++, line++)
1422 if (line->key_field == -1) continue;
1423 if (!strcmpiW( key, file->fields[line->key_field].text ))
1425 if (context_out != context_in) *context_out = *context_in;
1426 context_out->Line = i;
1428 TRACE( "(%p,%s,%s): returning %d\n",
1429 file, debugstr_w(section->name), debugstr_w(key), i );
1434 /* now search the appended files */
1436 for (file = file->next; file; file = file->next)
1438 int section_index = find_section( file, section->name );
1439 if (section_index == -1) continue;
1440 section = file->sections[section_index];
1441 for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1443 if (line->key_field == -1) continue;
1444 if (!strcmpiW( key, file->fields[line->key_field].text ))
1446 context_out->Inf = context_in->Inf;
1447 context_out->CurrentInf = file;
1448 context_out->Section = section_index;
1449 context_out->Line = i;
1451 TRACE( "(%p,%s,%s): returning %d/%d\n",
1452 file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1457 TRACE( "(%p,%s,%s): not found\n",
1458 context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1460 SetLastError( ERROR_LINE_NOT_FOUND );
1465 /***********************************************************************
1466 * SetupGetLineTextW (SETUPAPI.@)
1468 BOOL WINAPI SetupGetLineTextW( PINFCONTEXT context, HINF hinf, PCWSTR section_name,
1469 PCWSTR key_name, PWSTR buffer, DWORD size, PDWORD required )
1471 struct inf_file *file;
1473 struct field *field;
1479 INFCONTEXT new_context;
1480 if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1481 file = new_context.CurrentInf;
1482 line = get_line( file, new_context.Section, new_context.Line );
1486 file = context->CurrentInf;
1487 if (!(line = get_line( file, context->Section, context->Line )))
1489 SetLastError( ERROR_LINE_NOT_FOUND );
1494 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1495 total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1497 if (required) *required = total;
1502 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1505 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1507 unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1508 if (i+1 < line->nb_fields) buffer[len] = ',';
1516 /***********************************************************************
1517 * SetupGetLineTextA (SETUPAPI.@)
1519 BOOL WINAPI SetupGetLineTextA( PINFCONTEXT context, HINF hinf, PCSTR section_name,
1520 PCSTR key_name, PSTR buffer, DWORD size, PDWORD required )
1522 struct inf_file *file;
1524 struct field *field;
1530 INFCONTEXT new_context;
1531 if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1532 file = new_context.CurrentInf;
1533 line = get_line( file, new_context.Section, new_context.Line );
1537 file = context->CurrentInf;
1538 if (!(line = get_line( file, context->Section, context->Line )))
1540 SetLastError( ERROR_LINE_NOT_FOUND );
1545 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1546 total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1548 if (required) *required = total;
1553 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1556 for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1558 unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1559 if (i+1 < line->nb_fields) buffer[len] = ',';
1567 /***********************************************************************
1568 * SetupGetFieldCount (SETUPAPI.@)
1570 DWORD WINAPI SetupGetFieldCount( PINFCONTEXT context )
1572 struct inf_file *file = context->CurrentInf;
1573 struct line *line = get_line( file, context->Section, context->Line );
1575 if (!line) return 0;
1576 return line->nb_fields;
1580 /***********************************************************************
1581 * SetupGetStringFieldA (SETUPAPI.@)
1583 BOOL WINAPI SetupGetStringFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1584 DWORD size, PDWORD required )
1586 struct inf_file *file = context->CurrentInf;
1587 struct field *field = get_field( file, context->Section, context->Line, index );
1591 if (!field) return FALSE;
1592 len = PARSER_string_substA( file, field->text, NULL, 0 );
1593 if (required) *required = len + 1;
1598 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1601 PARSER_string_substA( file, field->text, buffer, size );
1603 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1604 context->Inf, context->CurrentInf, context->Section, context->Line,
1605 index, debugstr_a(buffer) );
1611 /***********************************************************************
1612 * SetupGetStringFieldW (SETUPAPI.@)
1614 BOOL WINAPI SetupGetStringFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1615 DWORD size, PDWORD required )
1617 struct inf_file *file = context->CurrentInf;
1618 struct field *field = get_field( file, context->Section, context->Line, index );
1622 if (!field) return FALSE;
1623 len = PARSER_string_substW( file, field->text, NULL, 0 );
1624 if (required) *required = len + 1;
1629 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1632 PARSER_string_substW( file, field->text, buffer, size );
1634 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1635 context->Inf, context->CurrentInf, context->Section, context->Line,
1636 index, debugstr_w(buffer) );
1642 /***********************************************************************
1643 * SetupGetIntField (SETUPAPI.@)
1645 BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result )
1648 char *end, *buffer = localbuff;
1653 if (!SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))
1655 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1656 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1657 if (!SetupGetStringFieldA( context, index, buffer, required, NULL )) goto done;
1659 res = strtol( buffer, &end, 0 );
1660 if (end != buffer && !*end)
1665 else SetLastError( ERROR_INVALID_DATA );
1668 if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1673 /***********************************************************************
1674 * SetupGetBinaryField (SETUPAPI.@)
1676 BOOL WINAPI SetupGetBinaryField( PINFCONTEXT context, DWORD index, BYTE *buffer,
1677 DWORD size, LPDWORD required )
1679 struct inf_file *file = context->CurrentInf;
1680 struct line *line = get_line( file, context->Section, context->Line );
1681 struct field *field;
1686 SetLastError( ERROR_LINE_NOT_FOUND );
1689 if (!index || index >= line->nb_fields)
1691 SetLastError( ERROR_INVALID_PARAMETER );
1694 index--; /* fields start at 0 */
1695 if (required) *required = line->nb_fields - index;
1696 if (!buffer) return TRUE;
1697 if (size < line->nb_fields - index)
1699 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1702 field = &file->fields[line->first_field + index];
1703 for (i = index; i < line->nb_fields; i++, field++)
1707 for (p = field->text; *p && isxdigitW(*p); p++)
1709 if ((value <<= 4) > 255)
1711 SetLastError( ERROR_INVALID_DATA );
1714 if (*p <= '9') value |= (*p - '0');
1715 else value |= (tolowerW(*p) - 'a' + 10);
1717 buffer[i - index] = value;
1719 if (TRACE_ON(setupapi))
1721 TRACE( "%p/%p/%d/%d index %ld returning",
1722 context->Inf, context->CurrentInf, context->Section, context->Line, index );
1723 for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] );
1730 /***********************************************************************
1731 * SetupGetMultiSzFieldA (SETUPAPI.@)
1733 BOOL WINAPI SetupGetMultiSzFieldA( PINFCONTEXT context, DWORD index, PSTR buffer,
1734 DWORD size, LPDWORD required )
1736 struct inf_file *file = context->CurrentInf;
1737 struct line *line = get_line( file, context->Section, context->Line );
1738 struct field *field;
1745 SetLastError( ERROR_LINE_NOT_FOUND );
1748 if (!index || index >= line->nb_fields)
1750 SetLastError( ERROR_INVALID_PARAMETER );
1753 index--; /* fields start at 0 */
1754 field = &file->fields[line->first_field + index];
1755 for (i = index; i < line->nb_fields; i++, field++)
1757 if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1761 if (required) *required = total;
1762 if (!buffer) return TRUE;
1765 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1768 field = &file->fields[line->first_field + index];
1769 for (i = index; i < line->nb_fields; i++, field++)
1771 if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1774 *buffer = 0; /* add final null */
1779 /***********************************************************************
1780 * SetupGetMultiSzFieldW (SETUPAPI.@)
1782 BOOL WINAPI SetupGetMultiSzFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer,
1783 DWORD size, LPDWORD required )
1785 struct inf_file *file = context->CurrentInf;
1786 struct line *line = get_line( file, context->Section, context->Line );
1787 struct field *field;
1794 SetLastError( ERROR_LINE_NOT_FOUND );
1797 if (!index || index >= line->nb_fields)
1799 SetLastError( ERROR_INVALID_PARAMETER );
1802 index--; /* fields start at 0 */
1803 field = &file->fields[line->first_field + index];
1804 for (i = index; i < line->nb_fields; i++, field++)
1806 if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1810 if (required) *required = total;
1811 if (!buffer) return TRUE;
1814 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1817 field = &file->fields[line->first_field + index];
1818 for (i = index; i < line->nb_fields; i++, field++)
1820 if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1823 *buffer = 0; /* add final null */