pwimport.cpp

来自「一款密码保险箱源码」· C++ 代码 · 共 786 行 · 第 1/2 页

CPP
786
字号
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2007 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "StdAfx.h"
#include "PwImport.h"

#include "../Util/MemUtil.h"
#include "../Util/StrUtil.h"
#include "../Util/TranslateEx.h"

static TCHAR g_pNullString[4] = { 0, 0, 0, 0 };

CPwImport::CPwImport()
{
}

CPwImport::~CPwImport()
{
}

char *CPwImport::FileToMemory(const TCHAR *pszFile, unsigned long *pFileSize)
{
	ASSERT(pszFile != NULL); if(pszFile == NULL) return NULL;
	if(_tcslen(pszFile) == 0) return NULL;

	FILE *fp = NULL;
	_tfopen_s(&fp, pszFile, _T("rb"));
	if(fp == NULL) return NULL;

	fseek(fp, 0, SEEK_END);
	unsigned long uFileSize = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	char *pData = new char[uFileSize + 3];
	if(pData == NULL) { fclose(fp); fp = NULL; return NULL; }
	pData[uFileSize] = 0; // Terminate buffer
	pData[uFileSize + 1] = 0;
	pData[uFileSize + 2] = 0;

	fread(pData, 1, uFileSize, fp);
	fclose(fp); fp = NULL;

	if(pFileSize != NULL) *pFileSize = uFileSize; // Store file size
	return pData;
}

DWORD CPwImport::ImportCsvToDb(const TCHAR *pszFile, CPwManager *pMgr, DWORD dwGroupId)
{
	unsigned long uFileSize, i, j = 0;
	char *pData;
	char *pProcessed;
	char ch;
	BOOL bInField = FALSE;
	BOOL bUTF8 = FALSE;
	DWORD dwFieldCounter = 0;

	ASSERT(pszFile != NULL); if(pszFile == NULL) return 0;
	ASSERT(pMgr != NULL); if(pMgr == NULL) return 0;

	if((dwGroupId == DWORD_MAX) || (dwGroupId == 0)) return 0;

	pData = CPwImport::FileToMemory(pszFile, &uFileSize);
	if(pData == NULL) return FALSE;

	pProcessed = new char[uFileSize + 2];
	if(pProcessed == NULL) { SAFE_DELETE_ARRAY(pData); return 0; }

	// Last character mustn't be an escape character
	if(pData[uFileSize - 1] == '\\') pData[uFileSize - 1] = 0;

	if(uFileSize > 3)
	{
		if((pData[0] == 0xEF) && (pData[1] == 0xBB) && (pData[2] == 0xBF))
		{
			j += 3; // Skip UTF-8 initialization characters
			bUTF8 = TRUE;
		}
	}

	if(bUTF8 == FALSE) bUTF8 = _IsUTF8String((const UTF8_BYTE *)pData);

	for(i = j; i < uFileSize; i++)
	{
		ch = pData[i];

		if((bInField == FALSE) && (ch == '\n'))
		{
			if((dwFieldCounter % 5) != 0)
			{
				SAFE_DELETE_ARRAY(pProcessed);
				SAFE_DELETE_ARRAY(pData);
				return (dwFieldCounter / 5);
			}
		}

		if(ch == 0) continue;
		else if((ch == '\\') && (pData[i+1] != 0))
		{
			i++; // Skip escape character
			pProcessed[j] = pData[i]; // Write escaped symbol
			j++; // Increase write counter
		}
		else if((bInField == FALSE) && (ch == ',') && (pData[i+1] == ','))
		{
			pProcessed[j] = 0; j++; dwFieldCounter++;
		}
		else if((bInField == FALSE) && (ch == ',') && (pData[i+1] == '\r'))
		{
			pProcessed[j] = 0; j++; dwFieldCounter++;
		}
		else if((bInField == FALSE) && (ch == ',') && (pData[i+1] == '\n'))
		{
			pProcessed[j] = 0; j++; dwFieldCounter++;
		}
		else if(ch == '\"')
		{
			if(bInField == TRUE)
			{
				pProcessed[j] = 0; j++; dwFieldCounter++;

				bInField = FALSE;
			}
			else bInField = TRUE;
		}
		else
		{
			if(bInField == TRUE)
			{
				pProcessed[j] = ch; j++;
			}
		}
	}
	if(bInField == TRUE) { SAFE_DELETE_ARRAY(pData); SAFE_DELETE_ARRAY(pProcessed); return (DWORD_MAX - 1); }

	m_pLastMgr = pMgr;
	m_dwLastGroupId = dwGroupId;
	_AddStringStreamToDb(pProcessed, j, bUTF8);

	SAFE_DELETE_ARRAY(pProcessed);
	SAFE_DELETE_ARRAY(pData);
	return DWORD_MAX;
}

