| 1 | // |
|---|---|
| 2 | // QuotedPrintableDecoder.cpp |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: Messages |
| 6 | // Module: QuotedPrintableDecoder |
| 7 | // |
| 8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/QuotedPrintableDecoder.h" |
| 16 | #include "Poco/NumberParser.h" |
| 17 | #include "Poco/Exception.h" |
| 18 | #include "Poco/Ascii.h" |
| 19 | |
| 20 | |
| 21 | using Poco::UnbufferedStreamBuf; |
| 22 | using Poco::NumberParser; |
| 23 | using Poco::DataFormatException; |
| 24 | |
| 25 | |
| 26 | namespace Poco { |
| 27 | namespace Net { |
| 28 | |
| 29 | |
| 30 | QuotedPrintableDecoderBuf::QuotedPrintableDecoderBuf(std::istream& istr): |
| 31 | _buf(*istr.rdbuf()) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | |
| 36 | QuotedPrintableDecoderBuf::~QuotedPrintableDecoderBuf() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | int QuotedPrintableDecoderBuf::readFromDevice() |
| 42 | { |
| 43 | int ch = _buf.sbumpc(); |
| 44 | while (ch == '=') |
| 45 | { |
| 46 | ch = _buf.sbumpc(); |
| 47 | if (ch == '\r') |
| 48 | { |
| 49 | _buf.sbumpc(); // read \n |
| 50 | } |
| 51 | else if (Poco::Ascii::isHexDigit(ch)) |
| 52 | { |
| 53 | std::string hex = "0x"; |
| 54 | hex += (char) ch; |
| 55 | ch = _buf.sbumpc(); |
| 56 | if (Poco::Ascii::isHexDigit(ch)) |
| 57 | { |
| 58 | hex += (char) ch; |
| 59 | return NumberParser::parseHex(hex); |
| 60 | } |
| 61 | throw DataFormatException("Incomplete hex number in quoted-printable encoded stream"); |
| 62 | } |
| 63 | else if (ch != '\n') |
| 64 | { |
| 65 | throw DataFormatException("Invalid occurrence of '=' in quoted-printable encoded stream"); |
| 66 | } |
| 67 | ch = _buf.sbumpc(); |
| 68 | } |
| 69 | return ch; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | QuotedPrintableDecoderIOS::QuotedPrintableDecoderIOS(std::istream& istr): _buf(istr) |
| 74 | { |
| 75 | poco_ios_init(&_buf); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | QuotedPrintableDecoderIOS::~QuotedPrintableDecoderIOS() |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | |
| 84 | QuotedPrintableDecoderBuf* QuotedPrintableDecoderIOS::rdbuf() |
| 85 | { |
| 86 | return &_buf; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | QuotedPrintableDecoder::QuotedPrintableDecoder(std::istream& istr): |
| 91 | QuotedPrintableDecoderIOS(istr), |
| 92 | std::istream(&_buf) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | |
| 97 | QuotedPrintableDecoder::~QuotedPrintableDecoder() |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | |
| 102 | } } // namespace Poco::Net |
| 103 |