File: chunk-decoder.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (160 lines) | stat: -rw-r--r-- 3,910 bytes parent folder | download | duplicates (4)
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
/** @file

  Chunk decoding.

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
#include <algorithm>
#include <cassert>

#include "chunk-decoder.h"

void
ChunkDecoder::parseSizeCharacter(const char a)
{
  assert(state_ == State::kSize);
  if (a >= '0' && a <= '9') {
    size_ = (size_ << 4) | (a - '0');
  } else if (a >= 'A' && a <= 'F') {
    size_ = (size_ << 4) | (a - 'A' + 10);
  } else if (a >= 'a' && a <= 'f') {
    size_ = (size_ << 4) | (a - 'a' + 10);
  } else if (a == '\r') {
    state_ = size_ == 0 ? State::kEndN : State::kDataN;
  } else {
    assert(false); // invalid input
  }
}

int
ChunkDecoder::parseSize(const char *p, const int64_t s)
{
  assert(p != nullptr);
  assert(s > 0);
  int length = 0;
  while (state_ != State::kData && *p != '\0' && length < s) {
    assert(state_ < State::kUpperBound); // VALID RANGE
    switch (state_) {
    case State::kData:
    case State::kInvalid:
    case State::kEnd:
    case State::kUpperBound:
      assert(false);
      break;

    case State::kDataN:
      assert(*p == '\n');
      state_ = (*p == '\n') ? State::kData : State::kInvalid;
      break;

    case State::kEndN:
      assert(*p == '\n');
      state_ = (*p == '\n') ? State::kEnd : State::kInvalid;
      return length;

    case State::kSizeR:
      assert(*p == '\r');
      state_ = (*p == '\r') ? State::kSizeN : State::kInvalid;
      break;

    case State::kSizeN:
      assert(*p == '\n');
      state_ = (*p == '\n') ? State::kSize : State::kInvalid;
      break;

    case State::kSize:
      parseSizeCharacter(*p);
      break;
    }
    ++length;
    ++p;
    assert(state_ != State::kInvalid);
  }
  return length;
}

bool
ChunkDecoder::isSizeState() const
{
  return state_ == State::kDataN || state_ == State::kEndN || state_ == State::kSize || state_ == State::kSizeN ||
         state_ == State::kSizeR;
}

int
ChunkDecoder::decode(const TSIOBufferReader &r)
{
  assert(r != nullptr);

  if (state_ == State::kEnd) {
    return 0;
  }

  {
    const int l = TSIOBufferReaderAvail(r);
    if (l == 0) {
      return 0;
    } else if (l < size_) {
      size_ -= l;
      return l;
    }
  }

  int64_t size;
  TSIOBufferBlock block = TSIOBufferReaderStart(r);

  // Trying to parse a size.
  if (isSizeState()) {
    while (block != nullptr && size_ == 0) {
      const char *p = TSIOBufferBlockReadStart(block, r, &size);
      assert(p != nullptr);
      const int i = parseSize(p, size);
      size -= i;
      TSIOBufferReaderConsume(r, i);
      if (state_ == State::kEnd) {
        assert(size_ == 0);
        return 0;
      }
      if (isSizeState()) {
        assert(size == 0);
        block = TSIOBufferBlockNext(block);
      }
    }
  }

  int length = 0;

  while (block != nullptr && state_ == State::kData) {
    assert(size_ > 0);
    const char *p = TSIOBufferBlockReadStart(block, r, &size);
    if (p != nullptr) {
      if (size >= size_) {
        length += size_;
        size_  = 0;
        state_ = State::kSizeR;
        break;
      } else {
        length += size;
        size_ -= size;
      }
    }
    block = TSIOBufferBlockNext(block);
  }

  return length;
}