#include "IMFBoundary.h" #include "Exception.h" namespace coreutils { IMFBoundary::IMFBoundary(StreamReader &reader, ZString boundary) : Parser(reader), boundary(boundary) {} Parser::Status IMFBoundary::parse() { MString &buffer = reader.getBuffer(); switch (state) { case PREFIX: { MString marker; marker << "\r\n--"; marker << boundary; int pos = buffer.find(marker); if (pos < 0) return Status::INCOMPLETE; if (!buffer.ifNext(marker)) throw coreutils::Exception("IMFBoundary: boundary marker vanished between find and ifNext."); state = TAIL; return Status::DATA_AVAIL; } case TAIL: // ZString::getLength() is already cursor-relative (bytes remaining // from the cursor to the end) - do not subtract the cursor offset // again here. if (buffer.getLength() < 2) return Status::INCOMPLETE; if (buffer.ifNext(ZString("--"))) { last = true; return Status::COMPLETE; } if (buffer.ifNext(ZString("\r\n"))) { last = false; return Status::COMPLETE; } throw coreutils::Exception("IMFBoundary: malformed boundary line - expected CRLF or trailing --."); } throw coreutils::Exception("IMFBoundary: unreachable state."); } bool IMFBoundary::isLast() { return last; } }