replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.git] / wsutil / cfutils.c
1 /* cfutils.c
2  * Routines to work around deficiencies in Core Foundation, such as the
3  * lack of a routine to convert a CFString to a C string of arbitrary
4  * size.
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 2001 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #include "config.h"
14
15 #include <glib.h>
16 #include <CoreFoundation/CoreFoundation.h>
17 #include <wsutil/cfutils.h>
18
19 /*
20  * Convert a CFString to a UTF-8-encoded C string; the resulting string
21  * is allocated with g_malloc().  Returns NULL if the conversion fails.
22  */
23 char *
24 CFString_to_C_string(CFStringRef cfstring)
25 {
26         CFIndex string_len;
27         char *string;
28
29         string_len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstring),
30             kCFStringEncodingUTF8);
31         string = (char *)g_malloc(string_len + 1);
32         if (!CFStringGetCString(cfstring, string, string_len + 1,
33             kCFStringEncodingUTF8)) {
34                 g_free(string);
35                 return NULL;
36         }
37         return string;
38 }
39
40 /*
41  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
42  *
43  * Local variables:
44  * c-basic-offset: 8
45  * tab-width: 8
46  * indent-tabs-mode: t
47  * End:
48  *
49  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
50  * :indentSize=8:tabSize=8:noTabs=false:
51  */