PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
base64.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

pg_nodiscard int pg_b64_encode (const char *src, int len, char *dst, int dstlen)
 
pg_nodiscard int pg_b64_decode (const char *src, int len, char *dst, int dstlen)
 
int pg_b64_enc_len (int srclen)
 
int pg_b64_dec_len (int srclen)
 

Function Documentation

◆ pg_b64_dec_len()

int pg_b64_dec_len ( int  srclen)

◆ pg_b64_decode()

pg_nodiscard int pg_b64_decode ( const char *  src,
int  len,
char *  dst,
int  dstlen 
)

Definition at line 116 of file base64.c.

117{
118 const char *srcend = src + len,
119 *s = src;
120 char *p = dst;
121 char c;
122 int b = 0;
123 uint32 buf = 0;
124 int pos = 0,
125 end = 0;
126
127 while (s < srcend)
128 {
129 c = *s++;
130
131 /* Leave if a whitespace is found */
132 if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
133 goto error;
134
135 if (c == '=')
136 {
137 /* end sequence */
138 if (!end)
139 {
140 if (pos == 2)
141 end = 1;
142 else if (pos == 3)
143 end = 2;
144 else
145 {
146 /*
147 * Unexpected "=" character found while decoding base64
148 * sequence.
149 */
150 goto error;
151 }
152 }
153 b = 0;
154 }
155 else
156 {
157 b = -1;
158 if (c > 0 && c < 127)
159 b = b64lookup[(unsigned char) c];
160 if (b < 0)
161 {
162 /* invalid symbol found */
163 goto error;
164 }
165 }
166 /* add it to buffer */
167 buf = (buf << 6) + b;
168 pos++;
169 if (pos == 4)
170 {
171 /*
172 * Leave if there is an overflow in the area allocated for the
173 * decoded string.
174 */
175 if ((p - dst + 1) > dstlen)
176 goto error;
177 *p++ = (buf >> 16) & 255;
178
179 if (end == 0 || end > 1)
180 {
181 /* overflow check */
182 if ((p - dst + 1) > dstlen)
183 goto error;
184 *p++ = (buf >> 8) & 255;
185 }
186 if (end == 0 || end > 2)
187 {
188 /* overflow check */
189 if ((p - dst + 1) > dstlen)
190 goto error;
191 *p++ = buf & 255;
192 }
193 buf = 0;
194 pos = 0;
195 }
196 }
197
198 if (pos != 0)
199 {
200 /*
201 * base64 end sequence is invalid. Input data is missing padding, is
202 * truncated or is otherwise corrupted.
203 */
204 goto error;
205 }
206
207 Assert((p - dst) <= dstlen);
208 return p - dst;
209
210error:
211 memset(dst, 0, dstlen);
212 return -1;
213}
static const int8 b64lookup[128]
Definition: base64.c:30
uint32_t uint32
Definition: c.h:502
Assert(PointerIsAligned(start, uint64))
int b
Definition: isn.c:69
const void size_t len
static char * buf
Definition: pg_test_fsync.c:72
char * c
static void error(void)
Definition: sql-dyntest.c:147

References Assert(), b, b64lookup, buf, error(), and len.

Referenced by parse_scram_secret(), pqConnectOptions2(), read_client_final_message(), read_server_final_message(), read_server_first_message(), and scram_verify_plain_password().

◆ pg_b64_enc_len()

int pg_b64_enc_len ( int  srclen)

Definition at line 224 of file base64.c.

225{
226 /* 3 bytes will be converted to 4 */
227 return (srclen + 2) / 3 * 4;
228}

Referenced by build_client_final_message(), build_client_first_message(), build_server_final_message(), build_server_first_message(), connect_pg_server(), mock_scram_secret(), read_client_final_message(), and scram_build_secret().

◆ pg_b64_encode()

pg_nodiscard int pg_b64_encode ( const char *  src,
int  len,
char *  dst,
int  dstlen 
)

Definition at line 49 of file base64.c.

50{
51 char *p;
52 const char *s,
53 *end = src + len;
54 int pos = 2;
55 uint32 buf = 0;
56
57 s = src;
58 p = dst;
59
60 while (s < end)
61 {
62 buf |= (unsigned char) *s << (pos << 3);
63 pos--;
64 s++;
65
66 /* write it out */
67 if (pos < 0)
68 {
69 /*
70 * Leave if there is an overflow in the area allocated for the
71 * encoded string.
72 */
73 if ((p - dst + 4) > dstlen)
74 goto error;
75
76 *p++ = _base64[(buf >> 18) & 0x3f];
77 *p++ = _base64[(buf >> 12) & 0x3f];
78 *p++ = _base64[(buf >> 6) & 0x3f];
79 *p++ = _base64[buf & 0x3f];
80
81 pos = 2;
82 buf = 0;
83 }
84 }
85 if (pos != 2)
86 {
87 /*
88 * Leave if there is an overflow in the area allocated for the encoded
89 * string.
90 */
91 if ((p - dst + 4) > dstlen)
92 goto error;
93
94 *p++ = _base64[(buf >> 18) & 0x3f];
95 *p++ = _base64[(buf >> 12) & 0x3f];
96 *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '=';
97 *p++ = '=';
98 }
99
100 Assert((p - dst) <= dstlen);
101 return p - dst;
102
103error:
104 memset(dst, 0, dstlen);
105 return -1;
106}
static const char _base64[]
Definition: base64.c:27

References _base64, Assert(), buf, error(), and len.

Referenced by build_client_final_message(), build_client_first_message(), build_server_final_message(), build_server_first_message(), connect_pg_server(), mock_scram_secret(), read_client_final_message(), and scram_build_secret().