LFA_FileRef LFA_Open ( const char * filePath, char mode ) { if ( (mode != 'r') && (mode != 'w') ) LFA_Throw ( "LFA_Open: invalid mode", kLFAErr_ExternalFailure );; FSRef fileRef; SInt8 perm = ( (mode == 'r') ? fsRdPerm : fsRdWrPerm ); HFSUniStr255 dataForkName; #if __LP64__ FSIORefNum refNum; #else SInt16 refNum; #endif OSErr err = FSGetDataForkName ( &dataForkName ); if ( err != noErr ) LFA_Throw ( "LFA_Open: FSGetDataForkName failure", kLFAErr_ExternalFailure ); err = FSPathMakeRef ( (XMP_Uns8*)filePath, &fileRef, 0 ); if ( err != noErr ) LFA_Throw ( "LFA_Open: FSPathMakeRef failure", kLFAErr_ExternalFailure ); err = FSOpenFork ( &fileRef, dataForkName.length, dataForkName.unicode, perm, &refNum ); if ( err != noErr ) LFA_Throw ( "LFA_Open: FSOpenFork failure", kLFAErr_ExternalFailure ); return (LFA_FileRef)refNum; } // LFA_Open
static OSErr FSOpenDFCompat(FSRef *ref, char permission, short *refNum) { HFSUniStr255 forkName; OSErr theErr; Boolean isFolder, wasChanged; theErr = FSResolveAliasFile(ref, TRUE, &isFolder, &wasChanged); if (theErr != noErr) { return theErr; } FSGetDataForkName(&forkName); #ifdef PLATFORM_64BIT theErr = FSOpenFork(ref, forkName.length, forkName.unicode, permission, (FSIORefNum *)refNum); #else theErr = FSOpenFork(ref, forkName.length, forkName.unicode, permission, refNum); #endif return theErr; }
bool XMLMacCarbonFile::openWithPermission(const XMLCh* const fileName, int macPermission) { OSErr err = noErr; if (mFileValid) ThrowXML1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, fileName); if (gHasHFSPlusAPIs) { FSRef ref; if (!XMLParsePathToFSRef(fileName, ref)) err = fnfErr; HFSUniStr255 forkName; if (err == noErr) err = FSGetDataForkName(&forkName); if (err == noErr) err = FSOpenFork(&ref, forkName.length, forkName.unicode, macPermission, &mFileRefNum); } else { FSSpec spec; if (!XMLParsePathToFSSpec(fileName, spec)) err = fnfErr; if (err == noErr) err = FSpOpenDF(&spec, macPermission, &mFileRefNum); } if (err != noErr) ThrowXML1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, fileName); mFileValid = true; return mFileValid; }
prMALError SDKOpenFile8( imStdParms *stdParms, imFileRef *SDKfileRef, imFileOpenRec8 *SDKfileOpenRec8) { prMALError result = malNoError; ImporterLocalRec8H localRecH = NULL; ImporterLocalRec8Ptr localRecP = NULL; if(SDKfileOpenRec8->privatedata) { localRecH = (ImporterLocalRec8H)SDKfileOpenRec8->privatedata; stdParms->piSuites->memFuncs->lockHandle(reinterpret_cast<char**>(localRecH)); localRecP = reinterpret_cast<ImporterLocalRec8Ptr>( *localRecH ); } else { localRecH = (ImporterLocalRec8H)stdParms->piSuites->memFuncs->newHandle(sizeof(ImporterLocalRec8)); SDKfileOpenRec8->privatedata = (PrivateDataPtr)localRecH; stdParms->piSuites->memFuncs->lockHandle(reinterpret_cast<char**>(localRecH)); localRecP = reinterpret_cast<ImporterLocalRec8Ptr>( *localRecH ); localRecP->vf = NULL; localRecP->opus = NULL; localRecP->flac = NULL; localRecP->importerID = SDKfileOpenRec8->inImporterID; localRecP->fileType = SDKfileOpenRec8->fileinfo.filetype; } SDKfileOpenRec8->fileinfo.fileref = *SDKfileRef = reinterpret_cast<imFileRef>(imInvalidHandleValue); if(localRecP) { const prUTF16Char *path = SDKfileOpenRec8->fileinfo.filepath; #ifdef PRWIN_ENV HANDLE fileH = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(fileH != imInvalidHandleValue) { SDKfileOpenRec8->fileinfo.fileref = *SDKfileRef = fileH; } else result = imFileOpenFailed; #else FSIORefNum refNum = CAST_REFNUM(imInvalidHandleValue); CFStringRef filePathCFSR = CFStringCreateWithCharacters(NULL, path, prUTF16CharLength(path)); CFURLRef filePathURL = CFURLCreateWithFileSystemPath(NULL, filePathCFSR, kCFURLPOSIXPathStyle, false); if(filePathURL != NULL) { FSRef fileRef; Boolean success = CFURLGetFSRef(filePathURL, &fileRef); if(success) { HFSUniStr255 dataForkName; FSGetDataForkName(&dataForkName); OSErr err = FSOpenFork( &fileRef, dataForkName.length, dataForkName.unicode, fsRdWrPerm, &refNum); } CFRelease(filePathURL); } CFRelease(filePathCFSR); if(CAST_FILEREF(refNum) != imInvalidHandleValue) { SDKfileOpenRec8->fileinfo.fileref = *SDKfileRef = CAST_FILEREF(refNum); } else result = imFileOpenFailed; #endif } if(result == malNoError) { localRecP->fileType = SDKfileOpenRec8->fileinfo.filetype; assert(0 == ogg_tell_func(static_cast<void *>(*SDKfileRef))); if(localRecP->fileType == Ogg_filetype) { localRecP->vf = new OggVorbis_File; OggVorbis_File &vf = *localRecP->vf; int ogg_err = ov_open_callbacks(static_cast<void *>(*SDKfileRef), &vf, NULL, 0, g_ov_callbacks); if(ogg_err == OV_OK) { if( ov_streams(&vf) == 0 ) { result = imFileHasNoImportableStreams; ov_clear(&vf); } else if( !ov_seekable(&vf) ) { result = imBadFile; } } else result = imBadHeader; } else if(localRecP->fileType == Opus_filetype) { int _error = 0; localRecP->opus = op_open_callbacks(static_cast<void *>(*SDKfileRef), &g_opusfile_callbacks, NULL, 0, &_error); if(localRecP->opus != NULL && _error == 0) { assert(op_link_count(localRecP->opus) == 1); // we're not really handling multi-link scenarios } else result = imBadHeader; } else if(localRecP->fileType == FLAC_filetype) { try { localRecP->flac = new OurDecoder(*SDKfileRef); localRecP->flac->set_md5_checking(true); FLAC__StreamDecoderInitStatus init_status = localRecP->flac->init(); assert(init_status == FLAC__STREAM_DECODER_INIT_STATUS_OK && localRecP->flac->is_valid()); bool ok = localRecP->flac->process_until_end_of_metadata(); assert(ok); } catch(...) { result = imBadHeader; } } } // close file and delete private data if we got a bad file if(result != malNoError) { if(SDKfileOpenRec8->privatedata) { stdParms->piSuites->memFuncs->disposeHandle(reinterpret_cast<PrMemoryHandle>(SDKfileOpenRec8->privatedata)); SDKfileOpenRec8->privatedata = NULL; } } else { stdParms->piSuites->memFuncs->unlockHandle(reinterpret_cast<char**>(SDKfileOpenRec8->privatedata)); } return result; }
prMALError SDKOpenFile8( imStdParms *stdParms, imFileRef *SDKfileRef, imFileOpenRec8 *SDKfileOpenRec8) { prMALError result = malNoError; ImporterLocalRec8H localRecH = NULL; ImporterLocalRec8Ptr localRecP = NULL; if(SDKfileOpenRec8->privatedata) { localRecH = (ImporterLocalRec8H)SDKfileOpenRec8->privatedata; stdParms->piSuites->memFuncs->lockHandle(reinterpret_cast<char**>(localRecH)); localRecP = reinterpret_cast<ImporterLocalRec8Ptr>( *localRecH ); } else { localRecH = (ImporterLocalRec8H)stdParms->piSuites->memFuncs->newHandle(sizeof(ImporterLocalRec8)); SDKfileOpenRec8->privatedata = (PrivateDataPtr)localRecH; stdParms->piSuites->memFuncs->lockHandle(reinterpret_cast<char**>(localRecH)); localRecP = reinterpret_cast<ImporterLocalRec8Ptr>( *localRecH ); localRecP->reader = NULL; localRecP->file = NULL; localRecP->audio_track = NULL; localRecP->alac = NULL; localRecP->importerID = SDKfileOpenRec8->inImporterID; localRecP->fileType = SDKfileOpenRec8->fileinfo.filetype; } SDKfileOpenRec8->fileinfo.fileref = *SDKfileRef = reinterpret_cast<imFileRef>(imInvalidHandleValue); if(localRecP) { const prUTF16Char *path = SDKfileOpenRec8->fileinfo.filepath; #ifdef PRWIN_ENV HANDLE fileH = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(fileH != imInvalidHandleValue) { SDKfileOpenRec8->fileinfo.fileref = *SDKfileRef = fileH; } else result = imFileOpenFailed; #else FSIORefNum refNum = CAST_REFNUM(imInvalidHandleValue); CFStringRef filePathCFSR = CFStringCreateWithCharacters(NULL, path, prUTF16CharLength(path)); CFURLRef filePathURL = CFURLCreateWithFileSystemPath(NULL, filePathCFSR, kCFURLPOSIXPathStyle, false); if(filePathURL != NULL) { FSRef fileRef; Boolean success = CFURLGetFSRef(filePathURL, &fileRef); if(success) { HFSUniStr255 dataForkName; FSGetDataForkName(&dataForkName); OSErr err = FSOpenFork( &fileRef, dataForkName.length, dataForkName.unicode, fsRdPerm, &refNum); } CFRelease(filePathURL); } CFRelease(filePathCFSR); if(CAST_FILEREF(refNum) != imInvalidHandleValue) { SDKfileOpenRec8->fileinfo.fileref = *SDKfileRef = CAST_FILEREF(refNum); } else result = imFileOpenFailed; #endif } if(result == malNoError) { localRecP->fileType = SDKfileOpenRec8->fileinfo.filetype; try { localRecP->reader = new My_ByteStream(*SDKfileRef); localRecP->file = new AP4_File(*localRecP->reader); AP4_Track *audio_track = localRecP->file->GetMovie()->GetTrack(AP4_Track::TYPE_AUDIO); if(audio_track != NULL) { assert(audio_track->GetSampleDescriptionCount() == 1); AP4_SampleDescription *desc = audio_track->GetSampleDescription(0); AP4_AudioSampleDescription *audio_desc = AP4_DYNAMIC_CAST(AP4_AudioSampleDescription, desc); if(desc != NULL && desc->GetFormat() == AP4_SAMPLE_FORMAT_ALAC) { localRecP->audio_track = audio_track; ALAC_Atom *alac_atom = AP4_DYNAMIC_CAST(ALAC_Atom, desc->GetDetails().GetChild(AP4_SAMPLE_FORMAT_ALAC)); if(alac_atom != NULL) { size_t magic_cookie_size = 0; void *magic_cookie = alac_atom->GetMagicCookie(magic_cookie_size); if(magic_cookie != NULL && magic_cookie_size > 0) { localRecP->alac = new ALACDecoder(); int32_t alac_result = localRecP->alac->Init(magic_cookie, magic_cookie_size); if(alac_result != 0) { result = imBadHeader; } } else result = imBadHeader; } else result = imBadHeader; } else result = imUnsupportedCompression; } else result = imFileHasNoImportableStreams; } catch(...) { result = imBadFile; } } // close file and delete private data if we got a bad file if(result != malNoError) { if(SDKfileOpenRec8->privatedata) { stdParms->piSuites->memFuncs->disposeHandle(reinterpret_cast<PrMemoryHandle>(SDKfileOpenRec8->privatedata)); SDKfileOpenRec8->privatedata = NULL; } } else { stdParms->piSuites->memFuncs->unlockHandle(reinterpret_cast<char**>(SDKfileOpenRec8->privatedata)); } return result; }
static int AudioFilePlayer_OpenFile (AudioFilePlayer *afp, const FSRef *inRef, SInt64 *outFileDataSize) { ContainerChunk chunkHeader; ChunkHeader chunk; SSNDData ssndData; OSErr result; HFSUniStr255 dfName; ByteCount actual; SInt64 offset; /* Open the data fork of the input file */ result = FSGetDataForkName(&dfName); if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSGetDataForkName")*/ result = FSOpenFork(inRef, dfName.length, dfName.unicode, fsRdPerm, &afp->mForkRefNum); if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSOpenFork")*/ /* Read the file header, and check if it's indeed an AIFC file */ result = FSReadFork(afp->mForkRefNum, fsAtMark, 0, sizeof(chunkHeader), &chunkHeader, &actual); if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/ if (SDL_SwapBE32(chunkHeader.ckID) != 'FORM') { result = -1; if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): chunk id is not 'FORM'");*/ } if (SDL_SwapBE32(chunkHeader.formType) != 'AIFC') { result = -1; if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): file format is not 'AIFC'");*/ } /* Search for the SSND chunk. We ignore all compression etc. information in other chunks. Of course that is kind of evil, but for now we are lazy and rely on the cdfs to always give us the same fixed format. TODO: Parse the COMM chunk we currently skip to fill in mFileDescription. */ offset = 0; do { result = FSReadFork(afp->mForkRefNum, fsFromMark, offset, sizeof(chunk), &chunk, &actual); if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/ chunk.ckID = SDL_SwapBE32(chunk.ckID); chunk.ckSize = SDL_SwapBE32(chunk.ckSize); /* Skip the chunk data */ offset = chunk.ckSize; } while (chunk.ckID != 'SSND'); /* Read the header of the SSND chunk. After this, we are positioned right at the start of the audio data. */ result = FSReadFork(afp->mForkRefNum, fsAtMark, 0, sizeof(ssndData), &ssndData, &actual); if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/ ssndData.offset = SDL_SwapBE32(ssndData.offset); result = FSSetForkPosition(afp->mForkRefNum, fsFromMark, ssndData.offset); if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSSetForkPosition")*/ /* Data size */ *outFileDataSize = chunk.ckSize - ssndData.offset - 8; /* File format */ afp->mFileDescription.mSampleRate = 44100; afp->mFileDescription.mFormatID = kAudioFormatLinearPCM; afp->mFileDescription.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger; afp->mFileDescription.mBytesPerPacket = 4; afp->mFileDescription.mFramesPerPacket = 1; afp->mFileDescription.mBytesPerFrame = 4; afp->mFileDescription.mChannelsPerFrame = 2; afp->mFileDescription.mBitsPerChannel = 16; return 1; }
int ReadTOCData (FSVolumeRefNum theVolume, SDL_CD *theCD) { HFSUniStr255 dataForkName; OSStatus theErr; FSIORefNum forkRefNum; SInt64 forkSize; Ptr forkData = 0; ByteCount actualRead; CFDataRef dataRef = 0; CFPropertyListRef propertyListRef = 0; FSRefParam fsRefPB; FSRef tocPlistFSRef; FSRef rootRef; const char* error = "Unspecified Error"; const UniChar uniName[] = { '.','T','O','C','.','p','l','i','s','t' }; theErr = FSGetVolumeInfo(theVolume, 0, 0, kFSVolInfoNone, 0, 0, &rootRef); if(theErr != noErr) { error = "FSGetVolumeInfo"; goto bail; } SDL_memset(&fsRefPB, '\0', sizeof (fsRefPB)); fsRefPB.ref = &rootRef; fsRefPB.newRef = &tocPlistFSRef; fsRefPB.nameLength = sizeof (uniName) / sizeof (uniName[0]); fsRefPB.name = uniName; fsRefPB.textEncodingHint = kTextEncodingUnknown; theErr = PBMakeFSRefUnicodeSync (&fsRefPB); if(theErr != noErr) { error = "PBMakeFSRefUnicodeSync"; goto bail; } theErr = FSGetDataForkName (&dataForkName); if (theErr != noErr) { error = "FSGetDataForkName"; goto bail; } theErr = FSOpenFork (&tocPlistFSRef, dataForkName.length, dataForkName.unicode, fsRdPerm, &forkRefNum); if (theErr != noErr) { error = "FSOpenFork"; goto bail; } theErr = FSGetForkSize (forkRefNum, &forkSize); if (theErr != noErr) { error = "FSGetForkSize"; goto bail; } forkData = NewPtr (forkSize); if(forkData == NULL) { error = "NewPtr"; goto bail; } theErr = FSReadFork (forkRefNum, fsFromStart, 0 , forkSize, forkData, &actualRead); if(theErr != noErr) { error = "FSReadFork"; goto bail; } dataRef = CFDataCreate (kCFAllocatorDefault, (UInt8 *)forkData, forkSize); if(dataRef == 0) { error = "CFDataCreate"; goto bail; } propertyListRef = CFPropertyListCreateFromXMLData (kCFAllocatorDefault, dataRef, kCFPropertyListImmutable, NULL); if (propertyListRef == NULL) { error = "CFPropertyListCreateFromXMLData"; goto bail; } if(CFGetTypeID(propertyListRef)== CFDictionaryGetTypeID()) { CFDictionaryRef dictRef = (CFDictionaryRef)propertyListRef; CFDataRef theRawTOCDataRef; CFArrayRef theSessionArrayRef; CFIndex numSessions; CFIndex index; theRawTOCDataRef = (CFDataRef)CFDictionaryGetValue (dictRef, CFSTR(kRawTOCDataString)); theSessionArrayRef = (CFArrayRef)CFDictionaryGetValue (dictRef, CFSTR(kSessionsString)); numSessions = CFArrayGetCount (theSessionArrayRef); theCD->numtracks = 0; for(index = 0; index < numSessions; index++) { CFDictionaryRef theSessionDict; CFNumberRef leadoutBlock; CFArrayRef trackArray; CFIndex numTracks; CFIndex trackIndex; UInt32 value = 0; theSessionDict = (CFDictionaryRef) CFArrayGetValueAtIndex (theSessionArrayRef, index); leadoutBlock = (CFNumberRef) CFDictionaryGetValue (theSessionDict, CFSTR(kLeadoutBlockString)); trackArray = (CFArrayRef)CFDictionaryGetValue (theSessionDict, CFSTR(kTrackArrayString)); numTracks = CFArrayGetCount (trackArray); for(trackIndex = 0; trackIndex < numTracks; trackIndex++) { CFDictionaryRef theTrackDict; CFNumberRef trackNumber; CFNumberRef sessionNumber; CFNumberRef startBlock; CFBooleanRef isDataTrack; UInt32 value; theTrackDict = (CFDictionaryRef) CFArrayGetValueAtIndex (trackArray, trackIndex); trackNumber = (CFNumberRef) CFDictionaryGetValue (theTrackDict, CFSTR(kPointKeyString)); sessionNumber = (CFNumberRef) CFDictionaryGetValue (theTrackDict, CFSTR(kSessionNumberKeyString)); startBlock = (CFNumberRef) CFDictionaryGetValue (theTrackDict, CFSTR(kStartBlockKeyString)); isDataTrack = (CFBooleanRef) CFDictionaryGetValue (theTrackDict, CFSTR(kDataKeyString)); int idx = theCD->numtracks++; CFNumberGetValue (trackNumber, kCFNumberSInt32Type, &value); theCD->track[idx].id = value; CFNumberGetValue (startBlock, kCFNumberSInt32Type, &value); theCD->track[idx].offset = value; theCD->track[idx].type = (isDataTrack == kCFBooleanTrue) ? SDL_DATA_TRACK : SDL_AUDIO_TRACK; if (trackIndex > 0) { theCD->track[idx-1].length = theCD->track[idx].offset - theCD->track[idx-1].offset; } } CFNumberGetValue (leadoutBlock, kCFNumberSInt32Type, &value); theCD->track[theCD->numtracks-1].length = value - theCD->track[theCD->numtracks-1].offset; theCD->track[theCD->numtracks].offset = value; } } theErr = 0; goto cleanup; bail: SDL_SetError ("ReadTOCData: %s returned %d", error, theErr); theErr = -1; cleanup: if (propertyListRef != NULL) CFRelease(propertyListRef); if (dataRef != NULL) CFRelease(dataRef); if (forkData != NULL) DisposePtr(forkData); FSCloseFork (forkRefNum); return theErr; }