/* bParse Copyright (c) 2006-2009 Charlie C & Erwin Coumans http://gamekit.googlecode.com This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 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. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ #include "b3File.h" #include "b3Common.h" #include "b3Chunk.h" #include "b3DNA.h" #include #include #include #include "b3Defines.h" #include "Bullet3Serialize/Bullet2FileLoader/b3Serializer.h" #include "Bullet3Common/b3AlignedAllocator.h" #include "Bullet3Common/b3MinMax.h" #define B3_SIZEOFBLENDERHEADER 12 #define MAX_ARRAY_LENGTH 512 using namespace bParse; #define MAX_STRLEN 1024 const char* getCleanName(const char* memName, char* buffer) { int slen = strlen(memName); assert(slen 0) { if (strncmp((tempBuffer + ChunkUtils::getOffset(mFlags)), "SDNANAME", 8) ==0) dna.oldPtr = (tempBuffer + ChunkUtils::getOffset(mFlags)); else dna.oldPtr = 0; } else dna.oldPtr = 0; } // Some Bullet files are missing the DNA1 block // In Blender it's DNA1 + ChunkUtils::getOffset() + SDNA + NAME // In Bullet tests its SDNA + NAME else if (strncmp(tempBuffer, "SDNANAME", 8) ==0) { dna.oldPtr = blenderData + i; dna.len = mFileLen-i; // Also no REND block, so exit now. if (mVersion==276) break; } if (mDataStart && dna.oldPtr) break; tempBuffer++; } if (!dna.oldPtr || !dna.len) { //printf("Failed to find DNA1+SDNA pair\n"); mFlags &= ~FD_OK; return; } mFileDNA = new bDNA(); ///mFileDNA->init will convert part of DNA file endianness to current CPU endianness if necessary mFileDNA->init((char*)dna.oldPtr, dna.len, (mFlags & FD_ENDIAN_SWAP)!=0); if (mVersion==276) { int i; for (i=0;igetNumNames();i++) { if (strcmp(mFileDNA->getName(i),"int")==0) { mFlags |= FD_BROKEN_DNA; } } if ((mFlags&FD_BROKEN_DNA)!=0) { //printf("warning: fixing some broken DNA version\n"); } } if (verboseMode & FD_VERBOSE_DUMP_DNA_TYPE_DEFINITIONS) mFileDNA->dumpTypeDefinitions(); mMemoryDNA = new bDNA(); int littleEndian= 1; littleEndian= ((char*)&littleEndian)[0]; mMemoryDNA->init(memDna,memDnaLength,littleEndian==0); ///@todo we need a better version check, add version/sub version info from FileGlobal into memory DNA/header files if (mMemoryDNA->getNumNames() != mFileDNA->getNumNames()) { mFlags |= FD_VERSION_VARIES; //printf ("Warning, file DNA is different than built in, performance is reduced. Best to re-export file with a matching version/platform"); } // as long as it kept up to date it will be ok!! if (mMemoryDNA->lessThan(mFileDNA)) { //printf ("Warning, file DNA is newer than built in."); } mFileDNA->initCmpFlags(mMemoryDNA); parseData(); resolvePointers(verboseMode); updateOldPointers(); } // ----------------------------------------------------- // void bFile::swap(char *head, bChunkInd& dataChunk, bool ignoreEndianFlag) { char *data = head; short *strc = mFileDNA->getStruct(dataChunk.dna_nr); const char s[] = "SoftBodyMaterialData"; int szs = sizeof(s); if (strncmp((char*)&dataChunk.code,"ARAY",4)==0) { short *oldStruct = mFileDNA->getStruct(dataChunk.dna_nr); char *oldType = mFileDNA->getType(oldStruct[0]); if (strncmp(oldType,s,szs)==0) { return; } } int len = mFileDNA->getLength(strc[0]); for (int i=0; icode & 0xFFFF)==0) c->code >>=16; B3_SWITCH_INT(c->len); B3_SWITCH_INT(c->dna_nr); B3_SWITCH_INT(c->nr); } else { bChunkPtr8* c = (bChunkPtr8*) dataPtr; if ((c->code & 0xFFFF)==0) c->code >>=16; B3_SWITCH_INT(c->len); B3_SWITCH_INT(c->dna_nr); B3_SWITCH_INT(c->nr); } } else { if (mFlags &FD_BITS_VARIES) { bChunkPtr8*c = (bChunkPtr8*) dataPtr; if ((c->code & 0xFFFF)==0) c->code >>=16; B3_SWITCH_INT(c->len); B3_SWITCH_INT(c->dna_nr); B3_SWITCH_INT(c->nr); } else { bChunkPtr4* c = (bChunkPtr4*) dataPtr; if ((c->code & 0xFFFF)==0) c->code >>=16; B3_SWITCH_INT(c->len); B3_SWITCH_INT(c->dna_nr); B3_SWITCH_INT(c->nr); } } } void bFile::swapDNA(char* ptr) { bool swap = ((mFlags & FD_ENDIAN_SWAP)!=0); char* data = &ptr[20]; // void bDNA::init(char *data, int len, bool swap) int *intPtr=0;short *shtPtr=0; char *cp = 0;int dataLen =0; //long nr=0; intPtr = (int*)data; /* SDNA (4 bytes) (magic number) NAME (4 bytes) (4 bytes) amount of names (int) */ if (strncmp(data, "SDNA", 4)==0) { // skip ++ NAME intPtr++; intPtr++; } // Parse names if (swap) dataLen = ChunkUtils::swapInt(*intPtr); else dataLen = *intPtr; *intPtr = ChunkUtils::swapInt(*intPtr); intPtr++; cp = (char*)intPtr; int i; for ( i=0; i amount of types (int) */ intPtr = (int*)cp; assert(strncmp(cp, "TYPE", 4)==0); intPtr++; if (swap) dataLen = ChunkUtils::swapInt(*intPtr); else dataLen = *intPtr; *intPtr = ChunkUtils::swapInt(*intPtr); intPtr++; cp = (char*)intPtr; for ( i=0; i (short) the lengths of types */ // Parse type lens intPtr = (int*)cp; assert(strncmp(cp, "TLEN", 4)==0); intPtr++; shtPtr = (short*)intPtr; for ( i=0; i amount of structs (int) */ intPtr = (int*)shtPtr; cp = (char*)intPtr; assert(strncmp(cp, "STRC", 4)==0); intPtr++; if (swap) dataLen = ChunkUtils::swapInt(*intPtr); else dataLen = *intPtr; *intPtr = ChunkUtils::swapInt(*intPtr); intPtr++; shtPtr = (short*)intPtr; for ( i=0; i=0) { swap(dataPtrHead, dataChunk,ignoreEndianFlag); } else { //printf("unknown chunk\n"); } } // next please! dataPtr += seek; seek = getNextBlock(&dataChunk, dataPtr, mFlags); if (seek < 0) break; } if (mFlags & FD_ENDIAN_SWAP) { mFlags &= ~FD_ENDIAN_SWAP; } else { mFlags |= FD_ENDIAN_SWAP; } } // ----------------------------------------------------- // char* bFile::readStruct(char *head, bChunkInd& dataChunk) { bool ignoreEndianFlag = false; if (mFlags & FD_ENDIAN_SWAP) swap(head, dataChunk, ignoreEndianFlag); if (!mFileDNA->flagEqual(dataChunk.dna_nr)) { // Ouch! need to rebuild the struct short *oldStruct,*curStruct; char *oldType, *newType; int oldLen, curLen, reverseOld; oldStruct = mFileDNA->getStruct(dataChunk.dna_nr); oldType = mFileDNA->getType(oldStruct[0]); oldLen = mFileDNA->getLength(oldStruct[0]); if ((mFlags&FD_BROKEN_DNA)!=0) { if ((strcmp(oldType,"b3QuantizedBvhNodeData")==0)&&oldLen==20) { return 0; } if ((strcmp(oldType,"b3ShortIntIndexData")==0)) { int allocLen = 2; char *dataAlloc = new char[(dataChunk.nr*allocLen)+1]; memset(dataAlloc, 0, (dataChunk.nr*allocLen)+1); short* dest = (short*) dataAlloc; const short* src = (short*) head; for (int i=0;igetReverseType(oldType); if ((reverseOld!=-1)) { // make sure it's here //assert(reverseOld!= -1 && "getReverseType() returned -1, struct required!"); // curStruct = mMemoryDNA->getStruct(reverseOld); newType = mMemoryDNA->getType(curStruct[0]); curLen = mMemoryDNA->getLength(curStruct[0]); // make sure it's the same assert((strcmp(oldType, newType)==0) && "internal error, struct mismatch!"); // numBlocks * length int allocLen = (curLen); char *dataAlloc = new char[(dataChunk.nr*allocLen)+1]; memset(dataAlloc, 0, (dataChunk.nr*allocLen)); // track allocated addDataBlock(dataAlloc); char *cur = dataAlloc; char *old = head; for (int block=0; blockgetStruct(dataChunk.dna_nr); oldType = mFileDNA->getType(oldStruct[0]); printf("%s equal structure, just memcpy\n",oldType); #endif // } char *dataAlloc = new char[(dataChunk.len)+1]; memset(dataAlloc, 0, dataChunk.len+1); // track allocated addDataBlock(dataAlloc); memcpy(dataAlloc, head, dataChunk.len); return dataAlloc; } // ----------------------------------------------------- // void bFile::parseStruct(char *strcPtr, char *dtPtr, int old_dna, int new_dna, bool fixupPointers) { if (old_dna == -1) return; if (new_dna == -1) return; //disable this, because we need to fixup pointers/ListBase if (0)//mFileDNA->flagEqual(old_dna)) { short *strc = mFileDNA->getStruct(old_dna); int len = mFileDNA->getLength(strc[0]); memcpy(strcPtr, dtPtr, len); return; } // Ok, now build the struct char *memType, *memName, *cpc, *cpo; short *fileStruct, *filePtrOld, *memoryStruct, *firstStruct; int elementLength, size, revType, old_nr, new_nr, fpLen; short firstStructType; // File to memory lookup memoryStruct = mMemoryDNA->getStruct(new_dna); fileStruct = mFileDNA->getStruct(old_dna); firstStruct = fileStruct; filePtrOld = fileStruct; firstStructType = mMemoryDNA->getStruct(0)[0]; // Get number of elements elementLength = memoryStruct[1]; memoryStruct+=2; cpc = strcPtr; cpo = 0; for (int ele=0; elegetType(memoryStruct[0]); memName = mMemoryDNA->getName(memoryStruct[1]); size = mMemoryDNA->getElementSize(memoryStruct[0], memoryStruct[1]); revType = mMemoryDNA->getReverseType(memoryStruct[0]); if (revType != -1 && memoryStruct[0]>=firstStructType && memName[0] != '*') { cpo = getFileElement(firstStruct, memName, memType, dtPtr, &filePtrOld); if (cpo) { int arrayLen = mFileDNA->getArraySizeNew(filePtrOld[1]); old_nr = mFileDNA->getReverseType(memType); new_nr = revType; fpLen = mFileDNA->getElementSize(filePtrOld[0], filePtrOld[1]); if (arrayLen==1) { parseStruct(cpc, cpo, old_nr, new_nr,fixupPointers); } else { char* tmpCpc = cpc; char* tmpCpo = cpo; for (int i=0;i3 && type <8) { char c; char *cp = data; for (int i=0; igetPointerSize(); int ptrMem = mMemoryDNA->getPointerSize(); if (!src && !dst) return; if (ptrFile == ptrMem) { memcpy(dst, src, ptrMem); } else if (ptrMem==4 && ptrFile==8) { b3PointerUid* oldPtr = (b3PointerUid*)src; b3PointerUid* newPtr = (b3PointerUid*)dst; if (oldPtr->m_uniqueIds[0] == oldPtr->m_uniqueIds[1]) { //Bullet stores the 32bit unique ID in both upper and lower part of 64bit pointers //so it can be used to distinguish between .blend and .bullet newPtr->m_uniqueIds[0] = oldPtr->m_uniqueIds[0]; } else { //deal with pointers the Blender .blend style way, see //readfile.c in the Blender source tree b3Long64 longValue = *((b3Long64*)src); //endian swap for 64bit pointer otherwise truncation will fail due to trailing zeros if (mFlags & FD_ENDIAN_SWAP) B3_SWITCH_LONGINT(longValue); *((int*)dst) = (int)(longValue>>3); } } else if (ptrMem==8 && ptrFile==4) { b3PointerUid* oldPtr = (b3PointerUid*)src; b3PointerUid* newPtr = (b3PointerUid*)dst; if (oldPtr->m_uniqueIds[0] == oldPtr->m_uniqueIds[1]) { newPtr->m_uniqueIds[0] = oldPtr->m_uniqueIds[0]; newPtr->m_uniqueIds[1] = 0; } else { *((b3Long64*)dst)= *((int*)src); } } else { printf ("%d %d\n", ptrFile,ptrMem); assert(0 && "Invalid pointer len"); } } // ----------------------------------------------------- // void bFile::getMatchingFileDNA(short* dna_addr, const char* lookupName, const char* lookupType, char *strcData, char *data, bool fixupPointers) { // find the matching memory dna data // to the file being loaded. Fill the // memory with the file data... int len = dna_addr[1]; dna_addr+=2; for (int i=0; igetType(dna_addr[0]); const char* name = mFileDNA->getName(dna_addr[1]); int eleLen = mFileDNA->getElementSize(dna_addr[0], dna_addr[1]); if ((mFlags&FD_BROKEN_DNA)!=0) { if ((strcmp(type,"short")==0)&&(strcmp(name,"int")==0)) { eleLen = 0; } } if (strcmp(lookupName, name)==0) { //int arrayLenold = mFileDNA->getArraySize((char*)name.c_str()); int arrayLen = mFileDNA->getArraySizeNew(dna_addr[1]); //assert(arrayLenold == arrayLen); if (name[0] == '*') { // cast pointers int ptrFile = mFileDNA->getPointerSize(); int ptrMem = mMemoryDNA->getPointerSize(); safeSwapPtr(strcData,data); if (fixupPointers) { if (arrayLen > 1) { //void **sarray = (void**)strcData; //void **darray = (void**)data; char *cpc, *cpo; cpc = (char*)strcData; cpo = (char*)data; for (int a=0; agetStruct(old_nr); int elementLength = old[1]; old+=2; for (int i=0; igetType(old[0]); char* name = mFileDNA->getName(old[1]); int len = mFileDNA->getElementSize(old[0], old[1]); if (strcmp(lookupName, name)==0) { if (strcmp(type, lookupType)==0) { if (foundPos) *foundPos = old; return data; } return 0; } data+=len; } return 0; } // ----------------------------------------------------- // void bFile::swapStruct(int dna_nr, char *data,bool ignoreEndianFlag) { if (dna_nr == -1) return; short *strc = mFileDNA->getStruct(dna_nr); //short *firstStrc = strc; int elementLen= strc[1]; strc+=2; short first = mFileDNA->getStruct(0)[0]; char *buf = data; for (int i=0; igetType(strc[0]); char *name = mFileDNA->getName(strc[1]); int size = mFileDNA->getElementSize(strc[0], strc[1]); if (strc[0] >= first && name[0]!='*') { int old_nr = mFileDNA->getReverseType(type); int arrayLen = mFileDNA->getArraySizeNew(strc[1]); if (arrayLen==1) { swapStruct(old_nr,buf,ignoreEndianFlag); } else { char* tmpBuf = buf; for (int i=0;igetArraySize(name); int arrayLen = mFileDNA->getArraySizeNew(strc[1]); //assert(arrayLenOld == arrayLen); swapData(buf, strc[0], arrayLen,ignoreEndianFlag); } buf+=size; } } void bFile::resolvePointersMismatch() { // printf("resolvePointersStructMismatch\n"); int i; for (i=0;i< m_pointerFixupArray.size();i++) { char* cur = m_pointerFixupArray.at(i); void** ptrptr = (void**) cur; void* ptr = *ptrptr; ptr = findLibPointer(ptr); if (ptr) { //printf("Fixup pointer!\n"); *(ptrptr) = ptr; } else { // printf("pointer not found: %x\n",cur); } } for (i=0; igetPointerSize(); int ptrFile = mFileDNA->getPointerSize(); int blockLen = block->len / ptrFile; void *onptr = findLibPointer(*ptrptr); if (onptr) { char *newPtr = new char[blockLen * ptrMem]; addDataBlock(newPtr); memset(newPtr, 0, blockLen * ptrMem); void **onarray = (void**)onptr; char *oldPtr = (char*)onarray; int p = 0; while (blockLen-- > 0) { b3PointerUid dp = {{0}}; safeSwapPtr((char*)dp.m_uniqueIds, oldPtr); void **tptr = (void**)(newPtr + p * ptrMem); *tptr = findLibPointer(dp.m_ptr); oldPtr += ptrFile; ++p; } *ptrptr = newPtr; } } } } ///this loop only works fine if the Blender DNA structure of the file matches the headerfiles void bFile::resolvePointersChunk(const bChunkInd& dataChunk, int verboseMode) { bParse::bDNA* fileDna = mFileDNA ? mFileDNA : mMemoryDNA; short int* oldStruct = fileDna->getStruct(dataChunk.dna_nr); short oldLen = fileDna->getLength(oldStruct[0]); //char* structType = fileDna->getType(oldStruct[0]); char* cur = (char*)findLibPointer(dataChunk.oldPtr); for (int block=0; blockgetStruct(0)[0]; char* elemPtr= strcPtr; short int* oldStruct = fileDna->getStruct(dna_nr); int elementLength = oldStruct[1]; oldStruct+=2; int totalSize = 0; for (int ele=0; elegetType(oldStruct[0]); memName = fileDna->getName(oldStruct[1]); int arrayLen = fileDna->getArraySizeNew(oldStruct[1]); if (memName[0] == '*') { if (arrayLen > 1) { void **array= (void**)elemPtr; for (int a=0; a ",&memName[1]); printf("%p ", array[a]); printf("\n",&memName[1]); } array[a] = findLibPointer(array[a]); } } else { void** ptrptr = (void**) elemPtr; void* ptr = *ptrptr; if (verboseMode & FD_VERBOSE_EXPORT_XML) { for (int i=0;i ",&memName[1]); printf("%p ", ptr); printf("\n",&memName[1]); } ptr = findLibPointer(ptr); if (ptr) { // printf("Fixup pointer at 0x%x from 0x%x to 0x%x!\n",ptrptr,*ptrptr,ptr); *(ptrptr) = ptr; if (memName[1] == '*' && ptrptr && *ptrptr) { // This will only work if the given **array is continuous void **array= (void**)*(ptrptr); void *np= array[0]; int n=0; while (np) { np= findLibPointer(array[n]); if (np) array[n]= np; n++; } } } else { // printf("Cannot fixup pointer at 0x%x from 0x%x to 0x%x!\n",ptrptr,*ptrptr,ptr); } } } else { int revType = fileDna->getReverseType(oldStruct[0]); if (oldStruct[0]>=firstStructType) //revType != -1 && { char cleanName[MAX_STRLEN]; getCleanName(memName,cleanName); int arrayLen = fileDna->getArraySizeNew(oldStruct[1]); int byteOffset = 0; if (verboseMode & FD_VERBOSE_EXPORT_XML) { for (int i=0;i1) { printf("<%s type=\"%s\" count=%d>\n",cleanName,memType, arrayLen); } else { printf("<%s type=\"%s\">\n",cleanName,memType); } } for (int i=0;i\n",cleanName); } } else { //export a simple type if (verboseMode & FD_VERBOSE_EXPORT_XML) { if (arrayLen>MAX_ARRAY_LENGTH) { printf("too long\n"); } else { //printf("%s %s\n",memType,memName); bool isIntegerType = (strcmp(memType,"char")==0) || (strcmp(memType,"int")==0) || (strcmp(memType,"short")==0); if (isIntegerType) { const char* newtype="int"; int dbarray[MAX_ARRAY_LENGTH]; int* dbPtr = 0; char* tmp = elemPtr; dbPtr = &dbarray[0]; if (dbPtr) { char cleanName[MAX_STRLEN]; getCleanName(memName,cleanName); int i; getElement(arrayLen, newtype,memType, tmp, (char*)dbPtr); for (i=0;i",cleanName,memType); else printf("<%s type=\"%s\" count=%d>",cleanName,memType,arrayLen); for (i=0;i\n",cleanName); } } else { const char* newtype="double"; double dbarray[MAX_ARRAY_LENGTH]; double* dbPtr = 0; char* tmp = elemPtr; dbPtr = &dbarray[0]; if (dbPtr) { int i; getElement(arrayLen, newtype,memType, tmp, (char*)dbPtr); for (i=0;i",memName,memType); } else { printf("<%s type=\"%s\" count=%d>",cleanName,memType,arrayLen); } for (i=0;i\n",cleanName); } } } } } } int size = fileDna->getElementSize(oldStruct[0], oldStruct[1]); totalSize += size; elemPtr+=size; } return totalSize; } ///Resolve pointers replaces the original pointers in structures, and linked lists by the new in-memory structures void bFile::resolvePointers(int verboseMode) { bParse::bDNA* fileDna = mFileDNA ? mFileDNA : mMemoryDNA; //char *dataPtr = mFileBuffer+mDataStart; if (1) //mFlags & (FD_BITS_VARIES | FD_VERSION_VARIES)) { resolvePointersMismatch(); } { if (verboseMode & FD_VERBOSE_EXPORT_XML) { printf("\n"); int numitems = m_chunks.size(); printf("\n", b3GetVersion(), numitems); } for (int i=0;iflagEqual(dataChunk.dna_nr)) { //dataChunk.len short int* oldStruct = fileDna->getStruct(dataChunk.dna_nr); char* oldType = fileDna->getType(oldStruct[0]); if (verboseMode & FD_VERBOSE_EXPORT_XML) printf(" <%s pointer=%p>\n",oldType,dataChunk.oldPtr); resolvePointersChunk(dataChunk, verboseMode); if (verboseMode & FD_VERBOSE_EXPORT_XML) printf(" \n",oldType); } else { //printf("skipping mStruct\n"); } } if (verboseMode & FD_VERBOSE_EXPORT_XML) { printf("\n"); } } } // ----------------------------------------------------- // void* bFile::findLibPointer(void *ptr) { bStructHandle** ptrptr = getLibPointers().find(ptr); if (ptrptr) return *ptrptr; return 0; } void bFile::updateOldPointers() { int i; for (i=0;igetStruct(dataChunk.dna_nr); char* typeName = dna->getType(newStruct[0]); printf("%3d: %s ",i,typeName); printf("code=%s ",codestr); printf("ptr=%p ",dataChunk.oldPtr); printf("len=%d ",dataChunk.len); printf("nr=%d ",dataChunk.nr); if (dataChunk.nr!=1) { printf("not 1\n"); } printf("\n"); } #if 0 IDFinderData ifd; ifd.success = 0; ifd.IDname = NULL; ifd.just_print_it = 1; for (i=0; im_blocks.size(); ++i) { BlendBlock* bb = bf->m_blocks[i]; printf("tag='%s'\tptr=%p\ttype=%s\t[%4d]", bb->tag, bb,bf->types[bb->type_index].name,bb->m_array_entries_.size()); block_ID_finder(bb, bf, &ifd); printf("\n"); } #endif } void bFile::writeChunks(FILE* fp, bool fixupPointers) { bParse::bDNA* fileDna = mFileDNA ? mFileDNA : mMemoryDNA; for (int i=0;igetStruct(dataChunk.dna_nr); oldType = fileDna->getType(oldStruct[0]); oldLen = fileDna->getLength(oldStruct[0]); ///don't try to convert Link block data, just memcpy it. Other data can be converted. reverseOld = mMemoryDNA->getReverseType(oldType); if ((reverseOld!=-1)) { // make sure it's here //assert(reverseOld!= -1 && "getReverseType() returned -1, struct required!"); // curStruct = mMemoryDNA->getStruct(reverseOld); newType = mMemoryDNA->getType(curStruct[0]); // make sure it's the same assert((strcmp(oldType, newType)==0) && "internal error, struct mismatch!"); curLen = mMemoryDNA->getLength(curStruct[0]); dataChunk.dna_nr = reverseOld; if (strcmp("Link",oldType)!=0) { dataChunk.len = curLen * dataChunk.nr; } else { // printf("keep length of link = %d\n",dataChunk.len); } //write the structure header fwrite(&dataChunk,sizeof(bChunkInd),1,fp); short int* curStruct1; curStruct1 = mMemoryDNA->getStruct(dataChunk.dna_nr); assert(curStruct1 == curStruct); char* cur = fixupPointers ? (char*)findLibPointer(dataChunk.oldPtr) : (char*)dataChunk.oldPtr; //write the actual contents of the structure(s) fwrite(cur,dataChunk.len,1,fp); } else { printf("serious error, struct mismatch: don't write\n"); } } } // ----------------------------------------------------- // int bFile::getNextBlock(bChunkInd *dataChunk, const char *dataPtr, const int flags) { bool swap = false; bool varies = false; if (flags &FD_ENDIAN_SWAP) swap = true; if (flags &FD_BITS_VARIES) varies = true; if (VOID_IS_8) { if (varies) { bChunkPtr4 head; memcpy(&head, dataPtr, sizeof(bChunkPtr4)); bChunkPtr8 chunk; chunk.code = head.code; chunk.len = head.len; chunk.m_uniqueInts[0] = head.m_uniqueInt; chunk.m_uniqueInts[1] = 0; chunk.dna_nr = head.dna_nr; chunk.nr = head.nr; if (swap) { if ((chunk.code & 0xFFFF)==0) chunk.code >>=16; B3_SWITCH_INT(chunk.len); B3_SWITCH_INT(chunk.dna_nr); B3_SWITCH_INT(chunk.nr); } memcpy(dataChunk, &chunk, sizeof(bChunkInd)); } else { bChunkPtr8 c; memcpy(&c, dataPtr, sizeof(bChunkPtr8)); if (swap) { if ((c.code & 0xFFFF)==0) c.code >>=16; B3_SWITCH_INT(c.len); B3_SWITCH_INT(c.dna_nr); B3_SWITCH_INT(c.nr); } memcpy(dataChunk, &c, sizeof(bChunkInd)); } } else { if (varies) { bChunkPtr8 head; memcpy(&head, dataPtr, sizeof(bChunkPtr8)); bChunkPtr4 chunk; chunk.code = head.code; chunk.len = head.len; if (head.m_uniqueInts[0]==head.m_uniqueInts[1]) { chunk.m_uniqueInt = head.m_uniqueInts[0]; } else { b3Long64 oldPtr =0; memcpy(&oldPtr, &head.m_uniqueInts[0], 8); if (swap) B3_SWITCH_LONGINT(oldPtr); chunk.m_uniqueInt = (int)(oldPtr >> 3); } chunk.dna_nr = head.dna_nr; chunk.nr = head.nr; if (swap) { if ((chunk.code & 0xFFFF)==0) chunk.code >>=16; B3_SWITCH_INT(chunk.len); B3_SWITCH_INT(chunk.dna_nr); B3_SWITCH_INT(chunk.nr); } memcpy(dataChunk, &chunk, sizeof(bChunkInd)); } else { bChunkPtr4 c; memcpy(&c, dataPtr, sizeof(bChunkPtr4)); if (swap) { if ((c.code & 0xFFFF)==0) c.code >>=16; B3_SWITCH_INT(c.len); B3_SWITCH_INT(c.dna_nr); B3_SWITCH_INT(c.nr); } memcpy(dataChunk, &c, sizeof(bChunkInd)); } } if (dataChunk->len < 0) return -1; #if 0 print ("----------"); print (dataChunk->code); print (dataChunk->len); print (dataChunk->old); print (dataChunk->dna_nr); print (dataChunk->nr); #endif return (dataChunk->len+ChunkUtils::getOffset(flags)); } //eof