Skip to content

Commit a13a471

Browse files
committed
fix: support utf8 on windows
1 parent 354fa81 commit a13a471

File tree

8 files changed

+264
-24
lines changed

8 files changed

+264
-24
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,9 @@ else(STDLIBS_HAVE_GETRPCBYNUMBER)
652652
endif(STDLIBS_HAVE_GETRPCBYNUMBER)
653653
cmake_pop_check_state()
654654

655+
656+
check_function_exists(wcwidth HAVE_WCWIDTH)
657+
655658
#
656659
# This requires the libraries we require, as ether_ntohost might be
657660
# in one of those libraries. That means we have to do this after
@@ -1443,7 +1446,7 @@ set(NETDISSECT_SOURCE_LIST_C
14431446
#
14441447
# Replace missing functions
14451448
#
1446-
foreach(FUNC strlcat strlcpy strsep getservent getopt_long)
1449+
foreach(FUNC strlcat strlcpy strsep getservent getopt_long wcwidth)
14471450
string(TOUPPER ${FUNC} FUNC_UPPERCASE)
14481451
set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
14491452
if(NOT ${HAVE_FUNC_UPPERCASE})

Makefile.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ EXTRA_DIST = \
363363
missing/strlcat.c \
364364
missing/strlcpy.c \
365365
missing/strsep.c \
366+
missing/wcwidth.c \
367+
missing/wcwidth.h \
366368
mkdep \
367369
packetdat.awk \
368370
print-smb.c \
@@ -396,6 +398,8 @@ strlcpy.o: $(srcdir)/missing/strlcpy.c
396398
$(CC) $(FULL_CFLAGS) -o $@ -c $(srcdir)/missing/strlcpy.c
397399
strsep.o: $(srcdir)/missing/strsep.c
398400
$(CC) $(FULL_CFLAGS) -o $@ -c $(srcdir)/missing/strsep.c
401+
wcwidth.o: $(srcdir)/missing/wcwidth.c
402+
$(CC) $(FULL_CFLAGS) -o $@ -c $(srcdir)/missing/wcwidth.c
399403

400404
install: all
401405
[ -d $(DESTDIR)$(bindir) ] || \

cmakeconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
/* Define to 1 if you have the `wsockinit' function. */
127127
#cmakedefine HAVE_WSOCKINIT 1
128128

129+
/* Define to 1 if you have the `wcwidth' function. */
130+
#cmakedefine HAVE_WCWIDTH 1
131+
129132
/* define if libpcap has yydebug */
130133
#cmakedefine HAVE_YYDEBUG 1
131134

configure.ac

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ AC_LBL_C_INIT(V_CCOPT, V_INCLS)
4545
#
4646
AC_CHECK_HEADERS(rpc/rpc.h rpc/rpcent.h)
4747

48-
#
49-
# Check for wide character support for UTF-8 handling
50-
#
51-
AC_CHECK_HEADERS(wchar.h)
52-
AC_CHECK_FUNCS(wcwidth)
53-
5448
# On Linux, if Autoconf version >= 2.72 and GNU C Library version >= 2.34,
5549
# uncomment AC_SYS_YEAR2038_RECOMMENDED to ensure time_t is Y2038-safe.
5650
# (Can be done by autogen.sh)
@@ -324,7 +318,7 @@ fi
324318
#
325319
AC_LBL_LIBRARY_NET
326320

327-
AC_REPLACE_FUNCS(strlcat strlcpy strsep getservent getopt_long)
321+
AC_REPLACE_FUNCS(strlcat strlcpy strsep getservent getopt_long wcwidth)
328322
AC_CHECK_FUNCS(fork vfork)
329323

330324
#

missing/wcwidth.c

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* The following wcwidth implementation is based on Markus Kuhn's
3+
* implementation and adapted.
4+
*
5+
* This is an implementation of wcwidth() and wcswidth() (defined in
6+
* IEEE Std 1002.1-2001) for Unicode.
7+
*
8+
* http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
9+
* http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
10+
*
11+
* In fixed-width output devices, Latin characters all occupy a single
12+
* "cell" position of equal width, whereas ideographic CJK characters
13+
* occupy two such cells. Interoperability between terminal-line
14+
* applications and (teletype-style) character terminals using the
15+
* UTF-8 encoding requires agreement on which character should advance
16+
* the cursor by how many cell positions. No established formal
17+
* standards exist at present on which Unicode character shall occupy
18+
* how many cell positions on character terminals. These routines are
19+
* a first attempt of defining such behavior based on simple rules
20+
* applied to data provided by the Unicode Consortium.
21+
*
22+
* For some graphical characters, the Unicode standard explicitly
23+
* defines a character-cell width via the definition of the East Asian
24+
* FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
25+
* In all these cases, there is no ambiguity about which width a
26+
* terminal shall use. For characters in the East Asian Ambiguous (A)
27+
* class, the width choice depends purely on a preference of backward
28+
* compatibility with either historic CJK or Western practice.
29+
* Choosing single-width for these characters is easy to justify as
30+
* the appropriate long-term solution, as the CJK practice of
31+
* displaying these characters as double-width comes from historic
32+
* implementation simplicity (8-bit encoded characters were displayed
33+
* single-width and 16-bit ones double-width, even for Greek,
34+
* Cyrillic, etc.) and not any typographic considerations.
35+
*
36+
* Much less clear is the choice of width for the Not East Asian
37+
* (Neutral) class. Existing practice does not dictate a width for any
38+
* of these characters. It would nevertheless make sense
39+
* typographically to allocate two character cells to characters such
40+
* as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
41+
* represented adequately with a single-width glyph. The following
42+
* routines at present merely assign a single-cell width to all
43+
* neutral characters, in the interest of simplicity. This is not
44+
* entirely satisfactory and should be reconsidered before
45+
* establishing a formal standard in this area. At the moment, the
46+
* decision which Not East Asian (Neutral) characters should be
47+
* represented by double-width glyphs cannot yet be answered by
48+
* applying a simple rule from the Unicode database content. Setting
49+
* up a proper standard for the behavior of UTF-8 character terminals
50+
* will require a careful analysis not only of each Unicode character,
51+
* but also of each presentation form, something the author of these
52+
* routines has avoided to do so far.
53+
*
54+
* http://www.unicode.org/unicode/reports/tr11/
55+
*
56+
* Markus Kuhn -- 2007-05-26 (Unicode 5.0)
57+
*
58+
* Permission to use, copy, modify, and distribute this software
59+
* for any purpose and without fee is hereby granted. The author
60+
* disclaims all warranties with regard to this software.
61+
*
62+
* Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
63+
*/
64+
65+
#include <wchar.h>
66+
67+
struct interval {
68+
int first;
69+
int last;
70+
};
71+
72+
/* auxiliary function for binary search in interval table */
73+
static int bisearch(wchar_t ucs, const struct interval *table, int max) {
74+
int min = 0;
75+
int mid;
76+
77+
if (ucs < table[0].first || ucs > table[max].last)
78+
return 0;
79+
while (max >= min) {
80+
mid = (min + max) / 2;
81+
if (ucs > table[mid].last)
82+
min = mid + 1;
83+
else if (ucs < table[mid].first)
84+
max = mid - 1;
85+
else
86+
return 1;
87+
}
88+
89+
return 0;
90+
}
91+
92+
93+
/* The following two functions define the column width of an ISO 10646
94+
* character as follows:
95+
*
96+
* - The null character (U+0000) has a column width of 0.
97+
*
98+
* - Other C0/C1 control characters and DEL will lead to a return
99+
* value of -1.
100+
*
101+
* - Non-spacing and enclosing combining characters (general
102+
* category code Mn or Me in the Unicode database) have a
103+
* column width of 0.
104+
*
105+
* - SOFT HYPHEN (U+00AD) has a column width of 1.
106+
*
107+
* - Other format characters (general category code Cf in the Unicode
108+
* database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
109+
*
110+
* - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
111+
* have a column width of 0.
112+
*
113+
* - Spacing characters in the East Asian Wide (W) or East Asian
114+
* Full-width (F) category as defined in Unicode Technical
115+
* Report #11 have a column width of 2.
116+
*
117+
* - All remaining characters (including all printable
118+
* ISO 8859-1 and WGL4 characters, Unicode control characters,
119+
* etc.) have a column width of 1.
120+
*
121+
* This implementation assumes that wchar_t characters are encoded
122+
* in ISO 10646.
123+
*/
124+
125+
int wcwidth(wchar_t ucs)
126+
{
127+
/* sorted list of non-overlapping intervals of non-spacing characters */
128+
/* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
129+
static const struct interval combining[] = {
130+
{ 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 },
131+
{ 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
132+
{ 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 },
133+
{ 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 },
134+
{ 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
135+
{ 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
136+
{ 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 },
137+
{ 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D },
138+
{ 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 },
139+
{ 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD },
140+
{ 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C },
141+
{ 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D },
142+
{ 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC },
143+
{ 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD },
144+
{ 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
145+
{ 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
146+
{ 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
147+
{ 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
148+
{ 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC },
149+
{ 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
150+
{ 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D },
151+
{ 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 },
152+
{ 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E },
153+
{ 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC },
154+
{ 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 },
155+
{ 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E },
156+
{ 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 },
157+
{ 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 },
158+
{ 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
159+
{ 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F },
160+
{ 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 },
161+
{ 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD },
162+
{ 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD },
163+
{ 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 },
164+
{ 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B },
165+
{ 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 },
166+
{ 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 },
167+
{ 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF },
168+
{ 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 },
169+
{ 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F },
170+
{ 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B },
171+
{ 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F },
172+
{ 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB },
173+
{ 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F },
174+
{ 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 },
175+
{ 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD },
176+
{ 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F },
177+
{ 0xE0100, 0xE01EF }
178+
};
179+
180+
/* test for 8-bit control characters */
181+
if (ucs == 0)
182+
return 0;
183+
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
184+
return -1;
185+
186+
/* binary search in table of non-spacing characters */
187+
if (bisearch(ucs, combining,
188+
sizeof(combining) / sizeof(struct interval) - 1))
189+
return 0;
190+
191+
/* if we arrive here, ucs is not a combining or C0/C1 control character */
192+
193+
return 1 +
194+
(ucs >= 0x1100 &&
195+
(ucs <= 0x115f || /* Hangul Jamo init. consonants */
196+
ucs == 0x2329 || ucs == 0x232a ||
197+
(ucs >= 0x2e80 && ucs <= 0xa4cf &&
198+
ucs != 0x303f) || /* CJK ... Yi */
199+
(ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
200+
(ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
201+
(ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */
202+
(ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
203+
(ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */
204+
(ucs >= 0xffe0 && ucs <= 0xffe6) ||
205+
(ucs >= 0x20000 && ucs <= 0x2fffd) ||
206+
(ucs >= 0x30000 && ucs <= 0x3fffd)));
207+
}

missing/wcwidth.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* wcwidth() implementation for Windows
3+
* Based on Markus Kuhn's public domain implementation
4+
*/
5+
6+
#ifndef WCWIDTH_H
7+
#define WCWIDTH_H
8+
9+
#include <wchar.h>
10+
11+
/* Determine the column width of a wide character */
12+
int wcwidth(wchar_t ucs);
13+
14+
#endif /* WCWIDTH_H */

print-ascii.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@
4444

4545
#include <stdio.h>
4646

47-
#ifdef HAVE_WCHAR_H
4847
#include <wchar.h>
4948
#include <wctype.h>
49+
50+
#ifndef HAVE_WCWIDTH
51+
#include "missing/wcwidth.h"
5052
#endif
5153

5254
#include "netdissect-ctype.h"
@@ -61,7 +63,6 @@
6163
#define HEXDUMP_HEXSTUFF_PER_LINE \
6264
(HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
6365

64-
#ifdef HAVE_WCHAR_H
6566

6667
/*
6768
* The blow is_utf8_printable is taken from ngrep
@@ -124,7 +125,6 @@ static u_int is_utf8_printable(const unsigned char *s, size_t max_len, int *widt
124125

125126
return 0;
126127
}
127-
#endif
128128

129129
void
130130
ascii_print(netdissect_options *ndo,
@@ -146,13 +146,7 @@ ascii_print(netdissect_options *ndo,
146146
int utf8_len;
147147
int j;
148148

149-
utf8_len = 0;
150-
151-
#ifdef HAVE_WCHAR_H
152-
if (ndo->ndo_utf8) {
153-
utf8_len = is_utf8_printable(cp, length, NULL);
154-
}
155-
#endif
149+
utf8_len = ndo->ndo_utf8 ? is_utf8_printable(cp, length, NULL) : 0;
156150

157151
if (utf8_len > 0) {
158152
/* Valid printable UTF-8 character */
@@ -230,9 +224,7 @@ hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *indent,
230224
utf8_bytes_to_skip--;
231225
} else {
232226
// try to add the display (utf8) chars
233-
#ifdef HAVE_WCHAR_H
234227
utf8_bytes_to_skip = ndo->ndo_utf8 ? is_utf8_printable(cp, length, NULL) : 0;
235-
#endif
236228
if (utf8_bytes_to_skip > 0) {
237229
u_int j;
238230
for (j=0; j<utf8_bytes_to_skip; j++) {

tcpdump.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,7 @@ static const struct option longopts[] = {
731731
#define IMMEDIATE_MODE_USAGE ""
732732
#endif
733733

734-
#ifdef HAVE_WCHAR_H
735734
#define DISPLAY_UTF8_USAGE "[ --utf8 ]"
736-
#else
737-
#define DISPLAY_UTF8_USAGE
738-
#endif
739735

740736
#ifndef _WIN32
741737
/* Drop root privileges and chroot if necessary */
@@ -1642,6 +1638,7 @@ main(int argc, char **argv)
16421638

16431639
setlocale(LC_CTYPE, "");
16441640

1641+
16451642
cnt = -1;
16461643
device = NULL;
16471644
infile = NULL;
@@ -2140,6 +2137,32 @@ main(int argc, char **argv)
21402137
/* NOTREACHED */
21412138
}
21422139

2140+
2141+
if (ndo->ndo_utf8) {
2142+
2143+
#if defined(_WIN32)
2144+
/* On Windows, explicitly set UTF-8 locale and console code page for mbrtowc() to work */
2145+
/* Try multiple locale formats for compatibility across Windows versions */
2146+
if (setlocale(LC_CTYPE, ".UTF-8") == NULL) {
2147+
if (setlocale(LC_CTYPE, ".UTF8") == NULL) {
2148+
if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL) {
2149+
setlocale(LC_CTYPE, "C.UTF-8");
2150+
}
2151+
}
2152+
}
2153+
/* Also set console code page to UTF-8 (65001) */
2154+
SetConsoleOutputCP(65001);
2155+
SetConsoleCP(65001);
2156+
#else
2157+
char const *locale = getenv("LANG");
2158+
if (locale == NULL)
2159+
locale = "en_US";
2160+
2161+
setlocale(LC_CTYPE, locale);
2162+
#endif
2163+
2164+
}
2165+
21432166
if (ndo->ndo_Aflag && ndo->ndo_xflag)
21442167
error("-A and -x[x] are mutually exclusive.");
21452168
if (ndo->ndo_Aflag && ndo->ndo_Xflag)

0 commit comments

Comments
 (0)