dav/pwd.c

changeset 747
efbd59642577
parent 731
e0358fa1a3b1
child 789
378b5ab86f77
--- a/dav/pwd.c	Sun Apr 16 14:12:24 2023 +0200
+++ b/dav/pwd.c	Fri Apr 21 21:25:32 2023 +0200
@@ -28,11 +28,13 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "pwd.h"
 
-#include <ucx/buffer.h>
-#include <ucx/utils.h>
+#include <cx/buffer.h>
+#include <cx/utils.h>
+#include <cx/hash_map.h>
 
 #ifdef _WIN32
 #include <winsock.h>
@@ -46,20 +48,20 @@
         return NULL;
     }
     
-    UcxBuffer *buf = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
-    ucx_stream_copy(in, buf, (read_func)fread, (write_func)ucx_buffer_write);
+    CxBuffer *buf = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
+    cx_stream_copy(in, buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite);
     fclose(in);
     
     if(buf->size < PWDS_HEADER_SIZE || buf->space[0] != PWDS_MAGIC_CHAR) {
-        ucx_buffer_free(buf);
+        cxBufferFree(buf);
         return NULL;
     }
     
     PwdStore *p = malloc(sizeof(PwdStore));
-    p->ids = ucx_map_new(16);
-    p->locations = NULL;
-    p->noloc = NULL;
-    p->index = ucx_map_new(16);
+    p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
+    p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
     p->content = buf;
     p->key = NULL;
     p->unlock_cmd = NULL;
