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-19 -> size of decompressed file including xof header,
224 * 20-21 -> size of first decompressed MSZIP chunk, 22-23 -> size of first compressed MSZIP chunk
225 * 24-xx -> compressed MSZIP data chunk
226 * xx-xx -> size of next decompressed MSZIP chunk, xx-xx -> size of next compressed MSZIP chunk
227 * xx-xx -> compressed MSZIP data chunk
228 * .............................................................................................. */
230 DWORD decomp_file_size
;
231 WORD decomp_chunk_size
;
232 WORD comp_chunk_size
;
233 LPBYTE decomp_buffer
;
235 if (!read_bytes(buf
, &decomp_file_size
, sizeof(decomp_file_size
)))
236 return DXFILEERR_BADFILETYPE
;
238 TRACE("Compressed format %s detected: decompressed file size with xof header = %d\n",
239 debugstr_fourcc(header
[2]), decomp_file_size
);
241 /* Does not take xof header into account */
242 decomp_file_size
-= 16;
244 decomp_buffer
= HeapAlloc(GetProcessHeap(), 0, decomp_file_size
);
247 ERR("Out of memory\n");
248 return DXFILEERR_BADALLOC
;
250 *decomp_buffer_ptr
= decomp_buffer
;
252 while (buf
->rem_bytes
)
254 if (!read_bytes(buf
, &decomp_chunk_size
, sizeof(decomp_chunk_size
)))
255 return DXFILEERR_BADFILETYPE
;
256 if (!read_bytes(buf
, &comp_chunk_size
, sizeof(comp_chunk_size
)))
257 return DXFILEERR_BADFILETYPE
;
259 TRACE("Process chunk: compressed_size = %d, decompressed_size = %d\n",
260 comp_chunk_size
, decomp_chunk_size
);
262 err
= mszip_decompress(comp_chunk_size
, decomp_chunk_size
, (char*)buf
->buffer
, (char*)decomp_buffer
);
265 WARN("Error while decompressing MSZIP chunk %d\n", err
);
266 HeapFree(GetProcessHeap(), 0, decomp_buffer
);
267 return DXFILEERR_BADALLOC
;
269 buf
->rem_bytes
-= comp_chunk_size
;
270 buf
->buffer
+= comp_chunk_size
;
271 decomp_buffer
+= decomp_chunk_size
;
274 if ((decomp_buffer
- *decomp_buffer_ptr
) != decomp_file_size
)
275 ERR("Size of all decompressed chunks (%u) does not match decompressed file size (%u)\n",
276 (DWORD
)(decomp_buffer
- *decomp_buffer_ptr
), decomp_file_size
);
278 /* Use decompressed data */
279 buf
->buffer
= *decomp_buffer_ptr
;
280 buf
->rem_bytes
= decomp_file_size
;
283 TRACE("Header is correct\n");
288 static void dump_TOKEN(WORD token
)
290 #define DUMP_TOKEN(t) case t: TRACE(#t "\n"); break
293 DUMP_TOKEN(TOKEN_NAME
);
294 DUMP_TOKEN(TOKEN_STRING
);
295 DUMP_TOKEN(TOKEN_INTEGER
);
296 DUMP_TOKEN(TOKEN_GUID
);
297 DUMP_TOKEN(TOKEN_INTEGER_LIST
);
298 DUMP_TOKEN(TOKEN_FLOAT_LIST
);
299 DUMP_TOKEN(TOKEN_OBRACE
);
300 DUMP_TOKEN(TOKEN_CBRACE
);
301 DUMP_TOKEN(TOKEN_OPAREN
);
302 DUMP_TOKEN(TOKEN_CPAREN
);
303 DUMP_TOKEN(TOKEN_OBRACKET
);
304 DUMP_TOKEN(TOKEN_CBRACKET
);
305 DUMP_TOKEN(TOKEN_OANGLE
);
306 DUMP_TOKEN(TOKEN_CANGLE
);
307 DUMP_TOKEN(TOKEN_DOT
);
308 DUMP_TOKEN(TOKEN_COMMA
);
309 DUMP_TOKEN(TOKEN_SEMICOLON
);
310 DUMP_TOKEN(TOKEN_TEMPLATE
);
311 DUMP_TOKEN(TOKEN_WORD
);
312 DUMP_TOKEN(TOKEN_DWORD
);
313 DUMP_TOKEN(TOKEN_FLOAT
);
314 DUMP_TOKEN(TOKEN_DOUBLE
);
315 DUMP_TOKEN(TOKEN_CHAR
);
316 DUMP_TOKEN(TOKEN_UCHAR
);
317 DUMP_TOKEN(TOKEN_SWORD
);
318 DUMP_TOKEN(TOKEN_SDWORD
);
319 DUMP_TOKEN(TOKEN_VOID
);
320 DUMP_TOKEN(TOKEN_LPSTR
);
321 DUMP_TOKEN(TOKEN_UNICODE
);
322 DUMP_TOKEN(TOKEN_CSTRING
);
323 DUMP_TOKEN(TOKEN_ARRAY
);
326 TRACE("Unknown token %d\n", token
);
332 static BOOL
is_space(char c
)
346 static BOOL
is_operator(char c
)
365 static inline BOOL
is_separator(char c
)
367 return is_space(c
) || is_operator(c
);
370 static WORD
get_operator_token(char c
)
379 return TOKEN_OBRACKET
;
381 return TOKEN_CBRACKET
;
393 return TOKEN_SEMICOLON
;
398 static BOOL
is_keyword(parse_buffer
* buf
, const char* keyword
)
400 char tmp
[8]; /* longest keyword size (template) */
401 DWORD len
= strlen(keyword
);
403 if (!read_bytes(buf
, tmp
, len
))
405 if (strncasecmp(tmp
, keyword
, len
))
407 rewind_bytes(buf
, len
);
411 if (!read_bytes(buf
, tmp
, 1))
413 if (is_separator(tmp
[0]))
415 rewind_bytes(buf
, 1);
418 rewind_bytes(buf
, len
+1);
422 static WORD
get_keyword_token(parse_buffer
* buf
)
424 if (is_keyword(buf
, "template"))
425 return TOKEN_TEMPLATE
;
426 if (is_keyword(buf
, "WORD"))
428 if (is_keyword(buf
, "DWORD"))
430 if (is_keyword(buf
, "FLOAT"))
432 if (is_keyword(buf
, "DOUBLE"))
434 if (is_keyword(buf
, "CHAR"))
436 if (is_keyword(buf
, "UCHAR"))
438 if (is_keyword(buf
, "SWORD"))
440 if (is_keyword(buf
, "SDWORD"))
442 if (is_keyword(buf
, "VOID"))
444 if (is_keyword(buf
, "STRING"))
446 if (is_keyword(buf
, "UNICODE"))
447 return TOKEN_UNICODE
;
448 if (is_keyword(buf
, "CSTRING"))
449 return TOKEN_CSTRING
;
450 if (is_keyword(buf
, "array"))
456 static BOOL
is_guid(parse_buffer
* buf
)
464 if (buf
->rem_bytes
< 38 || *buf
->buffer
!= '<')
467 while (pos
< sizeof(tmp
) - 2 && *(buf
->buffer
+pos
) != '>')
469 tmp
[pos
] = *(buf
->buffer
+pos
);
474 if (pos
!= 38 /* <+36+> */)
476 TRACE("Wrong guid %s (%d)\n", tmp
, pos
);
480 buf
->rem_bytes
-= pos
;
482 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);
485 TRACE("Wrong guid %s (%d)\n", tmp
, pos
);
488 TRACE("Found guid %s (%d)\n", tmp
, pos
);
490 class_id
.Data2
= tab
[0];
491 class_id
.Data3
= tab
[1];
492 class_id
.Data4
[0] = tab
[2];
493 class_id
.Data4
[1] = tab
[3];
494 class_id
.Data4
[2] = tab
[4];
495 class_id
.Data4
[3] = tab
[5];
496 class_id
.Data4
[4] = tab
[6];
497 class_id
.Data4
[5] = tab
[7];
498 class_id
.Data4
[6] = tab
[8];
499 class_id
.Data4
[7] = tab
[9];
501 *(GUID
*)buf
->value
= class_id
;
506 static BOOL
is_name(parse_buffer
* buf
)
512 while (pos
< buf
->rem_bytes
&& !is_separator(c
= *(buf
->buffer
+pos
)))
514 if (!(((c
>= 'a') && (c
<= 'z')) || ((c
>= 'A') && (c
<= 'Z')) || ((c
>= '0') && (c
<= '9')) || (c
== '_') || (c
== '-')))
516 if (pos
< sizeof(tmp
))
520 tmp
[min(pos
, sizeof(tmp
) - 1)] = 0;
524 TRACE("Wrong name %s\n", tmp
);
529 buf
->rem_bytes
-= pos
;
531 TRACE("Found name %s\n", tmp
);
532 strcpy((char*)buf
->value
, tmp
);
537 static BOOL
is_float(parse_buffer
* buf
)
545 while (pos
< buf
->rem_bytes
&& !is_separator(c
= *(buf
->buffer
+pos
)))
547 if (!((!pos
&& (c
== '-')) || ((c
>= '0') && (c
<= '9')) || (!dot
&& (c
== '.'))))
551 if (pos
< sizeof(tmp
))
555 tmp
[min(pos
, sizeof(tmp
) - 1)] = 0;
558 buf
->rem_bytes
-= pos
;
560 sscanf(tmp
, "%f", &decimal
);
562 TRACE("Found float %s - %f\n", tmp
, decimal
);
564 *(float*)buf
->value
= decimal
;
569 static BOOL
is_integer(parse_buffer
* buf
)
576 while (pos
< buf
->rem_bytes
&& !is_separator(c
= *(buf
->buffer
+pos
)))
578 if (!((c
>= '0') && (c
<= '9')))
580 if (pos
< sizeof(tmp
))
584 tmp
[min(pos
, sizeof(tmp
) - 1)] = 0;
587 buf
->rem_bytes
-= pos
;
589 sscanf(tmp
, "%d", &integer
);
591 TRACE("Found integer %s - %d\n", tmp
, integer
);
593 *(DWORD
*)buf
->value
= integer
;
598 static BOOL
is_string(parse_buffer
* buf
)
605 if (*buf
->buffer
!= '"')
608 while ((pos
+1) < buf
->rem_bytes
&& !is_operator(c
= *(buf
->buffer
+pos
+1)))
615 if (pos
< sizeof(tmp
))
619 tmp
[min(pos
, sizeof(tmp
) - 1)] = 0;
623 TRACE("Wrong string %s\n", tmp
);
627 buf
->buffer
+= pos
+ 2;
628 buf
->rem_bytes
-= pos
+ 2;
630 TRACE("Found string %s\n", tmp
);
631 strcpy((char*)buf
->value
, tmp
);
636 static WORD
parse_TOKEN(parse_buffer
* buf
)
645 if (!read_bytes(buf
, &c
, 1))
647 /*TRACE("char = '%c'\n", is_space(c) ? ' ' : c);*/
648 if ((c
== '#') || (c
== '/'))
650 /* Handle comment (# or //) */
653 if (!read_bytes(buf
, &c
, 1))
661 if (!read_bytes(buf
, &c
, 1))
668 if (is_operator(c
) && (c
!= '<'))
670 token
= get_operator_token(c
);
680 rewind_bytes(buf
, 1);
682 if ((token
= get_keyword_token(buf
)))
692 token
= TOKEN_INTEGER
;
711 FIXME("Unrecognize element\n");
723 if (!read_bytes(buf
, &token
, 2))
726 /* Convert integer and float list into separate elements */
727 if (token
== TOKEN_INTEGER_LIST
)
729 if (!read_bytes(buf
, &nb_elem
, 4))
731 token
= TOKEN_INTEGER
;
733 TRACE("Integer list (TOKEN_INTEGER_LIST) of size %d\n", nb_elem
);
735 else if (token
== TOKEN_FLOAT_LIST
)
737 if (!read_bytes(buf
, &nb_elem
, 4))
741 TRACE("Float list (TOKEN_FLOAT_LIST) of size %d\n", nb_elem
);
747 token
= is_float
? TOKEN_FLOAT
: TOKEN_INTEGER
;
752 if (!read_bytes(buf
, &integer
, 4))
755 *(DWORD
*)buf
->value
= integer
;
768 if (!read_bytes(buf
, &count
, 4))
770 if (!read_bytes(buf
, strname
, count
))
773 /*TRACE("name = %s\n", strname);*/
775 strcpy((char*)buf
->value
, strname
);
782 if (!read_bytes(buf
, &integer
, 4))
784 /*TRACE("integer = %ld\n", integer);*/
786 *(DWORD
*)buf
->value
= integer
;
794 if (!read_bytes(buf
, &class_id
, 16))
796 sprintf(strguid
, CLSIDFMT
, class_id
.Data1
, class_id
.Data2
, class_id
.Data3
, class_id
.Data4
[0],
797 class_id
.Data4
[1], class_id
.Data4
[2], class_id
.Data4
[3], class_id
.Data4
[4], class_id
.Data4
[5],
798 class_id
.Data4
[6], class_id
.Data4
[7]);
799 /*TRACE("guid = {%s}\n", strguid);*/
801 *(GUID
*)buf
->value
= class_id
;
809 if (!read_bytes(buf
, &count
, 4))
811 if (!read_bytes(buf
, strname
, count
))
814 if (!read_bytes(buf
, &tmp_token
, 2))
816 if ((tmp_token
!= TOKEN_COMMA
) && (tmp_token
!= TOKEN_SEMICOLON
))
817 ERR("No comma or semicolon (got %d)\n", tmp_token
);
818 /*TRACE("name = %s\n", strname);*/
820 strcpy((char*)buf
->value
, strname
);
834 case TOKEN_SEMICOLON
:
860 static WORD
get_TOKEN(parse_buffer
* buf
)
862 if (buf
->token_present
)
864 buf
->token_present
= FALSE
;
865 return buf
->current_token
;
868 buf
->current_token
= parse_TOKEN(buf
);
870 return buf
->current_token
;
873 static WORD
check_TOKEN(parse_buffer
* buf
)
875 if (buf
->token_present
)
876 return buf
->current_token
;
878 buf
->current_token
= parse_TOKEN(buf
);
879 buf
->token_present
= TRUE
;
881 return buf
->current_token
;
884 static BOOL
is_template_available(parse_buffer
* buf
)
886 return check_TOKEN(buf
) == TOKEN_TEMPLATE
;
889 static inline BOOL
is_primitive_type(WORD token
)
914 static BOOL
parse_template_option_info(parse_buffer
* buf
)
916 xtemplate
* cur_template
= &buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
];
918 if (check_TOKEN(buf
) == TOKEN_DOT
)
921 if (get_TOKEN(buf
) != TOKEN_DOT
)
923 if (get_TOKEN(buf
) != TOKEN_DOT
)
925 cur_template
->open
= TRUE
;
931 if (get_TOKEN(buf
) != TOKEN_NAME
)
933 strcpy(cur_template
->children
[cur_template
->nb_children
], (char*)buf
->value
);
934 if (check_TOKEN(buf
) == TOKEN_GUID
)
936 cur_template
->nb_children
++;
937 if (check_TOKEN(buf
) != TOKEN_COMMA
)
941 cur_template
->open
= FALSE
;
947 static BOOL
parse_template_members_list(parse_buffer
* buf
)
956 cur_member
= &buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
].members
[idx_member
];
958 if (check_TOKEN(buf
) == TOKEN_ARRAY
)
964 if (check_TOKEN(buf
) == TOKEN_NAME
)
966 cur_member
->type
= get_TOKEN(buf
);
967 cur_member
->idx_template
= 0;
968 while (cur_member
->idx_template
< buf
->pdxf
->nb_xtemplates
)
970 if (!strcasecmp((char*)buf
->value
, buf
->pdxf
->xtemplates
[cur_member
->idx_template
].name
))
972 cur_member
->idx_template
++;
974 if (cur_member
->idx_template
== buf
->pdxf
->nb_xtemplates
)
976 ERR("Reference to a nonexistent template '%s'\n", (char*)buf
->value
);
980 else if (is_primitive_type(check_TOKEN(buf
)))
981 cur_member
->type
= get_TOKEN(buf
);
985 if (get_TOKEN(buf
) != TOKEN_NAME
)
987 strcpy(cur_member
->name
, (char*)buf
->value
);
991 while (check_TOKEN(buf
) == TOKEN_OBRACKET
)
993 if (nb_dims
>= MAX_ARRAY_DIM
)
995 FIXME("Too many dimensions (%d) for multi-dimensional array\n", nb_dims
+ 1);
999 if (check_TOKEN(buf
) == TOKEN_INTEGER
)
1002 cur_member
->dim_fixed
[nb_dims
] = TRUE
;
1003 cur_member
->dim_value
[nb_dims
] = *(DWORD
*)buf
->value
;
1008 if (get_TOKEN(buf
) != TOKEN_NAME
)
1010 for (i
= 0; i
< idx_member
; i
++)
1012 if (!strcmp((char*)buf
->value
, buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
].members
[i
].name
))
1014 if (buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
].members
[i
].nb_dims
)
1016 ERR("Array cannot be used to specify variable array size\n");
1019 if (buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
].members
[i
].type
!= TOKEN_DWORD
)
1021 FIXME("Only DWORD supported to specify variable array size\n");
1027 if (i
== idx_member
)
1029 ERR("Reference to unknown member %s\n", (char*)buf
->value
);
1032 cur_member
->dim_fixed
[nb_dims
] = FALSE
;
1033 cur_member
->dim_value
[nb_dims
] = i
;
1035 if (get_TOKEN(buf
) != TOKEN_CBRACKET
)
1041 cur_member
->nb_dims
= nb_dims
;
1043 if (get_TOKEN(buf
) != TOKEN_SEMICOLON
)
1049 buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
].nb_members
= idx_member
;
1054 static BOOL
parse_template_parts(parse_buffer
* buf
)
1056 if (!parse_template_members_list(buf
))
1058 if (check_TOKEN(buf
) == TOKEN_OBRACKET
)
1061 if (!parse_template_option_info(buf
))
1063 if (get_TOKEN(buf
) != TOKEN_CBRACKET
)
1070 static void go_to_next_definition(parse_buffer
* buf
)
1073 while (buf
->rem_bytes
)
1075 if (!read_bytes(buf
, &c
, 1))
1077 if ((c
== '#') || (c
== '/'))
1079 /* Handle comment (# or //) */
1082 if (!read_bytes(buf
, &c
, 1))
1090 if (!read_bytes(buf
, &c
, 1))
1095 else if (!is_space(c
))
1097 rewind_bytes(buf
, 1);
1103 static BOOL
parse_template(parse_buffer
* buf
)
1105 if (get_TOKEN(buf
) != TOKEN_TEMPLATE
)
1107 if (get_TOKEN(buf
) != TOKEN_NAME
)
1109 strcpy(buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
].name
, (char*)buf
->value
);
1110 if (get_TOKEN(buf
) != TOKEN_OBRACE
)
1112 if (get_TOKEN(buf
) != TOKEN_GUID
)
1114 buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
].class_id
= *(GUID
*)buf
->value
;
1115 if (!parse_template_parts(buf
))
1117 if (get_TOKEN(buf
) != TOKEN_CBRACE
)
1121 /* Go to the next template */
1122 go_to_next_definition(buf
);
1125 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
));
1126 buf
->pdxf
->nb_xtemplates
++;
1131 BOOL
parse_templates(parse_buffer
* buf
)
1133 while (buf
->rem_bytes
&& is_template_available(buf
))
1135 if (!parse_template(buf
))
1137 WARN("Template is not correct\n");
1142 TRACE("Template successfully parsed:\n");
1143 if (TRACE_ON(d3dxof_parsing
))
1144 dump_template(buf
->pdxf
->xtemplates
, &buf
->pdxf
->xtemplates
[buf
->pdxf
->nb_xtemplates
- 1]);
1150 static BOOL
check_buffer(parse_buffer
* buf
, ULONG size
)
1152 if ((buf
->cur_pos_data
+ size
) > buf
->capacity
)
1155 ULONG new_capacity
= buf
->capacity
? 2 * buf
->capacity
: 100000;
1157 pdata
= HeapAlloc(GetProcessHeap(), 0, new_capacity
);
1160 memcpy(pdata
, buf
->pdata
, buf
->cur_pos_data
);
1161 HeapFree(GetProcessHeap(), 0, buf
->pdata
);
1162 buf
->capacity
= new_capacity
;
1164 buf
->pxo
->root
->pdata
= pdata
;
1169 static BOOL
parse_object_parts(parse_buffer
* buf
, BOOL allow_optional
);
1170 static BOOL
parse_object_members_list(parse_buffer
* buf
)
1174 xtemplate
* pt
= buf
->pxt
[buf
->level
];
1176 buf
->pxo
->nb_members
= pt
->nb_members
;
1178 for (i
= 0; i
< pt
->nb_members
; i
++)
1183 buf
->pxo
->members
[i
].name
= pt
->members
[i
].name
;
1184 buf
->pxo
->members
[i
].start
= buf
->cur_pos_data
;
1186 for (k
= 0; k
< pt
->members
[i
].nb_dims
; k
++)
1188 if (pt
->members
[i
].dim_fixed
[k
])
1189 nb_elems
*= pt
->members
[i
].dim_value
[k
];
1191 nb_elems
*= *(DWORD
*)(buf
->pxo
->root
->pdata
+ buf
->pxo
->members
[pt
->members
[i
].dim_value
[k
]].start
);
1194 TRACE("Elements to consider: %d\n", nb_elems
);
1196 for (k
= 0; k
< nb_elems
; k
++)
1200 token
= check_TOKEN(buf
);
1201 if (token
== TOKEN_COMMA
)
1207 /* Allow comma omission */
1208 if (!((token
== TOKEN_FLOAT
) || (token
== TOKEN_INTEGER
)))
1213 if (pt
->members
[i
].type
== TOKEN_NAME
)
1217 TRACE("Found sub-object %s\n", buf
->pdxf
->xtemplates
[pt
->members
[i
].idx_template
].name
);
1219 /* To do template lookup */
1220 for (j
= 0; j
< buf
->pdxf
->nb_xtemplates
; j
++)
1222 if (!strcasecmp(buf
->pdxf
->xtemplates
[pt
->members
[i
].idx_template
].name
, buf
->pdxf
->xtemplates
[j
].name
))
1224 buf
->pxt
[buf
->level
] = &buf
->pdxf
->xtemplates
[j
];
1228 if (j
== buf
->pdxf
->nb_xtemplates
)
1230 ERR("Unknown template %s\n", (char*)buf
->value
);
1234 TRACE("Enter %s\n", buf
->pdxf
->xtemplates
[pt
->members
[i
].idx_template
].name
);
1235 if (!parse_object_parts(buf
, FALSE
))
1244 token
= check_TOKEN(buf
);
1245 if (token
== TOKEN_INTEGER
)
1248 TRACE("%s = %d\n", pt
->members
[i
].name
, *(DWORD
*)buf
->value
);
1249 /* Assume larger size */
1250 if (!check_buffer(buf
, 4))
1252 if (pt
->members
[i
].type
== TOKEN_WORD
)
1254 *(((WORD
*)(buf
->pdata
+ buf
->cur_pos_data
))) = (WORD
)(*(DWORD
*)buf
->value
);
1255 buf
->cur_pos_data
+= 2;
1257 else if (pt
->members
[i
].type
== TOKEN_DWORD
)
1259 *(((DWORD
*)(buf
->pdata
+ buf
->cur_pos_data
))) = (DWORD
)(*(DWORD
*)buf
->value
);
1260 buf
->cur_pos_data
+= 4;
1264 FIXME("Token %d not supported\n", pt
->members
[i
].type
);
1268 else if (token
== TOKEN_FLOAT
)
1271 TRACE("%s = %f\n", pt
->members
[i
].name
, *(float*)buf
->value
);
1272 if (!check_buffer(buf
, 4))
1274 if (pt
->members
[i
].type
== TOKEN_FLOAT
)
1276 *(((float*)(buf
->pdata
+ buf
->cur_pos_data
))) = (float)(*(float*)buf
->value
);
1277 buf
->cur_pos_data
+= 4;
1281 FIXME("Token %d not supported\n", pt
->members
[i
].type
);
1285 else if (token
== TOKEN_LPSTR
)
1288 TRACE("%s = %s\n", pt
->members
[i
].name
, (char*)buf
->value
);
1289 if (!check_buffer(buf
, sizeof(LPSTR
)))
1291 if (pt
->members
[i
].type
== TOKEN_LPSTR
)
1293 int len
= strlen((char*)buf
->value
) + 1;
1294 if ((buf
->cur_pstrings
- buf
->pstrings
+ len
) > MAX_STRINGS_BUFFER
)
1296 FIXME("Buffer too small %p %p %d\n", buf
->cur_pstrings
, buf
->pstrings
, len
);
1299 strcpy((char*)buf
->cur_pstrings
, (char*)buf
->value
);
1300 *(((LPCSTR
*)(buf
->pdata
+ buf
->cur_pos_data
))) = (char*)buf
->cur_pstrings
;
1301 buf
->cur_pstrings
+= len
;
1302 buf
->cur_pos_data
+= sizeof(LPSTR
);
1306 FIXME("Token %d not supported\n", pt
->members
[i
].type
);
1312 FIXME("Unexpected token %d\n", token
);
1318 /* Empty arrays can have the semicolon at the end or not so remove it if any and skip next check */
1319 if (!nb_elems
&& (check_TOKEN(buf
) == TOKEN_SEMICOLON
))
1322 if (nb_elems
&& buf
->txt
&& (check_TOKEN(buf
) != TOKEN_CBRACE
) && (check_TOKEN(buf
) != TOKEN_NAME
))
1324 token
= get_TOKEN(buf
);
1325 if ((token
!= TOKEN_SEMICOLON
) && (token
!= TOKEN_COMMA
))
1328 buf
->pxo
->members
[i
].size
= buf
->cur_pos_data
- buf
->pxo
->members
[i
].start
;
1334 static BOOL
parse_object_parts(parse_buffer
* buf
, BOOL allow_optional
)
1336 buf
->pxo
->nb_children
= 0;
1338 if (!parse_object_members_list(buf
))
1343 buf
->pxo
->size
= buf
->cur_pos_data
- buf
->pxo
->pos_data
;
1345 /* Skip trailing semicolon */
1346 while (check_TOKEN(buf
) == TOKEN_SEMICOLON
)
1351 if (check_TOKEN(buf
) == TOKEN_OBRACE
)
1355 if (get_TOKEN(buf
) != TOKEN_NAME
)
1357 if (get_TOKEN(buf
) != TOKEN_CBRACE
)
1359 TRACE("Found optional reference %s\n", (char*)buf
->value
);
1360 for (i
= 0; i
< (buf
->nb_pxo_globals
+1); i
++)
1362 for (j
= 0; j
< (buf
->pxo_globals
[i
])[0].nb_subobjects
; j
++)
1364 if (!strcmp((buf
->pxo_globals
[i
])[j
].name
, (char*)buf
->value
))
1369 if (i
== (buf
->nb_pxo_globals
+1))
1371 ERR("Reference to unknown object %s\n", (char*)buf
->value
);
1375 if (buf
->pxo
->root
->nb_subobjects
>= MAX_SUBOBJECTS
)
1377 FIXME("Too many sub-objects\n");
1381 buf
->pxo
->children
[buf
->pxo
->nb_children
] = &buf
->pxo_tab
[buf
->pxo
->root
->nb_subobjects
++];
1382 buf
->pxo
->children
[buf
->pxo
->nb_children
]->ptarget
= &(buf
->pxo_globals
[i
])[j
];
1383 buf
->pxo
->children
[buf
->pxo
->nb_children
]->binary
= FALSE
;
1384 buf
->pxo
->nb_children
++;
1386 else if (check_TOKEN(buf
) == TOKEN_NAME
)
1388 xobject
* pxo
= buf
->pxo
;
1390 if (buf
->pxo
->root
->nb_subobjects
>= MAX_SUBOBJECTS
)
1392 FIXME("Too many sub-objects\n");
1396 buf
->pxo
= buf
->pxo
->children
[buf
->pxo
->nb_children
] = &buf
->pxo_tab
[buf
->pxo
->root
->nb_subobjects
++];
1398 TRACE("Enter optional %s\n", (char*)buf
->value
);
1400 if (!parse_object(buf
))
1407 buf
->pxo
->nb_children
++;
1414 if (buf
->pxo
->nb_children
> MAX_CHILDREN
)
1416 FIXME("Too many children %d\n", buf
->pxo
->nb_children
);
1423 BOOL
parse_object(parse_buffer
* buf
)
1427 buf
->pxo
->pos_data
= buf
->cur_pos_data
;
1428 buf
->pxo
->ptarget
= NULL
;
1429 buf
->pxo
->binary
= FALSE
;
1430 buf
->pxo
->root
= buf
->pxo_tab
;
1432 if (get_TOKEN(buf
) != TOKEN_NAME
)
1435 /* To do template lookup */
1436 for (i
= 0; i
< buf
->pdxf
->nb_xtemplates
; i
++)
1438 if (!strcasecmp((char*)buf
->value
, buf
->pdxf
->xtemplates
[i
].name
))
1440 buf
->pxt
[buf
->level
] = &buf
->pdxf
->xtemplates
[i
];
1441 memcpy(&buf
->pxo
->type
, &buf
->pdxf
->xtemplates
[i
].class_id
, 16);
1445 if (i
== buf
->pdxf
->nb_xtemplates
)
1447 ERR("Unknown template %s\n", (char*)buf
->value
);
1451 if (check_TOKEN(buf
) == TOKEN_NAME
)
1454 strcpy(buf
->pxo
->name
, (char*)buf
->value
);
1457 buf
->pxo
->name
[0] = 0;
1459 if (get_TOKEN(buf
) != TOKEN_OBRACE
)
1461 if (check_TOKEN(buf
) == TOKEN_GUID
)
1464 memcpy(&buf
->pxo
->class_id
, buf
->value
, 16);
1467 memset(&buf
->pxo
->class_id
, 0, 16);
1469 if (!parse_object_parts(buf
, TRUE
))
1471 if (get_TOKEN(buf
) != TOKEN_CBRACE
)
1476 /* Go to the next object */
1477 go_to_next_definition(buf
);