This source file includes following definitions.
- modp_b64_encode
- modp_b64_decode
- modp_b64_decode
#include "modp_b64.h"
#include "modp_b64_data.h"
#define BADCHAR 0x01FFFFFF
#define DOPAD 1
#ifndef DOPAD
#undef CHARPAD
#define CHARPAD '\0'
#endif
size_t modp_b64_encode(char* dest, const char* str, size_t len)
{
size_t i = 0;
uint8_t* p = (uint8_t*) dest;
uint8_t t1, t2, t3;
if (len > 2) {
for (; i < len - 2; i += 3) {
t1 = str[i]; t2 = str[i+1]; t3 = str[i+2];
*p++ = e0[t1];
*p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
*p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
*p++ = e2[t3];
}
}
switch (len - i) {
case 0:
break;
case 1:
t1 = str[i];
*p++ = e0[t1];
*p++ = e1[(t1 & 0x03) << 4];
*p++ = CHARPAD;
*p++ = CHARPAD;
break;
default:
t1 = str[i]; t2 = str[i+1];
*p++ = e0[t1];
*p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
*p++ = e2[(t2 & 0x0F) << 2];
*p++ = CHARPAD;
}
*p = '\0';
return p - (uint8_t*)dest;
}
#ifdef WORDS_BIGENDIAN
int modp_b64_decode(char* dest, const char* src, int len)
{
if (len == 0) return 0;
#ifdef DOPAD
if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR;
if (src[len-1] == CHARPAD) {
len--;
if (src[len -1] == CHARPAD) {
len--;
}
}
#endif
size_t i;
int leftover = len % 4;
size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
uint8_t* p = (uint8_t*) dest;
uint32_t x = 0;
uint32_t* destInt = (uint32_t*) p;
uint32_t* srcInt = (uint32_t*) src;
uint32_t y = *srcInt++;
for (i = 0; i < chunks; ++i) {
x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
d2[y >> 8 & 0xff] | d3[y & 0xff];
if (x >= BADCHAR) return MODP_B64_ERROR;
*destInt = x << 8;
p += 3;
destInt = (uint32_t*)p;
y = *srcInt++;
}
switch (leftover) {
case 0:
x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
d2[y >> 8 & 0xff] | d3[y & 0xff];
if (x >= BADCHAR) return MODP_B64_ERROR;
*p++ = ((uint8_t*)&x)[1];
*p++ = ((uint8_t*)&x)[2];
*p = ((uint8_t*)&x)[3];
return (chunks+1)*3;
case 1:
x = d3[y >> 24];
*p = (uint8_t)x;
break;
case 2:
x = d3[y >> 24] *64 + d3[(y >> 16) & 0xff];
*p = (uint8_t)(x >> 4);
break;
default:
x = (d3[y >> 24] *64 + d3[(y >> 16) & 0xff])*64 +
d3[(y >> 8) & 0xff];
*p++ = (uint8_t) (x >> 10);
*p = (uint8_t) (x >> 2);
break;
}
if (x >= BADCHAR) return MODP_B64_ERROR;
return 3*chunks + (6*leftover)/8;
}
#else
size_t modp_b64_decode(char* dest, const char* src, size_t len)
{
if (len == 0) return 0;
#ifdef DOPAD
if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR;
if (src[len-1] == CHARPAD) {
len--;
if (src[len -1] == CHARPAD) {
len--;
}
}
#endif
size_t i;
int leftover = len % 4;
size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
uint8_t* p = (uint8_t*)dest;
uint32_t x = 0;
uint32_t* destInt = (uint32_t*) p;
uint32_t* srcInt = (uint32_t*) src;
uint32_t y = *srcInt++;
for (i = 0; i < chunks; ++i) {
x = d0[y & 0xff] |
d1[(y >> 8) & 0xff] |
d2[(y >> 16) & 0xff] |
d3[(y >> 24) & 0xff];
if (x >= BADCHAR) return MODP_B64_ERROR;
*destInt = x ;
p += 3;
destInt = (uint32_t*)p;
y = *srcInt++;}
switch (leftover) {
case 0:
x = d0[y & 0xff] |
d1[(y >> 8) & 0xff] |
d2[(y >> 16) & 0xff] |
d3[(y >> 24) & 0xff];
if (x >= BADCHAR) return MODP_B64_ERROR;
*p++ = ((uint8_t*)(&x))[0];
*p++ = ((uint8_t*)(&x))[1];
*p = ((uint8_t*)(&x))[2];
return (chunks+1)*3;
break;
case 1:
x = d0[y & 0xff];
*p = *((uint8_t*)(&x));
break;
case 2:
x = d0[y & 0xff] | d1[y >> 8 & 0xff];
*p = *((uint8_t*)(&x));
break;
default:
x = d0[y & 0xff] |
d1[y >> 8 & 0xff ] |
d2[y >> 16 & 0xff];
*p++ = ((uint8_t*)(&x))[0];
*p = ((uint8_t*)(&x))[1];
break;
}
if (x >= BADCHAR) return MODP_B64_ERROR;
return 3*chunks + (6*leftover)/8;
}
#endif