win32u: Improve string duplication helpers.

Signed-off-by: Jacek Caban <jacek@codeweavers.com>
Signed-off-by: Huw Davies <huw@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Jacek Caban 2022-04-07 15:24:25 +02:00 committed by Alexandre Julliard
parent c699803ff0
commit b7f108ed9c
3 changed files with 47 additions and 40 deletions

View File

@ -277,15 +277,6 @@ static inline INT INTERNAL_YWSTODS(DC *dc, INT height)
return pt[1].y - pt[0].y;
}
static inline WCHAR *strdupW( const WCHAR *p )
{
WCHAR *ret;
DWORD len = (lstrlenW(p) + 1) * sizeof(WCHAR);
ret = malloc( len );
memcpy(ret, p, len);
return ret;
}
static INT FONT_GetObjectW( HGDIOBJ handle, INT count, LPVOID buffer );
static BOOL FONT_DeleteObject( HGDIOBJ handle );
@ -1201,8 +1192,8 @@ static struct gdi_font_face *create_face( struct gdi_font_family *family, const
struct gdi_font_face *face = calloc( 1, sizeof(*face) );
face->refcount = 1;
face->style_name = strdupW( style );
face->full_name = strdupW( fullname );
face->style_name = wcsdup( style );
face->full_name = wcsdup( fullname );
face->face_index = index;
face->fs = fs;
face->ntmFlags = ntmflags;
@ -1210,7 +1201,7 @@ static struct gdi_font_face *create_face( struct gdi_font_family *family, const
face->flags = flags;
face->data_ptr = data_ptr;
face->data_size = data_size;
if (file) face->file = strdupW( file );
if (file) face->file = wcsdup( file );
if (size) face->size = *size;
else face->scalable = TRUE;
if (insert_face_in_family_list( face, family )) return face;
@ -2075,9 +2066,9 @@ static struct gdi_font *create_gdi_font( const struct gdi_font_face *face, const
font->ntmFlags = face->ntmFlags;
font->aa_flags = HIWORD( face->flags );
if (!family_name) family_name = face->family->family_name;
font->otm.otmpFamilyName = (char *)strdupW( family_name );
font->otm.otmpStyleName = (char *)strdupW( face->style_name );
font->otm.otmpFaceName = (char *)strdupW( face->full_name );
font->otm.otmpFamilyName = (char *)wcsdup( family_name );
font->otm.otmpStyleName = (char *)wcsdup( face->style_name );
font->otm.otmpFaceName = (char *)wcsdup( face->full_name );
return font;
}
@ -3311,6 +3302,22 @@ DWORD win32u_mbtowc( CPTABLEINFO *info, WCHAR *dst, DWORD dstlen, const char *sr
if (!info && !(info = get_cptable( get_acp() ))) return 0;
if (!dst)
{
/* only compute length */
if (info->DBCSOffsets)
{
for (ret = 0; srclen--; src++, ret++)
{
if (!info->DBCSOffsets[(unsigned char)*src]) continue;
if (!srclen--) break;
src++;
}
}
else ret = srclen;
return ret * sizeof(WCHAR);
}
dstlen /= sizeof(WCHAR);
if (info->DBCSOffsets)
{

View File

@ -584,24 +584,6 @@ static BOOL is_subpixel_rendering_enabled( void )
}
static LPWSTR strdupW(LPCWSTR p)
{
LPWSTR ret;
DWORD len = (lstrlenW(p) + 1) * sizeof(WCHAR);
ret = malloc( len );
memcpy(ret, p, len);
return ret;
}
static WCHAR *towstr(const char *str)
{
DWORD len = strlen(str) + 1;
WCHAR *wstr = malloc( len * sizeof(WCHAR) );
win32u_mbtowc( NULL, wstr, len * sizeof(WCHAR), str, len );
return wstr;
}
static const LANGID mac_langid_table[] =
{
MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), /* TT_MAC_LANGID_ENGLISH */
@ -1168,7 +1150,7 @@ static WCHAR *decode_opentype_name( struct opentype_name *name )
if (len == ARRAY_SIZE(buffer)) WARN("Truncated font name %s -> %s\n", debugstr_an(name->bytes, name->length), debugstr_w(buffer));
else buffer[len] = 0;
return strdupW( buffer );
return wcsdup( buffer );
}
struct unix_face
@ -3687,7 +3669,7 @@ static BOOL freetype_set_outline_text_metrics( struct gdi_font *font )
{
static const WCHAR fake_nameW[] = {'f','a','k','e',' ','n','a','m','e', 0};
FIXME("failed to read full_nameW for font %s!\n", wine_dbgstr_w((WCHAR *)font->otm.otmpFamilyName));
font->otm.otmpFullName = (char *)strdupW(fake_nameW);
font->otm.otmpFullName = (char *)wcsdup( fake_nameW );
}
needed = sizeof(font->otm) + (lstrlenW( (WCHAR *)font->otm.otmpFamilyName ) + 1 +
lstrlenW( (WCHAR *)font->otm.otmpStyleName ) + 1 +

View File

@ -571,7 +571,30 @@ static inline ULONG win32u_wcstoul( const WCHAR *s, WCHAR **end, int base )
return negative ? -ret : ret;
}
DWORD win32u_mbtowc( CPTABLEINFO *info, WCHAR *dst, DWORD dstlen, const char *src,
DWORD srclen ) DECLSPEC_HIDDEN;
DWORD win32u_wctomb( CPTABLEINFO *info, char *dst, DWORD dstlen, const WCHAR *src,
DWORD srclen ) DECLSPEC_HIDDEN;
static inline WCHAR *win32u_wcsdup( const WCHAR *str )
{
DWORD size = (lstrlenW( str ) + 1) * sizeof(WCHAR);
WCHAR *ret = malloc( size );
if (ret) memcpy( ret, str, size );
return ret;
}
static inline WCHAR *towstr( const char *str )
{
DWORD len = strlen( str ) + 1;
DWORD size = win32u_mbtowc( NULL, NULL, 0, str, len );
WCHAR *ret = malloc( size );
if (ret) win32u_mbtowc( NULL, ret, size, str, len );
return ret;
}
#define towupper(c) win32u_towupper(c)
#define wcsdup(s) win32u_wcsdup(s)
#define wcstol(s,e,b) win32u_wcstol(s,e,b)
#define wcstoul(s,e,b) win32u_wcstoul(s,e,b)
@ -587,11 +610,6 @@ static inline UINT asciiz_to_unicode( WCHAR *dst, const char *src )
return (p - dst) * sizeof(WCHAR);
}
DWORD win32u_mbtowc( CPTABLEINFO *info, WCHAR *dst, DWORD dstlen, const char *src,
DWORD srclen ) DECLSPEC_HIDDEN;
DWORD win32u_wctomb( CPTABLEINFO *info, char *dst, DWORD dstlen, const WCHAR *src,
DWORD srclen ) DECLSPEC_HIDDEN;
static inline BOOL is_win9x(void)
{
return NtCurrentTeb()->Peb->OSPlatformId == VER_PLATFORM_WIN32s;