forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.cpp
More file actions
184 lines (155 loc) · 5.53 KB
/
decoder.cpp
File metadata and controls
184 lines (155 loc) · 5.53 KB
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
/*
Copyright (c) DataStax, Inc.
Licensed 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 "decoder.hpp"
#include "logger.hpp"
#include "value.hpp"
#define CHECK_REMAINING(SIZE, DETAIL) \
do { \
if (remaining_ < static_cast<size_t>(SIZE)) { \
notify_error(DETAIL, SIZE); \
return false; \
} \
} while (0)
using namespace datastax::internal::core;
void Decoder::maybe_log_remaining() const {
if (remaining_ > 0) {
LOG_TRACE("Data remaining in %s response: %u", type_, static_cast<unsigned int>(remaining_));
}
}
bool Decoder::decode_inet(Address* output) {
CHECK_REMAINING(sizeof(uint8_t), "length of inet");
uint8_t address_length = 0;
input_ = internal::decode_byte(input_, address_length);
remaining_ -= sizeof(uint8_t);
if (address_length > CASS_INET_V6_LENGTH) {
LOG_ERROR("Invalid inet address length of %d bytes", address_length);
return false;
}
CHECK_REMAINING(address_length, "inet");
uint8_t address[CASS_INET_V6_LENGTH];
memcpy(address, input_, address_length);
input_ += address_length;
remaining_ -= address_length;
CHECK_REMAINING(sizeof(int32_t), "port");
int32_t port = 0;
input_ = internal::decode_int32(input_, port);
remaining_ -= sizeof(int32_t);
*output = Address(address, address_length, port);
return output->is_valid_and_resolved();
}
bool Decoder::decode_inet(CassInet* output) {
CHECK_REMAINING(sizeof(uint8_t), "length of inet");
input_ = internal::decode_byte(input_, output->address_length);
remaining_ -= sizeof(uint8_t);
if (output->address_length > CASS_INET_V6_LENGTH) {
LOG_ERROR("Invalid inet address length of %d bytes", output->address_length);
return false;
}
CHECK_REMAINING(output->address_length, "inet");
memcpy(output->address, input_, output->address_length);
input_ += output->address_length;
remaining_ -= output->address_length;
return true;
}
bool Decoder::as_inet(const int address_length, CassInet* output) const {
output->address_length = static_cast<uint8_t>(address_length);
if (output->address_length > CASS_INET_V6_LENGTH) {
LOG_ERROR("Invalid inet address length of %d bytes", output->address_length);
return false;
}
CHECK_REMAINING(output->address_length, "inet");
memcpy(output->address, input_, output->address_length);
return true;
}
bool Decoder::decode_write_type(CassWriteType& output) {
StringRef write_type;
output = CASS_WRITE_TYPE_UNKNOWN;
if (!decode_string(&write_type)) return false;
if (write_type == "SIMPLE") {
output = CASS_WRITE_TYPE_SIMPLE;
} else if (write_type == "BATCH") {
output = CASS_WRITE_TYPE_BATCH;
} else if (write_type == "UNLOGGED_BATCH") {
output = CASS_WRITE_TYPE_UNLOGGED_BATCH;
} else if (write_type == "COUNTER") {
output = CASS_WRITE_TYPE_COUNTER;
} else if (write_type == "BATCH_LOG") {
output = CASS_WRITE_TYPE_BATCH_LOG;
} else if (write_type == "CAS") {
output = CASS_WRITE_TYPE_CAS;
} else if (write_type == "VIEW") {
output = CASS_WRITE_TYPE_VIEW;
} else if (write_type == "CDC") {
output = CASS_WRITE_TYPE_CDC;
} else {
LOG_WARN("Invalid write type %.*s", (int)write_type.size(), write_type.data());
return false;
}
return true;
}
bool Decoder::decode_warnings(WarningVec& output) {
if (remaining_ < sizeof(uint16_t)) {
notify_error("count of warnings", sizeof(uint16_t));
return false;
}
uint16_t count = 0;
input_ = internal::decode_uint16(input_, count);
remaining_ -= sizeof(uint16_t);
for (uint16_t i = 0; i < count; ++i) {
StringRef warning;
if (!decode_string(&warning)) return false;
LOG_WARN("Server-side warning: %.*s", (int)warning.size(), warning.data());
output.push_back(warning);
}
return true;
}
Value Decoder::decode_value(const DataType::ConstPtr& data_type) {
int32_t size = 0;
if (!decode_int32(size)) return Value();
if (size >= 0) {
Decoder decoder(input_, size, protocol_version_);
input_ += size;
remaining_ -= size;
int32_t count = 0;
if (!data_type->is_collection()) {
return Value(data_type, decoder);
} else if (decoder.decode_int32(count)) {
return Value(data_type, count, decoder);
}
return Value();
}
return Value(data_type);
}
bool Decoder::update_value(Value& value) {
int32_t size = 0;
if (decode_int32(size)) {
if (size >= 0) {
Decoder decoder(input_, size, protocol_version_);
input_ += size;
remaining_ -= size;
return value.update(decoder);
}
Decoder decoder;
return value.update(decoder);
}
return false;
}
void Decoder::notify_error(const char* detail, size_t bytes) const {
if (strlen(type_) == 0) {
LOG_ERROR("Expected at least %u byte%s to decode %s value", static_cast<unsigned int>(bytes),
(bytes > 1 ? "s" : ""), detail);
} else {
LOG_ERROR("Expected at least %u byte%s to decode %s %s response",
static_cast<unsigned int>(bytes), (bytes > 1 ? "s" : ""), detail, type_);
}
}