back to topotato report
topotato coverage report
Current view: top level - lib - xref.c (source / functions) Hit Total Coverage
Test: test_bgp_aggregate_address_route_map.py::BGPAggregateAddressRouteMap Lines: 56 59 94.9 %
Date: 2023-02-24 18:36:44 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017-20  David Lamparter, for NetDEF, Inc.
       3             :  *
       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.
       7             :  *
       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.
      15             :  */
      16             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include <stdlib.h>
      22             : #include <stdarg.h>
      23             : #include <string.h>
      24             : #include <pthread.h>
      25             : #include <signal.h>
      26             : #include <inttypes.h>
      27             : 
      28             : #include "xref.h"
      29             : #include "vty.h"
      30             : #include "jhash.h"
      31             : #include "sha256.h"
      32             : #include "memory.h"
      33             : #include "hash.h"
      34             : 
      35             : struct xref_block *xref_blocks;
      36             : static struct xref_block **xref_block_last = &xref_blocks;
      37             : 
      38             : struct xrefdata_uid_head xrefdata_uid = INIT_RBTREE_UNIQ(xrefdata_uid);
      39             : 
      40       17144 : static void base32(uint8_t **inpos, int *bitpos,
      41             :                    char *out, size_t n_chars)
      42             : {
      43       17144 :         static const char base32ch[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
      44             : 
      45       17144 :         char *opos = out;
      46       17144 :         uint8_t *in = *inpos;
      47       17144 :         int bp = *bitpos;
      48             : 
      49      102864 :         while (opos < out + n_chars) {
      50       85720 :                 uint32_t bits = in[0] | (in[1] << 8);
      51             : 
      52       85720 :                 if (bp == -1)
      53        8572 :                         bits |= 0x10;
      54             :                 else
      55       77148 :                         bits >>= bp;
      56             : 
      57       85720 :                 *opos++ = base32ch[bits & 0x1f];
      58             : 
      59       85720 :                 bp += 5;
      60       85720 :                 if (bp >= 8)
      61       51432 :                         in++, bp -= 8;
      62             :         }
      63       17144 :         *opos = '\0';
      64       17144 :         *inpos = in;
      65       17144 :         *bitpos = bp;
      66       17144 : }
      67             : 
      68       24086 : static void xref_add_one(const struct xref *xref)
      69             : {
      70       24086 :         SHA256_CTX sha;
      71       24086 :         struct xrefdata *xrefdata;
      72             : 
      73       24086 :         const char *filename, *p, *q;
      74       24086 :         uint8_t hash[32], *h = hash;
      75       24086 :         uint32_t be_val;
      76       24086 :         int bitpos;
      77             : 
      78       24086 :         if (!xref || !xref->xrefdata)
      79       15514 :                 return;
      80             : 
      81        8572 :         xrefdata = xref->xrefdata;
      82        8572 :         xrefdata->xref = xref;
      83             : 
      84        8572 :         if (!xrefdata->hashstr)
      85             :                 return;
      86             : 
      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.
      91             :          */
      92        8572 :         filename = xref->file;
      93        8572 :         p = strrchr(filename, '/');
      94        8572 :         if (p) {
      95        8572 :                 q = memrchr(filename, '/', p - filename);
      96        8572 :                 if (q)
      97         994 :                         filename = q + 1;
      98             :         }
      99             : 
     100        8572 :         SHA256_Init(&sha);
     101        8572 :         SHA256_Update(&sha, filename, strlen(filename));
     102        8572 :         SHA256_Update(&sha, xrefdata->hashstr,
     103             :                       strlen(xrefdata->hashstr));
     104        8572 :         be_val = htonl(xrefdata->hashu32[0]);
     105        8572 :         SHA256_Update(&sha, &be_val, sizeof(be_val));
     106        8572 :         be_val = htonl(xrefdata->hashu32[1]);
     107        8572 :         SHA256_Update(&sha, &be_val, sizeof(be_val));
     108        8572 :         SHA256_Final(hash, &sha);
     109             : 
     110        8572 :         bitpos = -1;
     111        8572 :         base32(&h, &bitpos, &xrefdata->uid[0], 5);
     112        8572 :         xrefdata->uid[5] = '-';
     113        8572 :         base32(&h, &bitpos, &xrefdata->uid[6], 5);
     114             : 
     115        8572 :         xrefdata_uid_add(&xrefdata_uid, xrefdata);
     116             : }
     117             : 
     118           0 : void xref_gcc_workaround(const struct xref *xref)
     119             : {
     120           0 :         xref_add_one(xref);
     121           0 : }
     122             : 
     123           8 : void xref_block_add(struct xref_block *block)
     124             : {
     125           8 :         const struct xref * const *xrefp;
     126             : 
     127           8 :         *xref_block_last = block;
     128           8 :         xref_block_last = &block->next;
     129             : 
     130       24094 :         for (xrefp = block->start; xrefp < block->stop; xrefp++)
     131       24086 :                 xref_add_one(*xrefp);
     132           8 : }

Generated by: LCOV version v1.16-topotato