📄 pgpmacbinary.c
字号:
#endif
err = PGPNewLineEndIO( outRef, FALSE, toType, &convertIO );
if ( IsntPGPError( err ) )
{
err = PGPCopyIO( inRef, requestCount, (PGPIORef)convertIO );
PGPFreeIO( (PGPIORef)convertIO );
convertIO = NULL;
}
return( err );
}
/*____________________________________________________________________________
Non-Mac Version
____________________________________________________________________________*/
static PGPError
sCreateFromMacBinary(
PGPIORef inRef,
MacBinaryHeader const * header,
PFLFileSpecRef outSpec )
{
PGPError err;
err = PFLFileSpecCreate( outSpec );
if ( IsntPGPError( err ) && header->dLength != 0 )
{
PGPIORef outRef;
err = PGPOpenFileSpec( outSpec, kPFLFileOpenFlags_ReadWrite,
(PGPFileIORef *)&outRef );
if ( IsntPGPError( err ) )
{
err = PGPIOSetPos( inRef, kPGPMacBinaryHeaderSize );
if ( IsntPGPError( err ) )
{
#define kFileTypeText ((OSType)0x54455854) /* 'TEXT' */
if ( header->info1.fdType != kFileTypeText )
{
err = PGPCopyIO( inRef, header->dLength, outRef );
}
else
{
err = sCopyAndLineEndConvert( inRef,
header->dLength, outRef );
}
}
PGPFreeIO( outRef );
}
}
return( err );
}
/*____________________________________________________________________________
Non-Mac Version
Get a unique filename using the baseSpec as a directory specifier and
first choice for file name.
If baseSpec does not exist, then an equivalent spec will be created with
that name.
____________________________________________________________________________*/
static PGPError
sGetUniqueTempSpec(
PFLFileSpecRef baseSpec,
PFLFileSpecRef * outSpec)
{
PGPError err;
PFLFileSpecRef tempSpec = NULL;
err = PFLCopyFileSpec( baseSpec, &tempSpec );
if ( IsntPGPError( err ) )
{
static const char kTempFileBase[] = "temp";
static const char kTempFileExtension[] = "tmp";
char tempName[ 64 ];
PGPUInt32 number = 0;
/* loop until name is unique */
do
{
PGPBoolean exists;
err = PFLFileSpecExists( tempSpec, &exists );
if ( IsPGPError( err ) )
break;
if ( ! exists )
break; /* good, done */
++number;
/* generate "temp 1.tmp", "temp 2.tmp", etc */
sprintf( tempName, "%s%d.%s",
kTempFileBase, (int)number, kTempFileExtension );
err = PFLSetFileSpecName( tempSpec, tempName );
if ( IsPGPError( err ) )
break;
} while ( TRUE );
if ( IsPGPError( err ) )
{
PFLFreeFileSpec( tempSpec );
tempSpec = kInvalidPFLFileSpecRef;
}
}
*outSpec = tempSpec;
return( err );
}
/*____________________________________________________________________________
Non-Mac Version
Remove the existing file name extension (if any) and add the new one.
____________________________________________________________________________*/
static PGPError
sCreateNewName(
PFLFileSpecRef inSpec,
char const * extension,
PGPSize maxSize,
char * newName )
{
PGPError err = kPGPError_NoErr;
PGPSize newLength;
newName[ 0 ] = '\0';
err = PFLGetFileSpecNameBuffer( inSpec, maxSize, newName );
if ( IsntPGPError( err ) )
{
char const * existingExtension;
existingExtension = PGPGetFileNameExtension( newName );
if ( IsntNull( existingExtension ) )
{
newLength = strlen( newName ) -
strlen( existingExtension );
newLength -= 1; /* account for '.' */
newName[ newLength ] = '\0';
existingExtension = NULL;
}
/* add the appropriate extension */
strcat( newName, "." );
strcat( newName, extension );
}
return( err );
}
/*____________________________________________________________________________
Determine if the existing file name extension is already suitable for
this type of file.
____________________________________________________________________________*/
static PGPBoolean
sHasSuitableExtensionForType(
OSType creator,
OSType type,
PFLFileSpecRef spec )
{
PGPError err = kPGPError_NoErr;
PGPBoolean isSuitable = FALSE;
char name[ 512 ];
err = PFLGetFileSpecNameBuffer( spec, sizeof( name ), name );
if ( IsntPGPError( err ) )
{
char const *extension;
extension = PGPGetFileNameExtension( name );
if ( IsntNull( extension ) )
{
isSuitable = pgpIsValidExtensionForMacType( creator, type,
extension );
}
}
return( isSuitable );
}
/*____________________________________________________________________________
Non-Mac version.
De-MacBinarize the file giving the new file a suitable file name extension
____________________________________________________________________________*/
static PGPError
sDeMacBinarize(
PFLFileSpecRef inSpec,
MacBinaryHeader const * header,
PFLFileSpecRef * outSpec )
{
PGPError err = kPGPError_NoErr;
PGPIORef inRef = kInvalidPGPIORef;
PFLFileSpecRef tempSpec = kInvalidPFLFileSpecRef;
*outSpec = kInvalidPFLFileSpecRef;
/* open the MacBinary file */
err = PGPOpenFileSpec( inSpec, kPFLFileOpenFlags_ReadOnly,
(PGPFileIORef *)&inRef );
if ( IsntPGPError( err ) )
{
/* we'll put the output into a temp file */
err = sGetUniqueTempSpec( inSpec, &tempSpec );
if ( IsntPGPError( err ) )
{
err = sCreateFromMacBinary( inRef, header, tempSpec );
}
/* all done with input */
PGPFreeIO( inRef );
inRef = NULL;
if ( IsntPGPError( err ) )
{
char extension[ 3 + 1 ];
PGPBoolean haveExtension;
PGPMemoryMgrRef memoryMgr;
char newName[ 512 ];
memoryMgr = PFLGetFileSpecMemoryMgr( inSpec );
/* delete original and move output into its place */
(void)PFLFileSpecDelete( inSpec );
if ( sHasSuitableExtensionForType( header->info1.fdCreator,
header->info1.fdType, inSpec ) )
{
haveExtension = FALSE;
}
else
{
err = pgpMapMacCreatorTypeToExtension(
header->info1.fdCreator,
header->info1.fdType, extension );
/* ok if mapping failed */
haveExtension = IsntPGPError( err );
err = kPGPError_NoErr;
}
if ( haveExtension )
{
err = sCreateNewName( inSpec,
extension, sizeof( newName ), newName );
}
else
{
/* just give it the same name as the original */
err = PFLGetFileSpecNameBuffer( inSpec,
sizeof( newName ), newName );
}
if ( IsntPGPError( err ) )
{
/* this changes the name and renames the file */
err = PFLFileSpecRename( tempSpec, newName );
}
pgpAssertNoErr( err );
}
}
*outSpec = tempSpec;
return( err );
}
#endif /* ] PGP_MACINTOSH */
/*____________________________________________________________________________
Internal routine to convert a MacBinary format file to something most
appropriate for the local machine.
____________________________________________________________________________*/
PGPError
pgpMacBinaryToLocal(
PFLFileSpecRef inSpec,
PFLFileSpecRef * outSpec,
PGPUInt32 * macCreator,
PGPUInt32 * macType )
{
PGPError err = kPGPError_NoErr;
PGPBoolean isValid;
MacBinaryHeader header;
(void)inSpec;
(void)outSpec;
if ( IsntNull( macCreator ) )
*macCreator = 0;
if ( IsntNull( macType ) )
*macType = 0;
err = sReadMacBinaryHeader( inSpec, &header, &isValid );
if ( IsntPGPError( err ) )
{
if ( isValid )
{
/* if there's no data fork, don't de-macbinarize it */
if ( header.dLength == 0 )
err = kPGPError_NoMacBinaryTranslationAvailable;
}
else
{
err = kPGPError_NotMacBinary;
}
}
if ( IsntPGPError( err ) )
{
if ( IsntNull( macCreator ) )
*macCreator = header.info1.fdCreator;
if ( IsntNull( macType ) )
*macType = header.info1.fdType;
err = sDeMacBinarize( inSpec, &header, outSpec );
}
return( err );
}
/*__Editor_settings____
Local Variables:
tab-width: 4
End:
vi: ts=4 sw=4
vim: si
_____________________*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -