binc: indent and cie

This commit is contained in:
Jean-Francois Dockes 2020-05-11 07:23:23 +01:00
parent 631532f100
commit 8a7e00a029
5 changed files with 191 additions and 196 deletions

View File

@ -1,4 +1,3 @@
/* -*- mode:c++;c-basic-offset:2 -*- */
/* -------------------------------------------------------------------- /* --------------------------------------------------------------------
* Filename: * Filename:
* src/mime-inputsource.h * src/mime-inputsource.h
@ -25,7 +24,6 @@
*/ */
#ifndef mime_inputsource_h_included #ifndef mime_inputsource_h_included
#define mime_inputsource_h_included #define mime_inputsource_h_included
#include "autoconfig.h"
// Data source for MIME parser // Data source for MIME parser
// Note about large files: we might want to change the unsigned int // Note about large files: we might want to change the unsigned int
@ -36,15 +34,15 @@
// stream input source (from a memory buffer, no file offsets). When // stream input source (from a memory buffer, no file offsets). When
// parsing a raw message file, it's only one message. // parsing a raw message file, it's only one message.
#include <string.h> #include <cstring>
#include "safeunistd.h" #include "safeunistd.h"
#include <iostream> #include <iostream>
namespace Binc { namespace Binc {
class MimeInputSource { class MimeInputSource {
public: public:
// Note that we do NOT take ownership of fd, won't close it on delete // Note that we do NOT take ownership of fd, won't close it on delete
inline MimeInputSource(int fd, unsigned int start = 0); inline MimeInputSource(int fd, unsigned int start = 0);
virtual inline ~MimeInputSource(void); virtual inline ~MimeInputSource(void);
@ -60,7 +58,7 @@ namespace Binc {
inline unsigned int getOffset(void) const; inline unsigned int getOffset(void) const;
private: private:
int fd; int fd;
char data[16384]; char data[16384];
unsigned int offset; unsigned int offset;
@ -68,10 +66,10 @@ namespace Binc {
unsigned int head; unsigned int head;
unsigned int start; unsigned int start;
char lastChar; char lastChar;
}; };
inline MimeInputSource::MimeInputSource(int fd, unsigned int start) inline MimeInputSource::MimeInputSource(int fd, unsigned int start)
{ {
this->fd = fd; this->fd = fd;
this->start = start; this->start = start;
offset = 0; offset = 0;
@ -81,141 +79,141 @@ namespace Binc {
memset(data, '\0', sizeof(data)); memset(data, '\0', sizeof(data));
seek(start); seek(start);
} }
inline MimeInputSource::~MimeInputSource(void) inline MimeInputSource::~MimeInputSource(void)
{ {
} }
inline ssize_t MimeInputSource::fillRaw(char *raw, size_t nbytes) inline ssize_t MimeInputSource::fillRaw(char *raw, size_t nbytes)
{ {
return read(fd, raw, nbytes); return read(fd, raw, nbytes);
} }
inline bool MimeInputSource::fillInputBuffer(void) inline bool MimeInputSource::fillInputBuffer(void)
{ {
char raw[4096]; char raw[4096];
ssize_t nbytes = fillRaw(raw, 4096); ssize_t nbytes = fillRaw(raw, 4096);
if (nbytes <= 0) { if (nbytes <= 0) {
// FIXME: If ferror(crlffile) we should log this. // FIXME: If ferror(crlffile) we should log this.
return false; return false;
} }
for (ssize_t i = 0; i < nbytes; ++i) { for (ssize_t i = 0; i < nbytes; ++i) {
const char c = raw[i]; const char c = raw[i];
if (c == '\r') { if (c == '\r') {
if (lastChar == '\r') { if (lastChar == '\r') {
data[tail++ & (0x4000-1)] = '\r'; data[tail++ & (0x4000-1)] = '\r';
data[tail++ & (0x4000-1)] = '\n'; data[tail++ & (0x4000-1)] = '\n';
} }
} else if (c == '\n') { } else if (c == '\n') {
data[tail++ & (0x4000-1)] = '\r'; data[tail++ & (0x4000-1)] = '\r';
data[tail++ & (0x4000-1)] = '\n'; data[tail++ & (0x4000-1)] = '\n';
} else { } else {
if (lastChar == '\r') { if (lastChar == '\r') {
data[tail++ & (0x4000-1)] = '\r'; data[tail++ & (0x4000-1)] = '\r';
data[tail++ & (0x4000-1)] = '\n'; data[tail++ & (0x4000-1)] = '\n';
} }
data[tail++ & (0x4000-1)] = c; data[tail++ & (0x4000-1)] = c;
} }
lastChar = c; lastChar = c;
} }
return true; return true;
} }
inline void MimeInputSource::reset(void) inline void MimeInputSource::reset(void)
{ {
offset = head = tail = 0; offset = head = tail = 0;
lastChar = '\0'; lastChar = '\0';
if (fd != -1) if (fd != -1)
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
} }
inline void MimeInputSource::seek(unsigned int seekToOffset) inline void MimeInputSource::seek(unsigned int seekToOffset)
{ {
if (offset > seekToOffset) if (offset > seekToOffset)
reset(); reset();
char c; char c;
int n = 0; int n = 0;
while (seekToOffset > offset) { while (seekToOffset > offset) {
if (!getChar(&c)) if (!getChar(&c))
break; break;
++n; ++n;
} }
} }
inline bool MimeInputSource::getChar(char *c) inline bool MimeInputSource::getChar(char *c)
{ {
if (head == tail && !fillInputBuffer()) if (head == tail && !fillInputBuffer())
return false; return false;
*c = data[head++ & (0x4000-1)]; *c = data[head++ & (0x4000-1)];
++offset; ++offset;
return true; return true;
} }
inline void MimeInputSource::ungetChar() inline void MimeInputSource::ungetChar()
{ {
--head; --head;
--offset; --offset;
} }
inline int MimeInputSource::getFileDescriptor(void) const inline int MimeInputSource::getFileDescriptor(void) const
{ {
return fd; return fd;
} }
inline unsigned int MimeInputSource::getOffset(void) const inline unsigned int MimeInputSource::getOffset(void) const
{ {
return offset; return offset;
} }
/////////////////////////////////// ///////////////////////////////////
class MimeInputSourceStream : public MimeInputSource { class MimeInputSourceStream : public MimeInputSource {
public: public:
inline MimeInputSourceStream(istream& s, unsigned int start = 0); inline MimeInputSourceStream(std::istream& s, unsigned int start = 0);
virtual inline ssize_t fillRaw(char *raw, size_t nb); virtual inline ssize_t fillRaw(char *raw, size_t nb);
virtual inline void reset(void); virtual inline void reset(void);
private: private:
istream& s; std::istream& s;
}; };
inline MimeInputSourceStream::MimeInputSourceStream(istream& si, inline MimeInputSourceStream::MimeInputSourceStream(std::istream& si,
unsigned int start) unsigned int start)
: MimeInputSource(-1, start), s(si) : MimeInputSource(-1, start), s(si)
{ {
} }
inline ssize_t MimeInputSourceStream::fillRaw(char *raw, size_t nb) inline ssize_t MimeInputSourceStream::fillRaw(char *raw, size_t nb)
{ {
// Why can't streams tell how many characters were actually read // Why can't streams tell how many characters were actually read
// when hitting eof ? // when hitting eof ?
std::streampos st = s.tellg(); std::streampos st = s.tellg();
s.seekg(0, ios::end); s.seekg(0, std::ios::end);
std::streampos lst = s.tellg(); std::streampos lst = s.tellg();
s.seekg(st); s.seekg(st);
size_t nbytes = size_t(lst - st); size_t nbytes = size_t(lst - st);
if (nbytes > nb) { if (nbytes > nb) {
nbytes = nb; nbytes = nb;
} }
if (nbytes <= 0) { if (nbytes <= 0) {
return (ssize_t)-1; return (ssize_t)-1;
} }
s.read(raw, nbytes); s.read(raw, nbytes);
return static_cast<ssize_t>(nbytes); return static_cast<ssize_t>(nbytes);
} }
inline void MimeInputSourceStream::reset(void) inline void MimeInputSourceStream::reset(void)
{ {
MimeInputSource::reset(); MimeInputSource::reset();
s.seekg(0); s.seekg(0);
} }
} }

View File

@ -36,15 +36,14 @@
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#ifndef NO_NAMESPACES
using namespace ::std;
#endif /* NO_NAMESPACES */
#include "mime.h" #include "mime.h"
#include "mime-utils.h" #include "mime-utils.h"
#include "mime-inputsource.h" #include "mime-inputsource.h"
#include "convert.h" #include "convert.h"
using namespace std;
// #define MPF // #define MPF
#ifdef MPF #ifdef MPF
#define MPFDEB(X) fprintf X #define MPFDEB(X) fprintf X

View File

@ -40,9 +40,7 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#ifndef NO_NAMESPACES using namespace std;
using namespace ::std;
#endif /* NO_NAMESPACES */
//------------------------------------------------------------------------ //------------------------------------------------------------------------
void Binc::MimeDocument::parseOnlyHeader(int fd) void Binc::MimeDocument::parseOnlyHeader(int fd)

View File

@ -35,15 +35,13 @@
#include <map> #include <map>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#ifndef NO_NAMESPACES
using namespace ::std;
#endif /* NO_NAMESPACES */
#include "mime.h" #include "mime.h"
#include "convert.h" #include "convert.h"
#include "mime-inputsource.h" #include "mime-inputsource.h"
using namespace std;
//------------------------------------------------------------------------ //------------------------------------------------------------------------
Binc::MimeDocument::MimeDocument(void) Binc::MimeDocument::MimeDocument(void)
{ {

View File

@ -1,4 +1,3 @@
/* -*- mode:c++;c-basic-offset:2 -*- */
/* -------------------------------------------------------------------- /* --------------------------------------------------------------------
* Filename: * Filename:
* src/parsers/mime/mime.h * src/parsers/mime/mime.h
@ -25,6 +24,7 @@
*/ */
#ifndef mime_h_included #ifndef mime_h_included
#define mime_h_included #define mime_h_included
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include <map>
@ -38,32 +38,33 @@ class MimeInputSource;
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class HeaderItem { class HeaderItem {
private: private:
mutable std::string key; mutable std::string key;
mutable std::string value; mutable std::string value;
public: public:
inline const std::string &getKey(void) const { return key; } inline const std::string &getKey(void) const { return key; }
inline const std::string &getValue(void) const { return value; } inline const std::string &getValue(void) const { return value; }
//-- //--
HeaderItem(void); HeaderItem(void);
HeaderItem(const std::string &key, const std::string &value); HeaderItem(const std::string &key, const std::string &value);
}; };
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class Header { class Header {
private: private:
mutable std::vector<HeaderItem> content; mutable std::vector<HeaderItem> content;
public: public:
bool getFirstHeader(const std::string &key, HeaderItem &dest) const; bool getFirstHeader(const std::string &key, HeaderItem &dest) const;
bool getAllHeaders(const std::string &key, std::vector<HeaderItem> &dest) const; bool getAllHeaders(const std::string &key, std::vector<HeaderItem> &dest)
void add(const std::string &name, const std::string &content); const;
void clear(void); void add(const std::string &name, const std::string &content);
void clear(void);
//-- //--
Header(void); Header(void);
~Header(void); ~Header(void);
}; };
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -72,113 +73,114 @@ class MimeDocument;
class MimePart { class MimePart {
protected: protected:
public: public:
mutable bool multipart; mutable bool multipart;
mutable bool messagerfc822; mutable bool messagerfc822;
mutable std::string subtype; mutable std::string subtype;
mutable std::string boundary; mutable std::string boundary;
mutable unsigned int headerstartoffsetcrlf; mutable unsigned int headerstartoffsetcrlf;
mutable unsigned int headerlength; mutable unsigned int headerlength;
mutable unsigned int bodystartoffsetcrlf; mutable unsigned int bodystartoffsetcrlf;
mutable unsigned int bodylength; mutable unsigned int bodylength;
mutable unsigned int nlines; mutable unsigned int nlines;
mutable unsigned int nbodylines; mutable unsigned int nbodylines;
mutable unsigned int size; mutable unsigned int size;
public: public:
enum FetchType { enum FetchType {
FetchBody, FetchBody,
FetchHeader, FetchHeader,
FetchMime FetchMime
}; };
mutable Header h; mutable Header h;
mutable std::vector<MimePart> members; mutable std::vector<MimePart> members;
inline const std::string &getSubType(void) const { return subtype; } inline const std::string &getSubType(void) const { return subtype; }
inline bool isMultipart(void) const { return multipart; } inline bool isMultipart(void) const { return multipart; }
inline bool isMessageRFC822(void) const { return messagerfc822; } inline bool isMessageRFC822(void) const { return messagerfc822; }
inline unsigned int getSize(void) const { return bodylength; } inline unsigned int getSize(void) const { return bodylength; }
inline unsigned int getNofLines(void) const { return nlines; } inline unsigned int getNofLines(void) const { return nlines; }
inline unsigned int getNofBodyLines(void) const { return nbodylines; } inline unsigned int getNofBodyLines(void) const { return nbodylines; }
inline unsigned int getBodyLength(void) const { return bodylength; } inline unsigned int getBodyLength(void) const { return bodylength; }
inline unsigned int getBodyStartOffset(void) const { return bodystartoffsetcrlf; } inline unsigned int getBodyStartOffset(void) const {
return bodystartoffsetcrlf; }
void printBody(Binc::IODevice &output, unsigned int startoffset, unsigned int length) const; void printBody(Binc::IODevice &output, unsigned int startoffset,
void getBody(std::string& s, unsigned int startoffset, unsigned int length) const; unsigned int length) const;
virtual void clear(void); void getBody(std::string& s, unsigned int startoffset, unsigned int length)
const;
virtual void clear(void);
virtual int doParseOnlyHeader(MimeInputSource *ms); virtual int doParseOnlyHeader(MimeInputSource *ms);
virtual int doParseFull(MimeInputSource *ms, virtual int doParseFull(MimeInputSource *ms,
const std::string &toboundary, int &boundarysize); const std::string &toboundary, int &boundarysize);
MimePart(void); MimePart(void);
virtual ~MimePart(void); virtual ~MimePart(void);
private: private:
MimeInputSource *mimeSource; MimeInputSource *mimeSource;
bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines); bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines);
bool skipUntilBoundary(const std::string &delimiter, bool skipUntilBoundary(const std::string &delimiter,
unsigned int *nlines, bool *eof); unsigned int *nlines, bool *eof);
inline void postBoundaryProcessing(bool *eof, inline void postBoundaryProcessing(bool *eof,
unsigned int *nlines, unsigned int *nlines,
int *boundarysize, int *boundarysize,
bool *foundendofpart); bool *foundendofpart);
void parseMultipart(const std::string &boundary, void parseMultipart(const std::string &boundary,
const std::string &toboundary, const std::string &toboundary,
bool *eof, bool *eof,
unsigned int *nlines, unsigned int *nlines,
int *boundarysize, int *boundarysize,
bool *foundendofpart, bool *foundendofpart,
unsigned int *bodylength, unsigned int *bodylength,
std::vector<Binc::MimePart> *members); std::vector<Binc::MimePart> *members);
void parseSinglePart(const std::string &toboundary, void parseSinglePart(const std::string &toboundary,
int *boundarysize, int *boundarysize,
unsigned int *nbodylines, unsigned int *nbodylines,
unsigned int *nlines, unsigned int *nlines,
bool *eof, bool *foundendofpart, bool *eof, bool *foundendofpart,
unsigned int *bodylength); unsigned int *bodylength);
void parseHeader(Binc::Header *header, unsigned int *nlines); void parseHeader(Binc::Header *header, unsigned int *nlines);
void analyzeHeader(Binc::Header *header, bool *multipart, void analyzeHeader(Binc::Header *header, bool *multipart,
bool *messagerfc822, std::string *subtype, bool *messagerfc822, std::string *subtype,
std::string *boundary); std::string *boundary);
void parseMessageRFC822(std::vector<Binc::MimePart> *members, void parseMessageRFC822(std::vector<Binc::MimePart> *members,
bool *foundendofpart, bool *foundendofpart,
unsigned int *bodylength, unsigned int *bodylength,
unsigned int *nbodylines, unsigned int *nbodylines,
const std::string &toboundary); const std::string &toboundary);
}; };
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class MimeDocument : public MimePart { class MimeDocument : public MimePart {
public: public:
MimeDocument(void); MimeDocument(void);
~MimeDocument(void); ~MimeDocument(void);
void parseOnlyHeader(int fd); void parseOnlyHeader(int fd);
void parseFull(int fd); void parseFull(int fd);
void parseOnlyHeader(std::istream& s); void parseOnlyHeader(std::istream& s);
void parseFull(std::istream& s); void parseFull(std::istream& s);
void clear(void); void clear(void);
bool isHeaderParsed(void) const bool isHeaderParsed(void) const {
{ return headerIsParsed;
return headerIsParsed; }
} bool isAllParsed(void) const {
bool isAllParsed(void) const return allIsParsed;
{ }
return allIsParsed;
}
private: private:
bool headerIsParsed; bool headerIsParsed;
bool allIsParsed; bool allIsParsed;
MimeInputSource *doc_mimeSource; MimeInputSource *doc_mimeSource;
}; };
}; };