-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodp_b85.c
161 lines (139 loc) · 4 KB
/
modp_b85.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* \file
* <PRE>
* MODP_B85 - High performance base85 encoder/decoder
* https://github.com/client9/stringencoders/
*
* Copyright © 2006-2016 Nick Galbreath -- nickg [at] client9 [dot] com
* Modified by spacewander to make it compatible with Go's encoding/ascii85
* All rights reserved.
* Released under MIT license. See LICENSE for details.
* </pre>
*/
#include "modp_stdint.h"
/* private header */
#include "modp_b85_data.h"
#define unlikely(x) __builtin_expect(!!(x), 0)
/*
* Might need changing depending on platform
* we need htonl, and ntohl
*/
#include <arpa/inet.h>
/**
* you can decode IN PLACE!
* no memory allocated
*/
size_t modp_b85_decode(char* out, const char* data, size_t len)
{
size_t i;
int j;
uint32_t tmp = 0;
uint32_t digit;
uint32_t* out_block = (uint32_t*)out;
uint32_t* origin_out = out_block;
const uint8_t* src = (const uint8_t*)data;
uint8_t ch;
for (i = 0, j = 0; i < len; ++i) {
ch = *src;
src++;
/* skip space and control characters */
if (unlikely(ch <= ' ')) {
continue;
}
if (unlikely(ch == 'z')) {
*out_block++ = 0;
continue;
}
digit = gsCharToInt[(uint32_t)ch];
if (unlikely(digit >= 85)) {
return (size_t)-1;
}
tmp = tmp * 85 + digit;
++j;
if (j == 5) {
*out_block++ = ntohl(tmp);
tmp = 0;
j = 0;
}
}
if (j != 0) {
if (j == 1) {
return (size_t)-1;
}
for (i = j; i < 5; i++) {
/* padding with 'u' */
tmp = tmp * 85 + 84;
}
*out_block = ntohl(tmp);
return 4*(out_block - origin_out) + j - 1;
}
return 4*(out_block - origin_out);
}
/**
* src != out
*/
size_t modp_b85_encode(char* out, const char* src, size_t len)
{
size_t i;
uint32_t tmp;
char* dst;
const uint32_t* sary = (const uint32_t*)src;
const size_t buckets = len / 4;
uint32_t remain = len % 4;
char* origin_out = out;
for (i = 0; i < buckets; ++i) {
tmp = *sary++;
tmp = htonl(tmp);
if (unlikely(tmp == 0)) {
*out++ = 'z';
} else {
/* this crazy function */
#if 1
*out++ = (char)gsIntToChar[(tmp / 52200625)]; /* don't need % 85 here, always < 85 */
*out++ = (char)gsIntToChar[(tmp / 614125) % 85];
*out++ = (char)gsIntToChar[(tmp / 7225) % 85];
*out++ = (char)gsIntToChar[(tmp / 85) % 85];
*out++ = (char)gsIntToChar[tmp % 85];
#else
/* is really this */
*(out + 4) = gsIntToChar[tmp % 85];
tmp /= 85;
*(out + 3) = gsIntToChar[tmp % 85];
tmp /= 85;
*(out + 2) = gsIntToChar[tmp % 85];
tmp /= 85;
*(out + 1) = gsIntToChar[tmp % 85];
tmp /= 85;
*out = gsIntToChar[tmp];
out += 5;
#endif
}
/* NOTES
* Version 1 under -O3 is about 10-20 PERCENT faster than version 2
* BUT Version 1 is 10 TIMES SLOWER when used with -Os !!!
* Reason: gcc does a lot of tricks to remove the divisions
* op with multiplies and shift.
* In V1 with -O3 this works. Under -Os it reverts to very
* slow division.
* In V2 -O3 it does the same thing, but under Os, it's smart
* enough to know we want the quotient and remainder and only
* one div call per line.
*/
}
if (remain > 0) {
src = (const char *)sary;
tmp = 0;
dst = (char *)&tmp;
for (i = 1; i <= remain; ++i) {
*dst++ = *src++;
}
tmp = htonl(tmp);
*out++ = (char)gsIntToChar[(tmp / 52200625)];
*out++ = (char)gsIntToChar[(tmp / 614125) % 85];
*out++ = (char)gsIntToChar[(tmp / 7225) % 85];
*out++ = (char)gsIntToChar[(tmp / 85) % 85];
*out++ = (char)gsIntToChar[tmp % 85];
return out - origin_out - (4 - remain);
}
return out - origin_out;
}