dav/pwd.c

changeset 489
fb69eae42ef0
parent 488
29b979ca8750
child 515
2465dd550bb5
--- a/dav/pwd.c	Sat Oct 20 13:46:32 2018 +0200
+++ b/dav/pwd.c	Sat Oct 27 12:16:26 2018 +0200
@@ -95,7 +95,14 @@
         return 0;
     }
     length = ntohl(length);
-    if((length == 0 && !allowzero) || length > PWDSTORE_MAX_LEN) {
+    if(length == 0) {
+        if(allowzero) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    if(length > PWDSTORE_MAX_LEN) {
         return 0;
     }
     
@@ -110,7 +117,39 @@
     return 1;
 }
 
-static int read_pwdentry(PwdStore *p, UcxBuffer *in, int index) {
+static int read_indexentry(PwdStore *p, UcxBuffer *in) {
+    int type = ucx_buffer_getc(in);
+    if(type == EOF || type != 0) {
+        // only type 0 supported yet
+        return 0;
+    }
+    
+    char *id = NULL;
+    UcxList *locations = NULL;
+    
+    int ret = 0;
+    if(readval(in, &id, FALSE)) {
+        ret = 1;
+        char *location = NULL;
+        while((ret = readval(in, &location, TRUE)) == 1) {
+            if(!location) {
+                break;
+            }
+            locations = ucx_list_append(locations, location);
+        }
+    }
+    
+    if(ret) {
+        pwdstore_put_index(p, id, locations);
+    } else {
+        if(id) free(id);
+        ucx_list_free_content(locations, free);
+    }
+    
+    return ret;
+}
+
+static int read_pwdentry(PwdStore *p, UcxBuffer *in) {
     int type = ucx_buffer_getc(in);
     if(type == EOF || type != 0) {
         // only type 0 supported yet
@@ -122,28 +161,13 @@
     char *user = NULL;
     char *password = NULL;
     
-    int res = 0;
     int ret = 0;
-    if((res += readval(in, &id, FALSE)) == 1) {
-        if(index) {
-            if((res += readval(in, &location, TRUE)) == 2) {
-                pwdstore_put_index(p, id, location);
+    if(readval(in, &id, FALSE)) {
+        if(readval(in, &user, FALSE)) {
+            if(readval(in, &password, FALSE)) {
+                pwdstore_put(p, id, user, password);
                 ret = 1;
             }
-        } else {
-            if((res += readval(in, &user, FALSE)) == 2) {
-                if((res += readval(in, &password, FALSE)) == 3) {
-                    pwdstore_put(p, id, user, password);
-                    ret = 1;
-                }
-            }
-        }
-        if((res += readval(in, &location, TRUE)) == 2) {
-            if(!index) {
-                if((res += readval(in, &user, FALSE)) == 3) {
-                    res += readval(in, &password, FALSE);
-                }
-            }
         }
     }
     
@@ -153,7 +177,6 @@
     if(password) free(password);
     
     return ret;
-    
 }
 
 int pwdstore_getindex(PwdStore *s) {
@@ -173,7 +196,7 @@
     
     UcxBuffer *index = ucx_buffer_new(s->content->space+PWDS_HEADER_SIZE, indexlen, 0);
     index->size = indexlen;
-    while(read_pwdentry(s, index, 1)) {}
+    while(read_indexentry(s, index)) {}
     
     ucx_buffer_free(index);
     
@@ -199,7 +222,7 @@
         return 1;
     }
     
-    while(read_pwdentry(p, content, 0)) {}
+    while(read_pwdentry(p, content)) {}
     
     ucx_buffer_free(content);
     
@@ -267,18 +290,18 @@
     ucx_map_cstr_put(p->ids, id, entry);
 }
 
-void pwdstore_put_index(PwdStore *p, const char *id, const char *location) {
+void pwdstore_put_index(PwdStore *p, char *id, UcxList *locations) {
     PwdIndexEntry *e = ucx_map_cstr_get(p->index, id);
     if(e) {
         return;
     }
     PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
-    newentry->id = strdup(id);
-    if(location) {
-        newentry->location = strdup(location);
+    newentry->id = id;
+    if(locations) {
+        newentry->locations = locations;
         p->locations = ucx_list_append(p->locations, newentry);
     } else {
-        newentry->location = NULL;
+        newentry->locations = NULL;
         p->noloc = ucx_list_append(p->noloc, newentry);
     }
     ucx_map_cstr_put(p->index, id, newentry);
@@ -286,18 +309,24 @@
 
 void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) {
     uint32_t idlen = strlen(e->id);
-    uint32_t locationlen = e->location ? strlen(e->location) : 0;
     uint32_t netidlen = htonl(idlen);
-    uint32_t netlocationlen = htonl(locationlen);
     
     ucx_buffer_putc(out, 0); // type
 
     ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out);
     ucx_buffer_write(e->id, 1, idlen, out);
-    ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out);
-    if(e->location) {
-        ucx_buffer_write(e->location, 1, locationlen, out);
+    
+    UCX_FOREACH(elm, e->locations) {
+        char *location = elm->data;
+        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);
     }
+    
+    uint32_t terminate = 0;
+    ucx_buffer_write(&terminate, 1, sizeof(uint32_t), out);
 }
 
 int pwdstore_store(PwdStore *p, const char *file) {

mercurial