sd2txt:
   45|    367|{
   46|    367|	unsigned char *buf, *raw;
   47|       |
   48|    367|	*len = 0;
   49|       |
   50|    367|	xht_walk(h, _sd2txt_count, (void *)len);
   51|    367|	if (!*len) {
  ------------------
  |  Branch (51:6): [True: 2, False: 365]
  ------------------
   52|      2|		*len = 1;
   53|      2|		buf = (unsigned char *)MDNSD_malloc(1);
  ------------------
  |  |   54|      2|#define MDNSD_malloc(size) malloc(size)
  ------------------
   54|      2|		*buf = 0;
   55|      2|		return buf;
   56|      2|	}
   57|       |
   58|    365|	raw = buf = (unsigned char *)MDNSD_malloc((size_t)(*len));
  ------------------
  |  |   54|    365|#define MDNSD_malloc(size) malloc(size)
  ------------------
   59|    365|	xht_walk(h, _sd2txt_write, &buf);
   60|       |
   61|    365|	return raw;
   62|    367|}
txt2sd:
   65|    367|{
   66|    367|	char key[256];
   67|    367|	xht_t *h = NULL;
   68|       |
   69|    367|	if (txt == 0 || len == 0 || *txt == 0)
  ------------------
  |  Branch (69:6): [True: 0, False: 367]
  |  Branch (69:18): [True: 1, False: 366]
  |  Branch (69:30): [True: 1, False: 365]
  ------------------
   70|      2|		return NULL;
   71|       |
   72|    365|	h = xht_new(23);
   73|       |
   74|       |	/* Loop through data breaking out each block, storing into hashtable */
   75|  29.7k|	for (; len > 0 && *txt <= len; len -= *txt, txt += *txt + 1) {
  ------------------
  |  Branch (75:9): [True: 29.7k, False: 0]
  |  Branch (75:20): [True: 29.6k, False: 122]
  ------------------
   76|  29.6k|		char* val;
   77|  29.6k|		if (*txt == 0)
  ------------------
  |  Branch (77:7): [True: 25, False: 29.5k]
  ------------------
   78|     25|			break;
   79|       |
   80|  29.5k|		memcpy(key, txt + 1, *txt);
   81|  29.5k|		key[*txt] = 0;
   82|  29.5k|		if ((val = strchr(key, '=')) != 0) {
  ------------------
  |  Branch (82:7): [True: 12.3k, False: 17.2k]
  ------------------
   83|  12.3k|			*val = 0;
   84|  12.3k|			val++;
   85|  12.3k|		}
   86|  29.5k|		if (val != NULL)
  ------------------
  |  Branch (86:7): [True: 12.3k, False: 17.2k]
  ------------------
   87|  12.3k|			xht_store(h, key, (int)strlen(key), val, (int)strlen(val));
   88|  29.5k|		if (*txt +1 > len)
  ------------------
  |  Branch (88:7): [True: 218, False: 29.3k]
  ------------------
   89|    218|		    break;
   90|  29.5k|	}
   91|       |
   92|    365|	return h;
   93|    367|}
sdtxt.c:_sd2txt_count:
   19|  8.42k|{
   20|  8.42k|	int *count = (int *)arg;
   21|       |
   22|  8.42k|	*count += (int)_sd2txt_len(key, (char *)val) + 1;
   23|  8.42k|}
sdtxt.c:_sd2txt_len:
    6|  16.8k|{
    7|  16.8k|	size_t ret = strlen(key);
    8|       |
    9|  16.8k|	if (!*val)
  ------------------
  |  Branch (9:6): [True: 5.33k, False: 11.5k]
  ------------------
   10|  5.33k|		return ret;
   11|       |
   12|  11.5k|	ret += strlen(val);
   13|  11.5k|	ret++;
   14|       |
   15|  11.5k|	return ret;
   16|  16.8k|}
