Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // This file is a .cpp file meant to be included in nsBrowserApp.cpp and other |
michael@0 | 6 | // similar bootstrap code. It converts wide-character windows wmain into UTF-8 |
michael@0 | 7 | // narrow-character strings. |
michael@0 | 8 | |
michael@0 | 9 | #ifndef XP_WIN |
michael@0 | 10 | #error This file only makes sense on Windows. |
michael@0 | 11 | #endif |
michael@0 | 12 | |
michael@0 | 13 | #include "nsUTF8Utils.h" |
michael@0 | 14 | |
michael@0 | 15 | #ifndef XRE_DONT_PROTECT_DLL_LOAD |
michael@0 | 16 | #include "nsSetDllDirectory.h" |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | #ifdef __MINGW32__ |
michael@0 | 20 | |
michael@0 | 21 | /* MingW currently does not implement a wide version of the |
michael@0 | 22 | startup routines. Workaround is to implement something like |
michael@0 | 23 | it ourselves. See bug 411826 */ |
michael@0 | 24 | |
michael@0 | 25 | #include <shellapi.h> |
michael@0 | 26 | |
michael@0 | 27 | int wmain(int argc, WCHAR **argv); |
michael@0 | 28 | |
michael@0 | 29 | int main(int argc, char **argv) |
michael@0 | 30 | { |
michael@0 | 31 | LPWSTR commandLine = GetCommandLineW(); |
michael@0 | 32 | int argcw = 0; |
michael@0 | 33 | LPWSTR *argvw = CommandLineToArgvW(commandLine, &argcw); |
michael@0 | 34 | if (!argvw) |
michael@0 | 35 | return 127; |
michael@0 | 36 | |
michael@0 | 37 | int result = wmain(argcw, argvw); |
michael@0 | 38 | LocalFree(argvw); |
michael@0 | 39 | return result; |
michael@0 | 40 | } |
michael@0 | 41 | #endif /* __MINGW32__ */ |
michael@0 | 42 | |
michael@0 | 43 | #define main NS_internal_main |
michael@0 | 44 | |
michael@0 | 45 | #ifndef XRE_WANT_ENVIRON |
michael@0 | 46 | int main(int argc, char **argv); |
michael@0 | 47 | #else |
michael@0 | 48 | int main(int argc, char **argv, char **envp); |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | static char* |
michael@0 | 52 | AllocConvertUTF16toUTF8(char16ptr_t arg) |
michael@0 | 53 | { |
michael@0 | 54 | // be generous... UTF16 units can expand up to 3 UTF8 units |
michael@0 | 55 | int len = wcslen(arg); |
michael@0 | 56 | char *s = new char[len * 3 + 1]; |
michael@0 | 57 | if (!s) |
michael@0 | 58 | return nullptr; |
michael@0 | 59 | |
michael@0 | 60 | ConvertUTF16toUTF8 convert(s); |
michael@0 | 61 | convert.write(arg, len); |
michael@0 | 62 | convert.write_terminator(); |
michael@0 | 63 | return s; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | static void |
michael@0 | 67 | FreeAllocStrings(int argc, char **argv) |
michael@0 | 68 | { |
michael@0 | 69 | while (argc) { |
michael@0 | 70 | --argc; |
michael@0 | 71 | delete [] argv[argc]; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | delete [] argv; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | int wmain(int argc, WCHAR **argv) |
michael@0 | 78 | { |
michael@0 | 79 | #ifndef XRE_DONT_PROTECT_DLL_LOAD |
michael@0 | 80 | mozilla::SanitizeEnvironmentVariables(); |
michael@0 | 81 | SetDllDirectoryW(L""); |
michael@0 | 82 | #endif |
michael@0 | 83 | |
michael@0 | 84 | char **argvConverted = new char*[argc + 1]; |
michael@0 | 85 | if (!argvConverted) |
michael@0 | 86 | return 127; |
michael@0 | 87 | |
michael@0 | 88 | for (int i = 0; i < argc; ++i) { |
michael@0 | 89 | argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]); |
michael@0 | 90 | if (!argvConverted[i]) { |
michael@0 | 91 | return 127; |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | argvConverted[argc] = nullptr; |
michael@0 | 95 | |
michael@0 | 96 | // need to save argvConverted copy for later deletion. |
michael@0 | 97 | char **deleteUs = new char*[argc+1]; |
michael@0 | 98 | if (!deleteUs) { |
michael@0 | 99 | FreeAllocStrings(argc, argvConverted); |
michael@0 | 100 | return 127; |
michael@0 | 101 | } |
michael@0 | 102 | for (int i = 0; i < argc; i++) |
michael@0 | 103 | deleteUs[i] = argvConverted[i]; |
michael@0 | 104 | #ifndef XRE_WANT_ENVIRON |
michael@0 | 105 | int result = main(argc, argvConverted); |
michael@0 | 106 | #else |
michael@0 | 107 | // Force creation of the multibyte _environ variable. |
michael@0 | 108 | getenv("PATH"); |
michael@0 | 109 | int result = main(argc, argvConverted, _environ); |
michael@0 | 110 | #endif |
michael@0 | 111 | |
michael@0 | 112 | delete[] argvConverted; |
michael@0 | 113 | FreeAllocStrings(argc, deleteUs); |
michael@0 | 114 | |
michael@0 | 115 | return result; |
michael@0 | 116 | } |