IOStream: Determine number of bytes written to array sink

Hello
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.
Private base class basic_zlib_decompressor of class basic_gzip_decompressor has method total_out() which seems to return the value in question but its return type is also only an "int".
Regards, Peter.
#include <iostream>
#include <stdexcept>
#include <iomanip>
#include

AMDG 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 { public: typedef char char_type; struct category : input_tag, filter_tag, multichar_tag, optimally_buffered_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; } private: std::streamsize chars_; }; In Christ, Steven Watanabe

Hi Steven
-----Original Message----- From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe via Boost-users ... 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; }
Thanks for the tip! I had hoped there was some kind of well hidden buffer pointer to do some pointer arithmetic. One problem I stumbled into with the counter filter: If optimal_buffer_size() returns 0, write() is called with 1 byte buffers. In my case this increased the program runtime from 8 seconds to 4 minutes. Returning a larger value solved this issue for my use case. Regards, Peter.
participants (2)
-
Peter Klotz
-
Steven Watanabe