4 * Copyright 2008 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/debug.h"
29 #include "d3dxof_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof_parsing);
36 #define MAKEFOUR(a,b,c,d) ((DWORD)a + ((DWORD)b << 8) + ((DWORD)c << 16) + ((DWORD)d << 24))
37 #define XOFFILE_FORMAT_MAGIC MAKEFOUR('x','o','f',' ')
38 #define XOFFILE_FORMAT_VERSION_302 MAKEFOUR('0','3','0','2')
39 #define XOFFILE_FORMAT_VERSION_303 MAKEFOUR('0','3','0','3')
40 #define XOFFILE_FORMAT_BINARY MAKEFOUR('b','i','n',' ')
41 #define XOFFILE_FORMAT_TEXT MAKEFOUR('t','x','t',' ')
42 #define XOFFILE_FORMAT_BINARY_MSZIP MAKEFOUR('b','z','i','p')
43 #define XOFFILE_FORMAT_TEXT_MSZIP MAKEFOUR('t','z','i','p')
44 #define XOFFILE_FORMAT_COMPRESSED MAKEFOUR('c','m','p',' ')
45 #define XOFFILE_FORMAT_FLOAT_BITS_32 MAKEFOUR('0','0','3','2')
46 #define XOFFILE_FORMAT_FLOAT_BITS_64 MAKEFOUR('0','0','6','4')
49 #define TOKEN_STRING 2
50 #define TOKEN_INTEGER 3
52 #define TOKEN_INTEGER_LIST 6
53 #define TOKEN_FLOAT_LIST 7
54 #define TOKEN_OBRACE 10
55 #define TOKEN_CBRACE 11
56 #define TOKEN_OPAREN 12
57 #define TOKEN_CPAREN 13
58 #define TOKEN_OBRACKET 14
59 #define TOKEN_CBRACKET 15
60 #define TOKEN_OANGLE 16
61 #define TOKEN_CANGLE 17
63 #define TOKEN_COMMA 19
64 #define TOKEN_SEMICOLON 20
65 #define TOKEN_TEMPLATE 31
67 #define TOKEN_DWORD 41
68 #define TOKEN_FLOAT 42
69 #define TOKEN_DOUBLE 43
71 #define TOKEN_UCHAR 45
72 #define TOKEN_SWORD 46
73 #define TOKEN_SDWORD 47
75 #define TOKEN_LPSTR 49
76 #define TOKEN_UNICODE 50
77 #define TOKEN_CSTRING 51
78 #define TOKEN_ARRAY 52
80 #define CLSIDFMT "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>"
82 /* FOURCC to string conversion for debug messages */
83 static const char *debugstr_fourcc(DWORD fourcc)
85 if (!fourcc) return "'null'";
86 return wine_dbg_sprintf ("\'%c%c%c%c\'",
87 (char)(fourcc), (char)(fourcc >> 8),
88 (char)(fourcc >> 16), (char)(fourcc >> 24));
91 static const char* get_primitive_string(WORD token)
125 static void dump_template(xtemplate* templates_array, xtemplate* ptemplate)
130 clsid = &ptemplate->class_id;
132 DPRINTF("template %s\n", ptemplate->name);
134 DPRINTF(CLSIDFMT "\n", clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
135 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
136 for (j = 0; j < ptemplate->nb_members; j++)
138 if (ptemplate->members[j].nb_dims)
140 if (ptemplate->members[j].type == TOKEN_NAME)
141 DPRINTF("%s ", templates_array[ptemplate->members[j].idx_template].name);
143 DPRINTF("%s ", get_primitive_string(ptemplate->members[j].type));
144 DPRINTF("%s", ptemplate->members[j].name);
145 for (k = 0; k < ptemplate->members[j].nb_dims; k++)
147 if (ptemplate->members[j].dim_fixed[k])
148 DPRINTF("[%d]", ptemplate->members[j].dim_value[k]);
150 DPRINTF("[%s]", ptemplate->members[ptemplate->members[j].dim_value[k]].name);
156 else if (ptemplate->nb_children)
158 DPRINTF("[%s", ptemplate->children[0]);
159 for (j = 1; j < ptemplate->nb_children; j++)
160 DPRINTF(",%s", ptemplate->children[j]);
166 static BOOL read_bytes(parse_buffer * buf, LPVOID data, DWORD size)
168 if (buf->rem_bytes < size)
170 memcpy(data, buf->buffer, size);
172 buf->rem_bytes -= size;
176 static void rewind_bytes(parse_buffer * buf, DWORD size)
179 buf->rem_bytes += size;
182 HRESULT parse_header(parse_buffer * buf, BYTE ** decomp_buffer_ptr)
184 /* X File common header:
185 * 0-3 -> Magic Number (format identifier)
186 * 4-7 -> Format Version
187 * 8-11 -> Format Type (text or binary, decompressed or compressed)
188 * 12-15 -> Float Size (32 or 64 bits) */
191 if (!read_bytes(buf, header, sizeof(header)))
192 return DXFILEERR_BADFILETYPE;
194 if (TRACE_ON(d3dxof_parsing))
197 memcpy(string, header, 16);
199 TRACE("header = '%s'\n", string);
202 if (header[0] != XOFFILE_FORMAT_MAGIC)
203 return DXFILEERR_BADFILETYPE;
205 if (header[1] != XOFFILE_FORMAT_VERSION_302 && header[1] != XOFFILE_FORMAT_VERSION_303)
206 return DXFILEERR_BADFILEVERSION;
208 if (header[2] != XOFFILE_FORMAT_BINARY && header[2] != XOFFILE_FORMAT_TEXT &&
209 header[2] != XOFFILE_FORMAT_BINARY_MSZIP && header[2] != XOFFILE_FORMAT_TEXT_MSZIP)
211 WARN("File type %s unknown\n", debugstr_fourcc(header[2]));
212 return DXFILEERR_BADFILETYPE;
215 if (header[3] != XOFFILE_FORMAT_FLOAT_BITS_32 && header[3] != XOFFILE_FORMAT_FLOAT_BITS_64)
216 return DXFILEERR_BADFILEFLOATSIZE;
218 buf->txt = header[2] == XOFFILE_FORMAT_TEXT || header[2] == XOFFILE_FORMAT_TEXT_MSZIP;
220 if (header[2] == XOFFILE_FORMAT_BINARY_MSZIP || header[2] == XOFFILE_FORMAT_TEXT_MSZIP)
222 /* Extended header for compressed data:
223 * 16-17 -> decompressed size w/ header, 18-19 -> null,
224 * 20-21 -> decompressed size w/o header, 22-23 -> size of MSZIP compressed data,
225 * 24-xx -> compressed MSZIP data */
229 LPBYTE decomp_buffer;
231 buf->rem_bytes -= sizeof(WORD) * 2;
232 buf->buffer += sizeof(WORD) * 2;
233 if (!read_bytes(buf, &decomp_size, sizeof(decomp_size)))
234 return DXFILEERR_BADFILETYPE;
235 if (!read_bytes(buf, &comp_size, sizeof(comp_size)))
236 return DXFILEERR_BADFILETYPE;
238 TRACE("Compressed format %s detected: compressed_size = %x, decompressed_size = %x\n",
239 debugstr_fourcc(header[2]), comp_size, decomp_size);
241 decomp_buffer = HeapAlloc(GetProcessHeap(), 0, decomp_size);
244 ERR("Out of memory\n");
245 return DXFILEERR_BADALLOC;
247 err = mszip_decompress(comp_size, decomp_size, (char*)buf->buffer, (char*)decomp_buffer);
250 WARN("Error while decompressing mszip archive %d\n", err);
251 HeapFree(GetProcessHeap(), 0, decomp_buffer);
252 return DXFILEERR_BADALLOC;
254 /* Use decompressed data */
255 buf->buffer = *decomp_buffer_ptr = decomp_buffer;
256 buf->rem_bytes = decomp_size;
259 TRACE("Header is correct\n");
264 static void dump_TOKEN(WORD token)
266 #define DUMP_TOKEN(t) case t: TRACE(#t "\n"); break
269 DUMP_TOKEN(TOKEN_NAME);
270 DUMP_TOKEN(TOKEN_STRING);
271 DUMP_TOKEN(TOKEN_INTEGER);
272 DUMP_TOKEN(TOKEN_GUID);
273 DUMP_TOKEN(TOKEN_INTEGER_LIST);
274 DUMP_TOKEN(TOKEN_FLOAT_LIST);
275 DUMP_TOKEN(TOKEN_OBRACE);
276 DUMP_TOKEN(TOKEN_CBRACE);
277 DUMP_TOKEN(TOKEN_OPAREN);
278 DUMP_TOKEN(TOKEN_CPAREN);
279 DUMP_TOKEN(TOKEN_OBRACKET);
280 DUMP_TOKEN(TOKEN_CBRACKET);
281 DUMP_TOKEN(TOKEN_OANGLE);
282 DUMP_TOKEN(TOKEN_CANGLE);
283 DUMP_TOKEN(TOKEN_DOT);
284 DUMP_TOKEN(TOKEN_COMMA);
285 DUMP_TOKEN(TOKEN_SEMICOLON);
286 DUMP_TOKEN(TOKEN_TEMPLATE);
287 DUMP_TOKEN(TOKEN_WORD);
288 DUMP_TOKEN(TOKEN_DWORD);
289 DUMP_TOKEN(TOKEN_FLOAT);
290 DUMP_TOKEN(TOKEN_DOUBLE);
291 DUMP_TOKEN(TOKEN_CHAR);
292 DUMP_TOKEN(TOKEN_UCHAR);
293 DUMP_TOKEN(TOKEN_SWORD);
294 DUMP_TOKEN(TOKEN_SDWORD);
295 DUMP_TOKEN(TOKEN_VOID);
296 DUMP_TOKEN(TOKEN_LPSTR);
297 DUMP_TOKEN(TOKEN_UNICODE);
298 DUMP_TOKEN(TOKEN_CSTRING);
299 DUMP_TOKEN(TOKEN_ARRAY);
302 TRACE("Unknown token %d\n", token);
308 static BOOL is_space(char c)
322 static BOOL is_operator(char c)
341 static inline BOOL is_separator(char c)
343 return is_space(c) || is_operator(c);
346 static WORD get_operator_token(char c)
355 return TOKEN_OBRACKET;
357 return TOKEN_CBRACKET;
369 return TOKEN_SEMICOLON;
374 static BOOL is_keyword(parse_buffer* buf, const char* keyword)
376 char tmp[8]; /* longest keyword size (template) */
377 DWORD len = strlen(keyword);
379 if (!read_bytes(buf, tmp, len))
381 if (strncasecmp(tmp, keyword, len))
383 rewind_bytes(buf, len);
387 if (!read_bytes(buf, tmp, 1))
389 if (is_separator(tmp[0]))
391 rewind_bytes(buf, 1);
394 rewind_bytes(buf, len+1);
398 static WORD get_keyword_token(parse_buffer* buf)
400 if (is_keyword(buf, "template"))
401 return TOKEN_TEMPLATE;
402 if (is_keyword(buf, "WORD"))
404 if (is_keyword(buf, "DWORD"))
406 if (is_keyword(buf, "FLOAT"))
408 if (is_keyword(buf, "DOUBLE"))
410 if (is_keyword(buf, "CHAR"))
412 if (is_keyword(buf, "UCHAR"))
414 if (is_keyword(buf, "SWORD"))
416 if (is_keyword(buf, "SDWORD"))
418 if (is_keyword(buf, "VOID"))
420 if (is_keyword(buf, "STRING"))
422 if (is_keyword(buf, "UNICODE"))
423 return TOKEN_UNICODE;
424 if (is_keyword(buf, "CSTRING"))
425 return TOKEN_CSTRING;
426 if (is_keyword(buf, "array"))
432 static BOOL is_guid(parse_buffer* buf)
440 if (buf->rem_bytes < 38 || *buf->buffer != '<')
443 while (pos < sizeof(tmp) - 2 && *(buf->buffer+pos) != '>')
445 tmp[pos] = *(buf->buffer+pos);
450 if (pos != 38 /* <+36+> */)
452 TRACE("Wrong guid %s (%d)\n", tmp, pos);
456 buf->rem_bytes -= pos;
458 ret = sscanf(tmp, CLSIDFMT, &class_id.Data1, tab, tab+1, tab+2, tab+3, tab+4, tab+5, tab+6, tab+7, tab+8, tab+9);
461 TRACE("Wrong guid %s (%d)\n", tmp, pos);
464 TRACE("Found guid %s (%d)\n", tmp, pos);
466 class_id.Data2 = tab[0];
467 class_id.Data3 = tab[1];
468 class_id.Data4[0] = tab[2];
469 class_id.Data4[1] = tab[3];
470 class_id.Data4[2] = tab[4];
471 class_id.Data4[3] = tab[5];
472 class_id.Data4[4] = tab[6];
473 class_id.Data4[5] = tab[7];
474 class_id.Data4[6] = tab[8];
475 class_id.Data4[7] = tab[9];
477 *(GUID*)buf->value = class_id;
482 static BOOL is_name(parse_buffer* buf)
488 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
490 if (!(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '_') || (c == '-')))
492 if (pos < sizeof(tmp))
496 tmp[min(pos, sizeof(tmp) - 1)] = 0;
500 TRACE("Wrong name %s\n", tmp);
505 buf->rem_bytes -= pos;
507 TRACE("Found name %s\n", tmp);
508 strcpy((char*)buf->value, tmp);
513 static BOOL is_float(parse_buffer* buf)
521 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
523 if (!((!pos && (c == '-')) || ((c >= '0') && (c <= '9')) || (!dot && (c == '.'))))
527 if (pos < sizeof(tmp))
531 tmp[min(pos, sizeof(tmp) - 1)] = 0;
534 buf->rem_bytes -= pos;
536 sscanf(tmp, "%f", &decimal);
538 TRACE("Found float %s - %f\n", tmp, decimal);
540 *(float*)buf->value = decimal;
545 static BOOL is_integer(parse_buffer* buf)
552 while (pos < buf->rem_bytes && !is_separator(c = *(buf->buffer+pos)))
554 if (!((c >= '0') && (c <= '9')))
556 if (pos < sizeof(tmp))
560 tmp[min(pos, sizeof(tmp) - 1)] = 0;
563 buf->rem_bytes -= pos;
565 sscanf(tmp, "%d", &integer);
567 TRACE("Found integer %s - %d\n", tmp, integer);
569 *(DWORD*)buf->value = integer;
574 static BOOL is_string(parse_buffer* buf)
581 if (*buf->buffer != '"')
584 while (pos < buf->rem_bytes && !is_operator(c = *(buf->buffer+pos+1)))
591 if (pos < sizeof(tmp))
595 tmp[min(pos, sizeof(tmp) - 1)] = 0;
599 TRACE("Wrong string %s\n", tmp);
603 buf->buffer += pos + 2;
604 buf->rem_bytes -= pos + 2;
606 TRACE("Found string %s\n", tmp);
607 strcpy((char*)buf->value, tmp);
612 static WORD parse_TOKEN(parse_buffer * buf)
621 if (!read_bytes(buf, &c, 1))
623 /*TRACE("char = '%c'\n", is_space(c) ? ' ' : c);*/
624 if ((c == '#') || (c == '/'))
626 /* Handle comment (# or //) */
629 if (!read_bytes(buf, &c, 1))
637 if (!read_bytes(buf, &c, 1))
644 if (is_operator(c) && (c != '<'))
646 token = get_operator_token(c);
656 rewind_bytes(buf, 1);
658 if ((token = get_keyword_token(buf)))
668 token = TOKEN_INTEGER;
687 FIXME("Unrecognize element\n");
699 if (!read_bytes(buf, &token, 2))
702 /* Convert integer and float list into separate elements */
703 if (token == TOKEN_INTEGER_LIST)
705 if (!read_bytes(buf, &nb_elem, 4))
707 token = TOKEN_INTEGER;
709 TRACE("Integer list (TOKEN_INTEGER_LIST) of size %d\n", nb_elem);
711 else if (token == TOKEN_FLOAT_LIST)
713 if (!read_bytes(buf, &nb_elem, 4))
717 TRACE("Float list (TOKEN_FLOAT_LIST) of size %d\n", nb_elem);
723 token = is_float ? TOKEN_FLOAT : TOKEN_INTEGER;
728 if (!read_bytes(buf, &integer, 4))
731 *(DWORD*)buf->value = integer;
744 if (!read_bytes(buf, &count, 4))
746 if (!read_bytes(buf, strname, count))
749 /*TRACE("name = %s\n", strname);*/
751 strcpy((char*)buf->value, strname);
758 if (!read_bytes(buf, &integer, 4))
760 /*TRACE("integer = %ld\n", integer);*/
762 *(DWORD*)buf->value = integer;
770 if (!read_bytes(buf, &class_id, 16))
772 sprintf(strguid, CLSIDFMT, class_id.Data1, class_id.Data2, class_id.Data3, class_id.Data4[0],
773 class_id.Data4[1], class_id.Data4[2], class_id.Data4[3], class_id.Data4[4], class_id.Data4[5],
774 class_id.Data4[6], class_id.Data4[7]);
775 /*TRACE("guid = {%s}\n", strguid);*/
777 *(GUID*)buf->value = class_id;
785 if (!read_bytes(buf, &count, 4))
787 if (!read_bytes(buf, strname, count))
790 if (!read_bytes(buf, &tmp_token, 2))
792 if ((tmp_token != TOKEN_COMMA) && (tmp_token != TOKEN_SEMICOLON))
793 ERR("No comma or semicolon (got %d)\n", tmp_token);
794 /*TRACE("name = %s\n", strname);*/
796 strcpy((char*)buf->value, strname);
810 case TOKEN_SEMICOLON:
836 static WORD get_TOKEN(parse_buffer * buf)
838 if (buf->token_present)
840 buf->token_present = FALSE;
841 return buf->current_token;
844 buf->current_token = parse_TOKEN(buf);
846 return buf->current_token;
849 static WORD check_TOKEN(parse_buffer * buf)
851 if (buf->token_present)
852 return buf->current_token;
854 buf->current_token = parse_TOKEN(buf);
855 buf->token_present = TRUE;
857 return buf->current_token;
860 static BOOL is_template_available(parse_buffer * buf)
862 return check_TOKEN(buf) == TOKEN_TEMPLATE;
865 static inline BOOL is_primitive_type(WORD token)
890 static BOOL parse_template_option_info(parse_buffer * buf)
892 xtemplate* cur_template = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates];
894 if (check_TOKEN(buf) == TOKEN_DOT)
897 if (get_TOKEN(buf) != TOKEN_DOT)
899 if (get_TOKEN(buf) != TOKEN_DOT)
901 cur_template->open = TRUE;
907 if (get_TOKEN(buf) != TOKEN_NAME)
909 strcpy(cur_template->children[cur_template->nb_children], (char*)buf->value);
910 if (check_TOKEN(buf) == TOKEN_GUID)
912 cur_template->nb_children++;
913 if (check_TOKEN(buf) != TOKEN_COMMA)
917 cur_template->open = FALSE;
923 static BOOL parse_template_members_list(parse_buffer * buf)
932 cur_member = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[idx_member];
934 if (check_TOKEN(buf) == TOKEN_ARRAY)
940 if (check_TOKEN(buf) == TOKEN_NAME)
942 cur_member->type = get_TOKEN(buf);
943 cur_member->idx_template = 0;
944 while (cur_member->idx_template < buf->pdxf->nb_xtemplates)
946 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[cur_member->idx_template].name))
948 cur_member->idx_template++;
950 if (cur_member->idx_template == buf->pdxf->nb_xtemplates)
952 ERR("Reference to a nonexistent template '%s'\n", (char*)buf->value);
956 else if (is_primitive_type(check_TOKEN(buf)))
957 cur_member->type = get_TOKEN(buf);
961 if (get_TOKEN(buf) != TOKEN_NAME)
963 strcpy(cur_member->name, (char*)buf->value);
967 while (check_TOKEN(buf) == TOKEN_OBRACKET)
969 if (nb_dims >= MAX_ARRAY_DIM)
971 FIXME("Too many dimensions (%d) for multi-dimensional array\n", nb_dims + 1);
975 if (check_TOKEN(buf) == TOKEN_INTEGER)
978 cur_member->dim_fixed[nb_dims] = TRUE;
979 cur_member->dim_value[nb_dims] = *(DWORD*)buf->value;
984 if (get_TOKEN(buf) != TOKEN_NAME)
986 for (i = 0; i < idx_member; i++)
988 if (!strcmp((char*)buf->value, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].name))
990 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].nb_dims)
992 ERR("Array cannot be used to specify variable array size\n");
995 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].type != TOKEN_DWORD)
997 FIXME("Only DWORD supported to specify variable array size\n");
1003 if (i == idx_member)
1005 ERR("Reference to unknown member %s\n", (char*)buf->value);
1008 cur_member->dim_fixed[nb_dims] = FALSE;
1009 cur_member->dim_value[nb_dims] = i;
1011 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1017 cur_member->nb_dims = nb_dims;
1019 if (get_TOKEN(buf) != TOKEN_SEMICOLON)
1025 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].nb_members = idx_member;
1030 static BOOL parse_template_parts(parse_buffer * buf)
1032 if (!parse_template_members_list(buf))
1034 if (check_TOKEN(buf) == TOKEN_OBRACKET)
1037 if (!parse_template_option_info(buf))
1039 if (get_TOKEN(buf) != TOKEN_CBRACKET)
1046 static void go_to_next_definition(parse_buffer * buf)
1049 while (buf->rem_bytes)
1051 if (!read_bytes(buf, &c, 1))
1053 if ((c == '#') || (c == '/'))
1055 /* Handle comment (# or //) */
1058 if (!read_bytes(buf, &c, 1))
1066 if (!read_bytes(buf, &c, 1))
1071 else if (!is_space(c))
1073 rewind_bytes(buf, 1);
1079 static BOOL parse_template(parse_buffer * buf)
1081 if (get_TOKEN(buf) != TOKEN_TEMPLATE)
1083 if (get_TOKEN(buf) != TOKEN_NAME)
1085 strcpy(buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, (char*)buf->value);
1086 if (get_TOKEN(buf) != TOKEN_OBRACE)
1088 if (get_TOKEN(buf) != TOKEN_GUID)
1090 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id = *(GUID*)buf->value;
1091 if (!parse_template_parts(buf))
1093 if (get_TOKEN(buf) != TOKEN_CBRACE)
1097 /* Go to the next template */
1098 go_to_next_definition(buf);
1101 TRACE("%d - %s - %s\n", buf->pdxf->nb_xtemplates, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, debugstr_guid(&buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id));
1102 buf->pdxf->nb_xtemplates++;
1107 BOOL parse_templates(parse_buffer * buf)
1109 while (buf->rem_bytes && is_template_available(buf))
1111 if (!parse_template(buf))
1113 WARN("Template is not correct\n");
1118 TRACE("Template successfully parsed:\n");
1119 if (TRACE_ON(d3dxof_parsing))
1120 dump_template(buf->pdxf->xtemplates, &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates - 1]);
1126 static BOOL check_buffer(parse_buffer * buf, ULONG size)
1128 if ((buf->cur_pos_data + size) > buf->capacity)
1131 ULONG new_capacity = buf->capacity ? 2 * buf->capacity : 100000;
1133 pdata = HeapAlloc(GetProcessHeap(), 0, new_capacity);
1136 memcpy(pdata, buf->pdata, buf->cur_pos_data);
1137 HeapFree(GetProcessHeap(), 0, buf->pdata);
1138 buf->capacity = new_capacity;
1140 buf->pxo->root->pdata = pdata;
1145 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional);
1146 static BOOL parse_object_members_list(parse_buffer * buf)
1150 xtemplate* pt = buf->pxt[buf->level];
1152 for (i = 0; i < pt->nb_members; i++)
1157 buf->pxo->members[i].name = pt->members[i].name;
1158 buf->pxo->members[i].start = buf->cur_pos_data;
1160 for (k = 0; k < pt->members[i].nb_dims; k++)
1162 if (pt->members[i].dim_fixed[k])
1163 nb_elems *= pt->members[i].dim_value[k];
1165 nb_elems *= *(DWORD*)(buf->pxo->root->pdata + buf->pxo->members[pt->members[i].dim_value[k]].start);
1168 TRACE("Elements to consider: %d\n", nb_elems);
1170 for (k = 0; k < nb_elems; k++)
1174 token = check_TOKEN(buf);
1175 if (token == TOKEN_COMMA)
1181 /* Allow comma omission */
1182 if (!((token == TOKEN_FLOAT) || (token == TOKEN_INTEGER)))
1187 if (pt->members[i].type == TOKEN_NAME)
1191 TRACE("Found sub-object %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1193 /* To do template lookup */
1194 for (j = 0; j < buf->pdxf->nb_xtemplates; j++)
1196 if (!strcasecmp(buf->pdxf->xtemplates[pt->members[i].idx_template].name, buf->pdxf->xtemplates[j].name))
1198 buf->pxt[buf->level] = &buf->pdxf->xtemplates[j];
1202 if (j == buf->pdxf->nb_xtemplates)
1204 ERR("Unknown template %s\n", (char*)buf->value);
1208 TRACE("Enter %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1209 if (!parse_object_parts(buf, FALSE))
1218 token = check_TOKEN(buf);
1219 if (token == TOKEN_INTEGER)
1222 TRACE("%s = %d\n", pt->members[i].name, *(DWORD*)buf->value);
1223 /* Assume larger size */
1224 if (!check_buffer(buf, 4))
1226 if (pt->members[i].type == TOKEN_WORD)
1228 *(((WORD*)(buf->cur_pos_data + buf->pdata))) = (WORD)(*(DWORD*)buf->value);
1229 buf->cur_pos_data += 2;
1231 else if (pt->members[i].type == TOKEN_DWORD)
1233 *(((DWORD*)(buf->cur_pos_data + buf->pdata))) = (DWORD)(*(DWORD*)buf->value);
1234 buf->cur_pos_data += 4;
1238 FIXME("Token %d not supported\n", pt->members[i].type);
1242 else if (token == TOKEN_FLOAT)
1245 TRACE("%s = %f\n", pt->members[i].name, *(float*)buf->value);
1246 if (!check_buffer(buf, 4))
1248 if (pt->members[i].type == TOKEN_FLOAT)
1250 *(((float*)(buf->cur_pos_data + buf->pdata))) = (float)(*(float*)buf->value);
1251 buf->cur_pos_data += 4;
1255 FIXME("Token %d not supported\n", pt->members[i].type);
1259 else if (token == TOKEN_LPSTR)
1262 TRACE("%s = %s\n", pt->members[i].name, (char*)buf->value);
1263 if (!check_buffer(buf, sizeof(LPSTR)))
1265 if (pt->members[i].type == TOKEN_LPSTR)
1267 int len = strlen((char*)buf->value) + 1;
1268 if ((buf->cur_pstrings - buf->pstrings + len) > MAX_STRINGS_BUFFER)
1270 FIXME("Buffer too small %p %p %d\n", buf->cur_pstrings, buf->pstrings, len);
1273 strcpy((char*)buf->cur_pstrings, (char*)buf->value);
1274 *(((LPCSTR*)(buf->cur_pos_data + buf->pdata))) = (char*)buf->cur_pstrings;
1275 buf->cur_pstrings += len;
1276 buf->cur_pos_data += sizeof(LPSTR);
1280 FIXME("Token %d not supported\n", pt->members[i].type);
1286 FIXME("Unexpected token %d\n", token);
1292 if (nb_elems && buf->txt && (check_TOKEN(buf) != TOKEN_CBRACE) && (check_TOKEN(buf) != TOKEN_NAME))
1294 token = get_TOKEN(buf);
1295 if ((token != TOKEN_SEMICOLON) && (token != TOKEN_COMMA))
1303 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional)
1305 buf->pxo->nb_children = 0;
1307 if (!parse_object_members_list(buf))
1312 buf->pxo->size = buf->cur_pos_data - buf->pxo->pos_data;
1314 /* Skip trailing semicolon */
1315 while (check_TOKEN(buf) == TOKEN_SEMICOLON)
1320 if (check_TOKEN(buf) == TOKEN_OBRACE)
1324 if (get_TOKEN(buf) != TOKEN_NAME)
1326 if (get_TOKEN(buf) != TOKEN_CBRACE)
1328 TRACE("Found optional reference %s\n", (char*)buf->value);
1329 for (i = 0; i < (buf->nb_pxo_globals+1); i++)
1331 for (j = 0; j < (buf->pxo_globals[i])[0].nb_subobjects; j++)
1333 if (!strcmp((buf->pxo_globals[i])[j].name, (char*)buf->value))
1338 if (i == (buf->nb_pxo_globals+1))
1340 ERR("Reference to unknown object %s\n", (char*)buf->value);
1344 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1346 FIXME("Too many sub-objects\n");
1350 buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects++];
1351 buf->pxo->children[buf->pxo->nb_children]->ptarget = &(buf->pxo_globals[i])[j];
1352 buf->pxo->children[buf->pxo->nb_children]->binary = FALSE;
1353 buf->pxo->nb_children++;
1355 else if (check_TOKEN(buf) == TOKEN_NAME)
1357 xobject* pxo = buf->pxo;
1359 if (buf->pxo->root->nb_subobjects >= MAX_SUBOBJECTS)
1361 FIXME("Too many sub-objects\n");
1365 buf->pxo = buf->pxo->children[buf->pxo->nb_children] = &buf->pxo_tab[buf->pxo->root->nb_subobjects++];
1367 TRACE("Enter optional %s\n", (char*)buf->value);
1369 if (!parse_object(buf))
1376 buf->pxo->nb_children++;
1383 if (buf->pxo->nb_children > MAX_CHILDREN)
1385 FIXME("Too many children %d\n", buf->pxo->nb_children);
1392 BOOL parse_object(parse_buffer * buf)
1396 buf->pxo->pos_data = buf->cur_pos_data;
1397 buf->pxo->ptarget = NULL;
1398 buf->pxo->binary = FALSE;
1399 buf->pxo->root = buf->pxo_tab;
1401 if (get_TOKEN(buf) != TOKEN_NAME)
1404 /* To do template lookup */
1405 for (i = 0; i < buf->pdxf->nb_xtemplates; i++)
1407 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[i].name))
1409 buf->pxt[buf->level] = &buf->pdxf->xtemplates[i];
1410 memcpy(&buf->pxo->type, &buf->pdxf->xtemplates[i].class_id, 16);
1414 if (i == buf->pdxf->nb_xtemplates)
1416 ERR("Unknown template %s\n", (char*)buf->value);
1420 if (check_TOKEN(buf) == TOKEN_NAME)
1423 strcpy(buf->pxo->name, (char*)buf->value);
1426 buf->pxo->name[0] = 0;
1428 if (get_TOKEN(buf) != TOKEN_OBRACE)
1430 if (check_TOKEN(buf) == TOKEN_GUID)
1433 memcpy(&buf->pxo->class_id, buf->value, 16);
1436 memset(&buf->pxo->class_id, 0, 16);
1438 if (!parse_object_parts(buf, TRUE))
1440 if (get_TOKEN(buf) != TOKEN_CBRACE)
1445 /* Go to the next object */
1446 go_to_next_definition(buf);