Line data Source code
1 : // SPDX-License-Identifier: ISC
2 : /*
3 : * Copyright (c) 2017-20 David Lamparter, for NetDEF, Inc.
4 : */
5 :
6 : #ifdef HAVE_CONFIG_H
7 : #include "config.h"
8 : #endif
9 :
10 : #include <stdlib.h>
11 : #include <stdarg.h>
12 : #include <string.h>
13 : #include <pthread.h>
14 : #include <signal.h>
15 : #include <inttypes.h>
16 :
17 : #include "xref.h"
18 : #include "vty.h"
19 : #include "jhash.h"
20 : #include "sha256.h"
21 : #include "memory.h"
22 : #include "hash.h"
23 :
24 : struct xref_block *xref_blocks;
25 : static struct xref_block **xref_block_last = &xref_blocks;
26 :
27 : struct xrefdata_uid_head xrefdata_uid = INIT_RBTREE_UNIQ(xrefdata_uid);
28 :
29 18600 : static void base32(uint8_t **inpos, int *bitpos,
30 : char *out, size_t n_chars)
31 : {
32 18600 : static const char base32ch[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
33 :
34 18600 : char *opos = out;
35 18600 : uint8_t *in = *inpos;
36 18600 : int bp = *bitpos;
37 :
38 111600 : while (opos < out + n_chars) {
39 93000 : uint32_t bits = in[0] | (in[1] << 8);
40 :
41 93000 : if (bp == -1)
42 9300 : bits |= 0x10;
43 : else
44 83700 : bits >>= bp;
45 :
46 93000 : *opos++ = base32ch[bits & 0x1f];
47 :
48 93000 : bp += 5;
49 93000 : if (bp >= 8)
50 55800 : in++, bp -= 8;
51 : }
52 18600 : *opos = '\0';
53 18600 : *inpos = in;
54 18600 : *bitpos = bp;
55 18600 : }
56 :
57 26056 : static void xref_add_one(const struct xref *xref)
58 : {
59 26056 : SHA256_CTX sha;
60 26056 : struct xrefdata *xrefdata;
61 :
62 26056 : const char *filename, *p, *q;
63 26056 : uint8_t hash[32], *h = hash;
64 26056 : uint32_t be_val;
65 26056 : int bitpos;
66 :
67 26056 : if (!xref || !xref->xrefdata)
68 16756 : return;
69 :
70 9300 : xrefdata = xref->xrefdata;
71 9300 : xrefdata->xref = xref;
72 :
73 9300 : if (!xrefdata->hashstr)
74 : return;
75 :
76 : /* as far as the unique ID is concerned, only use the last
77 : * directory name + filename, e.g. "bgpd/bgp_route.c". This
78 : * gives a little leeway in moving things and avoids IDs being
79 : * screwed up by out of tree builds or absolute pathnames.
80 : */
81 9300 : filename = xref->file;
82 9300 : p = strrchr(filename, '/');
83 9300 : if (p) {
84 9300 : q = memrchr(filename, '/', p - filename);
85 9300 : if (q)
86 994 : filename = q + 1;
87 : }
88 :
89 9300 : SHA256_Init(&sha);
90 9300 : SHA256_Update(&sha, filename, strlen(filename));
91 9300 : SHA256_Update(&sha, xrefdata->hashstr,
92 : strlen(xrefdata->hashstr));
93 9300 : be_val = htonl(xrefdata->hashu32[0]);
94 9300 : SHA256_Update(&sha, &be_val, sizeof(be_val));
95 9300 : be_val = htonl(xrefdata->hashu32[1]);
96 9300 : SHA256_Update(&sha, &be_val, sizeof(be_val));
97 9300 : SHA256_Final(hash, &sha);
98 :
99 9300 : bitpos = -1;
100 9300 : base32(&h, &bitpos, &xrefdata->uid[0], 5);
101 9300 : xrefdata->uid[5] = '-';
102 9300 : base32(&h, &bitpos, &xrefdata->uid[6], 5);
103 :
104 9300 : xrefdata_uid_add(&xrefdata_uid, xrefdata);
105 : }
106 :
107 0 : void xref_gcc_workaround(const struct xref *xref)
108 : {
109 0 : xref_add_one(xref);
110 0 : }
111 :
112 8 : void xref_block_add(struct xref_block *block)
113 : {
114 8 : const struct xref * const *xrefp;
115 :
116 8 : *xref_block_last = block;
117 8 : xref_block_last = &block->next;
118 :
119 26064 : for (xrefp = block->start; xrefp < block->stop; xrefp++)
120 26056 : xref_add_one(*xrefp);
121 8 : }
|