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
36 #include "setupapi_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
42 #define CONTROL_Z '\x1a'
43 #define MAX_SECTION_NAME_LEN 255
44 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
45 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
46 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
48 /* inf file structure definitions */
52 const WCHAR
*text
; /* field text */
57 int first_field
; /* index of first field in field array */
58 int nb_fields
; /* number of fields in line */
59 int key_field
; /* index of field for key or -1 if no key */
64 const WCHAR
*name
; /* section name */
65 unsigned int nb_lines
; /* number of used lines */
66 unsigned int alloc_lines
; /* total number of allocated lines in array below */
67 struct line lines
[16]; /* lines information (grown dynamically, 16 is initial size) */
72 struct inf_file
*next
; /* next appended file */
73 WCHAR
*strings
; /* buffer for string data (section names and field values) */
74 WCHAR
*string_pos
; /* position of next available string in buffer */
75 unsigned int nb_sections
; /* number of used sections */
76 unsigned int alloc_sections
; /* total number of allocated section pointers */
77 struct section
**sections
; /* section pointers array */
78 unsigned int nb_fields
;
79 unsigned int alloc_fields
;
81 int strings_section
; /* index of [Strings] section or -1 if none */
82 WCHAR
*filename
; /* filename of the INF */
85 /* parser definitions */
89 LINE_START
, /* at beginning of a line */
90 SECTION_NAME
, /* parsing a section name */
91 KEY_NAME
, /* parsing a key name */
92 VALUE_NAME
, /* parsing a value name */
93 EOL_BACKSLASH
, /* backslash at end of line */
94 QUOTES
, /* inside quotes */
95 LEADING_SPACES
, /* leading spaces */
96 TRAILING_SPACES
, /* trailing spaces */
97 COMMENT
, /* inside a comment */
103 const WCHAR
*start
; /* start position of item being parsed */
104 const WCHAR
*end
; /* end of buffer */
105 struct inf_file
*file
; /* file being built */
106 enum parser_state state
; /* current parser state */
107 enum parser_state stack
[4]; /* state stack */
108 int stack_pos
; /* current pos in stack */
110 int cur_section
; /* index of section being parsed*/
111 struct line
*line
; /* current line */
112 unsigned int line_pos
; /* current line position in file */
113 unsigned int broken_line
; /* first line containing invalid data (if any) */
114 unsigned int error
; /* error code */
115 unsigned int token_len
; /* current token len */
116 WCHAR token
[MAX_FIELD_LEN
+1]; /* current token */
119 typedef const WCHAR
* (*parser_state_func
)( struct parser
*parser
, const WCHAR
*pos
);
121 /* parser state machine functions */
122 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
);
123 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
);
124 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
);
125 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
);
126 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
);
127 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
);
128 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
129 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
130 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
);
132 static const parser_state_func parser_funcs
[NB_PARSER_STATES
] =
134 line_start_state
, /* LINE_START */
135 section_name_state
, /* SECTION_NAME */
136 key_name_state
, /* KEY_NAME */
137 value_name_state
, /* VALUE_NAME */
138 eol_backslash_state
, /* EOL_BACKSLASH */
139 quotes_state
, /* QUOTES */
140 leading_spaces_state
, /* LEADING_SPACES */
141 trailing_spaces_state
, /* TRAILING_SPACES */
142 comment_state
/* COMMENT */
146 /* Unicode string constants */
147 static const WCHAR Version
[] = {'V','e','r','s','i','o','n',0};
148 static const WCHAR Signature
[] = {'S','i','g','n','a','t','u','r','e',0};
149 static const WCHAR Chicago
[] = {'$','C','h','i','c','a','g','o','$',0};
150 static const WCHAR WindowsNT
[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
151 static const WCHAR Windows95
[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
152 static const WCHAR LayoutFile
[] = {'L','a','y','o','u','t','F','i','l','e',0};
154 /* extend an array, allocating more memory if necessary */
155 static void *grow_array( void *array
, unsigned int *count
, size_t elem
)
158 unsigned int new_count
= *count
+ *count
/ 2;
159 if (new_count
< 32) new_count
= 32;
162 new_array
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, array
, new_count
* elem
);
164 new_array
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* elem
);
169 HeapFree( GetProcessHeap(), 0, array
);
174 /* get the directory of the inf file (as counted string, not null-terminated) */
175 static const WCHAR
*get_inf_dir( const struct inf_file
*file
, unsigned int *len
)
177 const WCHAR
*p
= wcsrchr( file
->filename
, '\\' );
178 *len
= p
? (p
+ 1 - file
->filename
) : 0;
179 return file
->filename
;
183 /* find a section by name */
184 static int find_section( const struct inf_file
*file
, const WCHAR
*name
)
188 for (i
= 0; i
< file
->nb_sections
; i
++)
189 if (!wcsicmp( name
, file
->sections
[i
]->name
)) return i
;
194 /* find a line by name */
195 static struct line
*find_line( struct inf_file
*file
, int section_index
, const WCHAR
*name
)
197 struct section
*section
;
201 if (section_index
< 0 || section_index
>= file
->nb_sections
) return NULL
;
202 section
= file
->sections
[section_index
];
203 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
205 if (line
->key_field
== -1) continue;
206 if (!wcsicmp( name
, file
->fields
[line
->key_field
].text
)) return line
;
212 /* add a section to the file and return the section index */
213 static int add_section( struct inf_file
*file
, const WCHAR
*name
)
215 struct section
*section
;
217 if (file
->nb_sections
>= file
->alloc_sections
)
219 if (!(file
->sections
= grow_array( file
->sections
, &file
->alloc_sections
,
220 sizeof(file
->sections
[0]) ))) return -1;
222 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) ))) return -1;
223 section
->name
= name
;
224 section
->nb_lines
= 0;
225 section
->alloc_lines
= ARRAY_SIZE( section
->lines
);
226 file
->sections
[file
->nb_sections
] = section
;
227 return file
->nb_sections
++;
231 /* add a line to a given section */
232 static struct line
*add_line( struct inf_file
*file
, int section_index
)
234 struct section
*section
;
237 assert( section_index
>= 0 && section_index
< file
->nb_sections
);
239 section
= file
->sections
[section_index
];
240 if (section
->nb_lines
== section
->alloc_lines
) /* need to grow the section */
242 int size
= sizeof(*section
) - sizeof(section
->lines
) + 2*section
->alloc_lines
*sizeof(*line
);
243 if (!(section
= HeapReAlloc( GetProcessHeap(), 0, section
, size
))) return NULL
;
244 section
->alloc_lines
*= 2;
245 file
->sections
[section_index
] = section
;
247 line
= §ion
->lines
[section
->nb_lines
++];
248 line
->first_field
= file
->nb_fields
;
250 line
->key_field
= -1;
255 /* retrieve a given line from section/line index */
256 static inline struct line
*get_line( struct inf_file
*file
, unsigned int section_index
,
257 unsigned int line_index
)
259 struct section
*section
;
261 if (section_index
>= file
->nb_sections
) return NULL
;
262 section
= file
->sections
[section_index
];
263 if (line_index
>= section
->nb_lines
) return NULL
;
264 return §ion
->lines
[line_index
];
268 /* retrieve a given field from section/line/field index */
269 static struct field
*get_field( struct inf_file
*file
, int section_index
, int line_index
,
272 struct line
*line
= get_line( file
, section_index
, line_index
);
274 if (!line
) return NULL
;
275 if (!field_index
) /* get the key */
277 if (line
->key_field
== -1) return NULL
;
278 return &file
->fields
[line
->key_field
];
281 if (field_index
>= line
->nb_fields
) return NULL
;
282 return &file
->fields
[line
->first_field
+ field_index
];
286 /* allocate a new field, growing the array if necessary */
287 static struct field
*add_field( struct inf_file
*file
, const WCHAR
*text
)
291 if (file
->nb_fields
>= file
->alloc_fields
)
293 if (!(file
->fields
= grow_array( file
->fields
, &file
->alloc_fields
,
294 sizeof(file
->fields
[0]) ))) return NULL
;
296 field
= &file
->fields
[file
->nb_fields
++];
302 /* retrieve the string substitution for a directory id */
303 static const WCHAR
*get_dirid_subst( const struct inf_file
*file
, int dirid
, unsigned int *len
)
307 if (dirid
== DIRID_SRCPATH
) return get_inf_dir( file
, len
);
308 ret
= DIRID_get_string( dirid
);
309 if (ret
) *len
= lstrlenW(ret
);
314 /* retrieve the string substitution for a given string, or NULL if not found */
315 /* if found, len is set to the substitution length */
316 static const WCHAR
*get_string_subst( const struct inf_file
*file
, const WCHAR
*str
, unsigned int *len
,
317 BOOL no_trailing_slash
)
319 static const WCHAR percent
= '%';
321 struct section
*strings_section
;
326 WCHAR
*dirid_str
, *end
;
327 const WCHAR
*ret
= NULL
;
329 if (!*len
) /* empty string (%%) is replaced by single percent */
334 if (file
->strings_section
== -1) goto not_found
;
335 strings_section
= file
->sections
[file
->strings_section
];
336 for (i
= 0, line
= strings_section
->lines
; i
< strings_section
->nb_lines
; i
++, line
++)
338 if (line
->key_field
== -1) continue;
339 if (wcsnicmp( str
, file
->fields
[line
->key_field
].text
, *len
)) continue;
340 if (!file
->fields
[line
->key_field
].text
[*len
]) break;
342 if (i
== strings_section
->nb_lines
|| !line
->nb_fields
) goto not_found
;
343 field
= &file
->fields
[line
->first_field
];
344 *len
= lstrlenW( field
->text
);
347 not_found
: /* check for integer id */
348 if ((dirid_str
= HeapAlloc( GetProcessHeap(), 0, (*len
+1) * sizeof(WCHAR
) )))
350 memcpy( dirid_str
, str
, *len
* sizeof(WCHAR
) );
352 dirid
= wcstol( dirid_str
, &end
, 10 );
353 if (!*end
) ret
= get_dirid_subst( file
, dirid
, len
);
354 if (no_trailing_slash
&& ret
&& *len
&& ret
[*len
- 1] == '\\') *len
-= 1;
355 HeapFree( GetProcessHeap(), 0, dirid_str
);
362 /* do string substitutions on the specified text */
363 /* the buffer is assumed to be large enough */
364 /* returns necessary length not including terminating null */
365 static unsigned int PARSER_string_substW( const struct inf_file
*file
, const WCHAR
*text
,
366 WCHAR
*buffer
, unsigned int size
)
368 const WCHAR
*start
, *subst
, *p
;
369 unsigned int len
, total
= 0;
372 if (!buffer
) size
= MAX_STRING_LEN
+ 1;
373 for (p
= start
= text
; *p
; p
++)
375 if (*p
!= '%') continue;
377 if (inside
) /* start of a %xx% string */
380 if (len
> size
- 1) len
= size
- 1;
381 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
386 else /* end of the %xx% string, find substitution */
389 subst
= get_string_subst( file
, start
+ 1, &len
, p
[1] == '\\' );
395 if (len
> size
- 1) len
= size
- 1;
396 if (buffer
) memcpy( buffer
+ total
, subst
, len
* sizeof(WCHAR
) );
403 if (start
!= p
) /* unfinished string, copy it */
406 if (len
> size
- 1) len
= size
- 1;
407 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
410 if (buffer
&& size
) buffer
[total
] = 0;
415 /* do string substitutions on the specified text */
416 /* the buffer is assumed to be large enough */
417 /* returns necessary length not including terminating null */
418 static unsigned int PARSER_string_substA( const struct inf_file
*file
, const WCHAR
*text
,
419 char *buffer
, unsigned int size
)
421 WCHAR buffW
[MAX_STRING_LEN
+1];
424 unsigned int len
= PARSER_string_substW( file
, text
, buffW
, ARRAY_SIZE( buffW
));
425 if (!buffer
) RtlUnicodeToMultiByteSize( &ret
, buffW
, len
* sizeof(WCHAR
) );
428 RtlUnicodeToMultiByteN( buffer
, size
-1, &ret
, buffW
, len
* sizeof(WCHAR
) );
435 /* push some string data into the strings buffer */
436 static WCHAR
*push_string( struct inf_file
*file
, const WCHAR
*string
)
438 WCHAR
*ret
= file
->string_pos
;
439 lstrcpyW( ret
, string
);
440 file
->string_pos
+= lstrlenW( ret
) + 1;
445 /* push the current state on the parser stack */
446 static inline void push_state( struct parser
*parser
, enum parser_state state
)
448 assert( parser
->stack_pos
< ARRAY_SIZE( parser
->stack
));
449 parser
->stack
[parser
->stack_pos
++] = state
;
453 /* pop the current state */
454 static inline void pop_state( struct parser
*parser
)
456 assert( parser
->stack_pos
);
457 parser
->state
= parser
->stack
[--parser
->stack_pos
];
461 /* set the parser state and return the previous one */
462 static inline enum parser_state
set_state( struct parser
*parser
, enum parser_state state
)
464 enum parser_state ret
= parser
->state
;
465 parser
->state
= state
;
470 /* check if the pointer points to an end of file */
471 static inline BOOL
is_eof( const struct parser
*parser
, const WCHAR
*ptr
)
473 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
);
477 /* check if the pointer points to an end of line */
478 static inline BOOL
is_eol( const struct parser
*parser
, const WCHAR
*ptr
)
480 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
|| *ptr
== '\n');
484 /* push data from current token start up to pos into the current token */
485 static int push_token( struct parser
*parser
, const WCHAR
*pos
)
487 int len
= pos
- parser
->start
;
488 const WCHAR
*src
= parser
->start
;
489 WCHAR
*dst
= parser
->token
+ parser
->token_len
;
491 if (len
> MAX_FIELD_LEN
- parser
->token_len
) len
= MAX_FIELD_LEN
- parser
->token_len
;
493 parser
->token_len
+= len
;
494 for ( ; len
> 0; len
--, dst
++, src
++) *dst
= *src
? *src
: ' ';
501 /* add a section with the current token as name */
502 static int add_section_from_token( struct parser
*parser
)
506 if (parser
->token_len
> MAX_SECTION_NAME_LEN
)
508 parser
->error
= ERROR_SECTION_NAME_TOO_LONG
;
511 if ((section_index
= find_section( parser
->file
, parser
->token
)) == -1)
513 /* need to create a new one */
514 const WCHAR
*name
= push_string( parser
->file
, parser
->token
);
515 if ((section_index
= add_section( parser
->file
, name
)) == -1)
517 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
521 parser
->token_len
= 0;
522 parser
->cur_section
= section_index
;
523 return section_index
;
527 /* add a field containing the current token to the current line */
528 static struct field
*add_field_from_token( struct parser
*parser
, BOOL is_key
)
533 if (!parser
->line
) /* need to start a new line */
535 if (parser
->cur_section
== -1) /* got a line before the first section */
537 parser
->error
= ERROR_EXPECTED_SECTION_NAME
;
540 if (!(parser
->line
= add_line( parser
->file
, parser
->cur_section
))) goto error
;
542 else assert(!is_key
);
544 text
= push_string( parser
->file
, parser
->token
);
545 if ((field
= add_field( parser
->file
, text
)))
547 if (!is_key
) parser
->line
->nb_fields
++;
550 /* replace first field by key field */
551 parser
->line
->key_field
= parser
->line
->first_field
;
552 parser
->line
->first_field
++;
554 parser
->token_len
= 0;
558 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
563 /* close the current line and prepare for parsing a new one */
564 static void close_current_line( struct parser
*parser
)
566 struct line
*cur_line
= parser
->line
;
570 /* if line has a single field and no key, the field is the key too */
571 if (cur_line
->nb_fields
== 1 && cur_line
->key_field
== -1)
572 cur_line
->key_field
= cur_line
->first_field
;
578 /* handler for parser LINE_START state */
579 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
)
583 for (p
= pos
; !is_eof( parser
, p
); p
++)
589 close_current_line( parser
);
592 push_state( parser
, LINE_START
);
593 set_state( parser
, COMMENT
);
596 parser
->start
= p
+ 1;
597 set_state( parser
, SECTION_NAME
);
600 if (iswspace(*p
)) break;
601 if (parser
->cur_section
!= -1)
604 set_state( parser
, KEY_NAME
);
607 if (!parser
->broken_line
)
608 parser
->broken_line
= parser
->line_pos
;
612 close_current_line( parser
);
617 /* handler for parser SECTION_NAME state */
618 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
)
622 for (p
= pos
; !is_eol( parser
, p
); p
++)
626 push_token( parser
, p
);
627 if (add_section_from_token( parser
) == -1) return NULL
;
628 push_state( parser
, LINE_START
);
629 set_state( parser
, COMMENT
); /* ignore everything else on the line */
633 parser
->error
= ERROR_BAD_SECTION_NAME_LINE
; /* unfinished section name */
638 /* handler for parser KEY_NAME state */
639 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
)
641 const WCHAR
*p
, *token_end
= parser
->start
;
643 for (p
= pos
; !is_eol( parser
, p
); p
++)
645 if (*p
== ',') break;
650 push_token( parser
, token_end
);
651 if (!add_field_from_token( parser
, TRUE
)) return NULL
;
652 parser
->start
= p
+ 1;
653 push_state( parser
, VALUE_NAME
);
654 set_state( parser
, LEADING_SPACES
);
657 push_token( parser
, token_end
);
658 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
659 push_state( parser
, LINE_START
);
660 set_state( parser
, COMMENT
);
663 push_token( parser
, p
);
664 parser
->start
= p
+ 1;
665 push_state( parser
, KEY_NAME
);
666 set_state( parser
, QUOTES
);
669 push_token( parser
, token_end
);
671 push_state( parser
, KEY_NAME
);
672 set_state( parser
, EOL_BACKSLASH
);
675 if (!iswspace(*p
)) token_end
= p
+ 1;
678 push_token( parser
, p
);
679 push_state( parser
, KEY_NAME
);
680 set_state( parser
, TRAILING_SPACES
);
686 push_token( parser
, token_end
);
687 set_state( parser
, VALUE_NAME
);
692 /* handler for parser VALUE_NAME state */
693 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
)
695 const WCHAR
*p
, *token_end
= parser
->start
;
697 for (p
= pos
; !is_eol( parser
, p
); p
++)
702 push_token( parser
, token_end
);
703 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
704 push_state( parser
, LINE_START
);
705 set_state( parser
, COMMENT
);
708 push_token( parser
, token_end
);
709 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
710 parser
->start
= p
+ 1;
711 push_state( parser
, VALUE_NAME
);
712 set_state( parser
, LEADING_SPACES
);
715 push_token( parser
, p
);
716 parser
->start
= p
+ 1;
717 push_state( parser
, VALUE_NAME
);
718 set_state( parser
, QUOTES
);
721 push_token( parser
, token_end
);
723 push_state( parser
, VALUE_NAME
);
724 set_state( parser
, EOL_BACKSLASH
);
727 if (*p
&& !iswspace(*p
)) token_end
= p
+ 1;
730 push_token( parser
, p
);
731 push_state( parser
, VALUE_NAME
);
732 set_state( parser
, TRAILING_SPACES
);
738 push_token( parser
, token_end
);
739 if (!add_field_from_token( parser
, FALSE
)) return NULL
;
740 set_state( parser
, LINE_START
);
745 /* handler for parser EOL_BACKSLASH state */
746 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
)
750 for (p
= pos
; !is_eof( parser
, p
); p
++)
756 parser
->start
= p
+ 1;
757 set_state( parser
, LEADING_SPACES
);
762 push_state( parser
, EOL_BACKSLASH
);
763 set_state( parser
, COMMENT
);
766 if (iswspace(*p
)) continue;
767 push_token( parser
, p
);
778 /* handler for parser QUOTES state */
779 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
)
783 for (p
= pos
; !is_eol( parser
, p
); p
++)
787 if (p
+1 < parser
->end
&& p
[1] == '"') /* double quotes */
789 push_token( parser
, p
+ 1 );
790 parser
->start
= p
+ 2;
793 else /* end of quotes */
795 push_token( parser
, p
);
796 parser
->start
= p
+ 1;
802 push_token( parser
, p
);
808 /* handler for parser LEADING_SPACES state */
809 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
813 for (p
= pos
; !is_eol( parser
, p
); p
++)
818 set_state( parser
, EOL_BACKSLASH
);
821 if (!iswspace(*p
)) break;
829 /* handler for parser TRAILING_SPACES state */
830 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
834 for (p
= pos
; !is_eol( parser
, p
); p
++)
838 set_state( parser
, EOL_BACKSLASH
);
841 if (*p
&& !iswspace(*p
)) break;
848 /* handler for parser COMMENT state */
849 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
)
851 const WCHAR
*p
= pos
;
853 while (!is_eol( parser
, p
)) p
++;
859 static void free_inf_file( struct inf_file
*file
)
863 for (i
= 0; i
< file
->nb_sections
; i
++) HeapFree( GetProcessHeap(), 0, file
->sections
[i
] );
864 HeapFree( GetProcessHeap(), 0, file
->filename
);
865 HeapFree( GetProcessHeap(), 0, file
->sections
);
866 HeapFree( GetProcessHeap(), 0, file
->fields
);
867 HeapFree( GetProcessHeap(), 0, file
->strings
);
868 HeapFree( GetProcessHeap(), 0, file
);
872 /* parse a complete buffer */
873 static DWORD
parse_buffer( struct inf_file
*file
, const WCHAR
*buffer
, const WCHAR
*end
,
876 static const WCHAR Strings
[] = {'S','t','r','i','n','g','s',0};
878 struct parser parser
;
879 const WCHAR
*pos
= buffer
;
881 parser
.start
= buffer
;
885 parser
.state
= LINE_START
;
886 parser
.stack_pos
= 0;
887 parser
.cur_section
= -1;
889 parser
.broken_line
= 0;
891 parser
.token_len
= 0;
893 /* parser main loop */
894 while (pos
) pos
= (parser_funcs
[parser
.state
])( &parser
, pos
);
896 /* trim excess buffer space */
897 if (file
->alloc_sections
> file
->nb_sections
)
899 file
->sections
= HeapReAlloc( GetProcessHeap(), 0, file
->sections
,
900 file
->nb_sections
* sizeof(file
->sections
[0]) );
901 file
->alloc_sections
= file
->nb_sections
;
903 if (file
->alloc_fields
> file
->nb_fields
)
905 file
->fields
= HeapReAlloc( GetProcessHeap(), 0, file
->fields
,
906 file
->nb_fields
* sizeof(file
->fields
[0]) );
907 file
->alloc_fields
= file
->nb_fields
;
909 file
->strings
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, file
->strings
,
910 (file
->string_pos
- file
->strings
) * sizeof(WCHAR
) );
914 if (error_line
) *error_line
= parser
.line_pos
;
918 /* find the [strings] section */
919 file
->strings_section
= find_section( file
, Strings
);
921 if (file
->strings_section
== -1 && parser
.broken_line
)
923 if (error_line
) *error_line
= parser
.broken_line
;
924 return ERROR_EXPECTED_SECTION_NAME
;
931 /* append a child INF file to its parent list, in a thread-safe manner */
932 static void append_inf_file( struct inf_file
*parent
, struct inf_file
*child
)
934 struct inf_file
**ppnext
= &parent
->next
;
939 struct inf_file
*next
= InterlockedCompareExchangePointer( (void **)ppnext
, child
, NULL
);
941 ppnext
= &next
->next
;
946 /***********************************************************************
951 static struct inf_file
*parse_file( HANDLE handle
, const WCHAR
*class, DWORD style
, UINT
*error_line
)
955 struct inf_file
*file
;
957 DWORD size
= GetFileSize( handle
, NULL
);
958 HANDLE mapping
= CreateFileMappingW( handle
, NULL
, PAGE_READONLY
, 0, size
, NULL
);
959 if (!mapping
) return NULL
;
960 buffer
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, size
);
962 if (!buffer
) return NULL
;
964 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
966 if (!(file
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*file
) )))
968 err
= ERROR_NOT_ENOUGH_MEMORY
;
972 /* we won't need more strings space than the size of the file,
973 * so we can preallocate it here
975 if (!(file
->strings
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
977 err
= ERROR_NOT_ENOUGH_MEMORY
;
980 file
->string_pos
= file
->strings
;
981 file
->strings_section
= -1;
983 if (!RtlIsTextUnicode( buffer
, size
, NULL
))
985 static const BYTE utf8_bom
[3] = { 0xef, 0xbb, 0xbf };
987 UINT codepage
= CP_ACP
;
990 if (size
> sizeof(utf8_bom
) && !memcmp( buffer
, utf8_bom
, sizeof(utf8_bom
) ))
993 offset
= sizeof(utf8_bom
);
996 if ((new_buff
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
998 DWORD len
= MultiByteToWideChar( codepage
, 0, (char *)buffer
+ offset
,
999 size
- offset
, new_buff
, size
);
1000 err
= parse_buffer( file
, new_buff
, new_buff
+ len
, error_line
);
1001 HeapFree( GetProcessHeap(), 0, new_buff
);
1006 WCHAR
*new_buff
= buffer
;
1007 /* UCS-16 files should start with the Unicode BOM; we should skip it */
1008 if (*new_buff
== 0xfeff)
1010 err
= parse_buffer( file
, new_buff
, (WCHAR
*)((char *)buffer
+ size
), error_line
);
1013 if (!err
) /* now check signature */
1015 int version_index
= find_section( file
, Version
);
1016 if (version_index
!= -1)
1018 struct line
*line
= find_line( file
, version_index
, Signature
);
1019 if (line
&& line
->nb_fields
> 0)
1021 struct field
*field
= file
->fields
+ line
->first_field
;
1022 if (!wcsicmp( field
->text
, Chicago
)) goto done
;
1023 if (!wcsicmp( field
->text
, WindowsNT
)) goto done
;
1024 if (!wcsicmp( field
->text
, Windows95
)) goto done
;
1027 if (error_line
) *error_line
= 0;
1028 if (style
& INF_STYLE_WIN4
) err
= ERROR_WRONG_INF_STYLE
;
1032 UnmapViewOfFile( buffer
);
1035 if (file
) free_inf_file( file
);
1036 SetLastError( err
);
1043 /***********************************************************************
1044 * PARSER_get_inf_filename
1046 * Retrieve the filename of an inf file.
1048 const WCHAR
*PARSER_get_inf_filename( HINF hinf
)
1050 struct inf_file
*file
= hinf
;
1051 return file
->filename
;
1054 /***********************************************************************
1055 * PARSER_get_dest_dir
1057 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1058 * returned buffer must be freed by caller.
1060 WCHAR
*PARSER_get_dest_dir( INFCONTEXT
*context
)
1068 if (!SetupGetIntField( context
, 1, &dirid
)) return NULL
;
1069 if (!(dir
= get_dirid_subst( context
->Inf
, dirid
, &len1
))) return NULL
;
1070 if (!SetupGetStringFieldW( context
, 2, NULL
, 0, &len2
)) len2
= 0;
1071 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, (len1
+len2
+1) * sizeof(WCHAR
) ))) return NULL
;
1072 memcpy( ret
, dir
, len1
* sizeof(WCHAR
) );
1074 if (len2
&& ptr
> ret
&& ptr
[-1] != '\\') *ptr
++ = '\\';
1075 if (!SetupGetStringFieldW( context
, 2, ptr
, len2
, NULL
)) *ptr
= 0;
1080 /***********************************************************************
1081 * SetupOpenInfFileA (SETUPAPI.@)
1083 HINF WINAPI
SetupOpenInfFileA( PCSTR name
, PCSTR
class, DWORD style
, UINT
*error
)
1085 UNICODE_STRING nameW
, classW
;
1086 HINF ret
= INVALID_HANDLE_VALUE
;
1088 classW
.Buffer
= NULL
;
1089 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW
, class ))
1091 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1094 if (RtlCreateUnicodeStringFromAsciiz( &nameW
, name
))
1096 ret
= SetupOpenInfFileW( nameW
.Buffer
, classW
.Buffer
, style
, error
);
1097 RtlFreeUnicodeString( &nameW
);
1099 RtlFreeUnicodeString( &classW
);
1104 /***********************************************************************
1105 * SetupOpenInfFileW (SETUPAPI.@)
1107 HINF WINAPI
SetupOpenInfFileW( PCWSTR name
, PCWSTR
class, DWORD style
, UINT
*error
)
1109 struct inf_file
*file
= NULL
;
1114 if (wcschr( name
, '\\' ) || wcschr( name
, '/' ))
1116 if (!(len
= GetFullPathNameW( name
, 0, NULL
, NULL
))) return INVALID_HANDLE_VALUE
;
1117 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1119 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1120 return INVALID_HANDLE_VALUE
;
1122 GetFullPathNameW( name
, len
, path
, NULL
);
1123 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1125 else /* try Windows directory */
1127 static const WCHAR Inf
[] = {'\\','i','n','f','\\',0};
1128 static const WCHAR System32
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1130 len
= GetWindowsDirectoryW( NULL
, 0 ) + lstrlenW(name
) + 12;
1131 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1133 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1134 return INVALID_HANDLE_VALUE
;
1136 GetWindowsDirectoryW( path
, len
);
1137 p
= path
+ lstrlenW(path
);
1139 lstrcatW( p
, name
);
1140 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1141 if (handle
== INVALID_HANDLE_VALUE
)
1143 lstrcpyW( p
, System32
);
1144 lstrcatW( p
, name
);
1145 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1149 if (handle
!= INVALID_HANDLE_VALUE
)
1151 file
= parse_file( handle
, class, style
, error
);
1152 CloseHandle( handle
);
1156 HeapFree( GetProcessHeap(), 0, path
);
1157 return INVALID_HANDLE_VALUE
;
1159 TRACE( "%s -> %p\n", debugstr_w(path
), file
);
1160 file
->filename
= path
;
1166 /***********************************************************************
1167 * SetupOpenAppendInfFileA (SETUPAPI.@)
1169 BOOL WINAPI
SetupOpenAppendInfFileA( PCSTR name
, HINF parent_hinf
, UINT
*error
)
1173 if (!name
) return SetupOpenAppendInfFileW( NULL
, parent_hinf
, error
);
1174 child_hinf
= SetupOpenInfFileA( name
, NULL
, INF_STYLE_WIN4
, error
);
1175 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1176 append_inf_file( parent_hinf
, child_hinf
);
1177 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_a(name
), child_hinf
);
1182 /***********************************************************************
1183 * SetupOpenAppendInfFileW (SETUPAPI.@)
1185 BOOL WINAPI
SetupOpenAppendInfFileW( PCWSTR name
, HINF parent_hinf
, UINT
*error
)
1192 WCHAR filename
[MAX_PATH
];
1195 if (!SetupFindFirstLineW( parent_hinf
, Version
, LayoutFile
, &context
)) return FALSE
;
1196 while (SetupGetStringFieldW( &context
, idx
++, filename
, ARRAY_SIZE( filename
), NULL
))
1198 child_hinf
= SetupOpenInfFileW( filename
, 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_w(filename
), child_hinf
);
1205 child_hinf
= SetupOpenInfFileW( name
, NULL
, INF_STYLE_WIN4
, error
);
1206 if (child_hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1207 append_inf_file( parent_hinf
, child_hinf
);
1208 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(name
), child_hinf
);
1213 /***********************************************************************
1214 * SetupOpenMasterInf (SETUPAPI.@)
1216 HINF WINAPI
SetupOpenMasterInf( VOID
)
1218 static const WCHAR Layout
[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1219 WCHAR Buffer
[MAX_PATH
];
1221 GetWindowsDirectoryW( Buffer
, MAX_PATH
);
1222 lstrcatW( Buffer
, Layout
);
1223 return SetupOpenInfFileW( Buffer
, NULL
, INF_STYLE_WIN4
, NULL
);
1228 /***********************************************************************
1229 * SetupCloseInfFile (SETUPAPI.@)
1231 void WINAPI
SetupCloseInfFile( HINF hinf
)
1233 struct inf_file
*file
= hinf
;
1235 if (!hinf
|| (hinf
== INVALID_HANDLE_VALUE
)) return;
1237 free_inf_file( file
);
1241 /***********************************************************************
1242 * SetupEnumInfSectionsA (SETUPAPI.@)
1244 BOOL WINAPI
SetupEnumInfSectionsA( HINF hinf
, UINT index
, PSTR buffer
, DWORD size
, DWORD
*need
)
1246 struct inf_file
*file
= hinf
;
1248 for (file
= hinf
; file
; file
= file
->next
)
1250 if (index
< file
->nb_sections
)
1252 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, file
->sections
[index
]->name
, -1,
1253 NULL
, 0, NULL
, NULL
);
1254 if (need
) *need
= len
;
1257 if (!size
) return TRUE
;
1258 SetLastError( ERROR_INVALID_USER_BUFFER
);
1263 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1266 WideCharToMultiByte( CP_ACP
, 0, file
->sections
[index
]->name
, -1, buffer
, size
, NULL
, NULL
);
1269 index
-= file
->nb_sections
;
1271 SetLastError( ERROR_NO_MORE_ITEMS
);
1276 /***********************************************************************
1277 * SetupEnumInfSectionsW (SETUPAPI.@)
1279 BOOL WINAPI
SetupEnumInfSectionsW( HINF hinf
, UINT index
, PWSTR buffer
, DWORD size
, DWORD
*need
)
1281 struct inf_file
*file
= hinf
;
1283 for (file
= hinf
; file
; file
= file
->next
)
1285 if (index
< file
->nb_sections
)
1287 DWORD len
= lstrlenW( file
->sections
[index
]->name
) + 1;
1288 if (need
) *need
= len
;
1291 if (!size
) return TRUE
;
1292 SetLastError( ERROR_INVALID_USER_BUFFER
);
1297 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1300 memcpy( buffer
, file
->sections
[index
]->name
, len
* sizeof(WCHAR
) );
1303 index
-= file
->nb_sections
;
1305 SetLastError( ERROR_NO_MORE_ITEMS
);
1310 /***********************************************************************
1311 * SetupGetLineCountA (SETUPAPI.@)
1313 LONG WINAPI
SetupGetLineCountA( HINF hinf
, PCSTR name
)
1315 UNICODE_STRING sectionW
;
1318 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, name
))
1319 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1322 ret
= SetupGetLineCountW( hinf
, sectionW
.Buffer
);
1323 RtlFreeUnicodeString( §ionW
);
1329 /***********************************************************************
1330 * SetupGetLineCountW (SETUPAPI.@)
1332 LONG WINAPI
SetupGetLineCountW( HINF hinf
, PCWSTR section
)
1334 struct inf_file
*file
= hinf
;
1338 for (file
= hinf
; file
; file
= file
->next
)
1340 if ((section_index
= find_section( file
, section
)) == -1) continue;
1341 if (ret
== -1) ret
= 0;
1342 ret
+= file
->sections
[section_index
]->nb_lines
;
1344 TRACE( "(%p,%s) returning %ld\n", hinf
, debugstr_w(section
), ret
);
1345 SetLastError( (ret
== -1) ? ERROR_SECTION_NOT_FOUND
: 0 );
1350 /***********************************************************************
1351 * SetupGetLineByIndexA (SETUPAPI.@)
1353 BOOL WINAPI
SetupGetLineByIndexA( HINF hinf
, PCSTR section
, DWORD index
, INFCONTEXT
*context
)
1355 UNICODE_STRING sectionW
;
1358 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1359 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1362 ret
= SetupGetLineByIndexW( hinf
, sectionW
.Buffer
, index
, context
);
1363 RtlFreeUnicodeString( §ionW
);
1369 /***********************************************************************
1370 * SetupGetLineByIndexW (SETUPAPI.@)
1372 BOOL WINAPI
SetupGetLineByIndexW( HINF hinf
, PCWSTR section
, DWORD index
, INFCONTEXT
*context
)
1374 struct inf_file
*file
= hinf
;
1377 for (file
= hinf
; file
; file
= file
->next
)
1379 if ((section_index
= find_section( file
, section
)) == -1) continue;
1380 if (index
< file
->sections
[section_index
]->nb_lines
)
1382 context
->Inf
= hinf
;
1383 context
->CurrentInf
= file
;
1384 context
->Section
= section_index
;
1385 context
->Line
= index
;
1387 TRACE( "(%p,%s): returning %d/%ld\n",
1388 hinf
, debugstr_w(section
), section_index
, index
);
1391 index
-= file
->sections
[section_index
]->nb_lines
;
1393 TRACE( "(%p,%s) not found\n", hinf
, debugstr_w(section
) );
1394 SetLastError( ERROR_LINE_NOT_FOUND
);
1399 /***********************************************************************
1400 * SetupFindFirstLineA (SETUPAPI.@)
1402 BOOL WINAPI
SetupFindFirstLineA( HINF hinf
, PCSTR section
, PCSTR key
, INFCONTEXT
*context
)
1404 UNICODE_STRING sectionW
, keyW
;
1407 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1409 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1413 if (!key
) ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, NULL
, context
);
1416 if (RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1418 ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, keyW
.Buffer
, context
);
1419 RtlFreeUnicodeString( &keyW
);
1421 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1423 RtlFreeUnicodeString( §ionW
);
1428 /***********************************************************************
1429 * SetupFindFirstLineW (SETUPAPI.@)
1431 BOOL WINAPI
SetupFindFirstLineW( HINF hinf
, PCWSTR section
, PCWSTR key
, INFCONTEXT
*context
)
1433 struct inf_file
*file
;
1436 for (file
= hinf
; file
; file
= file
->next
)
1438 if ((section_index
= find_section( file
, section
)) == -1) continue;
1443 ctx
.CurrentInf
= file
;
1444 ctx
.Section
= section_index
;
1446 return SetupFindNextMatchLineW( &ctx
, key
, context
);
1448 if (file
->sections
[section_index
]->nb_lines
)
1450 context
->Inf
= hinf
;
1451 context
->CurrentInf
= file
;
1452 context
->Section
= section_index
;
1455 TRACE( "(%p,%s,%s): returning %d/0\n",
1456 hinf
, debugstr_w(section
), debugstr_w(key
), section_index
);
1460 TRACE( "(%p,%s,%s): not found\n", hinf
, debugstr_w(section
), debugstr_w(key
) );
1461 SetLastError( ERROR_LINE_NOT_FOUND
);
1466 /***********************************************************************
1467 * SetupFindNextLine (SETUPAPI.@)
1469 BOOL WINAPI
SetupFindNextLine( PINFCONTEXT context_in
, PINFCONTEXT context_out
)
1471 struct inf_file
*file
= context_in
->CurrentInf
;
1472 struct section
*section
;
1474 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1476 section
= file
->sections
[context_in
->Section
];
1477 if (context_in
->Line
+1 < section
->nb_lines
)
1479 if (context_out
!= context_in
) *context_out
= *context_in
;
1480 context_out
->Line
++;
1485 /* now search the appended files */
1487 for (file
= file
->next
; file
; file
= file
->next
)
1489 int section_index
= find_section( file
, section
->name
);
1490 if (section_index
== -1) continue;
1491 if (file
->sections
[section_index
]->nb_lines
)
1493 context_out
->Inf
= context_in
->Inf
;
1494 context_out
->CurrentInf
= file
;
1495 context_out
->Section
= section_index
;
1496 context_out
->Line
= 0;
1502 SetLastError( ERROR_LINE_NOT_FOUND
);
1507 /***********************************************************************
1508 * SetupFindNextMatchLineA (SETUPAPI.@)
1510 BOOL WINAPI
SetupFindNextMatchLineA( PINFCONTEXT context_in
, PCSTR key
,
1511 PINFCONTEXT context_out
)
1513 UNICODE_STRING keyW
;
1516 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1518 if (!RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1519 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1522 ret
= SetupFindNextMatchLineW( context_in
, keyW
.Buffer
, context_out
);
1523 RtlFreeUnicodeString( &keyW
);
1529 /***********************************************************************
1530 * SetupFindNextMatchLineW (SETUPAPI.@)
1532 BOOL WINAPI
SetupFindNextMatchLineW( PINFCONTEXT context_in
, PCWSTR key
,
1533 PINFCONTEXT context_out
)
1535 struct inf_file
*file
= context_in
->CurrentInf
;
1536 WCHAR buffer
[MAX_STRING_LEN
+ 1];
1537 struct section
*section
;
1541 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1543 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1545 section
= file
->sections
[context_in
->Section
];
1547 for (i
= context_in
->Line
+1, line
= §ion
->lines
[i
]; i
< section
->nb_lines
; i
++, line
++)
1549 if (line
->key_field
== -1) continue;
1550 PARSER_string_substW( file
, file
->fields
[line
->key_field
].text
, buffer
, ARRAY_SIZE(buffer
) );
1551 if (!wcsicmp( key
, buffer
))
1553 if (context_out
!= context_in
) *context_out
= *context_in
;
1554 context_out
->Line
= i
;
1556 TRACE( "(%p,%s,%s): returning %d\n",
1557 file
, debugstr_w(section
->name
), debugstr_w(key
), i
);
1562 /* now search the appended files */
1564 for (file
= file
->next
; file
; file
= file
->next
)
1566 int section_index
= find_section( file
, section
->name
);
1567 if (section_index
== -1) continue;
1568 section
= file
->sections
[section_index
];
1569 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
1571 if (line
->key_field
== -1) continue;
1572 if (!wcsicmp( key
, file
->fields
[line
->key_field
].text
))
1574 context_out
->Inf
= context_in
->Inf
;
1575 context_out
->CurrentInf
= file
;
1576 context_out
->Section
= section_index
;
1577 context_out
->Line
= i
;
1579 TRACE( "(%p,%s,%s): returning %d/%d\n",
1580 file
, debugstr_w(section
->name
), debugstr_w(key
), section_index
, i
);
1585 TRACE( "(%p,%s,%s): not found\n",
1586 context_in
->CurrentInf
, debugstr_w(section
->name
), debugstr_w(key
) );
1588 SetLastError( ERROR_LINE_NOT_FOUND
);
1593 /***********************************************************************
1594 * SetupGetLineTextW (SETUPAPI.@)
1596 BOOL WINAPI
SetupGetLineTextW( PINFCONTEXT context
, HINF hinf
, PCWSTR section_name
,
1597 PCWSTR key_name
, PWSTR buffer
, DWORD size
, PDWORD required
)
1599 struct inf_file
*file
;
1601 struct field
*field
;
1607 INFCONTEXT new_context
;
1608 if (!SetupFindFirstLineW( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1609 file
= new_context
.CurrentInf
;
1610 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1614 file
= context
->CurrentInf
;
1615 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1617 SetLastError( ERROR_LINE_NOT_FOUND
);
1622 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1623 total
+= PARSER_string_substW( file
, field
->text
, NULL
, 0 ) + 1;
1625 if (required
) *required
= total
;
1630 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1633 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1635 unsigned int len
= PARSER_string_substW( file
, field
->text
, buffer
, size
);
1636 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1644 /***********************************************************************
1645 * SetupGetLineTextA (SETUPAPI.@)
1647 BOOL WINAPI
SetupGetLineTextA( PINFCONTEXT context
, HINF hinf
, PCSTR section_name
,
1648 PCSTR key_name
, PSTR buffer
, DWORD size
, PDWORD required
)
1650 struct inf_file
*file
;
1652 struct field
*field
;
1658 INFCONTEXT new_context
;
1659 if (!SetupFindFirstLineA( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1660 file
= new_context
.CurrentInf
;
1661 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1665 file
= context
->CurrentInf
;
1666 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1668 SetLastError( ERROR_LINE_NOT_FOUND
);
1673 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1674 total
+= PARSER_string_substA( file
, field
->text
, NULL
, 0 ) + 1;
1676 if (required
) *required
= total
;
1681 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1684 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1686 unsigned int len
= PARSER_string_substA( file
, field
->text
, buffer
, size
);
1687 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1695 /***********************************************************************
1696 * SetupGetFieldCount (SETUPAPI.@)
1698 DWORD WINAPI
SetupGetFieldCount( PINFCONTEXT context
)
1700 struct inf_file
*file
= context
->CurrentInf
;
1701 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1703 if (!line
) return 0;
1704 return line
->nb_fields
;
1708 /***********************************************************************
1709 * SetupGetStringFieldA (SETUPAPI.@)
1711 BOOL WINAPI
SetupGetStringFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1712 DWORD size
, PDWORD required
)
1714 struct inf_file
*file
= context
->CurrentInf
;
1715 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1719 if (!field
) return FALSE
;
1720 len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 );
1721 if (required
) *required
= len
+ 1;
1726 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1729 PARSER_string_substA( file
, field
->text
, buffer
, size
);
1731 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1732 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1733 index
, debugstr_a(buffer
) );
1739 /***********************************************************************
1740 * SetupGetStringFieldW (SETUPAPI.@)
1742 BOOL WINAPI
SetupGetStringFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1743 DWORD size
, PDWORD required
)
1745 struct inf_file
*file
= context
->CurrentInf
;
1746 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1750 if (!field
) return FALSE
;
1751 len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 );
1752 if (required
) *required
= len
+ 1;
1757 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1760 PARSER_string_substW( file
, field
->text
, buffer
, size
);
1762 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1763 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1764 index
, debugstr_w(buffer
) );
1770 /***********************************************************************
1771 * SetupGetIntField (SETUPAPI.@)
1773 BOOL WINAPI
SetupGetIntField( PINFCONTEXT context
, DWORD index
, PINT result
)
1776 char *end
, *buffer
= localbuff
;
1781 if (!(ret
= SetupGetStringFieldA( context
, index
, localbuff
, sizeof(localbuff
), &required
)))
1783 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
1784 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
))) return FALSE
;
1785 if (!(ret
= SetupGetStringFieldA( context
, index
, buffer
, required
, NULL
))) goto done
;
1787 /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */
1788 if (!*buffer
) *result
= 0;
1791 res
= strtol( buffer
, &end
, 0 );
1792 if (end
!= buffer
&& !*end
) *result
= res
;
1795 SetLastError( ERROR_INVALID_DATA
);
1801 if (buffer
!= localbuff
) HeapFree( GetProcessHeap(), 0, buffer
);
1806 static int xdigit_to_int(WCHAR c
)
1808 if ('0' <= c
&& c
<= '9') return c
- '0';
1809 if ('a' <= c
&& c
<= 'f') return c
- 'a' + 10;
1810 if ('A' <= c
&& c
<= 'F') return c
- 'A' + 10;
1815 /***********************************************************************
1816 * SetupGetBinaryField (SETUPAPI.@)
1818 BOOL WINAPI
SetupGetBinaryField( PINFCONTEXT context
, DWORD index
, BYTE
*buffer
,
1819 DWORD size
, LPDWORD required
)
1821 struct inf_file
*file
= context
->CurrentInf
;
1822 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1823 struct field
*field
;
1828 SetLastError( ERROR_LINE_NOT_FOUND
);
1831 if (!index
|| index
> line
->nb_fields
)
1833 SetLastError( ERROR_INVALID_PARAMETER
);
1836 index
--; /* fields start at 0 */
1837 if (required
) *required
= line
->nb_fields
- index
;
1838 if (!buffer
) return TRUE
;
1839 if (size
< line
->nb_fields
- index
)
1841 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1844 field
= &file
->fields
[line
->first_field
+ index
];
1845 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1850 for (p
= field
->text
; *p
&& (d
= xdigit_to_int(*p
)) != -1; p
++)
1852 if ((value
<<= 4) > 255)
1854 SetLastError( ERROR_INVALID_DATA
);
1859 buffer
[i
- index
] = value
;
1861 TRACE( "%p/%p/%d/%d index %ld\n",
1862 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
, index
);
1867 /***********************************************************************
1868 * SetupGetMultiSzFieldA (SETUPAPI.@)
1870 BOOL WINAPI
SetupGetMultiSzFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1871 DWORD size
, LPDWORD required
)
1873 struct inf_file
*file
= context
->CurrentInf
;
1874 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1875 struct field
*field
;
1882 SetLastError( ERROR_LINE_NOT_FOUND
);
1885 if (!index
|| index
> line
->nb_fields
)
1887 SetLastError( ERROR_INVALID_PARAMETER
);
1890 index
--; /* fields start at 0 */
1891 field
= &file
->fields
[line
->first_field
+ index
];
1892 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1894 if (!(len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 ))) break;
1898 if (required
) *required
= total
;
1899 if (!buffer
) return TRUE
;
1902 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1905 field
= &file
->fields
[line
->first_field
+ index
];
1906 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1908 if (!(len
= PARSER_string_substA( file
, field
->text
, buffer
, size
))) break;
1911 *buffer
= 0; /* add final null */
1916 /***********************************************************************
1917 * SetupGetMultiSzFieldW (SETUPAPI.@)
1919 BOOL WINAPI
SetupGetMultiSzFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1920 DWORD size
, LPDWORD required
)
1922 struct inf_file
*file
= context
->CurrentInf
;
1923 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1924 struct field
*field
;
1931 SetLastError( ERROR_LINE_NOT_FOUND
);
1934 if (!index
|| index
> line
->nb_fields
)
1936 SetLastError( ERROR_INVALID_PARAMETER
);
1939 index
--; /* fields start at 0 */
1940 field
= &file
->fields
[line
->first_field
+ index
];
1941 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1943 if (!(len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 ))) break;
1947 if (required
) *required
= total
;
1948 if (!buffer
) return TRUE
;
1951 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1954 field
= &file
->fields
[line
->first_field
+ index
];
1955 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1957 if (!(len
= PARSER_string_substW( file
, field
->text
, buffer
, size
))) break;
1960 *buffer
= 0; /* add final null */
1964 /***********************************************************************
1965 * pSetupGetField (SETUPAPI.@)
1967 LPCWSTR WINAPI
pSetupGetField( PINFCONTEXT context
, DWORD index
)
1969 struct inf_file
*file
= context
->CurrentInf
;
1970 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1974 SetLastError( ERROR_INVALID_PARAMETER
);