---
name: "mtree_diff_subtree"
section: "3"
platform: "linux"
package: "liblibrecast-dev"
version: "0.9.1-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mtree_diff_subtree"
---

# mtree\_diff\_subtree(3)

compare two subtrees and return bitmap

## NAME

mtree\_diff\_subtree - compare two subtrees and return bitmap

## LIBRARY

Librecast library (*liblibrecast*, *-llibrecast*)

## SYNOPSIS

```
#include <librecast/mtree.h>
```

**unsigned char \*mtree\_diff\_subtree(mtree\_t** *\*t1***, mtree\_t** *\*t2***, size\_t** *root***, unsigned** *bits***);** **unsigned char \*mtree\_diff\_map(mtree\_t** *\*t1***, mtree\_t** *\*t2***);**

Compile and link with *-llibrecast*.

## DESCRIPTION

The **mtree\_diff\_subtree** function performs a bredth-first comparison of the merkle trees *t1* and *t2* and returns a pointer to a bitmap, setting *bits* bits for any chunks that differ. *bits* should be set to the number of packets required to to transport each chunk. When bits \> 1, chunks will be fragmented into multiple packets, and the bitmap will represent the packets required.

Checking the popcount (Hamming Weight) of the returned bitmap gives the number of packets required to patch the differences.

The returned bitmap must be freed by the caller when no longer needed by passing to [**free**(3)](/free/3.md).

**mtree\_diff\_map**() is a convenience wrapper that is the same as calling **mtree\_diff\_subtree**() with *root* set to zero (whole tree) and *bits* set to one. Thus, it returns a bitmap of the entire tree with a single bit per chunk.

## RETURN VALUE

**mtree\_verify**() returns zero if the tree is valid, or NULL on error, with *errno* set to indicate the error.

## ERRORS

- **ENOMEM**

  Not enough space/cannot allocate memory (POSIX.1-2001).

## SEE ALSO

[**mtree\_init**(3)](/mtree_init/3.md), [**mtree\_free**(3)](/mtree_free/3.md), [**mtree\_build**(3)](/mtree_build/3.md), [**free**(3)](/free/3.md)
