From 30619416b92f7326024a9696f6630e9d63f82032 Mon Sep 17 00:00:00 2001 From: Gerald Combs Date: Fri, 22 Sep 2006 22:34:54 +0000 Subject: [PATCH] Add missing files from last commit. Fix an #include. svn path=/trunk/; revision=19293 --- epan/filesystem.c | 4 +- epan/unicode-utils.c | 128 +++++++++++++++++++++++++++++++++++++++++++ epan/unicode-utils.h | 54 ++++++++++++++++++ 3 files changed, 184 insertions(+), 2 deletions(-) create mode 100755 epan/unicode-utils.c create mode 100755 epan/unicode-utils.h diff --git a/epan/filesystem.c b/epan/filesystem.c index 14ce111faf..436d722d7d 100644 --- a/epan/filesystem.c +++ b/epan/filesystem.c @@ -44,7 +44,7 @@ #ifdef _WIN32 #include #include -#include "epan/strutil.h" +#include "epan/unicode-utils.h" #else #include #endif @@ -654,7 +654,7 @@ get_persconffile_dir(void) /* * See if we are running in a U3 environment. */ - u3appdatapath = getenv_utf8("U3_APP_DATA_PATH"); + u3appdatapath = getenv_utf8("U3_APP_DATA_PATH"); if (u3appdatapath != NULL) { /* * We are; use the U3 application data path. diff --git a/epan/unicode-utils.c b/epan/unicode-utils.c new file mode 100755 index 0000000000..e8782d7727 --- /dev/null +++ b/epan/unicode-utils.c @@ -0,0 +1,128 @@ +/* unicode-utils.c + * Unicode utility routines + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 2006 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifdef _WIN32 + +#include +#include "unicode-utils.h" + +#include +#include +#include + +/** @file + * Unicode utilities (internal interface) + * + * We define UNICODE and _UNICODE under Windows. This means that + * Windows SDK routines expect UTF-16 strings, in contrast to newer + * versions of Glib and GTK+ which expect UTF-8. This module provides + * convenience routines for converting between UTF-8 and UTF-16. + */ + +#define INITIAL_UTFBUF_SIZE 128 + +/* + * XXX - Should we use g_utf8_to_utf16() and g_utf16_to_utf8() + * instead? The goal of the functions below was to provide simple + * wrappers for UTF-8 <-> UTF-16 conversion without making the + * caller worry about freeing up memory afterward. + */ + +/* Convert from UTF-8 to UTF-16. */ +wchar_t * utf_8to16(const char *utf8str) { + static wchar_t *utf16buf[3]; + static int utf16buf_len[3]; + static int idx; + + if (utf8str == NULL) + return NULL; + + idx = (idx + 1) % 3; + + /* + * Allocate the buffer if it's not already allocated. + */ + if (utf16buf[idx] == NULL) { + utf16buf_len[idx] = INITIAL_UTFBUF_SIZE; + utf16buf[idx] = g_malloc(utf16buf_len[idx] * sizeof(wchar_t)); + } + + while (MultiByteToWideChar(CP_UTF8, 0, utf8str, + -1, NULL, 0) >= utf16buf_len[idx]) { + /* + * Double the buffer's size if it's not big enough. + * The size of the buffer starts at 128, so doubling its size + * adds at least another 128 bytes, which is more than enough + * for one more character plus a terminating '\0'. + */ + utf16buf_len[idx] *= 2; + utf16buf[idx] = g_realloc(utf16buf[idx], utf16buf_len[idx] * sizeof(wchar_t)); + } + + if (MultiByteToWideChar(CP_UTF8, 0, utf8str, + -1, utf16buf[idx], utf16buf_len[idx]) == 0) + return NULL; + + return utf16buf[idx]; +} + +/* Convert from UTF-16 to UTF-8. */ +gchar * utf_16to8(const wchar_t *utf16str) { + static gchar *utf8buf[3]; + static int utf8buf_len[3]; + static int idx; + + if (utf16str == NULL) + return NULL; + + idx = (idx + 1) % 3; + + /* + * Allocate the buffer if it's not already allocated. + */ + if (utf8buf[idx] == NULL) { + utf8buf_len[idx] = INITIAL_UTFBUF_SIZE; + utf8buf[idx] = g_malloc(utf8buf_len[idx]); + } + + while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1, + NULL, 0, NULL, NULL) >= utf8buf_len[idx]) { + /* + * Double the buffer's size if it's not big enough. + * The size of the buffer starts at 128, so doubling its size + * adds at least another 128 bytes, which is more than enough + * for one more character plus a terminating '\0'. + */ + utf8buf_len[idx] *= 2; + utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]); + } + + if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1, + utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0) + return NULL; + + return utf8buf[idx]; +} + +#endif diff --git a/epan/unicode-utils.h b/epan/unicode-utils.h new file mode 100755 index 0000000000..2a08be71af --- /dev/null +++ b/epan/unicode-utils.h @@ -0,0 +1,54 @@ +/* unicode-utils.h + * Unicode utility definitions + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 2006 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __UNICODEUTIL_H__ +#define __UNICODEUTIL_H__ + +#ifdef _WIN32 + +/** + * @file Unicode convenience routines. + */ + +/** Given a UTF-8 string, convert it to UTF-16. This is meant to be used + * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16). + * + * @param utf8str The string to convert. May be NULL. + * @return The string converted to UTF-16. If utf8str is NULL, returns + * NULL. The return value should NOT be freed by the caller. + */ +wchar_t * utf_8to16(const char *utf8str); + +/** Given a UTF-16 string, convert it to UTF-8. This is meant to be used + * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16). + * + * @param utf16str The string to convert. May be NULL. + * @return The string converted to UTF-8. If utf16str is NULL, returns + * NULL. The return value should NOT be freed by the caller. + */ +gchar * utf_16to8(const wchar_t *utf16str); + +#endif /* _WIN32 */ + +#endif /* __UNICODEUTIL_H__ */