Skip to content

Commit

Permalink
Fix compilation warnings on Windows x64.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed May 18, 2015
1 parent 5cd0c8e commit c42cd01
Show file tree
Hide file tree
Showing 47 changed files with 167 additions and 137 deletions.
5 changes: 3 additions & 2 deletions include/tins/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ namespace Crypto {

template<typename ForwardIterator>
RC4Key::RC4Key(ForwardIterator start, ForwardIterator end) {
for(size_t i = 0; i < data_size; ++i)
data[i] = i;
for(size_t i = 0; i < data_size; ++i) {
data[i] = static_cast<uint8_t>(i);
}
size_t j = 0;
ForwardIterator iter = start;
for(size_t i = 0; i < data_size; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion include/tins/dhcpv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ void class_option_data2option(InputIterator start, InputIterator end,
uint16_t uint16_t_buffer;
while(start != end) {
buffer.resize(buffer.size() + sizeof(uint16_t) + start->size());
uint16_t_buffer = Endian::host_to_be<uint16_t>(start->size());
uint16_t_buffer = Endian::host_to_be(static_cast<uint16_t>(start->size()));
std::memcpy(&buffer[index], &uint16_t_buffer, sizeof(uint16_t));
index += sizeof(uint16_t);
std::copy(start->begin(), start->end(), buffer.begin() + index);
Expand Down
4 changes: 3 additions & 1 deletion include/tins/dot11/dot11_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ class Dot11ControlTA : public Dot11Control {
/**
* \brief Getter for the control ta additional fields size.
*/
uint32_t controlta_size() const { return _taddr.size() + sizeof(ieee80211_header); }
uint32_t controlta_size() const {
return static_cast<uint32_t>(_taddr.size() + sizeof(ieee80211_header));
}

uint32_t write_ext_header(uint8_t *buffer, uint32_t total_sz);
private:
Expand Down
6 changes: 4 additions & 2 deletions include/tins/dot11/dot11_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ class Dot11Data : public Dot11 {
uint32_t write_ext_header(uint8_t *buffer, uint32_t total_sz);

uint32_t data_frame_size() {
return Dot11::header_size() + sizeof(_ext_header) +
((from_ds() && to_ds()) ? _addr4.size() : 0);
return static_cast<uint32_t>(
Dot11::header_size() + sizeof(_ext_header) +
((from_ds() && to_ds()) ? _addr4.size() : 0)
);
}
private:
ExtendedHeader _ext_header;
Expand Down
11 changes: 11 additions & 0 deletions include/tins/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ class protocol_disabled : public std::exception {
}
};

/**
* \brief Exception thrown when a payload is too large to fit
* into a PDUOption.
*/
class option_payload_too_large : public std::exception {
public:
const char *what() const throw() {
return "Option payload too large";
}
};

} // Tins

#endif // TINS_EXCEPTIONS_H
12 changes: 8 additions & 4 deletions include/tins/pdu_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class PDUOption {
* \param data The option's data(if any).
*/
PDUOption(option_type opt = option_type(), size_t length = 0, const data_type *data = 0)
: option_(opt), size_(length) {
: option_(opt), size_(static_cast<uint16_t>(length)) {
set_payload_contents(data, data + (data ? length : 0));
}

Expand Down Expand Up @@ -397,7 +397,7 @@ class PDUOption {
*/
template<typename ForwardIterator>
PDUOption(option_type opt, ForwardIterator start, ForwardIterator end)
: option_(opt), size_(std::distance(start, end)) {
: option_(opt), size_(static_cast<uint16_t>(std::distance(start, end))) {
set_payload_contents(start, end);
}

Expand All @@ -417,7 +417,7 @@ class PDUOption {
* \param end The end of the option data.
*/
template<typename ForwardIterator>
PDUOption(option_type opt, size_t length, ForwardIterator start, ForwardIterator end)
PDUOption(option_type opt, uint16_t length, ForwardIterator start, ForwardIterator end)
: option_(opt), size_(length) {
set_payload_contents(start, end);
}
Expand Down Expand Up @@ -492,7 +492,11 @@ class PDUOption {
private:
template<typename ForwardIterator>
void set_payload_contents(ForwardIterator start, ForwardIterator end) {
real_size_ = std::distance(start, end);
size_t total_size = std::distance(start, end);
if (total_size > std::numeric_limits<uint16_t>::max()) {
throw option_payload_too_large();
}
real_size_ = static_cast<uint16_t>(total_size);
if(real_size_ <= small_buffer_size) {
std::copy(
start,
Expand Down
2 changes: 1 addition & 1 deletion include/tins/pppoe.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class PPPoE : public PDU {
* \param option The option to be added.
*/
void add_tag(tag &&option) {
_tags_size += option.data_size() + sizeof(uint16_t) * 2;
_tags_size += static_cast<uint16_t>(option.data_size() + sizeof(uint16_t) * 2);
_tags.push_back(std::move(option));
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/tins/rawpdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace Tins {
* \return uint32_t containing the payload size.
*/
uint32_t payload_size() const {
return _payload.size();
return static_cast<uint32_t>(_payload.size());
}

/**
Expand All @@ -184,7 +184,7 @@ namespace Tins {
*/
template<typename T>
T to() const {
return T(&_payload[0], _payload.size());
return T(&_payload[0], static_cast<uint32_t>(_payload.size()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/bootp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ BootP::BootP(const uint8_t *buffer, uint32_t total_sz, uint32_t vend_field_size)
}

uint32_t BootP::header_size() const {
return sizeof(bootphdr) + _vend.size();
return static_cast<uint32_t>(sizeof(bootphdr) + _vend.size());
}

void BootP::opcode(uint8_t new_opcode) {
Expand Down
5 changes: 3 additions & 2 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ PDU *WEPDecrypter::decrypt(RawPDU &raw, const std::string &password) {
// Generate the key
RC4Key key(key_buffer.begin(), key_buffer.begin() + password.size() + 3);
rc4(pload.begin() + 4, pload.end(), key, pload.begin());
uint32_t crc = Utils::crc32(&pload[0], pload.size() - 8);
uint32_t payload_size = static_cast<uint32_t>(pload.size() - 8);
uint32_t crc = Utils::crc32(&pload[0], payload_size);
if(pload[pload.size() - 8] != (crc & 0xff) ||
pload[pload.size() - 7] != ((crc >> 8) & 0xff) ||
pload[pload.size() - 6] != ((crc >> 16) & 0xff) ||
pload[pload.size() - 5] != ((crc >> 24) & 0xff))
return 0;

try {
return new SNAP(&pload[0], pload.size() - 8);
return new SNAP(&pload[0], payload_size);
}
catch(std::runtime_error&) {
return 0;
Expand Down
8 changes: 4 additions & 4 deletions src/dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz)
: BootP(buffer, total_sz, 0), _size(sizeof(uint32_t))
{
buffer += BootP::header_size() - vend().size();
total_sz -= BootP::header_size() - vend().size();
total_sz -= static_cast<uint32_t>(BootP::header_size() - vend().size());
uint8_t args[2] = {0};
uint32_t uint32_t_buffer;
std::memcpy(&uint32_t_buffer, buffer, sizeof(uint32_t));
Expand Down Expand Up @@ -86,7 +86,7 @@ void DHCP::add_option(const option &opt) {
}

void DHCP::internal_add_option(const option &opt) {
_size += opt.data_size() + (sizeof(uint8_t) << 1);
_size += static_cast<uint32_t>(opt.data_size() + (sizeof(uint8_t) << 1));
}

const DHCP::option *DHCP::search_option(OptionTypes opt) const {
Expand Down Expand Up @@ -216,7 +216,7 @@ PDU::serialization_type DHCP::serialize_list(const std::vector<ipaddress_type> &
}

uint32_t DHCP::header_size() const {
return BootP::header_size() - vend().size() + _size;
return static_cast<uint32_t>(BootP::header_size() - vend().size() + _size);
}

void DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
Expand All @@ -231,7 +231,7 @@ void DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *pa
*((uint32_t*)&result[0]) = Endian::host_to_be<uint32_t>(0x63825363);
for(options_type::const_iterator it = _options.begin(); it != _options.end(); ++it) {
*(ptr++) = it->option();
*(ptr++) = it->length_field();
*(ptr++) = static_cast<uint8_t>(it->length_field());
std::copy(it->data_ptr(), it->data_ptr() + it->data_size(), ptr);
ptr += it->data_size();
}
Expand Down
6 changes: 3 additions & 3 deletions src/dhcpv6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const DHCPv6::option *DHCPv6::search_option(OptionTypes id) const {
uint8_t* DHCPv6::write_option(const option &opt, uint8_t* buffer) const {
uint16_t uint16_t_buffer = Endian::host_to_be(opt.option());
std::memcpy(buffer, &uint16_t_buffer, sizeof(uint16_t));
uint16_t_buffer = Endian::host_to_be<uint16_t>(opt.length_field());
uint16_t_buffer = Endian::host_to_be(static_cast<uint16_t>(opt.length_field()));
std::memcpy(&buffer[sizeof(uint16_t)], &uint16_t_buffer, sizeof(uint16_t));
return std::copy(
opt.data_ptr(),
Expand Down Expand Up @@ -632,7 +632,7 @@ DHCPv6::vendor_class_type DHCPv6::vendor_class_type::from_option(const option &o
output.enterprise_number = Endian::be_to_host(output.enterprise_number);
output.vendor_class_data = Internals::option2class_option_data<data_type>(
opt.data_ptr() + sizeof(uint32_t),
opt.data_size() - sizeof(uint32_t)
static_cast<uint32_t>(opt.data_size() - sizeof(uint32_t))
);

return output;
Expand Down Expand Up @@ -660,7 +660,7 @@ DHCPv6::user_class_type DHCPv6::user_class_type::from_option(const option &opt)
throw malformed_option();
user_class_type output;
output.data = Internals::option2class_option_data<data_type>(
opt.data_ptr(), opt.data_size()
opt.data_ptr(), static_cast<uint32_t>(opt.data_size())
);
return output;
}
Expand Down
33 changes: 18 additions & 15 deletions src/dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ DNS::DNS(const uint8_t *buffer, uint32_t total_sz)
throw malformed_packet();
buffer += sizeof(uint16_t) * 2;
}
answers_idx = buffer - prev_start;
authority_idx = find_section_end(&records_data[0] + answers_idx, answers_count()) - &records_data[0];
additional_idx = find_section_end(&records_data[0] + authority_idx, authority_count()) - &records_data[0];
answers_idx = static_cast<uint32_t>(buffer - prev_start);
authority_idx = static_cast<uint32_t>(
find_section_end(&records_data[0] + answers_idx, answers_count()) - &records_data[0]
);
additional_idx = static_cast<uint32_t>(
find_section_end(&records_data[0] + authority_idx, authority_count()) - &records_data[0]
);
}
}

Expand Down Expand Up @@ -118,7 +122,7 @@ const uint8_t *DNS::find_section_end(const uint8_t *ptr, const uint32_t num_reco
}

uint32_t DNS::header_size() const {
return sizeof(dns) + records_data.size();
return static_cast<uint32_t>(sizeof(dns) + records_data.size());
}

void DNS::id(uint16_t new_id) {
Expand Down Expand Up @@ -180,7 +184,7 @@ void DNS::add_query(const Query &query) {
uint16_t_buffer = Endian::host_to_be<uint16_t>(query.query_class());
std::memcpy(&new_str[new_str.size() - 2], &uint16_t_buffer, sizeof(uint16_t));

uint32_t offset = new_str.size(), threshold = answers_idx;
uint32_t offset = static_cast<uint32_t>(new_str.size()), threshold = answers_idx;
update_records(answers_idx, answers_count(), threshold, offset);
update_records(authority_idx, authority_count(), threshold, offset);
update_records(additional_idx, additional_count(), threshold, offset);
Expand All @@ -189,9 +193,7 @@ void DNS::add_query(const Query &query) {
new_str.begin(),
new_str.end()
);
dns.questions = Endian::host_to_be<uint16_t>(
questions_count() + 1
);
dns.questions = Endian::host_to_be(static_cast<uint16_t>(questions_count() + 1));
}

void DNS::add_answer(const Resource &resource) {
Expand All @@ -211,7 +213,7 @@ void DNS::add_record(const Resource &resource, const sections_type &sections) {
IPv6Address v6_addr;
std::string buffer = encode_domain_name(resource.dname()), encoded_data;
// By default the data size is the length of the data field.
uint32_t data_size = resource.data().size();
size_t data_size = resource.data().size();
if(resource.type() == A) {
v4_addr = resource.data();
data_size = 4;
Expand All @@ -224,14 +226,15 @@ void DNS::add_record(const Resource &resource, const sections_type &sections) {
encoded_data = encode_domain_name(resource.data());
data_size = encoded_data.size();
}
uint32_t offset = buffer.size() + sizeof(uint16_t) * 3 + sizeof(uint32_t) + data_size,
size_t offset = buffer.size() + sizeof(uint16_t) * 3 + sizeof(uint32_t) + data_size,
threshold = sections.empty() ? records_data.size() : *sections.front().first;
// Skip the preference field
if(resource.type() == MX) {
offset += sizeof(uint16_t);
}
for(size_t i = 0; i < sections.size(); ++i) {
update_records(*sections[i].first, sections[i].second, threshold, offset);
update_records(*sections[i].first, sections[i].second,
static_cast<uint32_t>(threshold), static_cast<uint32_t>(offset));
}

records_data.insert(
Expand All @@ -257,8 +260,8 @@ void DNS::add_record(const Resource &resource, const sections_type &sections) {
uint32_t_buffer = Endian::host_to_be(resource.ttl());
std::memcpy(ptr, &uint32_t_buffer, sizeof(uint32_t));
ptr += sizeof(uint32_t);
uint16_t_buffer = Endian::host_to_be<uint16_t>(
data_size + (resource.type() == MX ? 2 : 0)
uint16_t_buffer = Endian::host_to_be(
static_cast<uint16_t>(data_size + (resource.type() == MX ? 2 : 0))
);
std::memcpy(ptr, &uint16_t_buffer, sizeof(uint16_t));
ptr += sizeof(uint16_t);
Expand Down Expand Up @@ -301,11 +304,11 @@ std::string DNS::encode_domain_name(const std::string &dn) {
size_t last_index(0), index;
if(!dn.empty()) {
while((index = dn.find('.', last_index+1)) != string::npos) {
output.push_back(index - last_index);
output.push_back(static_cast<char>(index - last_index));
output.append(dn.begin() + last_index, dn.begin() + index);
last_index = index + 1; //skip dot
}
output.push_back(dn.size() - last_index);
output.push_back(static_cast<char>(dn.size() - last_index));
output.append(dn.begin() + last_index, dn.end());
}
output.push_back('\0');
Expand Down
4 changes: 2 additions & 2 deletions src/dot11/dot11_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void Dot11::add_tagged_option(OptionTypes opt, uint8_t len, const uint8_t *val)
}

void Dot11::internal_add_option(const option &opt) {
_options_size += opt.data_size() + sizeof(uint8_t) * 2;
_options_size += static_cast<uint32_t>(opt.data_size() + sizeof(uint8_t) * 2);
}

void Dot11::add_option(const option &opt) {
Expand Down Expand Up @@ -212,7 +212,7 @@ void Dot11::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *p
#endif
for(std::list<option>::const_iterator it = _options.begin(); it != _options.end(); ++it) {
*(buffer++) = it->option();
*(buffer++) = it->length_field();
*(buffer++) = static_cast<uint8_t>(it->length_field());
std::copy(it->data_ptr(), it->data_ptr() + it->data_size(), buffer);
buffer += it->data_size();
}
Expand Down
6 changes: 3 additions & 3 deletions src/dot11/dot11_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ uint32_t Dot11Data::init(const uint8_t *buffer, uint32_t total_sz) {
throw malformed_packet();
_addr4 = buffer;
buffer += _addr4.size();
total_sz -= _addr4.size();
total_sz -= static_cast<uint32_t>(_addr4.size());
}
return buffer - start_ptr;
return static_cast<uint32_t>(buffer - start_ptr);
}

Dot11Data::Dot11Data(const address_type &dst_hw_addr,
Expand Down Expand Up @@ -128,7 +128,7 @@ uint32_t Dot11Data::write_ext_header(uint8_t *buffer, uint32_t total_sz) {
memcpy(buffer, &_ext_header, sizeof(_ext_header));
buffer += sizeof(_ext_header);
if (from_ds() && to_ds()) {
written += _addr4.size();
written += static_cast<uint32_t>(_addr4.size());
_addr4.copy(buffer);
}
return written;
Expand Down
Loading

0 comments on commit c42cd01

Please sign in to comment.