Archived
1
0

more c++ stuff probably broken

This commit is contained in:
Jeff Becker 2017-08-04 10:39:29 -04:00
parent aecbe2a5a9
commit 5ea8b1b245
7 changed files with 154 additions and 18 deletions

View File

@ -10,6 +10,25 @@ namespace nntpchan
void SHA512(const uint8_t * d, std::size_t l, SHA512Digest & h); void SHA512(const uint8_t * d, std::size_t l, SHA512Digest & h);
enum HashType
{
eHashTypeSHA1,
eHashTypeSHA256,
eHashTypeSHA512,
eHashTypeBLAKE2B
};
template<HashType t>
struct Hasher
{
typedef std::function<void(uint8_t *, size_t)> DigestVisitor;
Hasher();
~Hasher();
void Update(const char * data, size_t sz);
void Final(DigestVisitor v);
};
/** global crypto initializer */ /** global crypto initializer */
struct Crypto struct Crypto
{ {

View File

@ -0,0 +1,58 @@
#include "io.hpp"
namespace nntpchan
{
namespace io
{
template<class CharT = char>
struct MultiWriter : public std::basic_streambuf<CharT> , public std::basic_ostream<CharT>
{
using Base = std::basic_streambuf<CharT>;
using char_type = typename Base::char_type;
using int_type = typename Base::int_type;
std::vector<std::basic_ostream<CharT> *> writers;
int_type overflow(int_type ch)
{
return Base::overflow(ch);
}
virtual std::streamsize xputn(const char_type * data, std::streamsize sz)
{
for(const auto & itr : writers)
itr->write(data, sz);
return sz;
}
virtual int sync()
{
for(const auto & itr : writers)
itr->flush();
return 0;
}
void AddWriter(std::basic_ostream<CharT> * wr)
{
writers.push_back(wr);
}
virtual ~MultiWriter()
{
for(const auto & itr : writers)
delete itr;
}
};
std::basic_ostream<char> * multiWriter(const std::vector<std::basic_ostream<char> *> & writers)
{
MultiWriter<char> * wr = new MultiWriter<char>;
for(auto & itr : writers)
wr->AddWriter(itr);
return wr;
}
}
}

View File

@ -0,0 +1,34 @@
#ifndef NNTPCHAN_IO_HPP
#define NNTPCHAN_IO_HPP
#include <iostream>
#include <vector>
namespace nntpchan
{
namespace io
{
template<size_t bufferSize>
ssize_t copy(std::ostream * dst, std::istream * src)
{
char buff[bufferSize];
ssize_t n = 0;
while(!src->eof() && src->good())
{
std::streamsize sz;
src->read(buff, bufferSize);
sz = src->gcount();
n += sz;
dst->write(buff, sz);
if(!dst->good())
break;
}
return n;
}
std::basic_ostream<char> * multiWriter(const std::vector<std::basic_ostream<char> *> & writers);
std::basic_istream<char> * teeReader(std::basic_istream<char> * i, std::basic_ostream<char> * o);
}
}
#endif

View File

@ -1,33 +1,26 @@
#ifndef NNTPCHAN_MESSAGE_HPP #ifndef NNTPCHAN_MESSAGE_HPP
#define NNTPCHAN_MESSAGE_HPP #define NNTPCHAN_MESSAGE_HPP
#include <functional>
#include <string> #include <string>
#include <vector> #include <vector>
#include <map>
#include <functional>
namespace nntpchan namespace nntpchan
{ {
bool IsValidMessageID(const std::string & msgid); bool IsValidMessageID(const std::string & msgid);
typedef std::pair<std::string, std::string> MessageHeader; namespace mime
{
typedef std::function<bool(std::string, std::string)> HeaderValueVisitor;
typedef std::map<std::string, std::string> MIMEPartHeader; struct PartHeader
{
std::string boundary;
std::map<std::string, std::string> values;
typedef std::function<bool(const MessageHeader &)> MessageHeaderFilter; };
typedef std::function<bool(const MIMEPartHeader &)> MIMEPartFilter;
/**
read MIME message from i,
filter each header with h,
filter each part with p,
store result in o
return true if we read the whole message, return false if there is remaining
*/
bool StoreMIMEMessage(std::istream & i, MessageHeaderFilter h, MIMEPartHeader p, std::ostream & o);
}
} }

View File

@ -0,0 +1,26 @@
#ifndef NNTP_FILTER_HPP
#define NNTP_FILTER_HPP
#include <iostream>
#include <crypto.hpp>
namespace nntpchan
{
template<class CharT>
struct MessageFilter : public std::basic_ostream<CharT>
{
using Base = std::basic_streambuf<CharT>;
using char_type = Base::char_type;
using int_type = Base::int_type;
virtual int_type overflow(int_type ch);
protected:
virtual void xsputn(const char_type * data, std::streamsize sz);
};
}
#endif

View File

@ -1,5 +1,7 @@
#include "nntp_handler.hpp" #include "nntp_handler.hpp"
#include "io.hpp"
#include "message.hpp" #include "message.hpp"
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cstring> #include <cstring>
@ -125,7 +127,9 @@ namespace nntpchan
const std::string & msgid = command[1]; const std::string & msgid = command[1];
if(m_store.Accept(msgid)) if(m_store.Accept(msgid))
{ {
m_article = m_store.OpenWrite(msgid); std::ostream * file = m_store.OpenWrite(msgid);
m_filter = new MessageFilter(msgid);
m_article = nntpchan::io::multiWriter({file, m_filter});
} }
m_articleName = msgid; m_articleName = msgid;
EnterState(eStateStoreArticle); EnterState(eStateStoreArticle);

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include "line.hpp" #include "line.hpp"
#include "nntp_auth.hpp" #include "nntp_auth.hpp"
#include "nntp_filter.hpp"
#include "storage.hpp" #include "storage.hpp"
namespace nntpchan namespace nntpchan
@ -50,6 +51,7 @@ namespace nntpchan
private: private:
std::string m_articleName; std::string m_articleName;
std::fstream * m_article; std::fstream * m_article;
MessageFilter * m_filter;
NNTPCredentialDB * m_auth; NNTPCredentialDB * m_auth;
ArticleStorage m_store; ArticleStorage m_store;
std::string m_mode; std::string m_mode;