dxbc_assemble.cpp revision 70fed0b0ec8a3ec4f6b9b47f1fe98cc54c6037f0
1/**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27#include <stdlib.h>
28#include <string.h>
29#include "dxbc.h"
30
31std::pair<void*, size_t> dxbc_assemble(struct dxbc_chunk_header** chunks, unsigned num_chunks)
32{
33	size_t data_size = 0;
34	for(unsigned i = 0; i < num_chunks; ++i)
35		data_size += sizeof(uint32_t) + sizeof(dxbc_chunk_header) + bswap_le32(chunks[i]->size);
36
37	size_t total_size = sizeof(dxbc_container_header) + data_size;
38	dxbc_container_header* header = (dxbc_container_header*)malloc(total_size);
39	if(!header)
40		return std::make_pair((void*)0, 0);
41
42	header->fourcc = bswap_le32(FOURCC_DXBC);
43	memset(header->unk, 0, sizeof(header->unk));
44	header->one = bswap_le32(1);
45	header->total_size = bswap_le32(total_size);
46	header->chunk_count = num_chunks;
47
48	uint32_t* chunk_offsets = (uint32_t*)(header + 1);
49	uint32_t off = sizeof(struct dxbc_container_header) + num_chunks * sizeof(uint32_t);
50	for(unsigned i = 0; i < num_chunks; ++i)
51	{
52		chunk_offsets[i] = bswap_le32(off);
53		unsigned chunk_full_size = sizeof(dxbc_chunk_header) + bswap_le32(chunks[i]->size);
54		memcpy((char*)header + off, chunks[i], chunk_full_size);
55		off += chunk_full_size;
56	}
57
58	return std::make_pair((void*)header, total_size);
59}
60