• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/src/cli/asn1.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#if defined(BOTAN_HAS_ASN1)
10

11
   #include <botan/asn1_print.h>
12
   #include <botan/data_src.h>
13

14
   #if defined(BOTAN_HAS_PEM_CODEC)
15
      #include <botan/pem.h>
16
   #endif
17

18
namespace Botan_CLI {
19

20
class ASN1_Printer final : public Command {
21
   public:
22
      ASN1_Printer() :
2✔
23
            Command("asn1print --skip-context-specific --print-limit=4096 --bin-limit=2048 --max-depth=64 --pem file") {
2✔
24
      }
2✔
25

26
      std::string group() const override { return "asn1"; }
1✔
27

28
      std::string description() const override { return "Decode and print file with ASN.1 Basic Encoding Rules (BER)"; }
1✔
29

30
      static bool first_n(const std::vector<uint8_t>& data, size_t n, uint8_t b) {
31
         if(data.size() < n)
32
            return false;
33

34
         for(size_t i = 0; i != n; ++i)
×
35
            if(data[i] != b)
×
36
               return false;
37

38
         return true;
39
      }
40

41
      void go() override {
1✔
42
         const std::string input = get_arg("file");
1✔
43
         const size_t print_limit = get_arg_sz("print-limit");
1✔
44
         const size_t bin_limit = get_arg_sz("bin-limit");
1✔
45
         const bool print_context_specific = flag_set("skip-context-specific") == false;
1✔
46
         const size_t max_depth = get_arg_sz("max-depth");
1✔
47

48
         const size_t value_column = 60;
1✔
49
         const size_t initial_level = 0;
1✔
50

51
         std::vector<uint8_t> file_contents = slurp_file(input);
1✔
52
         std::vector<uint8_t> data;
1✔
53

54
         if(flag_set("pem") || (input.size() > 4 && input.substr(input.size() - 4) == ".pem") ||
2✔
55
            (file_contents.size() > 20 && first_n(file_contents, 5, '-'))) {
1✔
56
   #if defined(BOTAN_HAS_PEM_CODEC)
57
            std::string pem_label;
1✔
58
            Botan::DataSource_Memory src(file_contents);
1✔
59
            data = unlock(Botan::PEM_Code::decode(src, pem_label));
3✔
60
   #else
61
            throw CLI_Error_Unsupported("PEM decoding not available in this build");
62
   #endif
63
         } else {
1✔
64
            data.swap(file_contents);
×
65
         }
66

67
         Botan::ASN1_Pretty_Printer printer(
1✔
68
            print_limit, bin_limit, print_context_specific, initial_level, value_column, max_depth);
1✔
69

70
         printer.print_to_stream(output(), data.data(), data.size());
1✔
71
      }
3✔
72
};
73

74
BOTAN_REGISTER_COMMAND("asn1print", ASN1_Printer);
2✔
75

76
class OID_Info final : public Command {
77
   public:
78
      OID_Info() : Command("oid_info oid") {}
8✔
79

80
      std::string group() const override { return "asn1"; }
1✔
81

82
      std::string description() const override { return "Provide information about an object identifier"; }
1✔
83

84
      void go() override {
3✔
85
         const std::string oid_str = get_arg("oid");
3✔
86

87
         if(oid_str.empty())
3✔
88
            throw CLI_Usage_Error("Must provide non-empty OID string");
×
89

90
         try {
3✔
91
            Botan::OID oid(oid_str);
3✔
92

93
            std::string name = oid.human_name_or_empty();
2✔
94
            if(name.empty())
2✔
95
               output() << "OID " << oid_str << " is not recognized\n";
1✔
96
            else
97
               output() << "OID " << oid_str << " is associated with " << name << "\n";
1✔
98

99
            return;
2✔
100
         } catch(Botan::Decoding_Error&) {}
3✔
101

102
         // This throws if the string is not known
103
         Botan::OID oid = Botan::OID::from_string(oid_str);
1✔
104
         output() << "The string '" << oid_str << "' is associated with OID " << oid.to_string() << "\n";
2✔
105
      }
3✔
106
};
107

108
BOTAN_REGISTER_COMMAND("oid_info", OID_Info);
4✔
109

110
}
111

112
#endif  // BOTAN_HAS_ASN1
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc