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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

35
         for(size_t i = 0; i != n; ++i) {
×
36
            if(data[i] != b) {
×
37
               return false;
38
            }
39
         }
40

41
         return true;
42
      }
43

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

51
         const size_t value_column = 60;
1✔
52
         const size_t initial_level = 0;
1✔
53

54
         std::vector<uint8_t> file_contents = slurp_file(input);
1✔
55
         std::vector<uint8_t> data;
1✔
56

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

70
         Botan::ASN1_Pretty_Printer printer(
1✔
71
            print_limit, bin_limit, print_context_specific, initial_level, value_column, max_depth);
1✔
72

73
         printer.print_to_stream(output(), data.data(), data.size());
1✔
74
      }
3✔
75
};
76

77
BOTAN_REGISTER_COMMAND("asn1print", ASN1_Printer);
2✔
78

79
class OID_Info final : public Command {
80
   public:
81
      OID_Info() : Command("oid_info oid") {}
8✔
82

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

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

87
      void go() override {
3✔
88
         const std::string oid_str = get_arg("oid");
3✔
89

90
         if(oid_str.empty()) {
3✔
91
            throw CLI_Usage_Error("Must provide non-empty OID string");
×
92
         }
93

94
         try {
3✔
95
            Botan::OID oid(oid_str);
3✔
96

97
            std::string name = oid.human_name_or_empty();
2✔
98
            if(name.empty()) {
2✔
99
               output() << "OID " << oid_str << " is not recognized\n";
1✔
100
            } else {
101
               output() << "OID " << oid_str << " is associated with " << name << "\n";
1✔
102
            }
103

104
            return;
2✔
105
         } catch(Botan::Decoding_Error&) {}
3✔
106

107
         // This throws if the string is not known
108
         Botan::OID oid = Botan::OID::from_string(oid_str);
1✔
109
         output() << "The string '" << oid_str << "' is associated with OID " << oid.to_string() << "\n";
2✔
110
      }
3✔
111
};
112

113
BOTAN_REGISTER_COMMAND("oid_info", OID_Info);
4✔
114

115
}  // namespace Botan_CLI
116

117
#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