ChunkedEncoding.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "ChunkedEncoding.h"
  36. #include "DlAbortEx.h"
  37. #include "message.h"
  38. #include "Util.h"
  39. #include <string.h>
  40. #include <strings.h>
  41. #include <errno.h>
  42. #define MAX_BUFSIZE (1024*1024)
  43. ChunkedEncoding::ChunkedEncoding() {
  44. strbufSize = 4096;
  45. strbuf = new char[strbufSize];
  46. strbufTail = strbuf;
  47. state = READ_SIZE;
  48. chunkSize = 0;
  49. }
  50. ChunkedEncoding::~ChunkedEncoding() {
  51. if(strbuf != NULL) {
  52. delete [] strbuf;
  53. }
  54. }
  55. void ChunkedEncoding::init() {
  56. }
  57. bool ChunkedEncoding::finished() {
  58. return state == FINISH ? true : false;
  59. }
  60. void ChunkedEncoding::end() {}
  61. void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen) {
  62. addBuffer(inbuf, inlen);
  63. char* p = strbuf;
  64. int32_t clen = 0;
  65. while(1) {
  66. if(state == READ_SIZE) {
  67. if(readChunkSize(&p) == 0) {
  68. if(chunkSize == 0) {
  69. state = FINISH;
  70. } else {
  71. state = READ_DATA;
  72. }
  73. } else {
  74. // chunk size is not fully received.
  75. break;
  76. }
  77. } else if(state == READ_DATA) {
  78. if(readData(&p, outbuf, clen, outlen) == 0) {
  79. state = READ_SIZE;
  80. } else {
  81. break;
  82. }
  83. } else {
  84. break;
  85. }
  86. // all bytes in strbuf were examined?
  87. if(strbufTail <= p) {
  88. break;
  89. }
  90. }
  91. if(strbufTail <= p) {
  92. strbufTail = strbuf;
  93. } else {
  94. // copy string between [p, strbufTail]
  95. int32_t unreadSize = strbufTail-p;
  96. char* temp = new char[strbufSize];
  97. memcpy(temp, p, unreadSize);
  98. delete [] strbuf;
  99. strbuf = temp;
  100. strbufTail = strbuf+unreadSize;
  101. }
  102. outlen = clen;
  103. }
  104. int32_t ChunkedEncoding::readData(char** pp, char* buf, int32_t& len, int32_t maxlen) {
  105. if(buf+len == buf+maxlen) {
  106. return -1;
  107. }
  108. if(chunkSize == 0) {
  109. return readDataEOL(pp);
  110. }
  111. int32_t wsize;
  112. if(strbufTail-*pp < chunkSize) {
  113. wsize = strbufTail-*pp <= maxlen-len ? strbufTail-*pp : maxlen-len;
  114. } else {
  115. wsize = chunkSize <= maxlen-len ? chunkSize : maxlen-len;
  116. }
  117. memcpy(buf+len, *pp, wsize);
  118. chunkSize -= wsize;
  119. len += wsize;
  120. *pp += wsize;
  121. if(chunkSize == 0) {
  122. return readDataEOL(pp);
  123. } else {
  124. return -1;
  125. }
  126. }
  127. int32_t ChunkedEncoding::readDataEOL(char** pp) {
  128. char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
  129. char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
  130. if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
  131. *pp += 2;
  132. return 0;
  133. } else if(strbufTail-*pp < 2) {
  134. return -1;
  135. } else {
  136. throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  137. }
  138. }
  139. int32_t ChunkedEncoding::readChunkSize(char** pp) {
  140. // we read chunk-size from *pp
  141. char* p;
  142. char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
  143. char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
  144. if(np == NULL || rp == NULL || np-rp != 1) {
  145. // \r\n is not found. Return -1
  146. return -1;
  147. }
  148. p = rp;
  149. // We ignore chunk-extension
  150. char* exsp = (char*)memchr(*pp, ';', strbufTail-*pp);
  151. if(exsp == 0 || p < exsp) {
  152. exsp = p;
  153. }
  154. string temp(*pp, exsp);
  155. chunkSize = Util::parseInt(temp, 16);
  156. if(chunkSize < 0) {
  157. throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  158. }
  159. *pp = p+2;
  160. return 0;
  161. }
  162. void ChunkedEncoding::addBuffer(const char* inbuf, int32_t inlen) {
  163. int32_t realbufSize = strbufTail-strbuf;
  164. if(realbufSize+inlen >= strbufSize) {
  165. if(realbufSize+inlen > MAX_BUFSIZE) {
  166. throw new DlAbortEx(EX_TOO_LARGE_CHUNK, realbufSize+inlen);
  167. }
  168. strbufSize = realbufSize+inlen;
  169. char* temp = new char[strbufSize];
  170. memcpy(temp, strbuf, realbufSize);
  171. delete [] strbuf;
  172. strbuf = temp;
  173. strbufTail = strbuf+realbufSize;
  174. }
  175. memcpy(strbufTail, inbuf, inlen);
  176. strbufTail += inlen;
  177. }