Skip to content

Commit 3e73cb0

Browse files
committed
[lib][uefi] Use utf16 charset functions
1 parent 369e97d commit 3e73cb0

File tree

5 files changed

+15
-19
lines changed

5 files changed

+15
-19
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ ColumnLimit: 0
1111
IndentCaseLabels: true
1212
IndentGotoLabels: false
1313
AlignConsecutiveMacros:
14-
Enabled: true
14+
Enabled: true

lib/uefi/.clang-format

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Language: Cpp
3+
IndentWidth: 2
4+

lib/uefi/charset.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ int utf16_strcmp(const char16_t *s1, const char16_t *s2) {
4646
* This function converts utf-8 string to utf-16 string. However
4747
* it only supports us-ascii input right now.
4848
*/
49-
int utf8_to_utf16(char16_t *dest, const char *src, size_t size) {
49+
size_t utf8_to_utf16(char16_t *dest, const char *src, size_t size) {
5050
size_t i = 0;
5151
for (; i < size - 1 && *src; i++, src++) {
5252
dest[i] = *src;
5353
}
5454

5555
dest[i] = 0;
56-
return 0;
56+
return i;
5757
}
5858

5959
/**
@@ -62,12 +62,12 @@ int utf8_to_utf16(char16_t *dest, const char *src, size_t size) {
6262
* This function converts utf-16 string to utf-8 string. However
6363
* it only supports us-ascii output right now.
6464
*/
65-
int utf16_to_utf8(char *dest, const char16_t *src, size_t size) {
65+
size_t utf16_to_utf8(char *dest, const char16_t *src, size_t size) {
6666
size_t i = 0;
6767
for (; i < size - 1 && *src; i++, src++) {
6868
dest[i] = *src;
6969
}
7070

7171
dest[i] = 0;
72-
return 0;
72+
return i;
7373
}

lib/uefi/charset.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
size_t utf16_strlen(const char16_t *str);
2424
int utf16_strcmp(const char16_t *s1, const char16_t *s2);
25-
int utf8_to_utf16(char16_t *dest, const char *src, size_t size);
26-
int utf16_to_utf8(char *dest, const char16_t *src, size_t size);
25+
// Return number of characters converted, including the null terminator
26+
size_t utf8_to_utf16(char16_t *dest, const char *src, size_t size);
27+
size_t utf16_to_utf8(char *dest, const char16_t *src, size_t size);
2728

2829
#endif

lib/uefi/runtime_service_provider.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
#include <string.h>
2323
#include <uefi/types.h>
2424

25+
#include "charset.h"
2526
#include "variable_mem.h"
2627

2728
namespace {
2829

29-
constexpr auto &&kSecureBoot = "SecureBoot";
30-
3130
EFI_STATUS GetVariable(char16_t *VariableName, EfiGuid *VendorGuid,
3231
uint32_t *Attributes, size_t *DataSize, void *Data) {
3332

@@ -36,16 +35,8 @@ EFI_STATUS GetVariable(char16_t *VariableName, EfiGuid *VendorGuid,
3635
}
3736

3837
char buffer[512];
39-
size_t i = 0;
40-
while (VariableName[i] && i < sizeof(buffer)) {
41-
size_t j = 0;
42-
for (j = 0; j < sizeof(buffer) - 1 && VariableName[i + j]; j++) {
43-
buffer[j] = VariableName[i + j];
44-
}
45-
i += j;
46-
}
47-
buffer[i] = 0;
48-
if (strncmp(buffer, kSecureBoot, sizeof(kSecureBoot)) == 0 || strcmp(buffer, "SetupMode") == 0) {
38+
utf16_to_utf8(buffer, VariableName, sizeof(buffer));
39+
if (utf16_strcmp(VariableName, u"SecureBoot") == 0 && utf16_strcmp(VariableName, u"SetupMode") == 0) {
4940
if (DataSize) {
5041
*DataSize = 1;
5142
}

0 commit comments

Comments
 (0)