50 #include <sys/types.h> 55 static const char Base64[] =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57 static const char Pad64 =
'=';
124 unsigned
char const *src;
129 size_t datalength = 0;
130 unsigned char input[3];
131 unsigned char output[4];
134 while (2 < srclength) {
140 output[0] = input[0] >> 2;
141 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
142 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
143 output[3] = input[2] & 0x3f;
145 if (datalength + 4 > targsize)
147 target[datalength++] = Base64[output[0]];
148 target[datalength++] = Base64[output[1]];
149 target[datalength++] = Base64[output[2]];
150 target[datalength++] = Base64[output[3]];
154 if (0 != srclength) {
156 input[0] = input[1] = input[2] =
'\0';
157 for (i = 0; i < srclength; i++)
160 output[0] = input[0] >> 2;
161 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
162 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
164 if (datalength + 4 > targsize)
166 target[datalength++] = Base64[output[0]];
167 target[datalength++] = Base64[output[1]];
169 target[datalength++] = Pad64;
171 target[datalength++] = Base64[output[2]];
172 target[datalength++] = Pad64;
174 if (datalength >= targsize)
176 target[datalength] =
'\0';
177 return (
int)(datalength);
189 unsigned char *target;
193 unsigned int tarindex;
194 unsigned char nextbyte;
200 while ((ch = (
unsigned char)*src++) !=
'\0') {
207 pos = strchr(Base64, ch);
214 if (tarindex >= targsize)
216 target[tarindex] = (pos - Base64) << 2;
222 if (tarindex >= targsize)
224 target[tarindex] |= (pos - Base64) >> 4;
225 nextbyte = ((pos - Base64) & 0x0f) << 4;
226 if (tarindex + 1 < targsize)
227 target[tarindex+1] = nextbyte;
236 if (tarindex >= targsize)
238 target[tarindex] |= (pos - Base64) >> 2;
239 nextbyte = ((pos - Base64) & 0x03) << 6;
240 if (tarindex + 1 < targsize)
241 target[tarindex+1] = nextbyte;
250 if (tarindex >= targsize)
252 target[tarindex] |= (pos - Base64);
266 ch = (
unsigned char)*src++;
274 for (; ch !=
'\0'; ch = (
unsigned char)*src++)
280 ch = (
unsigned char)*src++;
289 for (; ch !=
'\0'; ch = (
unsigned char)*src++)
299 if (target && tarindex < targsize &&
300 target[tarindex] != 0)
int libscrypt_b64_decode(char const *src, unsigned char *target, size_t targsize)
int libscrypt_b64_encode(unsigned char const *src, size_t srclength, char *target, size_t targsize)
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)