Skip to content

Commit

Permalink
third: Add forward declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Jan 22, 2024
1 parent 606b014 commit 9b3c8a8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 95 deletions.
10 changes: 10 additions & 0 deletions poseidon/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,16 @@ class Easy_WS_Client;
class Easy_WSS_Server;
class Easy_WSS_Client;

// Helper types for third-party libraries
class BIO_ptr;
class X509_ptr;
class SSL_CTX_ptr;
class SSL_ptr;
class zlib_Stream_base;
class deflate_Stream;
class inflate_Stream;
class MySQL_Client;

// Singletons
extern atomic_relaxed<int> exit_signal;
extern class Main_Config& main_config;
Expand Down
8 changes: 4 additions & 4 deletions poseidon/third/openssl_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ namespace poseidon {
\
class OBJ##_ptr // no semicolon

POSEIDON_OPENSSL_FWD_DEFINE_PTR_(BIO); // class BIO_ptr
POSEIDON_OPENSSL_FWD_DEFINE_PTR_(X509); // class X509_ptr
POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL_CTX); // class SSL_CTX_ptr
POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL); // class SSL_ptr
POSEIDON_OPENSSL_FWD_DEFINE_PTR_(BIO); // class BIO_ptr;
POSEIDON_OPENSSL_FWD_DEFINE_PTR_(X509); // class X509_ptr;
POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL_CTX); // class SSL_CTX_ptr;
POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL); // class SSL_ptr;

} // namespace poseidon
#endif
137 changes: 46 additions & 91 deletions poseidon/third/zlib_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,24 @@
#include <zlib.h>
namespace poseidon {

class deflate_Stream
class zlib_Stream_base
{
private:
::z_stream m_zstrm[1];
protected:
mutable ::z_stream m_zstrm[1];

public:
explicit
deflate_Stream(zlib_Format fmt, uint8_t wbits, int level)
protected:
zlib_Stream_base() noexcept
{
this->m_zstrm->zalloc = nullptr;
this->m_zstrm->zfree = nullptr;
this->m_zstrm->opaque = nullptr;
this->m_zstrm->next_in = (const ::Bytef*) "";
this->m_zstrm->next_in = nullptr;
this->m_zstrm->avail_in = 0;

if((wbits < 9) || (wbits > 15))
::rocket::sprintf_and_throw<::std::invalid_argument>(
"deflate_Stream: window bits `%d` not valid",
wbits);

int fmt_wbits;
if(fmt == zlib_deflate)
fmt_wbits = wbits;
else if(fmt == zlib_raw)
fmt_wbits = -wbits;
else if(fmt == zlib_gzip)
fmt_wbits = wbits + 16;
else
::rocket::sprintf_and_throw<::std::invalid_argument>(
"deflate_Stream: format `%d` not valid",
fmt);

if((level < -1) || (level > 9))
::rocket::sprintf_and_throw<::std::invalid_argument>(
"deflate_Stream: compression level `%d` not valid",
level);

int err = ::deflateInit2(this->m_zstrm, level, Z_DEFLATED, fmt_wbits, 9, Z_DEFAULT_STRATEGY);
if(err != Z_OK)
this->throw_exception(err, "deflateInit2");
}

ASTERIA_NONCOPYABLE_DESTRUCTOR(deflate_Stream)
{
::deflateEnd(this->m_zstrm);
}
ASTERIA_NONCOPYABLE_DESTRUCTOR(zlib_Stream_base) = default;

public:
constexpr operator
const ::z_stream*() const noexcept
{ return this->m_zstrm; }
Expand All @@ -81,7 +52,7 @@ class deflate_Stream
msg = "no error message";

::rocket::sprintf_and_throw<::std::runtime_error>(
"deflate_Stream: zlib error: %s\n[`%s()` returned `%d`]",
"zlib error: %s\n[`%s()` returned `%d`]",
msg, func, err);
}

Expand All @@ -102,24 +73,14 @@ class deflate_Stream
}
};

