MCPcopy Create free account
hub / github.com/JeanLucPons/VanitySearch / bech32_decode

Function bech32_decode

Bech32.cpp:122–177  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120}
121
122int bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input) {
123 uint32_t chk = 1;
124 size_t i;
125 size_t input_len = strlen(input);
126 size_t hrp_len;
127 int have_lower = 0, have_upper = 0;
128 if (input_len < 8 || input_len > 90) {
129 return 0;
130 }
131 *data_len = 0;
132 while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') {
133 ++(*data_len);
134 }
135 hrp_len = input_len - (1 + *data_len);
136 if (1 + *data_len >= input_len || *data_len < 6) {
137 return 0;
138 }
139 *(data_len) -= 6;
140 for (i = 0; i < hrp_len; ++i) {
141 int ch = input[i];
142 if (ch < 33 || ch > 126) {
143 return 0;
144 }
145 if (ch >= 'a' && ch <= 'z') {
146 have_lower = 1;
147 } else if (ch >= 'A' && ch <= 'Z') {
148 have_upper = 1;
149 ch = (ch - 'A') + 'a';
150 }
151 hrp[i] = ch;
152 chk = bech32_polymod_step(chk) ^ (ch >> 5);
153 }
154 hrp[i] = 0;
155 chk = bech32_polymod_step(chk);
156 for (i = 0; i < hrp_len; ++i) {
157 chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f);
158 }
159 ++i;
160 while (i < input_len) {
161 int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]];
162 if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1;
163 if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1;
164 if (v == -1) {
165 return 0;
166 }
167 chk = bech32_polymod_step(chk) ^ v;
168 if (i + 6 < input_len) {
169 data[i - (1 + hrp_len)] = v;
170 }
171 ++i;
172 }
173 if (have_lower && have_upper) {
174 return 0;
175 }
176 return chk == 1;
177}
178
179static int convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) {

Callers 1

segwit_addr_decodeFunction · 0.85

Calls 1

bech32_polymod_stepFunction · 0.85

Tested by

no test coverage detected