summaryrefslogtreecommitdiff
path: root/src/ChunkedEncoding.cc
blob: ba45b3d74550a1a75851bb6b524a7dbfb18f7bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* <!-- copyright */
/*
 * aria2 - a simple utility for downloading files faster
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/* copyright --> */
#include "ChunkedEncoding.h"
#include "DlAbortEx.h"
#include "message.h"
#include <string.h>
#include <strings.h>
#include <errno.h>

#define MAX_BUFSIZE (1024*1024)

ChunkedEncoding::ChunkedEncoding() {
  strbufSize = 4096;
  strbuf = new char[strbufSize];
  strbufTail = strbuf;
  state = READ_SIZE;
  chunkSize = 0;
}

ChunkedEncoding::~ChunkedEncoding() {
  if(strbuf != NULL) {
    delete [] strbuf;
  }
}

void ChunkedEncoding::init() {
}

bool ChunkedEncoding::finished() {
  return state == FINISH ? true : false;
}

void ChunkedEncoding::end() {}

void ChunkedEncoding::inflate(char* outbuf, int& outlen, const char* inbuf, int inlen) {
  addBuffer(inbuf, inlen);
  char* p = strbuf;
  int clen = 0;
  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;
    }
    // all bytes in strbuf were examined?
    if(strbufTail <= p) {
      break;
    }
  }
  if(strbufTail <= p) {
    strbufTail = strbuf;
  } else {
    // copy string between [p, strbufTail]
    int unreadSize = strbufTail-p;
    char* temp = new char[strbufSize];
    memcpy(temp, p, unreadSize);
    delete [] strbuf;
    strbuf = temp;
    strbufTail = strbuf+unreadSize;
  }
  outlen = clen;
}

int ChunkedEncoding::readData(char** pp, char* buf, int& len, int maxlen) {
  if(buf+len == buf+maxlen) {
    return -1;
  }
  if(chunkSize == 0) {
    return readDataEOL(pp);
  }
  int wsize;
  if(strbufTail-*pp < chunkSize) {
    wsize = strbufTail-*pp <= maxlen-len ? strbufTail-*pp : maxlen-len;
  } else {
    wsize = chunkSize <= maxlen-len ? chunkSize : maxlen-len;
  }
  memcpy(buf+len, *pp, wsize);
  chunkSize -= wsize;
  len += wsize;
  *pp += wsize;
  if(chunkSize == 0) {
    return readDataEOL(pp);
  } else {
    return -1;
  }
}

int ChunkedEncoding::readDataEOL(char** pp) {
  char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
  char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
  if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
    *pp += 2;
    return 0;
  } else if(strbufTail-*pp < 2) {
    return -1;
  } else {
    throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  }  
}

int ChunkedEncoding::readChunkSize(char** pp) {
  // we read chunk-size from *pp
  char* p;
  char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
  char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
  if(np == NULL || rp == NULL ||  np-rp != 1) {
    // \r\n is not found. Return -1
    return -1;
  }
  p = rp;

  // We ignore chunk-extension
  char* exsp = (char*)memchr(*pp, ';', strbufTail-*pp);
  if(exsp == NULL) {
    exsp = p;
  }
  // TODO check invalid characters in buffer
  char* temp = new char[exsp-*pp+1];
  memcpy(temp, *pp, exsp-*pp);
  temp[exsp-*pp] = '\0';

  chunkSize = strtol(temp, NULL, 16);
  delete [] temp;
  if(chunkSize < 0) {
    throw new DlAbortEx(EX_INVALID_CHUNK_SIZE);
  } else if(errno == ERANGE && (chunkSize == LONG_MAX || chunkSize == LONG_MIN)) {
    throw new DlAbortEx(strerror(errno));
  }
  *pp = p+2;
  return 0;
}

void ChunkedEncoding::addBuffer(const char* inbuf, int inlen) {
  int realbufSize = strbufTail-strbuf;
  if(realbufSize+inlen >= strbufSize) {
    if(realbufSize+inlen > MAX_BUFSIZE) {
      throw new DlAbortEx(EX_TOO_LARGE_CHUNK, realbufSize+inlen);
    }
    strbufSize = realbufSize+inlen;
    char* temp = new char[strbufSize];
    memcpy(temp, strbuf, realbufSize);
    delete [] strbuf;
    strbuf = temp;
    strbufTail = strbuf+realbufSize;
  }
  memcpy(strbufTail, inbuf, inlen);
  strbufTail += inlen;
}