void seek_and_read_getc(concurrency::streams::istream stream, const std::vector<uint8_t>& content_to_compare)
{
    size_t content_len = (int)content_to_compare.size();
    size_t pos1 = 0;
    size_t pos2 = content_len / 4;
    size_t pos3 = content_len / 2;
    size_t pos4 = content_len - content_len / 4;

    std::vector<uint8_t> buffer;
    buffer.resize(content_len);

    CHECK_EQUAL(pos2, stream.seek((int)pos2, std::ios_base::beg));
    for (size_t i = pos2; i < pos3; ++i)
    {
        buffer[i] = (uint8_t)stream.read().get();
    }

    CHECK_EQUAL(pos1, stream.seek((int)(pos1 - pos3), std::ios_base::cur));
    for (size_t i = pos1; i < pos2; ++i)
    {
        buffer[i] = (uint8_t)stream.read().get();
    }

    CHECK_EQUAL(pos3, stream.seek((int)(pos3 - content_len), std::ios_base::end));
    for (size_t i = pos3; i < pos4; ++i)
    {
        buffer[i] = (uint8_t)stream.read().get();
    }

    CHECK_EQUAL(pos4, stream.seek((int)(pos4 - content_len), std::ios_base::end));
    for (size_t i = pos4; i < content_len; ++i)
    {
        buffer[i] = (uint8_t)stream.read().get();
    }

    CHECK_ARRAY_EQUAL(content_to_compare.data(), buffer.data(), (int)content_to_compare.size());
}
void seek_and_read_getn(concurrency::streams::istream stream, const std::vector<uint8_t>& content_to_compare)
{
    size_t content_len = content_to_compare.size();
    size_t pos1 = 0;
    size_t pos2 = content_len / 4;
    size_t pos3 = content_len / 2;
    size_t pos4 = content_len - content_len / 4;

    std::vector<uint8_t> buffer;
    buffer.resize(content_len);
    concurrency::streams::container_buffer<std::vector<uint8_t>> sbuf(buffer, std::ios_base::out);

    sbuf.seekpos(pos3, std::ios_base::out);
    CHECK_EQUAL(pos3, stream.seek((int)pos3, std::ios_base::beg));
    CHECK_EQUAL(pos4 - pos3, stream.read(sbuf, pos4 - pos3).get());

    sbuf.seekpos(pos1, std::ios_base::out);
    CHECK_EQUAL(pos1, stream.seek((int)(pos1 - pos4), std::ios_base::cur));
    CHECK_EQUAL(pos2 - pos1, stream.read(sbuf, pos2 - pos1).get());

    sbuf.seekpos(pos2, std::ios_base::out);
    CHECK_EQUAL(pos2, stream.seek((int)(pos2 - content_len), std::ios_base::end));
    CHECK_EQUAL(pos3 - pos2, stream.read(sbuf, pos3 - pos2).get());

    sbuf.seekpos(pos4, std::ios_base::out);
    CHECK_EQUAL(pos4, stream.seek((int)(pos4 - pos3), std::ios_base::cur));
    CHECK_EQUAL(content_len - pos4, stream.read(sbuf, content_len - pos4).get());

    CHECK_ARRAY_EQUAL(content_to_compare.data(), sbuf.collection().data(), (int)content_len);

    CHECK_EQUAL((concurrency::streams::istream::pos_type)concurrency::streams::istream::traits::eof(), stream.seek(-1, std::ios_base::beg));
    CHECK_EQUAL((concurrency::streams::istream::pos_type)0, stream.seek(0, std::ios_base::beg));
    CHECK_EQUAL((concurrency::streams::istream::pos_type)content_len, stream.seek(0, std::ios_base::end));
    CHECK_EQUAL((concurrency::streams::istream::pos_type)concurrency::streams::istream::traits::eof(), stream.seek(1, std::ios_base::end));
    CHECK_EQUAL((concurrency::streams::istream::pos_type)content_len, stream.seek(content_len, std::ios_base::beg));
    CHECK_EQUAL((concurrency::streams::istream::pos_type)concurrency::streams::istream::traits::eof(), stream.seek(content_len + 1, std::ios_base::beg));
}
Esempio n. 3
0
// Helper function to convert message body without extracting.
static utility::string_t convert_body_to_string_t(const utility::string_t &content_type, concurrency::streams::istream instream)
{
    if (!instream)
    {
        // The instream is not yet set
        return utility::string_t();
    }

    concurrency::streams::streambuf<uint8_t>  streambuf = instream.streambuf();

    _ASSERTE((bool)streambuf);
    _ASSERTE(streambuf.is_open());
    _ASSERTE(streambuf.can_read());

    utility::string_t content, charset;
    parse_content_type_and_charset(content_type, content, charset);

    // Content-Type must have textual type.
    if(!is_content_type_textual(content) || streambuf.in_avail() == 0)
    {
        return utility::string_t();
    }

    std::vector<unsigned char> body((std::vector<unsigned char>::size_type)streambuf.in_avail());
    size_t copied = streambuf.scopy(&body[0], body.size());
    if (copied == 0 || copied == (size_t)-1) 
        return _XPLATSTR("");

    // Latin1
#ifdef _MS_WINDOWS
    if(_wcsicmp(charset.c_str(), charset_latin1.c_str()) == 0)
#else
    if(boost::iequals(charset, charset_latin1))
#endif
    {
        return to_string_t(latin1_to_utf16(convert_bytes_to_string(&body[0], body.size())));
    }

    // utf-8.
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf8.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf8) )
#endif
    {
        return to_string_t(convert_utf8_to_utf16(&body[0], body.size()));
    }

    // utf-16.
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf16.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf16) )
#endif
    {
        return to_string_t(convert_utf16_to_utf16(&body[0], body.size()));
    }

    // utf-16le
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf16le.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf16le) )
#endif
    {
        return to_string_t(convert_bytes_to_wstring(&body[0], body.size()));
    }

    // utf-16be
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf16be.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf16be) )
#endif
    {
        return to_string_t(convert_utf16be_to_utf16le(&body[0], body.size(), false));
    }
    
    else
    {
        return utility::string_t();
    }
}
Esempio n. 4
0
// Helper function to convert message body without extracting.
static utility::string_t convert_body_to_string_t(const utility::string_t& content_type,
                                                  concurrency::streams::istream instream)
{
    if (!instream)
    {
        // The instream is not yet set
        return utility::string_t();
    }

    concurrency::streams::streambuf<uint8_t> streambuf = instream.streambuf();

    _ASSERTE((bool)streambuf);
    _ASSERTE(streambuf.is_open());
    _ASSERTE(streambuf.can_read());

    utility::string_t content, charset;
    parse_content_type_and_charset(content_type, content, charset);

    // Content-Type must have textual type.
    if (!is_content_type_textual(content) || streambuf.in_avail() == 0)
    {
        return utility::string_t();
    }

    // Latin1
    if (utility::details::str_iequal(charset, charset_types::latin1))
    {
        std::string body;
        body.resize(streambuf.in_avail());
        if (streambuf.scopy((unsigned char*)&body[0], body.size()) == 0) return string_t();
        return to_string_t(latin1_to_utf16(std::move(body)));
    }

    // utf-8.
    else if (utility::details::str_iequal(charset, charset_types::utf8))
    {
        std::string body;
        body.resize(streambuf.in_avail());
        if (streambuf.scopy((unsigned char*)&body[0], body.size()) == 0) return string_t();
        return to_string_t(std::move(body));
    }

    // utf-16.
    else if (utility::details::str_iequal(charset, charset_types::utf16))
    {
        utf16string body;
        body.resize(streambuf.in_avail() / sizeof(utf16string::value_type));
        if (streambuf.scopy((unsigned char*)&body[0], body.size() * sizeof(utf16string::value_type)) == 0)
            return string_t();
        return convert_utf16_to_string_t(std::move(body));
    }

    // utf-16le
    else if (utility::details::str_iequal(charset, charset_types::utf16le))
    {
        utf16string body;
        body.resize(streambuf.in_avail() / sizeof(utf16string::value_type));
        if (streambuf.scopy((unsigned char*)&body[0], body.size() * sizeof(utf16string::value_type)) == 0)
            return string_t();
        return convert_utf16le_to_string_t(std::move(body), false);
    }

    // utf-16be
    else if (utility::details::str_iequal(charset, charset_types::utf16be))
    {
        utf16string body;
        body.resize(streambuf.in_avail() / sizeof(utf16string::value_type));
        if (streambuf.scopy((unsigned char*)&body[0], body.size() * sizeof(utf16string::value_type)) == 0)
            return string_t();
        return convert_utf16be_to_string_t(std::move(body), false);
    }

    else
    {
        return utility::string_t();
    }
}
size_t seek_read_and_compare(concurrency::streams::istream stream, std::vector<uint8_t> buffer_to_compare, utility::size64_t offset, size_t count, size_t expected_read_count)
{
    std::vector<uint8_t> buffer;
    buffer.resize(count);
    stream.seek(offset);
    auto read_count = stream.streambuf().getn(buffer.data(), count).get();
    CHECK_EQUAL(expected_read_count, read_count);
    CHECK_ARRAY_EQUAL(buffer_to_compare.data() + offset, buffer.data(), (int)read_count);
    return read_count;
}