Browse Source
And use it in wireaddr. We fix up the double '.onion' in the test case, which seems like an error? Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>ppa-0.6.1
Rusty Russell
7 years ago
4 changed files with 37 additions and 78 deletions
@ -1,63 +1,24 @@ |
|||
#include <ccan/str/base32/base32.h> |
|||
#include <common/base32.h> |
|||
#include <sys/types.h> |
|||
|
|||
/* This is a rework of what i found on the Net about base32
|
|||
* |
|||
* so Orum (shallot) and Markus Gutschke (Google.inc) should be mentioned here |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
/* We want lower-case conversion please */ |
|||
static const char base32_lower[] = "abcdefghijklmnopqrstuvwxyz234567="; |
|||
|
|||
#define BASE32DATA "abcdefghijklmnopqrstuvwxyz234567" |
|||
|
|||
char *b32_encode(char *dst, u8 * src, u8 ver) |
|||
char *b32_encode(const tal_t *ctx, const void *data, size_t len) |
|||
{ |
|||
u16 byte = 0, poff = 0; |
|||
for (; byte < ((ver == 2) ? 16 : 56); poff += 5) { |
|||
if (poff > 7) { |
|||
poff -= 8; |
|||
src++; |
|||
} |
|||
dst[byte++] = |
|||
BASE32DATA[(htobe16(*(u16 *) src) >> (11 - poff)) & (u16) |
|||
0x001F]; |
|||
} |
|||
dst[byte] = 0; |
|||
return dst; |
|||
} |
|||
char *str = tal_arr(ctx, char, base32_str_size(len)); |
|||
|
|||
//FIXME quiknditry
|
|||
base32_chars = base32_lower; |
|||
base32_encode(data, len, str, tal_len(str)); |
|||
return str; |
|||
} |
|||
|
|||
void b32_decode(u8 * dst, u8 * src, u8 ver) |
|||
u8 *b32_decode(const tal_t *ctx, const char *str, size_t len) |
|||
{ |
|||
int rem = 0; |
|||
int i; |
|||
u8 *p = src; |
|||
int buf; |
|||
u8 ch; |
|||
for (i = 0; i < ((ver == 2) ? 16 : 56); p++) { |
|||
ch = *p; |
|||
buf <<= 5; |
|||
if ((ch >= 'a' && ch <= 'z')) { |
|||
ch = (ch & 0x1F) - 1; |
|||
} else if (ch != '.') { |
|||
ch -= '2' - 0x1A; |
|||
} else return; |
|||
buf = buf | ch; |
|||
rem = rem + 5; |
|||
if (rem >= 8) { |
|||
dst[i++] = buf >> (rem - 8); |
|||
rem -= 8; |
|||
} |
|||
} |
|||
u8 *ret = tal_arr(ctx, u8, base32_data_size(str, len)); |
|||
|
|||
base32_chars = base32_lower; |
|||
if (!base32_decode(str, len, ret, tal_len(ret))) |
|||
return tal_free(ret); |
|||
return ret; |
|||
} |
|||
|
Loading…
Reference in new issue