size_t noopEncoder::decode(utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress) { in.reset(); // may not work... // No decoding performed size_t res = 0; if (progress) res = utility::bufferedStreamCopy(in, out, 0, progress); else res = utility::bufferedStreamCopy(in, out); return res; }
size_t qpEncoder::decode( utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress ) { in.reset(); // may not work... // Process the data const bool rfc2047 = getProperties().getProperty <bool>("rfc2047", false); byte_t buffer[16384]; size_t bufferLength = 0; size_t bufferPos = 0; byte_t outBuffer[16384]; size_t outBufferPos = 0; size_t total = 0; size_t inTotal = 0; while (bufferPos < bufferLength || !in.eof()) { // Flush current output buffer if (outBufferPos >= sizeof(outBuffer)) { QP_WRITE(out, outBuffer, outBufferPos); total += outBufferPos; outBufferPos = 0; } // Need to get more data? if (bufferPos >= bufferLength) { bufferLength = in.read(buffer, sizeof(buffer)); bufferPos = 0; // No more data if (bufferLength == 0) { break; } } // Decode the next sequence (hex-encoded byte or printable character) byte_t c = buffer[bufferPos++]; ++inTotal; switch (c) { case '=': { if (bufferPos >= bufferLength) { bufferLength = in.read(buffer, sizeof(buffer)); bufferPos = 0; } if (bufferPos < bufferLength) { c = buffer[bufferPos++]; ++inTotal; switch (c) { // Ignore soft line break ("=\r\n" or "=\n") case '\r': // Read one byte more if (bufferPos >= bufferLength) { bufferLength = in.read(buffer, sizeof(buffer)); bufferPos = 0; } if (bufferPos < bufferLength) { ++bufferPos; ++inTotal; } break; case '\n': break; // Hex-encoded char default: { // We need another byte... if (bufferPos >= bufferLength) { bufferLength = in.read(buffer, sizeof(buffer)); bufferPos = 0; } if (bufferPos < bufferLength) { const byte_t next = buffer[bufferPos++]; ++inTotal; const byte_t value = static_cast <byte_t>( sm_hexDecodeTable[c] * 16 + sm_hexDecodeTable[next] ); outBuffer[outBufferPos++] = value; } else { // Premature end-of-data } break; } } } else { // Premature end-of-data } break; } case '_': { if (rfc2047) { // RFC-2047, Page 5, 4.2. The "Q" encoding: // << Note that the "_" always represents hexadecimal 20, even if the SPACE // character occupies a different code position in the character set in use. >> outBuffer[outBufferPos++] = 0x20; break; } outBuffer[outBufferPos++] = c; break; } default: { outBuffer[outBufferPos++] = c; break; } } if (progress) { progress->progress(inTotal, inTotal); } } // Flush remaining output buffer if (outBufferPos != 0) { QP_WRITE(out, outBuffer, outBufferPos); total += outBufferPos; } if (progress) { progress->stop(inTotal); } return total; }
size_t qpEncoder::encode( utility::inputStream& in, utility::outputStream& out, utility::progressListener* progress ) { in.reset(); // may not work... const size_t propMaxLineLength = getProperties().getProperty <size_t>("maxlinelength", static_cast <size_t>(-1)); const bool rfc2047 = getProperties().getProperty <bool>("rfc2047", false); const bool text = getProperties().getProperty <bool>("text", false); // binary mode by default const bool cutLines = (propMaxLineLength != static_cast <size_t>(-1)); const size_t maxLineLength = std::min(propMaxLineLength, static_cast <size_t>(74)); // Process the data byte_t buffer[16384]; size_t bufferLength = 0; size_t bufferPos = 0; size_t curCol = 0; byte_t outBuffer[16384]; size_t outBufferPos = 0; size_t total = 0; size_t inTotal = 0; if (progress) { progress->start(0); } while (bufferPos < bufferLength || !in.eof()) { // Flush current output buffer if (outBufferPos + 6 >= static_cast <int>(sizeof(outBuffer))) { QP_WRITE(out, outBuffer, outBufferPos); total += outBufferPos; outBufferPos = 0; } // Need to get more data? if (bufferPos >= bufferLength) { bufferLength = in.read(buffer, sizeof(buffer)); bufferPos = 0; // No more data if (bufferLength == 0) { break; } } // Get the next char and encode it const byte_t c = buffer[bufferPos++]; if (rfc2047) { if (c >= 128 || sm_RFC2047EncodeTable[c] != 0) { if (c == 32) { // space // RFC-2047, Page 5, 4.2. The "Q" encoding: // << The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be // represented as "_" (underscore, ASCII 95.). >> outBuffer[outBufferPos++] = '_'; ++curCol; } else { // Other characters: '=' + hexadecimal encoding QP_ENCODE_HEX(c); } } else { // No encoding outBuffer[outBufferPos++] = c; ++curCol; } } else { switch (c) { case 46: { // . if (curCol == 0) { // If a '.' appears at the beginning of a line, we encode it to // to avoid problems with SMTP servers... ("\r\n.\r\n" means the // end of data transmission). QP_ENCODE_HEX('.'); continue; } outBuffer[outBufferPos++] = '.'; ++curCol; break; } case 32: { // space // Need to get more data? if (bufferPos >= bufferLength) { bufferLength = in.read(buffer, sizeof(buffer)); bufferPos = 0; } // Spaces cannot appear at the end of a line. So, encode the space. if (bufferPos >= bufferLength || (buffer[bufferPos] == '\r' || buffer[bufferPos] == '\n')) { QP_ENCODE_HEX(' '); } else { outBuffer[outBufferPos++] = ' '; ++curCol; } break; } case 9: { // TAB QP_ENCODE_HEX(c); break; } case 13: // CR case 10: { // LF // RFC-2045/6.7(4) // Text data if (text && !rfc2047) { outBuffer[outBufferPos++] = c; ++curCol; if (c == 10) { curCol = 0; // reset current line length } // Binary data } else { QP_ENCODE_HEX(c); } break; } case 61: { // = QP_ENCODE_HEX('='); break; } /* Rule #2: (Literal representation) Octets with decimal values of 33 through 60 inclusive, and 62 through 126, inclusive, MAY be represented as the ASCII characters which correspond to those octets (EXCLAMATION POINT through LESS THAN, and GREATER THAN through TILDE, respectively). */ default: //if ((c >= 33 && c <= 60) || (c >= 62 && c <= 126)) if (c >= 33 && c <= 126 && c != 61 && c != 63) { outBuffer[outBufferPos++] = c; ++curCol; // Other characters: '=' + hexadecimal encoding } else { QP_ENCODE_HEX(c); } break; } // switch (c) // Soft line break : "=\r\n" if (cutLines && curCol >= maxLineLength - 1) { outBuffer[outBufferPos] = '='; outBuffer[outBufferPos + 1] = '\r'; outBuffer[outBufferPos + 2] = '\n'; outBufferPos += 3; curCol = 0; } } // !rfc2047 ++inTotal; if (progress) { progress->progress(inTotal, inTotal); } } // Flush remaining output buffer if (outBufferPos != 0) { QP_WRITE(out, outBuffer, outBufferPos); total += outBufferPos; } if (progress) { progress->stop(inTotal); } return total; }