From 0ade5ffe2778e7b238bba8d979ca4d53dee1e702 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 31 Mar 2014 08:57:28 -0400 Subject: [PATCH] journal: fix export of messages containing newlines In "export" format, newlines are significant, and messages containing newlines must be exported as "binary". --- src/shared/logs-show.c | 7 ++++--- src/shared/utf8.c | 5 +++-- src/shared/utf8.h | 5 ++++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 9d14933bc..b0b66f64f 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -547,7 +547,9 @@ static int output_export( startswith(data, "_BOOT_ID=")) continue; - if (!utf8_is_printable(data, length)) { + if (utf8_is_printable_newline(data, length, false)) + fwrite(data, length, 1, f); + else { const char *c; uint64_t le64; @@ -562,8 +564,7 @@ static int output_export( le64 = htole64(length - (c - (const char*) data) - 1); fwrite(&le64, sizeof(le64), 1, f); fwrite(c + 1, length - (c - (const char*) data) - 1, 1, f); - } else - fwrite(data, length, 1, f); + } fputc('\n', f); } diff --git a/src/shared/utf8.c b/src/shared/utf8.c index 0b524d8a9..c559c1367 100644 --- a/src/shared/utf8.c +++ b/src/shared/utf8.c @@ -136,7 +136,7 @@ int utf8_encoded_to_unichar(const char *str) { return unichar; } -bool utf8_is_printable(const char* str, size_t length) { +bool utf8_is_printable_newline(const char* str, size_t length, bool newline) { const uint8_t *p; assert(str); @@ -145,7 +145,8 @@ bool utf8_is_printable(const char* str, size_t length) { int encoded_len = utf8_encoded_valid_unichar((const char *)p); int val = utf8_encoded_to_unichar((const char*)p); - if (encoded_len < 0 || val < 0 || is_unicode_control(val)) + if (encoded_len < 0 || val < 0 || is_unicode_control(val) || + (!newline && val == '\n')) return false; length -= encoded_len; diff --git a/src/shared/utf8.h b/src/shared/utf8.h index c0eb73a21..c08799593 100644 --- a/src/shared/utf8.h +++ b/src/shared/utf8.h @@ -31,7 +31,10 @@ const char *utf8_is_valid(const char *s) _pure_; char *ascii_is_valid(const char *s) _pure_; char *utf8_escape_invalid(const char *s); -bool utf8_is_printable(const char* str, size_t length) _pure_; +bool utf8_is_printable_newline(const char* str, size_t length, bool newline) _pure_; +_pure_ static inline bool utf8_is_printable(const char* str, size_t length) { + return utf8_is_printable_newline(str, length, true); +} char *utf16_to_utf8(const void *s, size_t length); -- 2.30.2