static tsize_t _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size) { return (FSRead((short) fd, (long*) &size, (char*) buf) == noErr ? size : (tsize_t) -1); }
//filepath = fullpath // 0 = not exist, 1=success, -1=failed static s8 _Open(hFILE fp, const FSPath & fsPath, const tCHAR *filepath, const tCHAR *mode) { s8 ret = FALSE; //get the write mode u8 bWriteMode = FALSE; //we are going to write on the file? tCHAR *modePtr = (tCHAR*)mode; while(*modePtr != 0) { if(*modePtr == 'w') { bWriteMode = TRUE; break; } modePtr++; } if(TESTFLAGS(fsPath.flag, FS_DIR_ARCHIVE)) { //are we trying to write? //say it doesn't exist. if(bWriteMode) return 0; char sPath[MAXCHARBUFF]; wcstombs(sPath, filepath, MAXCHARBUFF); //switch all '\' to '/' u32 sSize = (u32)strlen(sPath); for(u32 i = 0; i < sSize; i++) { if(sPath[i] == '\\') sPath[i] = '/'; } if(fsPath.uf) { //check to see if the given filepath is in the zip if(unzLocateFile(fsPath.uf, sPath, 2) == UNZ_OK) { unz_file_info file_info; unzGetCurrentFileInfo(fsPath.uf, &file_info, sPath, MAXCHARBUFF, 0,0,0,0); if(unzOpenCurrentFile(fsPath.uf) == UNZ_OK) { //allocate the buffer for this file fp->buff = (u8*)MemAlloc(sizeof(u8)*file_info.uncompressed_size); if(fp->buff) { //copy the data to the buffer unzReadCurrentFile(fsPath.uf, (voidp)fp->buff, file_info.uncompressed_size); fp->buffSize = file_info.uncompressed_size; //check for unicode (we only use UC_Base/UC_BigEndian for now...) if(fp->buff[0] == 0xff && fp->buff[1] == 0xfe) fp->uCType = UC_Base; else if(fp->buff[0] == 0xfe && fp->buff[1] == 0xff) fp->uCType = UC_BigEndian; ret = TRUE; } else { ret = -1; LogMsg(LOG_FILE, L"_Open: unable to allocate buffer", filepath); } unzCloseCurrentFile(fsPath.uf); } else { ret = -1; LogMsg(LOG_FILE, L"_Open: unable to open file in archive", filepath); } } } } else { //are we trying to write? //say it doesn't exist if this path is read-only if(bWriteMode && TESTFLAGS(fsPath.flag, FS_DIR_READONLY)) return 0; wstring path = fsPath.str; path += L"\\"; path += filepath; //try to open the file fp->fp = _wfopen(path.c_str(), mode); if(fp->fp) { //detect unicode... u16 magic; FSRead(&magic, sizeof(u16), 1, fp); if(magic == 0xfffe) fp->uCType = UC_BigEndian; else if(magic == 0xfeff) fp->uCType = UC_Base; if(fp->uCType != UC_None) _setmode( _fileno( fp->fp ), _O_BINARY ); else FSSeek(fp, 0, FS_SEEK_SET); ret = TRUE; } } return ret; }
OSErr IsFlattenedResourceFile( ConstFSSpecPtr inFile, Boolean* outIsFlat) { OSErr err; CInfoPBRec pb; if (not inFile) { // This can occur when we create a new project document (Cmd-N) *outIsFlat = false; return noErr; } pb.hFileInfo.ioNamePtr = (StringPtr)inFile->name; pb.hFileInfo.ioVRefNum = inFile->vRefNum; pb.hFileInfo.ioDirID = inFile->parID; pb.hFileInfo.ioFDirIndex = 0; err = PBGetCatInfoSync(&pb); if (err == noErr) { if (pb.hFileInfo.ioFlAttrib & kioFlAttribDirMask) { // This is a directory *outIsFlat = false; return paramErr; } else { UInt32 dfSize; UInt32 rfSize; SInt16 dfRefNum; SInt32 filePos; dfSize = pb.hFileInfo.ioFlLgLen; rfSize = pb.hFileInfo.ioFlRLgLen; if (rfSize > 0) { *outIsFlat = false; } else if (dfSize == 0) { // This file has no data or resource fork. *outIsFlat = false; } else { // Only the data fork is non-empty. // Now we need to determine if it contains resources or not. UInt32 firstFourWords[4]; SInt32 byteCount; err = FSpOpenDF(inFile, fsRdPerm, &dfRefNum); if (err) return err; err = GetFPos(dfRefNum, &filePos); byteCount = sizeof(firstFourWords); err = FSRead(dfRefNum, &byteCount, &firstFourWords); if (err == noErr) { // Test is based on resource file format as described in IM: More Mac Toolbox // <http://developer.apple.com/techpubs/mac/MoreToolbox/MoreToolbox-99.html#HEADING99-0> // // First four words of the file represent the resource header // Word1: Offset from beginning of resource fork to resource data // Word2: Offset from beginning of resource fork to resource map // Word3: Length of resource data // Word4: Length of resource map // // So... // (Word1 + Word3 + Word4) == (Word2 + Word4) == size of resource fork if ((byteCount == sizeof(firstFourWords)) and (EndianU32_BtoN(firstFourWords[0]) + EndianU32_BtoN(firstFourWords[2]) + EndianU32_BtoN(firstFourWords[3]) == dfSize) and (EndianU32_BtoN(firstFourWords[1]) + EndianU32_BtoN(firstFourWords[3]) == dfSize)) { *outIsFlat = true; } } err = SetFPos(dfRefNum, fsFromStart, filePos); err = FSClose(dfRefNum); } } } return err; }
OSErr ReadObjectInfo (short FRefNum, ObjectRecHdl *thisObjectHdlPtr) { OSErr ErrCode = 0; long byteCount, structSize; Handle thisObjectHdl = nil, thisObjectDataHdl = nil; // read the size of this object's info handle byteCount = sizeof (long); ErrCode = FSRead (FRefNum, &byteCount, (Ptr) &structSize); if (!ErrCode) { thisObjectHdl = _NewHandleClear (structSize); if (thisObjectHdl != nil) { _HLock (thisObjectHdl); ErrCode = FSRead (FRefNum, &structSize, (Ptr) *thisObjectHdl); _HUnlock (thisObjectHdl); } else ErrCode = memFullErr; } if (!ErrCode) // read the object's data handle if any { if (GetObjectDataHdl ((ObjectRecHdl) thisObjectHdl) != nil) { // read the size of this object's data handle byteCount = sizeof (long); ErrCode = FSRead (FRefNum, &byteCount, (Ptr) &structSize); if (!ErrCode) { thisObjectDataHdl = _NewHandleClear (structSize); if (thisObjectDataHdl != nil) { _HLock (thisObjectDataHdl); ErrCode = FSRead (FRefNum, &structSize, (Ptr) *thisObjectDataHdl); _HUnlock (thisObjectDataHdl); if (!ErrCode) SetObjectDataHdl ((ObjectRecHdl) thisObjectHdl, thisObjectDataHdl); } else ErrCode = memFullErr; } } } if (ErrCode) { if (thisObjectHdl != nil) DisposeHandle (thisObjectDataHdl); if (thisObjectHdl != nil) DisposeHandle (thisObjectDataHdl); } if (!ErrCode) *thisObjectHdlPtr = (ObjectRecHdl) thisObjectHdl; // send this handle back return (ErrCode); }
OSErr PAS_decodeMisc(PASEntry *entry, FSSpec *outFile, short inRefNum) { OSErr err = noErr; short outRefNum; PASMiscInfo info; SInt32 infoSize; FInfo theFInfo; infoSize = sizeof(PASMiscInfo); err = SetFPos(inRefNum, fsFromStart, (*entry).entryOffset ); if (err != noErr) return err; err = FSRead( inRefNum, &infoSize, &info); if (err != noErr) return err; if(infoSize != sizeof(PASMiscInfo)) { return -1; } err = FSpOpenDF(outFile, fsRdWrPerm, &outRefNum); if (err != noErr) return err; memset(&theFInfo, 0, sizeof(FInfo)); theFInfo.fdType = info.fileType; theFInfo.fdCreator = info.fileCreator; theFInfo.fdFlags = info.fileFlags; err = FSpSetFInfo(outFile, &theFInfo); if (err != noErr) return err; FSClose(outRefNum); if (info.fileHasResFork) { outRefNum = FSpOpenResFile(outFile, fsRdWrPerm); if (outRefNum < noErr) { // maybe it does not have one! FSpCreateResFile(outFile, info.fileCreator, info.fileType, smSystemScript); outRefNum = FSpOpenResFile(outFile, fsRdWrPerm); if (outRefNum < noErr) { return outRefNum; } } SetResFileAttrs(outRefNum, info.fileResAttrs); CloseResFile(outRefNum); } if(info.fileType == 'APPL') { // we need to add applications to the desktop database. /* FIX :: need to find DTSetAPPL() function err = DTSetAPPL( NULL, outFile->vRefNum, info.fileCreator, outFile->parID, outFile->name); */ } return err; }
OSErr PAS_decodeResource(PASEntry *entry, FSSpec *outFile, short inRefNum) { OSErr err = noErr; short outRefNum; PASResFork info; SInt32 infoSize; short oldResFile; PASResource pasRes; SInt32 pasResSize; long bufSize; Handle buffer; long counter=0; infoSize = sizeof(PASResFork); err = SetFPos(inRefNum, fsFromStart, (*entry).entryOffset ); if (err != noErr) return err; err = FSRead( inRefNum, &infoSize, &info); if (err != noErr) return err; if(infoSize != sizeof(PASResFork)) { err = -1; goto error; } oldResFile=CurResFile(); outRefNum = FSpOpenResFile(outFile, fsRdWrPerm); if (outRefNum < noErr) return outRefNum; UseResFile(outRefNum); while (1) { pasResSize = sizeof(PASResource); err = FSRead( inRefNum, &pasResSize, &pasRes); if (err != noErr) { if(err == eofErr) err = noErr; break; } bufSize = pasRes.length; buffer = NewHandle(bufSize); HLock(buffer); if(buffer == NULL) { // if we did not get our memory, try updateresfile HUnlock(buffer); UpdateResFile(outRefNum); counter=0; buffer = NewHandle(bufSize); HLock(buffer); if(buffer == NULL) { err = memFullErr; break; } } err = FSRead( inRefNum, &bufSize, &(**buffer)); if (err != noErr && err != eofErr) break; AddResource(buffer, pasRes.attrType, pasRes.attrID, pasRes.attrName); WriteResource(buffer); SetResAttrs(buffer, pasRes.attr); ChangedResource(buffer); WriteResource(buffer); ReleaseResource(buffer); if (counter++ > 100) { UpdateResFile(outRefNum); counter=0; } } error: UseResFile(oldResFile); CloseResFile(outRefNum); return err; }
OSErr PAS_DecodeFile(FSSpec *inSpec, FSSpec *outSpec) { OSErr err; short inRefNum; PASHeader header; PASEntry dataEntry, miscEntry, resourceEntry; long sizeOfEntry; if (inSpec == NULL || outSpec == NULL) return paramErr; FSpDelete( outSpec ) ; err = FSpCreate( outSpec, kCreator, kType ,smSystemScript ); if (err != noErr) return err; err = FSpOpenDF(inSpec, fsRdPerm, &inRefNum); if (err != noErr) goto error; // Read Header err = PAS_decodeHeader(inRefNum, &header); if (err != noErr) goto error; if( header.magicNum != PAS_MAGIC_NUM || header.versionNum != PAS_VERSION) { err = -1; goto error; } // Read Data Entry err = SetFPos(inRefNum, fsFromStart, sizeof(PASHeader)); if (err != noErr) goto error; sizeOfEntry = sizeof(PASEntry); err = FSRead(inRefNum, &sizeOfEntry, &dataEntry); if (err != noErr) goto error; // Read Misc Entry err = SetFPos(inRefNum, fsFromStart, (sizeof(PASHeader) + sizeof(PASEntry))); if (err != noErr) goto error; sizeOfEntry = sizeof(PASEntry); err = FSRead(inRefNum, &sizeOfEntry, &miscEntry); if (err != noErr) goto error; // Read Resource Entry err = SetFPos(inRefNum, fsFromStart, (sizeof(PASHeader) + (2 * sizeof(PASEntry)))) ; if (err != noErr) goto error; sizeOfEntry = sizeof(PASEntry); err = FSRead(inRefNum, &sizeOfEntry, &resourceEntry); if (err != noErr) goto error; err = PAS_decodeData(&dataEntry, outSpec, inRefNum); if (err != noErr) goto error; err = PAS_decodeMisc(&miscEntry, outSpec, inRefNum); if (err != noErr) goto error; err = PAS_decodeResource(&resourceEntry, outSpec, inRefNum); if (err == kResFileNotOpened) { // there was no resource fork err = noErr; } else if (err != noErr) { goto error; } FSClose(inRefNum); return noErr; error: if (inRefNum != kResFileNotOpened) { FSClose(inRefNum); } FSpDelete( outSpec ) ; return err; }
void InitLicTxt(void) { Rect destRect, viewRect; FSSpec licFile; long dirID, dataSize; short vRefNum, dataRef, resRef; unsigned char* cLicFName; Str255 pLicFName; OSErr err; Handle text, stylHdl; ERR_CHECK(GetCWD(&dirID, &vRefNum)); /* open and read license file */ HLock(gControls->cfg->licFileName); if(**gControls->cfg->licFileName != nil) { cLicFName = CToPascal(*gControls->cfg->licFileName); ERR_CHECK(FSMakeFSSpec(vRefNum, dirID, cLicFName, &licFile)); if (cLicFName) DisposePtr((char*)cLicFName); } else /* assume default license filename from str rsrc */ { GetResourcedString(pLicFName, rInstList, sLicenseFName); ERR_CHECK(FSMakeFSSpec(vRefNum, dirID, pLicFName, &licFile)); } HUnlock(gControls->cfg->licFileName); /* read license text */ ERR_CHECK(FSpOpenDF( &licFile, fsRdPerm, &dataRef)); ERR_CHECK(GetEOF(dataRef, &dataSize)); if (dataSize > 0) { if (!(text = NewHandle(dataSize))) { ErrorHandler(eMem, nil); return; } ERR_CHECK(FSRead(dataRef, &dataSize, *text)); } else text = nil; ERR_CHECK(FSClose(dataRef)); /* get 'styl' if license is multistyled */ resRef = FSpOpenResFile( &licFile, fsRdPerm); ERR_CHECK(ResError()); UseResFile(resRef); stylHdl = RGetResource('styl', 128); ERR_CHECK(ResError()); if(stylHdl) DetachResource(stylHdl); else stylHdl = nil; CloseResFile(resRef); /* TE specific init */ HLock( (Handle) gControls->lw->licBox); SetRect(&viewRect, (*(gControls->lw->licBox))->contrlRect.left, (*(gControls->lw->licBox))->contrlRect.top, (*(gControls->lw->licBox))->contrlRect.right, (*(gControls->lw->licBox))->contrlRect.bottom); HUnlock( (Handle) gControls->lw->licBox); destRect.left = viewRect.left; viewRect.right = (*(gControls->lw->scrollBar))->contrlRect.left; destRect.right = viewRect.right; destRect.top = viewRect.top; destRect.bottom = viewRect.bottom * kNumLicScrns; // gControls->lw->licTxt = (TEHandle) NewPtrClear(sizeof(TEPtr)); TextFont(applFont); TextFace(normal); TextSize(9); HLock(text); if (stylHdl) { gControls->lw->licTxt = TEStyleNew( &destRect, &viewRect ); TEStyleInsert( *text, dataSize, (StScrpRec ** )stylHdl, gControls->lw->licTxt); } else { gControls->lw->licTxt = TENew( &destRect, &viewRect); TEInsert( *text, dataSize, gControls->lw->licTxt); } HUnlock(text); TextFont(systemFont); TextSize(12); TESetAlignment(teFlushDefault, gControls->lw->licTxt); }