708 INPUT_ITERATOR begin,
715 if (e_ERROR_STATE == d_state || e_DONE_STATE == d_state) {
716 int rv = e_DONE_STATE == d_state ? -2 : -1;
717 d_state = e_ERROR_STATE;
727 while (8 <= d_bitsInStack && numEmitted != maxNumOut) {
729 *out =
static_cast<char>((d_stack >> d_bitsInStack) & 0xff);
738 if (e_INPUT_STATE == d_state) {
739 while (18 >= d_bitsInStack && begin != end) {
740 const unsigned char byte =
static_cast<unsigned char>(*begin);
745 unsigned char converted =
static_cast<unsigned char>(
748 if (converted < 64) {
749 d_stack = (d_stack << 6) | converted;
751 if (8 <= d_bitsInStack && numEmitted != maxNumOut) {
753 *out =
static_cast<char>(
754 (d_stack >> d_bitsInStack) & 0xff);
759 else if (!d_ignorable_p[
byte]) {
760 if (
'=' ==
byte && d_isPadded) {
761 const int residual = residualBits(
762 d_outputLength + numEmitted);
780 const int leftOver = residual % 8;
781 d_state = 0 != (d_stack & ((1 << leftOver) - 1))
788 d_stack >>= leftOver;
789 d_bitsInStack -= leftOver;
792 d_state = e_ERROR_STATE;
799 if (e_NEED_EQUAL_STATE == d_state) {
802 while (begin != end) {
803 const unsigned char byte =
static_cast<unsigned char>(*begin);
808 if (!d_ignorable_p[
byte]) {
810 d_state = e_SOFT_DONE_STATE;
813 d_state = e_ERROR_STATE;
819 if (e_SOFT_DONE_STATE == d_state) {
820 while (begin != end) {
821 const unsigned char byte =
static_cast<unsigned char>(*begin);
826 if (!d_ignorable_p[
byte]) {
827 d_state = e_ERROR_STATE;
833 *numOut = numEmitted;
834 d_outputLength += numEmitted;
836 return e_ERROR_STATE == d_state ? -1 : d_bitsInStack / 8;
841int Base64Decoder::convert<char *, const char *>(
853 e_ERROR_STATE == d_state || e_DONE_STATE == d_state)) {
854 int rv = e_DONE_STATE == d_state ? -2 : -1;
855 d_state = e_ERROR_STATE;
866 while (8 <= d_bitsInStack && numEmitted != maxNumOut) {
868 *out =
static_cast<char>((d_stack >> d_bitsInStack) & 0xff);
876 const char *originalBegin = begin;
884 const __m128i *alphabetSlices =
885 reinterpret_cast<const __m128i *
>(d_alphabet_p);
886 __m128i lut5 = _mm_loadu_si128(alphabetSlices + 7);
887 __m128i lut4 = _mm_loadu_si128(alphabetSlices + 6);
888 __m128i lut3 = _mm_loadu_si128(alphabetSlices + 5);
889 __m128i lut2 = _mm_loadu_si128(alphabetSlices + 4);
890 __m128i lut1 = _mm_loadu_si128(alphabetSlices + 3);
891 __m128i lut0 = _mm_loadu_si128(alphabetSlices + 2);
897 lut5 = _mm_xor_si128(lut5, lut4);
898 lut4 = _mm_xor_si128(lut4, lut3);
899 lut3 = _mm_xor_si128(lut3, lut2);
900 lut2 = _mm_xor_si128(lut2, lut1);
901 lut1 = _mm_xor_si128(lut1, lut0);
903 while (end - begin >= 16 &&
static_cast<unsigned>(numEmitted + 12)
904 <=
static_cast<unsigned>(maxNumOut)) {
907 __m128i x = _mm_loadu_si128(
908 reinterpret_cast<const __m128i *
>(begin));
911 x = _mm_subs_epi8(x, _mm_set1_epi8(0x20));
915 __m128i tooSmall = x;
921 __m128i decoded = _mm_shuffle_epi8(lut0, x);
927 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
936 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut1, x));
939 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
940 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut2, x));
941 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
942 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut3, x));
943 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
944 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut4, x));
945 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
946 decoded = _mm_xor_si128(decoded, _mm_shuffle_epi8(lut5, x));
947 x = _mm_subs_epi8(x, _mm_set1_epi8(0x10));
956 !_mm_testz_si128(tooSmall | decoded | ~x,
957 _mm_set1_epi8(
static_cast<char>(0x80))))) {
966 decoded = _mm_maddubs_epi16(decoded, _mm_set1_epi16(0x0140));
972 decoded = _mm_madd_epi16(decoded, _mm_set1_epi32(0x00011000));
976 __m128i selection = _mm_set_epi64(
977 reinterpret_cast<__m64
>(0xffffffff0c0d0e08ull),
978 reinterpret_cast<__m64
>(0x090a040506000102ull));
979 decoded = _mm_shuffle_epi8(decoded, selection);
982 memcpy(out, &decoded, 12);
989 while (end - begin >= 4 &&
static_cast<unsigned>(numEmitted + 3)
990 <=
static_cast<unsigned>(maxNumOut)) {
992 uint8_t *in =
reinterpret_cast<uint8_t *
>(inBuffer.
buffer());
993 memcpy(in, begin, 4);
996 x[0] =
static_cast<uint8_t
>(d_alphabet_p[in[0]]);
997 x[1] =
static_cast<uint8_t
>(d_alphabet_p[in[1]]);
998 x[2] =
static_cast<uint8_t
>(d_alphabet_p[in[2]]);
999 x[3] =
static_cast<uint8_t
>(d_alphabet_p[in[3]]);
1002 memcpy(&x4, x,
sizeof(x4));
1009 out[0] =
static_cast<char>((x[0] << 2) | (x[1] >> 4));
1010 out[1] =
static_cast<char>((x[1] << 4) | (x[2] >> 2));
1011 out[2] =
static_cast<char>((x[2] << 6) | (x[3] >> 0));
1019 while (18 >= d_bitsInStack && begin != end) {
1020 const unsigned char byte =
static_cast<unsigned char>(*begin);
1024 unsigned char converted =
static_cast<unsigned char>(
1025 d_alphabet_p[byte]);
1027 if (converted < 64) {
1028 d_stack = (d_stack << 6) | converted;
1030 if (8 <= d_bitsInStack && numEmitted != maxNumOut) {
1032 *out =
static_cast<char>(
1033 (d_stack >> d_bitsInStack) & 0xff);
1038 else if (!d_ignorable_p[
byte]) {
1039 if (
'=' ==
byte && d_isPadded) {
1040 const int residual = residualBits(
1041 d_outputLength + numEmitted);
1059 const int leftOver = residual % 8;
1060 d_state = 0 != (d_stack & ((1 << leftOver) - 1))
1063 ? e_NEED_EQUAL_STATE
1067 d_stack >>= leftOver;
1068 d_bitsInStack -= leftOver;
1071 d_state = e_ERROR_STATE;
1078 if (e_NEED_EQUAL_STATE == d_state) {
1081 while (begin != end) {
1082 const unsigned char byte =
static_cast<unsigned char>(*begin);
1086 if (!d_ignorable_p[
byte]) {
1088 d_state = e_SOFT_DONE_STATE;
1091 d_state = e_ERROR_STATE;
1100 const unsigned char byte =
static_cast<unsigned char>(*begin);
1105 d_state = e_ERROR_STATE;
1108 }
while (begin != end);
1111 *numIn =
static_cast<int>(begin - originalBegin);
1112 *numOut = numEmitted;
1113 d_outputLength += numEmitted;
1115 return e_ERROR_STATE == d_state ? -1 : d_bitsInStack / 8;
1152 if (!d_isPadded && e_INPUT_STATE == d_state) {
1153 const int residual = residualBits(d_outputLength);
1154 const int leftOver = residual % 8;
1156 0 != (d_stack & ((1 << leftOver) - 1)))) {
1157 d_state = e_ERROR_STATE;
1162 d_stack >>= leftOver;
1163 d_bitsInStack -= leftOver;
1167 if (e_ERROR_STATE == d_state || e_NEED_EQUAL_STATE == d_state ||
1168 (e_DONE_STATE == d_state && 0 == d_bitsInStack) ||
1169 (d_isPadded && e_INPUT_STATE == d_state &&
1170 0 != residualBits(d_outputLength))) {
1171 d_state = e_ERROR_STATE;
1178 d_state = e_DONE_STATE;
1181 for (numEmitted = 0; 8 <= d_bitsInStack && numEmitted != maxNumOut;
1184 *out++ =
static_cast<char>((d_stack >> d_bitsInStack) & 0xff);
1187 *numOut = numEmitted;
1188 d_outputLength += numEmitted;
1190 return d_bitsInStack / 8;