syslog-ng source
utf8utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Balabit
3  * Copyright (c) 2015 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 #ifndef UTF8UTILS_H_INCLUDED
25 #define UTF8UTILS_H_INCLUDED
26 
27 #include "syslog-ng.h"
28 
29 #define AUTF8_UNSAFE_QUOTE 0x01
30 #define AUTF8_UNSAFE_APOSTROPHE 0x02
31 
32 void append_unsafe_utf8_as_escaped_binary(GString *escaped_string, const gchar *str,
33  gssize str_len, guint32 unsafe_flags);
34 gchar *convert_unsafe_utf8_to_escaped_binary(const gchar *str, gssize str_len,
35  guint32 unsafe_flags);
36 
37 void append_unsafe_utf8_as_escaped_text(GString *escaped_string, const gchar *str,
38  gssize str_len, guint32 unsafe_flags);
39 gchar *convert_unsafe_utf8_to_escaped_text(const gchar *str, gssize str_len,
40  guint32 unsafe_flags);
41 
42 
43 void append_unsafe_utf8_as_escaped(GString *escaped_output, const gchar *raw,
44  gssize raw_len, guint32 unsafe_flags,
45  const gchar *control_format,
46  const gchar *invalid_format);
47 
48 
49 /* for performance-critical use only */
50 
51 #define SANITIZE_UTF8_BUFFER_SIZE(l) (l * 6 + 1)
52 
53 static inline const gchar *
54 optimized_sanitize_utf8_to_escaped_binary(const guchar *data, gint length, gsize *new_length, gchar *out,
55  gsize out_size)
56 {
57  GString sanitized_message;
58 
59  /* avoid GString allocation */
60  sanitized_message.str = out;
61  sanitized_message.len = 0;
62  sanitized_message.allocated_len = out_size;
63 
64  append_unsafe_utf8_as_escaped_binary(&sanitized_message, (const gchar *) data, length, 0);
65 
66  /* MUST NEVER BE REALLOCATED */
67  g_assert(sanitized_message.str == out);
68 
69  *new_length = sanitized_message.len;
70  return out;
71 }
72 
73 #endif
void append_unsafe_utf8_as_escaped_text(GString *escaped_string, const gchar *str, gssize str_len, guint32 unsafe_flags)
Definition: utf8utils.c:264
gchar * convert_unsafe_utf8_to_escaped_binary(const gchar *str, gssize str_len, guint32 unsafe_flags)
Definition: utf8utils.c:231
gchar * convert_unsafe_utf8_to_escaped_text(const gchar *str, gssize str_len, guint32 unsafe_flags)
Definition: utf8utils.c:272
void append_unsafe_utf8_as_escaped_binary(GString *escaped_string, const gchar *str, gssize str_len, guint32 unsafe_flags)
Definition: utf8utils.c:223
void append_unsafe_utf8_as_escaped(GString *escaped_output, const gchar *raw, gssize raw_len, guint32 unsafe_flags, const gchar *control_format, const gchar *invalid_format)
Definition: utf8utils.c:189