795 INPUT_ITERATOR begin,
802 if (e_ERROR_STATE == d_state || e_DONE_STATE == d_state) {
803 int rv = e_DONE_STATE == d_state ? -2 : -1;
804 d_state = e_ERROR_STATE;
814 while (8 <= d_bitsInStack && numEmitted != maxNumOut) {
816 *out =
static_cast<char>((d_stack >> d_bitsInStack) & 0xff);
825 if (e_INPUT_STATE == d_state) {
826 while (18 >= d_bitsInStack && begin != end) {
827 const unsigned char byte =
static_cast<unsigned char>(*begin);
832 unsigned char converted =
static_cast<unsigned char>(
835 if (converted < 64) {
836 d_stack = (d_stack << 6) | converted;
838 if (8 <= d_bitsInStack && numEmitted != maxNumOut) {
840 *out =
static_cast<char>(
841 (d_stack >> d_bitsInStack) & 0xff);
846 else if (!d_ignorable_p[
byte]) {
847 if (
'=' ==
byte && d_isPadded) {
848 const int residual = residualBits(
849 d_outputLength + numEmitted);
867 const int leftOver = residual % 8;
868 d_state = 0 != (d_stack & ((1 << leftOver) - 1))
875 d_stack >>= leftOver;
876 d_bitsInStack -= leftOver;
879 d_state = e_ERROR_STATE;
886 if (e_NEED_EQUAL_STATE == d_state) {
889 while (begin != end) {
890 const unsigned char byte =
static_cast<unsigned char>(*begin);
895 if (!d_ignorable_p[
byte]) {
897 d_state = e_SOFT_DONE_STATE;
900 d_state = e_ERROR_STATE;
906 if (e_SOFT_DONE_STATE == d_state) {
907 while (begin != end) {
908 const unsigned char byte =
static_cast<unsigned char>(*begin);
913 if (!d_ignorable_p[
byte]) {
914 d_state = e_ERROR_STATE;
920 *numOut = numEmitted;
921 d_outputLength += numEmitted;
923 return e_ERROR_STATE == d_state ? -1 : d_bitsInStack / 8;
928int Base64Decoder::convert<char *, const char *>(
940 e_ERROR_STATE == d_state || e_DONE_STATE == d_state)) {
941 int rv = e_DONE_STATE == d_state ? -2 : -1;
942 d_state = e_ERROR_STATE;
953 while (8 <= d_bitsInStack && numEmitted != maxNumOut) {
955 *out =
static_cast<char>((d_stack >> d_bitsInStack) & 0xff);
963 const char *originalBegin = begin;
971 const __m128i *alphabetSlices =
972 reinterpret_cast<const __m128i *
>(d_alphabet_p);
973 __m128i lut5 = _mm_loadu_si128(alphabetSlices + 7);
974 __m128i lut4 = _mm_loadu_si128(alphabetSlices + 6);
975 __m128i lut3 = _mm_loadu_si128(alphabetSlices + 5);
976 __m128i lut2 = _mm_loadu_si128(alphabetSlices + 4);
977 __m128i lut1 = _mm_loadu_si128(alphabetSlices + 3);
978 __m128i lut0 = _mm_loadu_si128(alphabetSlices + 2);
984 lut5 = _mm_xor_si128(lut5, lut4);
985 lut4 = _mm_xor_si128(lut4, lut3);
986 lut3 = _mm_xor_si128(lut3, lut2);
987 lut2 = _mm_xor_si128(lut2, lut1);
988 lut1 = _mm_xor_si128(lut1, lut0);
990 while (end - begin >= 16 &&
static_cast<unsigned>(numEmitted + 12)
991 <=
static_cast<unsigned>(maxNumOut)) {
994 __m128i x = _mm_loadu_si128(
995 reinterpret_cast<const __m128i *
>(begin));
998 x = _mm_subs_epi8(x, _mm_set1_epi8(0x20));
1002 __m128i tooSmall = x;
1008 __m128i decoded = _mm_shuffle_epi8(lut0, x);
1014 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
1023 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut1, x));
1026 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
1027 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut2, x));
1028 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
1029 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut3, x));
1030 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
1031 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut4, x));
1032 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
1033 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut5, x));
1034 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
1043 !_mm_testz_si128(tooSmall | decoded | ~x,
1044 _mm_set1_epi8(
static_cast<char>(0x80))))) {
1053 decoded = _mm_maddubs_epi16(decoded, _mm_set1_epi16(0x0140));
1059 decoded = _mm_madd_epi16(decoded, _mm_set1_epi32(0x00011000));
1063 __m128i selection = _mm_set_epi64(
1064 reinterpret_cast<__m64
>(0xffffffff0c0d0e08ull),
1065 reinterpret_cast<__m64
>(0x090a040506000102ull));
1066 decoded = _mm_shuffle_epi8(decoded, selection);
1069 memcpy(out, &decoded, 12);
1076 while (end - begin >= 4 &&
static_cast<unsigned>(numEmitted + 3)
1077 <=
static_cast<unsigned>(maxNumOut)) {
1079 uint8_t *in =
reinterpret_cast<uint8_t *
>(inBuffer.
buffer());
1080 memcpy(in, begin, 4);
1083 x[0] =
static_cast<uint8_t
>(d_alphabet_p[in[0]]);
1084 x[1] =
static_cast<uint8_t
>(d_alphabet_p[in[1]]);
1085 x[2] =
static_cast<uint8_t
>(d_alphabet_p[in[2]]);
1086 x[3] =
static_cast<uint8_t
>(d_alphabet_p[in[3]]);
1089 memcpy(&x4, x,
sizeof(x4));
1096 out[0] =
static_cast<char>((x[0] << 2) | (x[1] >> 4));
1097 out[1] =
static_cast<char>((x[1] << 4) | (x[2] >> 2));
1098 out[2] =
static_cast<char>((x[2] << 6) | (x[3] >> 0));
1106 while (18 >= d_bitsInStack && begin != end) {
1107 const unsigned char byte =
static_cast<unsigned char>(*begin);
1111 unsigned char converted =
static_cast<unsigned char>(
1112 d_alphabet_p[byte]);
1114 if (converted < 64) {
1115 d_stack = (d_stack << 6) | converted;
1117 if (8 <= d_bitsInStack && numEmitted != maxNumOut) {
1119 *out =
static_cast<char>(
1120 (d_stack >> d_bitsInStack) & 0xff);
1125 else if (!d_ignorable_p[
byte]) {
1126 if (
'=' ==
byte && d_isPadded) {
1127 const int residual = residualBits(
1128 d_outputLength + numEmitted);
1146 const int leftOver = residual % 8;
1147 d_state = 0 != (d_stack & ((1 << leftOver) - 1))
1150 ? e_NEED_EQUAL_STATE
1154 d_stack >>= leftOver;
1155 d_bitsInStack -= leftOver;
1158 d_state = e_ERROR_STATE;
1165 if (e_NEED_EQUAL_STATE == d_state) {
1168 while (begin != end) {
1169 const unsigned char byte =
static_cast<unsigned char>(*begin);
1173 if (!d_ignorable_p[
byte]) {
1175 d_state = e_SOFT_DONE_STATE;
1178 d_state = e_ERROR_STATE;
1187 const unsigned char byte =
static_cast<unsigned char>(*begin);
1192 d_state = e_ERROR_STATE;
1195 }
while (begin != end);
1198 *numIn =
static_cast<int>(begin - originalBegin);
1199 *numOut = numEmitted;
1200 d_outputLength += numEmitted;
1202 return e_ERROR_STATE == d_state ? -1 : d_bitsInStack / 8;
1239 if (!d_isPadded && e_INPUT_STATE == d_state) {
1240 const int residual = residualBits(d_outputLength);
1241 const int leftOver = residual % 8;
1243 0 != (d_stack & ((1 << leftOver) - 1)))) {
1244 d_state = e_ERROR_STATE;
1249 d_stack >>= leftOver;
1250 d_bitsInStack -= leftOver;
1254 if (e_ERROR_STATE == d_state || e_NEED_EQUAL_STATE == d_state ||
1255 (e_DONE_STATE == d_state && 0 == d_bitsInStack) ||
1256 (d_isPadded && e_INPUT_STATE == d_state &&
1257 0 != residualBits(d_outputLength))) {
1258 d_state = e_ERROR_STATE;
1265 d_state = e_DONE_STATE;
1268 for (numEmitted = 0; 8 <= d_bitsInStack && numEmitted != maxNumOut;
1271 *out++ =
static_cast<char>((d_stack >> d_bitsInStack) & 0xff);
1274 *numOut = numEmitted;
1275 d_outputLength += numEmitted;
1277 return d_bitsInStack / 8;