sdtxt.c:_sd2txt_write:
   26|  8.42k|{
   27|  8.42k|	unsigned char **txtp = (unsigned char **)arg;
   28|  8.42k|	char *cval = (char *)val;
   29|       |
   30|       |	/* Copy in lengths, then strings */
   31|  8.42k|	**txtp = (unsigned char)_sd2txt_len(key, (char *)val);
   32|  8.42k|	(*txtp)++;
   33|  8.42k|	memcpy(*txtp, key, strlen(key));
   34|  8.42k|	*txtp += strlen(key);
   35|  8.42k|	if (!*cval)
  ------------------
  |  Branch (35:6): [True: 2.66k, False: 5.76k]
  ------------------
   36|  2.66k|		return;
   37|       |
   38|  5.76k|	**txtp = '=';
   39|  5.76k|	(*txtp)++;
   40|  5.76k|	memcpy(*txtp, cval, strlen(cval));
   41|  5.76k|	*txtp += strlen(cval);
   42|  5.76k|}

xht_new:
   50|    365|{
   51|    365|	xht_t *xnew;
   52|       |
   53|    365|	xnew = (xht_t *)MDNSD_malloc(sizeof(struct xht));
  ------------------
  |  |   54|    365|#define MDNSD_malloc(size) malloc(size)
  ------------------
   54|    365|	xnew->prime = prime;
   55|    365|	xnew->zen = (xhn_t *)MDNSD_calloc(1, (sizeof(struct xhn) * (size_t)prime));	/* array of xhn_t size of prime */
  ------------------
  |  |   55|    365|#define MDNSD_calloc(num, size) calloc(num, size)
  ------------------
   56|       |
   57|    365|	return xnew;
   58|    365|}
xht_set:
  104|    367|{
  105|    367|	if (h == 0 || key == 0)
  ------------------
  |  Branch (105:6): [True: 2, False: 365]
  |  Branch (105:16): [True: 0, False: 365]
  ------------------
  106|      2|		return;
  107|    365|	_xht_set(h, key, val, 0);
  108|    365|}
xht_store:
  111|  12.3k|{
  112|  12.3k|	char *ckey, *cval;
  113|       |
  114|  12.3k|	if (h == 0 || key == 0 || klen == 0)
  ------------------
  |  Branch (114:6): [True: 0, False: 12.3k]
  |  Branch (114:16): [True: 0, False: 12.3k]
  |  Branch (114:28): [True: 1.24k, False: 11.0k]
  ------------------
  115|  1.24k|		return;
  116|       |
  117|  11.0k|	ckey = (char *)MDNSD_malloc((size_t)klen + 1);
  ------------------
  |  |   54|  11.0k|#define MDNSD_malloc(size) malloc(size)
  ------------------
  118|  11.0k|	memcpy(ckey, key, (size_t)klen);
  119|  11.0k|	ckey[klen] = '\0';
  120|  11.0k|	cval = (char *)MDNSD_malloc((size_t)vlen + 1);
  ------------------
  |  |   54|  11.0k|#define MDNSD_malloc(size) malloc(size)
  ------------------
  121|  11.0k|	memcpy(cval, val, (size_t)vlen);
  122|  11.0k|	cval[vlen] = '\0';	/* convenience, in case it was a string too */
  123|  11.0k|	_xht_set(h, ckey, cval, 1);
  124|  11.0k|}
xht_free:
  139|    367|{
  140|    367|	int i;
  141|    367|	xhn_t *n, *f;
  142|       |
  143|    367|	if (h == 0)
  ------------------
  |  Branch (143:6): [True: 2, False: 365]
  ------------------
  144|      2|		return;
  145|       |
  146|  8.76k|	for (i = 0; i < h->prime; i++) {
  ------------------
  |  Branch (146:14): [True: 8.39k, False: 365]
  ------------------
  147|  8.39k|		if ((n = (&h->zen[i])) == NULL)
  ------------------
  |  Branch (147:7): [True: 0, False: 8.39k]
  ------------------
  148|      0|			continue;
  149|  8.39k|		if (n->flag) {
  ------------------
  |  Branch (149:7): [True: 1.50k, False: 6.88k]
  ------------------
  150|  1.50k|			MDNSD_free((void *)n->key);
  ------------------
  |  |   53|  1.50k|#define MDNSD_free(ptr) free(ptr)
  ------------------
  151|  1.50k|			MDNSD_free(n->val);
  ------------------
  |  |   53|  1.50k|#define MDNSD_free(ptr) free(ptr)
  ------------------
  152|  1.50k|		}
  153|  15.1k|		for (n = (&h->zen[i])->next; n != 0;) {
  ------------------
  |  Branch (153:32): [True: 6.78k, False: 8.39k]
  ------------------
  154|  6.78k|			f = n->next;
  155|  6.78k|			if (n->flag) {
  ------------------
  |  Branch (155:8): [True: 6.55k, False: 232]
  ------------------
  156|  6.55k|				MDNSD_free((void *)n->key);
  ------------------
  |  |   53|  6.55k|#define MDNSD_free(ptr) free(ptr)
  ------------------
  157|  6.55k|				MDNSD_free(n->val);
  ------------------
  |  |   53|  6.55k|#define MDNSD_free(ptr) free(ptr)
  ------------------
  158|  6.55k|			}
  159|  6.78k|			MDNSD_free(n);
  ------------------
  |  |   53|  6.78k|#define MDNSD_free(ptr) free(ptr)
  ------------------
  160|  6.78k|			n = f;
  161|  6.78k|		}
  162|  8.39k|	}
  163|       |
  164|    365|	MDNSD_free(h->zen);
  ------------------
  |  |   53|    365|#define MDNSD_free(ptr) free(ptr)
  ------------------
  165|    365|	MDNSD_free(h);
  ------------------
  |  |   53|    365|#define MDNSD_free(ptr) free(ptr)
  ------------------
  166|    365|}
