Exemple #1
0
void ASF_LegacyManager::ImportLegacy ( SXMPMeta* xmp )
{
	std::string utf8;

	if ( ! broadcastSet ) {
		ConvertMSDateToISODate ( fields[fieldCreationDate], &utf8 );
		if ( ! utf8.empty() ) xmp->SetProperty ( kXMP_NS_XMP, "CreateDate", utf8.c_str(), kXMP_DeleteExisting );
	}

	FromUTF16 ( (UTF16Unit*)fields[fieldTitle].c_str(), (fields[fieldTitle].size() / 2), &utf8, false );
	if ( ! utf8.empty() ) xmp->SetLocalizedText ( kXMP_NS_DC, "title", "", "x-default", utf8.c_str(), kXMP_DeleteExisting );

	xmp->DeleteProperty ( kXMP_NS_DC, "creator" );
	FromUTF16 ( (UTF16Unit*)fields[fieldAuthor].c_str(), (fields[fieldAuthor].size() / 2), &utf8, false );
	if ( ! utf8.empty() ) SXMPUtils::SeparateArrayItems ( xmp, kXMP_NS_DC, "creator",
														  (kXMP_PropArrayIsOrdered | kXMPUtil_AllowCommas), utf8.c_str() );

	FromUTF16 ( (UTF16Unit*)fields[fieldCopyright].c_str(), (fields[fieldCopyright].size() / 2), &utf8, false );
	if ( ! utf8.empty() ) xmp->SetLocalizedText ( kXMP_NS_DC, "rights", "", "x-default", utf8.c_str(), kXMP_DeleteExisting );

	FromUTF16 ( (UTF16Unit*)fields[fieldDescription].c_str(), (fields[fieldDescription].size() / 2), &utf8, false );
	if ( ! utf8.empty() ) xmp->SetLocalizedText ( kXMP_NS_DC, "description", "", "x-default", utf8.c_str(), kXMP_DeleteExisting );

	if ( ! fields[fieldCopyrightURL].empty() ) xmp->SetProperty ( kXMP_NS_XMP_Rights, "WebStatement", fields[fieldCopyrightURL].c_str(), kXMP_DeleteExisting );

#if ! Exclude_LicenseURL_Recon
	if ( ! fields[fieldLicenseURL].empty() ) xmp->SetProperty ( kXMP_NS_XMP_Rights, "Certificate", fields[fieldLicenseURL].c_str(), kXMP_DeleteExisting );
#endif

	imported = true;

}
Exemple #2
0
bool ID3v2Frame::getFrameValue ( XMP_Uns8 majorVersion, XMP_Uns32 logicalID, std::string* utf8string )
{

	XMP_Assert ( (this->content != 0) && (this->contentSize >= 0) && (this->contentSize < 20*1024*1024) );

	if ( this->contentSize == 0 ) {
		utf8string->erase();
		return true; // ...it is "of interest", even if empty contents.
	}

	XMP_Int32 pos = 0;
	XMP_Uns8 encByte = 0;
	// WCOP does not have an encoding byte, for all others: use [0] as EncByte, advance pos
	if ( logicalID != 0x57434F50 ) {
		encByte = this->content[0];
		pos++;
	}

	// mode specific forks, COMM or USLT
	bool commMode = ( (logicalID == 0x434F4D4D) || (logicalID == 0x55534C54) );

	switch ( encByte ) {

		case 0: //ISO-8859-1, 0-terminated
		{
			if ( commMode && (! advancePastCOMMDescriptor ( pos )) ) return false; // not a frame of interest!

			char* localPtr  = &this->content[pos];
			size_t localLen = this->contentSize - pos;
			ReconcileUtils::Latin1ToUTF8 ( localPtr, localLen, utf8string );
			break;

		}

		case 1: // Unicode, v2.4: UTF-16 (undetermined Endianess), with BOM, terminated 0x00 00
		case 2: // UTF-16BE without BOM, terminated 0x00 00
		{

			if ( commMode && (! advancePastCOMMDescriptor ( pos )) ) return false; // not a frame of interest!

			std::string tmp ( this->content, this->contentSize );
			bool bigEndian = true;	// assume for now (if no BOM follows)

			if ( GetUns16BE ( &this->content[pos] ) == 0xFEFF ) {
				pos += 2;
				bigEndian = true;
			} else if ( GetUns16BE ( &this->content[pos] ) == 0xFFFE ) {
				pos += 2;
				bigEndian = false;
			}

			FromUTF16 ( (UTF16Unit*)&this->content[pos], ((this->contentSize - pos)) / 2, utf8string, bigEndian );
			break;

		}

		case 3: // UTF-8 unicode, terminated \0
		{
			if ( commMode && (! advancePastCOMMDescriptor ( pos )) ) return false; // not a frame of interest!
		
			if ( (GetUns32BE ( &this->content[pos]) & 0xFFFFFF00 ) == 0xEFBBBF00 ) {
				pos += 3;	// swallow any BOM, just in case
			}

			utf8string->assign ( &this->content[pos], (this->contentSize - pos) );
			break;
		}

		default:
			XMP_Throw ( "unknown text encoding", kXMPErr_BadFileFormat ); //COULDDO assume latin-1 or utf-8 as best-effort
			break;

	}

	return true;

}	// ID3v2Frame::getFrameValue
STDMETHODIMP FileEncrypter::EncryptFile(BSTR szInFN,BSTR szOutFN,BSTR szPwd){
	if (!szInFN || _tcslen(szInFN)==0) {
		InfoMsgBox(_T("对不起,您必须提供待加密的文件名!"));
		return S_OK;
	}
	if (!szOutFN || _tcslen(szOutFN)==0) {
		InfoMsgBox(_T("对不起,您必须提供加密后的文件名!"));
		return S_OK;
	}
	FILE *hSource=0; 
	FILE *hDestination=0; 

	HCRYPTPROV hCryptProv; 
	HCRYPTKEY hKey; 
	HCRYPTKEY hXchgKey; 
	HCRYPTHASH hHash; 
	
	PBYTE pbKeyBlob; 
	DWORD dwKeyBlobLen; 
	
	PBYTE pbBuffer; 
	DWORD dwBlockLen; 
	DWORD dwBufferLen; 
	DWORD dwCount; 

	char* utf8Pwd=NULL;

	TCHAR msgTip[STRLEN_DEFAULT]={0};
	_tfopen_s(&hSource,szInFN,_T("rb"));
	if(!hSource){
		_stprintf_s(msgTip,_T("对不起,无法打开文件“%s”!"),szInFN);
		InfoMsgBox(msgTip);
		goto clean;
	}
	_tfopen_s(&hDestination,szOutFN,_T("wb"));
	if(!hDestination){
		_stprintf_s(msgTip,_T("对不起,无法打开文件“%s”!"),szOutFN);
		InfoMsgBox(msgTip);
		goto clean;
	}

	TCHAR szContainerName[]=_T("f0d74c20-4a68-47d2-a1d2-7c622399ad48");	//TODO:容器名称为“test”?
	//初始化CSP上下文
	if(CryptAcquireContext(&hCryptProv,szContainerName,PROVIDER_NAME,PROV_RSA_FULL,0))
	{
		//正确初始化CSP
	}
	else
	{
		if(!CryptAcquireContext(&hCryptProv,szContainerName,PROVIDER_NAME,PROV_RSA_FULL,CRYPT_NEWKEYSET)){
			InfoMsgBox(_T("对不起,无法初始化CSP上下文,请确认UKey是否插入电脑且工作正常,并安装了正确的驱动程序!"));
			goto clean;
		}
	}
	//如果没有提供密码
	if(!szPwd || _tcslen(szPwd)==0){
		if(!CryptGenKey(hCryptProv,ENCRYPT_ALGORITHM,CRYPT_EXPORTABLE, /*KEYLENGTH | CRYPT_EXPORTABLE, */&hKey))
		{
			InfoMsgBox(_T("对不起,无法生成密钥,请确认UKey是否插入电脑且工作正常,并安装了正确的驱动程序!"));
			goto clean;
		}
		if(CryptGetUserKey(hCryptProv,AT_KEYEXCHANGE,&hXchgKey)){
			//公钥被成功获取
		}
		else{
			CryptGenKey(hCryptProv,AT_KEYEXCHANGE,0,&hXchgKey);
		}
		if(!CryptExportKey(hKey,hXchgKey,SIMPLEBLOB, 0,NULL, &dwKeyBlobLen)){
			InfoMsgBox(_T("对不起,无法获取密钥块,请确认UKey是否插入电脑且工作正常,并安装了正确的驱动程序!"));
			goto clean;
		}
		if(!(pbKeyBlob =(BYTE *)malloc(dwKeyBlobLen)))
		{ 
			InfoMsgBox(_T("对不起,无法为密钥块分配内存!"));
			goto clean;
		}
		if(!CryptExportKey(hKey,hXchgKey,SIMPLEBLOB,0, pbKeyBlob, &dwKeyBlobLen)){
			InfoMsgBox(_T("对不起,无法为导出密钥!"));
			goto clean;
		}
		CryptDestroyKey(hXchgKey); 
		hXchgKey = 0;
		fwrite(&dwKeyBlobLen, sizeof(DWORD), 1, hDestination); 
		if(ferror(hDestination))
		{
			InfoMsgBox(_T("无法写入输出文件!"));
			goto clean;
		}
		fwrite(pbKeyBlob, 1, dwKeyBlobLen, hDestination); 
		if(ferror(hDestination))
		{ 
			InfoMsgBox(_T("无法写入输出文件!"));
			goto clean;
		}
	}
	else{	//提供了密码
		if(!CryptCreateHash(hCryptProv,CALG_MD5, 0,0,&hHash))
		{
			InfoMsgBox(_T("无法创建哈希!"));
			goto clean;
		}
		int keyLen=FromUTF16(szPwd,NULL,0);
		if (keyLen>0) keyLen+=1;
		utf8Pwd=new char[keyLen];
		FromUTF16(szPwd,utf8Pwd,keyLen);
		if(!CryptHashData( hHash, (BYTE *)utf8Pwd, keyLen-1, 0))
		{
			InfoMsgBox(_T("无法创建密码哈希值!"));
			goto clean;
		}
		if(!CryptDeriveKey( hCryptProv,ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
		{
			InfoMsgBox(_T("无法从哈希值创建密钥!"));
			goto clean;
		}
		CryptDestroyHash(hHash); 
		hHash = 0; 
	}
	dwBlockLen = 1024 - 1024 % ENCRYPT_BLOCK_SIZE; 
	if(ENCRYPT_BLOCK_SIZE > 1) 
		dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; 
	else 
		dwBufferLen = dwBlockLen; 

	if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
	{
		InfoMsgBox(_T("对不起,无法为缓冲区分配内存!"));
		goto clean;
	}
	//循环读取源文件的数据然后加密数据并把结果写入目标文件
	do
	{ 
		dwCount = fread(pbBuffer, 1, dwBlockLen, hSource); 
		if(ferror(hSource))
		{ 
			InfoMsgBox(_T("对不起,无法读取待加密文件数据!"));
			goto clean;
		}
		
		if(!CryptEncrypt(hKey,0,feof(hSource),0,pbBuffer,&dwCount,dwBufferLen))
		{ 
			InfoMsgBox(_T("加密过程出现错误!"));
			goto clean;
		} 
		
		fwrite(pbBuffer, 1, dwCount, hDestination); 
		if(ferror(hDestination))
		{ 
			InfoMsgBox(_T("对不起,无法写入加密后的数据到目标文件!"));
			goto clean;
		}
	} 
	while(!feof(hSource)); 
	InfoMsgBox(_T("加密成功!"));
	//资源释放
clean:
	if(utf8Pwd) delete[] utf8Pwd;
	if(hSource) fclose(hSource); 
	if(hDestination) fclose(hDestination);
	if(pbBuffer) free(pbBuffer); 

	if(hKey) CryptDestroyKey(hKey); 

	if(hXchgKey) CryptDestroyKey(hXchgKey); 

	if(hHash) CryptDestroyHash(hHash); 

	if(hCryptProv) CryptReleaseContext(hCryptProv, 0);
	return S_OK;
}