Browse code

cleanup: windows: convert argv (UCS-2 to UTF-8) at earliest

Discussed at [1].

Use wmain under windows, drop the custom parsing and shell32 linkage.

There is no need for gc magic as this allocation is static.

[1] http://permalink.gmane.org/gmane.network.openvpn.devel/5433

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
Message-Id: 1332621070-28464-1-git-send-email-alon.barlev@gmail.com
URL: http://article.gmane.org/gmane.network.openvpn.devel/6063
Tested-by: Samuli Seppänen <samuli@openvpn.net>
Signed-off-by: David Sommerseth <davids@redhat.com>

Alon Bar-Lev authored on 2012/03/25 05:31:10
Showing 4 changed files
... ...
@@ -27,6 +27,10 @@ AM_CFLAGS = \
27 27
 	$(OPTIONAL_CRYPTO_CFLAGS) \
28 28
 	$(OPTIONAL_LZO_CFLAGS) \
29 29
 	$(OPTIONAL_PKCS11_HELPER_CFLAGS)
30
+if WIN32
31
+# we want unicode entry point but not the macro
32
+AM_CFLAGS += -municode -UUNICODE
33
+endif
30 34
 
31 35
 sbin_PROGRAMS = openvpn
32 36
 
... ...
@@ -118,5 +122,5 @@ openvpn_LDADD = \
118 118
 	$(OPTIONAL_DL_LIBS)
119 119
 if WIN32
120 120
 openvpn_SOURCES += openvpn_win32_resources.rc
121
-openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lshell32
121
+openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm
122 122
 endif
... ...
@@ -127,8 +127,9 @@ tunnel_point_to_point (struct context *c)
127 127
  * @param argc - Commandline argument count.
128 128
  * @param argv - Commandline argument values.
129 129
  */
130
+static
130 131
 int
131
-main (int argc, char *argv[])
132
+openvpn_main (int argc, char *argv[])
132 133
 {
133 134
   struct context c;
134 135
 
... ...
@@ -289,3 +290,37 @@ main (int argc, char *argv[])
289 289
   openvpn_exit (OPENVPN_EXIT_STATUS_GOOD);  /* exit point */
290 290
   return 0;			            /* NOTREACHED */
291 291
 }
292
+
293
+#ifdef WIN32
294
+int
295
+wmain (int argc, wchar_t *wargv[]) {
296
+  char **argv;
297
+  int ret;
298
+  int i;
299
+
300
+  if ((argv = calloc(argc+1, sizeof(char*))) == NULL)
301
+    return 1;
302
+
303
+  for (i = 0; i < argc; i++)
304
+    {
305
+      int n = WideCharToMultiByte (CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
306
+      argv[i] = malloc (n);
307
+      WideCharToMultiByte (CP_UTF8, 0, wargv[i], -1, argv[i], n, NULL, NULL);
308
+    }
309
+
310
+  ret = openvpn_main(argc, argv);
311
+
312
+  for (i=0; i < argc; i++ )
313
+    {
314
+      free (argv[i]);
315
+    }
316
+  free(argv);
317
+
318
+  return ret;
319
+}
320
+#else
321
+int
322
+main (int argc, char *argv[]) {
323
+	return openvpn_main(argc, argv);
324
+}
325
+#endif
292 326
old mode 100644
293 327
new mode 100755
... ...
@@ -18,12 +18,12 @@
18 18
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
19 19
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
20 20
     <ConfigurationType>Application</ConfigurationType>
21
-    <CharacterSet>MultiByte</CharacterSet>
22 21
     <WholeProgramOptimization>true</WholeProgramOptimization>
22
+    <CharacterSet>Unicode</CharacterSet>
23 23
   </PropertyGroup>
24 24
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
25 25
     <ConfigurationType>Application</ConfigurationType>
26
-    <CharacterSet>MultiByte</CharacterSet>
26
+    <CharacterSet>Unicode</CharacterSet>
27 27
   </PropertyGroup>
28 28
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
29 29
   <ImportGroup Label="ExtensionSettings">
... ...
@@ -56,12 +56,13 @@
56 56
       </PrecompiledHeader>
57 57
       <WarningLevel>Level3</WarningLevel>
58 58
       <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
59
+      <UndefinePreprocessorDefinitions>UNICODE</UndefinePreprocessorDefinitions>
59 60
     </ClCompile>
60 61
     <ResourceCompile>
61 62
       <AdditionalIncludeDirectories>$(SOURCEBASE);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
62 63
     </ResourceCompile>
63 64
     <Link>
64
-      <AdditionalDependencies>libeay32.lib;ssleay32.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;shell32.lib;%(AdditionalDependencies)</AdditionalDependencies>
65
+      <AdditionalDependencies>libeay32.lib;ssleay32.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
65 66
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
66 67
       <GenerateDebugInformation>true</GenerateDebugInformation>
67 68
       <SubSystem>Console</SubSystem>
... ...
@@ -80,12 +81,13 @@
80 80
       </PrecompiledHeader>
81 81
       <WarningLevel>Level3</WarningLevel>
82 82
       <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
83
+      <UndefinePreprocessorDefinitions>UNICODE</UndefinePreprocessorDefinitions>
83 84
     </ClCompile>
84 85
     <ResourceCompile>
85 86
       <AdditionalIncludeDirectories>$(SOURCEBASE);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
86 87
     </ResourceCompile>
87 88
     <Link>
88
-      <AdditionalDependencies>libeay32.lib;ssleay32.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;shell32.lib;%(AdditionalDependencies)</AdditionalDependencies>
89
+      <AdditionalDependencies>libeay32.lib;ssleay32.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
89 90
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
90 91
       <GenerateDebugInformation>true</GenerateDebugInformation>
91 92
       <SubSystem>Console</SubSystem>
... ...
@@ -3866,33 +3866,6 @@ parse_argv (struct options *options,
3866 3866
 {
3867 3867
   int i, j;
3868 3868
 
3869
-#ifdef WIN32
3870
-  /*
3871
-   * Windows replaces Unicode characters in argv[] that are not present
3872
-   * in the current codepage with '?'. Get the wide char command line and
3873
-   * convert it to UTF-8 ourselves.
3874
-   */
3875
-  int wargc;
3876
-  WCHAR **wargv;
3877
-  char **uargv;
3878
-
3879
-  wargv = CommandLineToArgvW (GetCommandLineW (), &wargc);
3880
-  if (wargv == NULL || wargc != argc)
3881
-    usage ();
3882
-
3883
-  uargv = gc_malloc (wargc * sizeof (*uargv), false, &options->gc);
3884
-
3885
-  for (i = 0; i < wargc; i++)
3886
-    {
3887
-      int n = WideCharToMultiByte (CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
3888
-      uargv[i] = gc_malloc (n, false, &options->gc);
3889
-      WideCharToMultiByte (CP_UTF8, 0, wargv[i], -1, uargv[i], n, NULL, NULL);
3890
-    }
3891
-
3892
-  LocalFree (wargv);
3893
-  argv = uargv;
3894
-#endif
3895
-
3896 3869
   /* usage message */
3897 3870
   if (argc <= 1)
3898 3871
     usage ();