| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
/* This gets logged as part of the (more verbose) protocol logging */ |
| 9 |
#ifdef G_LOG_DOMAIN |
| 10 |
#undef G_LOG_DOMAIN |
| 11 |
#endif |
| 12 |
#define G_LOG_DOMAIN "cockpit-protocol" |
| 13 |
|
| 14 |
#include "cockpitunicode.h" |
| 15 |
|
| 16 |
gboolean |
| 17 |
cockpit_unicode_has_incomplete_ending (GBytes *input) |
| 18 |
{ |
| 19 |
const gchar *data; |
| 20 |
const gchar *end; |
| 21 |
gsize length; |
| 22 |
|
| 23 |
data = g_bytes_get_data (input, &length); |
| 24 |
if (g_utf8_validate (data, length, &end)) |
| 25 |
return FALSE; |
| 26 |
|
| 27 |
do |
| 28 |
{ |
| 29 |
length -= (end - data) + 1; |
| 30 |
data = end + 1; |
| 31 |
} |
| 32 |
while (!g_utf8_validate (data, length, &end)); |
| 33 |
|
| 34 |
return length == 0; |
| 35 |
} |
| 36 |
|
| 37 |
GBytes * |
| 38 |
cockpit_unicode_force_utf8 (GBytes *input) |
| 39 |
{ |
| 40 |
const gchar *data; |
| 41 |
const gchar *end; |
| 42 |
gsize length; |
| 43 |
GString *string; |
| 44 |
|
| 45 |
data = g_bytes_get_data (input, &length); |
| 46 |
if (g_utf8_validate (data, length, &end)) |
| 47 |
return g_bytes_ref (input); |
| 48 |
|
| 49 |
string = g_string_sized_new (length + 16); |
| 50 |
do |
| 51 |
{ |
| 52 |
/* Valid part of the string */ |
| 53 |
g_string_append_len (string, data, end - data); |
| 54 |
|
| 55 |
/* Replacement character */ |
| 56 |
g_string_append (string, "\xef\xbf\xbd"); |
| 57 |
|
| 58 |
length -= (end - data) + 1; |
| 59 |
data = end + 1; |
| 60 |
} |
| 61 |
while (!g_utf8_validate (data, length, &end)); |
| 62 |
|
| 63 |
if (length) |
| 64 |
g_string_append_len (string, data, length); |
| 65 |
|
| 66 |
return g_string_free_to_bytes (string); |
| 67 |
} |
| 68 |
|