|
| 1 | +/* |
| 2 | +* Author: Manoj Ampalam <[email protected]> |
| 3 | +* |
| 4 | +* Converts UTF-16 arguments to UTF-8 |
| 5 | +* |
| 6 | +* Copyright (c) 2015 Microsoft Corp. |
| 7 | +* All rights reserved |
| 8 | +* |
| 9 | +* Microsoft openssh win32 port |
| 10 | +* |
| 11 | +* Redistribution and use in source and binary forms, with or without |
| 12 | +* modification, are permitted provided that the following conditions |
| 13 | +* are met: |
| 14 | +* |
| 15 | +* 1. Redistributions of source code must retain the above copyright |
| 16 | +* notice, this list of conditions and the following disclaimer. |
| 17 | +* 2. Redistributions in binary form must reproduce the above copyright |
| 18 | +* notice, this list of conditions and the following disclaimer in the |
| 19 | +* documentation and/or other materials provided with the distribution. |
| 20 | +* |
| 21 | +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 22 | +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 23 | +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 24 | +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 26 | +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 30 | +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +*/ |
| 32 | + |
| 33 | +#include <Windows.h> |
| 34 | + |
| 35 | +char* |
| 36 | +utf16_to_utf8(wchar_t utf16str) { |
| 37 | + char* ret; |
| 38 | + int needed; |
| 39 | + if ((needed = WideCharToMultiByte(CP_UTF8, 0, utf16str, -1, NULL, 0, NULL, NULL)) == 0 || |
| 40 | + (ret = malloc(needed)) == NULL || |
| 41 | + WideCharToMultiByte(CP_UTF8, 0, utf16str, -1, ret, needed, NULL, NULL) != needed ) |
| 42 | + fatal("failed to covert input arguments"); |
| 43 | + |
| 44 | + return ret; |
| 45 | +} |
| 46 | + |
| 47 | +int |
| 48 | +wmain(int argc, wchar_t **wargv) { |
| 49 | + char** argv = NULL; |
| 50 | + int i; |
| 51 | + |
| 52 | + if (argc) { |
| 53 | + if ((argv = malloc(argc * sizeof(char*))) == NULL) |
| 54 | + fatal("out of memory"); |
| 55 | + for (i = 0; i < argc; i++) |
| 56 | + argv[i] = utf16_to_utf8(wargv[i]); |
| 57 | + } |
| 58 | + |
| 59 | + return main(argc, argv); |
| 60 | +} |
0 commit comments