summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-27 12:10:33 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-27 12:10:33 +0300
commitee8b890f2412119add7870d7aadd07078d6b315c (patch)
tree2355e37c3e444decabf60cc6c8da3c3fdcc645d7 /src
parent5aee0dbef19c954c6898ee9048baf03d515d4e26 (diff)
GmCerts: Use a StringSet for identity URLs
Diffstat (limited to 'src')
-rw-r--r--src/gmcerts.c60
-rw-r--r--src/gmcerts.h10
2 files changed, 47 insertions, 23 deletions
diff --git a/src/gmcerts.c b/src/gmcerts.c
index 0c6d709c..cffa744f 100644
--- a/src/gmcerts.c
+++ b/src/gmcerts.c
@@ -61,50 +61,38 @@ iDefineClass(TrustEntry)
61 61
62/*----------------------------------------------------------------------------------------------*/ 62/*----------------------------------------------------------------------------------------------*/
63 63
64static int cmpUrl_GmIdentity_(const void *a, const void *b) { 64static int cmpUrl_GmIdentity_(const iString *a, const iString *b) {
65 return cmpStringCase_String((const iString *) a, (const iString *) b); 65 return cmpStringCase_String(a, b);
66} 66}
67 67
68void init_GmIdentity(iGmIdentity *d) { 68void init_GmIdentity(iGmIdentity *d) {
69 d->icon = 0x1f511; /* key */ 69 d->icon = 0x1f511; /* key */
70 d->flags = 0; 70 d->flags = 0;
71 d->cert = new_TlsCertificate(); 71 d->cert = new_TlsCertificate();
72// init_String(&d->fileName);
73 init_Block(&d->fingerprint, 0); 72 init_Block(&d->fingerprint, 0);
74 init_SortedArray(&d->useUrls, sizeof(iString), cmpUrl_GmIdentity_); 73 d->useUrls = newCmp_StringSet(cmpUrl_GmIdentity_);
75 init_String(&d->notes); 74 init_String(&d->notes);
76} 75}
77 76
78static void clear_GmIdentity_(iGmIdentity *d) {
79 iForEach(Array, i, &d->useUrls.values) {
80 deinit_String(i.value);
81 }
82 clear_SortedArray(&d->useUrls);
83}
84
85void deinit_GmIdentity(iGmIdentity *d) { 77void deinit_GmIdentity(iGmIdentity *d) {
86 clear_GmIdentity_(d); 78 iRelease(d->useUrls);
87 deinit_String(&d->notes); 79 deinit_String(&d->notes);
88 deinit_SortedArray(&d->useUrls);
89 delete_TlsCertificate(d->cert); 80 delete_TlsCertificate(d->cert);
90 deinit_Block(&d->fingerprint); 81 deinit_Block(&d->fingerprint);
91// deinit_String(&d->fileName);
92} 82}
93 83
94void serialize_GmIdentity(const iGmIdentity *d, iStream *outs) { 84void serialize_GmIdentity(const iGmIdentity *d, iStream *outs) {
95// serialize_String(&d->fileName, outs);
96 serialize_Block(&d->fingerprint, outs); 85 serialize_Block(&d->fingerprint, outs);
97 writeU32_Stream(outs, d->icon); 86 writeU32_Stream(outs, d->icon);
98 serialize_String(&d->notes, outs); 87 serialize_String(&d->notes, outs);
99 write32_Stream(outs, d->flags); 88 write32_Stream(outs, d->flags);
100 writeU32_Stream(outs, size_SortedArray(&d->useUrls)); 89 writeU32_Stream(outs, size_StringSet(d->useUrls));
101 iConstForEach(Array, i, &d->useUrls.values) { 90 iConstForEach(StringSet, i, d->useUrls) {
102 serialize_String(i.value, outs); 91 serialize_String(i.value, outs);
103 } 92 }
104} 93}
105 94
106void deserialize_GmIdentity(iGmIdentity *d, iStream *ins) { 95void deserialize_GmIdentity(iGmIdentity *d, iStream *ins) {
107// deserialize_String(&d->fileName, ins);
108 deserialize_Block(&d->fingerprint, ins); 96 deserialize_Block(&d->fingerprint, ins);
109 d->icon = readU32_Stream(ins); 97 d->icon = readU32_Stream(ins);
110 deserialize_String(&d->notes, ins); 98 deserialize_String(&d->notes, ins);
@@ -114,7 +102,8 @@ void deserialize_GmIdentity(iGmIdentity *d, iStream *ins) {
114 iString url; 102 iString url;
115 init_String(&url); 103 init_String(&url);
116 deserialize_String(&url, ins); 104 deserialize_String(&url, ins);
117 insert_SortedArray(&d->useUrls, &url); 105 insert_StringSet(d->useUrls, &url);
106 deinit_String(&url);
118 } 107 }
119} 108}
120 109
@@ -149,7 +138,36 @@ static iBool writeTextFile_(const iString *path, const iString *content) {
149} 138}
150 139
151iBool isUsed_GmIdentity(const iGmIdentity *d) { 140iBool isUsed_GmIdentity(const iGmIdentity *d) {
152 return d && !isEmpty_SortedArray(&d->useUrls); 141 return d && !isEmpty_StringSet(d->useUrls);
142}
143
144iBool isUsedOn_GmIdentity(const iGmIdentity *d, const iString *url) {
145 size_t pos = iInvalidPos;
146 locate_StringSet(d->useUrls, url, &pos);
147 if (pos < size_StringSet(d->useUrls)) {
148 return startsWithCase_String(url, cstr_String(constAt_StringSet(d->useUrls, pos)));
149 }
150 return iFalse;
151}
152
153void setUse_GmIdentity(iGmIdentity *d, const iString *url, iBool use) {
154 if (use && isUsedOn_GmIdentity(d, url)) {
155 return; /* Redudant. */
156 }
157 if (use) {
158#if !defined (NDEBUG)
159 const iBool wasInserted =
160#endif
161 insert_StringSet(d->useUrls, url);
162 iAssert(wasInserted);
163 }
164 else {
165 remove_StringSet(d->useUrls, url);
166 }
167}
168
169void clearUse_GmIdentity(iGmIdentity *d) {
170 clear_StringSet(d->useUrls);
153} 171}
154 172
155iDefineTypeConstruction(GmIdentity) 173iDefineTypeConstruction(GmIdentity)
@@ -375,7 +393,7 @@ iBool checkTrust_GmCerts(iGmCerts *d, iRangecc domain, const iTlsCertificate *ce
375const iGmIdentity *identityForUrl_GmCerts(const iGmCerts *d, const iString *url) { 393const iGmIdentity *identityForUrl_GmCerts(const iGmCerts *d, const iString *url) {
376 iConstForEach(PtrArray, i, &d->idents) { 394 iConstForEach(PtrArray, i, &d->idents) {
377 const iGmIdentity *ident = i.ptr; 395 const iGmIdentity *ident = i.ptr;
378 iConstForEach(Array, j, &ident->useUrls.values) { 396 iConstForEach(StringSet, j, ident->useUrls) {
379 const iString *used = j.value; 397 const iString *used = j.value;
380 if (startsWithCase_String(url, cstr_String(used))) { 398 if (startsWithCase_String(url, cstr_String(used))) {
381 return ident; 399 return ident;
diff --git a/src/gmcerts.h b/src/gmcerts.h
index 35beca7c..1730ca8e 100644
--- a/src/gmcerts.h
+++ b/src/gmcerts.h
@@ -23,6 +23,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
23#pragma once 23#pragma once
24 24
25#include <the_Foundation/ptrarray.h> 25#include <the_Foundation/ptrarray.h>
26#include <the_Foundation/stringset.h>
26#include <the_Foundation/tlsrequest.h> 27#include <the_Foundation/tlsrequest.h>
27 28
28iDeclareType(GmIdentity) 29iDeclareType(GmIdentity)
@@ -34,16 +35,21 @@ enum iGmIdentityFlags {
34}; 35};
35 36
36struct Impl_GmIdentity { 37struct Impl_GmIdentity {
37// iString fileName;
38 iBlock fingerprint; 38 iBlock fingerprint;
39 iTlsCertificate *cert; 39 iTlsCertificate *cert;
40 iSortedArray useUrls; /* Strings */ 40 iStringSet *useUrls;
41 iChar icon; 41 iChar icon;
42 iString notes; /* private, local usage notes */ 42 iString notes; /* private, local usage notes */
43 int flags; 43 int flags;
44}; 44};
45 45
46iBool isUsed_GmIdentity (const iGmIdentity *); 46iBool isUsed_GmIdentity (const iGmIdentity *);
47iBool isUsedOn_GmIdentity (const iGmIdentity *, const iString *url);
48
49void setUse_GmIdentity (iGmIdentity *, const iString *url, iBool use);
50void clearUse_GmIdentity (iGmIdentity *);
51
52/*----------------------------------------------------------------------------------------------*/
47 53
48iDeclareType(GmCerts) 54iDeclareType(GmCerts)
49iDeclareTypeConstructionArgs(GmCerts, const char *saveDir) 55iDeclareTypeConstructionArgs(GmCerts, const char *saveDir)