diff -r 98d0e2516f4e -r 48f43130b4a2 libidav/pwdstore.c --- a/libidav/pwdstore.c Mon Oct 28 07:37:45 2024 +0100 +++ b/libidav/pwdstore.c Mon Oct 28 15:20:58 2024 +0100 @@ -109,6 +109,61 @@ return p; } +PwdStore* pwdstore_clone(PwdStore *p) { + CxBuffer *newbuffer = calloc(1, sizeof(CxBuffer)); + *newbuffer = *p->content; + newbuffer->space = malloc(p->content->capacity); + memcpy(newbuffer->space, p->content->space, p->content->capacity); + + DavKey *key = NULL; + if(p->key) { + key = malloc(sizeof(DavKey)); + key->data = malloc(p->key->length); + memcpy(key->data, p->key->data, p->key->length); + key->length = p->key->length; + key->type = p->key->type; + key->name = NULL; + } + + PwdStore *newp = calloc(1, sizeof(PwdStore)); + newp->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); + newp->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); + newp->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS); + newp->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); + newp->content = newbuffer; + newp->key = key; + newp->unlock_cmd = p->unlock_cmd ? strdup(p->unlock_cmd) : NULL; + newp->lock_cmd = p->lock_cmd ? strdup(p->lock_cmd) : NULL; + newp->encoffset = p->encoffset; + newp->isdecrypted = p->isdecrypted; + + CxIterator i = cxMapIterator(p->ids); + cx_foreach(CxMapEntry *, e, i) { + PwdEntry *entry = e->value; + PwdEntry *new_entry = malloc(sizeof(PwdEntry)); + new_entry->id = strdup(entry->id); + new_entry->user = entry->user ? strdup(entry->user) : NULL; + new_entry->password = entry->password ? strdup(entry->password) : NULL; + cxMapPut(newp->ids, *e->key, new_entry); + } + + i = cxMapIterator(p->index); + cx_foreach(CxMapEntry *, e, i) { + PwdIndexEntry *entry = e->value; + CxList *locations = NULL; + if(entry->locations) { + locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); + CxIterator li = cxListIterator(entry->locations); + cx_foreach(char *, location, li) { + cxListAdd(locations, strdup(location)); + } + } + pwdstore_put_index(newp, entry->id, locations); + } + + return newp; +} + static int readval(CxBuffer *in, char **val, int allowzero) { // value = length string // length = uint32 @@ -172,6 +227,9 @@ if(ret) { pwdstore_put_index(p, id, locations); + if(cxListSize(locations) == 0) { + cxListDestroy(locations); + } } else { if(id) free(id); cxListDestroy(locations); @@ -235,7 +293,9 @@ PwdEntry *e = cxMapRemoveAndGet(s->ids, key); if(i) { - cxListDestroy(i->locations); + if(i->locations) { + cxListDestroy(i->locations); + } free(i->id); free(i); } @@ -306,6 +366,8 @@ cxBufferFree(content); + p->isdecrypted = 1; + return 0; } @@ -377,13 +439,12 @@ } PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); newentry->id = id; - if(cxListSize(locations) > 0) { + if(locations && cxListSize(locations) > 0) { newentry->locations = locations; cxListAdd(p->locations, newentry); } else { newentry->locations = NULL; cxListAdd(p->noloc, newentry); - cxListDestroy(locations); } cxMapPut(p->index, cx_hash_key_str(id), newentry); } @@ -397,13 +458,15 @@ cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out); cxBufferWrite(e->id, 1, idlen, out); - CxIterator i = cxListIterator(e->locations); - cx_foreach(char *, location, i) { - uint32_t locationlen = strlen(location); - uint32_t netlocationlen = htonl(locationlen); - - cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); - cxBufferWrite(location, 1, locationlen, out); + if(e->locations) { + CxIterator i = cxListIterator(e->locations); + cx_foreach(char *, location, i) { + uint32_t locationlen = strlen(location); + uint32_t netlocationlen = htonl(locationlen); + + cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); + cxBufferWrite(location, 1, locationlen, out); + } } uint32_t terminate = 0;