diff options
Diffstat (limited to 'src/mime-parsefull.cc')
-rw-r--r-- | src/mime-parsefull.cc | 160 |
1 files changed, 78 insertions, 82 deletions
diff --git a/src/mime-parsefull.cc b/src/mime-parsefull.cc index 53d07db..50d3157 100644 --- a/src/mime-parsefull.cc +++ b/src/mime-parsefull.cc @@ -4,20 +4,21 @@ * @author Andreas Aardal Hanssen * @date 2002-2005 * ----------------------------------------------------------------- **/ -#include "mime.h" -#include "mime-utils.h" -#include "mime-inputsource.h" #include "convert.h" -#include <string> -#include <vector> -#include <map> +#include "mime-inputsource.h" +#include "mime-utils.h" +#include "mime.h" + #include <exception> #include <iostream> +#include <map> +#include <string> +#include <vector> -#include <string.h> #include <ctype.h> -#include <stdio.h> #include <errno.h> +#include <stdio.h> +#include <string.h> Binc::MimeInputSource *mimeSource = 0; @@ -26,8 +27,7 @@ using namespace ::std; //------------------------------------------------------------------------ void Binc::MimeDocument::parseFull(int fd) const { - if (allIsParsed) - return; + if (allIsParsed) return; allIsParsed = true; @@ -52,7 +52,8 @@ void Binc::MimeDocument::parseFull(int fd) const // eat any trailing junk to get the correct size char c; - while (mimeSource->getChar(&c)); + while (mimeSource->getChar(&c)) + ; size = mimeSource->getOffset(); } @@ -72,7 +73,7 @@ static bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines) // rewind back to the start of the line and assume we're at the // start of the body. if (c == '\r') { - for (int i = 0; i < (int) name.length() + 1; ++i) + for (int i = 0; i < (int)name.length() + 1; ++i) mimeSource->ungetChar(); return false; } @@ -112,8 +113,7 @@ static bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines) // whitespace, then rewind one character and store the current // key,value pair. if (cqueue[2] == '\n' && c != ' ' && c != '\t') { - if (content.length() > 2) - content.resize(content.length() - 2); + if (content.length() > 2) content.resize(content.length() - 2); trim(content); header->add(name, content); @@ -132,8 +132,7 @@ static bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines) } if (name != "") { - if (content.length() > 2) - content.resize(content.length() - 2); + if (content.length() > 2) content.resize(content.length() - 2); header->add(name, content); } @@ -143,13 +142,16 @@ static bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines) //------------------------------------------------------------------------ static void parseHeader(Binc::Header *header, unsigned int *nlines) { - while (parseOneHeaderLine(header, nlines)) - { } + while (parseOneHeaderLine(header, nlines)) { + } } //------------------------------------------------------------------------ -static void analyzeHeader(Binc::Header *header, bool *multipart, - bool *messagerfc822, string *subtype, string *boundary) +static void analyzeHeader(Binc::Header *header, + bool *multipart, + bool *messagerfc822, + string *subtype, + string *boundary) { using namespace ::Binc; @@ -178,13 +180,11 @@ static void analyzeHeader(Binc::Header *header, bool *multipart, *subtype = value; } else if (key == "message") { lowercase(value); - if (value == "rfc822") - *messagerfc822 = true; + if (value == "rfc822") *messagerfc822 = true; } } - for (vector<string>::const_iterator i = types.begin(); - i != types.end(); ++i) { + for (vector<string>::const_iterator i = types.begin(); i != types.end(); ++i) { string element = *i; trim(element); @@ -220,19 +220,18 @@ static void parseMessageRFC822(vector<Binc::MimePart> *members, MimePart m; unsigned int bodystartoffsetcrlf = mimeSource->getOffset(); - + // parsefull returns the number of bytes that need to be removed // from the body because of the terminating boundary string. int bsize = 0; - if (m.parseFull(toboundary, bsize)) - *foundendofpart = true; + if (m.parseFull(toboundary, bsize)) *foundendofpart = true; - // make sure bodylength doesn't overflow + // make sure bodylength doesn't overflow *bodylength = mimeSource->getOffset(); if (*bodylength >= bodystartoffsetcrlf) { *bodylength -= bodystartoffsetcrlf; - if (*bodylength >= (unsigned int) bsize) { - *bodylength -= (unsigned int) bsize; + if (*bodylength >= (unsigned int)bsize) { + *bodylength -= (unsigned int)bsize; } else { *bodylength = 0; } @@ -245,8 +244,7 @@ static void parseMessageRFC822(vector<Binc::MimePart> *members, members->push_back(m); } -static bool skipUntilBoundary(const string &delimiter, - unsigned int *nlines, bool *eof) +static bool skipUntilBoundary(const string &delimiter, unsigned int *nlines, bool *eof) { int endpos = delimiter.length(); char *delimiterqueue = 0; @@ -263,7 +261,7 @@ static bool skipUntilBoundary(const string &delimiter, char c; bool foundBoundary = false; - for (;;) { + for (;;) { if (!mimeSource->getChar(&c)) { *eof = true; break; @@ -277,8 +275,7 @@ static bool skipUntilBoundary(const string &delimiter, delimiterqueue[delimiterpos++ % endpos] = c; - if (compareStringToQueue(delimiterStr, delimiterqueue, - delimiterpos, endpos)) { + if (compareStringToQueue(delimiterStr, delimiterqueue, delimiterpos, endpos)) { foundBoundary = true; break; } @@ -290,7 +287,6 @@ static bool skipUntilBoundary(const string &delimiter, return foundBoundary; } - static void parseMultipart(const string &boundary, const string &toboundary, bool *eof, @@ -319,7 +315,7 @@ static void parseMultipart(const string &boundary, char a; if (!mimeSource->getChar(&a)) *eof = true; - if (a == '\n') ++*nlines; + if (a == '\n') ++*nlines; char b; if (!mimeSource->getChar(&b)) *eof = true; @@ -333,7 +329,7 @@ static void parseMultipart(const string &boundary, *boundarysize += 2; if (!mimeSource->getChar(&a)) *eof = true; - if (a == '\n') ++*nlines; + if (a == '\n') ++*nlines; if (!mimeSource->getChar(&b)) *eof = true; if (b == '\n') ++*nlines; } @@ -343,7 +339,7 @@ static void parseMultipart(const string &boundary, // delimiter of one part is not followed by CRLF, but // immediately followed by a CRLF prefixed delimiter. if (!mimeSource->getChar(&a) || !mimeSource->getChar(&b)) - *eof = true; + *eof = true; else if (a == '-' && b == '-') { mimeSource->ungetChar(); mimeSource->ungetChar(); @@ -397,7 +393,7 @@ static void parseMultipart(const string &boundary, char a = '\0'; if (!mimeSource->getChar(&a)) *eof = true; - if (a == '\n') ++*nlines; + if (a == '\n') ++*nlines; char b = '\0'; if (!mimeSource->getChar(&b)) *eof = true; @@ -409,22 +405,18 @@ static void parseMultipart(const string &boundary, if (a == '-' && b == '-') { *foundendofpart = true; *boundarysize += 2; - if (!mimeSource->getChar(&a)) - *eof = true; - if (a == '\n') - ++*nlines; - if (!mimeSource->getChar(&b)) - *eof = true; - if (b == '\n') - ++*nlines; + if (!mimeSource->getChar(&a)) *eof = true; + if (a == '\n') ++*nlines; + if (!mimeSource->getChar(&b)) *eof = true; + if (b == '\n') ++*nlines; } if (a == '\r' && b == '\n') { - // This exception is to handle a special case where the - // delimiter of one part is not followed by CRLF, but - // immediately followed by a CRLF prefixed delimiter. + // This exception is to handle a special case where the + // delimiter of one part is not followed by CRLF, but + // immediately followed by a CRLF prefixed delimiter. if (!mimeSource->getChar(&a) || !mimeSource->getChar(&b)) - *eof = true; + *eof = true; else if (a == '-' && b == '-') { mimeSource->ungetChar(); mimeSource->ungetChar(); @@ -443,12 +435,12 @@ static void parseMultipart(const string &boundary, } } - // make sure bodylength doesn't overflow + // make sure bodylength doesn't overflow *bodylength = mimeSource->getOffset(); if (*bodylength >= bodystartoffsetcrlf) { *bodylength -= bodystartoffsetcrlf; - if (*bodylength >= (unsigned int) *boundarysize) { - *bodylength -= (unsigned int) *boundarysize; + if (*bodylength >= (unsigned int)*boundarysize) { + *bodylength -= (unsigned int)*boundarysize; } else { *bodylength = 0; } @@ -458,18 +450,19 @@ static void parseMultipart(const string &boundary, } static void parseSinglePart(const string &toboundary, - int *boundarysize, - unsigned int *nbodylines, - unsigned int *nlines, - bool *eof, bool *foundendofpart, - unsigned int *bodylength) + int *boundarysize, + unsigned int *nbodylines, + unsigned int *nlines, + bool *eof, + bool *foundendofpart, + unsigned int *bodylength) { using namespace ::Binc; unsigned int bodystartoffsetcrlf = mimeSource->getOffset(); // If toboundary is empty, then we read until the end of the // file. Otherwise we will read until we encounter toboundary. - string _toboundary; + string _toboundary; if (toboundary != "") { _toboundary = "\r\n--"; _toboundary += toboundary; @@ -493,14 +486,16 @@ static void parseSinglePart(const string &toboundary, bool toboundaryIsEmpty = (toboundary == ""); char c; while (mimeSource->getChar(&c)) { - if (c == '\n') { ++*nbodylines; ++*nlines; } + if (c == '\n') { + ++*nbodylines; + ++*nlines; + } if (toboundaryIsEmpty) continue; // find boundary boundaryqueue[boundarypos++ % endpos] = c; - if (compareStringToQueue(_toboundaryStr, boundaryqueue, - boundarypos, endpos)) { + if (compareStringToQueue(_toboundaryStr, boundaryqueue, boundarypos, endpos)) { *boundarysize = _toboundary.length(); break; } @@ -509,7 +504,6 @@ static void parseSinglePart(const string &toboundary, delete[] boundaryqueue; if (toboundary != "") { - char a; if (!mimeSource->getChar(&a)) *eof = true; if (a == '\n') ++*nlines; @@ -532,12 +526,12 @@ static void parseSinglePart(const string &toboundary, // delimiter of one part is not followed by CRLF, but // immediately followed by a CRLF prefixed delimiter. if (!mimeSource->getChar(&a) || !mimeSource->getChar(&b)) - *eof = true; + *eof = true; else if (a == '-' && b == '-') { - mimeSource->ungetChar(); - mimeSource->ungetChar(); - mimeSource->ungetChar(); - mimeSource->ungetChar(); + mimeSource->ungetChar(); + mimeSource->ungetChar(); + mimeSource->ungetChar(); + mimeSource->ungetChar(); } else { mimeSource->ungetChar(); mimeSource->ungetChar(); @@ -550,24 +544,22 @@ static void parseSinglePart(const string &toboundary, } } - // make sure bodylength doesn't overflow + // make sure bodylength doesn't overflow *bodylength = mimeSource->getOffset(); if (*bodylength >= bodystartoffsetcrlf) { *bodylength -= bodystartoffsetcrlf; - if (*bodylength >= (unsigned int) *boundarysize) { - *bodylength -= (unsigned int) *boundarysize; + if (*bodylength >= (unsigned int)*boundarysize) { + *bodylength -= (unsigned int)*boundarysize; } else { *bodylength = 0; } } else { *bodylength = 0; } - } //------------------------------------------------------------------------ -int Binc::MimePart::parseFull(const string &toboundary, - int &boundarysize) const +int Binc::MimePart::parseFull(const string &toboundary, int &boundarysize) const { headerstartoffsetcrlf = mimeSource->getOffset(); @@ -588,15 +580,19 @@ int Binc::MimePart::parseFull(const string &toboundary, bool foundendofpart = false; if (messagerfc822) { - parseMessageRFC822(&members, &foundendofpart, &bodylength, - &nbodylines, toboundary); + parseMessageRFC822(&members, &foundendofpart, &bodylength, &nbodylines, toboundary); } else if (multipart) { - parseMultipart(boundary, toboundary, &eof, &nlines, &boundarysize, - &foundendofpart, &bodylength, &members); + parseMultipart(boundary, + toboundary, + &eof, + &nlines, + &boundarysize, + &foundendofpart, + &bodylength, + &members); } else { - parseSinglePart(toboundary, &boundarysize, &nbodylines, &nlines, - &eof, &foundendofpart, &bodylength); + parseSinglePart(toboundary, &boundarysize, &nbodylines, &nlines, &eof, &foundendofpart, &bodylength); } return (eof || foundendofpart) ? 1 : 0; |