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 |
375 if(e) { |
437 if(e) { |
376 return; |
438 return; |
377 } |
439 } |
378 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); |
440 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); |
379 newentry->id = id; |
441 newentry->id = id; |
380 if(cxListSize(locations) > 0) { |
442 if(locations && cxListSize(locations) > 0) { |
381 newentry->locations = locations; |
443 newentry->locations = locations; |
382 cxListAdd(p->locations, newentry); |
444 cxListAdd(p->locations, newentry); |
383 } else { |
445 } else { |
384 newentry->locations = NULL; |
446 newentry->locations = NULL; |
385 cxListAdd(p->noloc, newentry); |
447 cxListAdd(p->noloc, newentry); |
386 cxListDestroy(locations); |
|
387 } |
448 } |
388 cxMapPut(p->index, cx_hash_key_str(id), newentry); |
449 cxMapPut(p->index, cx_hash_key_str(id), newentry); |
389 } |
450 } |
390 |
451 |
391 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) { |
452 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) { |
395 cxBufferPut(out, 0); // type |
456 cxBufferPut(out, 0); // type |
396 |
457 |
397 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out); |
458 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out); |
398 cxBufferWrite(e->id, 1, idlen, out); |
459 cxBufferWrite(e->id, 1, idlen, out); |
399 |
460 |
400 CxIterator i = cxListIterator(e->locations); |
461 if(e->locations) { |
401 cx_foreach(char *, location, i) { |
462 CxIterator i = cxListIterator(e->locations); |
402 uint32_t locationlen = strlen(location); |
463 cx_foreach(char *, location, i) { |
403 uint32_t netlocationlen = htonl(locationlen); |
464 uint32_t locationlen = strlen(location); |
404 |
465 uint32_t netlocationlen = htonl(locationlen); |
405 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); |
466 |
406 cxBufferWrite(location, 1, locationlen, out); |
467 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); |
|
468 cxBufferWrite(location, 1, locationlen, out); |
|
469 } |
407 } |
470 } |
408 |
471 |
409 uint32_t terminate = 0; |
472 uint32_t terminate = 0; |
410 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out); |
473 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out); |
411 } |
474 } |