BOOL CPwImport::ImportCWalletToDb(const TCHAR *pszFile, CPwManager *pMgr)
{
	char *pData;
	CString strTitle, strURL, strUserName, strPassword, strNotes;
	DWORD uFileSize, i, b;
	CString str;
	CString strLastCategory = _T("General");
	DWORD dwLastGroupId = 0;
	BOOL bInNotes = FALSE;

	ASSERT(pMgr != NULL);

	pData = CPwImport::FileToMemory(pszFile, &uFileSize);
	if(pData == NULL) return FALSE;

	strTitle.Empty(); strURL.Empty(); strUserName.Empty();
	strPassword.Empty(); strNotes.Empty();

	i = DWORD_MAX;

	if(uFileSize > 3)
		if((pData[0] == 0xEF) && (pData[1] == 0xBB) && (pData[2] == 0xBF))
			i += 3; // Skip UTF-8 initialization characters

	while(1) // Processing the file
	{
		str.Empty();

		while(1) // Loading one line to CString
		{
			i++;
			if(i >= uFileSize) break;

			if(pData[i] == '\n') break;
			if(pData[i] != '\r') str += pData[i];
		}

		// Add the entry
		if(((str.Left(1) == _T("[")) && (str.Right(1) == _T("]"))) ||
			(str == DEF_CW_CATEGORY) || (i >= uFileSize))
		{
			if((strTitle.IsEmpty() == FALSE) || (strUserName.IsEmpty() == FALSE) ||
				(strURL.IsEmpty() == FALSE) || (strPassword.IsEmpty() == FALSE))
			{
				strTitle.TrimLeft(); strTitle.TrimRight();
				strURL.TrimLeft(); strURL.TrimRight();
				strUserName.TrimLeft(); strUserName.TrimRight();
				strPassword.TrimLeft(); strPassword.TrimRight();
				strNotes.TrimLeft(); strNotes.TrimRight();

				PW_ENTRY pwTemplate;
				PW_TIME tNow;

				_GetCurrentPwTime(&tNow);
				memset(&pwTemplate, 0, sizeof(PW_ENTRY));
				pwTemplate.pszAdditional = (TCHAR *)(LPCTSTR)strNotes;
				pwTemplate.pszPassword = (TCHAR *)(LPCTSTR)strPassword;
				pwTemplate.pszTitle = (TCHAR *)(LPCTSTR)strTitle;
				pwTemplate.pszURL = (TCHAR *)(LPCTSTR)strURL;
				pwTemplate.pszUserName = (TCHAR *)(LPCTSTR)strUserName;
				pwTemplate.tCreation = tNow; CPwManager::GetNeverExpireTime(&pwTemplate.tExpire);
				pwTemplate.tLastAccess = tNow; pwTemplate.tLastMod = tNow;
				pwTemplate.uGroupId = dwLastGroupId;
				pwTemplate.uImageId = _GetPreferredIcon((LPCTSTR)strTitle);
				pwTemplate.uPasswordLen = strPassword.GetLength();

				pMgr->AddEntry(&pwTemplate);
			}

			strTitle.Empty(); strURL.Empty(); strUserName.Empty();
			strPassword.Empty(); strNotes.Empty();
			bInNotes = FALSE;
		}

		if(i >= uFileSize) break;

		if((str.Left(1) == _T("[")) && (str.Right(1) == _T("]")))
		{
			strTitle = str;
			strTitle = strTitle.Left(strTitle.GetLength() - 1);
			strTitle = strTitle.Right(strTitle.GetLength() - 1);
			continue;
		}

		if(bInNotes == TRUE)
		{
			if(strNotes.GetLength() != 0) strNotes += _T("\r\n");
			strNotes += str;
		}

		if(str.Left(10) == _T("Category: "))
		{
			strLastCategory = str.Right(str.GetLength() - 10);
			strLastCategory.TrimLeft(); strLastCategory.TrimRight();

			while(1)
			{
				strLastCategory = strLastCategory.Left(strLastCategory.GetLength() - 1);
				if(strLastCategory.GetLength() == 0) break;
				if(strLastCategory.Right(1) == _T("("))
				{
					strLastCategory = strLastCategory.Left(strLastCategory.GetLength() - 2);
					break;
				}
				if(strLastCategory.GetLength() == 0) break;
			}

			if(strLastCategory.GetLength() == 0)
				strLastCategory = TRL("General");
			dwLastGroupId = pMgr->GetGroupId((LPCTSTR)strLastCategory);
			if(dwLastGroupId == DWORD_MAX)
			{
				PW_GROUP pwT;
				PW_TIME tNow;
				_GetCurrentPwTime(&tNow);
				memset(&pwT, 0, sizeof(PW_GROUP));
				pwT.pszGroupName = (TCHAR *)(LPCTSTR)strLastCategory;
				pwT.tCreation = tNow; CPwManager::GetNeverExpireTime(&pwT.tExpire);
				pwT.tLastAccess = tNow; pwT.tLastMod = tNow;
				pwT.uGroupId = 0; // 0 = create new group ID
				pwT.uImageId = _GetPreferredIcon((LPCTSTR)strLastCategory);
				pMgr->AddGroup(&pwT);
				dwLastGroupId = pMgr->GetGroupId((LPCTSTR)strLastCategory);
			}
			ASSERT(dwLastGroupId != DWORD_MAX);
		}

		if((str.Left(6) == _T("Notes:")) && (bInNotes == FALSE))
		{
			bInNotes = TRUE;
			str = str.Right(str.GetLength() - 6);
			if(str.GetLength() != 0) strNotes = str;
			continue;
		}

		b=0;
		if(str.Left(9) == _T("User ID: ")) {strUserName = str.Right(str.GetLength() - 9); b=1;}
		if(str.Left(7) == _T("Login: ")) {strUserName = str.Right(str.GetLength() - 7); b=1;}
		if(str.Left(10) == _T("Access #: ")) {strUserName = str.Right(str.GetLength() - 10); b=1;}
		if(str.Left(8) == _T("System: ")) {strUserName = str.Right(str.GetLength() - 8); b=1;}
		if(str.Left(9) == _T("Content: ")) {strUserName = str.Right(str.GetLength() - 9); b=1;}
		if(strUserName.GetLength() == 0)
		{
			if(str.Left(6) == _T("Date: ")) {strUserName = str.Right(str.GetLength() - 6); b=1;}
			if(str.Left(8) == _T("Issuer: ")) {strUserName = str.Right(str.GetLength() - 8); b=1;}
			if(str.Left(8) == _T("Number: ")) {strUserName = str.Right(str.GetLength() - 8); b=1;}
			if(str.Left(9) == _T("Network: ")) {strUserName = str.Right(str.GetLength() - 9); b=1;}
			if(str.Left(11) == _T("Ftp login: ")) {strUserName = str.Right(str.GetLength() - 11); b=1;}
		}

		if(str.Left(5) == _T("URL: ")) {strURL = str.Right(str.GetLength() - 5); b=1;}
		if(str.Left(10) == _T("Web site: ")) {strURL = str.Right(str.GetLength() - 10); b=1;}
		if(strURL.GetLength() == 0)
		{
			if(str.Left(19) == _T("Registered e-mail: ")) {strURL = str.Right(str.GetLength() - 19); b=1;}
		}

		if(str.Left(10) == _T("Password: ")) {strPassword = str.Right(str.GetLength() - 10); b=1;}
		if(strPassword.GetLength() == 0)
		{
			if(str.Left(5) == _T("PIN: ")) {strPassword = str.Right(str.GetLength() - 5); b=1;}
		}

		if((b == 0) && (bInNotes == FALSE))
		{
			if(strNotes.GetLength() != 0) strNotes += _T("\r\n");
			strNotes += str;
		}
	}

	SAFE_DELETE_ARRAY(pData);

	return TRUE;
}

BOOL CPwImport::ImportPVaultToDb(const TCHAR *pszFile, CPwManager *pMgr)
{
	char *pData;
	CString strTitle, strURL, strUserName, strPassword, strNotes;
	DWORD uFileSize, i, b;
	CString str;
	CString strLastCategory = _T("General");
	DWORD dwLastGroupId = 0;
	BOOL bInNotes = FALSE;

	ASSERT(pMgr != NULL);

	pData = CPwImport::FileToMemory(pszFile, &uFileSize);
	if(pData == NULL) return FALSE;

	strTitle.Empty(); strURL.Empty(); strUserName.Empty();
	strPassword.Empty(); strNotes.Empty();

	i = DWORD_MAX;

	if(uFileSize > 3)
		if((pData[0] == 0xEF) && (pData[1] == 0xBB) && (pData[2] == 0xBF))
			i += 3; // Skip UTF-8 initialization characters

	while(1) // Processing the file
	{
		str.Empty();

		while(1) // Loading one line to CString
		{
			i++;
			if(i >= uFileSize) break;

			if(pData[i] == '\n') break;
			if(pData[i] != '\r') str += pData[i];
		}

		// Add the entry
		if((str == DEF_PV_SEPENTRY) || (i >= uFileSize) || (str.Left(12) == DEF_PV_CATEGORY))
		{
			if((strTitle.IsEmpty() == FALSE) || (strUserName.IsEmpty() == FALSE) ||
				(strURL.IsEmpty() == FALSE) || (strPassword.IsEmpty() == FALSE))
			{
				strTitle.TrimLeft(); strTitle.TrimRight();
				strURL.TrimLeft(); strURL.TrimRight();
				strUserName.TrimLeft(); strUserName.TrimRight();
				strPassword.TrimLeft(); strPassword.TrimRight();
				strNotes.TrimLeft(); strNotes.TrimRight();

				PW_ENTRY pwTemplate;
				PW_TIME tNow;

				_GetCurrentPwTime(&tNow);
				memset(&pwTemplate, 0, sizeof(PW_ENTRY));
				pwTemplate.pszAdditional = (TCHAR *)(LPCTSTR)strNotes;
				pwTemplate.pszPassword = (TCHAR *)(LPCTSTR)strPassword;
				pwTemplate.pszTitle = (TCHAR *)(LPCTSTR)strTitle;
				pwTemplate.pszURL = (TCHAR *)(LPCTSTR)strURL;
				pwTemplate.pszUserName = (TCHAR *)(LPCTSTR)strUserName;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?