C++ API changes+fixes:

- context is now a pointer to make sure there's only one all the time
- fixed a couple of double free's
- some minor bug fixes
This commit is contained in:
git@daemon.de
2014-08-01 14:46:38 +02:00
parent 1e4e65b811
commit e6a5c51d8a
13 changed files with 168 additions and 114 deletions

View File

@@ -26,29 +26,31 @@ using namespace pcp;
Vault::Vault() {
V = NULL;
PTX = NULL;
}
Vault::Vault(PcpContext &P) {
Vault::Vault(PcpContext *P) {
V = NULL;
PTX = P;
}
Vault::Vault(PcpContext &P, string filename) {
Vault::Vault(PcpContext *P, string filename) {
PTX = P;
V = pcpvault_init(PTX.ptx, (char *)filename.c_str());
V = pcpvault_init(PTX->ptx, (char *)filename.c_str());
if (V == NULL)
throw pcp::exception(PTX);
}
Vault::~Vault() {
pcpvault_close(PTX.ptx, V);
if(V && PTX)
pcpvault_close(PTX->ptx, V);
}
std::map<std::string, Key> Vault::keys() {
std::map<std::string, Key> kmap;
pcp_key_t *k = NULL;
pcphash_iterate(PTX.ptx, k) {
pcphash_iterate(PTX->ptx, k) {
kmap.insert ( pair<string,Key>(string(k->id), Key(PTX, k, true)) );
}
@@ -59,7 +61,7 @@ std::map<std::string, PubKey> Vault::pubkeys() {
std::map<std::string, PubKey> kmap;
pcp_pubkey_t *k = NULL;
pcphash_iteratepub(PTX.ptx, k) {
pcphash_iteratepub(PTX->ptx, k) {
kmap.insert ( pair<string,PubKey>(string(k->id), PubKey(PTX, k, true)) );
}
@@ -67,31 +69,31 @@ std::map<std::string, PubKey> Vault::pubkeys() {
}
int Vault::key_count() {
return pcphash_count(PTX.ptx);
return pcphash_count(PTX->ptx);
}
int Vault::pubkey_count() {
return pcphash_countpub(PTX.ptx);
return pcphash_countpub(PTX->ptx);
}
void Vault::key_add(Key &key) {
if(V->isnew == 1 || pcphash_count(PTX.ptx) == 0) {
if(V->isnew == 1 || pcphash_count(PTX->ptx) == 0) {
key.K->type = PCP_KEY_TYPE_MAINSECRET;
}
if(pcpvault_addkey(PTX.ptx, V, (void *)key.K, key.K->type) != 0)
if(pcpvault_addkey(PTX->ptx, V, (void *)key.K, key.K->type) != 0)
throw pcp::exception(PTX);
key.is_stored(true);
}
void Vault::pubkey_add(PubKey &key) {
if(pcpvault_addkey(PTX.ptx, V, (void *)key.K, key.K->type) != 0)
if(pcpvault_addkey(PTX->ptx, V, (void *)key.K, key.K->type) != 0)
throw pcp::exception(PTX);
key.is_stored(true);
}
bool Vault::key_exists(string &id) {
pcp_key_t *s = pcphash_keyexists(PTX.ptx, (char *)id.c_str());
pcp_key_t *s = pcphash_keyexists(PTX->ptx, (char *)id.c_str());
if(s == NULL)
return false;
else
@@ -99,7 +101,7 @@ bool Vault::key_exists(string &id) {
}
bool Vault::pubkey_exists(string &id) {
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX.ptx, (char *)id.c_str());
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX->ptx, (char *)id.c_str());
if(p == NULL)
return false;
else
@@ -107,19 +109,19 @@ bool Vault::pubkey_exists(string &id) {
}
void Vault::key_delete(std::string &id) {
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX.ptx, (char *)id.c_str());
pcp_pubkey_t *p = pcphash_pubkeyexists(PTX->ptx, (char *)id.c_str());
if(p != NULL) {
// delete public
pcphash_del(PTX.ptx, p, p->type);
pcphash_del(PTX->ptx, p, p->type);
free(p);
V->unsafed = 1;
}
else {
pcp_key_t *s = pcphash_keyexists(PTX.ptx, (char *)id.c_str());
pcp_key_t *s = pcphash_keyexists(PTX->ptx, (char *)id.c_str());
if(s != NULL) {
// delete secret
pcphash_del(PTX.ptx, s, s->type);
pcphash_del(PTX->ptx, s, s->type);
free(s);
V->unsafed = 1;
}
@@ -131,14 +133,14 @@ void Vault::key_delete(std::string &id) {
Key Vault::get_primary() {
pcp_key_t *k = NULL;
pcphash_iterate(PTX.ptx, k) {
pcphash_iterate(PTX->ptx, k) {
if(k->type == PCP_KEY_TYPE_MAINSECRET) {
return Key(PTX, k);
}
}
if(Vault::key_count() == 1) {
pcphash_iterate(PTX.ptx, k) {
pcphash_iterate(PTX->ptx, k) {
return Key(PTX, k);
}
}
@@ -149,7 +151,7 @@ Key Vault::get_primary() {
Key Vault::get_secret(std::string &id) {
pcp_key_t *k = NULL;
pcphash_iterate(PTX.ptx, k) {
pcphash_iterate(PTX->ptx, k) {
if(memcmp(k->id, id.c_str(), 16) == 0) {
return Key(PTX, k);
}
@@ -160,7 +162,7 @@ Key Vault::get_secret(std::string &id) {
PubKey Vault::get_public(std::string &id) {
pcp_pubkey_t *k = NULL;
pcphash_iteratepub(PTX.ptx, k) {
pcphash_iteratepub(PTX->ptx, k) {
if(memcmp(k->id, id.c_str(), 16) == 0) {
return PubKey(PTX, k);
}