ChunkedEncoding.cc 4.3 KB

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