-
Notifications
You must be signed in to change notification settings - Fork 1
/
z85.c
145 lines (125 loc) · 4.68 KB
/
z85.c
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
142
143
144
145
/*
* Copyright 2013 Stanislav Artemkin <[email protected]>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Implementation of 32/Z85 specification (http://rfc.zeromq.org/spec:32/Z85)
* Source repository: http://github.com/artemkin/z85
*/
#include <assert.h>
#include <limits.h>
#include "z85.h"
typedef unsigned int uint32_t;
typedef unsigned char byte;
// make sure uint32_t is 32-bit
typedef char Z85_uint32_t_static_assert[(sizeof(uint32_t) * CHAR_BIT == 32) * 2 - 1];
#define DIV85_MAGIC 3233857729ULL
// make sure magic constant is 64-bit
typedef char Z85_div85_magic_static_assert[(sizeof(DIV85_MAGIC) * CHAR_BIT == 64) * 2 - 1];
#define DIV85(number) ((uint32_t)((DIV85_MAGIC * number) >> 32) >> 6)
static const char* base85 =
{
"0123456789"
"abcdefghij"
"klmnopqrst"
"uvwxyzABCD"
"EFGHIJKLMN"
"OPQRSTUVWX"
"YZ.-:+=^!/"
"*?&<>()[]{"
"}@%$#"
};
static byte base256[] =
{
0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00,
0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
};
char* Z85_encode_unsafe(const char* source, const char* sourceEnd, char* dest)
{
byte* src = (byte*)source;
byte* end = (byte*)sourceEnd;
byte* dst = (byte*)dest;
uint32_t value;
uint32_t value2;
for (; src != end; src += 4, dst += 5)
{
// unpack big-endian frame
value = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
value2 = DIV85(value); dst[4] = base85[value - value2 * 85]; value = value2;
value2 = DIV85(value); dst[3] = base85[value - value2 * 85]; value = value2;
value2 = DIV85(value); dst[2] = base85[value - value2 * 85]; value = value2;
value2 = DIV85(value); dst[1] = base85[value - value2 * 85];
dst[0] = base85[value2];
}
return (char*)dst;
}
char* Z85_decode_unsafe(const char* source, const char* sourceEnd, char* dest)
{
byte* src = (byte*)source;
byte* end = (byte*)sourceEnd;
byte* dst = (byte*)dest;
uint32_t value;
for (; src != end; src += 5, dst += 4)
{
value = base256[(src[0] - 32) & 127];
value = value * 85 + base256[(src[1] - 32) & 127];
value = value * 85 + base256[(src[2] - 32) & 127];
value = value * 85 + base256[(src[3] - 32) & 127];
value = value * 85 + base256[(src[4] - 32) & 127];
// pack big-endian frame
dst[0] = value >> 24;
dst[1] = (byte)(value >> 16);
dst[2] = (byte)(value >> 8);
dst[3] = (byte)(value);
}
return (char*)dst;
}
size_t Z85_encode(const char* source, char* dest, size_t inputSize)
{
if (!source || !dest || inputSize % 4)
{
assert(!"wrong source, destination or input size");
return 0;
}
return Z85_encode_unsafe(source, source + inputSize, dest) - dest;
}
size_t Z85_decode(const char* source, char* dest, size_t inputSize)
{
if (!source || !dest || inputSize % 5)
{
assert(!"wrong source, destination or input size");
return 0;
}
return Z85_decode_unsafe(source, source + inputSize, dest) - dest;
}