• 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

84.72
/src/lib/utils/charset.cpp
1
/*
2
* Character Set Handling
3
* (C) 1999-2007,2021 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/internal/charset.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/internal/loadstor.h>
12
#include <sstream>
13

14
namespace Botan {
15

16
namespace {
17

18
void append_utf8_for(std::string& s, uint32_t c) {
29,312✔
19
   if(c >= 0xD800 && c < 0xE000) {
29,312✔
20
      throw Decoding_Error("Invalid Unicode character");
×
21
   }
22

23
   if(c <= 0x7F) {
29,312✔
24
      const uint8_t b0 = static_cast<uint8_t>(c);
28,476✔
25
      s.push_back(static_cast<char>(b0));
28,476✔
26
   } else if(c <= 0x7FF) {
836✔
27
      const uint8_t b0 = 0xC0 | static_cast<uint8_t>(c >> 6);
671✔
28
      const uint8_t b1 = 0x80 | static_cast<uint8_t>(c & 0x3F);
671✔
29
      s.push_back(static_cast<char>(b0));
671✔
30
      s.push_back(static_cast<char>(b1));
671✔
31
   } else if(c <= 0xFFFF) {
165✔
32
      const uint8_t b0 = 0xE0 | static_cast<uint8_t>(c >> 12);
151✔
33
      const uint8_t b1 = 0x80 | static_cast<uint8_t>((c >> 6) & 0x3F);
151✔
34
      const uint8_t b2 = 0x80 | static_cast<uint8_t>(c & 0x3F);
151✔
35
      s.push_back(static_cast<char>(b0));
151✔
36
      s.push_back(static_cast<char>(b1));
151✔
37
      s.push_back(static_cast<char>(b2));
151✔
38
   } else if(c <= 0x10FFFF) {
14✔
39
      const uint8_t b0 = 0xF0 | static_cast<uint8_t>(c >> 18);
×
40
      const uint8_t b1 = 0x80 | static_cast<uint8_t>((c >> 12) & 0x3F);
×
41
      const uint8_t b2 = 0x80 | static_cast<uint8_t>((c >> 6) & 0x3F);
×
42
      const uint8_t b3 = 0x80 | static_cast<uint8_t>(c & 0x3F);
×
43
      s.push_back(static_cast<char>(b0));
×
44
      s.push_back(static_cast<char>(b1));
×
45
      s.push_back(static_cast<char>(b2));
×
46
      s.push_back(static_cast<char>(b3));
×
47
   } else {
48
      throw Decoding_Error("Invalid Unicode character");
14✔
49
   }
50
}
29,298✔
51

52
}  // namespace
53

54
std::string ucs2_to_utf8(const uint8_t ucs2[], size_t len) {
1,179✔
55
   if(len % 2 != 0) {
1,179✔
56
      throw Decoding_Error("Invalid length for UCS-2 string");
7✔
57
   }
58

59
   const size_t chars = len / 2;
1,172✔
60

61
   std::string s;
1,172✔
62
   for(size_t i = 0; i != chars; ++i) {
2,930✔
63
      const uint32_t c = load_be<uint16_t>(ucs2, i);
1,758✔
64
      append_utf8_for(s, c);
1,758✔
65
   }
66

67
   return s;
1,172✔
68
}
×
69

70
std::string ucs4_to_utf8(const uint8_t ucs4[], size_t len) {
28✔
71
   if(len % 4 != 0) {
28✔
72
      throw Decoding_Error("Invalid length for UCS-4 string");
5✔
73
   }
74

75
   const size_t chars = len / 4;
23✔
76

77
   std::string s;
23✔
78
   for(size_t i = 0; i != chars; ++i) {
67✔
79
      const uint32_t c = load_be<uint32_t>(ucs4, i);
58✔
80
      append_utf8_for(s, c);
58✔
81
   }
82

83
   return s;
9✔
84
}
14✔
85

86
/*
87
* Convert from ISO 8859-1 to UTF-8
88
*/
89
std::string latin1_to_utf8(const uint8_t chars[], size_t len) {
1,601✔
90
   std::string s;
1,601✔
91
   for(size_t i = 0; i != len; ++i) {
29,097✔
92
      const uint32_t c = static_cast<uint8_t>(chars[i]);
27,496✔
93
      append_utf8_for(s, c);
27,496✔
94
   }
95
   return s;
1,601✔
96
}
×
97

98
std::string format_char_for_display(char c) {
76✔
99
   std::ostringstream oss;
76✔
100

101
   oss << "'";
76✔
102

103
   if(c == '\t') {
76✔
104
      oss << "\\t";
14✔
105
   } else if(c == '\n') {
62✔
106
      oss << "\\n";
14✔
107
   } else if(c == '\r') {
48✔
108
      oss << "\\r";
14✔
109
   } else if(static_cast<unsigned char>(c) >= 128) {
34✔
110
      unsigned char z = static_cast<unsigned char>(c);
6✔
111
      oss << "\\x" << std::hex << std::uppercase << static_cast<int>(z);
6✔
112
   } else {
113
      oss << c;
28✔
114
   }
115

116
   oss << "'";
76✔
117

118
   return oss.str();
152✔
119
}
76✔
120

121
}  // namespace Botan
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

© 2026 Coveralls, Inc