ChunkedEncoding.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <string.h>
  39. #include <strings.h>
  40. #include <errno.h>
  41. #define MAX_BUFSIZE (1024*1024)
  42. ChunkedEncoding::ChunkedEncoding() {
  43. strbufSize = 4096;
  44. strbuf = new char[strbufSize];
  45. strbufTail = strbuf;
  46. state = READ_SIZE;
  47. chunkSize = 0;
  48. }
  49. ChunkedEncoding::~ChunkedEncoding() {
  50. if(strbuf != NULL) {
  51. delete [] strbuf;
  52. }
  53. }
  54. void ChunkedEncoding::init() {
  55. }
  56. bool ChunkedEncoding::finished() {
  57. return state == FINISH ? true : false;
  58. }
  59. void ChunkedEncoding::end() {}
  60. void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen) {
  61. addBuffer(inbuf, inlen);
  62. char* p = strbuf;
  63. int32_t clen = 0;
  64. while(1) {
  65. if(state == READ_SIZE) {
  66. if(readChunkSize(&p) == 0) {
  67. if(chunkSize == 0) {
  68. state = FINISH;
  69. } else {
  70. state = READ_DATA;
  71. }
  72. } else {
  73. // chunk size is not fully received.
  74. break;
  75. }
  76. } else if(state == READ_DATA) {
  77. if(readData(&p, outbuf, clen, outlen) == 0) {
  78. state = READ_SIZE;
  79. } else {
  80. break;
  81. }
  82. } else {
  83. break;
  84. }
  85. // all bytes in strbuf were examined?
  86. if(strbufTail <= p) {
  87. break;
  88. }
  89. }
  90. if(strbufTail <= p) {
  91. strbufTail = strbuf;
  92. } else {
  93. // copy string between [p, strbufTail]
  94. int32_t unreadSize = strbufTail-p;
  95. char* temp = new char[strbufSize];
  96. memcpy(temp, p, unreadSize);
  97. delete [] strbuf;
  98. strbuf = temp;
  99. strbufTail = strbuf+unreadSize;
  100. }
  101. outlen = clen;
  102. }
  103. int32_t ChunkedEncoding::readData(char** pp, char* buf, int32_t& len, int32_t maxlen) {
  104. if(buf+len == buf+maxlen) {
  105. return -1;
  106. }
  107. if(chunkSize == 0) {
  108. return readDataEOL(pp);
  109. }
  110. int32_t wsize;
  111. if(strbufTail-*pp < chunkSize) {
  112. wsize = strbufTail-*pp <= maxlen-len ? strbufTail-*pp : maxlen-len;
  113. } else {
  114. wsize = chunkSize <= maxlen-len ? chunkSize : maxlen-len;
  115. }
  116. memcpy(buf+len, *pp, wsize);
  117. chunkSize -= wsize;
  118. len += wsize;
  119. *pp += wsize;
  120. if(chunkSize == 0) {
  121. return readDataEOL(pp);
  122. } else {
  123. return -1;
  124. }
  125. }
  126. int32_t ChunkedEncoding::readDataEOL(char** pp) {
  127. char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
  128. char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
  129. if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
  130. *pp += 2;
  131. return 0;
  132. } else if(strbufTail-*pp < 2) {
  133. return -1;
  134. } else {
  135. throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  136. }
  137. }
  138. int32_t ChunkedEncoding::readChunkSize(char** pp) {
  139. // we read chunk-size from *pp
  140. char* p;
  141. char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
  142. char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
  143. if(np == NULL || rp == NULL || np-rp != 1) {
  144. // \r\n is not found. Return -1
  145. return -1;
  146. }
  147. p = rp;
  148. // We ignore chunk-extension
  149. char* exsp = (char*)memchr(*pp, ';', strbufTail-*pp);
  150. if(exsp == NULL) {
  151. exsp = p;
  152. }
  153. // TODO check invalid characters in buffer
  154. char* temp = new char[exsp-*pp+1];
  155. memcpy(temp, *pp, exsp-*pp);
  156. temp[exsp-*pp] = '\0';
  157. chunkSize = strtol(temp, NULL, 16);
  158. delete [] temp;
  159. if(chunkSize < 0) {
  160. throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  161. } else if(errno == ERANGE && (chunkSize == INT32_MAX || chunkSize == INT32_MIN)) {
  162. throw new DlAbortEx(strerror(errno));
  163. }
  164. *pp = p+2;
  165. return 0;
  166. }
  167. void ChunkedEncoding::addBuffer(const char* inbuf, int32_t inlen) {
  168. int32_t realbufSize = strbufTail-strbuf;
  169. if(realbufSize+inlen >= strbufSize) {
  170. if(realbufSize+inlen > MAX_BUFSIZE) {
  171. throw new DlAbortEx(EX_TOO_LARGE_CHUNK, realbufSize+inlen);
  172. }
  173. strbufSize = realbufSize+inlen;
  174. char* temp = new char[strbufSize];
  175. memcpy(temp, strbuf, realbufSize);
  176. delete [] strbuf;
  177. strbuf = temp;
  178. strbufTail = strbuf+realbufSize;
  179. }
  180. memcpy(strbufTail, inbuf, inlen);
  181. strbufTail += inlen;
  182. }