std::streamsize read()

in include/core/CBase64Filter.h [240:305]


    std::streamsize read(SOURCE& src, char_type* s, std::streamsize n) {
        // copy into the buffer while there is data to read and space in the buffer
        std::streamsize done = 0;
        char buf[4096];
        while (done < n) {
            std::streamsize toCopy = std::min(std::streamsize(m_BufferOut.size()),
                                              std::streamsize(n - done));
            LOG_TRACE(<< "Trying to copy " << toCopy << " bytes into stream, max "
                      << n << ", available " << m_BufferOut.size());
            for (std::streamsize i = 0; i < toCopy; i++) {
                s[done++] = m_BufferOut.front();
                m_BufferOut.pop_front();
            }
            LOG_TRACE(<< "Eos: " << m_Eos << ", In: " << m_BufferIn.empty()
                      << ", Out: " << m_BufferOut.empty());
            if (done == n) {
                break;
            }
            if ((done > 0) && m_BufferIn.empty() && m_BufferOut.empty() && m_Eos) {
                LOG_TRACE(<< "Base64 READ " << done << ", from n " << n
                          << ", left " << m_BufferOut.size());
                return done;
            }

            // grab some data if we need it
            if ((m_BufferIn.size() < 4) && (m_Eos == false)) {
                std::streamsize readBytes = boost::iostreams::read(src, buf, 4096);
                LOG_TRACE(<< "Read " << readBytes << " from input stream");
                if (readBytes == -1) {
                    LOG_TRACE(<< "Got EOS from underlying store");
                    m_Eos = true;
                } else {
                    for (std::streamsize i = 0; i < readBytes; i++) {
                        // Only copy Base64 characters - JSON punctuation is ignored
                        // The dechunker parses JSON and should give us only base64 strings,
                        // but we don't want to try and decode anything which might cause
                        // the decoder to choke
                        switch (buf[i]) {
                        case ']':
                        case '[':
                        case ',':
                        case '"':
                        case '{':
                        case '}':
                        case '\\':
                        case ' ':
                        case ':':
                            break;

                        default:
                            m_BufferIn.push_back(static_cast<uint8_t>(buf[i]));
                            break;
                        }
                    }
                }
            }
            this->Decode(m_Eos);
            if (m_Eos && m_BufferOut.empty() && m_BufferIn.empty() && (done == 0)) {
                LOG_TRACE(<< "Returning -1 from read");
                return -1;
            }
        }
        LOG_TRACE(<< "Base64 READ " << done << ", from n " << n << ", left "
                  << m_BufferOut.size());
        return done;
    }