changed data type of group and entry list from vector (STL) to QList (Qt)

git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@19 b624d157-de02-0410-bad0-e51aec6abb33
master
tariq 19 years ago
parent 16060b08e4
commit 3c4ac74af1
  1. 10
      src/Database.cpp
  2. 11
      src/Database.h
  3. 113
      src/PwManager.cpp
  4. 12
      src/PwManager.h
  5. 28
      src/lib/GroupView.cpp
  6. 12
      src/lib/SecString.cpp
  7. 4
      src/lib/SecString.h
  8. 1
      src/mainwindow.cpp

@ -34,6 +34,16 @@ pBinaryData=NULL;
bool CGroup::UI_ExpandByDefault=true; bool CGroup::UI_ExpandByDefault=true;
bool CEntry::operator==(const CEntry& e)const{
if(sID==e.sID)return true;
else return false;
}
bool CGroup::operator==(const CGroup& g)const{
if(ID==g.ID)return true;
else return false;
}
CGroup::CGroup(){ CGroup::CGroup(){
Creation=QDateTime::currentDateTime(); Creation=QDateTime::currentDateTime();
LastAccess=QDateTime::currentDateTime(); LastAccess=QDateTime::currentDateTime();

@ -20,7 +20,7 @@
#ifndef _DATABASE_H_ #ifndef _DATABASE_H_
#define _DATABASE_H_ #define _DATABASE_H_
#include <vector.h> #include <QList>
#include <QDateTime> #include <QDateTime>
#include "lib/SecString.h" #include "lib/SecString.h"
using namespace std; using namespace std;
@ -47,6 +47,7 @@ Q_UINT8 *pBinaryData;
Q_UINT32 BinaryDataLength; Q_UINT32 BinaryDataLength;
Q_UINT32 PasswordLength; Q_UINT32 PasswordLength;
bool ReadEntryField(Q_UINT16 FieldType, Q_UINT32 FieldSize, Q_UINT8 *pData); bool ReadEntryField(Q_UINT16 FieldType, Q_UINT32 FieldSize, Q_UINT8 *pData);
bool operator==(const CEntry&) const;
}; };
@ -65,6 +66,7 @@ QDateTime Expire;
Q_UINT16 Level; Q_UINT16 Level;
Q_UINT32 Flags; Q_UINT32 Flags;
bool ReadGroupField(Q_UINT16 FieldType, Q_UINT32 FieldSize, Q_UINT8 *pData); bool ReadGroupField(Q_UINT16 FieldType, Q_UINT32 FieldSize, Q_UINT8 *pData);
bool operator==(const CGroup&) const;
bool UI_ItemIsExpanded; bool UI_ItemIsExpanded;
static bool UI_ExpandByDefault; static bool UI_ExpandByDefault;
@ -72,9 +74,6 @@ static bool UI_ExpandByDefault;
}; };
typedef vector<CEntry>::iterator EntryItr;
typedef vector<CGroup>::iterator GroupItr;
class Database{ class Database{
public: public:
@ -83,8 +82,8 @@ public:
QString filename; QString filename;
bool modflag; bool modflag;
int SearchGroupID; int SearchGroupID;
vector<CGroup>Groups; QList<CGroup>Groups;
vector<CEntry>Entries; QList<CEntry>Entries;
protected: protected:
Q_UINT8 MasterKey[32]; Q_UINT8 MasterKey[32];

@ -140,16 +140,13 @@ err=trUtf8("Hash-Test fehlgeschlage: der Schlüssl ist falsch oder die Datei ist
return false;} return false;}
Groups.resize(NumGroups);
Entries.resize(NumEntries);
unsigned long tmp_id=0; unsigned long tmp_id=0;
unsigned long pos = DB_HEADER_SIZE; unsigned long pos = DB_HEADER_SIZE;
Q_UINT16 FieldType; Q_UINT16 FieldType;
Q_UINT32 FieldSize; Q_UINT32 FieldSize;
char* pField; char* pField;
bool bRet; bool bRet;
CGroup group;
for(unsigned long CurGroup = 0; CurGroup < NumGroups; ) for(unsigned long CurGroup = 0; CurGroup < NumGroups; )
{ {
@ -165,8 +162,9 @@ bool bRet;
if(pos >= (total_size + FieldSize)) { if(pos >= (total_size + FieldSize)) {
return false;} return false;}
bRet = Groups[CurGroup].ReadGroupField(FieldType, FieldSize, (Q_UINT8 *)pField); bRet = group.ReadGroupField(FieldType, FieldSize, (Q_UINT8 *)pField);
if((FieldType == 0xFFFF) && (bRet == true)){ if((FieldType == 0xFFFF) && (bRet == true)){
Groups << group;
CurGroup++;} // Now and ONLY now the counter gets increased CurGroup++;} // Now and ONLY now the counter gets increased
pField += FieldSize; pField += FieldSize;
@ -174,6 +172,7 @@ bool bRet;
if(pos >= total_size) { return false;} if(pos >= total_size) { return false;}
} }
CEntry entry;
for(unsigned long CurEntry = 0; CurEntry < NumEntries;) for(unsigned long CurEntry = 0; CurEntry < NumEntries;)
{ {
@ -189,9 +188,10 @@ bool bRet;
if(pos >= (total_size + FieldSize)) { if(pos >= (total_size + FieldSize)) {
return false; } return false; }
bRet = Entries[CurEntry].ReadEntryField(FieldType,FieldSize,(Q_UINT8*)pField); bRet = entry.ReadEntryField(FieldType,FieldSize,(Q_UINT8*)pField);
if((FieldType == 0xFFFF) && (bRet == true)){ if((FieldType == 0xFFFF) && (bRet == true)){
Entries[CurEntry].sID=tmp_id++; entry.sID=tmp_id++;
Entries << entry;
CurEntry++;} // Now and ONLY now the counter gets increased CurEntry++;} // Now and ONLY now the counter gets increased
pField += FieldSize; pField += FieldSize;
@ -203,10 +203,10 @@ bool bRet;
unsigned long CurGID, g, e, z, num; unsigned long CurGID, g, e, z, num;
delete [] buffer; delete [] buffer;
for(vector<CEntry>::iterator i=Entries.begin();i!=Entries.end();i++){ for(int i=0;i<Entries.size();i++){
if(IsMetaStream(*i)==true){ if(IsMetaStream(Entries[i])==true){
///@TODO Parse Metastreams ///@TODO Parse Metastreams
deleteEntry(i); deleteEntry(&Entries[i]);
i--; i--;
} }
} }
@ -387,13 +387,8 @@ delete[] PasswordKey;
delete[] FileKey; delete[] FileKey;
} }
void PwDatabase::deleteEntry(CEntry* entry){
vector<CEntry>::iterator PwDatabase::deleteEntry(vector<CEntry>::iterator iterator){ Entries.removeAt(Entries.indexOf(*entry));
return Entries.erase(iterator);
}
vector<CEntry>::iterator PwDatabase::deleteEntry(CEntry* entry){
return deleteEntry(getEntryIterator(entry));
} }
bool PwDatabase::IsMetaStream(CEntry& p){ bool PwDatabase::IsMetaStream(CEntry& p){
@ -559,6 +554,7 @@ Q_UINT8 FinalRandomSeed[16];
Q_UINT8 ContentsHash[32]; Q_UINT8 ContentsHash[32];
Q_UINT8 EncryptionIV[16]; Q_UINT8 EncryptionIV[16];
/*
if(SearchGroupID!=-1){ if(SearchGroupID!=-1){
for(int i=0;i<Groups.size();i++){ for(int i=0;i<Groups.size();i++){
if(Groups[i].ID==SearchGroupID){ if(Groups[i].ID==SearchGroupID){
@ -566,6 +562,8 @@ if(SearchGroupID!=-1){
Groups.erase(getGroupIterator(&Groups[i]));} Groups.erase(getGroupIterator(&Groups[i]));}
} }
} }
*/
if(filename==QString::null)return false; if(filename==QString::null)return false;
QFile file(filename); QFile file(filename);
unsigned int FileSize; unsigned int FileSize;
@ -815,16 +813,6 @@ if(SearchGroupID!=-1)Groups.push_back(SearchGroup);
return true; return true;
} }
GroupItr PwDatabase::getGroupIterator(CGroup* pGroup){
//for(vector<CGroup>::iterator i=Groups.begin();i!=Groups.end();i++){
//if((*i).ID==pGroup->ID)return i;}
return Groups.begin()+(pGroup-&Groups[0]);
}
EntryItr PwDatabase::getEntryIterator(CEntry* pEntry){
return Entries.begin()+(pEntry-&Entries[0]);
}
CGroup* PwDatabase::addGroup(CGroup* parent){ CGroup* PwDatabase::addGroup(CGroup* parent){
CGroup group; CGroup group;
@ -833,41 +821,51 @@ group.ImageID=0;
group.ID=getNewGroupId(); group.ID=getNewGroupId();
if(!Groups.size() || !parent){ if(!Groups.size() || !parent){
Groups.push_back(group); Groups << group;
return &Groups.back(); return &Groups.back();
} }
else { GroupItr groupIt; else { int i=Groups.indexOf(*parent)+1;
groupIt=Groups.insert(getGroupIterator(parent)+1,group); Groups.insert(i,group);
return &(*groupIt);} return &Groups[i];}
} }
GroupItr PwDatabase::deleteGroup(unsigned long id){ void PwDatabase::deleteGroup(unsigned long id){
for(int i=0;i<Groups.size();i++){ for(int i=0;i<Groups.size();i++){
if(Groups[i].ID==id) return deleteGroup(&Groups[i]); if(Groups[i].ID==id) return deleteGroup(&Groups[i]);
} }
} }
GroupItr PwDatabase::deleteGroup(CGroup* group){ void PwDatabase::deleteGroup(CGroup* group){
GroupItr first=getGroupIterator(group); int GroupIndex=Groups.indexOf(*group);
GroupItr last=Groups.end(); int NumChilds;
for(GroupItr i=getGroupIterator(group)+1; i!=Groups.end();i++){
if((*i).Level<=group->Level){
last=i;
break;}
}
for(GroupItr g=first;g!=last;g++){ int i;
for(EntryItr e=Entries.begin();e!=Entries.end();){ for(i=GroupIndex+1; i<Groups.size(); i++){
if((*g).ID==(*e).GroupID)e=deleteEntry(e); if(Groups[i].Level<=group->Level)break;
else e++; }
NumChilds=i-GroupIndex-1;
qDebug("NUMCHILDS=%i\n",NumChilds);
//delete entries
for(i=GroupIndex; i<=GroupIndex+NumChilds; i++){
for(int j=0; j<Entries.size();){
if(Entries[j].GroupID==Groups[i].ID)
deleteEntry(&Entries[j]);
else
j++;
} }
} }
return Groups.erase(first,last); for(i=NumChilds; i>=0; i--){
Groups.removeAt(GroupIndex+i);
}
} }
int PwDatabase::getGroupIndex(CGroup* g){
return getGroupIndex(g->ID);
}
int PwDatabase::getGroupIndex(unsigned long ID){ int PwDatabase::getGroupIndex(unsigned long ID){
@ -959,6 +957,31 @@ for(int i=0; i<64; i+=2){
} }
} }
void PwDatabase::moveGroup(CGroup* group, CGroup* DstGroup){
/*
int NumSubGroups=0;
int GroupIndex=getGroupIndex(group);
int i;
for(i=GroupIndex+1; i<groups.size(); i++){
if(groups[i].Level <= group->Level) break;
}
NumSubGroups=i-GroupIndex-1;
int LevelDiff;
if(DstGroup)
LevelDiff=DstGroup->Level - group->Level;
else
LevelDiff=-group->Level;
if(DstGroup){
groups.insert(pos, groups[
}
*/
}
void memcpyFromLEnd32(Q_UINT32* dst,char* src){ void memcpyFromLEnd32(Q_UINT32* dst,char* src){
if(QSysInfo::ByteOrder==QSysInfo::BigEndian){ if(QSysInfo::ByteOrder==QSysInfo::BigEndian){

@ -55,10 +55,13 @@ public:
bool CalcMasterKeyByFileAndPw(QString filename, QString& password); bool CalcMasterKeyByFileAndPw(QString filename, QString& password);
CGroup* addGroup(CGroup* parent); CGroup* addGroup(CGroup* parent);
GroupItr deleteGroup(CGroup* pGroup); void deleteGroup(CGroup* pGroup);
GroupItr deleteGroup(unsigned long ID); void deleteGroup(unsigned long ID);
void moveGroup(CGroup* group, CGroup* DstGroup);
int getGroupIndex(CGroup* group);
int getGroupIndex(unsigned long ID); int getGroupIndex(unsigned long ID);
EntryItr deleteEntry(CEntry* pEntry);
void deleteEntry(CEntry* pEntry);
void moveEntry(CEntry* pEntry,CGroup* pDstGroup); void moveEntry(CEntry* pEntry,CGroup* pDstGroup);
CEntry* addEntry(); CEntry* addEntry();
void merge(PwDatabase* db2); void merge(PwDatabase* db2);
@ -69,9 +72,6 @@ public:
private: private:
EntryItr deleteEntry(vector<CEntry>::iterator i);
EntryItr getEntryIterator(CEntry* pEntry);
GroupItr getGroupIterator(CGroup* pGroup);
bool IsMetaStream(CEntry& Entry); bool IsMetaStream(CEntry& Entry);
void transformKey(Q_UINT8* src,Q_UINT8* dst,Q_UINT8* seed,int rounds); void transformKey(Q_UINT8* src,Q_UINT8* dst,Q_UINT8* seed,int rounds);
bool readHeader(char* raw); bool readHeader(char* raw);

@ -127,29 +127,29 @@ void KeepassGroupView::mouseMoveEvent(QMouseEvent *event){
void KeepassGroupView::updateItems(){ void KeepassGroupView::updateItems(){
clear(); clear();
Items.clear(); Items.clear();
for(GroupItr i=db->Groups.begin();i!=db->Groups.end();i++){ for(int i=0; i<db->Groups.size();i++){
if((*i).Level==0){ if(db->Groups[i].Level==0){
if(Items.size()) Items.push_back(new GroupViewItem(this,getLastSameLevelItem(0))); if(Items.size()) Items.push_back(new GroupViewItem(this,getLastSameLevelItem(0)));
else Items.push_back(new GroupViewItem(this)); else Items.push_back(new GroupViewItem(this));
Items.back()->setText(0,(*i).Name); Items.back()->setText(0,db->Groups[i].Name);
Items.back()->pGroup=&(*i); Items.back()->pGroup=&db->Groups[i];
} }
else{ else{
if((*i).Level>(*(i-1)).Level){ if(db->Groups[i].Level>db->Groups[i-1].Level){
Items.push_back(new GroupViewItem(Items.back(),getLastSameLevelItem((*i).Level))); Items.push_back(new GroupViewItem(Items.back(),getLastSameLevelItem(db->Groups[i].Level)));
Items.back()->setText(0,(*i).Name); Items.back()->setText(0,db->Groups[i].Name);
Items.back()->pGroup=&(*i); Items.back()->pGroup=&db->Groups[i];
} }
if((*i).Level<=(*(i-1)).Level){ if(db->Groups[i].Level<=db->Groups[i-1].Level){
GroupItemItr j; GroupItemItr j;
for(j=Items.end()-1;j!=Items.begin();j--){ for(j=Items.end()-1;j!=Items.begin();j--){
if((*j)->pGroup->Level<(*i).Level)break;} if((*j)->pGroup->Level<db->Groups[i].Level)break;}
Items.push_back(new GroupViewItem((*j),getLastSameLevelItem((*i).Level))); Items.push_back(new GroupViewItem((*j),getLastSameLevelItem(db->Groups[i].Level)));
Items.back()->setText(0,(*i).Name); Items.back()->setText(0,db->Groups[i].Name);
Items.back()->pGroup=&(*i); Items.back()->pGroup=&db->Groups[i];
} }
} }
Items.back()->setIcon(0,EntryIcons[(*i).ImageID]); Items.back()->setIcon(0,EntryIcons[db->Groups[i].ImageID]);
} }
for(int i=0;i<Items.size();i++){ for(int i=0;i<Items.size();i++){

@ -26,25 +26,22 @@ using namespace std;
Q_UINT8 SecString::Key[32]={0}; Q_UINT8 SecString::Key[32]={0};
SecString::SecString(){ SecString::SecString(){
data=NULL;
len=0; len=0;
cryptlen=0;
} }
SecString::~SecString(){ SecString::~SecString(){
//if(data)delete [] data; ///@FIXME zerschießt den Stack, aber warum???
overwrite(plaintext); overwrite(plaintext);
} }
void SecString::getString(QString & str){ void SecString::getString(QString & str){
if(data){ if(data.size()){
Rijndael aes; Rijndael aes;
int r=aes.init(Rijndael::CBC, Rijndael::Decrypt,Key,Rijndael::Key32Bytes); int r=aes.init(Rijndael::CBC, Rijndael::Decrypt,Key,Rijndael::Key32Bytes);
if(r){ cout << "AES error, code " << r << endl; if(r){ cout << "AES error, code " << r << endl;
exit(-1);} exit(-1);}
char* out=new char[len]; char* out=new char[len];
r=aes.padDecrypt((unsigned char*)data,cryptlen,(unsigned char*)out); r=aes.padDecrypt((unsigned char*)data.data(),data.size(),(unsigned char*)out);
if(r!=len){ cout << "AES error in SecString::getString(), r!=length, r=" << r << endl; if(r!=len){ cout << "AES error in SecString::getString(), r!=length, r=" << r << endl;
exit(-1);} exit(-1);}
str=QString::fromUtf8(out,len); str=QString::fromUtf8(out,len);
@ -74,10 +71,8 @@ memcpy(input,str.utf8(),il);
r=aes.padEncrypt((unsigned char*)input,il,(unsigned char*)output); r=aes.padEncrypt((unsigned char*)input,il,(unsigned char*)output);
if(r<0){ cout << "AES error, code " << r << endl; if(r<0){ cout << "AES error, code " << r << endl;
exit(-1);} exit(-1);}
cryptlen=r;
len=il; len=il;
if(data)delete [] data; data=QByteArray(output,r);
data=output;
overwrite(input,il); overwrite(input,il);
delete [] input; delete [] input;
if(DelSrc)overwrite(str); if(DelSrc)overwrite(str);
@ -103,3 +98,4 @@ return len;
void SecString::generateSessionKey(){ void SecString::generateSessionKey(){
getRandomBytes(Key,32,1,false); getRandomBytes(Key,32,1,false);
} }

@ -20,6 +20,7 @@
#ifndef _SECSTRING_H_ #ifndef _SECSTRING_H_
#define _SECSTRING_H_ #define _SECSTRING_H_
#include <QByteArray>
#include <qstring.h> #include <qstring.h>
#include <qglobal.h> #include <qglobal.h>
#include "crypto/rijndael.h" #include "crypto/rijndael.h"
@ -42,9 +43,8 @@ public:
private: private:
static Q_UINT8 Key[32]; static Q_UINT8 Key[32];
QString plaintext; QString plaintext;
char* data; QByteArray data;
int len; int len;
int cryptlen;
}; };

@ -412,4 +412,5 @@ Q_ASSERT(GroupView->selectedItems().size());
CGroup *pGroup=static_cast<GroupViewItem*>(GroupView->selectedItems()[0])->pGroup; CGroup *pGroup=static_cast<GroupViewItem*>(GroupView->selectedItems()[0])->pGroup;
db->deleteGroup(pGroup); db->deleteGroup(pGroup);
GroupView->updateItems(); GroupView->updateItems();
setStateFileModified(true);
} }