4 * Copyright 2002 Alexandre Julliard for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
39 #include "setupapi_private.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
46 #define CONTROL_Z '\x1a'
47 #define MAX_SECTION_NAME_LEN 255
48 #define MAX_FIELD_LEN 511 /* larger fields get silently truncated */
49 /* actual string limit is MAX_INF_STRING_LENGTH+1 (plus terminating null) under Windows */
50 #define MAX_STRING_LEN (MAX_INF_STRING_LENGTH+1)
52 /* inf file structure definitions */
56 const WCHAR
*text
; /* field text */
61 int first_field
; /* index of first field in field array */
62 int nb_fields
; /* number of fields in line */
63 int key_field
; /* index of field for key or -1 if no key */
68 const WCHAR
*name
; /* section name */
69 unsigned int nb_lines
; /* number of used lines */
70 unsigned int alloc_lines
; /* total number of allocated lines in array below */
71 struct line lines
[16]; /* lines information (grown dynamically, 16 is initial size) */
76 struct inf_file
*next
; /* next appended file */
77 WCHAR
*strings
; /* buffer for string data (section names and field values) */
78 WCHAR
*string_pos
; /* position of next available string in buffer */
79 unsigned int nb_sections
; /* number of used sections */
80 unsigned int alloc_sections
; /* total number of allocated section pointers */
81 struct section
**sections
; /* section pointers array */
82 unsigned int nb_fields
;
83 unsigned int alloc_fields
;
85 int strings_section
; /* index of [Strings] section or -1 if none */
86 WCHAR
*src_root
; /* source root directory */
89 /* parser definitions */
93 LINE_START
, /* at beginning of a line */
94 SECTION_NAME
, /* parsing a section name */
95 KEY_NAME
, /* parsing a key name */
96 VALUE_NAME
, /* parsing a value name */
97 EOL_BACKSLASH
, /* backslash at end of line */
98 QUOTES
, /* inside quotes */
99 LEADING_SPACES
, /* leading spaces */
100 TRAILING_SPACES
, /* trailing spaces */
101 COMMENT
, /* inside a comment */
107 const WCHAR
*start
; /* start position of item being parsed */
108 const WCHAR
*end
; /* end of buffer */
109 struct inf_file
*file
; /* file being built */
110 enum parser_state state
; /* current parser state */
111 enum parser_state stack
[4]; /* state stack */
112 int stack_pos
; /* current pos in stack */
114 int cur_section
; /* index of section being parsed*/
115 struct line
*line
; /* current line */
116 unsigned int line_pos
; /* current line position in file */
117 unsigned int error
; /* error code */
118 unsigned int token_len
; /* current token len */
119 WCHAR token
[MAX_FIELD_LEN
+1]; /* current token */
122 typedef const WCHAR
* (*parser_state_func
)( struct parser
*parser
, const WCHAR
*pos
);
124 /* parser state machine functions */
125 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
);
126 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
);
127 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
);
128 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
);
129 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
);
130 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
);
131 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
132 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
);
133 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
);
135 static const parser_state_func parser_funcs
[NB_PARSER_STATES
] =
137 line_start_state
, /* LINE_START */
138 section_name_state
, /* SECTION_NAME */
139 key_name_state
, /* KEY_NAME */
140 value_name_state
, /* VALUE_NAME */
141 eol_backslash_state
, /* EOL_BACKSLASH */
142 quotes_state
, /* QUOTES */
143 leading_spaces_state
, /* LEADING_SPACES */
144 trailing_spaces_state
, /* TRAILING_SPACES */
145 comment_state
/* COMMENT */
149 /* Unicode string constants */
150 static const WCHAR Version
[] = {'V','e','r','s','i','o','n',0};
151 static const WCHAR Signature
[] = {'S','i','g','n','a','t','u','r','e',0};
152 static const WCHAR Chicago
[] = {'$','C','h','i','c','a','g','o','$',0};
153 static const WCHAR WindowsNT
[] = {'$','W','i','n','d','o','w','s',' ','N','T','$',0};
154 static const WCHAR Windows95
[] = {'$','W','i','n','d','o','w','s',' ','9','5','$',0};
155 static const WCHAR LayoutFile
[] = {'L','a','y','o','u','t','F','i','l','e',0};
157 /* extend an array, allocating more memory if necessary */
158 static void *grow_array( void *array
, unsigned int *count
, size_t elem
)
161 unsigned int new_count
= *count
+ *count
/ 2;
162 if (new_count
< 32) new_count
= 32;
165 new_array
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, array
, new_count
* elem
);
167 new_array
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* elem
);
173 HeapFree( GetProcessHeap(), 0, array
);
178 /* find a section by name */
179 static int find_section( struct inf_file
*file
, const WCHAR
*name
)
183 for (i
= 0; i
< file
->nb_sections
; i
++)
184 if (!strcmpiW( name
, file
->sections
[i
]->name
)) return i
;
189 /* find a line by name */
190 static struct line
*find_line( struct inf_file
*file
, int section_index
, const WCHAR
*name
)
192 struct section
*section
;
196 if (section_index
< 0 || section_index
>= file
->nb_sections
) return NULL
;
197 section
= file
->sections
[section_index
];
198 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
200 if (line
->key_field
== -1) continue;
201 if (!strcmpiW( name
, file
->fields
[line
->key_field
].text
)) return line
;
207 /* add a section to the file and return the section index */
208 static int add_section( struct inf_file
*file
, const WCHAR
*name
)
210 struct section
*section
;
212 if (file
->nb_sections
>= file
->alloc_sections
)
214 if (!(file
->sections
= grow_array( file
->sections
, &file
->alloc_sections
,
215 sizeof(file
->sections
[0]) ))) return -1;
217 if (!(section
= HeapAlloc( GetProcessHeap(), 0, sizeof(*section
) ))) return -1;
218 section
->name
= name
;
219 section
->nb_lines
= 0;
220 section
->alloc_lines
= sizeof(section
->lines
)/sizeof(section
->lines
[0]);
221 file
->sections
[file
->nb_sections
] = section
;
222 return file
->nb_sections
++;
226 /* add a line to a given section */
227 static struct line
*add_line( struct inf_file
*file
, int section_index
)
229 struct section
*section
;
232 assert( section_index
>= 0 && section_index
< file
->nb_sections
);
234 section
= file
->sections
[section_index
];
235 if (section
->nb_lines
== section
->alloc_lines
) /* need to grow the section */
237 int size
= sizeof(*section
) - sizeof(section
->lines
) + 2*section
->alloc_lines
*sizeof(*line
);
238 if (!(section
= HeapReAlloc( GetProcessHeap(), 0, section
, size
))) return NULL
;
239 section
->alloc_lines
*= 2;
240 file
->sections
[section_index
] = section
;
242 line
= §ion
->lines
[section
->nb_lines
++];
243 line
->first_field
= file
->nb_fields
;
245 line
->key_field
= -1;
250 /* retrieve a given line from section/line index */
251 inline static struct line
*get_line( struct inf_file
*file
, unsigned int section_index
,
252 unsigned int line_index
)
254 struct section
*section
;
256 if (section_index
>= file
->nb_sections
) return NULL
;
257 section
= file
->sections
[section_index
];
258 if (line_index
>= section
->nb_lines
) return NULL
;
259 return §ion
->lines
[line_index
];
263 /* retrieve a given field from section/line/field index */
264 static struct field
*get_field( struct inf_file
*file
, int section_index
, int line_index
,
267 struct line
*line
= get_line( file
, section_index
, line_index
);
269 if (!line
) return NULL
;
270 if (!field_index
) /* get the key */
272 if (line
->key_field
== -1) return NULL
;
273 return &file
->fields
[line
->key_field
];
276 if (field_index
>= line
->nb_fields
) return NULL
;
277 return &file
->fields
[line
->first_field
+ field_index
];
281 /* allocate a new field, growing the array if necessary */
282 static struct field
*add_field( struct inf_file
*file
, const WCHAR
*text
)
286 if (file
->nb_fields
>= file
->alloc_fields
)
288 if (!(file
->fields
= grow_array( file
->fields
, &file
->alloc_fields
,
289 sizeof(file
->fields
[0]) ))) return NULL
;
291 field
= &file
->fields
[file
->nb_fields
++];
297 /* retrieve the string substitution for a directory id */
298 static const WCHAR
*get_dirid_subst( int dirid
, unsigned int *len
)
300 extern const WCHAR
*DIRID_get_string( HINF hinf
, int dirid
);
301 const WCHAR
*ret
= DIRID_get_string( 0, dirid
);
302 if (ret
) *len
= strlenW(ret
);
307 /* retrieve the string substitution for a given string, or NULL if not found */
308 /* if found, len is set to the substitution length */
309 static const WCHAR
*get_string_subst( struct inf_file
*file
, const WCHAR
*str
, unsigned int *len
)
311 static const WCHAR percent
= '%';
313 struct section
*strings_section
;
318 WCHAR
*dirid_str
, *end
;
319 const WCHAR
*ret
= NULL
;
321 if (!*len
) /* empty string (%%) is replaced by single percent */
326 if (file
->strings_section
== -1) goto not_found
;
327 strings_section
= file
->sections
[file
->strings_section
];
328 for (i
= 0, line
= strings_section
->lines
; i
< strings_section
->nb_lines
; i
++, line
++)
330 if (line
->key_field
== -1) continue;
331 if (strncmpiW( str
, file
->fields
[line
->key_field
].text
, *len
)) continue;
332 if (!file
->fields
[line
->key_field
].text
[*len
]) break;
334 if (i
== strings_section
->nb_lines
|| !line
->nb_fields
) goto not_found
;
335 field
= &file
->fields
[line
->first_field
];
336 *len
= strlenW( field
->text
);
339 not_found
: /* check for integer id */
340 if ((dirid_str
= HeapAlloc( GetProcessHeap(), 0, (*len
+1) * sizeof(WCHAR
) )))
342 memcpy( dirid_str
, str
, *len
* sizeof(WCHAR
) );
344 dirid
= strtolW( dirid_str
, &end
, 10 );
345 if (!*end
) ret
= get_dirid_subst( dirid
, len
);
346 HeapFree( GetProcessHeap(), 0, dirid_str
);
353 /* do string substitutions on the specified text */
354 /* the buffer is assumed to be large enough */
355 /* returns necessary length not including terminating null */
356 unsigned int PARSER_string_substW( struct inf_file
*file
, const WCHAR
*text
, WCHAR
*buffer
,
359 const WCHAR
*start
, *subst
, *p
;
360 unsigned int len
, total
= 0;
363 if (!buffer
) size
= MAX_STRING_LEN
+ 1;
364 for (p
= start
= text
; *p
; p
++)
366 if (*p
!= '%') continue;
368 if (inside
) /* start of a %xx% string */
371 if (len
> size
- 1) len
= size
- 1;
372 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
377 else /* end of the %xx% string, find substitution */
380 subst
= get_string_subst( file
, start
+ 1, &len
);
386 if (len
> size
- 1) len
= size
- 1;
387 if (buffer
) memcpy( buffer
+ total
, subst
, len
* sizeof(WCHAR
) );
394 if (start
!= p
) /* unfinished string, copy it */
397 if (len
> size
- 1) len
= size
- 1;
398 if (buffer
) memcpy( buffer
+ total
, start
, len
* sizeof(WCHAR
) );
401 if (buffer
&& size
) buffer
[total
] = 0;
406 /* do string substitutions on the specified text */
407 /* the buffer is assumed to be large enough */
408 /* returns necessary length not including terminating null */
409 unsigned int PARSER_string_substA( struct inf_file
*file
, const WCHAR
*text
, char *buffer
,
412 WCHAR buffW
[MAX_STRING_LEN
+1];
415 unsigned int len
= PARSER_string_substW( file
, text
, buffW
, sizeof(buffW
)/sizeof(WCHAR
) );
416 if (!buffer
) RtlUnicodeToMultiByteSize( &ret
, buffW
, len
* sizeof(WCHAR
) );
419 RtlUnicodeToMultiByteN( buffer
, size
-1, &ret
, buffW
, len
* sizeof(WCHAR
) );
426 /* push some string data into the strings buffer */
427 static WCHAR
*push_string( struct inf_file
*file
, const WCHAR
*string
)
429 WCHAR
*ret
= file
->string_pos
;
430 strcpyW( ret
, string
);
431 file
->string_pos
+= strlenW( ret
) + 1;
436 /* push the current state on the parser stack */
437 inline static void push_state( struct parser
*parser
, enum parser_state state
)
439 assert( parser
->stack_pos
< sizeof(parser
->stack
)/sizeof(parser
->stack
[0]) );
440 parser
->stack
[parser
->stack_pos
++] = state
;
444 /* pop the current state */
445 inline static void pop_state( struct parser
*parser
)
447 assert( parser
->stack_pos
);
448 parser
->state
= parser
->stack
[--parser
->stack_pos
];
452 /* set the parser state and return the previous one */
453 inline static enum parser_state
set_state( struct parser
*parser
, enum parser_state state
)
455 enum parser_state ret
= parser
->state
;
456 parser
->state
= state
;
461 /* check if the pointer points to an end of file */
462 inline static int is_eof( struct parser
*parser
, const WCHAR
*ptr
)
464 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
);
468 /* check if the pointer points to an end of line */
469 inline static int is_eol( struct parser
*parser
, const WCHAR
*ptr
)
471 return (ptr
>= parser
->end
|| *ptr
== CONTROL_Z
|| *ptr
== '\n');
475 /* push data from current token start up to pos into the current token */
476 static int push_token( struct parser
*parser
, const WCHAR
*pos
)
478 int len
= pos
- parser
->start
;
479 const WCHAR
*src
= parser
->start
;
480 WCHAR
*dst
= parser
->token
+ parser
->token_len
;
482 if (len
> MAX_FIELD_LEN
- parser
->token_len
) len
= MAX_FIELD_LEN
- parser
->token_len
;
484 parser
->token_len
+= len
;
485 for ( ; len
> 0; len
--, dst
++, src
++) *dst
= *src
? *src
: ' ';
492 /* add a section with the current token as name */
493 static int add_section_from_token( struct parser
*parser
)
497 if (parser
->token_len
> MAX_SECTION_NAME_LEN
)
499 parser
->error
= ERROR_SECTION_NAME_TOO_LONG
;
502 if ((section_index
= find_section( parser
->file
, parser
->token
)) == -1)
504 /* need to create a new one */
505 const WCHAR
*name
= push_string( parser
->file
, parser
->token
);
506 if ((section_index
= add_section( parser
->file
, name
)) == -1)
508 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
512 parser
->token_len
= 0;
513 parser
->cur_section
= section_index
;
514 return section_index
;
518 /* add a field containing the current token to the current line */
519 static struct field
*add_field_from_token( struct parser
*parser
, int is_key
)
524 if (!parser
->line
) /* need to start a new line */
526 if (parser
->cur_section
== -1) /* got a line before the first section */
528 parser
->error
= ERROR_WRONG_INF_STYLE
;
531 if (!(parser
->line
= add_line( parser
->file
, parser
->cur_section
))) goto error
;
533 else assert(!is_key
);
535 text
= push_string( parser
->file
, parser
->token
);
536 if ((field
= add_field( parser
->file
, text
)))
538 if (!is_key
) parser
->line
->nb_fields
++;
541 /* replace first field by key field */
542 parser
->line
->key_field
= parser
->line
->first_field
;
543 parser
->line
->first_field
++;
545 parser
->token_len
= 0;
549 parser
->error
= ERROR_NOT_ENOUGH_MEMORY
;
554 /* close the current line and prepare for parsing a new one */
555 static void close_current_line( struct parser
*parser
)
557 struct line
*cur_line
= parser
->line
;
561 /* if line has a single field and no key, the field is the key too */
562 if (cur_line
->nb_fields
== 1 && cur_line
->key_field
== -1)
563 cur_line
->key_field
= cur_line
->first_field
;
569 /* handler for parser LINE_START state */
570 static const WCHAR
*line_start_state( struct parser
*parser
, const WCHAR
*pos
)
574 for (p
= pos
; !is_eof( parser
, p
); p
++)
580 close_current_line( parser
);
583 push_state( parser
, LINE_START
);
584 set_state( parser
, COMMENT
);
587 parser
->start
= p
+ 1;
588 set_state( parser
, SECTION_NAME
);
594 set_state( parser
, KEY_NAME
);
600 close_current_line( parser
);
605 /* handler for parser SECTION_NAME state */
606 static const WCHAR
*section_name_state( struct parser
*parser
, const WCHAR
*pos
)
610 for (p
= pos
; !is_eol( parser
, p
); p
++)
614 push_token( parser
, p
);
615 if (add_section_from_token( parser
) == -1) return NULL
;
616 push_state( parser
, LINE_START
);
617 set_state( parser
, COMMENT
); /* ignore everything else on the line */
621 parser
->error
= ERROR_BAD_SECTION_NAME_LINE
; /* unfinished section name */
626 /* handler for parser KEY_NAME state */
627 static const WCHAR
*key_name_state( struct parser
*parser
, const WCHAR
*pos
)
629 const WCHAR
*p
, *token_end
= parser
->start
;
631 for (p
= pos
; !is_eol( parser
, p
); p
++)
633 if (*p
== ',') break;
638 push_token( parser
, token_end
);
639 if (!add_field_from_token( parser
, 1 )) return NULL
;
640 parser
->start
= p
+ 1;
641 push_state( parser
, VALUE_NAME
);
642 set_state( parser
, LEADING_SPACES
);
645 push_token( parser
, token_end
);
646 if (!add_field_from_token( parser
, 0 )) return NULL
;
647 push_state( parser
, LINE_START
);
648 set_state( parser
, COMMENT
);
651 push_token( parser
, token_end
);
652 parser
->start
= p
+ 1;
653 push_state( parser
, KEY_NAME
);
654 set_state( parser
, QUOTES
);
657 push_token( parser
, token_end
);
659 push_state( parser
, KEY_NAME
);
660 set_state( parser
, EOL_BACKSLASH
);
663 if (!isspaceW(*p
)) token_end
= p
+ 1;
666 push_token( parser
, p
);
667 push_state( parser
, KEY_NAME
);
668 set_state( parser
, TRAILING_SPACES
);
674 push_token( parser
, token_end
);
675 set_state( parser
, VALUE_NAME
);
680 /* handler for parser VALUE_NAME state */
681 static const WCHAR
*value_name_state( struct parser
*parser
, const WCHAR
*pos
)
683 const WCHAR
*p
, *token_end
= parser
->start
;
685 for (p
= pos
; !is_eol( parser
, p
); p
++)
690 push_token( parser
, token_end
);
691 if (!add_field_from_token( parser
, 0 )) return NULL
;
692 push_state( parser
, LINE_START
);
693 set_state( parser
, COMMENT
);
696 push_token( parser
, token_end
);
697 if (!add_field_from_token( parser
, 0 )) return NULL
;
698 parser
->start
= p
+ 1;
699 push_state( parser
, VALUE_NAME
);
700 set_state( parser
, LEADING_SPACES
);
703 push_token( parser
, token_end
);
704 parser
->start
= p
+ 1;
705 push_state( parser
, VALUE_NAME
);
706 set_state( parser
, QUOTES
);
709 push_token( parser
, token_end
);
711 push_state( parser
, VALUE_NAME
);
712 set_state( parser
, EOL_BACKSLASH
);
715 if (!isspaceW(*p
)) token_end
= p
+ 1;
718 push_token( parser
, p
);
719 push_state( parser
, VALUE_NAME
);
720 set_state( parser
, TRAILING_SPACES
);
726 push_token( parser
, token_end
);
727 if (!add_field_from_token( parser
, 0 )) return NULL
;
728 set_state( parser
, LINE_START
);
733 /* handler for parser EOL_BACKSLASH state */
734 static const WCHAR
*eol_backslash_state( struct parser
*parser
, const WCHAR
*pos
)
738 for (p
= pos
; !is_eof( parser
, p
); p
++)
744 parser
->start
= p
+ 1;
745 set_state( parser
, LEADING_SPACES
);
750 push_state( parser
, EOL_BACKSLASH
);
751 set_state( parser
, COMMENT
);
754 if (isspaceW(*p
)) continue;
755 push_token( parser
, p
);
766 /* handler for parser QUOTES state */
767 static const WCHAR
*quotes_state( struct parser
*parser
, const WCHAR
*pos
)
769 const WCHAR
*p
, *token_end
= parser
->start
;
771 for (p
= pos
; !is_eol( parser
, p
); p
++)
775 if (p
+1 < parser
->end
&& p
[1] == '"') /* double quotes */
777 push_token( parser
, p
+ 1 );
778 parser
->start
= token_end
= p
+ 2;
781 else /* end of quotes */
783 push_token( parser
, p
);
784 parser
->start
= p
+ 1;
790 push_token( parser
, p
);
796 /* handler for parser LEADING_SPACES state */
797 static const WCHAR
*leading_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
801 for (p
= pos
; !is_eol( parser
, p
); p
++)
806 set_state( parser
, EOL_BACKSLASH
);
809 if (!isspaceW(*p
)) break;
817 /* handler for parser TRAILING_SPACES state */
818 static const WCHAR
*trailing_spaces_state( struct parser
*parser
, const WCHAR
*pos
)
822 for (p
= pos
; !is_eol( parser
, p
); p
++)
826 set_state( parser
, EOL_BACKSLASH
);
829 if (!isspaceW(*p
)) break;
836 /* handler for parser COMMENT state */
837 static const WCHAR
*comment_state( struct parser
*parser
, const WCHAR
*pos
)
839 const WCHAR
*p
= pos
;
841 while (!is_eol( parser
, p
)) p
++;
847 /* parse a complete buffer */
848 static DWORD
parse_buffer( struct inf_file
*file
, const WCHAR
*buffer
, const WCHAR
*end
,
851 static const WCHAR Strings
[] = {'S','t','r','i','n','g','s',0};
853 struct parser parser
;
854 const WCHAR
*pos
= buffer
;
856 parser
.start
= buffer
;
860 parser
.state
= LINE_START
;
861 parser
.stack_pos
= 0;
862 parser
.cur_section
= -1;
865 parser
.token_len
= 0;
867 /* parser main loop */
868 while (pos
) pos
= (parser_funcs
[parser
.state
])( &parser
, pos
);
870 /* trim excess buffer space */
871 if (file
->alloc_sections
> file
->nb_sections
)
873 file
->sections
= HeapReAlloc( GetProcessHeap(), 0, file
->sections
,
874 file
->nb_sections
* sizeof(file
->sections
[0]) );
875 file
->alloc_sections
= file
->nb_sections
;
877 if (file
->alloc_fields
> file
->nb_fields
)
879 file
->fields
= HeapReAlloc( GetProcessHeap(), 0, file
->fields
,
880 file
->nb_fields
* sizeof(file
->fields
[0]) );
881 file
->alloc_fields
= file
->nb_fields
;
883 file
->strings
= HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, file
->strings
,
884 (file
->string_pos
- file
->strings
) * sizeof(WCHAR
) );
888 if (error_line
) *error_line
= parser
.line_pos
;
892 /* find the [strings] section */
893 file
->strings_section
= find_section( file
, Strings
);
898 /* append a child INF file to its parent list, in a thread-safe manner */
899 static void append_inf_file( struct inf_file
*parent
, struct inf_file
*child
)
901 struct inf_file
**ppnext
= &parent
->next
;
906 struct inf_file
*next
= InterlockedCompareExchangePointer( (void **)ppnext
, child
, NULL
);
908 ppnext
= &next
->next
;
913 /***********************************************************************
918 static struct inf_file
*parse_file( HANDLE handle
, const WCHAR
*class, UINT
*error_line
)
922 struct inf_file
*file
;
924 DWORD size
= GetFileSize( handle
, NULL
);
925 HANDLE mapping
= CreateFileMappingW( handle
, NULL
, PAGE_READONLY
, 0, size
, NULL
);
926 if (!mapping
) return NULL
;
927 buffer
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, size
);
929 if (!buffer
) return NULL
;
931 if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) );
933 if (!(file
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*file
) )))
935 err
= ERROR_NOT_ENOUGH_MEMORY
;
939 /* we won't need more strings space than the size of the file,
940 * so we can preallocate it here
942 if (!(file
->strings
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
944 err
= ERROR_NOT_ENOUGH_MEMORY
;
947 file
->string_pos
= file
->strings
;
948 file
->strings_section
= -1;
950 if (!RtlIsTextUnicode( buffer
, size
, NULL
))
952 WCHAR
*new_buff
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) );
955 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, buffer
, size
, new_buff
,
956 size
* sizeof(WCHAR
) );
957 err
= parse_buffer( file
, new_buff
, new_buff
+ len
, error_line
);
958 HeapFree( GetProcessHeap(), 0, new_buff
);
961 else err
= parse_buffer( file
, buffer
, (WCHAR
*)((char *)buffer
+ size
), error_line
);
963 if (!err
) /* now check signature */
965 int version_index
= find_section( file
, Version
);
966 if (version_index
!= -1)
968 struct line
*line
= find_line( file
, version_index
, Signature
);
969 if (line
&& line
->nb_fields
> 0)
971 struct field
*field
= file
->fields
+ line
->first_field
;
972 if (!strcmpiW( field
->text
, Chicago
)) goto done
;
973 if (!strcmpiW( field
->text
, WindowsNT
)) goto done
;
974 if (!strcmpiW( field
->text
, Windows95
)) goto done
;
977 err
= ERROR_WRONG_INF_STYLE
;
981 UnmapViewOfFile( buffer
);
984 HeapFree( GetProcessHeap(), 0, file
);
992 /***********************************************************************
993 * PARSER_get_src_root
995 * Retrieve the source directory of an inf file.
997 const WCHAR
*PARSER_get_src_root( HINF hinf
)
999 struct inf_file
*file
= hinf
;
1000 return file
->src_root
;
1004 /***********************************************************************
1005 * PARSER_get_dest_dir
1007 * retrieve a destination dir of the form "dirid,relative_path" in the given entry.
1008 * returned buffer must be freed by caller.
1010 WCHAR
*PARSER_get_dest_dir( INFCONTEXT
*context
)
1017 if (!SetupGetIntField( context
, 1, &dirid
)) return NULL
;
1018 if (!(dir
= DIRID_get_string( context
->Inf
, dirid
))) return NULL
;
1019 len1
= strlenW(dir
) + 1;
1020 if (!SetupGetStringFieldW( context
, 2, NULL
, 0, &len2
)) len2
= 0;
1021 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, (len1
+len2
) * sizeof(WCHAR
) ))) return NULL
;
1022 strcpyW( ret
, dir
);
1023 ptr
= ret
+ strlenW(ret
);
1024 if (len2
&& ptr
> ret
&& ptr
[-1] != '\\') *ptr
++ = '\\';
1025 if (!SetupGetStringFieldW( context
, 2, ptr
, len2
, NULL
)) *ptr
= 0;
1030 /***********************************************************************
1031 * SetupOpenInfFileA (SETUPAPI.@)
1033 HINF WINAPI
SetupOpenInfFileA( PCSTR name
, PCSTR
class, DWORD style
, UINT
*error
)
1035 UNICODE_STRING nameW
, classW
;
1036 HINF ret
= (HINF
)INVALID_HANDLE_VALUE
;
1038 classW
.Buffer
= NULL
;
1039 if (class && !RtlCreateUnicodeStringFromAsciiz( &classW
, class ))
1041 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1044 if (RtlCreateUnicodeStringFromAsciiz( &nameW
, name
))
1046 ret
= SetupOpenInfFileW( nameW
.Buffer
, classW
.Buffer
, style
, error
);
1047 RtlFreeUnicodeString( &nameW
);
1049 RtlFreeUnicodeString( &classW
);
1054 /***********************************************************************
1055 * SetupOpenInfFileW (SETUPAPI.@)
1057 HINF WINAPI
SetupOpenInfFileW( PCWSTR name
, PCWSTR
class, DWORD style
, UINT
*error
)
1059 struct inf_file
*file
= NULL
;
1064 if (strchrW( name
, '\\' ) || strchrW( name
, '/' ))
1066 if (!(len
= GetFullPathNameW( name
, 0, NULL
, NULL
))) return (HINF
)INVALID_HANDLE_VALUE
;
1067 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1069 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1070 return (HINF
)INVALID_HANDLE_VALUE
;
1072 GetFullPathNameW( name
, len
, path
, NULL
);
1073 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1075 else /* try Windows directory */
1077 static const WCHAR Inf
[] = {'\\','i','n','f','\\',0};
1078 static const WCHAR System32
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
1080 len
= GetWindowsDirectoryW( NULL
, 0 ) + strlenW(name
) + 12;
1081 if (!(path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1083 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1084 return (HINF
)INVALID_HANDLE_VALUE
;
1086 GetWindowsDirectoryW( path
, len
);
1087 p
= path
+ strlenW(path
);
1090 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1091 if (handle
== INVALID_HANDLE_VALUE
)
1093 strcpyW( p
, System32
);
1095 handle
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1099 if (handle
!= INVALID_HANDLE_VALUE
)
1101 file
= parse_file( handle
, class, error
);
1102 CloseHandle( handle
);
1106 HeapFree( GetProcessHeap(), 0, path
);
1107 return (HINF
)INVALID_HANDLE_VALUE
;
1109 TRACE( "%s -> %p\n", debugstr_w(path
), file
);
1110 file
->src_root
= path
;
1111 if ((p
= strrchrW( path
, '\\' ))) p
[1] = 0; /* remove file name */
1117 /***********************************************************************
1118 * SetupOpenAppendInfFileA (SETUPAPI.@)
1120 BOOL WINAPI
SetupOpenAppendInfFileA( PCSTR name
, HINF parent_hinf
, UINT
*error
)
1124 if (!name
) return SetupOpenAppendInfFileW( NULL
, parent_hinf
, error
);
1125 child_hinf
= SetupOpenInfFileA( name
, NULL
, INF_STYLE_WIN4
, error
);
1126 if (child_hinf
== (HINF
)INVALID_HANDLE_VALUE
) return FALSE
;
1127 append_inf_file( parent_hinf
, child_hinf
);
1128 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_a(name
), child_hinf
);
1133 /***********************************************************************
1134 * SetupOpenAppendInfFileW (SETUPAPI.@)
1136 BOOL WINAPI
SetupOpenAppendInfFileW( PCWSTR name
, HINF parent_hinf
, UINT
*error
)
1143 WCHAR filename
[MAX_PATH
];
1146 if (!SetupFindFirstLineW( parent_hinf
, Version
, LayoutFile
, &context
)) return FALSE
;
1147 while (SetupGetStringFieldW( &context
, idx
++, filename
,
1148 sizeof(filename
)/sizeof(WCHAR
), NULL
))
1150 child_hinf
= SetupOpenInfFileW( filename
, NULL
, INF_STYLE_WIN4
, error
);
1151 if (child_hinf
== (HINF
)INVALID_HANDLE_VALUE
) return FALSE
;
1152 append_inf_file( parent_hinf
, child_hinf
);
1153 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(filename
), child_hinf
);
1157 child_hinf
= SetupOpenInfFileW( name
, NULL
, INF_STYLE_WIN4
, error
);
1158 if (child_hinf
== (HINF
)INVALID_HANDLE_VALUE
) return FALSE
;
1159 append_inf_file( parent_hinf
, child_hinf
);
1160 TRACE( "%p: appended %s (%p)\n", parent_hinf
, debugstr_w(name
), child_hinf
);
1165 /***********************************************************************
1166 * SetupOpenMasterInf (SETUPAPI.@)
1168 HINF WINAPI
SetupOpenMasterInf( VOID
)
1170 static const WCHAR Layout
[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0};
1171 WCHAR Buffer
[MAX_PATH
];
1173 GetWindowsDirectoryW( Buffer
, MAX_PATH
);
1174 strcatW( Buffer
, Layout
);
1175 return SetupOpenInfFileW( Buffer
, NULL
, INF_STYLE_WIN4
, NULL
);
1180 /***********************************************************************
1181 * SetupCloseInfFile (SETUPAPI.@)
1183 void WINAPI
SetupCloseInfFile( HINF hinf
)
1185 struct inf_file
*file
= hinf
;
1188 for (i
= 0; i
< file
->nb_sections
; i
++) HeapFree( GetProcessHeap(), 0, file
->sections
[i
] );
1189 HeapFree( GetProcessHeap(), 0, file
->src_root
);
1190 HeapFree( GetProcessHeap(), 0, file
->sections
);
1191 HeapFree( GetProcessHeap(), 0, file
->fields
);
1192 HeapFree( GetProcessHeap(), 0, file
->strings
);
1193 HeapFree( GetProcessHeap(), 0, file
);
1197 /***********************************************************************
1198 * SetupGetLineCountA (SETUPAPI.@)
1200 LONG WINAPI
SetupGetLineCountA( HINF hinf
, PCSTR name
)
1202 UNICODE_STRING sectionW
;
1205 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, name
))
1206 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1209 ret
= SetupGetLineCountW( hinf
, sectionW
.Buffer
);
1210 RtlFreeUnicodeString( §ionW
);
1216 /***********************************************************************
1217 * SetupGetLineCountW (SETUPAPI.@)
1219 LONG WINAPI
SetupGetLineCountW( HINF hinf
, PCWSTR section
)
1221 struct inf_file
*file
= hinf
;
1225 for (file
= hinf
; file
; file
= file
->next
)
1227 if ((section_index
= find_section( file
, section
)) == -1) continue;
1228 if (ret
== -1) ret
= 0;
1229 ret
+= file
->sections
[section_index
]->nb_lines
;
1231 TRACE( "(%p,%s) returning %ld\n", hinf
, debugstr_w(section
), ret
);
1232 SetLastError( (ret
== -1) ? ERROR_SECTION_NOT_FOUND
: 0 );
1237 /***********************************************************************
1238 * SetupGetLineByIndexA (SETUPAPI.@)
1240 BOOL WINAPI
SetupGetLineByIndexA( HINF hinf
, PCSTR section
, DWORD index
, INFCONTEXT
*context
)
1242 UNICODE_STRING sectionW
;
1245 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1246 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1249 ret
= SetupGetLineByIndexW( hinf
, sectionW
.Buffer
, index
, context
);
1250 RtlFreeUnicodeString( §ionW
);
1256 /***********************************************************************
1257 * SetupGetLineByIndexW (SETUPAPI.@)
1259 BOOL WINAPI
SetupGetLineByIndexW( HINF hinf
, PCWSTR section
, DWORD index
, INFCONTEXT
*context
)
1261 struct inf_file
*file
= hinf
;
1264 SetLastError( ERROR_SECTION_NOT_FOUND
);
1265 for (file
= hinf
; file
; file
= file
->next
)
1267 if ((section_index
= find_section( file
, section
)) == -1) continue;
1268 SetLastError( ERROR_LINE_NOT_FOUND
);
1269 if (index
< file
->sections
[section_index
]->nb_lines
)
1271 context
->Inf
= hinf
;
1272 context
->CurrentInf
= file
;
1273 context
->Section
= section_index
;
1274 context
->Line
= index
;
1276 TRACE( "(%p,%s): returning %d/%ld\n",
1277 hinf
, debugstr_w(section
), section_index
, index
);
1280 index
-= file
->sections
[section_index
]->nb_lines
;
1282 TRACE( "(%p,%s) not found\n", hinf
, debugstr_w(section
) );
1287 /***********************************************************************
1288 * SetupFindFirstLineA (SETUPAPI.@)
1290 BOOL WINAPI
SetupFindFirstLineA( HINF hinf
, PCSTR section
, PCSTR key
, INFCONTEXT
*context
)
1292 UNICODE_STRING sectionW
, keyW
;
1295 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1297 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1301 if (!key
) ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, NULL
, context
);
1304 if (RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1306 ret
= SetupFindFirstLineW( hinf
, sectionW
.Buffer
, keyW
.Buffer
, context
);
1307 RtlFreeUnicodeString( &keyW
);
1309 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1311 RtlFreeUnicodeString( §ionW
);
1316 /***********************************************************************
1317 * SetupFindFirstLineW (SETUPAPI.@)
1319 BOOL WINAPI
SetupFindFirstLineW( HINF hinf
, PCWSTR section
, PCWSTR key
, INFCONTEXT
*context
)
1321 struct inf_file
*file
;
1324 SetLastError( ERROR_SECTION_NOT_FOUND
);
1325 for (file
= hinf
; file
; file
= file
->next
)
1327 if ((section_index
= find_section( file
, section
)) == -1) continue;
1332 ctx
.CurrentInf
= file
;
1333 ctx
.Section
= section_index
;
1335 return SetupFindNextMatchLineW( &ctx
, key
, context
);
1337 SetLastError( ERROR_LINE_NOT_FOUND
); /* found at least one section */
1338 if (file
->sections
[section_index
]->nb_lines
)
1340 context
->Inf
= hinf
;
1341 context
->CurrentInf
= file
;
1342 context
->Section
= section_index
;
1345 TRACE( "(%p,%s,%s): returning %d/0\n",
1346 hinf
, debugstr_w(section
), debugstr_w(key
), section_index
);
1350 TRACE( "(%p,%s,%s): not found\n", hinf
, debugstr_w(section
), debugstr_w(key
) );
1355 /***********************************************************************
1356 * SetupFindNextLine (SETUPAPI.@)
1358 BOOL WINAPI
SetupFindNextLine( PINFCONTEXT context_in
, PINFCONTEXT context_out
)
1360 struct inf_file
*file
= context_in
->CurrentInf
;
1361 struct section
*section
;
1363 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1365 section
= file
->sections
[context_in
->Section
];
1366 if (context_in
->Line
+1 < section
->nb_lines
)
1368 if (context_out
!= context_in
) *context_out
= *context_in
;
1369 context_out
->Line
++;
1374 /* now search the appended files */
1376 for (file
= file
->next
; file
; file
= file
->next
)
1378 int section_index
= find_section( file
, section
->name
);
1379 if (section_index
== -1) continue;
1380 if (file
->sections
[section_index
]->nb_lines
)
1382 context_out
->Inf
= context_in
->Inf
;
1383 context_out
->CurrentInf
= file
;
1384 context_out
->Section
= section_index
;
1385 context_out
->Line
= 0;
1391 SetLastError( ERROR_LINE_NOT_FOUND
);
1396 /***********************************************************************
1397 * SetupFindNextMatchLineA (SETUPAPI.@)
1399 BOOL WINAPI
SetupFindNextMatchLineA( PINFCONTEXT context_in
, PCSTR key
,
1400 PINFCONTEXT context_out
)
1402 UNICODE_STRING keyW
;
1405 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1407 if (!RtlCreateUnicodeStringFromAsciiz( &keyW
, key
))
1408 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1411 ret
= SetupFindNextMatchLineW( context_in
, keyW
.Buffer
, context_out
);
1412 RtlFreeUnicodeString( &keyW
);
1418 /***********************************************************************
1419 * SetupFindNextMatchLineW (SETUPAPI.@)
1421 BOOL WINAPI
SetupFindNextMatchLineW( PINFCONTEXT context_in
, PCWSTR key
,
1422 PINFCONTEXT context_out
)
1424 struct inf_file
*file
= context_in
->CurrentInf
;
1425 struct section
*section
;
1429 if (!key
) return SetupFindNextLine( context_in
, context_out
);
1431 if (context_in
->Section
>= file
->nb_sections
) goto error
;
1433 section
= file
->sections
[context_in
->Section
];
1435 for (i
= context_in
->Line
+1, line
= §ion
->lines
[i
]; i
< section
->nb_lines
; i
++, line
++)
1437 if (line
->key_field
== -1) continue;
1438 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1440 if (context_out
!= context_in
) *context_out
= *context_in
;
1441 context_out
->Line
= i
;
1443 TRACE( "(%p,%s,%s): returning %d\n",
1444 file
, debugstr_w(section
->name
), debugstr_w(key
), i
);
1449 /* now search the appended files */
1451 for (file
= file
->next
; file
; file
= file
->next
)
1453 int section_index
= find_section( file
, section
->name
);
1454 if (section_index
== -1) continue;
1455 section
= file
->sections
[section_index
];
1456 for (i
= 0, line
= section
->lines
; i
< section
->nb_lines
; i
++, line
++)
1458 if (line
->key_field
== -1) continue;
1459 if (!strcmpiW( key
, file
->fields
[line
->key_field
].text
))
1461 context_out
->Inf
= context_in
->Inf
;
1462 context_out
->CurrentInf
= file
;
1463 context_out
->Section
= section_index
;
1464 context_out
->Line
= i
;
1466 TRACE( "(%p,%s,%s): returning %d/%d\n",
1467 file
, debugstr_w(section
->name
), debugstr_w(key
), section_index
, i
);
1472 TRACE( "(%p,%s,%s): not found\n",
1473 context_in
->CurrentInf
, debugstr_w(section
->name
), debugstr_w(key
) );
1475 SetLastError( ERROR_LINE_NOT_FOUND
);
1480 /***********************************************************************
1481 * SetupGetLineTextW (SETUPAPI.@)
1483 BOOL WINAPI
SetupGetLineTextW( PINFCONTEXT context
, HINF hinf
, PCWSTR section_name
,
1484 PCWSTR key_name
, PWSTR buffer
, DWORD size
, PDWORD required
)
1486 struct inf_file
*file
;
1488 struct field
*field
;
1494 INFCONTEXT new_context
;
1495 if (!SetupFindFirstLineW( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1496 file
= new_context
.CurrentInf
;
1497 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1501 file
= context
->CurrentInf
;
1502 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1504 SetLastError( ERROR_LINE_NOT_FOUND
);
1509 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1510 total
+= PARSER_string_substW( file
, field
->text
, NULL
, 0 ) + 1;
1512 if (required
) *required
= total
;
1517 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1520 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1522 unsigned int len
= PARSER_string_substW( file
, field
->text
, buffer
, size
);
1523 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1531 /***********************************************************************
1532 * SetupGetLineTextA (SETUPAPI.@)
1534 BOOL WINAPI
SetupGetLineTextA( PINFCONTEXT context
, HINF hinf
, PCSTR section_name
,
1535 PCSTR key_name
, PSTR buffer
, DWORD size
, PDWORD required
)
1537 struct inf_file
*file
;
1539 struct field
*field
;
1545 INFCONTEXT new_context
;
1546 if (!SetupFindFirstLineA( hinf
, section_name
, key_name
, &new_context
)) return FALSE
;
1547 file
= new_context
.CurrentInf
;
1548 line
= get_line( file
, new_context
.Section
, new_context
.Line
);
1552 file
= context
->CurrentInf
;
1553 if (!(line
= get_line( file
, context
->Section
, context
->Line
)))
1555 SetLastError( ERROR_LINE_NOT_FOUND
);
1560 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1561 total
+= PARSER_string_substA( file
, field
->text
, NULL
, 0 ) + 1;
1563 if (required
) *required
= total
;
1568 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1571 for (i
= 0, field
= &file
->fields
[line
->first_field
]; i
< line
->nb_fields
; i
++, field
++)
1573 unsigned int len
= PARSER_string_substA( file
, field
->text
, buffer
, size
);
1574 if (i
+1 < line
->nb_fields
) buffer
[len
] = ',';
1582 /***********************************************************************
1583 * SetupGetFieldCount (SETUPAPI.@)
1585 DWORD WINAPI
SetupGetFieldCount( PINFCONTEXT context
)
1587 struct inf_file
*file
= context
->CurrentInf
;
1588 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1590 if (!line
) return 0;
1591 return line
->nb_fields
;
1595 /***********************************************************************
1596 * SetupGetStringFieldA (SETUPAPI.@)
1598 BOOL WINAPI
SetupGetStringFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1599 DWORD size
, PDWORD required
)
1601 struct inf_file
*file
= context
->CurrentInf
;
1602 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1606 if (!field
) return FALSE
;
1607 len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 );
1608 if (required
) *required
= len
+ 1;
1613 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1616 PARSER_string_substA( file
, field
->text
, buffer
, size
);
1618 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1619 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1620 index
, debugstr_a(buffer
) );
1626 /***********************************************************************
1627 * SetupGetStringFieldW (SETUPAPI.@)
1629 BOOL WINAPI
SetupGetStringFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1630 DWORD size
, PDWORD required
)
1632 struct inf_file
*file
= context
->CurrentInf
;
1633 struct field
*field
= get_field( file
, context
->Section
, context
->Line
, index
);
1637 if (!field
) return FALSE
;
1638 len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 );
1639 if (required
) *required
= len
+ 1;
1644 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1647 PARSER_string_substW( file
, field
->text
, buffer
, size
);
1649 TRACE( "context %p/%p/%d/%d index %ld returning %s\n",
1650 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
,
1651 index
, debugstr_w(buffer
) );
1657 /***********************************************************************
1658 * SetupGetIntField (SETUPAPI.@)
1660 BOOL WINAPI
SetupGetIntField( PINFCONTEXT context
, DWORD index
, PINT result
)
1663 char *end
, *buffer
= localbuff
;
1668 if (!SetupGetStringFieldA( context
, index
, localbuff
, sizeof(localbuff
), &required
))
1670 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
) return FALSE
;
1671 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
))) return FALSE
;
1672 if (!SetupGetStringFieldA( context
, index
, buffer
, required
, NULL
)) goto done
;
1674 res
= strtol( buffer
, &end
, 0 );
1675 if (end
!= buffer
&& !*end
)
1680 else SetLastError( ERROR_INVALID_DATA
);
1683 if (buffer
!= localbuff
) HeapFree( GetProcessHeap(), 0, buffer
);
1688 /***********************************************************************
1689 * SetupGetBinaryField (SETUPAPI.@)
1691 BOOL WINAPI
SetupGetBinaryField( PINFCONTEXT context
, DWORD index
, BYTE
*buffer
,
1692 DWORD size
, LPDWORD required
)
1694 struct inf_file
*file
= context
->CurrentInf
;
1695 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1696 struct field
*field
;
1701 SetLastError( ERROR_LINE_NOT_FOUND
);
1704 if (!index
|| index
>= line
->nb_fields
)
1706 SetLastError( ERROR_INVALID_PARAMETER
);
1709 index
--; /* fields start at 0 */
1710 if (required
) *required
= line
->nb_fields
- index
;
1711 if (!buffer
) return TRUE
;
1712 if (size
< line
->nb_fields
- index
)
1714 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1717 field
= &file
->fields
[line
->first_field
+ index
];
1718 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1722 for (p
= field
->text
; *p
&& isxdigitW(*p
); p
++)
1724 if ((value
<<= 4) > 255)
1726 SetLastError( ERROR_INVALID_DATA
);
1729 if (*p
<= '9') value
|= (*p
- '0');
1730 else value
|= (tolowerW(*p
) - 'a' + 10);
1732 buffer
[i
- index
] = value
;
1734 if (TRACE_ON(setupapi
))
1736 TRACE( "%p/%p/%d/%d index %ld returning",
1737 context
->Inf
, context
->CurrentInf
, context
->Section
, context
->Line
, index
);
1738 for (i
= index
; i
< line
->nb_fields
; i
++) TRACE( " %02x", buffer
[i
- index
] );
1745 /***********************************************************************
1746 * SetupGetMultiSzFieldA (SETUPAPI.@)
1748 BOOL WINAPI
SetupGetMultiSzFieldA( PINFCONTEXT context
, DWORD index
, PSTR buffer
,
1749 DWORD size
, LPDWORD required
)
1751 struct inf_file
*file
= context
->CurrentInf
;
1752 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1753 struct field
*field
;
1760 SetLastError( ERROR_LINE_NOT_FOUND
);
1763 if (!index
|| index
>= line
->nb_fields
)
1765 SetLastError( ERROR_INVALID_PARAMETER
);
1768 index
--; /* fields start at 0 */
1769 field
= &file
->fields
[line
->first_field
+ index
];
1770 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1772 if (!(len
= PARSER_string_substA( file
, field
->text
, NULL
, 0 ))) break;
1776 if (required
) *required
= total
;
1777 if (!buffer
) return TRUE
;
1780 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1783 field
= &file
->fields
[line
->first_field
+ index
];
1784 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1786 if (!(len
= PARSER_string_substA( file
, field
->text
, buffer
, size
))) break;
1789 *buffer
= 0; /* add final null */
1794 /***********************************************************************
1795 * SetupGetMultiSzFieldW (SETUPAPI.@)
1797 BOOL WINAPI
SetupGetMultiSzFieldW( PINFCONTEXT context
, DWORD index
, PWSTR buffer
,
1798 DWORD size
, LPDWORD required
)
1800 struct inf_file
*file
= context
->CurrentInf
;
1801 struct line
*line
= get_line( file
, context
->Section
, context
->Line
);
1802 struct field
*field
;
1809 SetLastError( ERROR_LINE_NOT_FOUND
);
1812 if (!index
|| index
>= line
->nb_fields
)
1814 SetLastError( ERROR_INVALID_PARAMETER
);
1817 index
--; /* fields start at 0 */
1818 field
= &file
->fields
[line
->first_field
+ index
];
1819 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1821 if (!(len
= PARSER_string_substW( file
, field
->text
, NULL
, 0 ))) break;
1825 if (required
) *required
= total
;
1826 if (!buffer
) return TRUE
;
1829 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1832 field
= &file
->fields
[line
->first_field
+ index
];
1833 for (i
= index
; i
< line
->nb_fields
; i
++, field
++)
1835 if (!(len
= PARSER_string_substW( file
, field
->text
, buffer
, size
))) break;
1838 *buffer
= 0; /* add final null */