Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[d6386d]: pwsafe / pwsafe / corelib / PWSfile.h Maximize Restore History

Download this file

PWSfile.h    128 lines (115 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2003-2007 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license.php
*/
#ifndef __PWSFILE_H
#define __PWSFILE_H
// PWSfile.h
// Abstract the gory details of reading and writing an encrypted database
//-----------------------------------------------------------------------------
#include <stdio.h> // for FILE *
#include "PwsPlatform.h"
#include "ItemData.h"
#include "MyString.h"
#include "UUIDGen.h"
#include "UnknownField.h"
#define MIN_HASH_ITERATIONS 2048
class Fish;
class PWSfile {
public:
enum VERSION {V17, V20, V30, VCURRENT = V30,
UNKNOWN_VERSION = 99}; // supported file versions: V17 is last pre-2.0
enum RWmode {Read, Write};
enum {SUCCESS = 0, FAILURE = 1,
CANT_OPEN_FILE, // 2
UNSUPPORTED_VERSION, // 3
WRONG_VERSION, // 4
NOT_PWS3_FILE, // 5
WRONG_PASSWORD, // 6 - see PWScore.h
BAD_DIGEST, // 7 - see PWScore.h
END_OF_FILE // 8
};
static PWSfile *MakePWSfile(const CMyString &a_filename, VERSION &version,
RWmode mode, int &status);
static bool FileExists(const CMyString &filename);
static bool FileExists(const CMyString &filename, bool &bReadOnly);
static void FileError(int formatRes, int cause);
static VERSION ReadVersion(const CMyString &filename);
static int RenameFile(const CMyString &oldname, const CMyString &newname);
static int CheckPassword(const CMyString &filename,
const CMyString &passkey, VERSION &version);
static bool LockFile(const CMyString &filename, CMyString &locker,
HANDLE &lockFileHandle, int &LockCount);
static bool IsLockedFile(const CMyString &filename);
static void UnlockFile(const CMyString &filename,
HANDLE &lockFileHandle, int &LockCount);
static bool GetLocker(const CMyString &filename, CMyString &locker);
virtual ~PWSfile();
virtual int Open(const CMyString &passkey) = 0;
virtual int Close();
virtual int WriteRecord(const CItemData &item) = 0;
virtual int ReadRecord(CItemData &item) = 0;
void SetDefUsername(const CMyString &du) {m_defusername = du;} // for V17 conversion (read) only
void SetFileUUID(const uuid_array_t &file_uuid_array);
void SetFileHashIterations(const int &nITER)
{m_nITER = nITER;}
void GetFileUUID(uuid_array_t &file_uuid_array);
int GetFileHashIterations()
{return m_nITER;}
// The prefstring is read/written along with the rest of the file,
// see code for details on where it's kept.
void SetPrefString(const CMyString &prefStr) {m_prefString = prefStr;}
const CMyString &GetPrefString() const {return m_prefString;}
void SetDisplayStatus(const CString &displaystatus) {m_file_displaystatus = displaystatus;}
const CString &GetDisplayStatus() const {return m_file_displaystatus;}
void SetUseUTF8(bool flag) { m_useUTF8 = flag; } // nop for v1v2
void SetUserHost(const CString &user, const CString &sysname)
{m_user = user; m_sysname = sysname;}
void SetApplicationVersion(const DWORD dwMajorMinor)
{m_dwMajorMinor = dwMajorMinor;}
const CString &GetWhenLastSaved() const {return m_whenlastsaved;}
const CString &GetWhoLastSaved() const {return m_wholastsaved;}
const CString &GetWhatLastSaved() const {return m_whatlastsaved;}
unsigned short GetCurrentMajorVersion() const {return m_nCurrentMajorVersion;}
unsigned short GetCurrentMinorVersion() const {return m_nCurrentMinorVersion;}
void SetCurVersion(VERSION v) {m_curversion = v;}
void GetUnknownHeaderFields(UnknownFieldList &UHFL);
void SetUnknownHeaderFields(UnknownFieldList &UHFL);
int GetNumRecordsWithUnknownFields()
{return m_nRecordsWithUnknownFields;}
protected:
PWSfile(const CMyString &filename, RWmode mode);
void FOpen(); // calls right variant of m_fd = fopen(m_filename);
virtual size_t WriteCBC(unsigned char type, const CString &data) = 0;
virtual size_t WriteCBC(unsigned char type, const unsigned char *data,
unsigned int length);
virtual size_t ReadCBC(unsigned char &type, unsigned char* &data,
unsigned int &length);
const CMyString m_filename;
CMyString m_passkey;
FILE *m_fd;
VERSION m_curversion;
unsigned short m_nCurrentMajorVersion, m_nCurrentMinorVersion;
const RWmode m_rw;
CMyString m_defusername; // for V17 conversion (read) only
CMyString m_prefString; // prefererences stored in the file
CString m_file_displaystatus; // tree display sttaus stored in file
CString m_whenlastsaved; // When last saved
CString m_wholastsaved; // and by whom
CString m_whatlastsaved; // and by what
CString m_user, m_sysname; // current user & host
DWORD m_dwMajorMinor;
unsigned char *m_IV; // points to correct m_ipthing for *CBC()
Fish *m_fish;
unsigned char *m_terminal;
bool m_useUTF8; // turn off for none-unicode os's, e.g. win98
uuid_array_t m_file_uuid_array;
static int m_nITER;
// Save unknown header fields on read to put back on write unchanged
UnknownFieldList m_UHFL;
int m_nRecordsWithUnknownFields;
};
#endif /* __PWSFILE_H */