Boost logo

Boost Users :

Subject: Re: [Boost-users] IOStream: Determine number of bytes written to array sink
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2018-11-22 19:15:51


On 11/22/2018 11:40 AM, Peter Klotz via Boost-users wrote:
> The program below decompresses a std::string using Boost.IOStream. The target is an array sink of fixed size.
> The users know the decompressed size in advance but for security reasons I need to check the number of bytes written to the sink.
> If the array is too small, a "write area exhausted" exception is thrown, which is fine.
> If the array is too big, decompression succeeds. I would like to check the number of bytes written to the sink and throw an exception.
> How can I determine how many bytes have been written to the sink?
> Btw: I cannot use the "counter" filter because of the overhead involved in line counting and because the return value of its characters() method is "int". The buffers involved in my application require a 64 bit data type.

Use your own counter filter? It's not difficult:
(copy/paste from iostreams/filter/counter.hpp and edit)

class counter {
    typedef char char_type;
    struct category
        : input_tag,
        { };
    explicit counter()
        : chars_(0)
        { }
    std::streamsize characters() const { return chars_; }
    std::streamsize optimal_buffer_size() const { return 0; }

    template<typename Sink>
    std::streamsize write(Sink& snk, const char* s, std::streamsize n)
        std::streamsize result = iostreams::write(snk, s, n);
        chars_ += result;
        return result;
    std::streamsize chars_;

In Christ,
Steven Watanabe

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at