From X via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8914 :
[metze/wireshark/wip.git] / 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  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 2001 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #ifdef HAVE_OS_X_FRAMEWORKS
30 #include <glib.h>
31 #include <CoreFoundation/CoreFoundation.h>
32 #include "cfutils.h"
33
34 /*
35  * Convert a CFString to a UTF-8-encoded C string; the resulting string
36  * is allocated with g_malloc().  Returns NULL if the conversion fails.
37  */
38 char *
39 CFString_to_C_string(CFStringRef cfstring)
40 {
41         CFIndex string_len;
42         char *string;
43
44         string_len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstring),
45             kCFStringEncodingUTF8);
46         string = (char *)g_malloc(string_len + 1);
47         if (!CFStringGetCString(cfstring, string, string_len + 1,
48             kCFStringEncodingUTF8)) {
49                 g_free(string);
50                 return NULL;
51         }
52         return string;
53 }
54 #endif
55
56