Cleaning up stringhelp.c

This commit is contained in:
Manoj Ampalam 2016-10-17 22:11:51 -07:00
parent dcb63461a1
commit 49da240a9c
6 changed files with 22 additions and 265 deletions

View File

@ -153,7 +153,6 @@
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\kerberos.c" />
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\startupneeds.c" />
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\strcasecmp.c" />
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\stringhelp.c" />
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\tncon.c" />
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\tnnet.c" />
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\win32auth.c" />

View File

@ -63,9 +63,6 @@
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\strcasecmp.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\stringhelp.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\tncon.c">
<Filter>Source Files</Filter>
</ClCompile>

View File

@ -36,6 +36,7 @@
#include "inc/defs.h"
#include <errno.h>
#include <stddef.h>
#include "inc\utf.h"
/* internal read buffer size */
#define READ_BUFFER_SIZE 100*1024
@ -238,39 +239,40 @@ createFile_flags_setup(int flags, int mode, struct createFile_flags* cf_flags) {
/* open() implementation. Uses CreateFile to open file, console, device, etc */
struct w32_io*
fileio_open(const char *pathname, int flags, int mode) {
fileio_open(const char *path_utf8, int flags, int mode) {
struct w32_io* pio = NULL;
struct createFile_flags cf_flags;
HANDLE handle;
wchar_t wpathname[MAX_PATH];
wchar_t *path_utf16 = NULL;
debug2("open - pathname:%s, flags:%d, mode:%d", pathname, flags, mode);
debug2("open - pathname:%s, flags:%d, mode:%d", path_utf8, flags, mode);
/* check input params*/
if (pathname == NULL) {
if (path_utf8 == NULL) {
errno = EINVAL;
debug("open - ERROR:%d", errno);
return NULL;
}
if (MultiByteToWideChar(CP_UTF8, 0, pathname, -1, wpathname, MAX_PATH) == 0) {
errno = EFAULT;
debug("WideCharToMultiByte failed - ERROR:%d", GetLastError());
if ((path_utf16 = utf8_to_utf16(path_utf8)) == NULL) {
errno = ENOMEM;
debug("utf8_to_utf16 failed - ERROR:%d", GetLastError());
return NULL;
}
if (createFile_flags_setup(flags, mode, &cf_flags) == -1)
return NULL;
handle = CreateFileW(wpathname, cf_flags.dwDesiredAccess, cf_flags.dwShareMode,
handle = CreateFileW(path_utf16, cf_flags.dwDesiredAccess, cf_flags.dwShareMode,
&cf_flags.securityAttributes, cf_flags.dwCreationDisposition,
cf_flags.dwFlagsAndAttributes, NULL);
if (handle == INVALID_HANDLE_VALUE) {
errno = errno_from_Win32LastError();
debug("open - CreateFile ERROR:%d", GetLastError());
free(path_utf16);
return NULL;
}
free(path_utf16);
pio = (struct w32_io*)malloc(sizeof(struct w32_io));
if (pio == NULL) {
CloseHandle(handle);

View File

@ -116,13 +116,18 @@ int config_log_level() {
int pubkey_allowed(struct sshkey* pubkey, wchar_t* wuser, wchar_t* wuser_home) {
struct passwd pw;
char user[256], user_home[MAX_PATH];
int ret;
char *user = NULL, *user_home = NULL;
memset(&pw, 0, sizeof(pw));
if (WideCharToMultiByte(CP_UTF8, 0, wuser, -1, user, 256, NULL, NULL) == 0)
return 0;
WideCharToMultiByte(CP_UTF8, 0, wuser_home, -1, user_home, MAX_PATH, NULL, NULL);
pw.pw_dir = user_home;
if ((user_home = utf16_to_utf8(wuser_home)) == NULL ||
(user = utf16_to_utf8(wuser)) == NULL)
return 0;
pw.pw_dir = user_home;
pw.pw_name = user;
return user_key_allowed(&pw, pubkey, 1);
ret = user_key_allowed(&pw, pubkey, 1);
free(user);
free(user_home);
return ret;
}

View File

@ -1,198 +0,0 @@
/*
* Author: NoMachine <developers@nomachine.com>
*
* Copyright (c) 2009, 2010 NoMachine
* All rights reserved
*
* Support functions and system calls' replacements needed to let the
* software run on Win32 based operating systems.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Windows.h>
#include "Debug.h"
#undef DEBUG
#ifdef DEBUG
# define DBG_MSG(...) debug3(__VA_ARGS__)
#else
# define DBG_MSG(...)
#endif
#define SocketErrorStringSize 1024
/*
* Convert string encoding from one 8-bit CP to another 8-bit CP.
* WARNING: Returned string MUST be freed by caller.
*
* src - source string (IN).
* srcSize - size of src string in bytes or -1 if zero terminated (IN).
* srcCP - code page used by src string (IN).
* dstCP - target code page (IN).
* retSize - size of returned string, may be NULL (OUT/OPTIONAL).
*
* RETURNS: Pointer to new allocated string encoded in target CP
* or NULL if error.
*
*/
void *ConvertCodePage(const char *src, int srcSize,
DWORD srcCP, DWORD dstCP, int *retSize)
{
int exitCode = -1;
int utf16Len = 0;
int dstLen = 0;
wchar_t *utf16 = NULL;
char *ret = NULL;
DBG_MSG("-> ConvertCodePage(src=[%s], srcSize=[%d])...", src, srcSize);
/*
* Check args.
*/
FAIL(src == NULL);
/*
* Retrieve size for UTF16.
*/
DBG_MSG("Retrieving size of UTF16...");
utf16Len = MultiByteToWideChar(srcCP, 0, src, srcSize, NULL, 0);
FAIL(utf16Len <= 0);
/*
* Allocate buffer for UTF16.
*/
DBG_MSG("Allocating [%d] bytes for UTF16...", utf16Len * sizeof(wchar_t));
utf16 = (wchar_t *) malloc((utf16Len + 1) * sizeof(wchar_t));
FAIL(utf16 == NULL);
/*
* Convert src to UTF16.
*/
DBG_MSG("Allocating [%d] bytes for UTF16...", utf16Len * sizeof(wchar_t));
FAIL(MultiByteToWideChar(srcCP, 0, src, srcSize, utf16, utf16Len) < 0);
/*
* Allocate buffer for return.
*/
DBG_MSG("Allocating buffer for dst...");
dstLen = WideCharToMultiByte(dstCP, 0, utf16, -1, NULL, 0, NULL, NULL);
ret = malloc(dstLen + 1);
FAIL(ret == NULL);
ret[dstLen] = 0;
/*
* Convert utf16 to target CP.
*/
DBG_MSG("Converting UTF16 to dst...");
dstLen = WideCharToMultiByte(dstCP, 0, utf16, utf16Len,
ret, dstLen + 1, NULL, NULL);
FAIL(dstLen < 0);
if (retSize)
{
*retSize = dstLen;
}
/*
* Clean up.
*/
exitCode = 0;
fail:
if (exitCode)
{
debug3("ERROR: Cannot convert [%s] from CP[0x%x] to CP[0x%x]."
"Error code is : %d.\n", src, srcCP, dstCP, GetLastError());
if (ret)
{
free(ret);
ret = NULL;
}
}
if (utf16)
{
free(utf16);
}
DBG_MSG("<- ConvertCodePage()...");
return ret;
}
/*
* Convert string from UTF8 to CP used by current thread (Local8).
*
* utf8 - string in UTF8 (IN).
* utf8Size - size of utf8 string in bytes or -1 if zero terminated (IN).
* bytesReturned - size of returned Local8 string (OUT).
*
* RETURNS: Pointer to new allocated Local8 string or NULL if error.
*/
void *ConvertUtf8ToLocal8(const char *utf8, int utf8Size, int *bytesReturned)
{
return ConvertCodePage(utf8, utf8Size, CP_UTF8, GetACP(), bytesReturned);
}
/*
* Convert string from CP used by current thread (Local8) to UTF8.
*
* local8 - string in Local8 CP (IN).
* local8Size - size of local8 string in bytes or -1 if zero terminated (IN).
* bytesReturned - size of returned UTF8 string (OUT).
*
* RETURNS: Pointer to new allocated UTF8 string or NULL if error.
*/
void *ConvertLocal8ToUtf8(const char *local8, int local8Size, int *bytesReturned)
{
return ConvertCodePage(local8, local8Size,
GetACP(), CP_UTF8, bytesReturned);
}

View File

@ -1,48 +0,0 @@
/*
* Author: NoMachine <developers@nomachine.com>
*
* Copyright (c) 2009, 2010 NoMachine
* All rights reserved
*
* Support functions and system calls' replacements needed to let the
* software run on Win32 based operating systems.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef StringHelp_H
#define StringHelp_H 1
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif
void *ConvertCodePage(const char *src, int srcSize, DWORD srcCP, DWORD dstCP, int *retSize);
void *ConvertUtf8ToLocal8(const char *utf8, int utf8Size, int *bytesReturned);
#ifdef __cplusplus
};
#endif
#endif /* StringHelp_H */