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

Function bech32_case_fixup

common/codex32.c:256–302  ·  view source on GitHub ↗

Checks case inconsistency, and for non-bech32 chars. */

Source from the content-addressed store, hash-verified

254
255/* Checks case inconsistency, and for non-bech32 chars. */
256static const char *bech32_case_fixup(const tal_t *ctx,
257 const char *codex32str,
258 const char **sep)
259{
260 size_t str_len = strlen(codex32str);
261 char *was_upper_str;
262
263 *sep = NULL;
264
265 /* If first is upper, lower-case the rest */
266 if (cisupper(codex32str[0])) {
267 /* We need a non-const str var, and a flag */
268 was_upper_str = tal_strdup(ctx, codex32str);
269 codex32str = was_upper_str;
270 } else {
271 was_upper_str = NULL;
272 }
273
274 for (size_t i = 0; i < str_len; i++) {
275 int c = codex32str[i];
276 if (c == '1') {
277 /* Two separators? */
278 if (*sep)
279 goto fail;
280 *sep = codex32str + i;
281 continue;
282 }
283 if (c < 0 || c > 128)
284 goto fail;
285 if (was_upper_str) {
286 /* Mixed case not allowed! */
287 if (cislower(c))
288 goto fail;
289 was_upper_str[i] = c = tolower(c);
290 } else {
291 if (cisupper(c))
292 goto fail;
293 }
294 if (bech32_charset_rev[c] == -1)
295 goto fail;
296 }
297
298 return codex32str;
299
300fail:
301 return tal_free(was_upper_str);
302}
303
304/* Return NULL if the codex32 is invalid */
305struct codex32 *codex32_decode(const tal_t *ctx,

Callers 1

codex32_decodeFunction · 0.85

Calls 3

cisupperFunction · 0.85
cislowerFunction · 0.85
tal_freeFunction · 0.85

Tested by

no test coverage detected