summaryrefslogtreecommitdiff
path: root/src/libotutil/zbase32.c
blob: b92168b4441f81eff09b65c36ae8ad8daf5a93a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
 * copyright 2002, 2003 Bryce "Zooko" Wilcox-O'Hearn
 * mailto:zooko@zooko.com
 *
 * See the end of this file for the free software, open source license (BSD-style).
 */
#include "zbase32.h"

#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h> /* XXX only for debug printfs */

static const char*const chars="ybndrfg8ejkmcpqxot1uwisza345h769";

/* Types from zstr */
/**
 * A zstr is simply an unsigned int length and a pointer to a buffer of
 * unsigned chars.
 */
typedef struct {
	size_t len; /* the length of the string (not counting the null-terminating character) */
	unsigned char* buf; /* pointer to the first byte */
} zstr;

/**
 * A zstr is simply an unsigned int length and a pointer to a buffer of
 * const unsigned chars.
 */
typedef struct {
	size_t len; /* the length of the string (not counting the null-terminating character) */
	const unsigned char* buf; /* pointer to the first byte */
} czstr;

/* Functions from zstr */
static zstr
new_z(const size_t len)
{
	zstr result;
	result.buf = (unsigned char *)malloc(len+1);
	if (result.buf == NULL) {
		result.len = 0;
		return result;
	}
	result.len = len;
	result.buf[len] = '\0';
	return result;
}

/* Functions from zutil */
static size_t
divceil(size_t n, size_t d)
{
	return n/d+((n%d)!=0);
}

static zstr b2a_l_extra_Duffy(const czstr os, const size_t lengthinbits)
{
	zstr result = new_z(divceil(os.len*8, 5)); /* if lengthinbits is not a multiple of 8 then this is allocating space for 0, 1, or 2 extra quintets that will be truncated at the end of this function if they are not needed */
	if (result.buf == NULL)
		return result;

	unsigned char* resp = result.buf + result.len; /* pointer into the result buffer, initially pointing to the "one-past-the-end" quintet */
	const unsigned char* osp = os.buf + os.len; /* pointer into the os buffer, initially pointing to the "one-past-the-end" octet */

	/* Now this is a real live Duff's device.  You gotta love it. */
	unsigned long x=0; /* to hold up to 32 bits worth of the input */
	switch ((osp - os.buf) % 5) {
	case 0:
		do {
			x = *--osp;
			*--resp = chars[x % 32]; /* The least sig 5 bits go into the final quintet. */
			x /= 32; /* ... now we have 3 bits worth in x... */
	case 4:
			x |= ((unsigned long)(*--osp)) << 3; /* ... now we have 11 bits worth in x... */
			*--resp = chars[x % 32];
			x /= 32; /* ... now we have 6 bits worth in x... */
			*--resp = chars[x % 32];
			x /= 32; /* ... now we have 1 bits worth in x... */
	case 3:
			x |= ((unsigned long)(*--osp)) << 1; /* The 8 bits from the 2-indexed octet.  So now we have 9 bits worth in x... */
			*--resp = chars[x % 32];
			x /= 32; /* ... now we have 4 bits worth in x... */
	case 2:
			x |= ((unsigned long)(*--osp)) << 4; /* The 8 bits from the 1-indexed octet.  So now we have 12 bits worth in x... */
			*--resp = chars[x%32];
			x /= 32; /* ... now we have 7 bits worth in x... */
			*--resp = chars[x%32];
			x /= 32; /* ... now we have 2 bits worth in x... */
	case 1:
			x |= ((unsigned long)(*--osp)) << 2; /* The 8 bits from the 0-indexed octet.  So now we have 10 bits worth in x... */
			*--resp = chars[x%32];
			x /= 32; /* ... now we have 5 bits worth in x... */
			*--resp = chars[x];
		} while (osp > os.buf);
	} /* switch ((osp - os.buf) % 5) */

	/* truncate any unused trailing zero quintets */
	result.len = divceil(lengthinbits, 5);
	result.buf[result.len] = '\0';
	return result;
}

static zstr b2a_l(const czstr os, const size_t lengthinbits)
{
		return b2a_l_extra_Duffy(os, lengthinbits);
}

static zstr b2a(const czstr os)
{
	return b2a_l(os, os.len*8);
}

char *
zbase32_encode(const unsigned char *data, size_t length)
{
	czstr input = { length, data };
	zstr output = b2a(input);
	return (char *)output.buf;
}

/**
 * Copyright (c) 2002 Bryce "Zooko" Wilcox-O'Hearn
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software to deal in this software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this software, and to permit
 * persons to whom this software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of this software.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THIS SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THIS SOFTWARE.
 */