// ---- // Init (stand-alone version) // // Open the input and output files // Allocate and read the entire input file into memory // Validate the input file structure // Allocate the input track structures and initialize them // Initialize the output track structures // // Return TRUE on success // Prints its own error message if something goes wrong // // ---- static BOOL Init(LPSTR szInFile, LPSTR szOutFile) { BOOL fRet = FALSE; LONG cbRead; UINT32 *pChunkID; UINT32 *pChunkSize; LONG iChunkSize; LPMIDIFILEHDR pHeader; LPINTRACKSTATE pInTrack; UINT iTrack; // Initialize things we'll try to free later if we fail // ifs.FileSize = 0; ifs.pFile = NULL; //ifs.pTracks = NULL; // Attempt to open the input and output files // hInFile = CreateFileA(szInFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hInFile) { I_OutputMsg("Could not open \"%s\" for read.\n", szInFile); goto Init_Cleanup; } hOutFile = CreateFileA(szOutFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hOutFile) { I_OutputMsg("Could not open \"%s\" for write.\n", szOutFile); goto Init_Cleanup; } // Figure out how big the input file is and allocate a chunk of memory big enough // to hold the whole thing. Read the whole file in at once. // if (INVALID_FILE_SIZE == (ifs.FileSize = GetFileSize(hInFile, NULL))) { I_OutputMsg("File system error on input file.\n"); goto Init_Cleanup; } if (NULL == (ifs.pFile = GlobalAllocPtr(GPTR, ifs.FileSize))) { I_OutputMsg("Out of memory.\n"); goto Init_Cleanup; } if ((!ReadFile(hInFile, ifs.pFile, ifs.FileSize, &cbRead, NULL)) || cbRead != ifs.FileSize) { I_OutputMsg("Read error on input file.\n"); goto Init_Cleanup; } // Set up to read from the memory buffer. Read and validate // - MThd header // - size of file header chunk // - file header itself // ifs.iBytesLeft = ifs.FileSize; ifs.pFilePointer = ifs.pFile; // note: midi header size should always be 6 if ((pChunkID = (UINT32*)GetInFileData(sizeof (*pChunkID))) == NULL || *pChunkID != MThd || (pChunkSize = (UINT32*)GetInFileData(sizeof (*pChunkSize))) == NULL || (iChunkSize = LONGSWAP(*pChunkSize)) < sizeof (MIDIFILEHDR) || (pHeader = (LPMIDIFILEHDR)GetInFileData(iChunkSize)) == NULL) { I_OutputMsg("Read error on MIDI header.\n"); goto Init_Cleanup; } // File header is stored in hi-lo order. Swap this into Intel order and save // parameters in our native int size (32 bits) // ifs.dwFormat = (LONG)WORDSWAP(pHeader->wFormat); ifs.nTracks = (LONG)WORDSWAP(pHeader->nTracks); ifs.dwTimeDivision = (LONG)WORDSWAP(pHeader->wTimeDivision); #ifdef DEBUGMIDISTREAM I_OutputMsg("MIDI Header:\n" "------------\n" "format: %d\n" "number of tracks: %d\n" "time division: %d\n", ifs.dwFormat, ifs.nTracks, ifs.dwTimeDivision); #endif // We know how many tracks there are; allocate the structures for them and parse // them. The parse merely looks at the MTrk signature and track chunk length // in order to skip to the next track header. // faB: now static // ifs.pTracks = (INTRACKSTATE *)GlobalAllocPtr(GPTR, ifs.nTracks*sizeof (INTRACKSTATE)); // if (ifs.pTracks == NULL) // { // I_OutputMsg("Out of memory.\n"); // goto Init_Cleanup; // } // faB: made it static, but don't quit if there are more tracks, just skip'em // (this isn't really a limit, since 32 tracks are really the maximum for MIDI files) if (ifs.nTracks > MAX_MIDI_IN_TRACKS) ifs.nTracks = MAX_MIDI_IN_TRACKS; for (iTrack = 0, pInTrack = ifs.pTracks; iTrack < ifs.nTracks; ++iTrack, ++pInTrack) { if ((pChunkID = (UINT32*)GetInFileData(sizeof (*pChunkID))) == NULL || *pChunkID!= MTrk || (pChunkSize = (UINT32*)GetInFileData(sizeof (*pChunkSize))) == NULL) { I_OutputMsg("Read error on track header.\n"); goto Init_Cleanup; } iChunkSize = LONGSWAP(*pChunkSize); pInTrack->iTrackLen = iChunkSize; pInTrack->iBytesLeft = iChunkSize; pInTrack->pTrackData = GetInFileData(iChunkSize); if (pInTrack->pTrackData == NULL) { I_OutputMsg("Read error while reading track data.\n"); goto Init_Cleanup; } #ifdef DEBUGMIDISTREAM I_OutputMsg("Track %d : length %d bytes\n", iTrack, iChunkSize); pInTrack->nTrack = iTrack; #endif pInTrack->pTrackPointer = pInTrack->pTrackData; pInTrack->fdwTrack = 0; pInTrack->bRunningStatus = 0; // Handle bozo MIDI files which contain empty track chunks // if (!pInTrack->iBytesLeft) { pInTrack->fdwTrack |= ITS_F_ENDOFTRK; continue; } // We always preread the time from each track so the mixer code can // determine which track has the next event with a minimum of work // if (!GetTrackVDWord(pInTrack, &pInTrack->tkNextEventDue)) { I_OutputMsg("Read error while reading first delta time of track.\n"); goto Init_Cleanup; } } ots.tkTrack = 0; ots.pFirst = NULL; ots.pLast = NULL; fRet = TRUE; Init_Cleanup: if (!fRet) Cleanup(); return fRet; }
int fadcFirmwareReadFile(char *filename) { unsigned int arraySize; /* #define DEBUGFILE */ #ifdef DEBUGFILE int ichar=0; #endif FILE *arrayFile=NULL; arrayFile=fopen(filename,"r"); if(arrayFile==NULL) { printf("%s: ERROR opening file (%s) for reading\n", __FUNCTION__,filename); return ERROR; } /* First 32bits is the size of the array */ fread(&arraySize,sizeof(unsigned int),1,arrayFile); #ifdef VXWORKS /* Made this file in Linux... so byte swap it for VXWORKS */ MSC_arraySize = LONGSWAP(arraySize); #else MSC_arraySize = arraySize; #endif if(MSC_arraySize>MSC_MAX_SIZE) { printf("%s: ERROR: Firmware size (%d) from %s greater than MAX allowed (%d)\n", __FUNCTION__,MSC_arraySize,filename,MSC_MAX_SIZE); return ERROR; } #ifdef DEBUGFILE printf("MSC_arraySize = %d\n",MSC_arraySize); #endif fread(&MSC_ARRAY,MSC_arraySize,1,arrayFile); fclose(arrayFile); #ifdef DEBUGFILE for(ichar=0; ichar<16*10; ichar++) { if((ichar%16) == 0) printf("\n"); printf("0x%02x ",MSC_ARRAY[ichar]); } printf("\n\n"); #endif MSC_loaded = 1; printf("%s: Reading Firmware from %s\n", __FUNCTION__,filename); return OK; }
// ----------------------- // Mid2StreamConverterInit // // Validate the input file structure // Allocate the input track structures and initialize them (faB: now STATIC) // Initialize the output track structures // // Return TRUE on success // ----------------------- BOOL Mid2StreamConverterInit(LPBYTE pMidiData, size_t iMidiSize) { BOOL fRet = TRUE; UINT32 *pChunkID; UINT32 *pChunkSize; UINT32 iChunkSize; LPMIDIFILEHDR pHeader; LPINTRACKSTATE pInTrack; UINT iTrack; tkCurrentTime = 0; // Initialize things we'll try to free later if we fail ZeroMemory(&ifs, sizeof (INFILESTATE)); //ifs.pTracks = NULL; //faB: now static // Set up to read from the memory buffer. Read and validate // - MThd header // - size of file header chunk // - file header itself // ifs.FileSize = iMidiSize; ifs.pFile = pMidiData; ifs.iBytesLeft = ifs.FileSize; ifs.pFilePointer = ifs.pFile; #ifdef DEBUGMIDISTREAM I_OutputMsg("Midi file size: %d\n", iMidiSize); #endif // note: midi header size should always be 6 if ((pChunkID = (UINT32*)GetInFileData(sizeof (*pChunkID))) == NULL || *pChunkID != MThd || (pChunkSize = (UINT32*)GetInFileData(sizeof (*pChunkSize))) == NULL || (iChunkSize = LONGSWAP(*pChunkSize)) < sizeof (MIDIFILEHDR) || (pHeader = (LPMIDIFILEHDR)GetInFileData(iChunkSize)) == NULL) { I_OutputMsg("Read error on MIDI header.\n"); goto Init_Cleanup; } ifs.dwFormat = (LONG)WORDSWAP(pHeader->wFormat); ifs.nTracks = (LONG)WORDSWAP(pHeader->nTracks); ifs.dwTimeDivision = (LONG)WORDSWAP(pHeader->wTimeDivision); #ifdef DEBUGMIDISTREAM I_OutputMsg("MIDI Header:\n" "------------\n" "format: %d\n" "number of tracks: %d\n" "time division: %d\n", ifs.dwFormat, ifs.nTracks, ifs.dwTimeDivision); #endif /* faB: made static ifs.pTracks = (INTRACKSTATE *)GlobalAllocPtr(GPTR, ifs.nTracks*sizeof (INTRACKSTATE)); if (ifs.pTracks == NULL) { I_OutputMsg("Out of memory.\n"); goto Init_Cleanup; } */ // faB: made it static, but don't quit if there are more tracks, just skip'em if (ifs.nTracks > MAX_MIDI_IN_TRACKS) ifs.nTracks = MAX_MIDI_IN_TRACKS; for (iTrack = 0, pInTrack = ifs.pTracks; iTrack < ifs.nTracks; ++iTrack, ++pInTrack) { if ((pChunkID = (UINT32*)GetInFileData(sizeof (*pChunkID))) == NULL || *pChunkID!= MTrk || (pChunkSize = (UINT32*)GetInFileData(sizeof (*pChunkSize))) == NULL) { I_OutputMsg("Read error on track header.\n"); goto Init_Cleanup; } iChunkSize = LONGSWAP(*pChunkSize); pInTrack->iTrackLen = iChunkSize; // Total track length pInTrack->iBytesLeft = iChunkSize; pInTrack->pTrackData = GetInFileData(iChunkSize); if (pInTrack->pTrackData == NULL) { I_OutputMsg("Read error while reading track data.\n"); goto Init_Cleanup; } #ifdef DEBUGMIDISTREAM I_OutputMsg("Track %d : length %d bytes\n", iTrack, iChunkSize); pInTrack->nTrack = iTrack; #endif // Setup pointer to the current position in the track pInTrack->pTrackPointer = pInTrack->pTrackData; pInTrack->fdwTrack = 0; pInTrack->bRunningStatus = BAD_MIDI_FIX; pInTrack->tkNextEventDue = 0; // Handle bozo MIDI files which contain empty track chunks if (!pInTrack->iBytesLeft) { pInTrack->fdwTrack |= ITS_F_ENDOFTRK; continue; } // We always preread the time from each track so the mixer code can // determine which track has the next event with a minimum of work if (!GetTrackVDWord(pInTrack, &pInTrack->tkNextEventDue)) { I_OutputMsg("Read error while reading first delta time of track.\n"); goto Init_Cleanup; } } // End of track initialization code fRet = FALSE; Init_Cleanup: if (fRet) Mid2StreamConverterCleanup(); return fRet; }