@@ -77,11 +79,11 @@
 
 PwdStore* pwdstore_new(void) {
     PwdStore *p = calloc(1, sizeof(PwdStore));
-    p->ids = ucx_map_new(16);
-    p->locations = NULL;
-    p->noloc = NULL;
-    p->index = ucx_map_new(16);
-    p->content = ucx_buffer_new(NULL, PWDS_HEADER_SIZE, UCX_BUFFER_AUTOEXTEND);
+    p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
+    p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
+    p->content = cxBufferCreate(NULL, PWDS_HEADER_SIZE, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
     PWDS_MAGIC(p) = PWDS_MAGIC_CHAR;
     PWDS_VERSION(p) = 1;
     PWDS_ENC(p) = DAV_KEY_AES256;
@@ -92,7 +94,7 @@
     return p;
 }
 
-static int readval(UcxBuffer *in, char **val, int allowzero) {
+static int readval(CxBuffer *in, char **val, int allowzero) {
     // value  = length string
     // length = uint32
     // string = bytes
@@ -101,7 +103,7 @@
     
     // get length
     uint32_t length = 0;
-    if(ucx_buffer_read(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) {
+    if(cxBufferRead(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) {
         return 0;
     }
     length = ntohl(length); // convert from BE to host byte order
@@ -119,7 +121,7 @@
     // get value
     char *value = malloc(length + 1);
     value[length] = 0;
-    if(ucx_buffer_read(value, 1, length, in) != length) {
+    if(cxBufferRead(value, 1, length, in) != length) {
         free(value);
         return 0;
     }
@@ -128,16 +130,17 @@
     return 1;
 }
 
-static int read_indexentry(PwdStore *p, UcxBuffer *in) {
+static int read_indexentry(PwdStore *p, CxBuffer *in) {
     // read type of index element
-    int type = ucx_buffer_getc(in);
+    int type = cxBufferGet(in);
     if(type == EOF || type != 0) {
         // only type 0 supported yet
         return 0;
     }
       
     char *id = NULL;
-    UcxList *locations = NULL;
+    CxList *locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+    locations->simple_destructor = free;
     
     // get id (required)
     int ret = 0;
@@ -149,7 +152,7 @@
             if(!location) {
                 break;
             }
-            locations = ucx_list_append(locations, location);
+            cxListAdd(locations, location);
         }
     }
     
@@ -157,14 +160,14 @@
         pwdstore_put_index(p, id, locations);
     } else {
         if(id) free(id);
-        ucx_list_free_content(locations, free);
+        cxListDestroy(locations);
     }
     
     return ret;
 }
 
-static int read_pwdentry(PwdStore *p, UcxBuffer *in) {
-    int type = ucx_buffer_getc(in);
+static int read_pwdentry(PwdStore *p, CxBuffer *in) {
+    int type = cxBufferGet(in);
     if(type == EOF || type != 0) {
         // only type 0 supported yet
         return 0;
@@ -196,44 +199,36 @@
 static int remove_list_entries(PwdStore *s, const char *id) {
     int ret = 0;
     
-    UcxList *loc_entry = NULL;
-    UcxList *noloc_entry = NULL;
-    UCX_FOREACH(elm, s->locations) {
-        PwdIndexEntry *ie = elm->data;
+    CxList *loc_entry = NULL;
+    CxList *noloc_entry = NULL;
+    
+    CxMutIterator i = cxListMutIterator(s->locations);
+    cx_foreach(PwdIndexEntry*, ie, i) {
         if(!strcmp(ie->id, id)) {
-            loc_entry = elm;
-            ret = 1;
-            break;
+            cxIteratorFlagRemoval(i);
+            // TODO: break loop
         }
     }
-    UCX_FOREACH(elm, s->noloc) {
-        PwdIndexEntry *ie = elm->data;
+    i = cxListMutIterator(s->noloc);
+    cx_foreach(PwdIndexEntry*, ie, i) {
         if(!strcmp(ie->id, id)) {
-            noloc_entry = elm;
-            ret = 1;
-            break;
+            cxIteratorFlagRemoval(i);
+            // TODO: break loop
         }
     }
     
-    if(loc_entry) {
-        s->locations = ucx_list_remove(s->locations, loc_entry);
-    }
-    if(noloc_entry) {
-        s->noloc = ucx_list_remove(s->noloc, noloc_entry);
-    }
-    
     return ret;
 }
 
 void pwdstore_remove_entry(PwdStore *s, const char *id) {
     while(remove_list_entries(s, id)) {}
     
-    PwdIndexEntry *i = ucx_map_cstr_remove(s->index, id);
-    PwdEntry *e = ucx_map_cstr_remove(s->ids, id);
+    CxHashKey key = cx_hash_key_str(id);
+    PwdIndexEntry *i = cxMapRemoveAndGet(s->index, key);
+    PwdEntry *e = cxMapRemoveAndGet(s->ids, key);
     
     if(i) {
-        ucx_list_free_content(i->locations, free);
-        ucx_list_free(i->locations);
+        cxListDestroy(i->locations);
         free(i->id);
         free(i);
     }
@@ -253,7 +248,7 @@
     s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
     
     // read indexlen and convert to host byte order
-    if(ucx_buffer_read(&netindexlen, 1, sizeof(uint32_t), s->content) != sizeof(uint32_t)) {
+    if(cxBufferRead(&netindexlen, 1, sizeof(uint32_t), s->content) != sizeof(uint32_t)) {
         return 1;
     }
     uint32_t indexlen = ntohl(netindexlen);
@@ -269,14 +264,14 @@
     s->encoffset = PWDS_HEADER_SIZE + indexlen;
     
     // the index starts after the header
-    UcxBuffer *index = ucx_buffer_new(s->content->space+PWDS_HEADER_SIZE, indexlen, 0);
+    CxBuffer *index = cxBufferCreate(s->content->space+PWDS_HEADER_SIZE, indexlen, cxDefaultAllocator, 0);
     index->size = indexlen;
     
     // read index
     while(read_indexentry(s, index)) {}
     
     // free index buffer structure (not the content)
-    ucx_buffer_free(index);
+    cxBufferFree(index);
     
     return 0;
 }
@@ -291,18 +286,18 @@
     
     // decrypt contet
     size_t encsz = p->content->size - p->encoffset;
-    UcxBuffer *enc = ucx_buffer_new(p->content->space + p->encoffset, encsz, 0);
+    CxBuffer *enc = cxBufferCreate(p->content->space + p->encoffset, encsz, cxDefaultAllocator, 0);
     enc->size = encsz;
     enc->size = p->content->size - p->encoffset;
-    UcxBuffer *content = aes_decrypt_buffer(enc, p->key);
-    ucx_buffer_free(enc);
+    CxBuffer *content = aes_decrypt_buffer(enc, p->key);
+    cxBufferFree(enc);
     if(!content) {
         return 1;
     }
     
     while(read_pwdentry(p, content)) {}
     
-    ucx_buffer_free(content);
+    cxBufferFree(content);
     
     return 0;
 }
@@ -335,24 +330,24 @@
 }
 
 void pwdstore_free(PwdStore* p) {
-    ucx_map_free_content(p->ids, (ucx_destructor)pwdstore_free_entry);
-    ucx_map_free(p->ids);
+    p->ids->simple_destructor = (cx_destructor_func)pwdstore_free_entry;
+    cxMapDestroy(p->ids);
     
-    ucx_list_free(p->locations);
+    cxListDestroy(p->locations);
     
     if(p->content) {
-        ucx_buffer_free(p->content);
+        cxBufferFree(p->content);
     }
     
     free(p);
 }
 
 int pwdstore_has_id(PwdStore *s, const char *id) {
-    return ucx_map_cstr_get(s->index, id) ? 1 : 0;
+    return cxMapGet(s->index, cx_hash_key_str(id)) ? 1 : 0;
 }
 
 PwdEntry* pwdstore_get(PwdStore *p, const char *id) {
-    PwdEntry *e = ucx_map_cstr_get(p->ids, id);
+    PwdEntry *e = cxMapGet(p->ids, cx_hash_key_str(id));
     if(e && e->user && e->password) {
         return e;
     } else {
@@ -365,11 +360,11 @@
     entry->id = strdup(id);
     entry->user = strdup(username);
     entry->password = strdup(password);
-    ucx_map_cstr_put(p->ids, id, entry);
+    cxMapPut(p->ids, cx_hash_key_str(id), entry);
 }
 
-void pwdstore_put_index(PwdStore *p, char *id, UcxList *locations) {
-    PwdIndexEntry *e = ucx_map_cstr_get(p->index, id);
+void pwdstore_put_index(PwdStore *p, char *id, CxList *locations) {
+    PwdIndexEntry *e = cxMapGet(p->index, cx_hash_key_str(id));
     if(e) {
         return;
     }
@@ -377,34 +372,34 @@
     newentry->id = id;
     if(locations) {
         newentry->locations = locations;
-        p->locations = ucx_list_append(p->locations, newentry);
+        cxListAdd(p->locations, newentry);
     } else {
         newentry->locations = NULL;
-        p->noloc = ucx_list_append(p->noloc, newentry);
+        cxListAdd(p->noloc, newentry);
     }
-    ucx_map_cstr_put(p->index, id, newentry);
+    cxMapPut(p->index, cx_hash_key_str(id), newentry);
 }
 
-void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) {
+void write_index_entry(CxBuffer *out, PwdIndexEntry *e) {
     uint32_t idlen = strlen(e->id);
     uint32_t netidlen = htonl(idlen);
     
-    ucx_buffer_putc(out, 0); // type
+    cxBufferPut(out, 0); // type
 
-    ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out);
-    ucx_buffer_write(e->id, 1, idlen, out);
+    cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out);
+    cxBufferWrite(e->id, 1, idlen, out);
     
-    UCX_FOREACH(elm, e->locations) {
-        char *location = elm->data;
+    CxIterator i = cxListIterator(e->locations);
+    cx_foreach(char *, location, i) {
         uint32_t locationlen = strlen(location);
         uint32_t netlocationlen = htonl(locationlen);
         
-        ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out);
-        ucx_buffer_write(location, 1, locationlen, out);
+        cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out);
+        cxBufferWrite(location, 1, locationlen, out);
     }
     
     uint32_t terminate = 0;
-    ucx_buffer_write(&terminate, 1, sizeof(uint32_t), out);
+    cxBufferWrite(&terminate, 1, sizeof(uint32_t), out);
 }
 
 int pwdstore_store(PwdStore *p, const char *file) {
@@ -412,22 +407,21 @@
         return 1;
     }
     
-    UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
-    UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
+    CxBuffer *index = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
+    CxBuffer *content = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
     
     // create index
-    UCX_FOREACH(elm, p->noloc) {
-        PwdIndexEntry *e = elm->data;
+    CxIterator i = cxListIterator(p->noloc);
+    cx_foreach(PwdIndexEntry*, e, i) {
         write_index_entry(index, e);
     }
-    UCX_FOREACH(elm, p->locations) {
-        PwdIndexEntry *e = elm->data;
+    i = cxListIterator(p->locations);
+    cx_foreach(PwdIndexEntry*, e, i) {
         write_index_entry(index, e);
     }
     
-    UcxMapIterator i = ucx_map_iterator(p->ids);
-    PwdEntry *value;
-    UCX_MAP_FOREACH(key, value, i) {
+    i = cxMapIteratorValues(p->ids);
+    cx_foreach(PwdEntry*, value, i) {
         if(!value->id || !value->user || !value->password) {
             continue;
         }
@@ -440,31 +434,31 @@
         uint32_t netplen = htonl(plen);
         
         // content buffer
-        ucx_buffer_putc(content, 0); // type
+        cxBufferPut(content, 0); // type
         
-        ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content);
-        ucx_buffer_write(value->id, 1, idlen, content);
-        ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content);
-        ucx_buffer_write(value->user, 1, ulen, content);
-        ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content);
-        ucx_buffer_write(value->password, 1, plen, content);
+        cxBufferWrite(&netidlen, 1, sizeof(uint32_t), content);
+        cxBufferWrite(value->id, 1, idlen, content);
+        cxBufferWrite(&netulen, 1, sizeof(uint32_t), content);
+        cxBufferWrite(value->user, 1, ulen, content);
+        cxBufferWrite(&netplen, 1, sizeof(uint32_t), content);
+        cxBufferWrite(value->password, 1, plen, content);
     }
       
     content->pos = 0;
-    UcxBuffer *enc = aes_encrypt_buffer(content, p->key);
+    CxBuffer *enc = aes_encrypt_buffer(content, p->key);
 
     p->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
     p->content->size = PWDS_HEADER_SIZE;
     
     // add index after header
     uint32_t netindexlen = htonl((uint32_t)index->size);
-    ucx_buffer_write(&netindexlen, 1, sizeof(uint32_t), p->content);
-    ucx_buffer_write(index->space, 1, index->size, p->content);
+    cxBufferWrite(&netindexlen, 1, sizeof(uint32_t), p->content);
+    cxBufferWrite(index->space, 1, index->size, p->content);
     
     // add encrypted buffer
-    ucx_buffer_write(enc->space, 1, enc->size, p->content);
+    cxBufferWrite(enc->space, 1, enc->size, p->content);
     
-    ucx_buffer_free(enc);
+    cxBufferFree(enc);
     
     FILE *out = fopen(file, "w");
     if(!out) {

mercurial