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

Function bech32_encode

common/bech32.c:58–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56};
57
58int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, size_t max_output_len, bech32_encoding enc) {
59 uint32_t chk = 1;
60 size_t i = 0;
61 while (hrp[i] != 0) {
62 int ch = hrp[i];
63 if (ch < 33 || ch > 126) {
64 return 0;
65 }
66
67 if (ch >= 'A' && ch <= 'Z') return 0;
68 chk = bech32_polymod_step(chk) ^ (ch >> 5);
69 ++i;
70 }
71 if (i + 7 + data_len > max_output_len) return 0;
72 chk = bech32_polymod_step(chk);
73 while (*hrp != 0) {
74 chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f);
75 *(output++) = *(hrp++);
76 }
77 *(output++) = '1';
78 for (i = 0; i < data_len; ++i) {
79 if (*data >> 5) return 0;
80 chk = bech32_polymod_step(chk) ^ (*data);
81 *(output++) = bech32_charset[*(data++)];
82 }
83 for (i = 0; i < 6; ++i) {
84 chk = bech32_polymod_step(chk);
85 }
86 chk ^= bech32_final_constant(enc);
87 for (i = 0; i < 6; ++i) {
88 *(output++) = bech32_charset[(chk >> ((5 - i) * 5)) & 0x1f];
89 }
90 *output = 0;
91 return 1;
92}
93
94bech32_encoding bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t max_input_len) {
95 uint32_t chk = 1;

Callers 6

bolt11_encode_Function · 0.70
segwit_addr_encodeFunction · 0.70
print_emergencyrecoverFunction · 0.50
runFunction · 0.50
LLVMFuzzerCustomMutatorFunction · 0.50

Calls 2

bech32_polymod_stepFunction · 0.85
bech32_final_constantFunction · 0.85

Tested by

no test coverage detected