Skip to content

Commit 62c05c0

Browse files
committed
Apply 0004-Add-encoding-routines-for-base64-without-whitespace-.patch
1 parent b11a124 commit 62c05c0

File tree

4 files changed

+225
-5
lines changed

4 files changed

+225
-5
lines changed

src/common/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ override CPPFLAGS += -DVAL_LDFLAGS_EX="\"$(LDFLAGS_EX)\""
3636
override CPPFLAGS += -DVAL_LDFLAGS_SL="\"$(LDFLAGS_SL)\""
3737
override CPPFLAGS += -DVAL_LIBS="\"$(LIBS)\""
3838

39-
OBJS_COMMON = config_info.o controldata_utils.o exec.o keywords.o \
40-
pg_lzcompress.o pgfnames.o psprintf.o relpath.o rmtree.o \
41-
string.o username.o wait_error.o
42-
39+
OBJS_COMMON = base64.o config_info.o controldata_utils.o exec.o \
40+
keywords.o pg_lzcompress.o pgfnames.o psprintf.o relpath.o \
41+
rmtree.o string.o username.o wait_error.o
42+
4343
ifeq ($(with_openssl),yes)
4444
OBJS_COMMON += sha2_openssl.o
4545
else

src/common/base64.c

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* base64.c
4+
* Set of encoding and decoding routines for base64 without support
5+
* for whitespace. In case of failure, those routines return -1 in case
6+
* of error to let the caller do any error handling.
7+
*
8+
* Copyright (c) 2001-2016, PostgreSQL Global Development Group
9+
*
10+
*
11+
* IDENTIFICATION
12+
* src/common/base64.c
13+
*
14+
*-------------------------------------------------------------------------
15+
*/
16+
17+
#ifndef FRONTEND
18+
#include "postgres.h"
19+
#else
20+
#include "postgres_fe.h"
21+
#endif
22+
23+
#include "common/base64.h"
24+
25+
/*
26+
* BASE64
27+
*/
28+
29+
static const char _base64[] =
30+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31+
32+
static const int8 b64lookup[128] = {
33+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
36+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
37+
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
38+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
39+
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
40+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
41+
};
42+
43+
/*
44+
* pg_b64_encode
45+
*
46+
* Encode into base64 the given string. Returns the length of the encoded
47+
* string on success, and -1 in the event of an error.
48+
*/
49+
int
50+
pg_b64_encode(const char *src, int len, char *dst)
51+
{
52+
char *p;
53+
const char *s,
54+
*end = src + len;
55+
int pos = 2;
56+
uint32 buf = 0;
57+
58+
s = src;
59+
p = dst;
60+
61+
while (s < end)
62+
{
63+
buf |= (unsigned char) *s << (pos << 3);
64+
pos--;
65+
s++;
66+
67+
/* write it out */
68+
if (pos < 0)
69+
{
70+
*p++ = _base64[(buf >> 18) & 0x3f];
71+
*p++ = _base64[(buf >> 12) & 0x3f];
72+
*p++ = _base64[(buf >> 6) & 0x3f];
73+
*p++ = _base64[buf & 0x3f];
74+
75+
pos = 2;
76+
buf = 0;
77+
}
78+
}
79+
if (pos != 2)
80+
{
81+
*p++ = _base64[(buf >> 18) & 0x3f];
82+
*p++ = _base64[(buf >> 12) & 0x3f];
83+
*p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '=';
84+
*p++ = '=';
85+
}
86+
87+
return p - dst;
88+
}
89+
90+
/*
91+
* pg_b64_decode
92+
*
93+
* Decode the given base64 string. Returns the length of the decoded
94+
* string on success, and -1 in the event of an error.
95+
*/
96+
int
97+
pg_b64_decode(const char *src, int len, char *dst)
98+
{
99+
const char *srcend = src + len,
100+
*s = src;
101+
char *p = dst;
102+
char c;
103+
int b = 0;
104+
uint32 buf = 0;
105+
int pos = 0,
106+
end = 0;
107+
108+
while (s < srcend)
109+
{
110+
c = *s++;
111+
112+
/* Leave if a whitespace is found */
113+
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
114+
return -1;
115+
116+
if (c == '=')
117+
{
118+
/* end sequence */
119+
if (!end)
120+
{
121+
if (pos == 2)
122+
end = 1;
123+
else if (pos == 3)
124+
end = 2;
125+
else
126+
{
127+
/*
128+
* Unexpected "=" character found while decoding base64
129+
* sequence.
130+
*/
131+
return -1;
132+
}
133+
}
134+
b = 0;
135+
}
136+
else
137+
{
138+
b = -1;
139+
if (c > 0 && c < 127)
140+
b = b64lookup[(unsigned char) c];
141+
if (b < 0)
142+
{
143+
/* invalid symbol found */
144+
return -1;
145+
}
146+
}
147+
/* add it to buffer */
148+
buf = (buf << 6) + b;
149+
pos++;
150+
if (pos == 4)
151+
{
152+
*p++ = (buf >> 16) & 255;
153+
if (end == 0 || end > 1)
154+
*p++ = (buf >> 8) & 255;
155+
if (end == 0 || end > 2)
156+
*p++ = buf & 255;
157+
buf = 0;
158+
pos = 0;
159+
}
160+
}
161+
162+
if (pos != 0)
163+
{
164+
/*
165+
* base64 end sequence is invalid. Input data is missing padding,
166+
* is truncated or is otherwise corrupted.
167+
*/
168+
return -1;
169+
}
170+
171+
return p - dst;
172+
}
173+
174+
/*
175+
* pg_b64_enc_len
176+
*
177+
* Returns to caller the length of the string if it were encoded with
178+
* base64 based on the length provided by caller. This is useful to
179+
* estimate how large a buffer allocation needs to be done before doing
180+
* the actual encoding.
181+
*/
182+
int
183+
pg_b64_enc_len(int srclen)
184+
{
185+
/* 3 bytes will be converted to 4 */
186+
return (srclen + 2) * 4 / 3;
187+
}
188+
189+
/*
190+
* pg_b64_dec_len
191+
*
192+
* Returns to caller the length of the string if it were to be decoded
193+
* with base64, based on the length given by caller. This is useful to
194+
* estimate how large a buffer allocation needs to be done before doing
195+
* the actual decoding.
196+
*/
197+
int
198+
pg_b64_dec_len(int srclen)
199+
{
200+
return (srclen * 3) >> 2;
201+
}

src/include/common/base64.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* base64.h
3+
* Encoding and decoding routines for base64 without whitespace
4+
* support.
5+
*
6+
* Portions Copyright (c) 2001-2016, PostgreSQL Global Development Group
7+
*
8+
* src/include/common/base64.h
9+
*/
10+
#ifndef BASE64_H
11+
#define BASE64_H
12+
13+
/* base 64 */
14+
int pg_b64_encode(const char *src, int len, char *dst);
15+
int pg_b64_decode(const char *src, int len, char *dst);
16+
int pg_b64_enc_len(int srclen);
17+
int pg_b64_dec_len(int srclen);
18+
19+
#endif /* BASE64_H */

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ sub mkvcbuild
113113
}
114114

115115
our @pgcommonallfiles = qw(
116-
config_info.c controldata_utils.c exec.c keywords.c
116+
base64.c config_info.c controldata_utils.c exec.c keywords.c
117117
pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
118118
string.c username.c wait_error.c);
119119

0 commit comments

Comments
 (0)