File: ChunkedDecodingStreamFilter.cc

package info (click to toggle)
aria2 1.18.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,392 kB
  • ctags: 16,036
  • sloc: cpp: 115,823; sh: 12,015; ansic: 7,394; makefile: 1,445; ruby: 462; python: 216; xml: 176; asm: 58; sed: 16
file content (217 lines) | stat: -rw-r--r-- 5,974 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2012 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "ChunkedDecodingStreamFilter.h"

#include <cassert>

#include "util.h"
#include "message.h"
#include "DlAbortEx.h"
#include "A2STR.h"

namespace aria2 {

const std::string ChunkedDecodingStreamFilter::NAME
("ChunkedDecodingStreamFilter");

namespace {
enum {
  PREV_CHUNK_SIZE,
  CHUNK_SIZE,
  CHUNK_EXTENSION,
  PREV_CHUNK_SIZE_LF,
  CHUNK,
  PREV_CHUNK_CR,
  PREV_CHUNK_LF,
  PREV_TRAILER,
  TRAILER,
  PREV_TRAILER_LF,
  PREV_END_CR,
  PREV_END_LF,
  CHUNKS_COMPLETE
};
} // namespace

ChunkedDecodingStreamFilter::ChunkedDecodingStreamFilter
(std::unique_ptr<StreamFilter> delegate)
  : StreamFilter{std::move(delegate)},
    state_{PREV_CHUNK_SIZE},
    chunkSize_{0},
    chunkRemaining_{0},
    bytesProcessed_{0}
{}

ChunkedDecodingStreamFilter::~ChunkedDecodingStreamFilter() {}

void ChunkedDecodingStreamFilter::init() {}

ssize_t ChunkedDecodingStreamFilter::transform
(const std::shared_ptr<BinaryStream>& out,
 const std::shared_ptr<Segment>& segment,
 const unsigned char* inbuf, size_t inlen)
{
  ssize_t outlen = 0;
  size_t i;
  bytesProcessed_ = 0;
  for(i = 0; i < inlen; ++i) {
    unsigned char c = inbuf[i];
    switch(state_) {
    case PREV_CHUNK_SIZE:
      if(util::isHexDigit(c)) {
        chunkSize_ = util::hexCharToUInt(c);
        state_ = CHUNK_SIZE;
      } else {
        throw DL_ABORT_EX("Bad chunk size: not hex string");
      }
      break;
    case CHUNK_SIZE:
      if(util::isHexDigit(c)) {
        if(chunkSize_ & 0x7800000000000000LL) {
          throw DL_ABORT_EX("Too big chunk size");
        }
        chunkSize_ <<= 4;
        chunkSize_ += util::hexCharToUInt(c);
      } else if(c == ';') {
        state_ = CHUNK_EXTENSION;
      } else if(c == '\r') {
        state_ = PREV_CHUNK_SIZE_LF;
      } else {
        throw DL_ABORT_EX("Bad chunk size: not hex string");
      }
      break;
    case CHUNK_EXTENSION:
      if(c == '\r') {
        state_ = PREV_CHUNK_SIZE_LF;
      }
      break;
    case PREV_CHUNK_SIZE_LF:
      if(c == '\n') {
        chunkRemaining_ = chunkSize_;
        if(chunkSize_ == 0) {
          state_ = PREV_TRAILER;
        } else {
          state_ = CHUNK;
        }
      } else {
        throw DL_ABORT_EX("Bad chunk encoding: "
                          "missing LF at the end of chunk size");
      }
      break;
    case CHUNK: {
      int64_t readlen = std::min(chunkRemaining_,
                                 static_cast<int64_t>(inlen-i));
      outlen += getDelegate()->transform(out, segment, inbuf+i, readlen);
      chunkRemaining_ -= readlen;
      i += readlen-1;
      if(chunkRemaining_ == 0) {
        state_ = PREV_CHUNK_CR;
      }
      break;
    }
    case PREV_CHUNK_CR:
      if(c == '\r') {
        state_ = PREV_CHUNK_LF;
      } else {
        throw DL_ABORT_EX("Bad chunk encoding: "
                          "missing CR at the end of chunk");
      }
      break;
    case PREV_CHUNK_LF:
      if(c == '\n') {
        if(chunkSize_ == 0) {
          state_ = PREV_TRAILER;
        } else {
          chunkSize_ = chunkRemaining_ = 0;
          state_ = PREV_CHUNK_SIZE;
        }
      } else {
        throw DL_ABORT_EX("Bad chunk encoding: "
                          "missing LF at the end of chunk");
      }
      break;
    case PREV_TRAILER:
      if(c == '\r') {
        // No trailer
        state_ = PREV_END_LF;
      } else {
        state_= TRAILER;
      }
      break;
    case TRAILER:
      if(c == '\r') {
        state_ = PREV_TRAILER_LF;
      }
      break;
    case PREV_TRAILER_LF:
      if(c == '\n') {
        state_ = PREV_TRAILER;
      } else {
        throw DL_ABORT_EX("Bad chunk encoding: "
                          "missing LF at the end of trailer");
      }
      break;
    case PREV_END_LF:
      if(c == '\n') {
        state_ = CHUNKS_COMPLETE;
      } else {
        throw DL_ABORT_EX("Bad chunk encoding: "
                          "missing LF at the end of chunks");
      }
      break;
    case CHUNKS_COMPLETE:
      goto fin;
    default:
      // unreachable
      assert(0);
    }
  }
 fin:
  bytesProcessed_ += i;
  return outlen;
}

bool ChunkedDecodingStreamFilter::finished()
{
  return state_ == CHUNKS_COMPLETE && getDelegate()->finished();
}

void ChunkedDecodingStreamFilter::release() {}

const std::string& ChunkedDecodingStreamFilter::getName() const
{
  return NAME;
}

} // namespace aria2