2007-08-02 16:21:53 +02:00
|
|
|
/* strfuncs.cc: misc funcs that don't belong anywhere else
|
|
|
|
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
2008-04-01 15:22:47 +02:00
|
|
|
2005, 2006, 2007, 2008 Red Hat, Inc.
|
2007-08-02 16:21:53 +02:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2008-01-31 21:26:01 +01:00
|
|
|
#include <stdlib.h>
|
2008-05-14 12:21:22 +02:00
|
|
|
#include <wchar.h>
|
2007-08-02 16:21:53 +02:00
|
|
|
#include <winnls.h>
|
2007-08-12 14:48:02 +02:00
|
|
|
#include <ntdll.h>
|
2008-01-31 21:26:01 +01:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2007-08-02 16:21:53 +02:00
|
|
|
|
|
|
|
codepage_type current_codepage = ansi_cp;
|
2008-02-05 18:37:10 +01:00
|
|
|
UINT active_codepage = 0;
|
|
|
|
|
2007-08-02 16:21:53 +02:00
|
|
|
UINT
|
|
|
|
get_cp ()
|
|
|
|
{
|
2008-02-05 18:37:10 +01:00
|
|
|
if (!active_codepage)
|
|
|
|
codepage_init ("ansi");
|
|
|
|
return active_codepage;
|
|
|
|
}
|
|
|
|
|
2007-08-02 16:21:53 +02:00
|
|
|
/* tlen is always treated as the maximum buffer size, including the '\0'
|
|
|
|
character. sys_wcstombs will always return a 0-terminated result, no
|
|
|
|
matter what. */
|
|
|
|
int __stdcall
|
2008-01-31 21:26:01 +01:00
|
|
|
sys_wcstombs (char *tgt, int tlen, const PWCHAR src, int slen)
|
2007-08-02 16:21:53 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2008-05-14 12:21:22 +02:00
|
|
|
/* Convert UNICODE private use area. Reverse functionality (only for
|
|
|
|
path names) is transform_chars in path.cc. */
|
|
|
|
if (slen < 0)
|
|
|
|
slen = wcslen (src) + 1;
|
|
|
|
WCHAR sbuf[slen];
|
|
|
|
memcpy (sbuf, src, slen * sizeof (WCHAR));
|
|
|
|
const unsigned char *end = (unsigned char *) (sbuf + slen);
|
|
|
|
for (unsigned char *s = ((unsigned char *) sbuf) + 1; s < end;
|
|
|
|
s += sizeof (WCHAR))
|
|
|
|
if (*s == 0xf0)
|
|
|
|
*s = 0;
|
|
|
|
ret = WideCharToMultiByte (get_cp (), 0, sbuf, slen, tgt, tlen, NULL, NULL);
|
2008-01-31 21:26:01 +01:00
|
|
|
if (ret && tgt)
|
2007-10-23 18:26:28 +02:00
|
|
|
{
|
|
|
|
ret = (ret < tlen) ? ret : tlen - 1;
|
|
|
|
tgt[ret] = '\0';
|
|
|
|
}
|
2007-08-02 16:21:53 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-31 21:26:01 +01:00
|
|
|
/* Allocate a buffer big enough for the string, always including the
|
|
|
|
terminating '\0'. The buffer pointer is returned in *tgt_p, the return
|
|
|
|
value is the number of bytes written to the buffer, as usual.
|
|
|
|
The "type" argument determines where the resulting buffer is stored.
|
|
|
|
It's either one of the cygheap_types values, or it's "HEAP_NOTHEAP".
|
2008-02-04 13:00:19 +01:00
|
|
|
In the latter case the allocation uses simple calloc.
|
2008-02-15 18:53:11 +01:00
|
|
|
|
2008-02-04 13:00:19 +01:00
|
|
|
Note that this code is shared by cygserver (which requires it via
|
2008-02-15 18:53:11 +01:00
|
|
|
__small_vsprintf) and so when built there plain calloc is the
|
2008-02-04 13:00:19 +01:00
|
|
|
only choice. */
|
2008-01-31 21:26:01 +01:00
|
|
|
int __stdcall
|
|
|
|
sys_wcstombs_alloc (char **tgt_p, int type, const PWCHAR src, int slen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2008-02-25 19:32:23 +01:00
|
|
|
ret = WideCharToMultiByte (get_cp (), 0, src, slen, NULL, 0 ,NULL, NULL);
|
2008-01-31 21:26:01 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2008-03-02 23:51:19 +01:00
|
|
|
size_t tlen = (slen == -1) ? ret : ret + 1;
|
2008-01-31 21:26:01 +01:00
|
|
|
|
|
|
|
if (type == HEAP_NOTHEAP)
|
2008-02-15 18:53:11 +01:00
|
|
|
*tgt_p = (char *) calloc (tlen, sizeof (char));
|
2008-01-31 21:26:01 +01:00
|
|
|
else
|
2008-02-15 18:53:11 +01:00
|
|
|
*tgt_p = (char *) ccalloc ((cygheap_types) type, tlen, sizeof (char));
|
2008-01-31 21:26:01 +01:00
|
|
|
if (!*tgt_p)
|
2008-02-15 18:53:11 +01:00
|
|
|
return 0;
|
2008-01-31 21:26:01 +01:00
|
|
|
ret = sys_wcstombs (*tgt_p, tlen, src, slen);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-02 16:21:53 +02:00
|
|
|
int __stdcall
|
2008-02-25 19:32:23 +01:00
|
|
|
sys_mbstowcs (PWCHAR tgt, int tlen, const char *src, int slen)
|
2007-08-02 16:21:53 +02:00
|
|
|
{
|
2008-02-25 19:32:23 +01:00
|
|
|
int ret = MultiByteToWideChar (get_cp (), 0, src, slen, tgt, tlen);
|
|
|
|
if (ret && tgt)
|
|
|
|
{
|
|
|
|
ret = (ret < tlen) ? ret : tlen - 1;
|
|
|
|
tgt[ret] = L'\0';
|
|
|
|
}
|
|
|
|
return ret;
|
2007-08-02 16:21:53 +02:00
|
|
|
}
|
2007-08-12 14:48:02 +02:00
|
|
|
|
2008-01-31 21:26:01 +01:00
|
|
|
/* Same as sys_wcstombs_alloc, just backwards. */
|
|
|
|
int __stdcall
|
2008-02-25 19:32:23 +01:00
|
|
|
sys_mbstowcs_alloc (PWCHAR *tgt_p, int type, const char *src, int slen)
|
2008-01-31 21:26:01 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2008-02-25 19:32:23 +01:00
|
|
|
ret = MultiByteToWideChar (get_cp (), 0, src, slen, NULL, 0);
|
2008-01-31 21:26:01 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
2008-02-25 19:32:23 +01:00
|
|
|
size_t tlen = (slen == -1 ? ret : ret + 1);
|
|
|
|
|
2008-01-31 21:26:01 +01:00
|
|
|
if (type == HEAP_NOTHEAP)
|
2008-02-25 19:32:23 +01:00
|
|
|
*tgt_p = (PWCHAR) calloc (tlen, sizeof (WCHAR));
|
2008-01-31 21:26:01 +01:00
|
|
|
else
|
2008-02-25 19:32:23 +01:00
|
|
|
*tgt_p = (PWCHAR) ccalloc ((cygheap_types) type, tlen, sizeof (WCHAR));
|
2008-01-31 21:26:01 +01:00
|
|
|
if (!*tgt_p)
|
2008-02-15 18:53:11 +01:00
|
|
|
return 0;
|
2008-02-25 19:32:23 +01:00
|
|
|
ret = sys_mbstowcs (*tgt_p, tlen, src, slen);
|
2008-01-31 21:26:01 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-12 14:48:02 +02:00
|
|
|
static WCHAR hex_wchars[] = L"0123456789abcdef";
|
|
|
|
|
|
|
|
NTSTATUS NTAPI
|
|
|
|
RtlInt64ToHexUnicodeString (ULONGLONG value, PUNICODE_STRING dest,
|
|
|
|
BOOLEAN append)
|
|
|
|
{
|
|
|
|
USHORT len = append ? dest->Length : 0;
|
|
|
|
if (dest->MaximumLength - len < 16 * (int) sizeof (WCHAR))
|
|
|
|
return STATUS_BUFFER_OVERFLOW;
|
|
|
|
PWCHAR end = (PWCHAR) ((PBYTE) dest->Buffer + len);
|
|
|
|
register PWCHAR p = end + 16;
|
|
|
|
while (p-- > end)
|
|
|
|
{
|
|
|
|
*p = hex_wchars[value & 0xf];
|
|
|
|
value >>= 4;
|
|
|
|
}
|
|
|
|
dest->Length += 16 * sizeof (WCHAR);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|