Added a few stubs.
[wine] / dlls / setupapi / parser.c
1 /*
2  * INF file parsing
3  *
4  * Copyright 2002 Alexandre Julliard for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
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
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
44
45 #define CONTROL_Z  '\x1a'
46 #define MAX_SECTION_NAME_LEN  255
47 #define MAX_FIELD_LEN         511  /* larger fields get silently truncated */
48 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
49 #define MAX_STRING_LEN        (MAX_INF_STRING_LENGTH+1)
50
51 /* inf file structure definitions */
52
53 struct field
54 {
55     const WCHAR *text;         /* field text */
56 };
57
58 struct line
59 {
60     int first_field;           /* index of first field in field array */
61     int nb_fields;             /* number of fields in line */
62     int key_field;             /* index of field for key or -1 if no key */
63 };
64
65 struct section
66 {
67     const WCHAR *name;         /* section name */
68     unsigned int nb_lines;     /* number of used lines */
69     unsigned int alloc_lines;  /* total number of allocated lines in array below */
70     struct line  lines[16];    /* lines information (grown dynamically, 16 is initial size) */
71 };
72
73 struct inf_file
74 {
75     struct inf_file *next;            /* next appended file */
76     WCHAR           *strings;         /* buffer for string data (section names and field values) */
77     WCHAR           *string_pos;      /* position of next available string in buffer */
78     unsigned int     nb_sections;     /* number of used sections */
79     unsigned int     alloc_sections;  /* total number of allocated section pointers */
80     struct section **sections;        /* section pointers array */
81     unsigned int     nb_fields;
82     unsigned int     alloc_fields;
83     struct field    *fields;
84     int              strings_section; /* index of [Strings] section or -1 if none */
85     WCHAR           *src_root;        /* source root directory */
86 };
87
88 /* parser definitions */
89
90 enum parser_state
91 {
92     LINE_START,      /* at beginning of a line */
93     SECTION_NAME,    /* parsing a section name */
94     KEY_NAME,        /* parsing a key name */
95     VALUE_NAME,      /* parsing a value name */
96     EOL_BACKSLASH,   /* backslash at end of line */
97     QUOTES,          /* inside quotes */
98     LEADING_SPACES,  /* leading spaces */
99     TRAILING_SPACES, /* trailing spaces */
100     COMMENT,         /* inside a comment */
101     NB_PARSER_STATES
102 };
103
104 struct parser
105 {
106     const WCHAR      *start;        /* start position of item being parsed */
107     const WCHAR      *end;          /* end of buffer */
108     struct inf_file  *file;         /* file being built */
109     enum parser_state state;        /* current parser state */
110     enum parser_state stack[4];     /* state stack */
111     int               stack_pos;    /* current pos in stack */
112
113     int               cur_section;  /* index of section being parsed*/
114     struct line      *line;         /* current line */
115     unsigned int      line_pos;     /* current line position in file */
116     unsigned int      error;        /* error code */
117     unsigned int      token_len;    /* current token len */
118     WCHAR token[MAX_FIELD_LEN+1];   /* current token */
119 };
120
121 typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos );
122
123 /* parser state machine functions */
124 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos );
125 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos );
126 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos );
127 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos );
128 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos );
129 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos );
130 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos );
131 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos );
132 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos );
133
134 static const parser_state_func parser_funcs[NB_PARSER_STATES] =
135 {
136     line_start_state,      /* LINE_START */
137     section_name_state,    /* SECTION_NAME */
138     key_name_state,        /* KEY_NAME */
139     value_name_state,      /* VALUE_NAME */
140     eol_backslash_state,   /* EOL_BACKSLASH */
141     quotes_state,          /* QUOTES */
142     leading_spaces_state,  /* LEADING_SPACES */
143     trailing_spaces_state, /* TRAILING_SPACES */
144     comment_state          /* COMMENT */
145 };
146
147
148 /* Unicode string constants */
149 static const WCHAR Version[]    = {'V','e','r','s','i','o','n',0};
150 static const WCHAR Signature[]  = {'S','i','g','n','a','t','u','r','e',0};
151 static const WCHAR Chicago[]    = {'$','C','h','i','c','a','g','o','$',0};
152 static const WCHAR WindowsNT[]  = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
153 static const WCHAR Windows95[]  = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
154 static const WCHAR LayoutFile[] = {'L','a','y','o','u','t','F','i','l','e',0};
155
156 /* extend an array, allocating more memory if necessary */
157 static void *grow_array( void *array, unsigned int *count, size_t elem )
158 {
159     void *new_array;
160     unsigned int new_count = *count + *count / 2;
161     if (new_count < 32) new_count = 32;
162
163     if (array)
164         new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem );
165     else
166         new_array = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * elem );
167
168     if (new_array)
169         *count = new_count;
170     else
171         if (array)
172             HeapFree( GetProcessHeap(), 0, array );
173     return new_array;
174 }
175
176
177 /* find a section by name */
178 static int find_section( struct inf_file *file, const WCHAR *name )
179 {
180     int i;
181
182     for (i = 0; i < file->nb_sections; i++)
183         if (!strcmpiW( name, file->sections[i]->name )) return i;
184     return -1;
185 }
186
187
188 /* find a line by name */
189 static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name )
190 {
191     struct section *section;
192     struct line *line;
193     int i;
194
195     if (section_index < 0 || section_index >= file->nb_sections) return NULL;
196     section = file->sections[section_index];
197     for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
198     {
199         if (line->key_field == -1) continue;
200         if (!strcmpiW( name, file->fields[line->key_field].text )) return line;
201     }
202     return NULL;
203 }
204
205
206 /* add a section to the file and return the section index */
207 static int add_section( struct inf_file *file, const WCHAR *name )
208 {
209     struct section *section;
210
211     if (file->nb_sections >= file->alloc_sections)
212     {
213         if (!(file->sections = grow_array( file->sections, &file->alloc_sections,
214                                            sizeof(file->sections[0]) ))) return -1;
215     }
216     if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1;
217     section->name        = name;
218     section->nb_lines    = 0;
219     section->alloc_lines = sizeof(section->lines)/sizeof(section->lines[0]);
220     file->sections[file->nb_sections] = section;
221     return file->nb_sections++;
222 }
223
224
225 /* add a line to a given section */
226 static struct line *add_line( struct inf_file *file, int section_index )
227 {
228     struct section *section;
229     struct line *line;
230
231     assert( section_index >= 0 && section_index < file->nb_sections );
232
233     section = file->sections[section_index];
234     if (section->nb_lines == section->alloc_lines)  /* need to grow the section */
235     {
236         int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line);
237         if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL;
238         section->alloc_lines *= 2;
239         file->sections[section_index] = section;
240     }
241     line = &section->lines[section->nb_lines++];
242     line->first_field = file->nb_fields;
243     line->nb_fields   = 0;
244     line->key_field   = -1;
245     return line;
246 }
247
248
249 /* retrieve a given line from section/line index */
250 inline static struct line *get_line( struct inf_file *file, unsigned int section_index,
251                                      unsigned int line_index )
252 {
253     struct section *section;
254
255     if (section_index >= file->nb_sections) return NULL;
256     section = file->sections[section_index];
257     if (line_index >= section->nb_lines) return NULL;
258     return &section->lines[line_index];
259 }
260
261
262 /* retrieve a given field from section/line/field index */
263 static struct field *get_field( struct inf_file *file, int section_index, int line_index,
264                                 int field_index )
265 {
266     struct line *line = get_line( file, section_index, line_index );
267
268     if (!line) return NULL;
269     if (!field_index)  /* get the key */
270     {
271         if (line->key_field == -1) return NULL;
272         return &file->fields[line->key_field];
273     }
274     field_index--;
275     if (field_index >= line->nb_fields) return NULL;
276     return &file->fields[line->first_field + field_index];
277 }
278
279
280 /* allocate a new field, growing the array if necessary */
281 static struct field *add_field( struct inf_file *file, const WCHAR *text )
282 {
283     struct field *field;
284
285     if (file->nb_fields >= file->alloc_fields)
286     {
287         if (!(file->fields = grow_array( file->fields, &file->alloc_fields,
288                                          sizeof(file->fields[0]) ))) return NULL;
289     }
290     field = &file->fields[file->nb_fields++];
291     field->text = text;
292     return field;
293 }
294
295
296 /* retrieve the string substitution for a directory id */
297 static const WCHAR *get_dirid_subst( int dirid, unsigned int *len )
298 {
299     extern const WCHAR *DIRID_get_string( HINF hinf, int dirid );
300     const WCHAR *ret = DIRID_get_string( 0, dirid );
301     if (ret) *len = strlenW(ret);
302     return ret;
303 }
304
305
306 /* retrieve the string substitution for a given string, or NULL if not found */
307 /* if found, len is set to the substitution length */
308 static const WCHAR *get_string_subst( struct inf_file *file, const WCHAR *str, unsigned int *len )
309 {
310     static const WCHAR percent = '%';
311
312     struct section *strings_section;
313     struct line *line;
314     struct field *field;
315     int i, dirid;
316     WCHAR *dirid_str, *end;
317     const WCHAR *ret = NULL;
318
319     if (!*len)  /* empty string (%%) is replaced by single percent */
320     {
321         *len = 1;
322         return &percent;
323     }
324     if (file->strings_section == -1) goto not_found;
325     strings_section = file->sections[file->strings_section];
326     for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++)
327     {
328         if (line->key_field == -1) continue;
329         if (strncmpiW( str, file->fields[line->key_field].text, *len )) continue;
330         if (!file->fields[line->key_field].text[*len]) break;
331     }
332     if (i == strings_section->nb_lines || !line->nb_fields) goto not_found;
333     field = &file->fields[line->first_field];
334     *len = strlenW( field->text );
335     return field->text;
336
337  not_found:  /* check for integer id */
338     if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) )))
339     {
340         memcpy( dirid_str, str, *len * sizeof(WCHAR) );
341         dirid_str[*len] = 0;
342         dirid = strtolW( dirid_str, &end, 10 );
343         if (!*end) ret = get_dirid_subst( dirid, len );
344         HeapFree( GetProcessHeap(), 0, dirid_str );
345         return ret;
346     }
347     return NULL;
348 }
349
350
351 /* do string substitutions on the specified text */
352 /* the buffer is assumed to be large enough */
353 /* returns necessary length not including terminating null */
354 unsigned int PARSER_string_substW( struct inf_file *file, const WCHAR *text, WCHAR *buffer,
355                                    unsigned int size )
356 {
357     const WCHAR *start, *subst, *p;
358     unsigned int len, total = 0;
359     int inside = 0;
360
361     if (!buffer) size = MAX_STRING_LEN + 1;
362     for (p = start = text; *p; p++)
363     {
364         if (*p != '%') continue;
365         inside = !inside;
366         if (inside)  /* start of a %xx% string */
367         {
368             len = p - start;
369             if (len > size - 1) len = size - 1;
370             if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
371             total += len;
372             size -= len;
373             start = p;
374         }
375         else /* end of the %xx% string, find substitution */
376         {
377             len = p - start - 1;
378             subst = get_string_subst( file, start + 1, &len );
379             if (!subst)
380             {
381                 subst = start;
382                 len = p - start + 1;
383             }
384             if (len > size - 1) len = size - 1;
385             if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) );
386             total += len;
387             size -= len;
388             start = p + 1;
389         }
390     }
391
392     if (start != p) /* unfinished string, copy it */
393     {
394         len = p - start;
395         if (len > size - 1) len = size - 1;
396         if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) );
397         total += len;
398     }
399     if (buffer && size) buffer[total] = 0;
400     return total;
401 }
402
403
404 /* do string substitutions on the specified text */
405 /* the buffer is assumed to be large enough */
406 /* returns necessary length not including terminating null */
407 unsigned int PARSER_string_substA( struct inf_file *file, const WCHAR *text, char *buffer,
408                                    unsigned int size )
409 {
410     WCHAR buffW[MAX_STRING_LEN+1];
411     DWORD ret;
412
413     unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) );
414     if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) );
415     else
416     {
417         RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) );
418         buffer[ret] = 0;
419     }
420     return ret;
421 }
422
423
424 /* push some string data into the strings buffer */
425 static WCHAR *push_string( struct inf_file *file, const WCHAR *string )
426 {
427     WCHAR *ret = file->string_pos;
428     strcpyW( ret, string );
429     file->string_pos += strlenW( ret ) + 1;
430     return ret;
431 }
432
433
434 /* push the current state on the parser stack */
435 inline static void push_state( struct parser *parser, enum parser_state state )
436 {
437     assert( parser->stack_pos < sizeof(parser->stack)/sizeof(parser->stack[0]) );
438     parser->stack[parser->stack_pos++] = state;
439 }
440
441
442 /* pop the current state */
443 inline static void pop_state( struct parser *parser )
444 {
445     assert( parser->stack_pos );
446     parser->state = parser->stack[--parser->stack_pos];
447 }
448
449
450 /* set the parser state and return the previous one */
451 inline static enum parser_state set_state( struct parser *parser, enum parser_state state )
452 {
453     enum parser_state ret = parser->state;
454     parser->state = state;
455     return ret;
456 }
457
458
459 /* check if the pointer points to an end of file */
460 inline static int is_eof( struct parser *parser, const WCHAR *ptr )
461 {
462     return (ptr >= parser->end || *ptr == CONTROL_Z);
463 }
464
465
466 /* check if the pointer points to an end of line */
467 inline static int is_eol( struct parser *parser, const WCHAR *ptr )
468 {
469     return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n');
470 }
471
472
473 /* push data from current token start up to pos into the current token */
474 static int push_token( struct parser *parser, const WCHAR *pos )
475 {
476     int len = pos - parser->start;
477     const WCHAR *src = parser->start;
478     WCHAR *dst = parser->token + parser->token_len;
479
480     if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len;
481
482     parser->token_len += len;
483     for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' ';
484     *dst = 0;
485     parser->start = pos;
486     return 0;
487 }
488
489
490 /* add a section with the current token as name */
491 static int add_section_from_token( struct parser *parser )
492 {
493     int section_index;
494
495     if (parser->token_len > MAX_SECTION_NAME_LEN)
496     {
497         parser->error = ERROR_SECTION_NAME_TOO_LONG;
498         return -1;
499     }
500     if ((section_index = find_section( parser->file, parser->token )) == -1)
501     {
502         /* need to create a new one */
503         const WCHAR *name = push_string( parser->file, parser->token );
504         if ((section_index = add_section( parser->file, name )) == -1)
505         {
506             parser->error = ERROR_NOT_ENOUGH_MEMORY;
507             return -1;
508         }
509     }
510     parser->token_len = 0;
511     parser->cur_section = section_index;
512     return section_index;
513 }
514
515
516 /* add a field containing the current token to the current line */
517 static struct field *add_field_from_token( struct parser *parser, int is_key )
518 {
519     struct field *field;
520     WCHAR *text;
521
522     if (!parser->line)  /* need to start a new line */
523     {
524         if (parser->cur_section == -1)  /* got a line before the first section */
525         {
526             parser->error = ERROR_WRONG_INF_STYLE;
527             return NULL;
528         }
529         if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error;
530     }
531     else assert(!is_key);
532
533     text = push_string( parser->file, parser->token );
534     if ((field = add_field( parser->file, text )))
535     {
536         if (!is_key) parser->line->nb_fields++;
537         else
538         {
539             /* replace first field by key field */
540             parser->line->key_field = parser->line->first_field;
541             parser->line->first_field++;
542         }
543         parser->token_len = 0;
544         return field;
545     }
546  error:
547     parser->error = ERROR_NOT_ENOUGH_MEMORY;
548     return NULL;
549 }
550
551
552 /* close the current line and prepare for parsing a new one */
553 static void close_current_line( struct parser *parser )
554 {
555     struct line *cur_line = parser->line;
556
557     if (cur_line)
558     {
559         /* if line has a single field and no key, the field is the key too */
560         if (cur_line->nb_fields == 1 && cur_line->key_field == -1)
561             cur_line->key_field = cur_line->first_field;
562     }
563     parser->line = NULL;
564 }
565
566
567 /* handler for parser LINE_START state */
568 static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos )
569 {
570     const WCHAR *p;
571
572     for (p = pos; !is_eof( parser, p ); p++)
573     {
574         switch(*p)
575         {
576         case '\n':
577             parser->line_pos++;
578             close_current_line( parser );
579             break;
580         case ';':
581             push_state( parser, LINE_START );
582             set_state( parser, COMMENT );
583             return p + 1;
584         case '[':
585             parser->start = p + 1;
586             set_state( parser, SECTION_NAME );
587             return p + 1;
588         default:
589             if (!isspaceW(*p))
590             {
591                 parser->start = p;
592                 set_state( parser, KEY_NAME );
593                 return p;
594             }
595             break;
596         }
597     }
598     close_current_line( parser );
599     return NULL;
600 }
601
602
603 /* handler for parser SECTION_NAME state */
604 static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos )
605 {
606     const WCHAR *p;
607
608     for (p = pos; !is_eol( parser, p ); p++)
609     {
610         if (*p == ']')
611         {
612             push_token( parser, p );
613             if (add_section_from_token( parser ) == -1) return NULL;
614             push_state( parser, LINE_START );
615             set_state( parser, COMMENT );  /* ignore everything else on the line */
616             return p + 1;
617         }
618     }
619     parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */
620     return NULL;
621 }
622
623
624 /* handler for parser KEY_NAME state */
625 static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos )
626 {
627     const WCHAR *p, *token_end = parser->start;
628
629     for (p = pos; !is_eol( parser, p ); p++)
630     {
631         if (*p == ',') break;
632         switch(*p)
633         {
634
635          case '=':
636             push_token( parser, token_end );
637             if (!add_field_from_token( parser, 1 )) return NULL;
638             parser->start = p + 1;
639             push_state( parser, VALUE_NAME );
640             set_state( parser, LEADING_SPACES );
641             return p + 1;
642         case ';':
643             push_token( parser, token_end );
644             if (!add_field_from_token( parser, 0 )) return NULL;
645             push_state( parser, LINE_START );
646             set_state( parser, COMMENT );
647             return p + 1;
648         case '"':
649             push_token( parser, token_end );
650             parser->start = p + 1;
651             push_state( parser, KEY_NAME );
652             set_state( parser, QUOTES );
653             return p + 1;
654         case '\\':
655             push_token( parser, token_end );
656             parser->start = p;
657             push_state( parser, KEY_NAME );
658             set_state( parser, EOL_BACKSLASH );
659             return p;
660         default:
661             if (!isspaceW(*p)) token_end = p + 1;
662             else
663             {
664                 push_token( parser, p );
665                 push_state( parser, KEY_NAME );
666                 set_state( parser, TRAILING_SPACES );
667                 return p;
668             }
669             break;
670         }
671     }
672     push_token( parser, token_end );
673     set_state( parser, VALUE_NAME );
674     return p;
675 }
676
677
678 /* handler for parser VALUE_NAME state */
679 static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos )
680 {
681     const WCHAR *p, *token_end = parser->start;
682
683     for (p = pos; !is_eol( parser, p ); p++)
684     {
685         switch(*p)
686         {
687         case ';':
688             push_token( parser, token_end );
689             if (!add_field_from_token( parser, 0 )) return NULL;
690             push_state( parser, LINE_START );
691             set_state( parser, COMMENT );
692             return p + 1;
693         case ',':
694             push_token( parser, token_end );
695             if (!add_field_from_token( parser, 0 )) return NULL;
696             parser->start = p + 1;
697             push_state( parser, VALUE_NAME );
698             set_state( parser, LEADING_SPACES );
699             return p + 1;
700         case '"':
701             push_token( parser, token_end );
702             parser->start = p + 1;
703             push_state( parser, VALUE_NAME );
704             set_state( parser, QUOTES );
705             return p + 1;
706         case '\\':
707             push_token( parser, token_end );
708             parser->start = p;
709             push_state( parser, VALUE_NAME );
710             set_state( parser, EOL_BACKSLASH );
711             return p;
712         default:
713             if (!isspaceW(*p)) token_end = p + 1;
714             else
715             {
716                 push_token( parser, p );
717                 push_state( parser, VALUE_NAME );
718                 set_state( parser, TRAILING_SPACES );
719                 return p;
720             }
721             break;
722         }
723     }
724     push_token( parser, token_end );
725     if (!add_field_from_token( parser, 0 )) return NULL;
726     set_state( parser, LINE_START );
727     return p;
728 }
729
730
731 /* handler for parser EOL_BACKSLASH state */
732 static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos )
733 {
734     const WCHAR *p;
735
736     for (p = pos; !is_eof( parser, p ); p++)
737     {
738         switch(*p)
739         {
740         case '\n':
741             parser->line_pos++;
742             parser->start = p + 1;
743             set_state( parser, LEADING_SPACES );
744             return p + 1;
745         case '\\':
746             continue;
747         case ';':
748             push_state( parser, EOL_BACKSLASH );
749             set_state( parser, COMMENT );
750             return p + 1;
751         default:
752             if (isspaceW(*p)) continue;
753             push_token( parser, p );
754             pop_state( parser );
755             return p;
756         }
757     }
758     parser->start = p;
759     pop_state( parser );
760     return p;
761 }
762
763
764 /* handler for parser QUOTES state */
765 static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos )
766 {
767     const WCHAR *p, *token_end = parser->start;
768
769     for (p = pos; !is_eol( parser, p ); p++)
770     {
771         if (*p == '"')
772         {
773             if (p+1 < parser->end && p[1] == '"')  /* double quotes */
774             {
775                 push_token( parser, p + 1 );
776                 parser->start = token_end = p + 2;
777                 p++;
778             }
779             else  /* end of quotes */
780             {
781                 push_token( parser, p );
782                 parser->start = p + 1;
783                 pop_state( parser );
784                 return p + 1;
785             }
786         }
787     }
788     push_token( parser, p );
789     pop_state( parser );
790     return p;
791 }
792
793
794 /* handler for parser LEADING_SPACES state */
795 static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos )
796 {
797     const WCHAR *p;
798
799     for (p = pos; !is_eol( parser, p ); p++)
800     {
801         if (*p == '\\')
802         {
803             parser->start = p;
804             set_state( parser, EOL_BACKSLASH );
805             return p;
806         }
807         if (!isspaceW(*p)) break;
808     }
809     parser->start = p;
810     pop_state( parser );
811     return p;
812 }
813
814
815 /* handler for parser TRAILING_SPACES state */
816 static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos )
817 {
818     const WCHAR *p;
819
820     for (p = pos; !is_eol( parser, p ); p++)
821     {
822         if (*p == '\\')
823         {
824             set_state( parser, EOL_BACKSLASH );
825             return p;
826         }
827         if (!isspaceW(*p)) break;
828     }
829     pop_state( parser );
830     return p;
831 }
832
833
834 /* handler for parser COMMENT state */
835 static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos )
836 {
837     const WCHAR *p = pos;
838
839     while (!is_eol( parser, p )) p++;
840     pop_state( parser );
841     return p;
842 }
843
844
845 /* parse a complete buffer */
846 static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end,
847                            UINT *error_line )
848 {
849     static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
850
851     struct parser parser;
852     const WCHAR *pos = buffer;
853
854     parser.start       = buffer;
855     parser.end         = end;
856     parser.file        = file;
857     parser.line        = NULL;
858     parser.state       = LINE_START;
859     parser.stack_pos   = 0;
860     parser.cur_section = -1;
861     parser.line_pos    = 1;
862     parser.error       = 0;
863     parser.token_len   = 0;
864
865     /* parser main loop */
866     while (pos) pos = (parser_funcs[parser.state])( &parser, pos );
867
868     /* trim excess buffer space */
869     if (file->alloc_sections > file->nb_sections)
870     {
871         file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections,
872                                       file->nb_sections * sizeof(file->sections[0]) );
873         file->alloc_sections = file->nb_sections;
874     }
875     if (file->alloc_fields > file->nb_fields)
876     {
877         file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields,
878                                     file->nb_fields * sizeof(file->fields[0]) );
879         file->alloc_fields = file->nb_fields;
880     }
881     file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings,
882                                  (file->string_pos - file->strings) * sizeof(WCHAR) );
883
884     if (parser.error)
885     {
886         if (error_line) *error_line = parser.line_pos;
887         return parser.error;
888     }
889
890     /* find the [strings] section */
891     file->strings_section = find_section( file, Strings );
892     return 0;
893 }
894
895
896 /* append a child INF file to its parent list, in a thread-safe manner */
897 static void append_inf_file( struct inf_file *parent, struct inf_file *child )
898 {
899     struct inf_file **ppnext = &parent->next;
900     child->next = NULL;
901
902     for (;;)
903     {
904         struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL );
905         if (!next) return;
906         ppnext = &next->next;
907     }
908 }
909
910
911 /***********************************************************************
912  *            parse_file
913  *
914  * parse an INF file.
915  */
916 static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, UINT *error_line )
917 {
918     void *buffer;
919     DWORD err = 0;
920     struct inf_file *file;
921
922     DWORD size = GetFileSize( handle, NULL );
923     HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL );
924     if (!mapping) return NULL;
925     buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size );
926     NtClose( mapping );
927     if (!buffer) return NULL;
928
929     if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
930
931     if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) )))
932     {
933         err = ERROR_NOT_ENOUGH_MEMORY;
934         goto done;
935     }
936
937     /* we won't need more strings space than the size of the file,
938      * so we can preallocate it here
939      */
940     if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
941     {
942         err = ERROR_NOT_ENOUGH_MEMORY;
943         goto done;
944     }
945     file->string_pos = file->strings;
946     file->strings_section = -1;
947
948     if (!RtlIsTextUnicode( buffer, size, NULL ))
949     {
950         WCHAR *new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
951         if (new_buff)
952         {
953             DWORD len = MultiByteToWideChar( CP_ACP, 0, buffer, size, new_buff,
954                                              size * sizeof(WCHAR) );
955             err = parse_buffer( file, new_buff, new_buff + len, error_line );
956             HeapFree( GetProcessHeap(), 0, new_buff );
957         }
958     }
959     else err = parse_buffer( file, buffer, (WCHAR *)((char *)buffer + size), error_line );
960
961     if (!err)  /* now check signature */
962     {
963         int version_index = find_section( file, Version );
964         if (version_index != -1)
965         {
966             struct line *line = find_line( file, version_index, Signature );
967             if (line && line->nb_fields > 0)
968             {
969                 struct field *field = file->fields + line->first_field;
970                 if (!strcmpiW( field->text, Chicago )) goto done;
971                 if (!strcmpiW( field->text, WindowsNT )) goto done;
972                 if (!strcmpiW( field->text, Windows95 )) goto done;
973             }
974         }
975         err = ERROR_WRONG_INF_STYLE;
976     }
977
978  done:
979     UnmapViewOfFile( buffer );
980     if (err)
981     {
982         HeapFree( GetProcessHeap(), 0, file );
983         SetLastError( err );
984         file = NULL;
985     }
986     return file;
987 }
988
989
990 /***********************************************************************
991  *            PARSER_get_src_root
992  *
993  * Retrieve the source directory of an inf file.
994  */
995 const WCHAR *PARSER_get_src_root( HINF hinf )
996 {
997     struct inf_file *file = hinf;
998     return file->src_root;
999 }
1000
1001
1002 /***********************************************************************
1003  *            SetupOpenInfFileA   (SETUPAPI.@)
1004  */
1005 HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error )
1006 {
1007     UNICODE_STRING nameW, classW;
1008     HINF ret = (HINF)INVALID_HANDLE_VALUE;
1009
1010     classW.Buffer = NULL;
1011     if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class ))
1012     {
1013         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1014         return ret;
1015     }
1016     if (RtlCreateUnicodeStringFromAsciiz( &nameW, name ))
1017     {
1018         ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error );
1019         RtlFreeUnicodeString( &nameW );
1020     }
1021     RtlFreeUnicodeString( &classW );
1022     return ret;
1023 }
1024
1025
1026 /***********************************************************************
1027  *            SetupOpenInfFileW   (SETUPAPI.@)
1028  */
1029 HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error )
1030 {
1031     struct inf_file *file = NULL;
1032     HANDLE handle;
1033     WCHAR *path, *p;
1034     UINT len;
1035
1036     if (strchrW( name, '\\' ) || strchrW( name, '/' ))
1037     {
1038         if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return (HINF)INVALID_HANDLE_VALUE;
1039         if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1040         {
1041             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1042             return (HINF)INVALID_HANDLE_VALUE;
1043         }
1044         GetFullPathNameW( name, len, path, NULL );
1045         handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1046     }
1047     else  /* try Windows directory */
1048     {
1049         static const WCHAR Inf[]      = {'\\','i','n','f','\\',0};
1050         static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1051
1052         len = GetWindowsDirectoryW( NULL, 0 ) + strlenW(name) + 12;
1053         if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1054         {
1055             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1056             return (HINF)INVALID_HANDLE_VALUE;
1057         }
1058         GetWindowsDirectoryW( path, len );
1059         p = path + strlenW(path);
1060         strcpyW( p, Inf );
1061         strcatW( p, name );
1062         handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1063         if (handle == INVALID_HANDLE_VALUE)
1064         {
1065             strcpyW( p, System32 );
1066             strcatW( p, name );
1067             handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1068         }
1069     }
1070
1071     if (handle != INVALID_HANDLE_VALUE)
1072     {
1073         file = parse_file( handle, class, error );
1074         CloseHandle( handle );
1075     }
1076     if (!file)
1077     {
1078         HeapFree( GetProcessHeap(), 0, path );
1079         return (HINF)INVALID_HANDLE_VALUE;
1080     }
1081     TRACE( "%s -> %p\n", debugstr_w(path), file );
1082     file->src_root = path;
1083     if ((p = strrchrW( path, '\\' ))) p[1] = 0;  /* remove file name */
1084     SetLastError( 0 );
1085     return (HINF)file;
1086 }
1087
1088
1089 /***********************************************************************
1090  *            SetupOpenAppendInfFileA    (SETUPAPI.@)
1091  */
1092 BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error )
1093 {
1094     HINF child_hinf;
1095
1096     if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error );
1097     child_hinf = SetupOpenInfFileA( name, NULL, INF_STYLE_WIN4, error );
1098     if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1099     append_inf_file( parent_hinf, child_hinf );
1100     TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_a(name), child_hinf );
1101     return TRUE;
1102 }
1103
1104
1105 /***********************************************************************
1106  *            SetupOpenAppendInfFileW    (SETUPAPI.@)
1107  */
1108 BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error )
1109 {
1110     HINF child_hinf;
1111
1112     if (!name)
1113     {
1114         INFCONTEXT context;
1115         WCHAR filename[MAX_PATH];
1116         int idx = 1;
1117
1118         if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE;
1119         while (SetupGetStringFieldW( &context, idx++, filename,
1120                                      sizeof(filename)/sizeof(WCHAR), NULL ))
1121         {
1122             child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error );
1123             if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1124             append_inf_file( parent_hinf, child_hinf );
1125             TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf );
1126         }
1127         return TRUE;
1128     }
1129     child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error );
1130     if (child_hinf == (HINF)INVALID_HANDLE_VALUE) return FALSE;
1131     append_inf_file( parent_hinf, child_hinf );
1132     TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf );
1133     return TRUE;
1134 }
1135
1136
1137 /***********************************************************************
1138  *            SetupCloseInfFile   (SETUPAPI.@)
1139  */
1140 void WINAPI SetupCloseInfFile( HINF hinf )
1141 {
1142     struct inf_file *file = hinf;
1143     int i;
1144
1145     for (i = 0; i < file->nb_sections; i++) HeapFree( GetProcessHeap(), 0, file->sections[i] );
1146     HeapFree( GetProcessHeap(), 0, file->src_root );
1147     HeapFree( GetProcessHeap(), 0, file->sections );
1148     HeapFree( GetProcessHeap(), 0, file->fields );
1149     HeapFree( GetProcessHeap(), 0, file->strings );
1150     HeapFree( GetProcessHeap(), 0, file );
1151 }
1152
1153
1154 /***********************************************************************
1155  *            SetupGetLineCountA   (SETUPAPI.@)
1156  */
1157 LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name )
1158 {
1159     UNICODE_STRING sectionW;
1160     LONG ret = -1;
1161
1162     if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, name ))
1163         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1164     else
1165     {
1166         ret = SetupGetLineCountW( hinf, sectionW.Buffer );
1167         RtlFreeUnicodeString( &sectionW );
1168     }
1169     return ret;
1170 }
1171
1172
1173 /***********************************************************************
1174  *            SetupGetLineCountW   (SETUPAPI.@)
1175  */
1176 LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section )
1177 {
1178     struct inf_file *file = hinf;
1179     int section_index;
1180     LONG ret = -1;
1181
1182     for (file = hinf; file; file = file->next)
1183     {
1184         if ((section_index = find_section( file, section )) == -1) continue;
1185         if (ret == -1) ret = 0;
1186         ret += file->sections[section_index]->nb_lines;
1187     }
1188     TRACE( "(%p,%s) returning %ld\n", hinf, debugstr_w(section), ret );
1189     SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 );
1190     return ret;
1191 }
1192
1193
1194 /***********************************************************************
1195  *            SetupGetLineByIndexA   (SETUPAPI.@)
1196  */
1197 BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context )
1198 {
1199     UNICODE_STRING sectionW;
1200     BOOL ret = FALSE;
1201
1202     if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1203         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1204     else
1205     {
1206         ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context );
1207         RtlFreeUnicodeString( &sectionW );
1208     }
1209     return ret;
1210 }
1211
1212
1213 /***********************************************************************
1214  *            SetupGetLineByIndexW   (SETUPAPI.@)
1215  */
1216 BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context )
1217 {
1218     struct inf_file *file = hinf;
1219     int section_index;
1220
1221     SetLastError( ERROR_SECTION_NOT_FOUND );
1222     for (file = hinf; file; file = file->next)
1223     {
1224         if ((section_index = find_section( file, section )) == -1) continue;
1225         SetLastError( ERROR_LINE_NOT_FOUND );
1226         if (index < file->sections[section_index]->nb_lines)
1227         {
1228             context->Inf        = hinf;
1229             context->CurrentInf = file;
1230             context->Section    = section_index;
1231             context->Line       = index;
1232             SetLastError( 0 );
1233             TRACE( "(%p,%s): returning %d/%ld\n",
1234                    hinf, debugstr_w(section), section_index, index );
1235             return TRUE;
1236         }
1237         index -= file->sections[section_index]->nb_lines;
1238     }
1239     TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) );
1240     return FALSE;
1241 }
1242
1243
1244 /***********************************************************************
1245  *            SetupFindFirstLineA   (SETUPAPI.@)
1246  */
1247 BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context )
1248 {
1249     UNICODE_STRING sectionW, keyW;
1250     BOOL ret = FALSE;
1251
1252     if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1253     {
1254         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1255         return FALSE;
1256     }
1257
1258     if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context );
1259     else
1260     {
1261         if (RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1262         {
1263             ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context );
1264             RtlFreeUnicodeString( &keyW );
1265         }
1266         else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1267     }
1268     RtlFreeUnicodeString( &sectionW );
1269     return ret;
1270 }
1271
1272
1273 /***********************************************************************
1274  *            SetupFindFirstLineW   (SETUPAPI.@)
1275  */
1276 BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context )
1277 {
1278     struct inf_file *file;
1279     int section_index;
1280
1281     SetLastError( ERROR_SECTION_NOT_FOUND );
1282     for (file = hinf; file; file = file->next)
1283     {
1284         if ((section_index = find_section( file, section )) == -1) continue;
1285         if (key)
1286         {
1287             INFCONTEXT ctx;
1288             ctx.Inf        = hinf;
1289             ctx.CurrentInf = file;
1290             ctx.Section    = section_index;
1291             ctx.Line       = -1;
1292             return SetupFindNextMatchLineW( &ctx, key, context );
1293         }
1294         SetLastError( ERROR_LINE_NOT_FOUND );  /* found at least one section */
1295         if (file->sections[section_index]->nb_lines)
1296         {
1297             context->Inf        = hinf;
1298             context->CurrentInf = file;
1299             context->Section    = section_index;
1300             context->Line       = 0;
1301             SetLastError( 0 );
1302             TRACE( "(%p,%s,%s): returning %d/0\n",
1303                    hinf, debugstr_w(section), debugstr_w(key), section_index );
1304             return TRUE;
1305         }
1306     }
1307     TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) );
1308     return FALSE;
1309 }
1310
1311
1312 /***********************************************************************
1313  *            SetupFindNextLine   (SETUPAPI.@)
1314  */
1315 BOOL WINAPI SetupFindNextLine( const INFCONTEXT *context_in, INFCONTEXT *context_out )
1316 {
1317     struct inf_file *file = context_in->CurrentInf;
1318     struct section *section;
1319
1320     if (context_in->Section >= file->nb_sections) goto error;
1321
1322     section = file->sections[context_in->Section];
1323     if (context_in->Line+1 < section->nb_lines)
1324     {
1325         if (context_out != context_in) *context_out = *context_in;
1326         context_out->Line++;
1327         SetLastError( 0 );
1328         return TRUE;
1329     }
1330
1331     /* now search the appended files */
1332
1333     for (file = file->next; file; file = file->next)
1334     {
1335         int section_index = find_section( file, section->name );
1336         if (section_index == -1) continue;
1337         if (file->sections[section_index]->nb_lines)
1338         {
1339             context_out->Inf        = context_in->Inf;
1340             context_out->CurrentInf = file;
1341             context_out->Section    = section_index;
1342             context_out->Line       = 0;
1343             SetLastError( 0 );
1344             return TRUE;
1345         }
1346     }
1347  error:
1348     SetLastError( ERROR_LINE_NOT_FOUND );
1349     return FALSE;
1350 }
1351
1352
1353 /***********************************************************************
1354  *            SetupFindNextMatchLineA   (SETUPAPI.@)
1355  */
1356 BOOL WINAPI SetupFindNextMatchLineA( const INFCONTEXT *context_in, PCSTR key,
1357                                      INFCONTEXT *context_out )
1358 {
1359     UNICODE_STRING keyW;
1360     BOOL ret = FALSE;
1361
1362     if (!key) return SetupFindNextLine( context_in, context_out );
1363
1364     if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key ))
1365         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1366     else
1367     {
1368         ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out );
1369         RtlFreeUnicodeString( &keyW );
1370     }
1371     return ret;
1372 }
1373
1374
1375 /***********************************************************************
1376  *            SetupFindNextMatchLineW   (SETUPAPI.@)
1377  */
1378 BOOL WINAPI SetupFindNextMatchLineW( const INFCONTEXT *context_in, PCWSTR key,
1379                                      INFCONTEXT *context_out )
1380 {
1381     struct inf_file *file = context_in->CurrentInf;
1382     struct section *section;
1383     struct line *line;
1384     unsigned int i;
1385
1386     if (!key) return SetupFindNextLine( context_in, context_out );
1387
1388     if (context_in->Section >= file->nb_sections) goto error;
1389
1390     section = file->sections[context_in->Section];
1391
1392     for (i = context_in->Line+1, line = &section->lines[i]; i < section->nb_lines; i++, line++)
1393     {
1394         if (line->key_field == -1) continue;
1395         if (!strcmpiW( key, file->fields[line->key_field].text ))
1396         {
1397             if (context_out != context_in) *context_out = *context_in;
1398             context_out->Line = i;
1399             SetLastError( 0 );
1400             TRACE( "(%p,%s,%s): returning %d\n",
1401                    file, debugstr_w(section->name), debugstr_w(key), i );
1402             return TRUE;
1403         }
1404     }
1405
1406     /* now search the appended files */
1407
1408     for (file = file->next; file; file = file->next)
1409     {
1410         int section_index = find_section( file, section->name );
1411         if (section_index == -1) continue;
1412         section = file->sections[section_index];
1413         for (i = 0, line = section->lines; i < section->nb_lines; i++, line++)
1414         {
1415             if (line->key_field == -1) continue;
1416             if (!strcmpiW( key, file->fields[line->key_field].text ))
1417             {
1418                 context_out->Inf        = context_in->Inf;
1419                 context_out->CurrentInf = file;
1420                 context_out->Section    = section_index;
1421                 context_out->Line       = i;
1422                 SetLastError( 0 );
1423                 TRACE( "(%p,%s,%s): returning %d/%d\n",
1424                        file, debugstr_w(section->name), debugstr_w(key), section_index, i );
1425                 return TRUE;
1426             }
1427         }
1428     }
1429     TRACE( "(%p,%s,%s): not found\n",
1430            context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) );
1431  error:
1432     SetLastError( ERROR_LINE_NOT_FOUND );
1433     return FALSE;
1434 }
1435
1436
1437 /***********************************************************************
1438  *              SetupGetLineTextW    (SETUPAPI.@)
1439  */
1440 BOOL WINAPI SetupGetLineTextW( const INFCONTEXT *context, HINF hinf, PCWSTR section_name,
1441                                PCWSTR key_name, PWSTR buffer, DWORD size, DWORD *required )
1442 {
1443     struct inf_file *file;
1444     struct line *line;
1445     struct field *field;
1446     int i;
1447     DWORD total = 0;
1448
1449     if (!context)
1450     {
1451         INFCONTEXT new_context;
1452         if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE;
1453         file = new_context.CurrentInf;
1454         line = get_line( file, new_context.Section, new_context.Line );
1455     }
1456     else
1457     {
1458         file = context->CurrentInf;
1459         if (!(line = get_line( file, context->Section, context->Line )))
1460         {
1461             SetLastError( ERROR_LINE_NOT_FOUND );
1462             return FALSE;
1463         }
1464     }
1465
1466     for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1467         total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1;
1468
1469     if (required) *required = total;
1470     if (buffer)
1471     {
1472         if (total > size)
1473         {
1474             SetLastError( ERROR_INSUFFICIENT_BUFFER );
1475             return FALSE;
1476         }
1477         for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1478         {
1479             unsigned int len = PARSER_string_substW( file, field->text, buffer, size );
1480             if (i+1 < line->nb_fields) buffer[len] = ',';
1481             buffer += len + 1;
1482         }
1483     }
1484     return TRUE;
1485 }
1486
1487
1488 /***********************************************************************
1489  *              SetupGetLineTextA    (SETUPAPI.@)
1490  */
1491 BOOL WINAPI SetupGetLineTextA( const INFCONTEXT *context, HINF hinf, PCSTR section_name,
1492                                PCSTR key_name, PSTR buffer, DWORD size, DWORD *required )
1493 {
1494     struct inf_file *file;
1495     struct line *line;
1496     struct field *field;
1497     int i;
1498     DWORD total = 0;
1499
1500     if (!context)
1501     {
1502         INFCONTEXT new_context;
1503         if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE;
1504         file = new_context.CurrentInf;
1505         line = get_line( file, new_context.Section, new_context.Line );
1506     }
1507     else
1508     {
1509         file = context->CurrentInf;
1510         if (!(line = get_line( file, context->Section, context->Line )))
1511         {
1512             SetLastError( ERROR_LINE_NOT_FOUND );
1513             return FALSE;
1514         }
1515     }
1516
1517     for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1518         total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1;
1519
1520     if (required) *required = total;
1521     if (buffer)
1522     {
1523         if (total > size)
1524         {
1525             SetLastError( ERROR_INSUFFICIENT_BUFFER );
1526             return FALSE;
1527         }
1528         for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++)
1529         {
1530             unsigned int len = PARSER_string_substA( file, field->text, buffer, size );
1531             if (i+1 < line->nb_fields) buffer[len] = ',';
1532             buffer += len + 1;
1533         }
1534     }
1535     return TRUE;
1536 }
1537
1538
1539 /***********************************************************************
1540  *              SetupGetFieldCount    (SETUPAPI.@)
1541  */
1542 DWORD WINAPI SetupGetFieldCount( const INFCONTEXT *context )
1543 {
1544     struct inf_file *file = context->CurrentInf;
1545     struct line *line = get_line( file, context->Section, context->Line );
1546
1547     if (!line) return 0;
1548     return line->nb_fields;
1549 }
1550
1551
1552 /***********************************************************************
1553  *              SetupGetStringFieldA    (SETUPAPI.@)
1554  */
1555 BOOL WINAPI SetupGetStringFieldA( const INFCONTEXT *context, DWORD index, PSTR buffer,
1556                                   DWORD size, DWORD *required )
1557 {
1558     struct inf_file *file = context->CurrentInf;
1559     struct field *field = get_field( file, context->Section, context->Line, index );
1560     unsigned int len;
1561
1562     SetLastError(0);
1563     if (!field) return FALSE;
1564     len = PARSER_string_substA( file, field->text, NULL, 0 );
1565     if (required) *required = len + 1;
1566     if (buffer)
1567     {
1568         if (size <= len)
1569         {
1570             SetLastError( ERROR_INSUFFICIENT_BUFFER );
1571             return FALSE;
1572         }
1573         PARSER_string_substA( file, field->text, buffer, size );
1574
1575         TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1576                context->Inf, context->CurrentInf, context->Section, context->Line,
1577                index, debugstr_a(buffer) );
1578     }
1579     return TRUE;
1580 }
1581
1582
1583 /***********************************************************************
1584  *              SetupGetStringFieldW    (SETUPAPI.@)
1585  */
1586 BOOL WINAPI SetupGetStringFieldW( const INFCONTEXT *context, DWORD index, PWSTR buffer,
1587                                   DWORD size, DWORD *required )
1588 {
1589     struct inf_file *file = context->CurrentInf;
1590     struct field *field = get_field( file, context->Section, context->Line, index );
1591     unsigned int len;
1592
1593     SetLastError(0);
1594     if (!field) return FALSE;
1595     len = PARSER_string_substW( file, field->text, NULL, 0 );
1596     if (required) *required = len + 1;
1597     if (buffer)
1598     {
1599         if (size <= len)
1600         {
1601             SetLastError( ERROR_INSUFFICIENT_BUFFER );
1602             return FALSE;
1603         }
1604         PARSER_string_substW( file, field->text, buffer, size );
1605
1606         TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1607                context->Inf, context->CurrentInf, context->Section, context->Line,
1608                index, debugstr_w(buffer) );
1609     }
1610     return TRUE;
1611 }
1612
1613
1614 /***********************************************************************
1615  *              SetupGetIntField    (SETUPAPI.@)
1616  */
1617 BOOL WINAPI SetupGetIntField( const INFCONTEXT *context, DWORD index, INT *result )
1618 {
1619     char localbuff[20];
1620     char *end, *buffer = localbuff;
1621     DWORD required;
1622     INT res;
1623     BOOL ret = FALSE;
1624
1625     if (!SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))
1626     {
1627         if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
1628         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
1629         if (!SetupGetStringFieldA( context, index, buffer, required, NULL )) goto done;
1630     }
1631     res = strtol( buffer, &end, 0 );
1632     if (end != buffer && !*end)
1633     {
1634         *result = res;
1635         ret = TRUE;
1636     }
1637     else SetLastError( ERROR_INVALID_DATA );
1638
1639  done:
1640     if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );
1641     return ret;
1642 }
1643
1644
1645 /***********************************************************************
1646  *              SetupGetBinaryField    (SETUPAPI.@)
1647  */
1648 BOOL WINAPI SetupGetBinaryField( const INFCONTEXT *context, DWORD index, BYTE *buffer,
1649                                  DWORD size, DWORD *required )
1650 {
1651     struct inf_file *file = context->CurrentInf;
1652     struct line *line = get_line( file, context->Section, context->Line );
1653     struct field *field;
1654     int i;
1655
1656     if (!line)
1657     {
1658         SetLastError( ERROR_LINE_NOT_FOUND );
1659         return FALSE;
1660     }
1661     if (!index || index >= line->nb_fields)
1662     {
1663         SetLastError( ERROR_INVALID_PARAMETER );
1664         return FALSE;
1665     }
1666     index--;  /* fields start at 0 */
1667     if (required) *required = line->nb_fields - index;
1668     if (!buffer) return TRUE;
1669     if (size < line->nb_fields - index)
1670     {
1671         SetLastError( ERROR_INSUFFICIENT_BUFFER );
1672         return FALSE;
1673     }
1674     field = &file->fields[line->first_field + index];
1675     for (i = index; i < line->nb_fields; i++, field++)
1676     {
1677         const WCHAR *p;
1678         DWORD value = 0;
1679         for (p = field->text; *p && isxdigitW(*p); p++)
1680         {
1681             if ((value <<= 4) > 255)
1682             {
1683                 SetLastError( ERROR_INVALID_DATA );
1684                 return FALSE;
1685             }
1686             if (*p <= '9') value |= (*p - '0');
1687             else value |= (tolowerW(*p) - 'a' + 10);
1688         }
1689         buffer[i - index] = value;
1690     }
1691     if (TRACE_ON(setupapi))
1692     {
1693         TRACE( "%p/%p/%d/%d index %ld returning",
1694                context->Inf, context->CurrentInf, context->Section, context->Line, index );
1695         for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] );
1696         TRACE( "\n" );
1697     }
1698     return TRUE;
1699 }
1700
1701
1702 /***********************************************************************
1703  *              SetupGetMultiSzFieldA    (SETUPAPI.@)
1704  */
1705 BOOL WINAPI SetupGetMultiSzFieldA( const INFCONTEXT *context, DWORD index, PSTR buffer,
1706                                    DWORD size, DWORD *required )
1707 {
1708     struct inf_file *file = context->CurrentInf;
1709     struct line *line = get_line( file, context->Section, context->Line );
1710     struct field *field;
1711     unsigned int len;
1712     int i;
1713     DWORD total = 1;
1714
1715     if (!line)
1716     {
1717         SetLastError( ERROR_LINE_NOT_FOUND );
1718         return FALSE;
1719     }
1720     if (!index || index >= line->nb_fields)
1721     {
1722         SetLastError( ERROR_INVALID_PARAMETER );
1723         return FALSE;
1724     }
1725     index--;  /* fields start at 0 */
1726     field = &file->fields[line->first_field + index];
1727     for (i = index; i < line->nb_fields; i++, field++)
1728     {
1729         if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break;
1730         total += len + 1;
1731     }
1732
1733     if (required) *required = total;
1734     if (!buffer) return TRUE;
1735     if (total > size)
1736     {
1737         SetLastError( ERROR_INSUFFICIENT_BUFFER );
1738         return FALSE;
1739     }
1740     field = &file->fields[line->first_field + index];
1741     for (i = index; i < line->nb_fields; i++, field++)
1742     {
1743         if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break;
1744         buffer += len + 1;
1745     }
1746     *buffer = 0;  /* add final null */
1747     return TRUE;
1748 }
1749
1750
1751 /***********************************************************************
1752  *              SetupGetMultiSzFieldW    (SETUPAPI.@)
1753  */
1754 BOOL WINAPI SetupGetMultiSzFieldW( const INFCONTEXT *context, DWORD index, PWSTR buffer,
1755                                    DWORD size, DWORD *required )
1756 {
1757     struct inf_file *file = context->CurrentInf;
1758     struct line *line = get_line( file, context->Section, context->Line );
1759     struct field *field;
1760     unsigned int len;
1761     int i;
1762     DWORD total = 1;
1763
1764     if (!line)
1765     {
1766         SetLastError( ERROR_LINE_NOT_FOUND );
1767         return FALSE;
1768     }
1769     if (!index || index >= line->nb_fields)
1770     {
1771         SetLastError( ERROR_INVALID_PARAMETER );
1772         return FALSE;
1773     }
1774     index--;  /* fields start at 0 */
1775     field = &file->fields[line->first_field + index];
1776     for (i = index; i < line->nb_fields; i++, field++)
1777     {
1778         if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break;
1779         total += len + 1;
1780     }
1781
1782     if (required) *required = total;
1783     if (!buffer) return TRUE;
1784     if (total > size)
1785     {
1786         SetLastError( ERROR_INSUFFICIENT_BUFFER );
1787         return FALSE;
1788     }
1789     field = &file->fields[line->first_field + index];
1790     for (i = index; i < line->nb_fields; i++, field++)
1791     {
1792         if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break;
1793         buffer += len + 1;
1794     }
1795     *buffer = 0;  /* add final null */
1796     return TRUE;
1797 }