MCPcopy Create free account
hub / github.com/ElementsProject/lightning / run

Function run

tests/fuzz/fuzz-bech32.c:13–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11}
12
13void run(const uint8_t *data, size_t size)
14{
15 const char hrp_inv[5] = "lnbc\0", hrp_addr[3] = "bc\0";
16 char *bech32_str, *hrp_out, *addr;
17 uint8_t *data_out;
18 size_t data_out_len, bech32_str_cap;
19 int decode_wit_version;
20 bech32_encoding benc, benc_decoded;
21
22 if (size < 1)
23 return;
24
25 /* Buffer size is defined in each function's doc comment. */
26 benc = data[0] ? BECH32_ENCODING_BECH32 : BECH32_ENCODING_BECH32M;
27 bech32_str_cap = (size - 1) + strlen(hrp_inv) + 8;
28 bech32_str = tal_arr(tmpctx, char, bech32_str_cap);
29 if (bech32_encode(bech32_str, hrp_inv, data + 1, size - 1,
30 bech32_str_cap, benc) == 1) {
31 hrp_out = tal_arr(tmpctx, char, strlen(bech32_str) - 6);
32 data_out = tal_arr(tmpctx, uint8_t, strlen(bech32_str) - 8);
33
34 benc_decoded = bech32_decode(hrp_out, data_out, &data_out_len,
35 bech32_str, bech32_str_cap);
36 assert(benc_decoded == benc);
37 assert(strcmp(hrp_inv, hrp_out) == 0);
38 assert(data_out_len == size - 1);
39 assert(memcmp(data_out, data + 1, data_out_len) == 0);
40 }
41
42 /* Convert data to 5-bit values (0-31) */
43 u8 *five_bit_data = tal_dup_arr(tmpctx, u8, data, size, 0);
44 for (size_t i = 0; i < size; i++)
45 five_bit_data[i] &= 0x1F;
46
47 u8 *eight_bit_data = tal_arr(tmpctx, u8, size);
48 size_t eight_bit_len = 0;
49 /* Convert 5-to-8 without padding */
50 if (bech32_convert_bits(eight_bit_data, &eight_bit_len, 8,
51 five_bit_data, size, 5, 0)) {
52 u8 *five_bit_deconv = tal_arr(tmpctx, u8, size);
53 size_t five_bit_deconv_len = 0;
54 /* Convert 8-to-5 with padding */
55 if (bech32_convert_bits(five_bit_deconv, &five_bit_deconv_len, 5,
56 eight_bit_data, eight_bit_len, 8, 1)) {
57 assert(five_bit_deconv_len == size);
58 assert(memcmp(five_bit_data, five_bit_deconv, five_bit_deconv_len) == 0);
59 }
60 }
61
62 data_out = tal_arr(tmpctx, uint8_t, size);
63 data_out_len = 0;
64
65 addr = tal_arr(tmpctx, char, 73 + strlen(hrp_addr));
66 for (int wit_version = 0; wit_version <= 16; ++wit_version) {
67 if (segwit_addr_encode(addr, hrp_addr, wit_version, data,
68 size) == 0)
69 continue;
70

Callers

nothing calls this directly

Calls 6

bech32_convert_bitsFunction · 0.85
segwit_addr_encodeFunction · 0.85
clean_tmpctxFunction · 0.85
bech32_encodeFunction · 0.50
bech32_decodeFunction · 0.50
segwit_addr_decodeFunction · 0.50

Tested by

no test coverage detected