/* */ #include "ChunkedDecodingStreamFilter.h" #include #include "util.h" #include "message.h" #include "DlAbortEx.h" #include "A2STR.h" namespace aria2 { const std::string ChunkedDecodingStreamFilter::NAME ("ChunkedDecodingStreamFilter"); namespace { enum { PREV_CHUNK_SIZE, CHUNK_SIZE, CHUNK_EXTENSION, PREV_CHUNK_SIZE_LF, CHUNK, PREV_CHUNK_CR, PREV_CHUNK_LF, PREV_TRAILER, TRAILER, PREV_TRAILER_LF, PREV_END_CR, PREV_END_LF, CHUNKS_COMPLETE }; } // namespace ChunkedDecodingStreamFilter::ChunkedDecodingStreamFilter (const std::shared_ptr& delegate): StreamFilter(delegate), state_(PREV_CHUNK_SIZE), chunkSize_(0), chunkRemaining_(0), bytesProcessed_(0) {} ChunkedDecodingStreamFilter::~ChunkedDecodingStreamFilter() {} void ChunkedDecodingStreamFilter::init() {} ssize_t ChunkedDecodingStreamFilter::transform (const std::shared_ptr& out, const std::shared_ptr& segment, const unsigned char* inbuf, size_t inlen) { ssize_t outlen = 0; size_t i; bytesProcessed_ = 0; for(i = 0; i < inlen; ++i) { unsigned char c = inbuf[i]; switch(state_) { case PREV_CHUNK_SIZE: if(util::isHexDigit(c)) { chunkSize_ = util::hexCharToUInt(c); state_ = CHUNK_SIZE; } else { throw DL_ABORT_EX("Bad chunk size: not hex string"); } break; case CHUNK_SIZE: if(util::isHexDigit(c)) { if(chunkSize_ & 0x7800000000000000LL) { throw DL_ABORT_EX("Too big chunk size"); } chunkSize_ <<= 4; chunkSize_ += util::hexCharToUInt(c); } else if(c == ';') { state_ = CHUNK_EXTENSION; } else if(c == '\r') { state_ = PREV_CHUNK_SIZE_LF; } else { throw DL_ABORT_EX("Bad chunk size: not hex string"); } break; case CHUNK_EXTENSION: if(c == '\r') { state_ = PREV_CHUNK_SIZE_LF; } break; case PREV_CHUNK_SIZE_LF: if(c == '\n') { chunkRemaining_ = chunkSize_; if(chunkSize_ == 0) { state_ = PREV_TRAILER; } else { state_ = CHUNK; } } else { throw DL_ABORT_EX("Bad chunk encoding: " "missing LF at the end of chunk size"); } break; case CHUNK: { int64_t readlen = std::min(chunkRemaining_, static_cast(inlen-i)); outlen += getDelegate()->transform(out, segment, inbuf+i, readlen); chunkRemaining_ -= readlen; i += readlen-1; if(chunkRemaining_ == 0) { state_ = PREV_CHUNK_CR; } break; } case PREV_CHUNK_CR: if(c == '\r') { state_ = PREV_CHUNK_LF; } else { throw DL_ABORT_EX("Bad chunk encoding: " "missing CR at the end of chunk"); } break; case PREV_CHUNK_LF: if(c == '\n') { if(chunkSize_ == 0) { state_ = PREV_TRAILER; } else { chunkSize_ = chunkRemaining_ = 0; state_ = PREV_CHUNK_SIZE; } } else { throw DL_ABORT_EX("Bad chunk encoding: " "missing LF at the end of chunk"); } break; case PREV_TRAILER: if(c == '\r') { // No trailer state_ = PREV_END_LF; } else { state_= TRAILER; } break; case TRAILER: if(c == '\r') { state_ = PREV_TRAILER_LF; } break; case PREV_TRAILER_LF: if(c == '\n') { state_ = PREV_TRAILER; } else { throw DL_ABORT_EX("Bad chunk encoding: " "missing LF at the end of trailer"); } break; case PREV_END_LF: if(c == '\n') { state_ = CHUNKS_COMPLETE; } else { throw DL_ABORT_EX("Bad chunk encoding: " "missing LF at the end of chunks"); } break; case CHUNKS_COMPLETE: goto fin; default: // unreachable assert(0); } } fin: bytesProcessed_ += i; return outlen; } bool ChunkedDecodingStreamFilter::finished() { return state_ == CHUNKS_COMPLETE && getDelegate()->finished(); } void ChunkedDecodingStreamFilter::release() {} const std::string& ChunkedDecodingStreamFilter::getName() const { return NAME; } } // namespace aria2