//switch the endianess of a 32bit number uint32_t endianSwap32(uint32_t num) { uint16_t * word = (uint16_t *)(&num); uint32_t tmp = endianSwap16(*word) << 16; *(uint16_t *)(&tmp) = endianSwap16(*(word+1)); return tmp; }
void AtomMVHD::SerializeToBuffer(IOBuffer& data, uint32_t maxFrames) { uint32_t start=GETAVAILABLEBYTESCOUNT(data); VersionedAtom::SerializeToBuffer(data, maxFrames); data.ReadFromDataType<uint32_t>(endianSwap32(_creationTime)); data.ReadFromDataType<uint32_t>(endianSwap32(_modificationTime)); data.ReadFromDataType<uint32_t>(endianSwap32(_timeScale)); _offset=GETAVAILABLEBYTESCOUNT(data); data.ReadFromDataType<uint32_t>(endianSwap32(_duration)); data.ReadFromDataType<uint32_t>(endianSwap32(_preferredRate)); data.ReadFromDataType<uint16_t>(endianSwap16(_preferredVolume)); data.ReadFromBuffer(_reserved, sizeof(_reserved)); for (uint32_t i=0; i<9; i++) data.ReadFromDataType<uint32_t>(endianSwap32(_matrixStructure[i])); for (uint32_t i=0; i<6; i++) { data.ReadFromRepeat(0x00, 4); } data.ReadFromDataType<uint32_t>(endianSwap32(_nextTrakId)); _size=GETAVAILABLEBYTESCOUNT(data)-start; *(uint32_t*)(GETIBPOINTER(data)+start) = endianSwap32(_size); }
IFFP MyProcessGroup(GroupContext *parent) { /*compilerBug register*/ IFFP iffp; GroupContext rigidbodyContext; BitMapHeader bmHeader; bool foundBMHD = false; if (parent->subtype != ID_ILBM) return(IFF_OKAY); /* just continue scaning the file */ iffp = OpenRGroup(parent, &rigidbodyContext); CheckIFFP(); do { iffp = GetFChunkHdr(&rigidbodyContext); if (iffp == ID_BMHD) { printf("found ID_BMHD\n"); foundBMHD = true; iffp = IFFReadBytes(&rigidbodyContext, (BYTE *)&bmHeader, (long)sizeof(BitMapHeader)); //do endian swap bmHeader.w = endianSwap16(bmHeader.w); bmHeader.h = endianSwap16(bmHeader.h); bmHeader.pageWidth = endianSwap16(bmHeader.pageWidth); bmHeader.pageHeight = endianSwap16(bmHeader.pageHeight); } else if (iffp == ID_CMAP) { printf("found ID_CMAP\n"); // ilbmFrame.nColorRegs = maxColorReg; /* we have room for this many */ // iffp = GetCMAP( // &rigidbodyContext, (WORD *)&ilbmFrame.colorMap, &ilbmFrame.nColorRegs); } else if (iffp == ID_BODY) { printf("found ID_BODY\n"); if (!foundBMHD) return BAD_FORM; // if (!ilbmFrame.foundBMHD) return(BAD_FORM); /* No BMHD chunk! */ int moreBytes = ChunkMoreBytes(&rigidbodyContext); while (moreBytes>0) { int curRead = moreBytes > bufSz? bufSz : moreBytes; //read iffp = IFFReadBytes(&rigidbodyContext, bodyBuffer, curRead); moreBytes -= curRead; } printf("remaining=%d\n",moreBytes); if (iffp == IFF_OKAY) iffp = IFF_DONE; /* Eureka */ // nPlanes = MIN(ilbmFrame.bmHdr.nPlanes, EXDepth); } else if (iffp == END_MARK) iffp = BAD_FORM; } while (iffp >= IFF_OKAY); /* loop if valid ID of ignored chunk or a * subroutine returned IFF_OKAY (no errors).*/ if (iffp != IFF_DONE) return(iffp); /* If we get this far, there were no errors. */ CloseRGroup(&rigidbodyContext); return(iffp); }
int main(int argc, char ** argv) { if(argc != 2) { printf("Usage is midiParse <file>\n"); exit(1); } //now lets open it up FILE * mFile; mFile = fopen(argv[1], "r"); if(!mFile) { //printf("Opening file \"%s\" failed. Aborting.\n", argv[1]); exit(1); } //get the file size // obtain file size: fseek (mFile , 0 , SEEK_END); uint32_t lSize = ftell(mFile); rewind (mFile); printf("Size = %d\n", lSize); //read the first header fread(&hChunk, 14, 1, mFile); /*printf("Header is [ "); printf("%s %08X %04X %04X %04X]\n", &hChunk.type, hChunk.length, hChunk.format, hChunk.numtrk, hChunk.div );*/ //hChunk.type[4] = 0; //printf("Type is %s\n", &hChunk.type); //printf("Length is %d\n", endianSwap32(hChunk.length)); //printf("Format is %d\n", endianSwap16(hChunk.format)); //printf("Format is %d\n", endianSwap16(hChunk.numtrk)); //printf("Format is %d\n", endianSwap16(hChunk.div)); //TODO: allow for reading of any len > 5 headers if((endianSwap32(hChunk.length) != 6) || (endianSwap16(hChunk.format) == 2)) { printf("Length of header corrupt or format unknown! Aborting.\n"); exit(1); } fseek(mFile, 8 + endianSwap32(hChunk.length), SEEK_SET); //loop through the rest of the file while(ftell(mFile) < lSize) { char type[5]; type[4] = 0; struct t_chunk * chunk = (struct t_chunk *)malloc(sizeof(struct t_chunk)); printf("Reading chunk...\n"); fread(&type, 1, 4, mFile); fread(&((*chunk).size), 4, 1, mFile); (*chunk).size = endianSwap32((*chunk).size); (*chunk).data = (uint8_t *)malloc((*chunk).size); fread((*chunk).data, 1, (*chunk).size, mFile); printf("Type %s, Length = %d bytes\n", type, (*chunk).size); //link them up if(TCs) { //find the last one in the list struct t_chunk * tmpTC = TCs; while((*tmpTC).next) { tmpTC = (*tmpTC).next; numTCs++; } //add our latest node (*tmpTC).next = chunk; numTCs++; } else { TCs = chunk; numTCs++; } //break; printf("at pos %d\n", ftell(mFile)); } printf("Parsed %d Track Chunks.\n", numTCs); //parse events struct t_chunk * node = TCs; while(node) { parseEvents((*node).data, (*node).size); node = (*node).next; } return 0; }