4 * Copyright 2002 Alexandre Julliard for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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
*filename
; /* filename of the INF */
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 broken_line
; /* first line containing invalid data (if any) */
118 unsigned int error
; /* error code */
119 unsigned int token_len
; /* current token len */
120 WCHAR token
[MAX_FIELD_LEN
+1]; /* current token */
123 typedef const WCHAR
* (*parser_state_func
)( struct parser
*parser
, const WCHAR
*pos
);
125 /* parser state machine functions */
126 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
);
127 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
);
128 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
);
129 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
);
130 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
);
131 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
);
132 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
133 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
134 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
);
136 static const parser_state_func parser_funcs
[NB_PARSER_STATES
] =
138 line_start_state
, /* LINE_START */
139 section_name_state
, /* SECTION_NAME */
140 key_name_state
, /* KEY_NAME */
141 value_name_state
, /* VALUE_NAME */
142 eol_backslash_state
, /* EOL_BACKSLASH */
143 quotes_state
, /* QUOTES */
144 leading_spaces_state
, /* LEADING_SPACES */
145 trailing_spaces_state
, /* TRAILING_SPACES */
146 comment_state
/* COMMENT */
150 /* Unicode string constants */
151 static const WCHAR Version
[] = {'V','e','r','s','i','o','n',0};
152 static const WCHAR Signature
[] = {'S','i','g','n','a','t','u','r','e',0};
153 static const WCHAR Chicago
[] = {'$','C','h','i','c','a','g','o','$',0};
154 static const WCHAR WindowsNT
[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
155 static const WCHAR Windows95
[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
156 static const WCHAR LayoutFile
[] = {'L','a','y','o','u','t','F','i','l','e',0};
158 /* extend an array, allocating more memory if necessary */
159 static void *grow_array( void *array
, unsigned int *count
, size_t elem
)
162 unsigned int new_count
= *count
+ *count
/ 2;
163 if (new_count
< 32) new_count
= 32;
166 new_array
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, array
, new_count
* elem
);
168 new_array
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* elem
);
173 HeapFree( GetProcessHeap(), 0, array
);
178 /* get the directory of the inf file (as counted string, not null-terminated) */
179 static const WCHAR
*get_inf_dir( const struct inf_file
*file
, unsigned int *len
)
181 const WCHAR
*p
= strrchrW( file
->filename
, '\\' );
182 *len
= p
? (p
+ 1 - file
->filename
) : 0;
183 return file
->filename
;
187 /* find a section by name */
188 static int find_section( const struct inf_file
*file
, const WCHAR
*name
)
192 for (i
= 0; i
< file
->nb_sections
; i
++)
193 if (!strcmpiW( name
, file
->sections
[i
]->name
)) return i
;
198 /* find a line by name */
199 static struct line
*find_line( struct inf_file
*file
, int section_index
, const WCHAR
*name
)
201 struct section
*section
;
205 if (section_index
< 0 || section_index
>= file
->nb_sections
) return NULL
;
206 section
= file
->sections
[section_index
];
207 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
209 if (line
->key_field
== -1) continue;
210 if (!strcmpiW( name
, file
->fields
[line
->key_field
].text
)) return line
;
216 /* add a section to the file and return the section index */
217 static int add_section( struct inf_file
*file
, const WCHAR
*name
)
219 struct section
*section
;
221 if (file
->nb_sections
>= file
->alloc_sections
)
223 if (!(file
->sections
= grow_array( file
->sections
, &file
->alloc_sections
,
224 sizeof(file
->sections
[0]) ))) return -1;
226 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) ))) return -1;
227 section
->name
= name
;
228 section
->nb_lines
= 0;
229 section
->alloc_lines
= sizeof(section
->lines
)/sizeof(section
->lines
[0]);
230 file
->sections
[file
->nb_sections
] = section
;
231 return file
->nb_sections
++;
235 /* add a line to a given section */
236 static struct line
*add_line( struct inf_file
*file
, int section_index
)
238 struct section
*section
;
241 assert( section_index
>= 0 && section_index
< file
->nb_sections
);
243 section
= file
->sections
[section_index
];
244 if (section
->nb_lines
== section
->alloc_lines
) /* need to grow the section */
246 int size
= sizeof(*section
) - sizeof(section
->lines
) + 2*section
->alloc_lines
*sizeof(*line
);
247 if (!(section
= HeapReAlloc( GetProcessHeap(), 0, section
, size
))) return NULL
;
248 section
->alloc_lines
*= 2;
249 file
->sections
[section_index
] = section
;
251 line
= §ion
->lines
[section
->nb_lines
++];
252 line
->first_field
= file
->nb_fields
;
254 line
->key_field
= -1;
259 /* retrieve a given line from section/line index */
260 static inline struct line
*get_line( struct inf_file
*file
, unsigned int section_index
,
261 unsigned int line_index
)
263 struct section
*section
;
265 if (section_index
>= file
->nb_sections
) return NULL
;
266 section
= file
->sections
[section_index
];
267 if (line_index
>= section
->nb_lines
) return NULL
;
268 return §ion
->lines
[line_index
];
272 /* retrieve a given field from section/line/field index */
273 static struct field
*get_field( struct inf_file
*file
, int section_index
, int line_index
,
276 struct line
*line
= get_line( file
, section_index
, line_index
);
278 if (!line
) return NULL
;
279 if (!field_index
) /* get the key */
281 if (line
->key_field
== -1) return NULL
;
282 return &file
->fields
[line
->key_field
];
285 if (field_index
>= line
->nb_fields
) return NULL
;
286 return &file
->fields
[line
->first_field
+ field_index
];
290 /* allocate a new field, growing the array if necessary */
291 static struct field
*add_field( struct inf_file
*file
, const WCHAR
*text
)
295 if (file
->nb_fields
>= file
->alloc_fields
)
297 if (!(file
->fields
= grow_array( file
->fields
, &file
->alloc_fields
,
298 sizeof(file
->fields
[0]) ))) return NULL
;
300 field
= &file
->fields
[file
->nb_fields
++];
306 /* retrieve the string substitution for a directory id */
307 static const WCHAR
*get_dirid_subst( const struct inf_file
*file
, int dirid
, unsigned int *len
)
311 if (dirid
== DIRID_SRCPATH
) return get_inf_dir( file
, len
);
312 ret
= DIRID_get_string( dirid
);
313 if (ret
) *len
= strlenW(ret
);
318 /* retrieve the string substitution for a given string, or NULL if not found */
319 /* if found, len is set to the substitution length */
320 static const WCHAR
*get_string_subst( const struct inf_file
*file
, const WCHAR
*str
, unsigned int *len
,
321 BOOL no_trailing_slash
)
323 static const WCHAR percent
= '%';
325 struct section
*strings_section
;
330 WCHAR
*dirid_str
, *end
;
331 const WCHAR
*ret
= NULL
;
333 if (!*len
) /* empty string (%%) is replaced by single percent */
338 if (file
->strings_section
== -1) goto not_found
;
339 strings_section
= file
->sections
[file
->strings_section
];
340 for (i
= 0, line
= strings_section
->lines
; i
< strings_section
->nb_lines
; i
++, line
++)
342 if (line
->key_field
== -1) continue;
343 if (strncmpiW( str
, file
->fields
[line
->key_field
].text
, *len
)) continue;
344 if (!file
->fields
[line
->key_field
].text
[*len
]) break;
346 if (i
== strings_section
->nb_lines
|| !line
->nb_fields
) goto not_found
;
347 field
= &file
->fields
[line
->first_field
];
348 *len
= strlenW( field
->text
);
351 not_found
: /* check for integer id */
352 if ((dirid_str
= HeapAlloc( GetProcessHeap(), 0, (*len
+1) * sizeof(WCHAR
) )))
354 memcpy( dirid_str
, str
, *len
* sizeof(WCHAR
) );
356 dirid
= strtolW( dirid_str
, &end
, 10 );
357 if (!*end
) ret
= get_dirid_subst( file
, dirid
, len
);
358 if (no_trailing_slash
&& ret
&& *len
&& ret
[*len
- 1] == '\\') *len
-= 1;
359 HeapFree( GetProcessHeap(), 0, dirid_str
);
366 /* do string substitutions on the specified text */
367 /* the buffer is assumed to be large enough */
368 /* returns necessary length not including terminating null */
369 static unsigned int PARSER_string_substW( const struct inf_file
*file
, const WCHAR
*text
,
370 WCHAR
*buffer
, unsigned int size
)
372 const WCHAR
*start
, *subst
, *p
;
373 unsigned int len
, total
= 0;
376 if (!buffer
) size
= MAX_STRING_LEN
+ 1;
377 for (p
= start
= text
; *p
; p
++)
379 if (*p
!= '%') continue;
381 if (inside
) /* start of a %xx% string */
384 if (len
> size
- 1) len
= size
- 1;
385 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
390 else /* end of the %xx% string, find substitution */
393 subst
= get_string_subst( file
, start
+ 1, &len
, p
[1] == '\\' );
399 if (len
> size
- 1) len
= size
- 1;
400 if (buffer
) memcpy( buffer
+ total
, subst
, len
* sizeof(WCHAR
) );
407 if (start
!= p
) /* unfinished string, copy it */
410 if (len
> size
- 1) len
= size
- 1;
411 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
414 if (buffer
&& size
) buffer
[total
] = 0;
419 /* do string substitutions on the specified text */
420 /* the buffer is assumed to be large enough */
421 /* returns necessary length not including terminating null */
422 static unsigned int PARSER_string_substA( const struct inf_file
*file
, const WCHAR
*text
,
423 char *buffer
, unsigned int size
)
425 WCHAR buffW
[MAX_STRING_LEN
+1];
428 unsigned int len
= PARSER_string_substW( file
, text
, buffW
, sizeof(buffW
)/sizeof(WCHAR
) );
429 if (!buffer
) RtlUnicodeToMultiByteSize( &ret
, buffW
, len
* sizeof(WCHAR
) );
432 RtlUnicodeToMultiByteN( buffer
, size
-1, &ret
, buffW
, len
* sizeof(WCHAR
) );
439 /* push some string data into the strings buffer */
440 static WCHAR
*push_string( struct inf_file
*file
, const WCHAR
*string
)
442 WCHAR
*ret
= file
->string_pos
;
443 strcpyW( ret
, string
);
444 file
->string_pos
+= strlenW( ret
) + 1;
449 /* push the current state on the parser stack */
450 static inline void push_state( struct parser
*parser
, enum parser_state state
)
452 assert( parser
->stack_pos
< sizeof(parser
->stack
)/sizeof(parser
->stack
[0]) );
453 parser
->stack
[parser
->stack_pos
++] = state
;
457 /* pop the current state */
458 static inline void pop_state( struct parser
*parser
)
460 assert( parser
->stack_pos
);
461 parser
->state
= parser
->stack
[--parser
->stack_pos
];
465 /* set the parser state and return the previous one */
466 static inline enum parser_state
set_state( struct parser
*parser
, enum parser_state state
)
468 enum parser_state ret
= parser
->state
;
469 parser
->state
= state
;
474 /* check if the pointer points to an end of file */
475 static inline BOOL
is_eof( const struct parser
*parser
, const WCHAR
*ptr
)
477 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
);
481 /* check if the pointer points to an end of line */
482 static inline BOOL
is_eol( const struct parser
*parser
, const WCHAR
*ptr
)
484 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
|| *ptr
== '\n');
488 /* push data from current token start up to pos into the current token */
489 static int push_token( struct parser
*parser
, const WCHAR
*pos
)
491 int len
= pos
- parser
->start
;
492 const WCHAR
*src
= parser
->start
;
493 WCHAR
*dst
= parser
->token
+ parser
->token_len
;
495 if (len
> MAX_FIELD_LEN
- parser
->token_len
) len
= MAX_FIELD_LEN
- parser
->token_len
;
497 parser
->token_len
+= len
;
498 for ( ; len
> 0; len
--, dst
++, src
++) *dst
= *src
? *src
: ' ';
505 /* add a section with the current token as name */
506 static int add_section_from_token( struct parser
*parser
)
510 if (parser
->token_len
> MAX_SECTION_NAME_LEN
)
512 parser
->error
= ERROR_SECTION_NAME_TOO_LONG
;
515 if ((section_index
= find_section( parser
->file
, parser
->token
)) == -1)
517 /* need to create a new one */
518 const WCHAR
*name
= push_string( parser
->file
, parser
->token
);
519 if ((section_index
= add_section( parser
->file
, name
)) == -1)
521 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
525 parser
->token_len
= 0;
526 parser
->cur_section
= section_index
;
527 return section_index
;
531 /* add a field containing the current token to the current line */
532 static struct field
*add_field_from_token( struct parser
*parser
, BOOL is_key
)
537 if (!parser
->line
) /* need to start a new line */
539 if (parser
->cur_section
== -1) /* got a line before the first section */
541 parser
->error
= ERROR_EXPECTED_SECTION_NAME
;
544 if (!(parser
->line
= add_line( parser
->file
, parser
->cur_section
))) goto error
;
546 else assert(!is_key
);
548 text
= push_string( parser
->file
, parser
->token
);
549 if ((field
= add_field( parser
->file
, text
)))
551 if (!is_key
) parser
->line
->nb_fields
++;
554 /* replace first field by key field */
555 parser
->line
->key_field
= parser
->line
->first_field
;
556 parser
->line
->first_field
++;
558 parser
->token_len
= 0;
562 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
567 /* close the current line and prepare for parsing a new one */
568 static void close_current_line( struct parser
*parser
)
570 struct line
*cur_line
= parser
->line
;
574 /* if line has a single field and no key, the field is the key too */
575 if (cur_line
->nb_fields
== 1 && cur_line
->key_field
== -1)
576 cur_line
->key_field
= cur_line
->first_field
;
582 /* handler for parser LINE_START state */
583 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
)
587 for (p
= pos
; !is_eof( parser
, p
); p
++)
593 close_current_line( parser
);
596 push_state( parser
, LINE_START
);
597 set_state( parser
, COMMENT
);
600 parser
->start
= p
+ 1;
601 set_state( parser
, SECTION_NAME
);
604 if (isspaceW(*p
)) break;
605 if (parser
->cur_section
!= -1)
608 set_state( parser
, KEY_NAME
);
611 if (!parser
->broken_line
)
612 parser
->broken_line
= parser
->line_pos
;
616 close_current_line( parser
);
621 /* handler for parser SECTION_NAME state */
622 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
)
626 for (p
= pos
; !is_eol( parser
, p
); p
++)
630 push_token( parser
, p
);
631 if (add_section_from_token( parser
) == -1) return NULL
;
632 push_state( parser
, LINE_START
);
633 set_state( parser
, COMMENT
); /* ignore everything else on the line */
637 parser
->error
= ERROR_BAD_SECTION_NAME_LINE
; /* unfinished section name */
642 /* handler for parser KEY_NAME state */
643 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
)
645 const WCHAR
*p
, *token_end
= parser
->start
;
647 for (p
= pos
; !is_eol( parser
, p
); p
++)
649 if (*p
== ',') break;
654 push_token( parser
, token_end
);
655 if (!add_field_from_token( parser
, TRUE
)) return NULL
;
656 parser
->start
= p
+ 1;
657 push_state( parser
, VALUE_NAME
);
658 set_state( parser
, LEADING_SPACES
);
661 push_token( parser
, token_end
);
662 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
663 push_state( parser
, LINE_START
);
664 set_state( parser
, COMMENT
);
667 push_token( parser
, p
);
668 parser
->start
= p
+ 1;
669 push_state( parser
, KEY_NAME
);
670 set_state( parser
, QUOTES
);
673 push_token( parser
, token_end
);
675 push_state( parser
, KEY_NAME
);
676 set_state( parser
, EOL_BACKSLASH
);
679 if (!isspaceW(*p
)) token_end
= p
+ 1;
682 push_token( parser
, p
);
683 push_state( parser
, KEY_NAME
);
684 set_state( parser
, TRAILING_SPACES
);
690 push_token( parser
, token_end
);
691 set_state( parser
, VALUE_NAME
);
696 /* handler for parser VALUE_NAME state */
697 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
)
699 const WCHAR
*p
, *token_end
= parser
->start
;
701 for (p
= pos
; !is_eol( parser
, p
); p
++)
706 push_token( parser
, token_end
);
707 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
708 push_state( parser
, LINE_START
);
709 set_state( parser
, COMMENT
);
712 push_token( parser
, token_end
);
713 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
714 parser
->start
= p
+ 1;
715 push_state( parser
, VALUE_NAME
);
716 set_state( parser
, LEADING_SPACES
);
719 push_token( parser
, p
);
720 parser
->start
= p
+ 1;
721 push_state( parser
, VALUE_NAME
);
722 set_state( parser
, QUOTES
);
725 push_token( parser
, token_end
);
727 push_state( parser
, VALUE_NAME
);
728 set_state( parser
, EOL_BACKSLASH
);
731 if (!isspaceW(*p
)) token_end
= p
+ 1;
734 push_token( parser
, p
);
735 push_state( parser
, VALUE_NAME
);
736 set_state( parser
, TRAILING_SPACES
);
742 push_token( parser
, token_end
);
743 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
744 set_state( parser
, LINE_START
);
749 /* handler for parser EOL_BACKSLASH state */
750 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
)
754 for (p
= pos
; !is_eof( parser
, p
); p
++)
760 parser
->start
= p
+ 1;
761 set_state( parser
, LEADING_SPACES
);
766 push_state( parser
, EOL_BACKSLASH
);
767 set_state( parser
, COMMENT
);
770 if (isspaceW(*p
)) continue;
771 push_token( parser
, p
);
782 /* handler for parser QUOTES state */
783 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
)
787 for (p
= pos
; !is_eol( parser
, p
); p
++)
791 if (p
+1 < parser
->end
&& p
[1] == '"') /* double quotes */
793 push_token( parser
, p
+ 1 );
794 parser
->start
= p
+ 2;
797 else /* end of quotes */
799 push_token( parser
, p
);
800 parser
->start
= p
+ 1;
806 push_token( parser
, p
);
812 /* handler for parser LEADING_SPACES state */
813 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
817 for (p
= pos
; !is_eol( parser
, p
); p
++)
822 set_state( parser
, EOL_BACKSLASH
);
825 if (!isspaceW(*p
)) break;
833 /* handler for parser TRAILING_SPACES state */
834 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
838 for (p
= pos
; !is_eol( parser
, p
); p
++)
842 set_state( parser
, EOL_BACKSLASH
);
845 if (!isspaceW(*p
)) break;
852 /* handler for parser COMMENT state */
853 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
)
855 const WCHAR
*p
= pos
;
857 while (!is_eol( parser
, p
)) p
++;
863 static void free_inf_file( struct inf_file
*file
)
867 for (i
= 0; i
< file
->nb_sections
; i
++) HeapFree( GetProcessHeap(), 0, file
->sections
[i
] );
868 HeapFree( GetProcessHeap(), 0, file
->filename
);
869 HeapFree( GetProcessHeap(), 0, file
->sections
);
870 HeapFree( GetProcessHeap(), 0, file
->fields
);
871 HeapFree( GetProcessHeap(), 0, file
->strings
);
872 HeapFree( GetProcessHeap(), 0, file
);
876 /* parse a complete buffer */
877 static DWORD
parse_buffer( struct inf_file
*file
, const WCHAR
*buffer
, const WCHAR
*end
,
880 static const WCHAR Strings
[] = {'S','t','r','i','n','g','s',0};
882 struct parser parser
;
883 const WCHAR
*pos
= buffer
;
885 parser
.start
= buffer
;
889 parser
.state
= LINE_START
;
890 parser
.stack_pos
= 0;
891 parser
.cur_section
= -1;
893 parser
.broken_line
= 0;
895 parser
.token_len
= 0;
897 /* parser main loop */
898 while (pos
) pos
= (parser_funcs
[parser
.state
])( &parser
, pos
);
900 /* trim excess buffer space */
901 if (file
->alloc_sections
> file
->nb_sections
)
903 file
->sections
= HeapReAlloc( GetProcessHeap(), 0, file
->sections
,
904 file
->nb_sections
* sizeof(file
->sections
[0]) );
905 file
->alloc_sections
= file
->nb_sections
;
907 if (file
->alloc_fields
> file
->nb_fields
)
909 file
->fields
= HeapReAlloc( GetProcessHeap(), 0, file
->fields
,
910 file
->nb_fields
* sizeof(file
->fields
[0]) );
911 file
->alloc_fields
= file
->nb_fields
;
913 file
->strings
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, file
->strings
,
914 (file
->string_pos
- file
->strings
) * sizeof(WCHAR
) );
918 if (error_line
) *error_line
= parser
.line_pos
;
922 /* find the [strings] section */
923 file
->strings_section
= find_section( file
, Strings
);
925 if (file
->strings_section
== -1 && parser
.broken_line
)
927 if (error_line
) *error_line
= parser
.broken_line
;
928 return ERROR_EXPECTED_SECTION_NAME
;
935 /* append a child INF file to its parent list, in a thread-safe manner */
936 static void append_inf_file( struct inf_file
*parent
, struct inf_file
*child
)
938 struct inf_file
**ppnext
= &parent
->next
;
943 struct inf_file
*next
= InterlockedCompareExchangePointer( (void **)ppnext
, child
, NULL
);
945 ppnext
= &next
->next
;
950 /***********************************************************************
955 static struct inf_file
*parse_file( HANDLE handle
, const WCHAR
*class, DWORD style
, UINT
*error_line
)
959 struct inf_file
*file
;
961 DWORD size
= GetFileSize( handle
, NULL
);
962 HANDLE mapping
= CreateFileMappingW( handle
, NULL
, PAGE_READONLY
, 0, size
, NULL
);
963 if (!mapping
) return NULL
;
964 buffer
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, size
);
966 if (!buffer
) return NULL
;
968 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
970 if (!(file
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*file
) )))
972 err
= ERROR_NOT_ENOUGH_MEMORY
;
976 /* we won't need more strings space than the size of the file,
977 * so we can preallocate it here
979 if (!(file
->strings
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
981 err
= ERROR_NOT_ENOUGH_MEMORY
;
984 file
->string_pos
= file
->strings
;
985 file
->strings_section
= -1;
987 if (!RtlIsTextUnicode( buffer
, size
, NULL
))
989 static const BYTE utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
991 UINT codepage
= CP_ACP
;
994 if (size
> sizeof(utf8_bom
) && !memcmp( buffer
, utf8_bom
, sizeof(utf8_bom
) ))
997 offset
= sizeof(utf8_bom
);
1000 if ((new_buff
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
1002 DWORD len
= MultiByteToWideChar( codepage
, 0, (char *)buffer
+ offset
,
1003 size
- offset
, new_buff
, size
);
1004 err
= parse_buffer( file
, new_buff
, new_buff
+ len
, error_line
);
1005 HeapFree( GetProcessHeap(), 0, new_buff
);
1010 WCHAR
*new_buff
= buffer
;
1011 /* UCS-16 files should start with the Unicode BOM; we should skip it */
1012 if (*new_buff
== 0xfeff)
1014 err
= parse_buffer( file
, new_buff
, (WCHAR
*)((char *)buffer
+ size
), error_line
);
1017 if (!err
) /* now check signature */
1019 int version_index
= find_section( file
, Version
);
1020 if (version_index
!= -1)
1022 struct line
*line
= find_line( file
, version_index
, Signature
);
1023 if (line
&& line
->nb_fields
> 0)
1025 struct field
*field
= file
->fields
+ line
->first_field
;
1026 if (!strcmpiW( field
->text
, Chicago
)) goto done
;
1027 if (!strcmpiW( field
->text
, WindowsNT
)) goto done
;
1028 if (!strcmpiW( field
->text
, Windows95
)) goto done
;
1031 if (error_line
) *error_line
= 0;
1032 if (style
& INF_STYLE_WIN4
) err
= ERROR_WRONG_INF_STYLE
;
1036 UnmapViewOfFile( buffer
);
1039 if (file
) free_inf_file( file
);
1040 SetLastError( err
);
1047 /***********************************************************************
1048 * PARSER_get_inf_filename
1050 * Retrieve the filename of an inf file.
1052 const WCHAR
*PARSER_get_inf_filename( HINF hinf
)
1054 struct inf_file
*file
= hinf
;
1055 return file
->filename
;
1059 /***********************************************************************
1060 * PARSER_get_src_root
1062 * Retrieve the source directory of an inf file.
1064 WCHAR
*PARSER_get_src_root( HINF hinf
)
1067 const WCHAR
*dir
= get_inf_dir( hinf
, &len
);
1068 WCHAR
*ret
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
1071 memcpy( ret
, dir
, len
* sizeof(WCHAR
) );
1078 /***********************************************************************
1079 * PARSER_get_dest_dir
1081 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1082 * returned buffer must be freed by caller.
1084 WCHAR
*PARSER_get_dest_dir( INFCONTEXT
*context
)
1092 if (!SetupGetIntField( context
, 1, &dirid
)) return NULL
;
1093 if (!(dir
= get_dirid_subst( context
->Inf
, dirid
, &len1
))) return NULL
;
1094 if (!SetupGetStringFieldW( context
, 2, NULL
, 0, &len2
)) len2
= 0;
1095 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, (len1
+len2
+1) * sizeof(WCHAR
) ))) return NULL
;
1096 memcpy( ret
, dir
, len1
* sizeof(WCHAR
) );
1098 if (len2
&& ptr
> ret
&& ptr
[-1] != '\\') *ptr
++ = '\\';
1099 if (!SetupGetStringFieldW( context
, 2, ptr
, len2
, NULL
)) *ptr
= 0;
1104 /***********************************************************************
1105 * SetupOpenInfFileA (SETUPAPI.@)
1107 HINF WINAPI
SetupOpenInfFileA( PCSTR name
, PCSTR
class, DWORD style
, UINT
*error
)
1109 UNICODE_STRING nameW
, classW
;
1110 HINF ret
= INVALID_HANDLE_VALUE
;
1112 classW
.Buffer
= NULL
;
1113 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW
, class ))
1115 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1118 if (RtlCreateUnicodeStringFromAsciiz( &nameW
, name
))
1120 ret
= SetupOpenInfFileW( nameW
.Buffer
, classW
.Buffer
, style
, error
);
1121 RtlFreeUnicodeString( &nameW
);
1123 RtlFreeUnicodeString( &classW
);
1128 /***********************************************************************
1129 * SetupOpenInfFileW (SETUPAPI.@)
1131 HINF WINAPI
SetupOpenInfFileW( PCWSTR name
, PCWSTR
class, DWORD style
, UINT
*error
)
1133 struct inf_file
*file
= NULL
;
1138 if (strchrW( name
, '\\' ) || strchrW( name
, '/' ))
1140 if (!(len
= GetFullPathNameW( name
, 0, NULL
, NULL
))) return INVALID_HANDLE_VALUE
;
1141 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1143 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1144 return INVALID_HANDLE_VALUE
;
1146 GetFullPathNameW( name
, len
, path
, NULL
);
1147 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1149 else /* try Windows directory */
1151 static const WCHAR Inf
[] = {'\\','i','n','f','\\',0};
1152 static const WCHAR System32
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1154 len
= GetWindowsDirectoryW( NULL
, 0 ) + strlenW(name
) + 12;
1155 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1157 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1158 return INVALID_HANDLE_VALUE
;
1160 GetWindowsDirectoryW( path
, len
);
1161 p
= path
+ strlenW(path
);
1164 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1165 if (handle
== INVALID_HANDLE_VALUE
)
1167 strcpyW( p
, System32
);
1169 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1173 if (handle
!= INVALID_HANDLE_VALUE
)
1175 file
= parse_file( handle
, class, style
, error
);
1176 CloseHandle( handle
);
1180 HeapFree( GetProcessHeap(), 0, path
);
1181 return INVALID_HANDLE_VALUE
;
1183 TRACE( "%s -> %p\n", debugstr_w(path
), file
);
1184 file
->filename
= path
;
1190 /***********************************************************************
1191 * SetupOpenAppendInfFileA (SETUPAPI.@)
1193 BOOL WINAPI
SetupOpenAppendInfFileA( PCSTR name
, HINF parent_hinf
, UINT
*error
)
1197 if (!name
) return SetupOpenAppendInfFileW( NULL
, parent_hinf
, error
);
1198 child_hinf
= SetupOpenInfFileA( name
, NULL
, INF_STYLE_WIN4
, error
);
1199 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1200 append_inf_file( parent_hinf
, child_hinf
);
1201 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_a(name
), child_hinf
);
1206 /***********************************************************************
1207 * SetupOpenAppendInfFileW (SETUPAPI.@)
1209 BOOL WINAPI
SetupOpenAppendInfFileW( PCWSTR name
, HINF parent_hinf
, UINT
*error
)
1216 WCHAR filename
[MAX_PATH
];
1219 if (!SetupFindFirstLineW( parent_hinf
, Version
, LayoutFile
, &context
)) return FALSE
;
1220 while (SetupGetStringFieldW( &context
, idx
++, filename
,
1221 sizeof(filename
)/sizeof(WCHAR
), NULL
))
1223 child_hinf
= SetupOpenInfFileW( filename
, NULL
, INF_STYLE_WIN4
, error
);
1224 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1225 append_inf_file( parent_hinf
, child_hinf
);
1226 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(filename
), child_hinf
);
1230 child_hinf
= SetupOpenInfFileW( name
, NULL
, INF_STYLE_WIN4
, error
);
1231 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1232 append_inf_file( parent_hinf
, child_hinf
);
1233 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(name
), child_hinf
);
1238 /***********************************************************************
1239 * SetupOpenMasterInf (SETUPAPI.@)
1241 HINF WINAPI
SetupOpenMasterInf( VOID
)
1243 static const WCHAR Layout
[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1244 WCHAR Buffer
[MAX_PATH
];
1246 GetWindowsDirectoryW( Buffer
, MAX_PATH
);
1247 strcatW( Buffer
, Layout
);
1248 return SetupOpenInfFileW( Buffer
, NULL
, INF_STYLE_WIN4
, NULL
);
1253 /***********************************************************************
1254 * SetupCloseInfFile (SETUPAPI.@)
1256 void WINAPI
SetupCloseInfFile( HINF hinf
)
1258 struct inf_file
*file
= hinf
;
1260 if (!hinf
|| (hinf
== INVALID_HANDLE_VALUE
)) return;
1262 free_inf_file( file
);
1266 /***********************************************************************
1267 * SetupEnumInfSectionsA (SETUPAPI.@)
1269 BOOL WINAPI
SetupEnumInfSectionsA( HINF hinf
, UINT index
, PSTR buffer
, DWORD size
, DWORD
*need
)
1271 struct inf_file
*file
= hinf
;
1273 for (file
= hinf
; file
; file
= file
->next
)
1275 if (index
< file
->nb_sections
)
1277 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, file
->sections
[index
]->name
, -1,
1278 NULL
, 0, NULL
, NULL
);
1279 if (need
) *need
= len
;
1282 if (!size
) return TRUE
;
1283 SetLastError( ERROR_INVALID_USER_BUFFER
);
1288 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1291 WideCharToMultiByte( CP_ACP
, 0, file
->sections
[index
]->name
, -1, buffer
, size
, NULL
, NULL
);
1294 index
-= file
->nb_sections
;
1296 SetLastError( ERROR_NO_MORE_ITEMS
);
1301 /***********************************************************************
1302 * SetupEnumInfSectionsW (SETUPAPI.@)
1304 BOOL WINAPI
SetupEnumInfSectionsW( HINF hinf
, UINT index
, PWSTR buffer
, DWORD size
, DWORD
*need
)
1306 struct inf_file
*file
= hinf
;
1308 for (file
= hinf
; file
; file
= file
->next
)
1310 if (index
< file
->nb_sections
)
1312 DWORD len
= strlenW( file
->sections
[index
]->name
) + 1;
1313 if (need
) *need
= len
;
1316 if (!size
) return TRUE
;
1317 SetLastError( ERROR_INVALID_USER_BUFFER
);
1322 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1325 memcpy( buffer
, file
->sections
[index
]->name
, len
* sizeof(WCHAR
) );
1328 index
-= file
->nb_sections
;
1330 SetLastError( ERROR_NO_MORE_ITEMS
);
1335 /***********************************************************************
1336 * SetupGetLineCountA (SETUPAPI.@)
1338 LONG WINAPI
SetupGetLineCountA( HINF hinf
, PCSTR name
)
1340 UNICODE_STRING sectionW
;
1343 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, name
))
1344 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1347 ret
= SetupGetLineCountW( hinf
, sectionW
.Buffer
);
1348 RtlFreeUnicodeString( §ionW
);
1354 /***********************************************************************
1355 * SetupGetLineCountW (SETUPAPI.@)
1357 LONG WINAPI
SetupGetLineCountW( HINF hinf
, PCWSTR section
)
1359 struct inf_file
*file
= hinf
;
1363 for (file
= hinf
; file
; file
= file
->next
)
1365 if ((section_index
= find_section( file
, section
)) == -1) continue;
1366 if (ret
== -1) ret
= 0;
1367 ret
+= file
->sections
[section_index
]->nb_lines
;
1369 TRACE( "(%p,%s) returning %d\n", hinf
, debugstr_w(section
), ret
);
1370 SetLastError( (ret
== -1) ? ERROR_SECTION_NOT_FOUND
: 0 );
1375 /***********************************************************************
1376 * SetupGetLineByIndexA (SETUPAPI.@)
1378 BOOL WINAPI
SetupGetLineByIndexA( HINF hinf
, PCSTR section
, DWORD index
, INFCONTEXT
*context
)
1380 UNICODE_STRING sectionW
;
1383 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1384 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1387 ret
= SetupGetLineByIndexW( hinf
, sectionW
.Buffer
, index
, context
);
1388 RtlFreeUnicodeString( §ionW
);
1394 /***********************************************************************
1395 * SetupGetLineByIndexW (SETUPAPI.@)
1397 BOOL WINAPI
SetupGetLineByIndexW( HINF hinf
, PCWSTR section
, DWORD index
, INFCONTEXT
*context
)
1399 struct inf_file
*file
= hinf
;
1402 for (file
= hinf
; file
; file
= file
->next
)
1404 if ((section_index
= find_section( file
, section
)) == -1) continue;
1405 if (index
< file
->sections
[section_index
]->nb_lines
)
1407 context
->Inf
= hinf
;
1408 context
->CurrentInf
= file
;
1409 context
->Section
= section_index
;
1410 context
->Line
= index
;
1412 TRACE( "(%p,%s): returning %d/%d\n",
1413 hinf
, debugstr_w(section
), section_index
, index
);
1416 index
-= file
->sections
[section_index
]->nb_lines
;
1418 TRACE( "(%p,%s) not found\n", hinf
, debugstr_w(section
) );
1419 SetLastError( ERROR_LINE_NOT_FOUND
);
1424 /***********************************************************************
1425 * SetupFindFirstLineA (SETUPAPI.@)
1427 BOOL WINAPI
SetupFindFirstLineA( HINF hinf
, PCSTR section
, PCSTR key
, INFCONTEXT
*context
)
1429 UNICODE_STRING sectionW
, keyW
;
1432 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1434 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1438 if (!key
) ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, NULL
, context
);
1441 if (RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1443 ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, keyW
.Buffer
, context
);
1444 RtlFreeUnicodeString( &keyW
);
1446 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1448 RtlFreeUnicodeString( §ionW
);
1453 /***********************************************************************
1454 * SetupFindFirstLineW (SETUPAPI.@)
1456 BOOL WINAPI
SetupFindFirstLineW( HINF hinf
, PCWSTR section
, PCWSTR key
, INFCONTEXT
*context
)
1458 struct inf_file
*file
;
1461 for (file
= hinf
; file
; file
= file
->next
)
1463 if ((section_index
= find_section( file
, section
)) == -1) continue;
1468 ctx
.CurrentInf
= file
;
1469 ctx
.Section
= section_index
;
1471 return SetupFindNextMatchLineW( &ctx
, key
, context
);
1473 if (file
->sections
[section_index
]->nb_lines
)
1475 context
->Inf
= hinf
;
1476 context
->CurrentInf
= file
;
1477 context
->Section
= section_index
;
1480 TRACE( "(%p,%s,%s): returning %d/0\n",
1481 hinf
, debugstr_w(section
), debugstr_w(key
), section_index
);
1485 TRACE( "(%p,%s,%s): not found\n", hinf
, debugstr_w(section
), debugstr_w(key
) );
1486 SetLastError( ERROR_LINE_NOT_FOUND
);
1491 /***********************************************************************
1492 * SetupFindNextLine (SETUPAPI.@)
1494 BOOL WINAPI
SetupFindNextLine( PINFCONTEXT context_in
, PINFCONTEXT context_out
)
1496 struct inf_file
*file
= context_in
->CurrentInf
;
1497 struct section
*section
;
1499 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1501 section
= file
->sections
[context_in
->Section
];
1502 if (context_in
->Line
+1 < section
->nb_lines
)
1504 if (context_out
!= context_in
) *context_out
= *context_in
;
1505 context_out
->Line
++;
1510 /* now search the appended files */
1512 for (file
= file
->next
; file
; file
= file
->next
)
1514 int section_index
= find_section( file
, section
->name
);
1515 if (section_index
== -1) continue;
1516 if (file
->sections
[section_index
]->nb_lines
)
1518 context_out
->Inf
= context_in
->Inf
;
1519 context_out
->CurrentInf
= file
;
1520 context_out
->Section
= section_index
;
1521 context_out
->Line
= 0;
1527 SetLastError( ERROR_LINE_NOT_FOUND
);
1532 /***********************************************************************
1533 * SetupFindNextMatchLineA (SETUPAPI.@)
1535 BOOL WINAPI
SetupFindNextMatchLineA( PINFCONTEXT context_in
, PCSTR key
,
1536 PINFCONTEXT context_out
)
1538 UNICODE_STRING keyW
;
1541 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1543 if (!RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1544 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1547 ret
= SetupFindNextMatchLineW( context_in
, keyW
.Buffer
, context_out
);
1548 RtlFreeUnicodeString( &keyW
);
1554 /***********************************************************************
1555 * SetupFindNextMatchLineW (SETUPAPI.@)
1557 BOOL WINAPI
SetupFindNextMatchLineW( PINFCONTEXT context_in
, PCWSTR key
,
1558 PINFCONTEXT context_out
)
1560 struct inf_file
*file
= context_in
->CurrentInf
;
1561 struct section
*section
;
1565 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1567 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1569 section
= file
->sections
[context_in
->Section
];
1571 for (i
= context_in
->Line
+1, line
= §ion
->lines
[i
]; i
< section
->nb_lines
; i
++, line
++)
1573 if (line
->key_field
== -1) continue;
1574 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1576 if (context_out
!= context_in
) *context_out
= *context_in
;
1577 context_out
->Line
= i
;
1579 TRACE( "(%p,%s,%s): returning %d\n",
1580 file
, debugstr_w(section
->name
), debugstr_w(key
), i
);
1585 /* now search the appended files */
1587 for (file
= file
->next
; file
; file
= file
->next
)
1589 int section_index
= find_section( file
, section
->name
);
1590 if (section_index
== -1) continue;
1591 section
= file
->sections
[section_index
];
1592 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
1594 if (line
->key_field
== -1) continue;
1595 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1597 context_out
->Inf
= context_in
->Inf
;
1598 context_out
->CurrentInf
= file
;
1599 context_out
->Section
= section_index
;
1600 context_out
->Line
= i
;
1602 TRACE( "(%p,%s,%s): returning %d/%d\n",
1603 file
, debugstr_w(section
->name
), debugstr_w(key
), section_index
, i
);
1608 TRACE( "(%p,%s,%s): not found\n",
1609 context_in
->CurrentInf
, debugstr_w(section
->name
), debugstr_w(key
) );
1611 SetLastError( ERROR_LINE_NOT_FOUND
);
1616 /***********************************************************************
1617 * SetupGetLineTextW (SETUPAPI.@)
1619 BOOL WINAPI
SetupGetLineTextW( PINFCONTEXT context
, HINF hinf
, PCWSTR section_name
,
1620 PCWSTR key_name
, PWSTR buffer
, DWORD size
, PDWORD required
)
1622 struct inf_file
*file
;
1624 struct field
*field
;
1630 INFCONTEXT new_context
;
1631 if (!SetupFindFirstLineW( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1632 file
= new_context
.CurrentInf
;
1633 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1637 file
= context
->CurrentInf
;
1638 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1640 SetLastError( ERROR_LINE_NOT_FOUND
);
1645 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1646 total
+= PARSER_string_substW( file
, field
->text
, NULL
, 0 ) + 1;
1648 if (required
) *required
= total
;
1653 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1656 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1658 unsigned int len
= PARSER_string_substW( file
, field
->text
, buffer
, size
);
1659 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1667 /***********************************************************************
1668 * SetupGetLineTextA (SETUPAPI.@)
1670 BOOL WINAPI
SetupGetLineTextA( PINFCONTEXT context
, HINF hinf
, PCSTR section_name
,
1671 PCSTR key_name
, PSTR buffer
, DWORD size
, PDWORD required
)
1673 struct inf_file
*file
;
1675 struct field
*field
;
1681 INFCONTEXT new_context
;
1682 if (!SetupFindFirstLineA( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1683 file
= new_context
.CurrentInf
;
1684 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1688 file
= context
->CurrentInf
;
1689 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1691 SetLastError( ERROR_LINE_NOT_FOUND
);
1696 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1697 total
+= PARSER_string_substA( file
, field
->text
, NULL
, 0 ) + 1;
1699 if (required
) *required
= total
;
1704 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1707 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1709 unsigned int len
= PARSER_string_substA( file
, field
->text
, buffer
, size
);
1710 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1718 /***********************************************************************
1719 * SetupGetFieldCount (SETUPAPI.@)
1721 DWORD WINAPI
SetupGetFieldCount( PINFCONTEXT context
)
1723 struct inf_file
*file
= context
->CurrentInf
;
1724 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1726 if (!line
) return 0;
1727 return line
->nb_fields
;
1731 /***********************************************************************
1732 * SetupGetStringFieldA (SETUPAPI.@)
1734 BOOL WINAPI
SetupGetStringFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1735 DWORD size
, PDWORD required
)
1737 struct inf_file
*file
= context
->CurrentInf
;
1738 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1742 if (!field
) return FALSE
;
1743 len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 );
1744 if (required
) *required
= len
+ 1;
1749 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1752 PARSER_string_substA( file
, field
->text
, buffer
, size
);
1754 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1755 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1756 index
, debugstr_a(buffer
) );
1762 /***********************************************************************
1763 * SetupGetStringFieldW (SETUPAPI.@)
1765 BOOL WINAPI
SetupGetStringFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1766 DWORD size
, PDWORD required
)
1768 struct inf_file
*file
= context
->CurrentInf
;
1769 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1773 if (!field
) return FALSE
;
1774 len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 );
1775 if (required
) *required
= len
+ 1;
1780 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1783 PARSER_string_substW( file
, field
->text
, buffer
, size
);
1785 TRACE( "context %p/%p/%d/%d index %d returning %s\n",
1786 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1787 index
, debugstr_w(buffer
) );
1793 /***********************************************************************
1794 * SetupGetIntField (SETUPAPI.@)
1796 BOOL WINAPI
SetupGetIntField( PINFCONTEXT context
, DWORD index
, PINT result
)
1799 char *end
, *buffer
= localbuff
;
1804 if (!(ret
= SetupGetStringFieldA( context
, index
, localbuff
, sizeof(localbuff
), &required
)))
1806 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
1807 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
))) return FALSE
;
1808 if (!(ret
= SetupGetStringFieldA( context
, index
, buffer
, required
, NULL
))) goto done
;
1810 /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */
1811 if (!*buffer
) *result
= 0;
1814 res
= strtol( buffer
, &end
, 0 );
1815 if (end
!= buffer
&& !*end
) *result
= res
;
1818 SetLastError( ERROR_INVALID_DATA
);
1824 if (buffer
!= localbuff
) HeapFree( GetProcessHeap(), 0, buffer
);
1829 /***********************************************************************
1830 * SetupGetBinaryField (SETUPAPI.@)
1832 BOOL WINAPI
SetupGetBinaryField( PINFCONTEXT context
, DWORD index
, BYTE
*buffer
,
1833 DWORD size
, LPDWORD required
)
1835 struct inf_file
*file
= context
->CurrentInf
;
1836 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1837 struct field
*field
;
1842 SetLastError( ERROR_LINE_NOT_FOUND
);
1845 if (!index
|| index
> line
->nb_fields
)
1847 SetLastError( ERROR_INVALID_PARAMETER
);
1850 index
--; /* fields start at 0 */
1851 if (required
) *required
= line
->nb_fields
- index
;
1852 if (!buffer
) return TRUE
;
1853 if (size
< line
->nb_fields
- index
)
1855 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1858 field
= &file
->fields
[line
->first_field
+ index
];
1859 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1863 for (p
= field
->text
; *p
&& isxdigitW(*p
); p
++)
1865 if ((value
<<= 4) > 255)
1867 SetLastError( ERROR_INVALID_DATA
);
1870 if (*p
<= '9') value
|= (*p
- '0');
1871 else value
|= (tolowerW(*p
) - 'a' + 10);
1873 buffer
[i
- index
] = value
;
1875 if (TRACE_ON(setupapi
))
1877 TRACE( "%p/%p/%d/%d index %d returning",
1878 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
, index
);
1879 for (i
= index
; i
< line
->nb_fields
; i
++) TRACE( " %02x", buffer
[i
- index
] );
1886 /***********************************************************************
1887 * SetupGetMultiSzFieldA (SETUPAPI.@)
1889 BOOL WINAPI
SetupGetMultiSzFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1890 DWORD size
, LPDWORD required
)
1892 struct inf_file
*file
= context
->CurrentInf
;
1893 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1894 struct field
*field
;
1901 SetLastError( ERROR_LINE_NOT_FOUND
);
1904 if (!index
|| index
> line
->nb_fields
)
1906 SetLastError( ERROR_INVALID_PARAMETER
);
1909 index
--; /* fields start at 0 */
1910 field
= &file
->fields
[line
->first_field
+ index
];
1911 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1913 if (!(len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 ))) break;
1917 if (required
) *required
= total
;
1918 if (!buffer
) return TRUE
;
1921 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1924 field
= &file
->fields
[line
->first_field
+ index
];
1925 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1927 if (!(len
= PARSER_string_substA( file
, field
->text
, buffer
, size
))) break;
1930 *buffer
= 0; /* add final null */
1935 /***********************************************************************
1936 * SetupGetMultiSzFieldW (SETUPAPI.@)
1938 BOOL WINAPI
SetupGetMultiSzFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1939 DWORD size
, LPDWORD required
)
1941 struct inf_file
*file
= context
->CurrentInf
;
1942 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1943 struct field
*field
;
1950 SetLastError( ERROR_LINE_NOT_FOUND
);
1953 if (!index
|| index
> line
->nb_fields
)
1955 SetLastError( ERROR_INVALID_PARAMETER
);
1958 index
--; /* fields start at 0 */
1959 field
= &file
->fields
[line
->first_field
+ index
];
1960 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1962 if (!(len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 ))) break;
1966 if (required
) *required
= total
;
1967 if (!buffer
) return TRUE
;
1970 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1973 field
= &file
->fields
[line
->first_field
+ index
];
1974 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1976 if (!(len
= PARSER_string_substW( file
, field
->text
, buffer
, size
))) break;
1979 *buffer
= 0; /* add final null */
1983 /***********************************************************************
1984 * pSetupGetField (SETUPAPI.@)
1986 LPCWSTR WINAPI
pSetupGetField( PINFCONTEXT context
, DWORD index
)
1988 struct inf_file
*file
= context
->CurrentInf
;
1989 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1993 SetLastError( ERROR_INVALID_PARAMETER
);