Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * INF file parsing |
| 3 | * |
| 4 | * Copyright 2002 Alexandre Julliard for CodeWeavers |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <limits.h> |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 26 | #include <stdarg.h> |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "windef.h" |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 31 | #include "winbase.h" |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 32 | #include "wingdi.h" |
| 33 | #include "winuser.h" |
| 34 | #include "winnls.h" |
Steven Edwards | b2b7262 | 2003-09-11 02:58:46 +0000 | [diff] [blame] | 35 | #include "winreg.h" |
| 36 | #include "winternl.h" |
| 37 | #include "winerror.h" |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 38 | #include "setupapi.h" |
Alexandre Julliard | cb28348 | 2004-02-27 00:47:10 +0000 | [diff] [blame] | 39 | #include "setupapi_private.h" |
Steven Edwards | b2b7262 | 2003-09-11 02:58:46 +0000 | [diff] [blame] | 40 | |
| 41 | #include "wine/unicode.h" |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 42 | #include "wine/debug.h" |
| 43 | |
| 44 | WINE_DEFAULT_DEBUG_CHANNEL(setupapi); |
| 45 | |
| 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) |
| 51 | |
| 52 | /* inf file structure definitions */ |
| 53 | |
| 54 | struct field |
| 55 | { |
| 56 | const WCHAR *text; /* field text */ |
| 57 | }; |
| 58 | |
| 59 | struct line |
| 60 | { |
| 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 */ |
| 64 | }; |
| 65 | |
| 66 | struct section |
| 67 | { |
| 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) */ |
| 72 | }; |
| 73 | |
| 74 | struct inf_file |
| 75 | { |
| 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; |
| 84 | struct field *fields; |
| 85 | int strings_section; /* index of [Strings] section or -1 if none */ |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 86 | WCHAR *filename; /* filename of the INF */ |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /* parser definitions */ |
| 90 | |
| 91 | enum parser_state |
| 92 | { |
| 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 */ |
| 102 | NB_PARSER_STATES |
| 103 | }; |
| 104 | |
| 105 | struct parser |
| 106 | { |
| 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 */ |
| 113 | |
| 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 */ |
| 120 | }; |
| 121 | |
| 122 | typedef const WCHAR * (*parser_state_func)( struct parser *parser, const WCHAR *pos ); |
| 123 | |
| 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 ); |
| 134 | |
| 135 | static const parser_state_func parser_funcs[NB_PARSER_STATES] = |
| 136 | { |
| 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 */ |
| 146 | }; |
| 147 | |
| 148 | |
| 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}; |
| 156 | |
| 157 | /* extend an array, allocating more memory if necessary */ |
| 158 | static void *grow_array( void *array, unsigned int *count, size_t elem ) |
| 159 | { |
| 160 | void *new_array; |
| 161 | unsigned int new_count = *count + *count / 2; |
| 162 | if (new_count < 32) new_count = 32; |
Oleg Prokhorov | b6ee614 | 2003-10-24 00:29:43 +0000 | [diff] [blame] | 163 | |
| 164 | if (array) |
| 165 | new_array = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, array, new_count * elem ); |
| 166 | else |
| 167 | new_array = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, new_count * elem ); |
| 168 | |
| 169 | if (new_array) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 170 | *count = new_count; |
| 171 | else |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 172 | HeapFree( GetProcessHeap(), 0, array ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 173 | return new_array; |
| 174 | } |
| 175 | |
| 176 | |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 177 | /* get the directory of the inf file (as counted string, not null-terminated) */ |
Andrew Talbot | 3bf6315 | 2007-08-27 16:09:06 +0100 | [diff] [blame] | 178 | static const WCHAR *get_inf_dir( const struct inf_file *file, unsigned int *len ) |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 179 | { |
| 180 | const WCHAR *p = strrchrW( file->filename, '\\' ); |
| 181 | *len = p ? (p + 1 - file->filename) : 0; |
| 182 | return file->filename; |
| 183 | } |
| 184 | |
| 185 | |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 186 | /* find a section by name */ |
Andrew Talbot | 3bf6315 | 2007-08-27 16:09:06 +0100 | [diff] [blame] | 187 | static int find_section( const struct inf_file *file, const WCHAR *name ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 188 | { |
Hans Leidekker | 719a789 | 2004-09-22 02:46:38 +0000 | [diff] [blame] | 189 | unsigned int i; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 190 | |
| 191 | for (i = 0; i < file->nb_sections; i++) |
| 192 | if (!strcmpiW( name, file->sections[i]->name )) return i; |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /* find a line by name */ |
| 198 | static struct line *find_line( struct inf_file *file, int section_index, const WCHAR *name ) |
| 199 | { |
| 200 | struct section *section; |
| 201 | struct line *line; |
Andrew Talbot | 1c64c93 | 2008-11-10 19:37:42 +0000 | [diff] [blame] | 202 | unsigned int i; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 203 | |
| 204 | if (section_index < 0 || section_index >= file->nb_sections) return NULL; |
| 205 | section = file->sections[section_index]; |
| 206 | for (i = 0, line = section->lines; i < section->nb_lines; i++, line++) |
| 207 | { |
| 208 | if (line->key_field == -1) continue; |
| 209 | if (!strcmpiW( name, file->fields[line->key_field].text )) return line; |
| 210 | } |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* add a section to the file and return the section index */ |
| 216 | static int add_section( struct inf_file *file, const WCHAR *name ) |
| 217 | { |
| 218 | struct section *section; |
| 219 | |
| 220 | if (file->nb_sections >= file->alloc_sections) |
| 221 | { |
| 222 | if (!(file->sections = grow_array( file->sections, &file->alloc_sections, |
| 223 | sizeof(file->sections[0]) ))) return -1; |
| 224 | } |
| 225 | if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) ))) return -1; |
| 226 | section->name = name; |
| 227 | section->nb_lines = 0; |
| 228 | section->alloc_lines = sizeof(section->lines)/sizeof(section->lines[0]); |
| 229 | file->sections[file->nb_sections] = section; |
| 230 | return file->nb_sections++; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /* add a line to a given section */ |
| 235 | static struct line *add_line( struct inf_file *file, int section_index ) |
| 236 | { |
| 237 | struct section *section; |
| 238 | struct line *line; |
| 239 | |
| 240 | assert( section_index >= 0 && section_index < file->nb_sections ); |
| 241 | |
| 242 | section = file->sections[section_index]; |
| 243 | if (section->nb_lines == section->alloc_lines) /* need to grow the section */ |
| 244 | { |
| 245 | int size = sizeof(*section) - sizeof(section->lines) + 2*section->alloc_lines*sizeof(*line); |
| 246 | if (!(section = HeapReAlloc( GetProcessHeap(), 0, section, size ))) return NULL; |
| 247 | section->alloc_lines *= 2; |
| 248 | file->sections[section_index] = section; |
| 249 | } |
| 250 | line = §ion->lines[section->nb_lines++]; |
| 251 | line->first_field = file->nb_fields; |
| 252 | line->nb_fields = 0; |
| 253 | line->key_field = -1; |
| 254 | return line; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /* retrieve a given line from section/line index */ |
Andrew Talbot | a89d5de | 2007-03-19 19:48:48 +0000 | [diff] [blame] | 259 | static inline struct line *get_line( struct inf_file *file, unsigned int section_index, |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 260 | unsigned int line_index ) |
| 261 | { |
| 262 | struct section *section; |
| 263 | |
| 264 | if (section_index >= file->nb_sections) return NULL; |
| 265 | section = file->sections[section_index]; |
| 266 | if (line_index >= section->nb_lines) return NULL; |
| 267 | return §ion->lines[line_index]; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /* retrieve a given field from section/line/field index */ |
| 272 | static struct field *get_field( struct inf_file *file, int section_index, int line_index, |
| 273 | int field_index ) |
| 274 | { |
| 275 | struct line *line = get_line( file, section_index, line_index ); |
| 276 | |
| 277 | if (!line) return NULL; |
| 278 | if (!field_index) /* get the key */ |
| 279 | { |
| 280 | if (line->key_field == -1) return NULL; |
| 281 | return &file->fields[line->key_field]; |
| 282 | } |
| 283 | field_index--; |
| 284 | if (field_index >= line->nb_fields) return NULL; |
| 285 | return &file->fields[line->first_field + field_index]; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /* allocate a new field, growing the array if necessary */ |
| 290 | static struct field *add_field( struct inf_file *file, const WCHAR *text ) |
| 291 | { |
| 292 | struct field *field; |
| 293 | |
| 294 | if (file->nb_fields >= file->alloc_fields) |
| 295 | { |
| 296 | if (!(file->fields = grow_array( file->fields, &file->alloc_fields, |
| 297 | sizeof(file->fields[0]) ))) return NULL; |
| 298 | } |
| 299 | field = &file->fields[file->nb_fields++]; |
| 300 | field->text = text; |
| 301 | return field; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | /* retrieve the string substitution for a directory id */ |
Andrew Talbot | 3bf6315 | 2007-08-27 16:09:06 +0100 | [diff] [blame] | 306 | static const WCHAR *get_dirid_subst( const struct inf_file *file, int dirid, unsigned int *len ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 307 | { |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 308 | const WCHAR *ret; |
| 309 | |
| 310 | if (dirid == DIRID_SRCPATH) return get_inf_dir( file, len ); |
| 311 | ret = DIRID_get_string( dirid ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 312 | if (ret) *len = strlenW(ret); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* retrieve the string substitution for a given string, or NULL if not found */ |
| 318 | /* if found, len is set to the substitution length */ |
Andrew Talbot | 3bf6315 | 2007-08-27 16:09:06 +0100 | [diff] [blame] | 319 | static const WCHAR *get_string_subst( const struct inf_file *file, const WCHAR *str, unsigned int *len, |
Jason Edmeades | 7f901f4 | 2007-08-10 22:28:35 +0100 | [diff] [blame] | 320 | BOOL no_trailing_slash ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 321 | { |
| 322 | static const WCHAR percent = '%'; |
| 323 | |
| 324 | struct section *strings_section; |
| 325 | struct line *line; |
| 326 | struct field *field; |
Hans Leidekker | 719a789 | 2004-09-22 02:46:38 +0000 | [diff] [blame] | 327 | unsigned int i; |
| 328 | int dirid; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 329 | WCHAR *dirid_str, *end; |
| 330 | const WCHAR *ret = NULL; |
| 331 | |
| 332 | if (!*len) /* empty string (%%) is replaced by single percent */ |
| 333 | { |
| 334 | *len = 1; |
| 335 | return &percent; |
| 336 | } |
| 337 | if (file->strings_section == -1) goto not_found; |
| 338 | strings_section = file->sections[file->strings_section]; |
| 339 | for (i = 0, line = strings_section->lines; i < strings_section->nb_lines; i++, line++) |
| 340 | { |
| 341 | if (line->key_field == -1) continue; |
| 342 | if (strncmpiW( str, file->fields[line->key_field].text, *len )) continue; |
| 343 | if (!file->fields[line->key_field].text[*len]) break; |
| 344 | } |
| 345 | if (i == strings_section->nb_lines || !line->nb_fields) goto not_found; |
| 346 | field = &file->fields[line->first_field]; |
| 347 | *len = strlenW( field->text ); |
| 348 | return field->text; |
| 349 | |
| 350 | not_found: /* check for integer id */ |
| 351 | if ((dirid_str = HeapAlloc( GetProcessHeap(), 0, (*len+1) * sizeof(WCHAR) ))) |
| 352 | { |
| 353 | memcpy( dirid_str, str, *len * sizeof(WCHAR) ); |
| 354 | dirid_str[*len] = 0; |
Alexandre Julliard | 9b6f433 | 2002-05-16 18:58:47 +0000 | [diff] [blame] | 355 | dirid = strtolW( dirid_str, &end, 10 ); |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 356 | if (!*end) ret = get_dirid_subst( file, dirid, len ); |
Jason Edmeades | 7f901f4 | 2007-08-10 22:28:35 +0100 | [diff] [blame] | 357 | if (no_trailing_slash && ret && *len && ret[*len - 1] == '\\') *len -= 1; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 358 | HeapFree( GetProcessHeap(), 0, dirid_str ); |
| 359 | return ret; |
| 360 | } |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /* do string substitutions on the specified text */ |
| 366 | /* the buffer is assumed to be large enough */ |
| 367 | /* returns necessary length not including terminating null */ |
Andrew Talbot | f34e733 | 2009-01-24 13:27:07 +0000 | [diff] [blame] | 368 | static unsigned int PARSER_string_substW( const struct inf_file *file, const WCHAR *text, |
| 369 | WCHAR *buffer, unsigned int size ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 370 | { |
| 371 | const WCHAR *start, *subst, *p; |
| 372 | unsigned int len, total = 0; |
| 373 | int inside = 0; |
| 374 | |
| 375 | if (!buffer) size = MAX_STRING_LEN + 1; |
| 376 | for (p = start = text; *p; p++) |
| 377 | { |
| 378 | if (*p != '%') continue; |
| 379 | inside = !inside; |
| 380 | if (inside) /* start of a %xx% string */ |
| 381 | { |
| 382 | len = p - start; |
| 383 | if (len > size - 1) len = size - 1; |
| 384 | if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) ); |
| 385 | total += len; |
| 386 | size -= len; |
| 387 | start = p; |
| 388 | } |
| 389 | else /* end of the %xx% string, find substitution */ |
| 390 | { |
| 391 | len = p - start - 1; |
Jason Edmeades | 7f901f4 | 2007-08-10 22:28:35 +0100 | [diff] [blame] | 392 | subst = get_string_subst( file, start + 1, &len, p[1] == '\\' ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 393 | if (!subst) |
| 394 | { |
| 395 | subst = start; |
| 396 | len = p - start + 1; |
| 397 | } |
| 398 | if (len > size - 1) len = size - 1; |
| 399 | if (buffer) memcpy( buffer + total, subst, len * sizeof(WCHAR) ); |
| 400 | total += len; |
| 401 | size -= len; |
| 402 | start = p + 1; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (start != p) /* unfinished string, copy it */ |
| 407 | { |
| 408 | len = p - start; |
| 409 | if (len > size - 1) len = size - 1; |
| 410 | if (buffer) memcpy( buffer + total, start, len * sizeof(WCHAR) ); |
| 411 | total += len; |
| 412 | } |
| 413 | if (buffer && size) buffer[total] = 0; |
| 414 | return total; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | /* do string substitutions on the specified text */ |
| 419 | /* the buffer is assumed to be large enough */ |
| 420 | /* returns necessary length not including terminating null */ |
Alexandre Julliard | b70bbe3 | 2009-10-01 19:44:43 +0200 | [diff] [blame] | 421 | static unsigned int PARSER_string_substA( const struct inf_file *file, const WCHAR *text, |
| 422 | char *buffer, unsigned int size ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 423 | { |
| 424 | WCHAR buffW[MAX_STRING_LEN+1]; |
| 425 | DWORD ret; |
| 426 | |
| 427 | unsigned int len = PARSER_string_substW( file, text, buffW, sizeof(buffW)/sizeof(WCHAR) ); |
| 428 | if (!buffer) RtlUnicodeToMultiByteSize( &ret, buffW, len * sizeof(WCHAR) ); |
| 429 | else |
| 430 | { |
| 431 | RtlUnicodeToMultiByteN( buffer, size-1, &ret, buffW, len * sizeof(WCHAR) ); |
| 432 | buffer[ret] = 0; |
| 433 | } |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | |
| 438 | /* push some string data into the strings buffer */ |
| 439 | static WCHAR *push_string( struct inf_file *file, const WCHAR *string ) |
| 440 | { |
| 441 | WCHAR *ret = file->string_pos; |
| 442 | strcpyW( ret, string ); |
| 443 | file->string_pos += strlenW( ret ) + 1; |
| 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | |
| 448 | /* push the current state on the parser stack */ |
Andrew Talbot | a89d5de | 2007-03-19 19:48:48 +0000 | [diff] [blame] | 449 | static inline void push_state( struct parser *parser, enum parser_state state ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 450 | { |
| 451 | assert( parser->stack_pos < sizeof(parser->stack)/sizeof(parser->stack[0]) ); |
| 452 | parser->stack[parser->stack_pos++] = state; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | /* pop the current state */ |
Andrew Talbot | a89d5de | 2007-03-19 19:48:48 +0000 | [diff] [blame] | 457 | static inline void pop_state( struct parser *parser ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 458 | { |
| 459 | assert( parser->stack_pos ); |
| 460 | parser->state = parser->stack[--parser->stack_pos]; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /* set the parser state and return the previous one */ |
Andrew Talbot | a89d5de | 2007-03-19 19:48:48 +0000 | [diff] [blame] | 465 | static inline enum parser_state set_state( struct parser *parser, enum parser_state state ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 466 | { |
| 467 | enum parser_state ret = parser->state; |
| 468 | parser->state = state; |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* check if the pointer points to an end of file */ |
Andrew Talbot | 3bf6315 | 2007-08-27 16:09:06 +0100 | [diff] [blame] | 474 | static inline int is_eof( const struct parser *parser, const WCHAR *ptr ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 475 | { |
| 476 | return (ptr >= parser->end || *ptr == CONTROL_Z); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /* check if the pointer points to an end of line */ |
Andrew Talbot | 3bf6315 | 2007-08-27 16:09:06 +0100 | [diff] [blame] | 481 | static inline int is_eol( const struct parser *parser, const WCHAR *ptr ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 482 | { |
| 483 | return (ptr >= parser->end || *ptr == CONTROL_Z || *ptr == '\n'); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | /* push data from current token start up to pos into the current token */ |
| 488 | static int push_token( struct parser *parser, const WCHAR *pos ) |
| 489 | { |
| 490 | int len = pos - parser->start; |
| 491 | const WCHAR *src = parser->start; |
| 492 | WCHAR *dst = parser->token + parser->token_len; |
| 493 | |
| 494 | if (len > MAX_FIELD_LEN - parser->token_len) len = MAX_FIELD_LEN - parser->token_len; |
| 495 | |
| 496 | parser->token_len += len; |
| 497 | for ( ; len > 0; len--, dst++, src++) *dst = *src ? *src : ' '; |
| 498 | *dst = 0; |
| 499 | parser->start = pos; |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /* add a section with the current token as name */ |
| 505 | static int add_section_from_token( struct parser *parser ) |
| 506 | { |
| 507 | int section_index; |
| 508 | |
| 509 | if (parser->token_len > MAX_SECTION_NAME_LEN) |
| 510 | { |
| 511 | parser->error = ERROR_SECTION_NAME_TOO_LONG; |
| 512 | return -1; |
| 513 | } |
| 514 | if ((section_index = find_section( parser->file, parser->token )) == -1) |
| 515 | { |
| 516 | /* need to create a new one */ |
| 517 | const WCHAR *name = push_string( parser->file, parser->token ); |
| 518 | if ((section_index = add_section( parser->file, name )) == -1) |
| 519 | { |
| 520 | parser->error = ERROR_NOT_ENOUGH_MEMORY; |
| 521 | return -1; |
| 522 | } |
| 523 | } |
| 524 | parser->token_len = 0; |
| 525 | parser->cur_section = section_index; |
| 526 | return section_index; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | /* add a field containing the current token to the current line */ |
| 531 | static struct field *add_field_from_token( struct parser *parser, int is_key ) |
| 532 | { |
| 533 | struct field *field; |
| 534 | WCHAR *text; |
| 535 | |
| 536 | if (!parser->line) /* need to start a new line */ |
| 537 | { |
| 538 | if (parser->cur_section == -1) /* got a line before the first section */ |
| 539 | { |
Alexandre Julliard | 6c0fc42 | 2005-11-07 16:41:37 +0000 | [diff] [blame] | 540 | parser->error = ERROR_EXPECTED_SECTION_NAME; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 541 | return NULL; |
| 542 | } |
| 543 | if (!(parser->line = add_line( parser->file, parser->cur_section ))) goto error; |
| 544 | } |
| 545 | else assert(!is_key); |
| 546 | |
| 547 | text = push_string( parser->file, parser->token ); |
| 548 | if ((field = add_field( parser->file, text ))) |
| 549 | { |
| 550 | if (!is_key) parser->line->nb_fields++; |
| 551 | else |
| 552 | { |
| 553 | /* replace first field by key field */ |
| 554 | parser->line->key_field = parser->line->first_field; |
| 555 | parser->line->first_field++; |
| 556 | } |
| 557 | parser->token_len = 0; |
| 558 | return field; |
| 559 | } |
| 560 | error: |
| 561 | parser->error = ERROR_NOT_ENOUGH_MEMORY; |
| 562 | return NULL; |
| 563 | } |
| 564 | |
| 565 | |
Alexandre Julliard | 6f27910 | 2002-06-13 21:52:44 +0000 | [diff] [blame] | 566 | /* close the current line and prepare for parsing a new one */ |
| 567 | static void close_current_line( struct parser *parser ) |
| 568 | { |
| 569 | struct line *cur_line = parser->line; |
| 570 | |
| 571 | if (cur_line) |
| 572 | { |
| 573 | /* if line has a single field and no key, the field is the key too */ |
| 574 | if (cur_line->nb_fields == 1 && cur_line->key_field == -1) |
| 575 | cur_line->key_field = cur_line->first_field; |
| 576 | } |
| 577 | parser->line = NULL; |
| 578 | } |
| 579 | |
| 580 | |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 581 | /* handler for parser LINE_START state */ |
| 582 | static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos ) |
| 583 | { |
| 584 | const WCHAR *p; |
| 585 | |
| 586 | for (p = pos; !is_eof( parser, p ); p++) |
| 587 | { |
| 588 | switch(*p) |
| 589 | { |
| 590 | case '\n': |
| 591 | parser->line_pos++; |
Alexandre Julliard | 6f27910 | 2002-06-13 21:52:44 +0000 | [diff] [blame] | 592 | close_current_line( parser ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 593 | break; |
| 594 | case ';': |
| 595 | push_state( parser, LINE_START ); |
| 596 | set_state( parser, COMMENT ); |
| 597 | return p + 1; |
| 598 | case '[': |
| 599 | parser->start = p + 1; |
| 600 | set_state( parser, SECTION_NAME ); |
| 601 | return p + 1; |
| 602 | default: |
| 603 | if (!isspaceW(*p)) |
| 604 | { |
| 605 | parser->start = p; |
| 606 | set_state( parser, KEY_NAME ); |
| 607 | return p; |
| 608 | } |
| 609 | break; |
| 610 | } |
| 611 | } |
Alexandre Julliard | 6f27910 | 2002-06-13 21:52:44 +0000 | [diff] [blame] | 612 | close_current_line( parser ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | /* handler for parser SECTION_NAME state */ |
| 618 | static const WCHAR *section_name_state( struct parser *parser, const WCHAR *pos ) |
| 619 | { |
| 620 | const WCHAR *p; |
| 621 | |
| 622 | for (p = pos; !is_eol( parser, p ); p++) |
| 623 | { |
| 624 | if (*p == ']') |
| 625 | { |
| 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 */ |
| 630 | return p + 1; |
| 631 | } |
| 632 | } |
| 633 | parser->error = ERROR_BAD_SECTION_NAME_LINE; /* unfinished section name */ |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /* handler for parser KEY_NAME state */ |
| 639 | static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos ) |
| 640 | { |
| 641 | const WCHAR *p, *token_end = parser->start; |
| 642 | |
| 643 | for (p = pos; !is_eol( parser, p ); p++) |
| 644 | { |
| 645 | if (*p == ',') break; |
| 646 | switch(*p) |
| 647 | { |
| 648 | |
| 649 | case '=': |
| 650 | push_token( parser, token_end ); |
| 651 | if (!add_field_from_token( parser, 1 )) return NULL; |
| 652 | parser->start = p + 1; |
| 653 | push_state( parser, VALUE_NAME ); |
| 654 | set_state( parser, LEADING_SPACES ); |
| 655 | return p + 1; |
| 656 | case ';': |
| 657 | push_token( parser, token_end ); |
| 658 | if (!add_field_from_token( parser, 0 )) return NULL; |
| 659 | push_state( parser, LINE_START ); |
| 660 | set_state( parser, COMMENT ); |
| 661 | return p + 1; |
| 662 | case '"': |
Alexandre Julliard | 4ed08c4 | 2005-07-22 18:30:53 +0000 | [diff] [blame] | 663 | push_token( parser, p ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 664 | parser->start = p + 1; |
| 665 | push_state( parser, KEY_NAME ); |
| 666 | set_state( parser, QUOTES ); |
| 667 | return p + 1; |
| 668 | case '\\': |
| 669 | push_token( parser, token_end ); |
| 670 | parser->start = p; |
| 671 | push_state( parser, KEY_NAME ); |
| 672 | set_state( parser, EOL_BACKSLASH ); |
| 673 | return p; |
| 674 | default: |
| 675 | if (!isspaceW(*p)) token_end = p + 1; |
| 676 | else |
| 677 | { |
| 678 | push_token( parser, p ); |
| 679 | push_state( parser, KEY_NAME ); |
| 680 | set_state( parser, TRAILING_SPACES ); |
| 681 | return p; |
| 682 | } |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | push_token( parser, token_end ); |
| 687 | set_state( parser, VALUE_NAME ); |
| 688 | return p; |
| 689 | } |
| 690 | |
| 691 | |
| 692 | /* handler for parser VALUE_NAME state */ |
| 693 | static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos ) |
| 694 | { |
| 695 | const WCHAR *p, *token_end = parser->start; |
| 696 | |
| 697 | for (p = pos; !is_eol( parser, p ); p++) |
| 698 | { |
| 699 | switch(*p) |
| 700 | { |
| 701 | case ';': |
| 702 | push_token( parser, token_end ); |
| 703 | if (!add_field_from_token( parser, 0 )) return NULL; |
| 704 | push_state( parser, LINE_START ); |
| 705 | set_state( parser, COMMENT ); |
| 706 | return p + 1; |
| 707 | case ',': |
| 708 | push_token( parser, token_end ); |
| 709 | if (!add_field_from_token( parser, 0 )) return NULL; |
| 710 | parser->start = p + 1; |
| 711 | push_state( parser, VALUE_NAME ); |
| 712 | set_state( parser, LEADING_SPACES ); |
| 713 | return p + 1; |
| 714 | case '"': |
Alexandre Julliard | 4ed08c4 | 2005-07-22 18:30:53 +0000 | [diff] [blame] | 715 | push_token( parser, p ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 716 | parser->start = p + 1; |
| 717 | push_state( parser, VALUE_NAME ); |
| 718 | set_state( parser, QUOTES ); |
| 719 | return p + 1; |
| 720 | case '\\': |
| 721 | push_token( parser, token_end ); |
| 722 | parser->start = p; |
| 723 | push_state( parser, VALUE_NAME ); |
| 724 | set_state( parser, EOL_BACKSLASH ); |
| 725 | return p; |
| 726 | default: |
| 727 | if (!isspaceW(*p)) token_end = p + 1; |
| 728 | else |
| 729 | { |
| 730 | push_token( parser, p ); |
| 731 | push_state( parser, VALUE_NAME ); |
| 732 | set_state( parser, TRAILING_SPACES ); |
| 733 | return p; |
| 734 | } |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | push_token( parser, token_end ); |
| 739 | if (!add_field_from_token( parser, 0 )) return NULL; |
| 740 | set_state( parser, LINE_START ); |
| 741 | return p; |
| 742 | } |
| 743 | |
| 744 | |
| 745 | /* handler for parser EOL_BACKSLASH state */ |
| 746 | static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos ) |
| 747 | { |
| 748 | const WCHAR *p; |
| 749 | |
| 750 | for (p = pos; !is_eof( parser, p ); p++) |
| 751 | { |
| 752 | switch(*p) |
| 753 | { |
| 754 | case '\n': |
| 755 | parser->line_pos++; |
| 756 | parser->start = p + 1; |
| 757 | set_state( parser, LEADING_SPACES ); |
| 758 | return p + 1; |
| 759 | case '\\': |
| 760 | continue; |
| 761 | case ';': |
| 762 | push_state( parser, EOL_BACKSLASH ); |
| 763 | set_state( parser, COMMENT ); |
| 764 | return p + 1; |
| 765 | default: |
| 766 | if (isspaceW(*p)) continue; |
| 767 | push_token( parser, p ); |
| 768 | pop_state( parser ); |
| 769 | return p; |
| 770 | } |
| 771 | } |
| 772 | parser->start = p; |
| 773 | pop_state( parser ); |
| 774 | return p; |
| 775 | } |
| 776 | |
| 777 | |
| 778 | /* handler for parser QUOTES state */ |
| 779 | static const WCHAR *quotes_state( struct parser *parser, const WCHAR *pos ) |
| 780 | { |
Andrew Talbot | 362cd67 | 2008-04-24 21:43:51 +0100 | [diff] [blame] | 781 | const WCHAR *p; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 782 | |
| 783 | for (p = pos; !is_eol( parser, p ); p++) |
| 784 | { |
| 785 | if (*p == '"') |
| 786 | { |
| 787 | if (p+1 < parser->end && p[1] == '"') /* double quotes */ |
| 788 | { |
| 789 | push_token( parser, p + 1 ); |
Andrew Talbot | 362cd67 | 2008-04-24 21:43:51 +0100 | [diff] [blame] | 790 | parser->start = p + 2; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 791 | p++; |
| 792 | } |
| 793 | else /* end of quotes */ |
| 794 | { |
| 795 | push_token( parser, p ); |
| 796 | parser->start = p + 1; |
| 797 | pop_state( parser ); |
| 798 | return p + 1; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | push_token( parser, p ); |
| 803 | pop_state( parser ); |
| 804 | return p; |
| 805 | } |
| 806 | |
| 807 | |
| 808 | /* handler for parser LEADING_SPACES state */ |
| 809 | static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *pos ) |
| 810 | { |
| 811 | const WCHAR *p; |
| 812 | |
| 813 | for (p = pos; !is_eol( parser, p ); p++) |
| 814 | { |
| 815 | if (*p == '\\') |
| 816 | { |
| 817 | parser->start = p; |
| 818 | set_state( parser, EOL_BACKSLASH ); |
| 819 | return p; |
| 820 | } |
| 821 | if (!isspaceW(*p)) break; |
| 822 | } |
| 823 | parser->start = p; |
| 824 | pop_state( parser ); |
| 825 | return p; |
| 826 | } |
| 827 | |
| 828 | |
| 829 | /* handler for parser TRAILING_SPACES state */ |
| 830 | static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *pos ) |
| 831 | { |
| 832 | const WCHAR *p; |
| 833 | |
| 834 | for (p = pos; !is_eol( parser, p ); p++) |
| 835 | { |
| 836 | if (*p == '\\') |
| 837 | { |
| 838 | set_state( parser, EOL_BACKSLASH ); |
| 839 | return p; |
| 840 | } |
| 841 | if (!isspaceW(*p)) break; |
| 842 | } |
| 843 | pop_state( parser ); |
| 844 | return p; |
| 845 | } |
| 846 | |
| 847 | |
| 848 | /* handler for parser COMMENT state */ |
| 849 | static const WCHAR *comment_state( struct parser *parser, const WCHAR *pos ) |
| 850 | { |
| 851 | const WCHAR *p = pos; |
| 852 | |
| 853 | while (!is_eol( parser, p )) p++; |
| 854 | pop_state( parser ); |
| 855 | return p; |
| 856 | } |
| 857 | |
| 858 | |
James Hawkins | b116175 | 2010-01-06 18:26:52 -0800 | [diff] [blame] | 859 | static void free_inf_file( struct inf_file *file ) |
| 860 | { |
| 861 | unsigned int i; |
| 862 | |
| 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 ); |
| 869 | } |
| 870 | |
| 871 | |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 872 | /* parse a complete buffer */ |
| 873 | static DWORD parse_buffer( struct inf_file *file, const WCHAR *buffer, const WCHAR *end, |
| 874 | UINT *error_line ) |
| 875 | { |
| 876 | static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0}; |
| 877 | |
| 878 | struct parser parser; |
| 879 | const WCHAR *pos = buffer; |
| 880 | |
Alexandre Julliard | 4b63ed8 | 2005-06-01 11:05:47 +0000 | [diff] [blame] | 881 | parser.start = buffer; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 882 | parser.end = end; |
| 883 | parser.file = file; |
| 884 | parser.line = NULL; |
| 885 | parser.state = LINE_START; |
| 886 | parser.stack_pos = 0; |
| 887 | parser.cur_section = -1; |
| 888 | parser.line_pos = 1; |
| 889 | parser.error = 0; |
| 890 | parser.token_len = 0; |
| 891 | |
| 892 | /* parser main loop */ |
| 893 | while (pos) pos = (parser_funcs[parser.state])( &parser, pos ); |
| 894 | |
| 895 | /* trim excess buffer space */ |
| 896 | if (file->alloc_sections > file->nb_sections) |
| 897 | { |
| 898 | file->sections = HeapReAlloc( GetProcessHeap(), 0, file->sections, |
| 899 | file->nb_sections * sizeof(file->sections[0]) ); |
| 900 | file->alloc_sections = file->nb_sections; |
| 901 | } |
| 902 | if (file->alloc_fields > file->nb_fields) |
| 903 | { |
| 904 | file->fields = HeapReAlloc( GetProcessHeap(), 0, file->fields, |
| 905 | file->nb_fields * sizeof(file->fields[0]) ); |
| 906 | file->alloc_fields = file->nb_fields; |
| 907 | } |
| 908 | file->strings = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, file->strings, |
| 909 | (file->string_pos - file->strings) * sizeof(WCHAR) ); |
| 910 | |
| 911 | if (parser.error) |
| 912 | { |
| 913 | if (error_line) *error_line = parser.line_pos; |
| 914 | return parser.error; |
| 915 | } |
| 916 | |
| 917 | /* find the [strings] section */ |
| 918 | file->strings_section = find_section( file, Strings ); |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | |
| 923 | /* append a child INF file to its parent list, in a thread-safe manner */ |
| 924 | static void append_inf_file( struct inf_file *parent, struct inf_file *child ) |
| 925 | { |
| 926 | struct inf_file **ppnext = &parent->next; |
| 927 | child->next = NULL; |
| 928 | |
| 929 | for (;;) |
| 930 | { |
| 931 | struct inf_file *next = InterlockedCompareExchangePointer( (void **)ppnext, child, NULL ); |
| 932 | if (!next) return; |
| 933 | ppnext = &next->next; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | |
| 938 | /*********************************************************************** |
| 939 | * parse_file |
| 940 | * |
| 941 | * parse an INF file. |
| 942 | */ |
Hans Leidekker | 3cfb018 | 2007-05-18 20:24:18 +0200 | [diff] [blame] | 943 | static struct inf_file *parse_file( HANDLE handle, const WCHAR *class, DWORD style, UINT *error_line ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 944 | { |
| 945 | void *buffer; |
| 946 | DWORD err = 0; |
| 947 | struct inf_file *file; |
| 948 | |
| 949 | DWORD size = GetFileSize( handle, NULL ); |
| 950 | HANDLE mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, size, NULL ); |
| 951 | if (!mapping) return NULL; |
| 952 | buffer = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, size ); |
| 953 | NtClose( mapping ); |
| 954 | if (!buffer) return NULL; |
| 955 | |
| 956 | if (class) FIXME( "class %s not supported yet\n", debugstr_w(class) ); |
| 957 | |
| 958 | if (!(file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*file) ))) |
| 959 | { |
| 960 | err = ERROR_NOT_ENOUGH_MEMORY; |
| 961 | goto done; |
| 962 | } |
| 963 | |
| 964 | /* we won't need more strings space than the size of the file, |
| 965 | * so we can preallocate it here |
| 966 | */ |
| 967 | if (!(file->strings = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) |
| 968 | { |
| 969 | err = ERROR_NOT_ENOUGH_MEMORY; |
| 970 | goto done; |
| 971 | } |
| 972 | file->string_pos = file->strings; |
| 973 | file->strings_section = -1; |
| 974 | |
| 975 | if (!RtlIsTextUnicode( buffer, size, NULL )) |
| 976 | { |
Alexandre Julliard | 31ade1e | 2007-03-21 13:52:43 +0100 | [diff] [blame] | 977 | static const BYTE utf8_bom[3] = { 0xef, 0xbb, 0xbf }; |
| 978 | WCHAR *new_buff; |
| 979 | UINT codepage = CP_ACP; |
| 980 | UINT offset = 0; |
| 981 | |
| 982 | if (size > sizeof(utf8_bom) && !memcmp( buffer, utf8_bom, sizeof(utf8_bom) )) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 983 | { |
Alexandre Julliard | 31ade1e | 2007-03-21 13:52:43 +0100 | [diff] [blame] | 984 | codepage = CP_UTF8; |
| 985 | offset = sizeof(utf8_bom); |
| 986 | } |
| 987 | |
| 988 | if ((new_buff = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) |
| 989 | { |
| 990 | DWORD len = MultiByteToWideChar( codepage, 0, (char *)buffer + offset, |
| 991 | size - offset, new_buff, size ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 992 | err = parse_buffer( file, new_buff, new_buff + len, error_line ); |
| 993 | HeapFree( GetProcessHeap(), 0, new_buff ); |
| 994 | } |
| 995 | } |
Robert Shearman | 4d79464 | 2006-04-26 15:42:14 +0100 | [diff] [blame] | 996 | else |
| 997 | { |
Michael Stefaniuc | ffae012 | 2009-01-28 10:24:55 +0100 | [diff] [blame] | 998 | WCHAR *new_buff = buffer; |
Robert Shearman | 4d79464 | 2006-04-26 15:42:14 +0100 | [diff] [blame] | 999 | /* UCS-16 files should start with the Unicode BOM; we should skip it */ |
| 1000 | if (*new_buff == 0xfeff) |
| 1001 | new_buff++; |
| 1002 | err = parse_buffer( file, new_buff, (WCHAR *)((char *)buffer + size), error_line ); |
| 1003 | } |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (!err) /* now check signature */ |
| 1006 | { |
| 1007 | int version_index = find_section( file, Version ); |
| 1008 | if (version_index != -1) |
| 1009 | { |
| 1010 | struct line *line = find_line( file, version_index, Signature ); |
| 1011 | if (line && line->nb_fields > 0) |
| 1012 | { |
| 1013 | struct field *field = file->fields + line->first_field; |
| 1014 | if (!strcmpiW( field->text, Chicago )) goto done; |
| 1015 | if (!strcmpiW( field->text, WindowsNT )) goto done; |
| 1016 | if (!strcmpiW( field->text, Windows95 )) goto done; |
| 1017 | } |
| 1018 | } |
Alexandre Julliard | 6c0fc42 | 2005-11-07 16:41:37 +0000 | [diff] [blame] | 1019 | if (error_line) *error_line = 0; |
Hans Leidekker | 3cfb018 | 2007-05-18 20:24:18 +0200 | [diff] [blame] | 1020 | if (style & INF_STYLE_WIN4) err = ERROR_WRONG_INF_STYLE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | done: |
| 1024 | UnmapViewOfFile( buffer ); |
| 1025 | if (err) |
| 1026 | { |
Marcus Meissner | 1b27af1 | 2010-01-08 09:20:34 +0100 | [diff] [blame] | 1027 | if (file) free_inf_file( file ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1028 | SetLastError( err ); |
| 1029 | file = NULL; |
| 1030 | } |
| 1031 | return file; |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | /*********************************************************************** |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 1036 | * PARSER_get_inf_filename |
| 1037 | * |
| 1038 | * Retrieve the filename of an inf file. |
| 1039 | */ |
| 1040 | const WCHAR *PARSER_get_inf_filename( HINF hinf ) |
| 1041 | { |
| 1042 | struct inf_file *file = hinf; |
| 1043 | return file->filename; |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | /*********************************************************************** |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1048 | * PARSER_get_src_root |
| 1049 | * |
| 1050 | * Retrieve the source directory of an inf file. |
| 1051 | */ |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 1052 | WCHAR *PARSER_get_src_root( HINF hinf ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1053 | { |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 1054 | unsigned int len; |
| 1055 | const WCHAR *dir = get_inf_dir( hinf, &len ); |
| 1056 | WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ); |
| 1057 | if (ret) |
| 1058 | { |
| 1059 | memcpy( ret, dir, len * sizeof(WCHAR) ); |
| 1060 | ret[len] = 0; |
| 1061 | } |
| 1062 | return ret; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | /*********************************************************************** |
Alexandre Julliard | cb28348 | 2004-02-27 00:47:10 +0000 | [diff] [blame] | 1067 | * PARSER_get_dest_dir |
| 1068 | * |
| 1069 | * retrieve a destination dir of the form "dirid,relative_path" in the given entry. |
| 1070 | * returned buffer must be freed by caller. |
| 1071 | */ |
| 1072 | WCHAR *PARSER_get_dest_dir( INFCONTEXT *context ) |
| 1073 | { |
| 1074 | const WCHAR *dir; |
| 1075 | WCHAR *ptr, *ret; |
| 1076 | INT dirid; |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 1077 | unsigned int len1; |
| 1078 | DWORD len2; |
Alexandre Julliard | cb28348 | 2004-02-27 00:47:10 +0000 | [diff] [blame] | 1079 | |
| 1080 | if (!SetupGetIntField( context, 1, &dirid )) return NULL; |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 1081 | if (!(dir = get_dirid_subst( context->Inf, dirid, &len1 ))) return NULL; |
Alexandre Julliard | cb28348 | 2004-02-27 00:47:10 +0000 | [diff] [blame] | 1082 | if (!SetupGetStringFieldW( context, 2, NULL, 0, &len2 )) len2 = 0; |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 1083 | if (!(ret = HeapAlloc( GetProcessHeap(), 0, (len1+len2+1) * sizeof(WCHAR) ))) return NULL; |
| 1084 | memcpy( ret, dir, len1 * sizeof(WCHAR) ); |
| 1085 | ptr = ret + len1; |
Alexandre Julliard | cb28348 | 2004-02-27 00:47:10 +0000 | [diff] [blame] | 1086 | if (len2 && ptr > ret && ptr[-1] != '\\') *ptr++ = '\\'; |
| 1087 | if (!SetupGetStringFieldW( context, 2, ptr, len2, NULL )) *ptr = 0; |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /*********************************************************************** |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1093 | * SetupOpenInfFileA (SETUPAPI.@) |
| 1094 | */ |
| 1095 | HINF WINAPI SetupOpenInfFileA( PCSTR name, PCSTR class, DWORD style, UINT *error ) |
| 1096 | { |
| 1097 | UNICODE_STRING nameW, classW; |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1098 | HINF ret = INVALID_HANDLE_VALUE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1099 | |
| 1100 | classW.Buffer = NULL; |
| 1101 | if (class && !RtlCreateUnicodeStringFromAsciiz( &classW, class )) |
| 1102 | { |
| 1103 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 1104 | return ret; |
| 1105 | } |
| 1106 | if (RtlCreateUnicodeStringFromAsciiz( &nameW, name )) |
| 1107 | { |
| 1108 | ret = SetupOpenInfFileW( nameW.Buffer, classW.Buffer, style, error ); |
| 1109 | RtlFreeUnicodeString( &nameW ); |
| 1110 | } |
| 1111 | RtlFreeUnicodeString( &classW ); |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | /*********************************************************************** |
| 1117 | * SetupOpenInfFileW (SETUPAPI.@) |
| 1118 | */ |
| 1119 | HINF WINAPI SetupOpenInfFileW( PCWSTR name, PCWSTR class, DWORD style, UINT *error ) |
| 1120 | { |
| 1121 | struct inf_file *file = NULL; |
| 1122 | HANDLE handle; |
| 1123 | WCHAR *path, *p; |
| 1124 | UINT len; |
| 1125 | |
| 1126 | if (strchrW( name, '\\' ) || strchrW( name, '/' )) |
| 1127 | { |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1128 | if (!(len = GetFullPathNameW( name, 0, NULL, NULL ))) return INVALID_HANDLE_VALUE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1129 | if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) |
| 1130 | { |
| 1131 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1132 | return INVALID_HANDLE_VALUE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1133 | } |
| 1134 | GetFullPathNameW( name, len, path, NULL ); |
| 1135 | handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 ); |
| 1136 | } |
| 1137 | else /* try Windows directory */ |
| 1138 | { |
| 1139 | static const WCHAR Inf[] = {'\\','i','n','f','\\',0}; |
| 1140 | static const WCHAR System32[] = {'\\','s','y','s','t','e','m','3','2','\\',0}; |
| 1141 | |
| 1142 | len = GetWindowsDirectoryW( NULL, 0 ) + strlenW(name) + 12; |
| 1143 | if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) |
| 1144 | { |
| 1145 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1146 | return INVALID_HANDLE_VALUE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1147 | } |
| 1148 | GetWindowsDirectoryW( path, len ); |
| 1149 | p = path + strlenW(path); |
| 1150 | strcpyW( p, Inf ); |
| 1151 | strcatW( p, name ); |
| 1152 | handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 ); |
| 1153 | if (handle == INVALID_HANDLE_VALUE) |
| 1154 | { |
| 1155 | strcpyW( p, System32 ); |
| 1156 | strcatW( p, name ); |
| 1157 | handle = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 ); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | if (handle != INVALID_HANDLE_VALUE) |
| 1162 | { |
Hans Leidekker | 3cfb018 | 2007-05-18 20:24:18 +0200 | [diff] [blame] | 1163 | file = parse_file( handle, class, style, error ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1164 | CloseHandle( handle ); |
| 1165 | } |
| 1166 | if (!file) |
| 1167 | { |
| 1168 | HeapFree( GetProcessHeap(), 0, path ); |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1169 | return INVALID_HANDLE_VALUE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1170 | } |
| 1171 | TRACE( "%s -> %p\n", debugstr_w(path), file ); |
Alexandre Julliard | 6cb7ca4 | 2006-02-07 16:08:19 +0100 | [diff] [blame] | 1172 | file->filename = path; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1173 | SetLastError( 0 ); |
Michael Stefaniuc | 02d1154 | 2008-12-08 04:06:20 +0100 | [diff] [blame] | 1174 | return file; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | /*********************************************************************** |
| 1179 | * SetupOpenAppendInfFileA (SETUPAPI.@) |
| 1180 | */ |
| 1181 | BOOL WINAPI SetupOpenAppendInfFileA( PCSTR name, HINF parent_hinf, UINT *error ) |
| 1182 | { |
| 1183 | HINF child_hinf; |
| 1184 | |
| 1185 | if (!name) return SetupOpenAppendInfFileW( NULL, parent_hinf, error ); |
| 1186 | child_hinf = SetupOpenInfFileA( name, NULL, INF_STYLE_WIN4, error ); |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1187 | if (child_hinf == INVALID_HANDLE_VALUE) return FALSE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1188 | append_inf_file( parent_hinf, child_hinf ); |
| 1189 | TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_a(name), child_hinf ); |
| 1190 | return TRUE; |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | /*********************************************************************** |
| 1195 | * SetupOpenAppendInfFileW (SETUPAPI.@) |
| 1196 | */ |
| 1197 | BOOL WINAPI SetupOpenAppendInfFileW( PCWSTR name, HINF parent_hinf, UINT *error ) |
| 1198 | { |
| 1199 | HINF child_hinf; |
| 1200 | |
| 1201 | if (!name) |
| 1202 | { |
| 1203 | INFCONTEXT context; |
| 1204 | WCHAR filename[MAX_PATH]; |
| 1205 | int idx = 1; |
| 1206 | |
| 1207 | if (!SetupFindFirstLineW( parent_hinf, Version, LayoutFile, &context )) return FALSE; |
| 1208 | while (SetupGetStringFieldW( &context, idx++, filename, |
| 1209 | sizeof(filename)/sizeof(WCHAR), NULL )) |
| 1210 | { |
| 1211 | child_hinf = SetupOpenInfFileW( filename, NULL, INF_STYLE_WIN4, error ); |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1212 | if (child_hinf == INVALID_HANDLE_VALUE) return FALSE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1213 | append_inf_file( parent_hinf, child_hinf ); |
| 1214 | TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(filename), child_hinf ); |
| 1215 | } |
| 1216 | return TRUE; |
| 1217 | } |
| 1218 | child_hinf = SetupOpenInfFileW( name, NULL, INF_STYLE_WIN4, error ); |
Andrew Talbot | e48bbc2 | 2008-01-19 14:56:39 +0000 | [diff] [blame] | 1219 | if (child_hinf == INVALID_HANDLE_VALUE) return FALSE; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1220 | append_inf_file( parent_hinf, child_hinf ); |
| 1221 | TRACE( "%p: appended %s (%p)\n", parent_hinf, debugstr_w(name), child_hinf ); |
| 1222 | return TRUE; |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | /*********************************************************************** |
Eric Kohl | faa7df1 | 2004-12-02 18:09:53 +0000 | [diff] [blame] | 1227 | * SetupOpenMasterInf (SETUPAPI.@) |
| 1228 | */ |
| 1229 | HINF WINAPI SetupOpenMasterInf( VOID ) |
| 1230 | { |
| 1231 | static const WCHAR Layout[] = {'\\','i','n','f','\\', 'l', 'a', 'y', 'o', 'u', 't', '.', 'i', 'n', 'f', 0}; |
| 1232 | WCHAR Buffer[MAX_PATH]; |
| 1233 | |
| 1234 | GetWindowsDirectoryW( Buffer, MAX_PATH ); |
| 1235 | strcatW( Buffer, Layout ); |
| 1236 | return SetupOpenInfFileW( Buffer, NULL, INF_STYLE_WIN4, NULL); |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | /*********************************************************************** |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1242 | * SetupCloseInfFile (SETUPAPI.@) |
| 1243 | */ |
| 1244 | void WINAPI SetupCloseInfFile( HINF hinf ) |
| 1245 | { |
| 1246 | struct inf_file *file = hinf; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1247 | |
Detlef Riekenberg | 8be4e85 | 2007-07-09 19:31:23 +0200 | [diff] [blame] | 1248 | if (!hinf || (hinf == INVALID_HANDLE_VALUE)) return; |
James Hawkins | bce4416 | 2006-06-26 14:46:04 -0700 | [diff] [blame] | 1249 | |
James Hawkins | b116175 | 2010-01-06 18:26:52 -0800 | [diff] [blame] | 1250 | free_inf_file( file ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | /*********************************************************************** |
Alexandre Julliard | b45cfc1 | 2009-01-05 20:26:50 +0100 | [diff] [blame] | 1255 | * SetupEnumInfSectionsA (SETUPAPI.@) |
| 1256 | */ |
| 1257 | BOOL WINAPI SetupEnumInfSectionsA( HINF hinf, UINT index, PSTR buffer, DWORD size, DWORD *need ) |
| 1258 | { |
| 1259 | struct inf_file *file = hinf; |
| 1260 | |
| 1261 | for (file = hinf; file; file = file->next) |
| 1262 | { |
| 1263 | if (index < file->nb_sections) |
| 1264 | { |
| 1265 | DWORD len = WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1, |
| 1266 | NULL, 0, NULL, NULL ); |
Rico Schüller | 619a74e | 2009-01-11 16:06:02 +0100 | [diff] [blame] | 1267 | if (need) *need = len; |
Alexandre Julliard | b45cfc1 | 2009-01-05 20:26:50 +0100 | [diff] [blame] | 1268 | if (!buffer) |
| 1269 | { |
| 1270 | if (!size) return TRUE; |
| 1271 | SetLastError( ERROR_INVALID_USER_BUFFER ); |
| 1272 | return FALSE; |
| 1273 | } |
| 1274 | if (len > size) |
| 1275 | { |
| 1276 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1277 | return FALSE; |
| 1278 | } |
| 1279 | WideCharToMultiByte( CP_ACP, 0, file->sections[index]->name, -1, buffer, size, NULL, NULL ); |
| 1280 | return TRUE; |
| 1281 | } |
| 1282 | index -= file->nb_sections; |
| 1283 | } |
| 1284 | SetLastError( ERROR_NO_MORE_ITEMS ); |
| 1285 | return FALSE; |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | /*********************************************************************** |
| 1290 | * SetupEnumInfSectionsW (SETUPAPI.@) |
| 1291 | */ |
| 1292 | BOOL WINAPI SetupEnumInfSectionsW( HINF hinf, UINT index, PWSTR buffer, DWORD size, DWORD *need ) |
| 1293 | { |
| 1294 | struct inf_file *file = hinf; |
| 1295 | |
| 1296 | for (file = hinf; file; file = file->next) |
| 1297 | { |
| 1298 | if (index < file->nb_sections) |
| 1299 | { |
| 1300 | DWORD len = strlenW( file->sections[index]->name ) + 1; |
Rico Schüller | 619a74e | 2009-01-11 16:06:02 +0100 | [diff] [blame] | 1301 | if (need) *need = len; |
Alexandre Julliard | b45cfc1 | 2009-01-05 20:26:50 +0100 | [diff] [blame] | 1302 | if (!buffer) |
| 1303 | { |
| 1304 | if (!size) return TRUE; |
| 1305 | SetLastError( ERROR_INVALID_USER_BUFFER ); |
| 1306 | return FALSE; |
| 1307 | } |
| 1308 | if (len > size) |
| 1309 | { |
| 1310 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1311 | return FALSE; |
| 1312 | } |
| 1313 | memcpy( buffer, file->sections[index]->name, len * sizeof(WCHAR) ); |
| 1314 | return TRUE; |
| 1315 | } |
| 1316 | index -= file->nb_sections; |
| 1317 | } |
| 1318 | SetLastError( ERROR_NO_MORE_ITEMS ); |
| 1319 | return FALSE; |
| 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | /*********************************************************************** |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1324 | * SetupGetLineCountA (SETUPAPI.@) |
| 1325 | */ |
| 1326 | LONG WINAPI SetupGetLineCountA( HINF hinf, PCSTR name ) |
| 1327 | { |
| 1328 | UNICODE_STRING sectionW; |
| 1329 | LONG ret = -1; |
| 1330 | |
| 1331 | if (!RtlCreateUnicodeStringFromAsciiz( §ionW, name )) |
| 1332 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 1333 | else |
| 1334 | { |
| 1335 | ret = SetupGetLineCountW( hinf, sectionW.Buffer ); |
| 1336 | RtlFreeUnicodeString( §ionW ); |
| 1337 | } |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | /*********************************************************************** |
| 1343 | * SetupGetLineCountW (SETUPAPI.@) |
| 1344 | */ |
| 1345 | LONG WINAPI SetupGetLineCountW( HINF hinf, PCWSTR section ) |
| 1346 | { |
| 1347 | struct inf_file *file = hinf; |
| 1348 | int section_index; |
| 1349 | LONG ret = -1; |
| 1350 | |
| 1351 | for (file = hinf; file; file = file->next) |
| 1352 | { |
| 1353 | if ((section_index = find_section( file, section )) == -1) continue; |
| 1354 | if (ret == -1) ret = 0; |
| 1355 | ret += file->sections[section_index]->nb_lines; |
| 1356 | } |
Michael Stefaniuc | 79ecfaf | 2006-10-13 23:32:50 +0200 | [diff] [blame] | 1357 | TRACE( "(%p,%s) returning %d\n", hinf, debugstr_w(section), ret ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1358 | SetLastError( (ret == -1) ? ERROR_SECTION_NOT_FOUND : 0 ); |
| 1359 | return ret; |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | /*********************************************************************** |
| 1364 | * SetupGetLineByIndexA (SETUPAPI.@) |
| 1365 | */ |
| 1366 | BOOL WINAPI SetupGetLineByIndexA( HINF hinf, PCSTR section, DWORD index, INFCONTEXT *context ) |
| 1367 | { |
| 1368 | UNICODE_STRING sectionW; |
| 1369 | BOOL ret = FALSE; |
| 1370 | |
| 1371 | if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section )) |
| 1372 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 1373 | else |
| 1374 | { |
| 1375 | ret = SetupGetLineByIndexW( hinf, sectionW.Buffer, index, context ); |
| 1376 | RtlFreeUnicodeString( §ionW ); |
| 1377 | } |
| 1378 | return ret; |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | /*********************************************************************** |
| 1383 | * SetupGetLineByIndexW (SETUPAPI.@) |
| 1384 | */ |
| 1385 | BOOL WINAPI SetupGetLineByIndexW( HINF hinf, PCWSTR section, DWORD index, INFCONTEXT *context ) |
| 1386 | { |
| 1387 | struct inf_file *file = hinf; |
| 1388 | int section_index; |
| 1389 | |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1390 | for (file = hinf; file; file = file->next) |
| 1391 | { |
| 1392 | if ((section_index = find_section( file, section )) == -1) continue; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1393 | if (index < file->sections[section_index]->nb_lines) |
| 1394 | { |
| 1395 | context->Inf = hinf; |
| 1396 | context->CurrentInf = file; |
| 1397 | context->Section = section_index; |
| 1398 | context->Line = index; |
| 1399 | SetLastError( 0 ); |
Michael Stefaniuc | 79ecfaf | 2006-10-13 23:32:50 +0200 | [diff] [blame] | 1400 | TRACE( "(%p,%s): returning %d/%d\n", |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1401 | hinf, debugstr_w(section), section_index, index ); |
| 1402 | return TRUE; |
| 1403 | } |
| 1404 | index -= file->sections[section_index]->nb_lines; |
| 1405 | } |
| 1406 | TRACE( "(%p,%s) not found\n", hinf, debugstr_w(section) ); |
Paul Vriens | a355727 | 2008-03-28 16:41:20 +0100 | [diff] [blame] | 1407 | SetLastError( ERROR_LINE_NOT_FOUND ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1408 | return FALSE; |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | /*********************************************************************** |
| 1413 | * SetupFindFirstLineA (SETUPAPI.@) |
| 1414 | */ |
| 1415 | BOOL WINAPI SetupFindFirstLineA( HINF hinf, PCSTR section, PCSTR key, INFCONTEXT *context ) |
| 1416 | { |
| 1417 | UNICODE_STRING sectionW, keyW; |
| 1418 | BOOL ret = FALSE; |
| 1419 | |
| 1420 | if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section )) |
| 1421 | { |
| 1422 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 1423 | return FALSE; |
| 1424 | } |
| 1425 | |
| 1426 | if (!key) ret = SetupFindFirstLineW( hinf, sectionW.Buffer, NULL, context ); |
| 1427 | else |
| 1428 | { |
| 1429 | if (RtlCreateUnicodeStringFromAsciiz( &keyW, key )) |
| 1430 | { |
| 1431 | ret = SetupFindFirstLineW( hinf, sectionW.Buffer, keyW.Buffer, context ); |
| 1432 | RtlFreeUnicodeString( &keyW ); |
| 1433 | } |
| 1434 | else SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 1435 | } |
| 1436 | RtlFreeUnicodeString( §ionW ); |
| 1437 | return ret; |
| 1438 | } |
| 1439 | |
| 1440 | |
| 1441 | /*********************************************************************** |
| 1442 | * SetupFindFirstLineW (SETUPAPI.@) |
| 1443 | */ |
| 1444 | BOOL WINAPI SetupFindFirstLineW( HINF hinf, PCWSTR section, PCWSTR key, INFCONTEXT *context ) |
| 1445 | { |
| 1446 | struct inf_file *file; |
| 1447 | int section_index; |
| 1448 | |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1449 | for (file = hinf; file; file = file->next) |
| 1450 | { |
| 1451 | if ((section_index = find_section( file, section )) == -1) continue; |
| 1452 | if (key) |
| 1453 | { |
| 1454 | INFCONTEXT ctx; |
| 1455 | ctx.Inf = hinf; |
| 1456 | ctx.CurrentInf = file; |
| 1457 | ctx.Section = section_index; |
| 1458 | ctx.Line = -1; |
| 1459 | return SetupFindNextMatchLineW( &ctx, key, context ); |
| 1460 | } |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1461 | if (file->sections[section_index]->nb_lines) |
| 1462 | { |
| 1463 | context->Inf = hinf; |
| 1464 | context->CurrentInf = file; |
| 1465 | context->Section = section_index; |
| 1466 | context->Line = 0; |
| 1467 | SetLastError( 0 ); |
| 1468 | TRACE( "(%p,%s,%s): returning %d/0\n", |
| 1469 | hinf, debugstr_w(section), debugstr_w(key), section_index ); |
| 1470 | return TRUE; |
| 1471 | } |
| 1472 | } |
| 1473 | TRACE( "(%p,%s,%s): not found\n", hinf, debugstr_w(section), debugstr_w(key) ); |
Paul Vriens | 50e81d0 | 2008-03-28 16:29:52 +0100 | [diff] [blame] | 1474 | SetLastError( ERROR_LINE_NOT_FOUND ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1475 | return FALSE; |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | /*********************************************************************** |
| 1480 | * SetupFindNextLine (SETUPAPI.@) |
| 1481 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1482 | BOOL WINAPI SetupFindNextLine( PINFCONTEXT context_in, PINFCONTEXT context_out ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1483 | { |
| 1484 | struct inf_file *file = context_in->CurrentInf; |
| 1485 | struct section *section; |
| 1486 | |
| 1487 | if (context_in->Section >= file->nb_sections) goto error; |
| 1488 | |
| 1489 | section = file->sections[context_in->Section]; |
| 1490 | if (context_in->Line+1 < section->nb_lines) |
| 1491 | { |
| 1492 | if (context_out != context_in) *context_out = *context_in; |
| 1493 | context_out->Line++; |
| 1494 | SetLastError( 0 ); |
| 1495 | return TRUE; |
| 1496 | } |
| 1497 | |
| 1498 | /* now search the appended files */ |
| 1499 | |
| 1500 | for (file = file->next; file; file = file->next) |
| 1501 | { |
| 1502 | int section_index = find_section( file, section->name ); |
| 1503 | if (section_index == -1) continue; |
| 1504 | if (file->sections[section_index]->nb_lines) |
| 1505 | { |
| 1506 | context_out->Inf = context_in->Inf; |
| 1507 | context_out->CurrentInf = file; |
| 1508 | context_out->Section = section_index; |
| 1509 | context_out->Line = 0; |
| 1510 | SetLastError( 0 ); |
| 1511 | return TRUE; |
| 1512 | } |
| 1513 | } |
| 1514 | error: |
| 1515 | SetLastError( ERROR_LINE_NOT_FOUND ); |
| 1516 | return FALSE; |
| 1517 | } |
| 1518 | |
| 1519 | |
| 1520 | /*********************************************************************** |
| 1521 | * SetupFindNextMatchLineA (SETUPAPI.@) |
| 1522 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1523 | BOOL WINAPI SetupFindNextMatchLineA( PINFCONTEXT context_in, PCSTR key, |
| 1524 | PINFCONTEXT context_out ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1525 | { |
| 1526 | UNICODE_STRING keyW; |
| 1527 | BOOL ret = FALSE; |
| 1528 | |
| 1529 | if (!key) return SetupFindNextLine( context_in, context_out ); |
| 1530 | |
| 1531 | if (!RtlCreateUnicodeStringFromAsciiz( &keyW, key )) |
| 1532 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
| 1533 | else |
| 1534 | { |
| 1535 | ret = SetupFindNextMatchLineW( context_in, keyW.Buffer, context_out ); |
| 1536 | RtlFreeUnicodeString( &keyW ); |
| 1537 | } |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | |
| 1542 | /*********************************************************************** |
| 1543 | * SetupFindNextMatchLineW (SETUPAPI.@) |
| 1544 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1545 | BOOL WINAPI SetupFindNextMatchLineW( PINFCONTEXT context_in, PCWSTR key, |
| 1546 | PINFCONTEXT context_out ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1547 | { |
| 1548 | struct inf_file *file = context_in->CurrentInf; |
| 1549 | struct section *section; |
| 1550 | struct line *line; |
| 1551 | unsigned int i; |
| 1552 | |
| 1553 | if (!key) return SetupFindNextLine( context_in, context_out ); |
| 1554 | |
| 1555 | if (context_in->Section >= file->nb_sections) goto error; |
| 1556 | |
| 1557 | section = file->sections[context_in->Section]; |
| 1558 | |
| 1559 | for (i = context_in->Line+1, line = §ion->lines[i]; i < section->nb_lines; i++, line++) |
| 1560 | { |
| 1561 | if (line->key_field == -1) continue; |
| 1562 | if (!strcmpiW( key, file->fields[line->key_field].text )) |
| 1563 | { |
| 1564 | if (context_out != context_in) *context_out = *context_in; |
| 1565 | context_out->Line = i; |
| 1566 | SetLastError( 0 ); |
| 1567 | TRACE( "(%p,%s,%s): returning %d\n", |
| 1568 | file, debugstr_w(section->name), debugstr_w(key), i ); |
| 1569 | return TRUE; |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | /* now search the appended files */ |
| 1574 | |
| 1575 | for (file = file->next; file; file = file->next) |
| 1576 | { |
| 1577 | int section_index = find_section( file, section->name ); |
| 1578 | if (section_index == -1) continue; |
| 1579 | section = file->sections[section_index]; |
| 1580 | for (i = 0, line = section->lines; i < section->nb_lines; i++, line++) |
| 1581 | { |
| 1582 | if (line->key_field == -1) continue; |
| 1583 | if (!strcmpiW( key, file->fields[line->key_field].text )) |
| 1584 | { |
| 1585 | context_out->Inf = context_in->Inf; |
| 1586 | context_out->CurrentInf = file; |
| 1587 | context_out->Section = section_index; |
| 1588 | context_out->Line = i; |
| 1589 | SetLastError( 0 ); |
| 1590 | TRACE( "(%p,%s,%s): returning %d/%d\n", |
| 1591 | file, debugstr_w(section->name), debugstr_w(key), section_index, i ); |
| 1592 | return TRUE; |
| 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | TRACE( "(%p,%s,%s): not found\n", |
| 1597 | context_in->CurrentInf, debugstr_w(section->name), debugstr_w(key) ); |
| 1598 | error: |
| 1599 | SetLastError( ERROR_LINE_NOT_FOUND ); |
| 1600 | return FALSE; |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | /*********************************************************************** |
| 1605 | * SetupGetLineTextW (SETUPAPI.@) |
| 1606 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1607 | BOOL WINAPI SetupGetLineTextW( PINFCONTEXT context, HINF hinf, PCWSTR section_name, |
| 1608 | PCWSTR key_name, PWSTR buffer, DWORD size, PDWORD required ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1609 | { |
| 1610 | struct inf_file *file; |
| 1611 | struct line *line; |
| 1612 | struct field *field; |
| 1613 | int i; |
| 1614 | DWORD total = 0; |
| 1615 | |
| 1616 | if (!context) |
| 1617 | { |
| 1618 | INFCONTEXT new_context; |
| 1619 | if (!SetupFindFirstLineW( hinf, section_name, key_name, &new_context )) return FALSE; |
| 1620 | file = new_context.CurrentInf; |
| 1621 | line = get_line( file, new_context.Section, new_context.Line ); |
| 1622 | } |
| 1623 | else |
| 1624 | { |
| 1625 | file = context->CurrentInf; |
| 1626 | if (!(line = get_line( file, context->Section, context->Line ))) |
| 1627 | { |
| 1628 | SetLastError( ERROR_LINE_NOT_FOUND ); |
| 1629 | return FALSE; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++) |
| 1634 | total += PARSER_string_substW( file, field->text, NULL, 0 ) + 1; |
| 1635 | |
| 1636 | if (required) *required = total; |
| 1637 | if (buffer) |
| 1638 | { |
| 1639 | if (total > size) |
| 1640 | { |
| 1641 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1642 | return FALSE; |
| 1643 | } |
| 1644 | for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++) |
| 1645 | { |
| 1646 | unsigned int len = PARSER_string_substW( file, field->text, buffer, size ); |
| 1647 | if (i+1 < line->nb_fields) buffer[len] = ','; |
| 1648 | buffer += len + 1; |
| 1649 | } |
| 1650 | } |
| 1651 | return TRUE; |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | /*********************************************************************** |
| 1656 | * SetupGetLineTextA (SETUPAPI.@) |
| 1657 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1658 | BOOL WINAPI SetupGetLineTextA( PINFCONTEXT context, HINF hinf, PCSTR section_name, |
| 1659 | PCSTR key_name, PSTR buffer, DWORD size, PDWORD required ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1660 | { |
| 1661 | struct inf_file *file; |
| 1662 | struct line *line; |
| 1663 | struct field *field; |
| 1664 | int i; |
| 1665 | DWORD total = 0; |
| 1666 | |
| 1667 | if (!context) |
| 1668 | { |
| 1669 | INFCONTEXT new_context; |
| 1670 | if (!SetupFindFirstLineA( hinf, section_name, key_name, &new_context )) return FALSE; |
| 1671 | file = new_context.CurrentInf; |
| 1672 | line = get_line( file, new_context.Section, new_context.Line ); |
| 1673 | } |
| 1674 | else |
| 1675 | { |
| 1676 | file = context->CurrentInf; |
| 1677 | if (!(line = get_line( file, context->Section, context->Line ))) |
| 1678 | { |
| 1679 | SetLastError( ERROR_LINE_NOT_FOUND ); |
| 1680 | return FALSE; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++) |
| 1685 | total += PARSER_string_substA( file, field->text, NULL, 0 ) + 1; |
| 1686 | |
| 1687 | if (required) *required = total; |
| 1688 | if (buffer) |
| 1689 | { |
| 1690 | if (total > size) |
| 1691 | { |
| 1692 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1693 | return FALSE; |
| 1694 | } |
| 1695 | for (i = 0, field = &file->fields[line->first_field]; i < line->nb_fields; i++, field++) |
| 1696 | { |
| 1697 | unsigned int len = PARSER_string_substA( file, field->text, buffer, size ); |
| 1698 | if (i+1 < line->nb_fields) buffer[len] = ','; |
| 1699 | buffer += len + 1; |
| 1700 | } |
| 1701 | } |
| 1702 | return TRUE; |
| 1703 | } |
| 1704 | |
| 1705 | |
| 1706 | /*********************************************************************** |
| 1707 | * SetupGetFieldCount (SETUPAPI.@) |
| 1708 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1709 | DWORD WINAPI SetupGetFieldCount( PINFCONTEXT context ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1710 | { |
| 1711 | struct inf_file *file = context->CurrentInf; |
| 1712 | struct line *line = get_line( file, context->Section, context->Line ); |
| 1713 | |
| 1714 | if (!line) return 0; |
| 1715 | return line->nb_fields; |
| 1716 | } |
| 1717 | |
| 1718 | |
| 1719 | /*********************************************************************** |
| 1720 | * SetupGetStringFieldA (SETUPAPI.@) |
| 1721 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1722 | BOOL WINAPI SetupGetStringFieldA( PINFCONTEXT context, DWORD index, PSTR buffer, |
| 1723 | DWORD size, PDWORD required ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1724 | { |
| 1725 | struct inf_file *file = context->CurrentInf; |
| 1726 | struct field *field = get_field( file, context->Section, context->Line, index ); |
| 1727 | unsigned int len; |
| 1728 | |
| 1729 | SetLastError(0); |
| 1730 | if (!field) return FALSE; |
| 1731 | len = PARSER_string_substA( file, field->text, NULL, 0 ); |
| 1732 | if (required) *required = len + 1; |
| 1733 | if (buffer) |
| 1734 | { |
| 1735 | if (size <= len) |
| 1736 | { |
| 1737 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1738 | return FALSE; |
| 1739 | } |
| 1740 | PARSER_string_substA( file, field->text, buffer, size ); |
| 1741 | |
Michael Stefaniuc | 79ecfaf | 2006-10-13 23:32:50 +0200 | [diff] [blame] | 1742 | TRACE( "context %p/%p/%d/%d index %d returning %s\n", |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1743 | context->Inf, context->CurrentInf, context->Section, context->Line, |
| 1744 | index, debugstr_a(buffer) ); |
| 1745 | } |
| 1746 | return TRUE; |
| 1747 | } |
| 1748 | |
| 1749 | |
| 1750 | /*********************************************************************** |
| 1751 | * SetupGetStringFieldW (SETUPAPI.@) |
| 1752 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1753 | BOOL WINAPI SetupGetStringFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer, |
| 1754 | DWORD size, PDWORD required ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1755 | { |
| 1756 | struct inf_file *file = context->CurrentInf; |
| 1757 | struct field *field = get_field( file, context->Section, context->Line, index ); |
| 1758 | unsigned int len; |
| 1759 | |
| 1760 | SetLastError(0); |
| 1761 | if (!field) return FALSE; |
| 1762 | len = PARSER_string_substW( file, field->text, NULL, 0 ); |
| 1763 | if (required) *required = len + 1; |
| 1764 | if (buffer) |
| 1765 | { |
| 1766 | if (size <= len) |
| 1767 | { |
| 1768 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1769 | return FALSE; |
| 1770 | } |
| 1771 | PARSER_string_substW( file, field->text, buffer, size ); |
| 1772 | |
Michael Stefaniuc | 79ecfaf | 2006-10-13 23:32:50 +0200 | [diff] [blame] | 1773 | TRACE( "context %p/%p/%d/%d index %d returning %s\n", |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1774 | context->Inf, context->CurrentInf, context->Section, context->Line, |
| 1775 | index, debugstr_w(buffer) ); |
| 1776 | } |
| 1777 | return TRUE; |
| 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | /*********************************************************************** |
| 1782 | * SetupGetIntField (SETUPAPI.@) |
| 1783 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1784 | BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1785 | { |
| 1786 | char localbuff[20]; |
| 1787 | char *end, *buffer = localbuff; |
| 1788 | DWORD required; |
| 1789 | INT res; |
Paul Vriens | ebec0a9 | 2008-03-31 19:51:21 +0200 | [diff] [blame] | 1790 | BOOL ret; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1791 | |
Paul Vriens | ebec0a9 | 2008-03-31 19:51:21 +0200 | [diff] [blame] | 1792 | if (!(ret = SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1793 | { |
| 1794 | if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE; |
| 1795 | if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE; |
Paul Vriens | ebec0a9 | 2008-03-31 19:51:21 +0200 | [diff] [blame] | 1796 | if (!(ret = SetupGetStringFieldA( context, index, buffer, required, NULL ))) goto done; |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1797 | } |
Paul Vriens | ebec0a9 | 2008-03-31 19:51:21 +0200 | [diff] [blame] | 1798 | /* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */ |
| 1799 | if (!*buffer) *result = 0; |
| 1800 | else |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1801 | { |
Paul Vriens | ebec0a9 | 2008-03-31 19:51:21 +0200 | [diff] [blame] | 1802 | res = strtol( buffer, &end, 0 ); |
| 1803 | if (end != buffer && !*end) *result = res; |
| 1804 | else |
| 1805 | { |
| 1806 | SetLastError( ERROR_INVALID_DATA ); |
| 1807 | ret = FALSE; |
| 1808 | } |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1809 | } |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1810 | |
| 1811 | done: |
| 1812 | if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer ); |
| 1813 | return ret; |
| 1814 | } |
| 1815 | |
| 1816 | |
| 1817 | /*********************************************************************** |
| 1818 | * SetupGetBinaryField (SETUPAPI.@) |
| 1819 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1820 | BOOL WINAPI SetupGetBinaryField( PINFCONTEXT context, DWORD index, BYTE *buffer, |
| 1821 | DWORD size, LPDWORD required ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1822 | { |
| 1823 | struct inf_file *file = context->CurrentInf; |
| 1824 | struct line *line = get_line( file, context->Section, context->Line ); |
| 1825 | struct field *field; |
| 1826 | int i; |
| 1827 | |
| 1828 | if (!line) |
| 1829 | { |
| 1830 | SetLastError( ERROR_LINE_NOT_FOUND ); |
| 1831 | return FALSE; |
| 1832 | } |
Ge van Geldorp | 3ebeb23 | 2005-08-15 09:43:28 +0000 | [diff] [blame] | 1833 | if (!index || index > line->nb_fields) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1834 | { |
| 1835 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1836 | return FALSE; |
| 1837 | } |
| 1838 | index--; /* fields start at 0 */ |
| 1839 | if (required) *required = line->nb_fields - index; |
| 1840 | if (!buffer) return TRUE; |
| 1841 | if (size < line->nb_fields - index) |
| 1842 | { |
| 1843 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1844 | return FALSE; |
| 1845 | } |
| 1846 | field = &file->fields[line->first_field + index]; |
| 1847 | for (i = index; i < line->nb_fields; i++, field++) |
| 1848 | { |
| 1849 | const WCHAR *p; |
| 1850 | DWORD value = 0; |
| 1851 | for (p = field->text; *p && isxdigitW(*p); p++) |
| 1852 | { |
Dmitry Timoshkov | 2a00807 | 2002-08-13 18:11:15 +0000 | [diff] [blame] | 1853 | if ((value <<= 4) > 255) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1854 | { |
| 1855 | SetLastError( ERROR_INVALID_DATA ); |
| 1856 | return FALSE; |
| 1857 | } |
| 1858 | if (*p <= '9') value |= (*p - '0'); |
| 1859 | else value |= (tolowerW(*p) - 'a' + 10); |
| 1860 | } |
| 1861 | buffer[i - index] = value; |
| 1862 | } |
| 1863 | if (TRACE_ON(setupapi)) |
| 1864 | { |
Michael Stefaniuc | 79ecfaf | 2006-10-13 23:32:50 +0200 | [diff] [blame] | 1865 | TRACE( "%p/%p/%d/%d index %d returning", |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1866 | context->Inf, context->CurrentInf, context->Section, context->Line, index ); |
Tony Lambregts | 2e24a14 | 2003-03-15 00:12:42 +0000 | [diff] [blame] | 1867 | for (i = index; i < line->nb_fields; i++) TRACE( " %02x", buffer[i - index] ); |
| 1868 | TRACE( "\n" ); |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1869 | } |
| 1870 | return TRUE; |
| 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | /*********************************************************************** |
| 1875 | * SetupGetMultiSzFieldA (SETUPAPI.@) |
| 1876 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1877 | BOOL WINAPI SetupGetMultiSzFieldA( PINFCONTEXT context, DWORD index, PSTR buffer, |
| 1878 | DWORD size, LPDWORD required ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1879 | { |
| 1880 | struct inf_file *file = context->CurrentInf; |
| 1881 | struct line *line = get_line( file, context->Section, context->Line ); |
| 1882 | struct field *field; |
| 1883 | unsigned int len; |
| 1884 | int i; |
| 1885 | DWORD total = 1; |
| 1886 | |
| 1887 | if (!line) |
| 1888 | { |
| 1889 | SetLastError( ERROR_LINE_NOT_FOUND ); |
| 1890 | return FALSE; |
| 1891 | } |
Ge van Geldorp | 3ebeb23 | 2005-08-15 09:43:28 +0000 | [diff] [blame] | 1892 | if (!index || index > line->nb_fields) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1893 | { |
| 1894 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1895 | return FALSE; |
| 1896 | } |
| 1897 | index--; /* fields start at 0 */ |
| 1898 | field = &file->fields[line->first_field + index]; |
| 1899 | for (i = index; i < line->nb_fields; i++, field++) |
| 1900 | { |
| 1901 | if (!(len = PARSER_string_substA( file, field->text, NULL, 0 ))) break; |
| 1902 | total += len + 1; |
| 1903 | } |
| 1904 | |
| 1905 | if (required) *required = total; |
| 1906 | if (!buffer) return TRUE; |
| 1907 | if (total > size) |
| 1908 | { |
| 1909 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1910 | return FALSE; |
| 1911 | } |
| 1912 | field = &file->fields[line->first_field + index]; |
| 1913 | for (i = index; i < line->nb_fields; i++, field++) |
| 1914 | { |
| 1915 | if (!(len = PARSER_string_substA( file, field->text, buffer, size ))) break; |
| 1916 | buffer += len + 1; |
| 1917 | } |
| 1918 | *buffer = 0; /* add final null */ |
| 1919 | return TRUE; |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | /*********************************************************************** |
| 1924 | * SetupGetMultiSzFieldW (SETUPAPI.@) |
| 1925 | */ |
Steven Edwards | 6518b5c | 2004-01-05 21:12:21 +0000 | [diff] [blame] | 1926 | BOOL WINAPI SetupGetMultiSzFieldW( PINFCONTEXT context, DWORD index, PWSTR buffer, |
| 1927 | DWORD size, LPDWORD required ) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1928 | { |
| 1929 | struct inf_file *file = context->CurrentInf; |
| 1930 | struct line *line = get_line( file, context->Section, context->Line ); |
| 1931 | struct field *field; |
| 1932 | unsigned int len; |
| 1933 | int i; |
| 1934 | DWORD total = 1; |
| 1935 | |
| 1936 | if (!line) |
| 1937 | { |
| 1938 | SetLastError( ERROR_LINE_NOT_FOUND ); |
| 1939 | return FALSE; |
| 1940 | } |
Ge van Geldorp | 3ebeb23 | 2005-08-15 09:43:28 +0000 | [diff] [blame] | 1941 | if (!index || index > line->nb_fields) |
Alexandre Julliard | 5068347 | 2002-04-02 02:57:50 +0000 | [diff] [blame] | 1942 | { |
| 1943 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1944 | return FALSE; |
| 1945 | } |
| 1946 | index--; /* fields start at 0 */ |
| 1947 | field = &file->fields[line->first_field + index]; |
| 1948 | for (i = index; i < line->nb_fields; i++, field++) |
| 1949 | { |
| 1950 | if (!(len = PARSER_string_substW( file, field->text, NULL, 0 ))) break; |
| 1951 | total += len + 1; |
| 1952 | } |
| 1953 | |
| 1954 | if (required) *required = total; |
| 1955 | if (!buffer) return TRUE; |
| 1956 | if (total > size) |
| 1957 | { |
| 1958 | SetLastError( ERROR_INSUFFICIENT_BUFFER ); |
| 1959 | return FALSE; |
| 1960 | } |
| 1961 | field = &file->fields[line->first_field + index]; |
| 1962 | for (i = index; i < line->nb_fields; i++, field++) |
| 1963 | { |
| 1964 | if (!(len = PARSER_string_substW( file, field->text, buffer, size ))) break; |
| 1965 | buffer += len + 1; |
| 1966 | } |
| 1967 | *buffer = 0; /* add final null */ |
| 1968 | return TRUE; |
| 1969 | } |
James Hawkins | 7588b66 | 2006-09-13 14:19:12 -0700 | [diff] [blame] | 1970 | |
| 1971 | /*********************************************************************** |
| 1972 | * pSetupGetField (SETUPAPI.@) |
| 1973 | */ |
| 1974 | LPCWSTR WINAPI pSetupGetField( PINFCONTEXT context, DWORD index ) |
| 1975 | { |
| 1976 | struct inf_file *file = context->CurrentInf; |
| 1977 | struct field *field = get_field( file, context->Section, context->Line, index ); |
| 1978 | |
| 1979 | if (!field) |
| 1980 | { |
| 1981 | SetLastError( ERROR_INVALID_PARAMETER ); |
| 1982 | return NULL; |
| 1983 | } |
| 1984 | return field->text; |
| 1985 | } |