xht_walk:
  169|    732|{
  170|    732|	int i;
  171|    732|	xhn_t *n;
  172|       |
  173|    732|	if (h == 0 || w == 0)
  ------------------
  |  Branch (173:6): [True: 2, False: 730]
  |  Branch (173:16): [True: 0, False: 730]
  ------------------
  174|      2|		return;
  175|       |
  176|  17.5k|	for (i = 0; i < h->prime; i++) {
  ------------------
  |  Branch (176:14): [True: 16.7k, False: 730]
  ------------------
  177|  47.1k|		for (n = &h->zen[i]; n != 0; n = n->next) {
  ------------------
  |  Branch (177:24): [True: 30.3k, False: 16.7k]
  ------------------
  178|  30.3k|			if (n->key != 0 && n->val != 0)
  ------------------
  |  Branch (178:8): [True: 16.8k, False: 13.5k]
  |  Branch (178:23): [True: 16.8k, False: 0]
  ------------------
  179|  16.8k|				(*w)(h, n->key, n->val, arg);
  180|  30.3k|		}
  181|  16.7k|	}
  182|    730|}
xht.c:_xht_set:
   62|  11.4k|{
   63|  11.4k|	int i;
   64|  11.4k|	xhn_t *n;
   65|       |
   66|       |	/* get our index for this key */
   67|  11.4k|	i = _xhter(key) % h->prime;
   68|       |
   69|       |	/* check for existing key first, or find an empty one */
   70|  11.4k|	if ((n = _xht_node_find(&h->zen[i], key)) == 0) {
  ------------------
  |  Branch (70:6): [True: 8.42k, False: 3.02k]
  ------------------
   71|  78.2k|		for (n = &h->zen[i]; n != 0; n = n->next) {
  ------------------
  |  Branch (71:24): [True: 71.4k, False: 6.78k]
  ------------------
   72|  71.4k|			if (n->val == 0)
  ------------------
  |  Branch (72:8): [True: 1.63k, False: 69.8k]
  ------------------
   73|  1.63k|				break;
   74|  71.4k|		}
   75|  8.42k|	}
   76|       |
   77|       |	/* if none, make a new one, link into this index */
   78|  11.4k|	if (n == NULL) {
  ------------------
  |  Branch (78:6): [True: 6.78k, False: 4.66k]
  ------------------
   79|  6.78k|		if (h->zen != NULL) {
  ------------------
  |  Branch (79:7): [True: 6.78k, False: 0]
  ------------------
   80|  6.78k|			n = (xhn_t *)MDNSD_malloc(sizeof(struct xhn));
  ------------------
  |  |   54|  6.78k|#define MDNSD_malloc(size) malloc(size)
  ------------------
   81|  6.78k|			n->next = NULL;
   82|  6.78k|			n->next = h->zen[i].next;
   83|  6.78k|			h->zen[i].next = n;
   84|  6.78k|		}
   85|  6.78k|	} else if (n->flag) {
  ------------------
  |  Branch (85:13): [True: 3.02k, False: 1.63k]
  ------------------
   86|       |		/* When flag is set, we manage their mem and free em first */
   87|  3.02k|		MDNSD_free((void *)n->key);
  ------------------
  |  |   53|  3.02k|#define MDNSD_free(ptr) free(ptr)
  ------------------
   88|  3.02k|		MDNSD_free(n->val);
  ------------------
  |  |   53|  3.02k|#define MDNSD_free(ptr) free(ptr)
  ------------------
   89|  3.02k|	}
   90|       |
   91|  11.4k|	if (n != NULL) {
  ------------------
  |  Branch (91:6): [True: 11.4k, False: 0]
  ------------------
   92|  11.4k|		n->flag = flag;
   93|  11.4k|		n->key = key;
   94|  11.4k|		n->val = val;
   95|  11.4k|	} else {
   96|      0|		MDNSD_free(key);
  ------------------
  |  |   53|      0|#define MDNSD_free(ptr) free(ptr)
  ------------------
   97|      0|		MDNSD_free(val);
  ------------------
  |  |   53|      0|#define MDNSD_free(ptr) free(ptr)
  ------------------
   98|      0|	}
   99|       |
  100|  11.4k|	return n;
  101|  11.4k|}