class inflate_Stream
struct deflate_Stream : zlib_Stream_base
{
private:
::z_stream m_zstrm[1];

public:
explicit
inflate_Stream(zlib_Format fmt, uint8_t wbits)
deflate_Stream(zlib_Format fmt, uint8_t wbits, int level)
{
this->m_zstrm->zalloc = nullptr;
this->m_zstrm->zfree = nullptr;
this->m_zstrm->opaque = nullptr;
this->m_zstrm->next_in = (const ::Bytef*) "";
this->m_zstrm->avail_in = 0;

if((wbits < 9) || (wbits > 15))
::rocket::sprintf_and_throw<::std::invalid_argument>(
"inflate_Stream: window bits `%d` not valid",
"deflate_Stream: window bits `%d` not valid",
wbits);

int fmt_wbits;
Expand All @@ -131,61 +92,55 @@ class inflate_Stream
fmt_wbits = wbits + 16;
else
::rocket::sprintf_and_throw<::std::invalid_argument>(
"inflate_Stream: format `%d` not valid",
"deflate_Stream: format `%d` not valid",
fmt);

int err = ::inflateInit2(this->m_zstrm, fmt_wbits);
if((level < -1) || (level > 9))
::rocket::sprintf_and_throw<::std::invalid_argument>(
"deflate_Stream: compression level `%d` not valid",
level);

int err = ::deflateInit2(this->m_zstrm, level, Z_DEFLATED, fmt_wbits, 9, Z_DEFAULT_STRATEGY);
if(err != Z_OK)
this->throw_exception(err, "inflateInit2");
this->throw_exception(err, "deflateInit2");
}

ASTERIA_NONCOPYABLE_DESTRUCTOR(inflate_Stream)
ASTERIA_NONCOPYABLE_DESTRUCTOR(deflate_Stream)
{
::inflateEnd(this->m_zstrm);
::deflateEnd(this->m_zstrm);
}
};

constexpr operator
const ::z_stream*() const noexcept
{ return this->m_zstrm; }

operator
::z_stream*() noexcept
{ return this->m_zstrm; }

[[noreturn]]
void
throw_exception(int err, const char* func) const
struct inflate_Stream : zlib_Stream_base
{
explicit
inflate_Stream(zlib_Format fmt, uint8_t wbits)
{
const char* msg;
if((wbits < 9) || (wbits > 15))
::rocket::sprintf_and_throw<::std::invalid_argument>(
"inflate_Stream: window bits `%d` not valid",
wbits);

if(err == Z_VERSION_ERROR)
msg = "zlib library version mismatch";
else if(err == Z_MEM_ERROR)
msg = "memory allocation failure";
else if(this->m_zstrm->msg != nullptr)
msg = this->m_zstrm->msg;
int fmt_wbits;
if(fmt == zlib_deflate)
fmt_wbits = wbits;
else if(fmt == zlib_raw)
fmt_wbits = -wbits;
else if(fmt == zlib_gzip)
fmt_wbits = wbits + 16;
else
msg = "no error message";

::rocket::sprintf_and_throw<::std::runtime_error>(
"inflate_Stream: zlib error: %s\n[`%s()` returned `%d`]",
msg, func, err);
}
::rocket::sprintf_and_throw<::std::invalid_argument>(
"inflate_Stream: format `%d` not valid",
fmt);

void
get_buffers(char*& ocur, const char*& icur) const noexcept
{
ocur = reinterpret_cast<char*>(this->m_zstrm->next_out);
icur = reinterpret_cast<const char*>(this->m_zstrm->next_in);
int err = ::inflateInit2(this->m_zstrm, fmt_wbits);
if(err != Z_OK)
this->throw_exception(err, "inflateInit2");
}

void
set_buffers(char* ocur, char* oend, const char* icur, const char* iend) noexcept
ASTERIA_NONCOPYABLE_DESTRUCTOR(inflate_Stream)
{
this->m_zstrm->next_out = reinterpret_cast<::Bytef*>(ocur);
this->m_zstrm->avail_out = ::rocket::clamp_cast<::uInt>(oend - ocur, 0, INT_MAX);
this->m_zstrm->next_in = reinterpret_cast<const ::Bytef*>(icur);
this->m_zstrm->avail_in = ::rocket::clamp_cast<const ::uInt>(iend - icur, 0, INT_MAX);
::inflateEnd(this->m_zstrm);
}
};

Expand Down

0 comments on commit 9b3c8a8

Please sign in to comment.