1 /*
2 bParse
3 Copyright (c) 2006-2009 Charlie C & Erwin Coumans  http://gamekit.googlecode.com
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #ifndef __BFILE_H__
17 #define __BFILE_H__
18 
19 #include "b3Common.h"
20 #include "b3Chunk.h"
21 #include <stdio.h>
22 
23 namespace bParse
24 {
25 // ----------------------------------------------------- //
26 enum bFileFlags
27 {
28 	FD_INVALID = 0,
29 	FD_OK = 1,
30 	FD_VOID_IS_8 = 2,
31 	FD_ENDIAN_SWAP = 4,
32 	FD_FILE_64 = 8,
33 	FD_BITS_VARIES = 16,
34 	FD_VERSION_VARIES = 32,
35 	FD_DOUBLE_PRECISION = 64,
36 	FD_BROKEN_DNA = 128
37 };
38 
39 enum bFileVerboseMode
40 {
41 	FD_VERBOSE_EXPORT_XML = 1,
42 	FD_VERBOSE_DUMP_DNA_TYPE_DEFINITIONS = 2,
43 	FD_VERBOSE_DUMP_CHUNKS = 4,
44 	FD_VERBOSE_DUMP_FILE_INFO = 8,
45 };
46 // ----------------------------------------------------- //
47 class bFile
48 {
49 protected:
50 	char m_headerString[7];
51 
52 	bool mOwnsBuffer;
53 	char *mFileBuffer;
54 	int mFileLen;
55 	int mVersion;
56 
57 	bPtrMap mLibPointers;
58 
59 	int mDataStart;
60 	bDNA *mFileDNA;
61 	bDNA *mMemoryDNA;
62 
63 	b3AlignedObjectArray<char *> m_pointerFixupArray;
64 	b3AlignedObjectArray<char *> m_pointerPtrFixupArray;
65 
66 	b3AlignedObjectArray<bChunkInd> m_chunks;
67 	b3HashMap<b3HashPtr, bChunkInd> m_chunkPtrPtrMap;
68 
69 	//
70 
71 	bPtrMap mDataPointers;
72 
73 	int mFlags;
74 
75 	// ////////////////////////////////////////////////////////////////////////////
76 
77 	// buffer offset util
78 	int getNextBlock(bChunkInd *dataChunk, const char *dataPtr, const int flags);
79 	void safeSwapPtr(char *dst, const char *src);
80 
81 	virtual void parseHeader();
82 
83 	virtual void parseData() = 0;
84 
85 	void resolvePointersMismatch();
86 	void resolvePointersChunk(const bChunkInd &dataChunk, int verboseMode);
87 
88 	int resolvePointersStructRecursive(char *strcPtr, int old_dna, int verboseMode, int recursion);
89 	//void swapPtr(char *dst, char *src);
90 
91 	void parseStruct(char *strcPtr, char *dtPtr, int old_dna, int new_dna, bool fixupPointers);
92 	void getMatchingFileDNA(short *old, const char *lookupName, const char *lookupType, char *strcData, char *data, bool fixupPointers);
93 	char *getFileElement(short *firstStruct, char *lookupName, char *lookupType, char *data, short **foundPos);
94 
95 	void swap(char *head, class bChunkInd &ch, bool ignoreEndianFlag);
96 	void swapData(char *data, short type, int arraySize, bool ignoreEndianFlag);
97 	void swapStruct(int dna_nr, char *data, bool ignoreEndianFlag);
98 	void swapLen(char *dataPtr);
99 	void swapDNA(char *ptr);
100 
101 	char *readStruct(char *head, class bChunkInd &chunk);
102 	char *getAsString(int code);
103 
104 	void parseInternal(int verboseMode, char *memDna, int memDnaLength);
105 
106 public:
107 	bFile(const char *filename, const char headerString[7]);
108 
109 	//todo: make memoryBuffer const char
110 	//bFile( const char *memoryBuffer, int len);
111 	bFile(char *memoryBuffer, int len, const char headerString[7]);
112 	virtual ~bFile();
113 
getFileDNA()114 	bDNA *getFileDNA()
115 	{
116 		return mFileDNA;
117 	}
118 
119 	virtual void addDataBlock(char *dataBlock) = 0;
120 
getFlags()121 	int getFlags() const
122 	{
123 		return mFlags;
124 	}
125 
getLibPointers()126 	bPtrMap &getLibPointers()
127 	{
128 		return mLibPointers;
129 	}
130 
131 	void *findLibPointer(void *ptr);
132 
133 	bool ok();
134 
135 	virtual void parse(int verboseMode) = 0;
136 
137 	virtual int write(const char *fileName, bool fixupPointers = false) = 0;
138 
139 	virtual void writeChunks(FILE *fp, bool fixupPointers);
140 
141 	virtual void writeDNA(FILE *fp) = 0;
142 
143 	void updateOldPointers();
144 	void resolvePointers(int verboseMode);
145 
146 	void dumpChunks(bDNA *dna);
147 
getVersion()148 	int getVersion() const
149 	{
150 		return mVersion;
151 	}
152 	//pre-swap the endianness, so that data loaded on a target with different endianness doesn't need to be swapped
153 	void preSwap();
154 	void writeFile(const char *fileName);
155 };
156 }  // namespace bParse
157 
158 #endif  //__BFILE_H__
159