📄 optestutils.c
字号:
void dumpTAR( PGPByte * buffer, int length, PGPBoolean detail)
{
TARrecord *t = (TARrecord*)buffer;
char modeStr[12];
char timeStr[64];
static char *linkChar = "-2lcbdp7";
static char *modeChar = "-rwx";
char firstTime = TRUE;
char emptyHeadersConsecutively = 0;
OPTESTPrintF("\tTAR Records (%d bytes)\n",length);
while( (PGPByte*)t < (buffer + length))
{
char *p;
long int size = strtol(t->header.size, &p, 8);
unsigned int mode = strtol(t->header.mode, &p, 8);
unsigned long int chksum = strtol(t->header.chksum, &p, 8);
time_t mtime = strtol(t->header.mtime, &p, 8);
if(detail && !firstTime) OPTESTPrintF("\n");
if(firstTime) firstTime = FALSE;
if( t->header.arch_name[0] == 0)
{
OPTESTPrintF("\t -- NULL RECORD -\n");
emptyHeadersConsecutively++;
if(emptyHeadersConsecutively > 1) break;
}
else
{
emptyHeadersConsecutively = 0;
modeStr[0]=0;
/* link mode */
if((t->header.linkflag >= LF_NORMAL) && (t->header.linkflag <= LF_CONTIG))
strcatchar(modeStr, linkChar[t->header.linkflag - LF_NORMAL]);
else if (t->header.linkflag == LF_OLDNORMAL) strcatchar(modeStr,linkChar[0]);
else strcatchar(modeStr, t->header.linkflag);
/* file protection */
strcatchar(modeStr, modeChar[ mode & TUREAD?1:0]);
strcatchar(modeStr, modeChar[ mode & TUWRITE?2:0]);
strcatchar(modeStr, modeChar[ mode & TUEXEC?3:0]);
strcatchar(modeStr, modeChar[ mode & TGREAD?1:0]);
strcatchar(modeStr, modeChar[ mode & TGWRITE?2:0]);
strcatchar(modeStr, modeChar[ mode & TGEXEC?3:0]);
strcatchar(modeStr, modeChar[ mode & TOREAD?1:0]);
strcatchar(modeStr, modeChar[ mode & TOWRITE?2:0]);
strcatchar(modeStr, modeChar[ mode & TOEXEC?3:0]);
#if PGP_WIN32
strftime( timeStr, sizeof(timeStr), "%d %b %X", localtime(&mtime));
#else
strftime( timeStr, sizeof(timeStr), "%d %b %R", localtime(&mtime));
#endif
OPTESTPrintF("\t%6ld ", chksum);
OPTESTPrintF(" %s %8s %8s %6ld %s ",modeStr,t->header.uname, t->header.gname,
size ,timeStr);
printTruncated( t->header.arch_name, strlen( t->header.arch_name), 32);
}
if(detail) dumpHex((PGPByte*)t, 512, (int)((PGPByte *)t-buffer));
t = (TARrecord*)((PGPByte*)t + 512 + size + ((~(size & 0x01FF) + 1) & 0x1FF));
// if(detail) dumpHex( (unsigned char*)t - 512, 512, (int)t - (int)buffer - 512);
}
// if(detail) dumpHex(buffer, length, 0);
}
/* [ */
//---------------------------------------------------------------
#include "pgpCBC.h"
#include "pgpCFB.h"
PGPError dumpTarCacheContent(PGPContextRef context, char* filename, PGPByte * sessionKey, char detail)
{
PGPError err = kPGPError_NoErr;
PGPSymmetricCipherContextRef cipher = kInvalidPGPSymmetricCipherContextRef;
PGPCBCContextRef cbc = kInvalidPGPCBCContextRef;
PGPSize keySize, blockSize;
int fd = 0;
PGPByte *data = NULL;
PGPByte key2[32] = {0};
PGPByte IV[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
fd = open(filename, O_RDONLY, 0); // Added 0 for Mobile
if(fd)
{
struct stat st;
PGPUInt32 size;
fstat(fd, &st);
size = st.st_size;
OPTESTPrintF("\tDumping Tar cache \"%s\" %u bytes\n", filename, (int)size);
if(size)
{
PGPUInt32 bytesRead = 0;
data = PGPNewSecureData( PGPGetDefaultMemoryMgr(), size, kPGPMemoryMgrFlags_None);
do
{
int retval;
retval = read( fd, (char *) data + bytesRead , size - bytesRead );
if(retval == -1 && errno == EAGAIN) continue;
if( retval < 0 ) return kPGPError_ReadFailed;
if( retval == 0 ) break;
bytesRead += retval;
}while(bytesRead < size);
if( bytesRead == size)
{
OPTESTPrintF(" SessionKey \n");
dumpHex(sessionKey, 25,0);
err = PGPNewSymmetricCipherContext(context, sessionKey[0], &cipher); CKERR;
err = PGPGetSymmetricCipherSizes( cipher, &keySize,&blockSize);CKERR;
err = PGPNewCBCContext(cipher, &cbc); CKERR;
cipher = kInvalidPGPSymmetricCipherContextRef;
err = PGPInitCBC(cbc, &sessionKey[1], IV); CKERR;
err = PGPCBCDecrypt(cbc, data+20, keySize, key2 );CKERR
(void) PGPFreeCBCContext(cbc);
cbc = kInvalidPGPCBCContextRef;
OPTESTPrintF("\n mainheader+20 \n");
dumpHex(data+20, 32,0);
OPTESTPrintF("\n Decoded Tar Key \n");
dumpHex(key2, 32,0);
if(bytesRead > 512)
{
err = PGPNewSymmetricCipherContext(context, kPGPCipherAlgorithm_AES128, &cipher); CKERR;
err = PGPNewCBCContext(cipher, &cbc); CKERR;
cipher = kInvalidPGPSymmetricCipherContextRef;
err = PGPInitCBC(cbc, key2, data+20); CKERR;
err = PGPCBCDecrypt(cbc, data+512, bytesRead-512, data+512 );CKERR
OPTESTPrintF("\n");
dumpTAR(data+512, bytesRead-512, detail);
}
}
}
}
OPTESTPrintF("\n");
done:
if( PGPSymmetricCipherContextRefIsValid( cipher ) )
(void) PGPFreeSymmetricCipherContext( cipher );
if( PGPCBCContextRefIsValid( cbc ) )
(void) PGPFreeCBCContext(cbc);
if( data != NULL) PGPFreeData(data);
if(fd) close(fd);
return err;
}
//---------------------------------------------------------------
/* ] */
#if PGP_WIN32 || PGP_SYMBIAN
PGPError getPassphrase(
PGPSize passphraseSize,
PGPChar8 *passphrase)
{
PGPError err = kPGPError_NoErr;
PGPBoolean bDone = FALSE;
unsigned int currentSize = 0;
fprintf (stderr, PGPTEXT("Enter Passphrase: "));
ZERO ((void *)passphrase, passphraseSize);
currentSize = 0;
for (bDone = FALSE; !bDone; )
{
passphrase[currentSize] = getc (stdin);
switch (passphrase[currentSize])
{
case PGPTXT_MACHINE('\n'):
passphrase[currentSize] = PGPTXT_MACHINE('\0');
bDone = TRUE;
break;
case 0x03:
/* Break */
fprintf (stderr, PGPTXT_MACHINE("\n"));
/* Don't set this here, otherwise we get prompted for confirmation */
/* g_bSignalBreak++; */
RETERR (kPGPError_UserAbort);
break;
case 0x7F:
/* Backspace */
if (currentSize)
currentSize--;
break;
default:
currentSize++;
break;
}
if (currentSize == passphraseSize)
RETERR (kPGPError_BufferTooSmall);
}
fprintf (stderr, PGPTXT_MACHINE("\n"));
done:
return err;
}
#else
#include <termios.h>
PGPError getPassphrase(
PGPSize passphraseSize,
PGPChar8 *passphrase)
{
PGPError err = kPGPError_NoErr;
struct termios origTerm /* Init */;
struct termios newTerm /* Init */;
PGPBoolean bDone = FALSE;
unsigned int currentSize = 0;
ZERO ((void *)&origTerm, sizeof(origTerm));
tcgetattr (0, &origTerm);
memcpy ((void *)&newTerm, (void *)&origTerm, sizeof(struct termios));
/* Disable canonical mode and echo, set timeout to zero and buffer size to 1 */
newTerm.c_lflag &= (~ICANON);
newTerm.c_lflag &= (~ECHO);
#if PGP_UNIX_DARWIN
newTerm.c_lflag &= (~ISIG);
#else
newTerm.c_lflag &= (~IGNBRK);
newTerm.c_lflag &= (~BRKINT);
#endif
newTerm.c_cc[VTIME] = 0;
newTerm.c_cc[VMIN] = 1;
tcsetattr (0, TCSANOW, &newTerm);
fprintf (stderr, PGPTEXT("Enter Passphrase: "));
ZERO ((void *)passphrase, passphraseSize);
currentSize = 0;
for (bDone = FALSE; !bDone; )
{
passphrase[currentSize] = getc (stdin);
switch (passphrase[currentSize])
{
case PGPTXT_MACHINE('\n'):
passphrase[currentSize] = PGPTXT_MACHINE('\0');
bDone = TRUE;
break;
case 0x03:
/* Break */
fprintf (stderr, PGPTXT_MACHINE("\n"));
/* Don't set this here, otherwise we get prompted for confirmation */
/* g_bSignalBreak++; */
RETERR (kPGPError_UserAbort);
break;
case 0x7F:
/* Backspace */
if (currentSize)
currentSize--;
break;
default:
currentSize++;
break;
}
if (currentSize == passphraseSize)
RETERR (kPGPError_BufferTooSmall);
}
fprintf (stderr, PGPTXT_MACHINE("\n"));
done:
tcsetattr (0, TCSANOW, &origTerm);
return err;
}
#endif
int makeFile(char *name)
{
int fd;
#if PGP_WIN32
fd = _open(name, O_CREAT | O_TRUNC | O_WRONLY, _S_IREAD | _S_IWRITE);
if(fd < 1) return errno;
_close(fd);
#else
fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU);
if(fd < 1) return errno;
close(fd);
#endif
return 0;
}
int makeDirectory(char *dirname )
{
int retval;
#if PGP_WIN32
retval = _mkdir(dirname);
#else
retval = mkdir(dirname, 0777);
#endif
return (retval == 0?0: (errno == EEXIST?0:-1));
}
/*---------------------------------------------------------------------------------------
platform information
---------------------------------------------------------------------------------------*/
#if PGP_OSX
#include <ctype.h>
int sGetValueString(char *buffer, int bufsize, char *tag, char **value )
{
char* p, *p1;
p = strnstr(buffer,tag, bufsize);
if(p)
{
for(p+=strlen(tag); p && isspace(*p); p++);
for(p1 = p; p1 && !iscntrl(*p1); p1++);
if(p1-p > 1)
{
*value = p;
return p1-p;
}
}
return 0;
}
void getPlatformInfo(char* osName, char* hwName)
{
FILE *fp;
char *buf, *value;
size_t itemlen, buflen;
*osName = 0;
*hwName = 0;
fp = popen("system_profiler SPHardwareDataType SPSoftwareDataType", "r");
while( buf = fgetln(fp, &buflen))
{
if(itemlen = sGetValueString(buf, buflen, "Machine Name:", &value))
{
strncpy(hwName, value,itemlen);
hwName[itemlen] = 0;
}
else if(itemlen = sGetValueString(buf, buflen, "System Version:", &value))
{
strncpy(osName, value,itemlen);
osName[itemlen] = 0;
}
else if(itemlen = sGetValueString(buf, buflen, "CPU Type:", &value))
{
strcat(hwName, ", ");
strncat(hwName, value,itemlen);
}
else if(itemlen = sGetValueString(buf, buflen, "Processor Name:", &value))
{
strcat(hwName, ", ");
strncat(hwName, value,itemlen);
}
}
pclose (fp);
}
#elif PGP_UNIX
#include <sys/utsname.h>
void getPlatformInfo(char* osName, char* hwName)
{
struct utsname osinfo;
uname(&osinfo);
strcpy(osName, osinfo.version);
strcpy(hwName, osinfo.machine);
}
#elif PGP_WIN32
#include <windows.h>
void getPlatformInfo(char* osName, char* hwName)
{
OSVERSIONINFOW osi;
*osName = 0;
*hwName = 0;
osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionExW(&osi))
{
switch (osi.dwPlatformId)
{
// Test for the Windows NT product family.
case VER_PLATFORM_WIN32_NT:
// Test for the specific product.
if ( osi.dwMajorVersion == 5 && osi.dwMinorVersion == 2 )
strcat (osName, "Microsoft Windows Server 2003, ");
if ( osi.dwMajorVersion == 5 && osi.dwMinorVersion == 1 )
strcat (osName,"Microsoft Windows XP ");
if ( osi.dwMajorVersion == 5 && osi.dwMinorVersion == 0 )
strcat (osName,"Microsoft Windows 2000 ");
if ( osi.dwMajorVersion <= 4 )
strcat (osName, "Microsoft Windows NT ");
break;
// Test for the Windows Me/98/95.
case VER_PLATFORM_WIN32_WINDOWS:
if (osi.dwMajorVersion == 4 && osi.dwMinorVersion == 0)
{
strcat (osName, "Microsoft Windows 95 ");
if (osi.szCSDVersion[1]=='C' || osi.szCSDVersion[1]=='B')
strcat (osName, "OSR2 " );
}
if (osi.dwMajorVersion == 4 && osi.dwMinorVersion == 10)
{
strcat (osName, "Microsoft Windows 98 ");
if ( osi.szCSDVersion[1] == 'A' )
strcat (osName, "SE " );
}
if (osi.dwMajorVersion == 4 && osi.dwMinorVersion == 90)
{
strcat (osName,"Microsoft Windows Millennium Edition\n");
}
break;
default:
sprintf(osName,"Microsoft(%d)", osi.dwPlatformId);
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -