Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

KeePass Password Safe

, 6 Apr 2014
KeePass is a free, open-source, light-weight and easy-to-use password safe.
KeePass.zip
KeePass_Src.zip
Build
Docs
Ext
Icons
Finals
Finals2
plock-blu.ico
plock-dis.ico
plock.ico
plockb.ico
key-blu.png
key-bw.png
key-grn.ico
key-grn.png
plock-blu.ico
plock-blu.png
plock-bw.png
plock-dis.ico
plock-grn.ico
plock-grn.png
plock.ico
plock.png
plockb.png
plockbw-r.png
plockbw.png
Untitled.ico
key-org.ico
keyhole-grn.ico
lockblue.ico
lockgreen.ico
lockred.ico
lockyellow.ico
plockb.ico
plockb2.ico
plockbw.ico
plockg.ico
plocko.ico
plockr.ico
plocky.ico
KeePass.ini
KeePass.iss
KeePassMsi
KeePassMsi.vdproj
KeePassLibC
KeePassLibCpp
Crypto
SHA2
DataExchange
Details
IO
PasswordGenerator
SDK
Details
SysSpec_Windows
Util
KeePassAPI
KeePassLib.def
res
WinGUI
NewGUI
TaskbarListEx
TaskDialog
VistaMenu
Plugins
res
autotype.bmp
cancel.bmp
clienticex.bmp
clock.bmp
disk.bmp
document.bmp
entry_ed.ico
file.bmp
help_sma.bmp
help_smp.bmp
iconpic.ico
infoicon.bmp
infoiconex.bmp
key.ico
keyhole.ico
key_smal.bmp
language.bmp
locked.ico
lock_ovr.ico
MostPopularPasswords.bin
mouse_sm.bmp
ok.bmp
optionicex.bmp
options.ico
plugins.ico
PwSafe.ico
randomke.bmp
random_b.bmp
search.ico
tb_about.bmp
tb_adden.bmp
tb_copyp.bmp
tb_copyu.bmp
tb_defau.bmp
tb_delet.bmp
tb_edite.bmp
tb_find.bmp
tb_lock.bmp
tb_new.bmp
tb_open.bmp
tb_save.bmp
tb_save1.bmp
tb_saved.bmp
toolssma.bmp
unlocked.ico
winprops.bmp
world.ico
Util
CmdLine
SprEngine
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2014 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
*/

#ifndef ___PW_UTIL_H___
#define ___PW_UTIL_H___

#pragma once

#include "../SysDefEx.h"
#include <string>
#include <vector>
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>

#include "../PwManager.h"

#define PWU_CVT_EX PMD_HREL_ID
#define PWU_SPN_CONST 0x9AEAA5A3

typedef struct _PG_TREENODE
{
	PW_GROUP g;
	std::vector<boost::shared_ptr<_PG_TREENODE> > vChildNodes;
} PG_TREENODE;

#ifdef _UNICODE
#define ENCRYPTFILE_FNNAME "EncryptFileW"
#else
#define ENCRYPTFILE_FNNAME "EncryptFileA"
#endif
typedef BOOL(WINAPI *LPENCRYPTFILE)(LPCTSTR lpFileName);

template<typename T>
class CNullableEx : boost::noncopyable
{
public:
	CNullableEx() : m_bHasValue(false), m_tValue() { }
	CNullableEx(const T& t) : m_bHasValue(true), m_tValue(t) { }

	bool HasValue() const { return m_bHasValue; }
	const T& GetValue() const { return m_tValue; }
	T* GetValuePtr() { return &m_tValue; }

	void SetValue(const T& t) { m_bHasValue = true; m_tValue = t; }

private:
	bool m_bHasValue;
	T m_tValue;
};

class CPwUtil : boost::noncopyable
{
private:
	CPwUtil();

public:
	static BOOL LoadHexKey32(FILE *fp, BYTE *pBuf);
	static BOOL SaveHexKey32(FILE *fp, BYTE *pBuf);

	static LPCTSTR GetUniCvtPtr(LPCTSTR lpBase, BOOL bTranslate);
	static CString FormatError(int nErrorCode, DWORD dwFlags);

	static BOOL MemAllocCopyEntry(_In_ const PW_ENTRY *pExisting,
		_Out_ PW_ENTRY *pDestination);
	static void MemFreeEntry(_Inout_ PW_ENTRY *pEntry);

	// Convert PW_TIME to 5-byte compressed structure and the other way round
	static void TimeToPwTime(_In_bytecount_c_(5) const BYTE *pCompressedTime,
		_Out_ PW_TIME *pPwTime);
	static void PwTimeToTime(_In_ const PW_TIME *pPwTime,
		_Out_bytecap_c_(5) BYTE *pCompressedTime);

	// static void PwEntryStlToKpEntry(const PW_ENTRY_STL* pIn, KP_ENTRY* pOut,
	//	const PW_ENTRY* pOriginalEntry, const CPwManager* pMgr);
	// static void ClearPwEntryStl(PW_ENTRY_STL* p);

	static void FreeKpEntry(KP_ENTRY* p);

	// static bool EqualsPwEntryStl(const PW_ENTRY_STL* p1, const PW_ENTRY_STL* p2);

	static void HashKpEntry(const KP_ENTRY* pEntry, BYTE* pHash32);

	static BOOL AttachFileAsBinaryData(_Inout_ PW_ENTRY *pEntry,
		const TCHAR *lpFile);
	static BOOL SaveBinaryData(_In_ const PW_ENTRY *pEntry,
		const TCHAR *lpFile);
	static BOOL RemoveBinaryData(_Inout_ PW_ENTRY *pEntry);

	static BOOL IsAllowedStoreGroup(LPCTSTR lpGroupName, LPCTSTR lpSearchGroupName);

	static BOOL IsZeroUUID(_In_bytecount_c_(16) const BYTE *pUUID);
	static CString CreateUUIDStr(CNewRandom* pRandomSource);
	static DWORD GetUniCPT(LPCTSTR lp);

	static BOOL IsTANEntry(const PW_ENTRY *pe);

	// static void ProtectMemory(UINT8 *pMem, size_t uBytes, bool bEncrypt);

	static PG_TREENODE GroupsToTree(CPwManager* pMgr);
	static boost::shared_ptr<PG_TREENODE> FindGroupInTree(PG_TREENODE* pRoot,
		DWORD dwGroupId, bool bUnlinkGroup, int iMoveGroup);
	static void FlattenGroupTree(PW_GROUP* pStorage, PG_TREENODE* pRoot,
		DWORD dwStorageCount);

#ifdef _DEBUG
	static void CheckGroupList(CPwManager* pMgr);
#endif

	static std::basic_string<TCHAR> FormatSystemMessage(DWORD dwLastErrorCode);

	static bool UnhideFile(LPCTSTR lpFile);
	static bool HideFile(LPCTSTR lpFile, bool bHide);

	static CNullableEx<FILETIME> GetFileCreationTime(LPCTSTR lpFile);
	static bool SetFileCreationTime(LPCTSTR lpFile, const FILETIME* pTime);

	static bool EfsEncryptFile(LPCTSTR lpFile);

	static bool IsDatabaseFile(LPCTSTR lpFile);

private:
	inline static BOOL ConvertStrToHex(char ch1, char ch2, BYTE& bt);
	inline static void ConvertHexToStr(BYTE bt, char& ch1, char& ch2);

	static boost::shared_ptr<PG_TREENODE> GroupToTreeNode(CPwManager* pMgr,
		DWORD dwIndex, DWORD& dwAllocCount);
	static void FlattenGroupTreeInternal(PW_GROUP* pStorage, PG_TREENODE* pRoot,
		DWORD& dwIndex, DWORD dwStorageCount, USHORT usLevel);

	static void HashStringWithTerm(LPCTSTR lp, sha256_ctx& sha32);
};

/* class CPwErrorInfo
{
public:
	CPwErrorInfo();
	void SetInfo(const std::basic_string<TCHAR>& strText, int nPwErrorCode,
		BOOL bUseLastError);

	std::basic_string<TCHAR> ToString() const;

private:
	void FormatInfo();

	std::basic_string<TCHAR> m_strText;
	int m_nPwErrorCode;
	DWORD m_dwLastError;

	std::basic_string<TCHAR> m_strFinal;
}; */

#endif // ___PW_UTIL_H___

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dominik Reichl
Software Developer
Germany Germany
Dominik started programming in Omikron Basic, a programming language for the good old Atari ST. After this, there was some short period of QBasic programming on the PC, but soon he began learning C++, which is his favorite language up to now.
 
Today, his programming experience includes C / C++ / [Visual] C++ [MFC], C#/.NET, Java, JavaScript, PHP and HTML and the basics of pure assembler.
 
He is interested in almost everything that has to do with computing, his special interests are security and data compression.
 
You can find his latest freeware, open-source projects and all articles on his homepage: http://www.dominik-reichl.de/

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 6 Apr 2014
Article Copyright 2003 by Dominik Reichl
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid