summaryrefslogtreecommitdiff
path: root/src/chunked.cc
blob: dfafd89d53ad8d888f6733a44f4928dec6f49d8e (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
// -*- mode: c++; c-basic-offset: 2; -*-

#include "common.hh"

#include <cerrno>
#include <cstdint>
#include <cstdlib>

#include "chunked.hh"

namespace {

enum State {
  CHUNK,
  IN_CHUNK,
  END_CHUNK,
  TRAILER,
  DONE,
};

class ChunkedImpl : public Chunked {
public:
  ChunkedImpl()
    : state_(CHUNK), good_(true) {
  }

  size_t add(void const* data, size_t avail) override {
    if (!good_) return 0;
    auto const start = reinterpret_cast<char const*>(data);
    auto const end = start + avail;
    auto d = start;
    while (true) {
      if (d == end) return avail;
      switch (state_) {
      case CHUNK: {
        auto p = find_crlf(d, end);
        if (!p) return d - start;
        char* x = nullptr;
        errno = 0;
        auto tmp = strtoull(d, &x, 16);
        if (errno || (x != p && (!x || *x != ';'))) {
          good_ = false;
          return d - start;
        }
        size_ = tmp;
        d = p + 2;
        if (size_ == 0) {
          // Last chunk
          state_ = TRAILER;
        } else {
          state_ = IN_CHUNK;
        }
        break;
      }
      case IN_CHUNK: {
        uint64_t left = end - d;
        if (left < size_) {
          this->data(d, left);
          size_ -= left;
          return avail;
        }
        this->data(d, size_);
        d += size_;
        state_ = END_CHUNK;
        break;
      }
      case END_CHUNK: {
        auto p = find_crlf(d, end);
        if (!p) return d - start;
        if (p != d) {
          good_ = false;
          return d - start;
        }
        d = p + 2;
        state_ = CHUNK;
        break;
      }
      case TRAILER: {
        auto p = find_crlf(d, end);
        if (!p) return d - start;
        if (p == d) {
          state_ = DONE;
        }
        d = p + 2;
        break;
      }
      case DONE:
        return d - start;
      }
    }
  }

  bool good() const override {
    return good_;
  }

  bool eof() const override {
    return state_ == DONE;
  }

protected:
  virtual void data(void const* UNUSED(data), size_t UNUSED(size)) {
  }

private:
  char const* find_crlf(char const* start, char const* end) {
    for (; start != end; ++start) {
      if (*start == '\r') {
        if (start + 1 == end) break;
        if (start[1] == '\n') return start;
      }
    }
    return nullptr;
  }

  State state_;
  bool good_;
  uint64_t size_;
};

class ChunkedCallbackImpl : public ChunkedImpl {
public:
  ChunkedCallbackImpl(DataCallback const& callback)
    : callback_(callback) {
  }

  void data(void const* data, size_t size) override {
    callback_(data, size);
  }

private:
  DataCallback const callback_;
};

}  // namespace

// static
Chunked* Chunked::create() {
  return new ChunkedImpl();
}

// static
Chunked* Chunked::create(DataCallback const& callback) {
  return new ChunkedCallbackImpl(callback);
}