2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "ChunkedEncoding.h"
|
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "message.h"
|
2007-11-22 11:17:35 +00:00
|
|
|
#include "Util.h"
|
2008-04-27 02:22:14 +00:00
|
|
|
#include "StringFormat.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-02-17 18:51:12 +00:00
|
|
|
#define MAX_BUFSIZE (1024*1024)
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
ChunkedEncoding::ChunkedEncoding() {
|
|
|
|
strbufSize = 4096;
|
2008-03-07 12:05:50 +00:00
|
|
|
strbuf = new unsigned char[strbufSize];
|
2006-03-09 14:52:07 +00:00
|
|
|
strbufTail = strbuf;
|
2006-02-17 13:35:04 +00:00
|
|
|
state = READ_SIZE;
|
|
|
|
chunkSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChunkedEncoding::~ChunkedEncoding() {
|
2008-02-08 15:53:45 +00:00
|
|
|
delete [] strbuf;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChunkedEncoding::init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChunkedEncoding::finished() {
|
|
|
|
return state == FINISH ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChunkedEncoding::end() {}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void ChunkedEncoding::inflate(unsigned char* outbuf, size_t& outlen,
|
|
|
|
const unsigned char* inbuf, size_t inlen) {
|
2006-02-17 13:35:04 +00:00
|
|
|
addBuffer(inbuf, inlen);
|
2008-03-07 12:05:50 +00:00
|
|
|
unsigned char* p = strbuf;
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t clen = 0;
|
2006-02-17 13:35:04 +00:00
|
|
|
while(1) {
|
|
|
|
if(state == READ_SIZE) {
|
|
|
|
if(readChunkSize(&p) == 0) {
|
|
|
|
if(chunkSize == 0) {
|
|
|
|
state = FINISH;
|
|
|
|
} else {
|
|
|
|
state = READ_DATA;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// chunk size is not fully received.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if(state == READ_DATA) {
|
|
|
|
if(readData(&p, outbuf, clen, outlen) == 0) {
|
|
|
|
state = READ_SIZE;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2006-03-09 14:52:07 +00:00
|
|
|
// all bytes in strbuf were examined?
|
|
|
|
if(strbufTail <= p) {
|
2006-02-17 13:35:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-03-09 14:52:07 +00:00
|
|
|
if(strbufTail <= p) {
|
|
|
|
strbufTail = strbuf;
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
2006-03-09 14:52:07 +00:00
|
|
|
// copy string between [p, strbufTail]
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t unreadSize = strbufTail-p;
|
2008-03-07 12:05:50 +00:00
|
|
|
unsigned char* temp = new unsigned char[strbufSize];
|
2006-03-09 14:52:07 +00:00
|
|
|
memcpy(temp, p, unreadSize);
|
2006-02-17 13:35:04 +00:00
|
|
|
delete [] strbuf;
|
|
|
|
strbuf = temp;
|
2006-03-09 14:52:07 +00:00
|
|
|
strbufTail = strbuf+unreadSize;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
outlen = clen;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
int ChunkedEncoding::readData(unsigned char** pp,
|
|
|
|
unsigned char* buf, size_t& len,
|
|
|
|
size_t maxlen)
|
2008-03-07 12:05:50 +00:00
|
|
|
{
|
2006-02-17 13:35:04 +00:00
|
|
|
if(buf+len == buf+maxlen) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(chunkSize == 0) {
|
|
|
|
return readDataEOL(pp);
|
|
|
|
}
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t wsize;
|
|
|
|
if((size_t)(strbufTail-*pp) < chunkSize) {
|
|
|
|
wsize = std::min((size_t)(strbufTail-*pp), maxlen-len);
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
2008-03-09 12:24:01 +00:00
|
|
|
wsize = std::min(chunkSize, maxlen-len);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
memcpy(buf+len, *pp, wsize);
|
|
|
|
chunkSize -= wsize;
|
|
|
|
len += wsize;
|
|
|
|
*pp += wsize;
|
|
|
|
if(chunkSize == 0) {
|
|
|
|
return readDataEOL(pp);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
int ChunkedEncoding::readDataEOL(unsigned char** pp) {
|
2008-03-07 12:05:50 +00:00
|
|
|
unsigned char* np = reinterpret_cast<unsigned char*>(memchr(*pp, '\n', strbufTail-*pp));
|
|
|
|
unsigned char* rp = reinterpret_cast<unsigned char*>(memchr(*pp, '\r', strbufTail-*pp));
|
2006-03-09 14:52:07 +00:00
|
|
|
if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
|
2006-02-17 13:35:04 +00:00
|
|
|
*pp += 2;
|
|
|
|
return 0;
|
2006-03-09 14:52:07 +00:00
|
|
|
} else if(strbufTail-*pp < 2) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2008-04-27 02:22:14 +00:00
|
|
|
throw DlAbortEx(EX_INVALID_CHUNK_SIZE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
int ChunkedEncoding::readChunkSize(unsigned char** pp) {
|
2006-02-17 13:35:04 +00:00
|
|
|
// we read chunk-size from *pp
|
2008-03-07 12:05:50 +00:00
|
|
|
unsigned char* p;
|
|
|
|
unsigned char* np = reinterpret_cast<unsigned char*>(memchr(*pp, '\n', strbufTail-*pp));
|
|
|
|
unsigned char* rp = reinterpret_cast<unsigned char*>(memchr(*pp, '\r', strbufTail-*pp));
|
2006-03-09 14:52:07 +00:00
|
|
|
if(np == NULL || rp == NULL || np-rp != 1) {
|
|
|
|
// \r\n is not found. Return -1
|
2006-02-17 13:35:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-03-09 14:52:07 +00:00
|
|
|
p = rp;
|
2006-02-17 13:35:04 +00:00
|
|
|
// We ignore chunk-extension
|
2008-03-07 12:05:50 +00:00
|
|
|
unsigned char* exsp = reinterpret_cast<unsigned char*>(memchr(*pp, ';', strbufTail-*pp));
|
2007-11-22 11:17:35 +00:00
|
|
|
if(exsp == 0 || p < exsp) {
|
2006-02-17 13:35:04 +00:00
|
|
|
exsp = p;
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string temp(*pp, exsp);
|
2007-11-22 11:17:35 +00:00
|
|
|
chunkSize = Util::parseInt(temp, 16);
|
2006-02-17 13:35:04 +00:00
|
|
|
if(chunkSize < 0) {
|
2008-04-27 02:22:14 +00:00
|
|
|
throw DlAbortEx(EX_INVALID_CHUNK_SIZE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
*pp = p+2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void ChunkedEncoding::addBuffer(const unsigned char* inbuf, size_t inlen) {
|
|
|
|
size_t realbufSize = strbufTail-strbuf;
|
2006-03-09 14:52:07 +00:00
|
|
|
if(realbufSize+inlen >= strbufSize) {
|
|
|
|
if(realbufSize+inlen > MAX_BUFSIZE) {
|
2008-04-27 02:22:14 +00:00
|
|
|
throw DlAbortEx
|
|
|
|
(StringFormat(EX_TOO_LARGE_CHUNK, realbufSize+inlen).str());
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-03-09 14:52:07 +00:00
|
|
|
strbufSize = realbufSize+inlen;
|
2008-03-07 12:05:50 +00:00
|
|
|
unsigned char* temp = new unsigned char[strbufSize];
|
2006-03-09 14:52:07 +00:00
|
|
|
memcpy(temp, strbuf, realbufSize);
|
2006-02-17 13:35:04 +00:00
|
|
|
delete [] strbuf;
|
|
|
|
strbuf = temp;
|
2006-03-09 14:52:07 +00:00
|
|
|
strbufTail = strbuf+realbufSize;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-03-09 14:52:07 +00:00
|
|
|
memcpy(strbufTail, inbuf, inlen);
|
|
|
|
strbufTail += inlen;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|