diff options
author | lloyd <[email protected]> | 2014-02-13 15:09:31 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-02-13 15:09:31 +0000 |
commit | 9168b5747037b7cde74e3bb60ffce6bf27be910b (patch) | |
tree | 2c4408665ca27de7a96ab9c46a048363a34ad7a0 /src | |
parent | 6af80db3b7697461e3f24d6ff7a306b1b9eea8a4 (diff) |
Cleanups
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/asn1.cpp | 64 | ||||
-rw-r--r-- | src/cmd/tls_client.cpp | 1 | ||||
-rw-r--r-- | src/cmd/tls_server.cpp | 1 | ||||
-rw-r--r-- | src/tests/test_bigint.cpp | 6 | ||||
-rw-r--r-- | src/tests/test_tss.cpp | 12 |
5 files changed, 43 insertions, 41 deletions
diff --git a/src/cmd/asn1.cpp b/src/cmd/asn1.cpp index eeda6f809..f9baae902 100644 --- a/src/cmd/asn1.cpp +++ b/src/cmd/asn1.cpp @@ -10,8 +10,9 @@ #include <botan/charset.h> using namespace Botan; +#include <iostream> +#include <iomanip> #include <sstream> -#include <stdio.h> #include <ctype.h> // Set this if your terminal understands UTF-8; otherwise output is in Latin-1 @@ -34,14 +35,23 @@ std::string url_encode(const std::vector<byte>& in) { std::ostringstream out; + size_t unprintable = 0; + for(size_t i = 0; i != in.size(); ++i) { const int c = in[i]; - if(isprint(c)) + if(::isprint(c)) out << static_cast<char>(c); else - out << "%" << std::hex << static_cast<char>(c) << std::dec; + { + out << "%" << std::hex << static_cast<int>(c) << std::dec; + ++unprintable; + } } + + if(unprintable >= in.size() / 4) + return hex_encode(in); + return out.str(); } @@ -51,7 +61,7 @@ int asn1_main(int argc, char* argv[]) { if(argc != 2) { - printf("Usage: %s <file>\n", argv[0]); + std::cout << "Usage: " << argv[0] << " <file>\n"; return 1; } @@ -69,13 +79,13 @@ int asn1_main(int argc, char* argv[]) BER_Decoder decoder(PEM_Code::decode(in, label)); decode(decoder, INITIAL_LEVEL); } - } catch(std::exception& e) { - printf("%s\n", e.what()); - return 1; + std::cout << "Error: " << e.what() << "\n"; + return 2; } + return 0; } @@ -259,8 +269,12 @@ void decode(BER_Decoder& decoder, size_t level) emit(type_name(type_tag), level, length, time.readable_string()); } else - fprintf(stderr, "Unknown tag: class=%02X, type=%02X\n", - class_tag, type_tag); + { + std::cout << "Unknown ASN.1 tag class=" + << static_cast<int>(class_tag) + << " type=" + << static_cast<int>(type_tag) << "\n"; + } obj = decoder.get_next_object(); } @@ -272,26 +286,34 @@ void emit(const std::string& type, size_t level, size_t length, const size_t LIMIT = 128; const size_t BIN_LIMIT = 64; - int written = 0; - written += printf(" d=%2d, l=%4d: ", (int)level, (int)length); + std::ostringstream out; + + out << " d=" << std::setw(2) << level + << ", l=" << std::setw(4) << length << ": "; + for(size_t i = INITIAL_LEVEL; i != level; ++i) - written += printf(" "); - written += printf("%s ", type.c_str()); + out << ' '; + + out << type; bool should_skip = false; - if(value.length() > LIMIT) should_skip = true; - if((type == "OCTET STRING" || type == "BIT STRING") && - value.length() > BIN_LIMIT) + + if(value.length() > LIMIT) + should_skip = true; + + if((type == "OCTET STRING" || type == "BIT STRING") && value.length() > BIN_LIMIT) should_skip = true; if(value != "" && !should_skip) { - if(written % 2 == 0) printf(" "); - while(written < 50) written += printf(" "); - printf(":%s\n", value.c_str()); + if(out.tellp() % 2 == 0) out << ' '; + + while(out.tellp() < 50) out << ' '; + + out << value; } - else - printf("\n"); + + std::cout << out.str() << "\n"; } std::string type_name(ASN1_Tag type) diff --git a/src/cmd/tls_client.cpp b/src/cmd/tls_client.cpp index 0c92d2cba..18b8044b5 100644 --- a/src/cmd/tls_client.cpp +++ b/src/cmd/tls_client.cpp @@ -4,7 +4,6 @@ #include <botan/tls_client.h> #include <botan/pkcs8.h> #include <botan/hex.h> -#include <stdio.h> #include <string> #include <iostream> #include <memory> diff --git a/src/cmd/tls_server.cpp b/src/cmd/tls_server.cpp index 396497727..980a68d09 100644 --- a/src/cmd/tls_server.cpp +++ b/src/cmd/tls_server.cpp @@ -15,7 +15,6 @@ using namespace Botan; using namespace std::placeholders; -#include <stdio.h> #include <string> #include <iostream> #include <memory> diff --git a/src/tests/test_bigint.cpp b/src/tests/test_bigint.cpp index b71e0af2d..87de4aff3 100644 --- a/src/tests/test_bigint.cpp +++ b/src/tests/test_bigint.cpp @@ -28,12 +28,6 @@ void strip_comments(std::string& line) line = line.erase(line.find('#'), std::string::npos); } -void strip_newlines(std::string& line) - { - while(line.find('\n') != std::string::npos) - line = line.erase(line.find('\n'), 1); - } - /* Strip comments, whitespace, etc */ void strip(std::string& line) { diff --git a/src/tests/test_tss.cpp b/src/tests/test_tss.cpp index 38d9dfbfa..7645af399 100644 --- a/src/tests/test_tss.cpp +++ b/src/tests/test_tss.cpp @@ -8,23 +8,11 @@ #include <botan/auto_rng.h> #include <botan/hex.h> #include <iostream> -#include <stdio.h> #if defined(BOTAN_HAS_THRESHOLD_SECRET_SHARING) #include <botan/tss.h> -namespace { - -void print(const Botan::secure_vector<Botan::byte>& r) - { - for(Botan::u32bit i = 0; i != r.size(); ++i) - printf("%02X", r[i]); - printf("\n"); - } - -} - size_t test_tss() { using namespace Botan; |