2 * Copyright (c) 2017-20 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 struct xref_block
*xref_blocks
;
36 static struct xref_block
**xref_block_last
= &xref_blocks
;
38 struct xrefdata_uid_head xrefdata_uid
= INIT_RBTREE_UNIQ(xrefdata_uid
);
40 static void base32(uint8_t **inpos
, int *bitpos
,
41 char *out
, size_t n_chars
)
43 static const char base32ch
[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
49 while (opos
< out
+ n_chars
) {
50 uint32_t bits
= in
[0] | (in
[1] << 8);
57 *opos
++ = base32ch
[bits
& 0x1f];
68 static void xref_add_one(const struct xref
*xref
)
71 struct xrefdata
*xrefdata
;
73 const char *filename
, *p
, *q
;
74 uint8_t hash
[32], *h
= hash
;
78 if (!xref
|| !xref
->xrefdata
)
81 xrefdata
= xref
->xrefdata
;
82 xrefdata
->xref
= xref
;
84 if (!xrefdata
->hashstr
)
87 /* as far as the unique ID is concerned, only use the last
88 * directory name + filename, e.g. "bgpd/bgp_route.c". This
89 * gives a little leeway in moving things and avoids IDs being
90 * screwed up by out of tree builds or absolute pathnames.
92 filename
= xref
->file
;
93 p
= strrchr(filename
, '/');
95 q
= memrchr(filename
, '/', p
- filename
);
101 SHA256_Update(&sha
, filename
, strlen(filename
));
102 SHA256_Update(&sha
, xrefdata
->hashstr
,
103 strlen(xrefdata
->hashstr
));
104 be_val
= htonl(xrefdata
->hashu32
[0]);
105 SHA256_Update(&sha
, &be_val
, sizeof(be_val
));
106 be_val
= htonl(xrefdata
->hashu32
[1]);
107 SHA256_Update(&sha
, &be_val
, sizeof(be_val
));
108 SHA256_Final(hash
, &sha
);
111 base32(&h
, &bitpos
, &xrefdata
->uid
[0], 5);
112 xrefdata
->uid
[5] = '-';
113 base32(&h
, &bitpos
, &xrefdata
->uid
[6], 5);
115 xrefdata_uid_add(&xrefdata_uid
, xrefdata
);
118 void xref_gcc_workaround(const struct xref
*xref
)
123 void xref_block_add(struct xref_block
*block
)
125 const struct xref
* const *xrefp
;
127 *xref_block_last
= block
;
128 xref_block_last
= &block
->next
;
130 for (xrefp
= block
->start
; xrefp
< block
->stop
; xrefp
++)
131 xref_add_one(*xrefp
);