diff options
38 files changed, 147 insertions, 117 deletions
diff --git a/include/bit_ops.h b/include/bit_ops.h index 4b8d248d6..5883d2163 100644 --- a/include/bit_ops.h +++ b/include/bit_ops.h @@ -15,10 +15,14 @@ namespace Botan { * Word Rotation Functions * *************************************************/ template<typename T> inline T rotate_left(T input, u32bit rot) - { return (T)((input << rot) | (input >> (8*sizeof(T)-rot))); } + { + return static_cast<T>((input << rot) | (input >> (8*sizeof(T)-rot)));; + } template<typename T> inline T rotate_right(T input, u32bit rot) - { return (T)((input >> rot) | (input << (8*sizeof(T)-rot))); } + { + return static_cast<T>((input >> rot) | (input << (8*sizeof(T)-rot))); + } /************************************************* * XOR Functions * diff --git a/include/loadstor.h b/include/loadstor.h index f9ce7417f..992a71afe 100644 --- a/include/loadstor.h +++ b/include/loadstor.h @@ -14,27 +14,39 @@ namespace Botan { * Byte Extraction Function * *************************************************/ template<typename T> inline byte get_byte(u32bit byte_num, T input) - { return (byte)(input >> ((sizeof(T)-1-(byte_num&(sizeof(T)-1))) << 3)); } + { + return (input >> ((sizeof(T)-1-(byte_num&(sizeof(T)-1))) << 3)); + } /************************************************* * Byte to Word Conversions * *************************************************/ -inline u16bit make_u16bit(byte input0, byte input1) - { return (u16bit)(((u16bit)input0 << 8) | input1); } - -inline u32bit make_u32bit(byte input0, byte input1, byte input2, byte input3) - { return (u32bit)(((u32bit)input0 << 24) | ((u32bit)input1 << 16) | - ((u32bit)input2 << 8) | input3); } +inline u16bit make_u16bit(byte i0, byte i1) + { + return ((static_cast<u16bit>(i0) << 8) | i1); + } -inline u64bit make_u64bit(byte input0, byte input1, byte input2, byte input3, - byte input4, byte input5, byte input6, byte input7) +inline u32bit make_u32bit(byte i0, byte i1, byte i2, byte i3) { - return (u64bit)(((u64bit)input0 << 56) | ((u64bit)input1 << 48) | - ((u64bit)input2 << 40) | ((u64bit)input3 << 32) | - ((u64bit)input4 << 24) | ((u64bit)input5 << 16) | - ((u64bit)input6 << 8) | input7); + return ((static_cast<u32bit>(i0) << 24) | + (static_cast<u32bit>(i1) << 16) | + (static_cast<u32bit>(i2) << 8) | + (static_cast<u32bit>(i3))); } +inline u64bit make_u64bit(byte i0, byte i1, byte i2, byte i3, + byte i4, byte i5, byte i6, byte i7) + { + return ((static_cast<u64bit>(i0) << 56) | + (static_cast<u64bit>(i1) << 48) | + (static_cast<u64bit>(i2) << 40) | + (static_cast<u64bit>(i3) << 32) | + (static_cast<u64bit>(i4) << 24) | + (static_cast<u64bit>(i5) << 16) | + (static_cast<u64bit>(i6) << 8) | + (static_cast<u64bit>(i7))); + } + /************************************************* * Endian-Specific Word Loading Operations * *************************************************/ diff --git a/include/mp_types.h b/include/mp_types.h index ed68a4ba1..8ba551bd8 100644 --- a/include/mp_types.h +++ b/include/mp_types.h @@ -22,8 +22,8 @@ namespace Botan { #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64 #endif -const word MP_WORD_MASK = ~((word)0); -const word MP_WORD_TOP_BIT = (word)1 << (8*sizeof(word) - 1); +const word MP_WORD_MASK = ~static_cast<word>(0); +const word MP_WORD_TOP_BIT = static_cast<word>(1) << (8*sizeof(word) - 1); const word MP_WORD_MAX = MP_WORD_MASK; } diff --git a/include/secmem.h b/include/secmem.h index 2b5e046f8..b50022c83 100644 --- a/include/secmem.h +++ b/include/secmem.h @@ -78,7 +78,11 @@ class MemoryRegion void init(bool locking, u32bit size = 0) { alloc = Allocator::get(locking); create(size); } private: - T* allocate(u32bit n) const { return (T*)alloc->allocate(sizeof(T)*n); } + T* allocate(u32bit n) const + { + return static_cast<T*>(alloc->allocate(sizeof(T)*n)); + } + void deallocate(T* p, u32bit n) const { alloc->deallocate(p, sizeof(T)*n); } diff --git a/src/asn1_int.cpp b/src/asn1_int.cpp index 1b16efdf6..ee39c529d 100644 --- a/src/asn1_int.cpp +++ b/src/asn1_int.cpp @@ -43,8 +43,8 @@ SecureVector<byte> put_in_sequence(const MemoryRegion<byte>& contents) *************************************************/ std::string to_string(const BER_Object& obj) { - std::string str((const char*)obj.value.begin(), obj.value.size()); - return str; + return std::string(reinterpret_cast<const char*>(obj.value.begin()), + obj.value.size()); } /************************************************* diff --git a/src/asn1_str.cpp b/src/asn1_str.cpp index a2285a682..903fa0e64 100644 --- a/src/asn1_str.cpp +++ b/src/asn1_str.cpp @@ -44,7 +44,7 @@ ASN1_Tag choose_encoding(const std::string& str) 0x00, 0x00, 0x00, 0x00 }; for(u32bit j = 0; j != str.size(); ++j) - if(!IS_PRINTABLE[(byte)str[j]]) + if(!IS_PRINTABLE[static_cast<byte>(str[j])]) { const std::string type = global_config().option("x509/ca/str_type"); diff --git a/src/asn1_tm.cpp b/src/asn1_tm.cpp index f91196c9d..598d99df0 100644 --- a/src/asn1_tm.cpp +++ b/src/asn1_tm.cpp @@ -20,11 +20,7 @@ namespace { *************************************************/ std::tm get_tm(u64bit timer) { - std::time_t time_val = (std::time_t)timer; - - if((u64bit)time_val != timer) - throw Encoding_Error("X509_Time: time_t overflow with time value " + - to_string(timer)); + std::time_t time_val = static_cast<std::time_t>(timer); std::tm* tm_p = std::gmtime(&time_val); if(tm_p == 0) diff --git a/src/base.cpp b/src/base.cpp index fb5e219c9..9382f57ba 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -150,7 +150,7 @@ void BufferedComputation::update(const MemoryRegion<byte>& in) *************************************************/ void BufferedComputation::update(const std::string& str) { - update((const byte*)str.data(), str.size()); + update(reinterpret_cast<const byte*>(str.data()), str.size()); } /************************************************* diff --git a/src/base64.cpp b/src/base64.cpp index b96665379..c08780955 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -147,9 +147,9 @@ bool Base64_Decoder::is_valid(byte in) *************************************************/ void Base64_Decoder::decode(const byte in[4], byte out[3]) { - out[0] = (byte)((BASE64_TO_BIN[in[0]] << 2) | (BASE64_TO_BIN[in[1]] >> 4)); - out[1] = (byte)((BASE64_TO_BIN[in[1]] << 4) | (BASE64_TO_BIN[in[2]] >> 2)); - out[2] = (byte)((BASE64_TO_BIN[in[2]] << 6) | (BASE64_TO_BIN[in[3]])); + out[0] = ((BASE64_TO_BIN[in[0]] << 2) | (BASE64_TO_BIN[in[1]] >> 4)); + out[1] = ((BASE64_TO_BIN[in[1]] << 4) | (BASE64_TO_BIN[in[2]] >> 2)); + out[2] = ((BASE64_TO_BIN[in[2]] << 6) | (BASE64_TO_BIN[in[3]])); } /************************************************* @@ -176,7 +176,8 @@ void Base64_Decoder::handle_bad_char(byte c) return; throw Decoding_Error( - std::string("Base64_Decoder: Invalid base64 character '") + (char)c + "'" + std::string("Base64_Decoder: Invalid base64 character '") + + static_cast<char>(c) + "'" ); } diff --git a/src/ber_dec.cpp b/src/ber_dec.cpp index 251cf952b..5df9a2c35 100644 --- a/src/ber_dec.cpp +++ b/src/ber_dec.cpp @@ -453,7 +453,7 @@ BER_Decoder& BER_Decoder::decode_optional_string(MemoryRegion<byte>& out, { BER_Object obj = get_next_object(); - ASN1_Tag type_tag = (ASN1_Tag)type_no; + ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no); out.clear(); push_back(obj); diff --git a/src/big_base.cpp b/src/big_base.cpp index a6d9e3e9e..25d0832c6 100644 --- a/src/big_base.cpp +++ b/src/big_base.cpp @@ -25,7 +25,7 @@ BigInt::BigInt(u64bit n) reg.create(4*limbs_needed); for(u32bit j = 0; j != limbs_needed; ++j) - reg[j] = (word)((n >> (j*MP_WORD_BITS)) & MP_WORD_MASK); + reg[j] = ((n >> (j*MP_WORD_BITS)) & MP_WORD_MASK); } /************************************************* @@ -73,7 +73,7 @@ BigInt::BigInt(const std::string& str) else if(str.length() > markers + 1 && str[markers] == '0') { markers += 1; base = Octal; } - *this = decode((const byte*)str.data() + markers, + *this = decode(reinterpret_cast<const byte*>(str.data()) + markers, str.length() - markers, base); if(negative) set_sign(Negative); @@ -191,7 +191,7 @@ u32bit BigInt::get_substring(u32bit offset, u32bit length) const void BigInt::set_bit(u32bit n) { const u32bit which = n / MP_WORD_BITS; - const word mask = (word)1 << (n % MP_WORD_BITS); + const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); if(which >= size()) grow_to(which + 1); reg[which] |= mask; } @@ -202,7 +202,7 @@ void BigInt::set_bit(u32bit n) void BigInt::clear_bit(u32bit n) { const u32bit which = n / MP_WORD_BITS; - const word mask = (word)1 << (n % MP_WORD_BITS); + const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); if(which < size()) reg[which] &= ~mask; } @@ -216,7 +216,7 @@ void BigInt::mask_bits(u32bit n) if(n >= bits()) return; const u32bit top_word = n / MP_WORD_BITS; - const word mask = ((word)1 << (n % MP_WORD_BITS)) - 1; + const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1; if(top_word < size()) for(u32bit j = top_word + 1; j != size(); ++j) @@ -283,7 +283,7 @@ u32bit BigInt::encoded_size(Base base) const else if(base == Octal) return ((bits() + 2) / 3); else if(base == Decimal) - return (u32bit)((bits() * LOG_2_BASE_10) + 1); + return static_cast<u32bit>((bits() * LOG_2_BASE_10) + 1); else throw Invalid_Argument("Unknown base for BigInt encoding"); } diff --git a/src/big_io.cpp b/src/big_io.cpp index 1590ca046..f5b952109 100644 --- a/src/big_io.cpp +++ b/src/big_io.cpp @@ -29,7 +29,8 @@ std::ostream& operator<<(std::ostream& stream, const BigInt& n) u32bit skip = 0; while(buffer[skip] == '0' && skip < buffer.size()) ++skip; - stream.write((const char*)buffer.begin() + skip, buffer.size() - skip); + stream.write(reinterpret_cast<const char*>(buffer.begin()) + skip, + buffer.size() - skip); } if(!stream.good()) throw Stream_IO_Error("BigInt output operator has failed"); diff --git a/src/buf_es.cpp b/src/buf_es.cpp index 857d7688b..409a98297 100644 --- a/src/buf_es.cpp +++ b/src/buf_es.cpp @@ -52,7 +52,7 @@ void Buffered_EntropySource::do_fast_poll() *************************************************/ void Buffered_EntropySource::add_bytes(const void* entropy_ptr, u32bit length) { - const byte* bytes = (const byte*)entropy_ptr; + const byte* bytes = static_cast<const byte*>(entropy_ptr); while(length) { u32bit copied = std::min(length, buffer.size() - write_pos); @@ -68,7 +68,7 @@ void Buffered_EntropySource::add_bytes(const void* entropy_ptr, u32bit length) *************************************************/ void Buffered_EntropySource::add_bytes(u64bit entropy) { - add_bytes((const void*)&entropy, 8); + add_bytes(&entropy, 8); } /************************************************* diff --git a/src/charset.cpp b/src/charset.cpp index 08ab32be3..7cda83f0d 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -92,7 +92,8 @@ char digit2char(byte b) *************************************************/ bool caseless_cmp(char a, char b) { - return (std::tolower((unsigned char)a) == std::tolower((unsigned char)b)); + return (std::tolower(static_cast<unsigned char>(a)) == + std::tolower(static_cast<unsigned char>(b))); } } diff --git a/src/data_snk.cpp b/src/data_snk.cpp index 7f2080723..a76fbfa09 100644 --- a/src/data_snk.cpp +++ b/src/data_snk.cpp @@ -13,7 +13,7 @@ namespace Botan { *************************************************/ void DataSink_Stream::write(const byte out[], u32bit length) { - sink->write((const char*)out, length); + sink->write(reinterpret_cast<const char*>(out), length); if(!sink->good()) throw Stream_IO_Error("DataSink_Stream: Failure writing to " + fsname); } diff --git a/src/data_src.cpp b/src/data_src.cpp index 9ed7dc6c9..2a83a3860 100644 --- a/src/data_src.cpp +++ b/src/data_src.cpp @@ -93,7 +93,7 @@ DataSource_Memory::DataSource_Memory(const MemoryRegion<byte>& in) *************************************************/ DataSource_Memory::DataSource_Memory(const std::string& in) { - source.set((const byte*)in.data(), in.length()); + source.set(reinterpret_cast<const byte*>(in.data()), in.length()); offset = 0; } @@ -102,13 +102,13 @@ DataSource_Memory::DataSource_Memory(const std::string& in) *************************************************/ u32bit DataSource_Stream::read(byte out[], u32bit length) { - source->read((char*)out, length); + source->read(reinterpret_cast<char*>(out), length); if(source->bad()) throw Stream_IO_Error("DataSource_Stream::read: Source failure"); u32bit got = source->gcount(); total_read += got; - return (u32bit)got; + return got; } /************************************************* @@ -124,7 +124,7 @@ u32bit DataSource_Stream::peek(byte out[], u32bit length, u32bit offset) const if(offset) { SecureVector<byte> buf(offset); - source->read((char*)buf.begin(), buf.size()); + source->read(reinterpret_cast<char*>(buf.begin()), buf.size()); if(source->bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); got = source->gcount(); @@ -132,7 +132,7 @@ u32bit DataSource_Stream::peek(byte out[], u32bit length, u32bit offset) const if(got == offset) { - source->read((char*)out, length); + source->read(reinterpret_cast<char*>(out), length); if(source->bad()) throw Stream_IO_Error("DataSource_Stream::peek: Source failure"); got = source->gcount(); diff --git a/src/def_char.cpp b/src/def_char.cpp index 69b00e620..0bbd719f6 100644 --- a/src/def_char.cpp +++ b/src/def_char.cpp @@ -29,7 +29,7 @@ std::string ucs2_to_latin1(const std::string& ucs2) if(c1 != 0) throw Decoding_Error("UCS-2 has non-Latin1 characters"); - latin1 += (char)c2; + latin1 += static_cast<char>(c2); } return latin1; @@ -45,22 +45,22 @@ std::string utf8_to_latin1(const std::string& utf8) u32bit position = 0; while(position != utf8.size()) { - const byte c1 = (byte)utf8[position++]; + const byte c1 = static_cast<byte>(utf8[position++]); if(c1 <= 0x7F) - iso8859 += (char)c1; + iso8859 += static_cast<char>(c1); else if(c1 >= 0xC0 && c1 <= 0xC7) { if(position == utf8.size()) throw Decoding_Error("UTF-8: sequence truncated"); - const byte c2 = (byte)utf8[position++]; + const byte c2 = static_cast<byte>(utf8[position++]); const byte iso_char = ((c1 & 0x07) << 6) | (c2 & 0x3F); if(iso_char <= 0x7F) throw Decoding_Error("UTF-8: sequence longer than needed"); - iso8859 += (char)iso_char; + iso8859 += static_cast<char>(iso_char); } else throw Decoding_Error("UTF-8: Unicode chars not in Latin1 used"); @@ -77,14 +77,14 @@ std::string latin1_to_utf8(const std::string& iso8859) std::string utf8; for(u32bit j = 0; j != iso8859.size(); ++j) { - const byte c = (byte)iso8859[j]; + const byte c = static_cast<byte>(iso8859[j]); if(c <= 0x7F) - utf8 += (char)c; + utf8 += static_cast<char>(c); else { - utf8 += (char)(0xC0 | (c >> 6)); - utf8 += (char)(0x80 | (c & 0x3F)); + utf8 += static_cast<char>((0xC0 | (c >> 6))); + utf8 += static_cast<char>((0x80 | (c & 0x3F))); } } return utf8; diff --git a/src/der_enc.cpp b/src/der_enc.cpp index 1a4bb1b48..fa4575858 100644 --- a/src/der_enc.cpp +++ b/src/der_enc.cpp @@ -25,7 +25,7 @@ SecureVector<byte> encode_tag(ASN1_Tag type_tag, ASN1_Tag class_tag) SecureVector<byte> encoded_tag; if(type_tag <= 30) - encoded_tag.append((byte)(type_tag | class_tag)); + encoded_tag.append(static_cast<byte>(type_tag | class_tag)); else { u32bit blocks = high_bit(type_tag) + 6; @@ -47,11 +47,11 @@ SecureVector<byte> encode_length(u32bit length) { SecureVector<byte> encoded_length; if(length <= 127) - encoded_length.append((byte)length); + encoded_length.append(static_cast<byte>(length)); else { const u32bit top_byte = significant_bytes(length); - encoded_length.append((byte)(0x80 | top_byte)); + encoded_length.append(static_cast<byte>(0x80 | top_byte)); for(u32bit j = 4-top_byte; j != 4; ++j) encoded_length.append(get_byte(j, length)); } @@ -157,7 +157,7 @@ DER_Encoder& DER_Encoder::end_cons() *************************************************/ DER_Encoder& DER_Encoder::start_explicit(u16bit type_no) { - ASN1_Tag type_tag = (ASN1_Tag)type_no; + ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no); if(type_tag == SET) throw Internal_Error("DER_Encoder.start_explicit(SET); cannot perform"); @@ -373,7 +373,7 @@ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string& rep_str) { - const byte* rep = (const byte*)rep_str.data(); + const byte* rep = reinterpret_cast<const byte*>(rep_str.data()); const u32bit rep_len = rep_str.size(); return add_object(type_tag, class_tag, rep, rep_len); } diff --git a/src/des.cpp b/src/des.cpp index e8f173c5c..de95ed5be 100644 --- a/src/des.cpp +++ b/src/des.cpp @@ -45,8 +45,8 @@ void DES::IP(u32bit& L, u32bit& R) (IPTAB1[get_byte(2, L)] << 2) | (IPTAB1[get_byte(3, L)] << 3) | (IPTAB1[get_byte(0, R)] << 4) | (IPTAB1[get_byte(1, R)] << 5) | (IPTAB1[get_byte(2, R)] << 6) | (IPTAB2[get_byte(3, R)] ); - L = (u32bit)((T >> 32) & 0xFFFFFFFF); - R = (u32bit)((T ) & 0xFFFFFFFF); + L = static_cast<u32bit>(T >> 32); + R = static_cast<u32bit>(T); } /************************************************* @@ -58,8 +58,8 @@ void DES::FP(u32bit& L, u32bit& R) (FPTAB1[get_byte(2, L)] << 1) | (FPTAB2[get_byte(3, L)] << 1) | (FPTAB1[get_byte(0, R)] << 4) | (FPTAB1[get_byte(1, R)] << 2) | (FPTAB1[get_byte(2, R)] ) | (FPTAB2[get_byte(3, R)] ); - L = (u32bit)((T >> 32) & 0xFFFFFFFF); - R = (u32bit)((T ) & 0xFFFFFFFF); + L = static_cast<u32bit>(T >> 32); + R = static_cast<u32bit>(T); } /************************************************* diff --git a/src/es_file.cpp b/src/es_file.cpp index 3064bdc53..5d010fa70 100644 --- a/src/es_file.cpp +++ b/src/es_file.cpp @@ -22,7 +22,7 @@ u32bit File_EntropySource::slow_poll(byte output[], u32bit length) { std::ifstream random_source(sources[j].c_str(), std::ios::binary); if(!random_source) continue; - random_source.read((char*)output + read, length); + random_source.read(reinterpret_cast<char*>(output) + read, length); read += random_source.gcount(); length -= random_source.gcount(); if(length == 0) diff --git a/src/hex.cpp b/src/hex.cpp index 384c8421d..88ee4b6ae 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -145,7 +145,7 @@ void Hex_Decoder::handle_bad_char(byte c) *************************************************/ byte Hex_Decoder::decode(const byte hex[2]) { - return (byte)((HEX_TO_BIN[hex[0]] << 4) | HEX_TO_BIN[hex[1]]); + return ((HEX_TO_BIN[hex[0]] << 4) | HEX_TO_BIN[hex[1]]); } /************************************************* diff --git a/src/idea.cpp b/src/idea.cpp index ed142ca9b..c12f7b5b9 100644 --- a/src/idea.cpp +++ b/src/idea.cpp @@ -17,13 +17,13 @@ inline void mul(u16bit& a, u16bit b) { if(a && b) { - u32bit temp = (u32bit)a * b; - a = (u16bit)(temp >> 16); - b = (u16bit)(temp & 0xFFFF); - a = (u16bit)(b - a + ((b < a) ? 1 : 0)); + u32bit temp = static_cast<u32bit>(a) * b; + a = static_cast<u16bit>(temp >> 16); + b = static_cast<u16bit>(temp & 0xFFFF); + a = static_cast<u16bit>(b - a + ((b < a) ? 1 : 0)); } else - a = (u16bit)(1 - a - b); + a = static_cast<u16bit>(1 - a - b); } } @@ -48,7 +48,7 @@ void IDEA::enc(const byte in[], byte out[]) const X3 ^= X1; mul(X3, EK[6*j+4]); u16bit T1 = X2; - X2 = (u16bit)((X2 ^ X4) + X3); + X2 = static_cast<u16bit>((X2 ^ X4) + X3); mul(X2, EK[6*j+5]); X3 += X2; X1 ^= X2; @@ -82,7 +82,7 @@ void IDEA::dec(const byte in[], byte out[]) const X3 ^= X1; mul(X3, DK[6*j+4]); u16bit T1 = X2; - X2 = (u16bit)((X2 ^ X4) + X3); + X2 = static_cast<u16bit>((X2 ^ X4) + X3); mul(X2, DK[6*j+5]); X3 += X2; X1 ^= X2; @@ -103,19 +103,22 @@ u16bit IDEA::mul_inv(u16bit x) { if(x <= 1) return x; - u16bit t0 = (u16bit)(65537 / x), t1 = 1, y = (u16bit)(65537 % x); + + u16bit t0 = static_cast<u16bit>(65537 / x), t1 = 1; + u16bit y = static_cast<u16bit>(65537 % x); + while(y != 1) { - u16bit q = (u16bit)(x / y); + u16bit q = static_cast<u16bit>(x / y); x %= y; - t1 += (u16bit)(q * t0); + t1 += static_cast<u16bit>(q * t0); if(x == 1) return t1; - q = (u16bit)(y / x); + q = static_cast<u16bit>(y / x); y %= x; - t0 += (u16bit)(q * t1); + t0 += static_cast<u16bit>(q * t1); } - return (u16bit)(1 - t0); + return static_cast<u16bit>(1 - t0); } /************************************************* @@ -128,14 +131,14 @@ void IDEA::key(const byte key[], u32bit) for(u32bit j = 1, k = 8, offset = 0; k != 52; j %= 8, ++j, ++k) { - EK[j+7+offset] = (u16bit)((EK[(j % 8) + offset] << 9) | - (EK[((j+1) % 8) + offset] >> 7)); + EK[j+7+offset] = static_cast<u16bit>((EK[(j % 8) + offset] << 9) | + (EK[((j+1) % 8) + offset] >> 7)); offset += (j == 8) ? 8 : 0; } DK[51] = mul_inv(EK[3]); - DK[50] = (u16bit)-EK[2]; - DK[49] = (u16bit)-EK[1]; + DK[50] = -EK[2]; + DK[49] = -EK[1]; DK[48] = mul_inv(EK[0]); for(u32bit j = 1, k = 4, counter = 47; j != 8; ++j, k += 6) @@ -143,16 +146,16 @@ void IDEA::key(const byte key[], u32bit) DK[counter--] = EK[k+1]; DK[counter--] = EK[k]; DK[counter--] = mul_inv(EK[k+5]); - DK[counter--] = (u16bit)-EK[k+3]; - DK[counter--] = (u16bit)-EK[k+4]; + DK[counter--] = -EK[k+3]; + DK[counter--] = -EK[k+4]; DK[counter--] = mul_inv(EK[k+2]); } DK[5] = EK[47]; DK[4] = EK[46]; DK[3] = mul_inv(EK[51]); - DK[2] = (u16bit)-EK[50]; - DK[1] = (u16bit)-EK[49]; + DK[2] = -EK[50]; + DK[1] = -EK[49]; DK[0] = mul_inv(EK[48]); } diff --git a/src/if_algo.cpp b/src/if_algo.cpp index 2a96163f7..204daed1d 100644 --- a/src/if_algo.cpp +++ b/src/if_algo.cpp @@ -90,7 +90,7 @@ PKCS8_Encoder* IF_Scheme_PrivateKey::pkcs8_encoder() const { return DER_Encoder() .start_cons(SEQUENCE) - .encode((u32bit)0) + .encode(static_cast<u32bit>(0)) .encode(key->n) .encode(key->e) .encode(key->d) diff --git a/src/kdf.cpp b/src/kdf.cpp index 5dd53b957..3a7a2e025 100644 --- a/src/kdf.cpp +++ b/src/kdf.cpp @@ -19,7 +19,8 @@ SecureVector<byte> KDF::derive_key(u32bit key_len, const std::string& salt) const { return derive_key(key_len, secret, secret.size(), - (const byte*)salt.data(), salt.length()); + reinterpret_cast<const byte*>(salt.data()), + salt.length()); } /************************************************* @@ -52,7 +53,8 @@ SecureVector<byte> KDF::derive_key(u32bit key_len, const std::string& salt) const { return derive_key(key_len, secret, secret_len, - (const byte*)salt.data(), salt.length()); + reinterpret_cast<const byte*>(salt.data()), + salt.length()); } /************************************************* diff --git a/src/md2.cpp b/src/md2.cpp index a5a713574..8984f9f61 100644 --- a/src/md2.cpp +++ b/src/md2.cpp @@ -85,7 +85,7 @@ void MD2::add_data(const byte input[], u32bit length) void MD2::final_result(byte output[]) { for(u32bit j = position; j != HASH_BLOCK_SIZE; ++j) - buffer[j] = (byte)(HASH_BLOCK_SIZE - position); + buffer[j] = static_cast<byte>(HASH_BLOCK_SIZE - position); hash(buffer); hash(checksum); copy_mem(output, X.begin(), OUTPUT_LENGTH); diff --git a/src/mem_pool.cpp b/src/mem_pool.cpp index faf399ec6..29930b8a1 100644 --- a/src/mem_pool.cpp +++ b/src/mem_pool.cpp @@ -48,7 +48,7 @@ bool Pooling_Allocator::Memory_Block::contains(void* ptr, u32bit length) const throw() { return ((buffer <= ptr) && - (buffer_end >= (byte*)ptr + length * BLOCK_SIZE)); + (buffer_end >= static_cast<byte*>(ptr) + length * BLOCK_SIZE)); } /************************************************* @@ -70,7 +70,7 @@ byte* Pooling_Allocator::Memory_Block::alloc(u32bit n) throw() } } - bitmap_type mask = ((bitmap_type)1 << n) - 1; + bitmap_type mask = (static_cast<bitmap_type>(1) << n) - 1; u32bit offset = 0; while(bitmap & mask) @@ -96,16 +96,16 @@ byte* Pooling_Allocator::Memory_Block::alloc(u32bit n) throw() *************************************************/ void Pooling_Allocator::Memory_Block::free(void* ptr, u32bit blocks) throw() { - clear_mem((byte*)ptr, blocks * BLOCK_SIZE); + clear_mem(static_cast<byte*>(ptr), blocks * BLOCK_SIZE); - const u32bit offset = ((byte*)ptr - buffer) / BLOCK_SIZE; + const u32bit offset = (static_cast<byte*>(ptr) - buffer) / BLOCK_SIZE; if(offset == 0 && blocks == BITMAP_SIZE) bitmap = ~bitmap; else { for(u32bit j = 0; j != blocks; ++j) - bitmap &= ~((bitmap_type)1 << (j+offset)); + bitmap &= ~(static_cast<bitmap_type>(1) << (j+offset)); } } diff --git a/src/misty1.cpp b/src/misty1.cpp index 4df3ecc76..6405dc335 100644 --- a/src/misty1.cpp +++ b/src/misty1.cpp @@ -20,7 +20,7 @@ u16bit FI(u16bit input, u16bit key7, u16bit key9) D9 = MISTY1_SBOX_S9[D9] ^ D7; D7 = (MISTY1_SBOX_S7[D7] ^ key7 ^ D9) & 0x7F; D9 = MISTY1_SBOX_S9[D9 ^ key9] ^ D7; - return (u16bit)((D7 << 9) | D9); + return static_cast<u16bit>((D7 << 9) | D9); } } diff --git a/src/openpgp.cpp b/src/openpgp.cpp index e2301207e..b94b0c5b2 100644 --- a/src/openpgp.cpp +++ b/src/openpgp.cpp @@ -97,7 +97,7 @@ SecureVector<byte> decode(DataSource& source, std::string& label, throw Decoding_Error("PGP: Malformed PGP header"); if(position == 0) - label += (char)b; + label += static_cast<char>(b); } headers.clear(); @@ -111,7 +111,7 @@ SecureVector<byte> decode(DataSource& source, std::string& label, if(!source.read_byte(b)) throw Decoding_Error("PGP: Bad armor header"); if(b != '\n') - this_header += (char)b; + this_header += static_cast<char>(b); } end_of_headers = true; @@ -160,7 +160,7 @@ SecureVector<byte> decode(DataSource& source, std::string& label, if(!source.read_byte(b)) throw Decoding_Error("PGP: Bad CRC tail"); if(b != '\n') - crc += (char)b; + crc += static_cast<char>(b); } } else if(b == '\n') diff --git a/src/pem.cpp b/src/pem.cpp index fd037b53b..67546986f 100644 --- a/src/pem.cpp +++ b/src/pem.cpp @@ -88,7 +88,7 @@ SecureVector<byte> decode(DataSource& source, std::string& label) throw Decoding_Error("PEM: Malformed PEM header"); if(position == 0) - label += (char)b; + label += static_cast<char>(b); } Pipe base64(new Base64_Decoder); diff --git a/src/pgp_s2k.cpp b/src/pgp_s2k.cpp index b8a27cc6e..8da044a1c 100644 --- a/src/pgp_s2k.cpp +++ b/src/pgp_s2k.cpp @@ -44,7 +44,7 @@ OctetString OpenPGP_S2K::derive(u32bit key_len, const std::string& passphrase, { hash->update(salt_buf, salt_size); left -= salt_size; - hash->update((const byte*)passphrase.data(), left); + hash->update(reinterpret_cast<const byte*>(passphrase.data()), left); } hash_buf = hash->final(); diff --git a/src/pipe.cpp b/src/pipe.cpp index e876e57d1..f821a9ffe 100644 --- a/src/pipe.cpp +++ b/src/pipe.cpp @@ -130,7 +130,7 @@ void Pipe::process_msg(const MemoryRegion<byte>& input) *************************************************/ void Pipe::process_msg(const std::string& input) { - process_msg((const byte*)input.data(), input.length()); + process_msg(reinterpret_cast<const byte*>(input.data()), input.length()); } /************************************************* diff --git a/src/pipe_io.cpp b/src/pipe_io.cpp index a4da3d5ac..bd59e58cd 100644 --- a/src/pipe_io.cpp +++ b/src/pipe_io.cpp @@ -17,7 +17,7 @@ std::ostream& operator<<(std::ostream& stream, Pipe& pipe) while(stream.good() && pipe.remaining()) { u32bit got = pipe.read(buffer, buffer.size()); - stream.write((const char*)buffer.begin(), got); + stream.write(reinterpret_cast<const char*>(buffer.begin()), got); } if(!stream.good()) throw Stream_IO_Error("Pipe output operator (iostream) has failed"); @@ -32,7 +32,7 @@ std::istream& operator>>(std::istream& stream, Pipe& pipe) SecureVector<byte> buffer(DEFAULT_BUFFERSIZE); while(stream.good()) { - stream.read((char*)buffer.begin(), buffer.size()); + stream.read(reinterpret_cast<char*>(buffer.begin()), buffer.size()); pipe.write(buffer, stream.gcount()); } if(stream.bad() || (stream.fail() && !stream.eof())) diff --git a/src/pipe_rw.cpp b/src/pipe_rw.cpp index 7a7e672fb..1fa195210 100644 --- a/src/pipe_rw.cpp +++ b/src/pipe_rw.cpp @@ -48,7 +48,7 @@ void Pipe::write(const MemoryRegion<byte>& input) *************************************************/ void Pipe::write(const std::string& str) { - write((const byte*)str.data(), str.size()); + write(reinterpret_cast<const byte*>(str.data()), str.size()); } /************************************************* @@ -122,7 +122,7 @@ std::string Pipe::read_all_as_string(u32bit msg) u32bit got = read(buffer, buffer.size(), msg); if(got == 0) break; - str.append((const char*)buffer.begin(), got); + str.append(reinterpret_cast<const char*>(buffer.begin()), got); } return str; diff --git a/src/pkcs5.cpp b/src/pkcs5.cpp index c0dc72b35..c762ecf31 100644 --- a/src/pkcs5.cpp +++ b/src/pkcs5.cpp @@ -72,7 +72,10 @@ OctetString PKCS5_PBKDF2::derive(u32bit key_len, throw Invalid_Argument("PKCS#5 PBKDF2: Empty passphrase is invalid"); HMAC hmac(hash_name); - hmac.set_key((const byte*)passphrase.data(), passphrase.length()); + + hmac.set_key(reinterpret_cast<const byte*>(passphrase.data()), + passphrase.length()); + SecureVector<byte> key(key_len); byte* T = key.begin(); diff --git a/src/pubkey.cpp b/src/pubkey.cpp index f480c425f..cf9cb927c 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -378,7 +378,8 @@ SymmetricKey PK_Key_Agreement::derive_key(u32bit key_len, const std::string& params) const { return derive_key(key_len, in, in_len, - (const byte*)params.data(), params.length()); + reinterpret_cast<const byte*>(params.data()), + params.length()); } /************************************************* diff --git a/src/randpool.cpp b/src/randpool.cpp index ed60b385a..f51660160 100644 --- a/src/randpool.cpp +++ b/src/randpool.cpp @@ -29,7 +29,7 @@ SecureVector<byte> randpool_prf(MessageAuthenticationCode* mac, RANDPOOL_PRF_TAG tag, const byte in[], u32bit length) { - mac->update((byte)tag); + mac->update(static_cast<byte>(tag)); mac->update(in, length); return mac->final(); } diff --git a/src/util.cpp b/src/util.cpp index fec8cfe3d..2744f984a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -40,10 +40,12 @@ u32bit dl_work_factor(u32bit n_bits) const double log_x = n_bits / 1.44; - u32bit estimate = (u32bit)(2.76 * std::pow(log_x, 1.0/3.0) * - std::pow(std::log(log_x), 2.0/3.0)); + const double strength = + 2.76 * std::pow(log_x, 1.0/3.0) * std::pow(std::log(log_x), 2.0/3.0); - return std::max(estimate, MIN_ESTIMATE); + if(strength > MIN_ESTIMATE) + return static_cast<u32bit>(strength); + return MIN_ESTIMATE; } /************************************************* diff --git a/src/x509_ext.cpp b/src/x509_ext.cpp index a96f9ae42..65ea2872c 100644 --- a/src/x509_ext.cpp +++ b/src/x509_ext.cpp @@ -525,7 +525,7 @@ void CRL_Number::contents_to(Data_Store& info, Data_Store&) const MemoryVector<byte> CRL_ReasonCode::encode_inner() const { return DER_Encoder() - .encode((u32bit)reason, ENUMERATED, UNIVERSAL) + .encode(static_cast<u32bit>(reason), ENUMERATED, UNIVERSAL) .get_contents(); } @@ -536,7 +536,7 @@ void CRL_ReasonCode::decode_inner(const MemoryRegion<byte>& in) { u32bit reason_code = 0; BER_Decoder(in).decode(reason_code, ENUMERATED, UNIVERSAL); - reason = (CRL_Code)reason_code; + reason = static_cast<CRL_Code>(reason_code); } /************************************************* |