• 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

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

50
}
51

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

56
   const size_t chars = len / 2;
1,172✔
57

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

64
   return s;
1,172✔
65
}
×
66

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

71
   const size_t chars = len / 4;
23✔
72

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

79
   return s;
9✔
80
}
14✔
81

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

94
std::string format_char_for_display(char c) {
76✔
95
   std::ostringstream oss;
76✔
96

97
   oss << "'";
76✔
98

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

112
   oss << "'";
76✔
113

114
   return oss.str();
152✔
115
}
76✔
116

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