summaryrefslogtreecommitdiff
path: root/shared/nm-glib-aux/nm-json-aux.c
blob: db80642515b0926b7efe9cf0e06c50b8cbaac9b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: LGPL-2.1+
/*
 * Copyright 2019 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-json-aux.h"

/*****************************************************************************/

static void
_gstr_append_string_len (GString *gstr,
                         const char *str,
                         gsize len)
{
	g_string_append_c (gstr, '\"');

	while (len > 0) {
		gsize n;
		const char *end;
		gboolean valid;

		nm_assert (len > 0);

		valid = g_utf8_validate (str, len, &end);

		nm_assert (   end
		           && end >= str
		           && end <= &str[len]);

		if (end > str) {
			const char *s;

			for (s = str; s < end; s++) {
				nm_assert (s[0] != '\0');

				if (s[0] < 0x20) {
					const char *text;

					switch (s[0]) {
					case '\\': text = "\\\\"; break;
					case '\"': text = "\\\""; break;
					case '\b': text = "\\b";  break;
					case '\f': text = "\\f";  break;
					case '\n': text = "\\n";  break;
					case '\r': text = "\\r";  break;
					case '\t': text = "\\t";  break;
					default:
						g_string_append_printf (gstr, "\\u%04X", (guint) s[0]);
						continue;
					}
					g_string_append (gstr, text);
					continue;
				}

				if (NM_IN_SET (s[0], '\\', '\"'))
					g_string_append_c (gstr, '\\');
				g_string_append_c (gstr, s[0]);
			}
		} else
			nm_assert (!valid);

		if (valid) {
			nm_assert (end == &str[len]);
			break;
		}

		nm_assert (end < &str[len]);

		if (end[0] == '\0') {
			/* there is a NUL byte in the string. Technically this is valid UTF-8, so we
			 * encode it there. However, this will likely result in a truncated string when
			 * parsing. */
			g_string_append (gstr, "\\u0000");
		} else {
			/* the character is not valid UTF-8. There is nothing we can do about it, because
			 * JSON can only contain UTF-8 and even the escape sequences can only escape Unicode
			 * codepoints (but not binary).
			 *
			 * The argument is not a a string (in any known encoding), hence we cannot represent
			 * it as a JSON string (which are unicode strings).
			 *
			 * Print an underscore instead of the invalid char :) */
			g_string_append_c (gstr, '_');
		}

		n = str - end;
		nm_assert (n < len);
		n++;
		str += n;
		len -= n;
	}

	g_string_append_c (gstr, '\"');
}

void
nm_json_aux_gstr_append_string_len (GString *gstr,
                                    const char *str,
                                    gsize n)
{
	g_return_if_fail (gstr);

	_gstr_append_string_len (gstr, str, n);
}

void
nm_json_aux_gstr_append_string (GString *gstr,
                                const char *str)
{
	g_return_if_fail (gstr);

	if (!str)
		g_string_append (gstr, "null");
	else
		_gstr_append_string_len (gstr, str, strlen (str));
}

void
nm_json_aux_gstr_append_obj_name (GString *gstr,
                                  const char *key,
                                  char start_container)
{
	g_return_if_fail (gstr);
	g_return_if_fail (key);

	nm_json_aux_gstr_append_string (gstr, key);

	if (start_container != '\0') {
		nm_assert (NM_IN_SET (start_container, '[', '{'));
		g_string_append_printf (gstr, ": %c ", start_container);
	} else
		g_string_append (gstr, ": ");
}