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

Function decode_payload

common/codex32.c:212–253  ·  view source on GitHub ↗

Helper to fetch data from payload as a valid hex buffer */

Source from the content-addressed store, hash-verified

210
211/* Helper to fetch data from payload as a valid hex buffer */
212static const u8 *decode_payload(const tal_t *ctx, const char *payload, size_t payload_len)
213{
214 u8 *ret = tal_arr(ctx, u8, (payload_len * 5 + 7) / 8);
215 uint8_t next_byte = 0;
216 uint8_t rem = 0;
217 size_t j = 0;
218
219 /* We have already checked this is a valid bech32 string! */
220 for (size_t i = 0; i < payload_len; i++) {
221 int ch = payload[i];
222 uint8_t fe = bech32_charset_rev[ch];
223
224 if (rem < 3) {
225 // If we are within 3 bits of the start we can fit the whole next char in
226 next_byte |= fe << (3 - rem);
227 }
228 else if (rem == 3) {
229 // If we are exactly 3 bits from the start then this char fills in the byte
230 ret[j++] = next_byte | fe;
231 next_byte = 0;
232 }
233 else { // rem > 3
234 // Otherwise we have to break it in two
235 u8 overshoot = rem - 3;
236 assert(overshoot > 0);
237 ret[j++] = next_byte | (fe >> overshoot);
238 next_byte = fe << (8 - overshoot);
239 }
240
241 rem = (rem + 5) % 8;
242 }
243
244 /* BIP-93:
245 * Any incomplete group at the end MUST be 4 bits or less, and is discarded.
246 */
247 if (rem > 4)
248 return tal_free(ret);
249
250 /* As a result, we often don't use the final byte */
251 tal_resize(&ret, j);
252 return ret;
253}
254
255/* Checks case inconsistency, and for non-bech32 chars. */
256static const char *bech32_case_fixup(const tal_t *ctx,

Callers 1

codex32_decodeFunction · 0.85

Calls 1

tal_freeFunction · 0.85

Tested by

no test coverage detected