ChunkedEncoding.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "ChunkedEncoding.h"
  23. #include "DlAbortEx.h"
  24. #include "message.h"
  25. #include <string.h>
  26. #include <strings.h>
  27. #include <errno.h>
  28. #define MAX_BUFSIZE (1024*1024)
  29. ChunkedEncoding::ChunkedEncoding() {
  30. strbufSize = 4096;
  31. strbuf = new char[strbufSize];
  32. strbufTail = strbuf;
  33. state = READ_SIZE;
  34. chunkSize = 0;
  35. }
  36. ChunkedEncoding::~ChunkedEncoding() {
  37. if(strbuf != NULL) {
  38. delete [] strbuf;
  39. }
  40. }
  41. void ChunkedEncoding::init() {
  42. }
  43. bool ChunkedEncoding::finished() {
  44. return state == FINISH ? true : false;
  45. }
  46. void ChunkedEncoding::end() {}
  47. void ChunkedEncoding::inflate(char* outbuf, int& outlen, const char* inbuf, int inlen) {
  48. addBuffer(inbuf, inlen);
  49. char* p = strbuf;
  50. int clen = 0;
  51. while(1) {
  52. if(state == READ_SIZE) {
  53. if(readChunkSize(&p) == 0) {
  54. if(chunkSize == 0) {
  55. state = FINISH;
  56. } else {
  57. state = READ_DATA;
  58. }
  59. } else {
  60. // chunk size is not fully received.
  61. break;
  62. }
  63. } else if(state == READ_DATA) {
  64. if(readData(&p, outbuf, clen, outlen) == 0) {
  65. state = READ_SIZE;
  66. } else {
  67. break;
  68. }
  69. } else {
  70. break;
  71. }
  72. // all bytes in strbuf were examined?
  73. if(strbufTail <= p) {
  74. break;
  75. }
  76. }
  77. if(strbufTail <= p) {
  78. strbufTail = strbuf;
  79. } else {
  80. // copy string between [p, strbufTail]
  81. int unreadSize = strbufTail-p;
  82. char* temp = new char[strbufSize];
  83. memcpy(temp, p, unreadSize);
  84. delete [] strbuf;
  85. strbuf = temp;
  86. strbufTail = strbuf+unreadSize;
  87. }
  88. outlen = clen;
  89. }
  90. int ChunkedEncoding::readData(char** pp, char* buf, int& len, int maxlen) {
  91. if(buf+len == buf+maxlen) {
  92. return -1;
  93. }
  94. if(chunkSize == 0) {
  95. return readDataEOL(pp);
  96. }
  97. int wsize;
  98. if(strbufTail-*pp < chunkSize) {
  99. wsize = strbufTail-*pp <= maxlen-len ? strbufTail-*pp : maxlen-len;
  100. } else {
  101. wsize = chunkSize <= maxlen-len ? chunkSize : maxlen-len;
  102. }
  103. memcpy(buf+len, *pp, wsize);
  104. chunkSize -= wsize;
  105. len += wsize;
  106. *pp += wsize;
  107. if(chunkSize == 0) {
  108. return readDataEOL(pp);
  109. } else {
  110. return -1;
  111. }
  112. }
  113. int ChunkedEncoding::readDataEOL(char** pp) {
  114. char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
  115. char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
  116. if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
  117. *pp += 2;
  118. return 0;
  119. } else if(strbufTail-*pp < 2) {
  120. return -1;
  121. } else {
  122. throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  123. }
  124. }
  125. int ChunkedEncoding::readChunkSize(char** pp) {
  126. // we read chunk-size from *pp
  127. char* p;
  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) {
  131. // \r\n is not found. Return -1
  132. return -1;
  133. }
  134. p = rp;
  135. // We ignore chunk-extension
  136. char* exsp = (char*)memchr(*pp, ';', strbufTail-*pp);
  137. if(exsp == NULL) {
  138. exsp = p;
  139. }
  140. // TODO check invalid characters in buffer
  141. char* temp = new char[exsp-*pp+1];
  142. memcpy(temp, *pp, exsp-*pp);
  143. temp[exsp-*pp] = '\0';
  144. chunkSize = strtol(temp, NULL, 16);
  145. delete [] temp;
  146. if(chunkSize < 0) {
  147. throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  148. } else if(errno == ERANGE && (chunkSize == LONG_MAX || chunkSize == LONG_MIN)) {
  149. throw new DlAbortEx(strerror(errno));
  150. }
  151. *pp = p+2;
  152. return 0;
  153. }
  154. void ChunkedEncoding::addBuffer(const char* inbuf, int inlen) {
  155. int realbufSize = strbufTail-strbuf;
  156. if(realbufSize+inlen >= strbufSize) {
  157. if(realbufSize+inlen > MAX_BUFSIZE) {
  158. throw new DlAbortEx(EX_TOO_LARGE_CHUNK, realbufSize+inlen);
  159. }
  160. strbufSize = realbufSize+inlen;
  161. char* temp = new char[strbufSize];
  162. memcpy(temp, strbuf, realbufSize);
  163. delete [] strbuf;
  164. strbuf = temp;
  165. strbufTail = strbuf+realbufSize;
  166. }
  167. memcpy(strbufTail, inbuf, inlen);
  168. strbufTail += inlen;
  169. }