Skip to content

Commit fa7210b

Browse files
committed
Base64 implementation code format, refs #9
1 parent 4d0b6e6 commit fa7210b

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/base64.h

+28-30
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,27 @@ size_t base64_encode(char *dst, const char *src, size_t src_len)
5151
const unsigned char *basis64 = base64_default_tables.encode;
5252

5353
while (src_len > 2) {
54-
*d++ = basis64[(s[0] >> 2) & 0x3f];
55-
*d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
56-
*d++ = basis64[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
57-
*d++ = basis64[s[2] & 0x3f];
54+
*d++ = basis64[(s[0] >> 2) & 0x3f];
55+
*d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
56+
*d++ = basis64[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
57+
*d++ = basis64[s[2] & 0x3f];
5858

59-
s += 3;
60-
src_len -= 3;
59+
s += 3;
60+
src_len -= 3;
6161
}
6262

6363
if (src_len) {
64-
*d++ = basis64[(s[0] >> 2) & 0x3f];
65-
66-
if (src_len == 1) {
67-
*d++ = basis64[(s[0] & 3) << 4];
68-
*d++ = base64_default_tables.padding;
69-
70-
} else {
71-
*d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
72-
*d++ = basis64[(s[1] & 0x0f) << 2];
73-
}
64+
*d++ = basis64[(s[0] >> 2) & 0x3f];
7465

66+
if (src_len == 1) {
67+
*d++ = basis64[(s[0] & 3) << 4];
7568
*d++ = base64_default_tables.padding;
69+
} else {
70+
*d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
71+
*d++ = basis64[(s[1] & 0x0f) << 2];
72+
}
73+
74+
*d++ = base64_default_tables.padding;
7675
}
7776

7877
return (d - dst);
@@ -86,35 +85,34 @@ size_t base64_decode(char *dst, const char *src, size_t src_len)
8685
const unsigned char *basis = base64_default_tables.decode;
8786

8887
for (len = 0; len < src_len; len++) {
89-
9088
if (s[len] == base64_default_tables.padding) {
91-
break;
92-
}
89+
break;
90+
}
9391

94-
if (basis[s[len]] == 77) {
95-
return 0;
96-
}
92+
if (basis[s[len]] == 77) {
93+
return 0;
94+
}
9795
}
9896

9997
if (len % 4 == 1) {
100-
return 0;
98+
return 0;
10199
}
102100

103101
while (len > 3) {
104-
*d++ = (char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
105-
*d++ = (char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
106-
*d++ = (char) (basis[s[2]] << 6 | basis[s[3]]);
102+
*d++ = (char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
103+
*d++ = (char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
104+
*d++ = (char) (basis[s[2]] << 6 | basis[s[3]]);
107105

108-
s += 4;
109-
len -= 4;
106+
s += 4;
107+
len -= 4;
110108
}
111109

112110
if (len > 1) {
113-
*d++ = (char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
111+
*d++ = (char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
114112
}
115113

116114
if (len > 2) {
117-
*d++ = (char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
115+
*d++ = (char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
118116
}
119117

120118
return (d - dst);

0 commit comments

Comments
 (0)