2 grahpstuff.c - Code to handle the various graph algorithms
3 Written by Jonathan McDowell <noodles@earth.li>.
5 19/02/2000 - Started writing (sort of).
13 //#include "gpgstats.h"
17 #include "graphstuff.h"
19 struct keycount { unsigned long count; struct stats_key *k; };
21 struct ll *finished=NULL;
22 struct ll *trees=NULL;
25 int keycmp(struct stats_key *key1, struct stats_key *key2)
27 if (key1->keyid == key2->keyid) {
34 struct ll *addkey(struct ll *curkey, uint64_t keyid)
36 return lladd(curkey, createandaddtohash(keyid));
39 void readkeys(const char *filename)
42 unsigned long keys=0,sigs=0,pub=0, revoked=0;
45 struct stats_key *curkey=NULL, *cursign=NULL;
51 printf("Reading key info from '%s'.\n", filename);
52 if ((keyfile=fopen(filename, "r"))==NULL) {
57 fgets(curline, 1023, keyfile);
58 while (!feof(keyfile)) {
59 if (curline[0]=='P') {
62 printf("\rRead %ld keys so far.", keys);
63 keyin = strtoul(&curline[1], NULL, 16);
64 curkey = createandaddtohash(keyin);
65 } else if (curline[0]=='S') {
66 cursig = strtoul(&curline[1], NULL, 16);
67 /* if (curkey->keyid==cursig) {
71 if (!llfind(curkey->sigs, &cursig, p)) {
72 curkey->sigs = addkey(curkey->sigs, cursig);
76 if ((cursign=findinhash(cursig))==NULL) {
77 cursign = createandaddtohash(cursig);
80 //SIGNS if (!llfind(cursign->signs, curkey, p)) {
81 //SIGNS cursign->signs = addkey(cursign->signs,
82 //SIGNS curkey->keyid);
84 } else if (curline[0]=='N') {
85 /* if (curkey->name==NULL) {
86 curkey->name=strdup(&curline[1]);
87 curkey->name[strlen(curkey->name)-1]=0;
88 if (strcmp(curkey->name, "[revoked]")==0) {
94 fgets(curline, 1023, keyfile);
97 printf("\rRead %ld keys, %ld pub, %ld sigs, %ld revoked.\n", keys, pub, sigs, revoked);
98 printf("\rRead %ld keys, %ld pub, %ld sigs, %ld revoked.\n", keys, pub, sigs, revoked);
102 void DFSVisit(int type, struct stats_key *key, unsigned long *time, unsigned long *depth)
111 //SIGNS curkey = key->signs;
115 while (curkey != NULL) {
116 v = (struct stats_key *)findinhash(
117 ((struct stats_key *) curkey->object)->keyid);
119 printf("Couldn't find key in hash. Most odd.\n");
121 if (v != NULL && v->colour == 0) {
122 if (type == 1 && key->parent == 0) {
123 printf("key->parent is 0.\n");
124 } else if (type == 1) {
125 key->parent->object = lladd(key->parent->object,
127 v->parent = key->parent;
131 DFSVisit(type, v, time, depth);
138 finished=lladd(finished, key);
143 unsigned long DFS(void)
145 unsigned long loop,time=0,depth,maxdepth=0;
149 for (loop=0; loop<HASHSIZE; loop++) {
150 curkey=gethashtableentry(loop);
151 while (curkey!=NULL) {
152 if (((struct stats_key *)curkey->object)->colour==0) {
154 DFSVisit(0, ((struct stats_key *)curkey->object),
156 if (depth>maxdepth) maxdepth=depth;
165 unsigned long DFSsorted(void)
167 unsigned long time=0,depth,maxdepth=0;
172 while (curkey != NULL) {
173 if (((struct stats_key *)curkey->object)->colour == 0) {
174 trees = lladd(trees, curkey->object);
175 ((struct stats_key *)curkey->object)->parent =
177 ((struct stats_key *)curkey->object)->parent->object =
178 lladd(NULL, curkey->object);
181 DFSVisit(1, ((struct stats_key *)curkey->object),
183 if (depth>maxdepth) {
187 curkey = curkey->next;
196 unsigned long selfsig=0;
198 for (loop = 0; loop < HASHSIZE; loop++) {
199 curkey = gethashtableentry(loop);
200 while (curkey != NULL) {
201 //SELFSIGNED if (((struct stats_key *)curkey->object)->selfsigned) {
202 //SELFSIGNED ++selfsig;
204 curkey = curkey->next;
212 unsigned long countdegree(struct stats_key *have, int sigs, int maxdegree)
214 unsigned long count = 0, curdegree = 0;
215 struct ll *curll, *nextll, *sigll, *tmp;
220 curll = lladd(NULL, have);
222 while (curll != NULL && curdegree <= maxdegree) {
224 sigll = ((struct stats_key *)curll->object)->sigs;
226 //SIGNS sigll = ((struct stats_key *)curll->object)->signs;
228 while (sigll!=NULL) {
229 if (((struct stats_key *) sigll->object)->colour==0) {
230 /* We've never seen it. Count it, mark it and
231 explore its subtree */
233 ((struct stats_key *)sigll->object)->colour=curdegree;
234 ((struct stats_key *)sigll->object)->parent =
235 ((struct stats_key *)
236 curll->object)->keyid;
237 nextll=lladd(nextll, sigll->object);