xht.c:_xht_node_find:
   41|  11.4k|{
   42|   123k|	for (; n != 0; n = n->next)
  ------------------
  |  Branch (42:9): [True: 114k, False: 8.42k]
  ------------------
   43|   114k|		if (n->key != 0 && strcmp(key, n->key) == 0)
  ------------------
  |  Branch (43:7): [True: 113k, False: 1.63k]
  |  Branch (43:22): [True: 3.02k, False: 110k]
  ------------------
   44|  3.02k|			return n;
   45|  8.42k|	return 0;
   46|  11.4k|}
xht.c:_xhter:
   22|  11.4k|{
   23|       |	/* ELF hash uses unsigned chars and unsigned arithmetic for portability */
   24|  11.4k|	const unsigned char *name = (const unsigned char *)s;
   25|  11.4k|	unsigned long int h = 0;
   26|       |
   27|  2.88M|	while (*name) {		/* do some fancy bitwanking on the string */
  ------------------
  |  Branch (27:9): [True: 2.86M, False: 11.4k]
  ------------------
   28|  2.86M|		unsigned long int g;
   29|  2.86M|		h = (h << 4) + (unsigned long int)(*name++);
   30|  2.86M|		if ((g = (h & 0xF0000000UL)) != 0)
  ------------------
  |  Branch (30:7): [True: 2.60M, False: 268k]
  ------------------
   31|  2.60M|			h ^= (g >> 24);
   32|  2.86M|		h &= ~g;
   33|       |
   34|  2.86M|	}
   35|       |
   36|  11.4k|	return (int)h;
   37|  11.4k|}

LLVMFuzzerTestOneInput:
   39|    367|LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   40|       |
   41|       |
   42|    367|    size_t halfSize = size / 2;
   43|    367|    size_t remainSize = size - halfSize;
   44|    367|    const uint8_t *secondHalfData = data + halfSize;
   45|       |
   46|    367|    xht_t *sd = txt2sd((const unsigned  char*)data, halfSize);
   47|       |
   48|       |
   49|    367|    uint8_t *dataCopy = (unsigned char *)malloc(remainSize);
   50|    367|    if (!dataCopy) {
  ------------------
  |  Branch (50:9): [True: 0, False: 367]
  ------------------
   51|      0|        return 0;
   52|      0|    }
   53|    367|    memcpy(dataCopy, secondHalfData, remainSize);
   54|       |    // make sure we have a valid null-terminated string
   55|    367|    if (remainSize > 0)
  ------------------
  |  Branch (55:9): [True: 367, False: 0]
  ------------------
   56|    367|        dataCopy[remainSize-1] = 0;
   57|       |
   58|    367|    xht_set(sd, (char*)dataCopy, dataCopy);
   59|       |
   60|    367|    int len;
   61|    367|    unsigned char* out = sd2txt(sd, &len);
   62|       |
   63|    367|    MDNSD_free(out);
  ------------------
  |  |   53|    367|#define MDNSD_free(ptr) free(ptr)
  ------------------
   64|    367|    xht_free(sd);
   65|       |
   66|    367|    free(dataCopy);
   67|       |
   68|    367|    return 0;
   69|    367|}

