Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip heading UTF-8 encoded BOM #394

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compose/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ parse(struct xkb_compose_table *table, struct scanner *s,
production.mods = 0;
production.modmask = 0;

/* Skip UTF-8 encoded BOM (U+FEFF) */
/* See: https://www.unicode.org/faq/utf_bom.html#bom5 */
scanner_str(s, "\xef\xbb\xbf", 3);

/* fallthrough */

initial_eol:
Expand Down
4 changes: 4 additions & 0 deletions src/xkbcomp/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,10 @@ read_rules_file(struct xkb_context *ctx,

scanner_init(&scanner, matcher->ctx, string, size, path, NULL);

/* Skip UTF-8 encoded BOM (U+FEFF) */
/* See: https://www.unicode.org/faq/utf_bom.html#bom5 */
scanner_str(&scanner, "\xef\xbb\xbf", 3);

ret = matcher_match(matcher, &scanner, include_depth, string, size, path);

unmap_file(string, size);
Expand Down
5 changes: 5 additions & 0 deletions src/xkbcomp/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
{
struct scanner scanner;
scanner_init(&scanner, ctx, string, len, file_name, NULL);

/* Skip UTF-8 encoded BOM (U+FEFF) */
/* See: https://www.unicode.org/faq/utf_bom.html#bom5 */
scanner_str(&scanner, "\xef\xbb\xbf", 3);

return parse(ctx, &scanner, map);
}

Expand Down
12 changes: 12 additions & 0 deletions test/buffercomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ main(int argc, char *argv[])
keymap = test_compile_buffer(ctx, "", 0);
assert(!keymap);

/* Accept UTF-8 encoded BOM (U+FEFF) */
const char *bom =
"\xef\xbb\xbfxkb_keymap {"
" xkb_keycodes { include \"evdev\" };"
" xkb_types { include \"complete\" };"
" xkb_compat { include \"complete\" };"
" xkb_symbols { include \"pc\" };"
"};";
keymap = test_compile_buffer(ctx, bom, strlen(bom));
assert(keymap);
xkb_keymap_unref(keymap);

/* Make sure we can recompile our output for a normal keymap from rules. */
keymap = test_compile_rules(ctx, NULL, NULL,
"ru,ca,de,us", ",multix,neo,intl", NULL);
Expand Down
11 changes: 11 additions & 0 deletions test/compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ test_compose_seq_buffer(struct xkb_context *ctx, const char *buffer, ...)
return ok;
}

static void
test_compose_utf8_bom(struct xkb_context *ctx)
{
const char *buffer = "\xef\xbb\xbf<A> : X";
assert(test_compose_seq_buffer(ctx, buffer,
XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "X", XKB_KEY_X,
XKB_KEY_NoSymbol));
}


static void
test_seqs(struct xkb_context *ctx)
{
Expand Down Expand Up @@ -723,6 +733,7 @@ main(int argc, char *argv[])
unsetenv("XLOCALEDIR");
#endif

test_compose_utf8_bom(ctx);
test_seqs(ctx);
test_conflicting(ctx);
test_XCOMPOSEFILE(ctx);
Expand Down
22 changes: 22 additions & 0 deletions test/data/rules/utf-8_with_bom
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// NOTE: this file is encoded in UTF-8 with a leading BOM (U+FEFF)
! model = keycodes
my_model = my_keycodes
* = default_keycodes

! layout variant = symbols
my_layout my_variant = my_symbols+extra_variant

! layout = symbols
my_layout = my_symbols
* = default_symbols

! model = types
my_model = my_types
* = default_types

! model = compat
my_model = my_compat
* = default_compat

! option = compat
my_option = |some:compat
12 changes: 12 additions & 0 deletions test/rules-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ main(int argc, char *argv[])
ctx = test_get_context(0);
assert(ctx);

struct test_data test_utf_8_with_bom = {
.rules = "utf-8_with_bom",

.model = "my_model", .layout = "my_layout", .variant = "my_variant",
.options = "my_option",

.keycodes = "my_keycodes", .types = "my_types",
.compat = "my_compat|some:compat",
.symbols = "my_symbols+extra_variant",
};
assert(test_rules(ctx, &test_utf_8_with_bom));

struct test_data test1 = {
.rules = "simple",

Expand Down