libidav/pwdstore.c

changeset 841
21403bdaf54c
parent 840
9904eee3ca9a
child 842
10ee615ca557
equal deleted inserted replaced
840:9904eee3ca9a 841:21403bdaf54c
107 p->isdecrypted = 1; 107 p->isdecrypted = 1;
108 p->encoffset = PWDS_HEADER_SIZE; 108 p->encoffset = PWDS_HEADER_SIZE;
109 return p; 109 return p;
110 } 110 }
111 111
112 PwdStore* pwdstore_clone(PwdStore *p) {
113 CxBuffer *newbuffer = calloc(1, sizeof(CxBuffer));
114 *newbuffer = *p->content;
115 newbuffer->space = malloc(p->content->capacity);
116 memcpy(newbuffer->space, p->content->space, p->content->capacity);
117
118 DavKey *key = NULL;
119 if(p->key) {
120 key = malloc(sizeof(DavKey));
121 key->data = malloc(p->key->length);
122 memcpy(key->data, p->key->data, p->key->length);
123 key->length = p->key->length;
124 key->type = p->key->type;
125 key->name = NULL;
126 }
127
128 PwdStore *newp = calloc(1, sizeof(PwdStore));
129 newp->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
130 newp->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
131 newp->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
132 newp->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
133 newp->content = newbuffer;
134 newp->key = key;
135 newp->unlock_cmd = p->unlock_cmd ? strdup(p->unlock_cmd) : NULL;
136 newp->lock_cmd = p->lock_cmd ? strdup(p->lock_cmd) : NULL;
137 newp->encoffset = p->encoffset;
138 newp->isdecrypted = p->isdecrypted;
139
140 CxIterator i = cxMapIterator(p->ids);
141 cx_foreach(CxMapEntry *, e, i) {
142 PwdEntry *entry = e->value;
143 PwdEntry *new_entry = malloc(sizeof(PwdEntry));
144 new_entry->id = strdup(entry->id);
145 new_entry->user = entry->user ? strdup(entry->user) : NULL;
146 new_entry->password = entry->password ? strdup(entry->password) : NULL;
147 cxMapPut(newp->ids, *e->key, new_entry);
148 }
149
150 i = cxMapIterator(p->index);
151 cx_foreach(CxMapEntry *, e, i) {
152 PwdIndexEntry *entry = e->value;
153 CxList *locations = NULL;
154 if(entry->locations) {
155 locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
156 CxIterator li = cxListIterator(entry->locations);
157 cx_foreach(char *, location, li) {
158 cxListAdd(locations, strdup(location));
159 }
160 }
161 pwdstore_put_index(newp, entry->id, locations);
162 }
163
164 return newp;
165 }
166
112 static int readval(CxBuffer *in, char **val, int allowzero) { 167 static int readval(CxBuffer *in, char **val, int allowzero) {
113 // value = length string 168 // value = length string
114 // length = uint32 169 // length = uint32
115 // string = bytes 170 // string = bytes
116 171
170 } 225 }
171 } 226 }
172 227
173 if(ret) { 228 if(ret) {
174 pwdstore_put_index(p, id, locations); 229 pwdstore_put_index(p, id, locations);
230 if(cxListSize(locations) == 0) {
231 cxListDestroy(locations);
232 }
175 } else { 233 } else {
176 if(id) free(id); 234 if(id) free(id);
177 cxListDestroy(locations); 235 cxListDestroy(locations);
178 } 236 }
179 237
379 if(e) { 437 if(e) {
380 return; 438 return;
381 } 439 }
382 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); 440 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
383 newentry->id = id; 441 newentry->id = id;
384 if(cxListSize(locations) > 0) { 442 if(locations && cxListSize(locations) > 0) {
385 newentry->locations = locations; 443 newentry->locations = locations;
386 cxListAdd(p->locations, newentry); 444 cxListAdd(p->locations, newentry);
387 } else { 445 } else {
388 newentry->locations = NULL; 446 newentry->locations = NULL;
389 cxListAdd(p->noloc, newentry); 447 cxListAdd(p->noloc, newentry);
390 cxListDestroy(locations);
391 } 448 }
392 cxMapPut(p->index, cx_hash_key_str(id), newentry); 449 cxMapPut(p->index, cx_hash_key_str(id), newentry);
393 } 450 }
394 451
395 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) { 452 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) {
399 cxBufferPut(out, 0); // type 456 cxBufferPut(out, 0); // type
400 457
401 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out); 458 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out);
402 cxBufferWrite(e->id, 1, idlen, out); 459 cxBufferWrite(e->id, 1, idlen, out);
403 460
404 CxIterator i = cxListIterator(e->locations); 461 if(e->locations) {
405 cx_foreach(char *, location, i) { 462 CxIterator i = cxListIterator(e->locations);
406 uint32_t locationlen = strlen(location); 463 cx_foreach(char *, location, i) {
407 uint32_t netlocationlen = htonl(locationlen); 464 uint32_t locationlen = strlen(location);
408 465 uint32_t netlocationlen = htonl(locationlen);
409 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); 466
410 cxBufferWrite(location, 1, locationlen, out); 467 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out);
468 cxBufferWrite(location, 1, locationlen, out);
469 }
411 } 470 }
412 471
413 uint32_t terminate = 0; 472 uint32_t terminate = 0;
414 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out); 473 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out);
415 } 474 }

mercurial