Unifiy string append functions

(cherry picked from commit f25261e271)
This commit is contained in:
akallabeth 2022-06-03 10:25:30 +02:00 committed by akallabeth
parent 75bfec61f9
commit a4c86723a3
15 changed files with 167 additions and 202 deletions

View File

@ -21,59 +21,37 @@
#ifndef FREERDP_INT_AINPUT_COMMON_H
#define FREERDP_INT_AINPUT_COMMON_H
#include <assert.h>
#include <winpr/string.h>
#include <freerdp/channels/ainput.h>
static INLINE void ainput_append(char* buffer, size_t size, const char* what, BOOL separator)
{
size_t have;
size_t toadd;
assert(buffer || (size == 0));
assert(what);
have = strnlen(buffer, size);
toadd = strlen(what);
if (have > 0)
toadd += 1;
if (size - have < toadd + 1)
return;
if (have > 0)
strcat(buffer, separator ? "|" : " ");
strcat(buffer, what);
}
static INLINE const char* ainput_flags_to_string(UINT64 flags, char* buffer, size_t size)
{
char number[32] = { 0 };
if (flags & AINPUT_FLAGS_HAVE_REL)
ainput_append(buffer, size, "AINPUT_FLAGS_HAVE_REL", TRUE);
winpr_str_append("AINPUT_FLAGS_HAVE_REL", buffer, size, "|");
if (flags & AINPUT_FLAGS_WHEEL)
ainput_append(buffer, size, "AINPUT_FLAGS_WHEEL", TRUE);
winpr_str_append("AINPUT_FLAGS_WHEEL", buffer, size, "|");
if (flags & AINPUT_FLAGS_MOVE)
ainput_append(buffer, size, "AINPUT_FLAGS_MOVE", TRUE);
winpr_str_append("AINPUT_FLAGS_MOVE", buffer, size, "|");
if (flags & AINPUT_FLAGS_DOWN)
ainput_append(buffer, size, "AINPUT_FLAGS_DOWN", TRUE);
winpr_str_append("AINPUT_FLAGS_DOWN", buffer, size, "|");
if (flags & AINPUT_FLAGS_REL)
ainput_append(buffer, size, "AINPUT_FLAGS_REL", TRUE);
winpr_str_append("AINPUT_FLAGS_REL", buffer, size, "|");
if (flags & AINPUT_FLAGS_BUTTON1)
ainput_append(buffer, size, "AINPUT_FLAGS_BUTTON1", TRUE);
winpr_str_append("AINPUT_FLAGS_BUTTON1", buffer, size, "|");
if (flags & AINPUT_FLAGS_BUTTON2)
ainput_append(buffer, size, "AINPUT_FLAGS_BUTTON2", TRUE);
winpr_str_append("AINPUT_FLAGS_BUTTON2", buffer, size, "|");
if (flags & AINPUT_FLAGS_BUTTON3)
ainput_append(buffer, size, "AINPUT_FLAGS_BUTTON3", TRUE);
winpr_str_append("AINPUT_FLAGS_BUTTON3", buffer, size, "|");
if (flags & AINPUT_XFLAGS_BUTTON1)
ainput_append(buffer, size, "AINPUT_XFLAGS_BUTTON1", TRUE);
winpr_str_append("AINPUT_XFLAGS_BUTTON1", buffer, size, "|");
if (flags & AINPUT_XFLAGS_BUTTON2)
ainput_append(buffer, size, "AINPUT_XFLAGS_BUTTON2", TRUE);
winpr_str_append("AINPUT_XFLAGS_BUTTON2", buffer, size, "|");
_snprintf(number, sizeof(number), "[0x%08" PRIx64 "]", flags);
ainput_append(buffer, size, number, FALSE);
winpr_str_append(number, buffer, size, " ");
return buffer;
}

View File

@ -1952,7 +1952,7 @@ static UINT rdpdr_server_drive_query_directory_callback1(RdpdrServerContext* con
irp->Callback = rdpdr_server_drive_query_directory_callback2;
irp->DeviceId = deviceId;
irp->FileId = fileId;
strcat(irp->PathName, "\\*.*");
winpr_str_append("\\*.*", irp->PathName, ARRAYSIZE(irp->PathName), NULL);
if (!rdpdr_server_enqueue_irp(context, irp))
{

View File

@ -511,12 +511,7 @@ static BOOL filter_by_class(uint8_t bDeviceClass, uint8_t bDeviceSubClass)
static BOOL append(char* dst, size_t length, const char* src)
{
size_t slen = strlen(src);
size_t dlen = strnlen(dst, length);
if (dlen + slen >= length)
return FALSE;
strcat(dst, src);
return TRUE;
return winpr_str_append(src, dst, length, NULL);
}
static BOOL device_is_filtered(struct libusb_device* dev,

View File

@ -400,18 +400,19 @@ static int xf_keyboard_execute_action_script(xfContext* xfc, XF_MODIFIER_KEYS* m
}
if (mod->Shift)
strcat(combination, "Shift+");
winpr_str_append("Shift", combination, sizeof(combination), "+");
if (mod->Ctrl)
strcat(combination, "Ctrl+");
winpr_str_append("Ctrl", combination, sizeof(combination), "+");
if (mod->Alt)
strcat(combination, "Alt+");
winpr_str_append("Alt", combination, sizeof(combination), "+");
if (mod->Super)
strcat(combination, "Super+");
winpr_str_append("Super", combination, sizeof(combination), "+");
winpr_str_append(keyStr, combination, sizeof(combination), NULL);
strcat(combination, keyStr);
count = ArrayList_Count(xfc->keyCombinations);
for (index = 0; index < count; index++)

View File

@ -211,6 +211,7 @@ static BOOL copy_value(const char* value, char** dst)
static BOOL append_value(const char* value, char** dst)
{
size_t x = 0, y;
size_t size;
char* tmp;
if (!dst || !value)
return FALSE;
@ -218,14 +219,16 @@ static BOOL append_value(const char* value, char** dst)
if (*dst)
x = strlen(*dst);
y = strlen(value);
tmp = realloc(*dst, x + y + 2);
size = x + y + 2;
tmp = realloc(*dst, size);
if (!tmp)
return FALSE;
if (x == 0)
tmp[0] = '\0';
else
strcat(tmp, ",");
strcat(tmp, value);
winpr_str_append(",", tmp, size, NULL);
winpr_str_append(value, tmp, size, NULL);
*dst = tmp;
return TRUE;
}

View File

@ -257,20 +257,18 @@ static const char* flags_to_string(UINT32 flags, const t_flag_mapping* map, size
{
size_t x = 0;
static char buffer[1024] = { 0 };
char fields[12];
memset(buffer, 0, sizeof(buffer));
char fields[12] = { 0 };
for (x = 0; x < elements; x++)
{
if (buffer[0] != '\0')
strcat(buffer, "|");
const t_flag_mapping* cur = &map[x];
if ((map[x].code & flags) != 0)
strcat(buffer, map[x].name);
if ((cur->code & flags) != 0)
winpr_str_append(cur->name, buffer, sizeof(buffer), "|");
}
sprintf_s(fields, ARRAYSIZE(fields), " [%04" PRIx32 "]", flags);
strcat(buffer, fields);
winpr_str_append(fields, buffer, sizeof(buffer), NULL);
return buffer;
}

View File

@ -41,11 +41,13 @@ static const char* const INFO_TYPE_LOGON_STRINGS[4] = { "Logon Info V1", "Logon
/* This define limits the length of the strings in the label field. */
#define MAX_LABEL_LENGTH 40
static struct
struct info_flags_t
{
UINT32 flag;
const char* label;
} const info_flags[] = {
};
static const struct info_flags_t info_flags[] = {
{ INFO_MOUSE, "INFO_MOUSE" },
{ INFO_DISABLECTRLALTDEL, "INFO_DISABLECTRLALTDEL" },
{ INFO_AUTOLOGON, "INFO_AUTOLOGON" },
@ -118,12 +120,8 @@ static BOOL rdp_read_info_null_string(UINT32 flags, wStream* s, size_t cbLen, CH
static char* rdp_info_package_flags_description(UINT32 flags)
{
char* result;
size_t maximum_size = 1; /* Reserve space for the terminating '\0' by strcat if all flags set */
size_t maximum_size = 1 + MAX_LABEL_LENGTH * ARRAYSIZE(info_flags);
size_t i;
size_t size;
for (i = 0; i < ARRAYSIZE(info_flags); i++)
maximum_size += strnlen(info_flags[i].label, MAX_LABEL_LENGTH) + 1;
result = calloc(maximum_size, sizeof(char));
@ -132,18 +130,13 @@ static char* rdp_info_package_flags_description(UINT32 flags)
for (i = 0; i < ARRAYSIZE(info_flags); i++)
{
if (info_flags[i].flag & flags)
const struct info_flags_t* cur = &info_flags[i];
if (cur->flag & flags)
{
strcat(result, info_flags[i].label);
strcat(result, "|");
winpr_str_append(cur->label, result, maximum_size, "|");
}
}
size = strnlen(result, maximum_size);
if (size > 0)
result[size - 1] = '\0'; /* remove last "|" */
return result;
}

View File

@ -945,6 +945,28 @@ BOOL nego_process_negotiation_request(rdpNego* nego, wStream* s)
* @param nego
* @param s
*/
static const char* nego_rdp_neg_rsp_flags_str(UINT32 flags)
{
static char buffer[1024] = { 0 };
_snprintf(buffer, ARRAYSIZE(buffer), "[0x%02" PRIx32 "] ", flags);
if (flags & EXTENDED_CLIENT_DATA_SUPPORTED)
winpr_str_append("EXTENDED_CLIENT_DATA_SUPPORTED", buffer, sizeof(buffer), "|");
if (flags & DYNVC_GFX_PROTOCOL_SUPPORTED)
winpr_str_append("DYNVC_GFX_PROTOCOL_SUPPORTED", buffer, sizeof(buffer), "|");
if (flags & RDP_NEGRSP_RESERVED)
winpr_str_append("RDP_NEGRSP_RESERVED", buffer, sizeof(buffer), "|");
if (flags & RESTRICTED_ADMIN_MODE_SUPPORTED)
winpr_str_append("RESTRICTED_ADMIN_MODE_SUPPORTED", buffer, sizeof(buffer), "|");
if (flags & REDIRECTED_AUTHENTICATION_MODE_SUPPORTED)
winpr_str_append("REDIRECTED_AUTHENTICATION_MODE_SUPPORTED", buffer, sizeof(buffer), "|");
if ((flags &
~(EXTENDED_CLIENT_DATA_SUPPORTED | DYNVC_GFX_PROTOCOL_SUPPORTED | RDP_NEGRSP_RESERVED |
RESTRICTED_ADMIN_MODE_SUPPORTED | REDIRECTED_AUTHENTICATION_MODE_SUPPORTED)))
winpr_str_append("UNKNOWN", buffer, sizeof(buffer), "|");
return buffer;
}
BOOL nego_process_negotiation_response(rdpNego* nego, wStream* s)
{
@ -959,6 +981,7 @@ BOOL nego_process_negotiation_response(rdpNego* nego, wStream* s)
}
Stream_Read_UINT8(s, nego->flags);
WLog_DBG(TAG, "RDP_NEG_RSP::flags = { %s }", nego_rdp_neg_rsp_flags_str(nego->flags));
Stream_Read_UINT16(s, length);
Stream_Read_UINT32(s, nego->SelectedProtocol);
nego->state = NEGO_STATE_FINAL;

View File

@ -81,6 +81,7 @@ enum RDP_NEG_MSG
#define DYNVC_GFX_PROTOCOL_SUPPORTED 0x02
#define RDP_NEGRSP_RESERVED 0x04
#define RESTRICTED_ADMIN_MODE_SUPPORTED 0x08
#define REDIRECTED_AUTHENTICATION_MODE_SUPPORTED 0x10
#define PRECONNECTION_PDU_V1_SIZE 16
#define PRECONNECTION_PDU_V2_MIN_SIZE (PRECONNECTION_PDU_V1_SIZE + 2)

View File

@ -31,6 +31,9 @@ extern "C"
{
#endif
WINPR_API BOOL winpr_str_append(const char* what, char* buffer, size_t size,
const char* separator);
#ifndef _WIN32
#define CSTR_LESS_THAN 1

View File

@ -321,91 +321,107 @@ static void* clipboard_synthesize_image_bmp(wClipboard* clipboard, UINT32 format
*/
static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 formatId,
const void* data, UINT32* pSize)
const void* pData, UINT32* pSize)
{
char* pSrcData = NULL;
union
{
const void* cpv;
const char* cpc;
const BYTE* cpb;
WCHAR* pv;
} pSrcData;
char* pDstData = NULL;
INT64 SrcSize = (INT64)*pSize;
pSrcData.cpv = NULL;
WINPR_ASSERT(clipboard);
WINPR_ASSERT(pSize);
if (formatId == ClipboardGetFormatId(clipboard, "text/html"))
{
const INT64 SrcSize = (INT64)*pSize;
const size_t DstSize = SrcSize + 200;
char* body;
BYTE bom[2];
char num[20];
WCHAR* wstr;
char num[20] = { 0 };
/* Create a copy, we modify the input data */
pSrcData.pv = calloc(1, SrcSize + 1);
if (!pSrcData.pv)
goto fail;
memcpy(pSrcData.pv, pData, SrcSize);
if (SrcSize > 2)
{
CopyMemory(bom, data, 2);
if ((bom[0] == 0xFE) && (bom[1] == 0xFF))
{
ByteSwapUnicode((WCHAR*)data, SrcSize / 2);
}
if ((bom[0] == 0xFF) && (bom[1] == 0xFE))
{
wstr = (WCHAR*)&((BYTE*)data)[2];
ConvertFromUnicode(CP_UTF8, 0, wstr, (SrcSize - 2) / 2, &pSrcData, 0, NULL, NULL);
}
}
if (!pSrcData)
{
pSrcData = (char*)calloc(1, SrcSize + 1);
if (!pSrcData)
if (SrcSize > INT_MAX)
return NULL;
CopyMemory(pSrcData, data, SrcSize);
/* Check the BOM (Byte Order Mark) */
if ((pSrcData.cpb[0] == 0xFE) && (pSrcData.cpb[1] == 0xFF))
ByteSwapUnicode(pSrcData.pv, (int)(SrcSize / 2));
/* Check if we have WCHAR, convert to UTF-8 */
if ((pSrcData.cpb[0] == 0xFF) && (pSrcData.cpb[1] == 0xFE))
{
char* utfString = NULL;
ConvertFromUnicode(CP_UTF8, 0, &pSrcData.pv[1], (int)(SrcSize - 2) / 2, &utfString,
0, NULL, NULL);
free(pSrcData.pv);
pSrcData.cpc = utfString;
}
}
pDstData = (char*)calloc(1, SrcSize + 200);
pDstData = (char*)calloc(1, DstSize);
if (!pDstData)
{
free(pSrcData);
return NULL;
}
goto fail;
sprintf_s(pDstData, SrcSize + 200,
sprintf_s(pDstData, DstSize,
"Version:0.9\r\n"
"StartHTML:0000000000\r\n"
"EndHTML:0000000000\r\n"
"StartFragment:0000000000\r\n"
"EndFragment:0000000000\r\n");
body = strstr(pSrcData, "<body");
body = strstr(pSrcData.cpc, "<body");
if (!body)
body = strstr(pSrcData, "<BODY");
body = strstr(pSrcData.cpc, "<BODY");
/* StartHTML */
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, DstSize));
CopyMemory(&pDstData[23], num, 10);
if (!body)
strcat(pDstData, "<HTML><BODY>");
{
if (!winpr_str_append("<HTML><BODY>", pDstData, DstSize, NULL))
goto fail;
}
strcat(pDstData, "<!--StartFragment-->");
if (!winpr_str_append("<!--StartFragment-->", pDstData, DstSize, NULL))
goto fail;
/* StartFragment */
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
CopyMemory(&pDstData[69], num, 10);
strcat(pDstData, pSrcData);
if (!winpr_str_append(pSrcData.cpc, pDstData, DstSize, NULL))
goto fail;
/* EndFragment */
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
CopyMemory(&pDstData[93], num, 10);
strcat(pDstData, "<!--EndFragment-->");
if (!winpr_str_append("<!--EndFragment-->", pDstData, DstSize, NULL))
goto fail;
if (!body)
strcat(pDstData, "</BODY></HTML>");
/* EndHTML */
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
CopyMemory(&pDstData[43], num, 10);
*pSize = (UINT32)strlen(pDstData) + 1;
free(pSrcData);
{
if (!winpr_str_append("</BODY></HTML>", pDstData, DstSize, NULL))
goto fail;
}
/* EndHTML */
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, DstSize));
CopyMemory(&pDstData[43], num, 10);
*pSize = (UINT32)strnlen(pDstData, DstSize) + 1;
}
fail:
free(pSrcData.pv);
return pDstData;
}

View File

@ -33,6 +33,23 @@
#include "../log.h"
#define TAG WINPR_TAG("crt")
BOOL winpr_str_append(const char* what, char* buffer, size_t size, const char* separator)
{
const size_t used = strnlen(buffer, size);
const size_t add = strnlen(what, size);
const size_t sep_len = separator ? strnlen(separator, size) : 0;
const size_t sep = (used > 0) ? sep_len : 0;
if (used + add + sep >= size)
return FALSE;
if ((used > 0) && (sep_len > 0))
strncat(buffer, separator, sep_len);
strncat(buffer, what, add);
return TRUE;
}
#ifndef _WIN32
char* _strdup(const char* strSource)

View File

@ -24,6 +24,7 @@
#endif
#include <winpr/crt.h>
#include <winpr/string.h>
#include <winpr/path.h>
#include <winpr/file.h>
@ -541,15 +542,7 @@ DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
static char* append(char* buffer, size_t size, const char* append)
{
const size_t len = strnlen(buffer, size);
if (len == 0)
_snprintf(buffer, size, "%s", append);
else
{
strcat(buffer, "|");
strcat(buffer, append);
}
winpr_str_append(append, buffer, size, "|");
return buffer;
}
@ -588,7 +581,7 @@ static const char* flagsToStr(char* buffer, size_t size, DWORD flags)
append(buffer, size, "FILE_ATTRIBUTE_VIRTUAL");
_snprintf(strflags, sizeof(strflags), " [0x%08" PRIx32 "]", flags);
strcat(buffer, strflags);
winpr_str_append(strflags, buffer, size, NULL);
return buffer;
}

View File

@ -1047,103 +1047,47 @@ WINSCARDAPI const char* WINAPI SCardGetCardStateString(DWORD dwCardState)
WINSCARDAPI char* WINAPI SCardGetReaderStateString(DWORD dwReaderState)
{
char* szReaderState = malloc(512);
const size_t size = 512;
char* buffer = calloc(size, sizeof(char));
if (!szReaderState)
if (!buffer)
return NULL;
szReaderState[0] = '\0';
if (dwReaderState & SCARD_STATE_IGNORE)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_IGNORE");
}
winpr_str_append("SCARD_STATE_IGNORE", buffer, size, "|");
if (dwReaderState & SCARD_STATE_CHANGED)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_CHANGED");
}
winpr_str_append("SCARD_STATE_CHANGED", buffer, size, "|");
if (dwReaderState & SCARD_STATE_UNKNOWN)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_UNKNOWN");
}
winpr_str_append("SCARD_STATE_UNKNOWN", buffer, size, "|");
if (dwReaderState & SCARD_STATE_UNAVAILABLE)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_UNAVAILABLE");
}
winpr_str_append("SCARD_STATE_UNAVAILABLE", buffer, size, "|");
if (dwReaderState & SCARD_STATE_EMPTY)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_EMPTY");
}
winpr_str_append("SCARD_STATE_EMPTY", buffer, size, "|");
if (dwReaderState & SCARD_STATE_PRESENT)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_PRESENT");
}
winpr_str_append("SCARD_STATE_PRESENT", buffer, size, "|");
if (dwReaderState & SCARD_STATE_ATRMATCH)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_ATRMATCH");
}
winpr_str_append("SCARD_STATE_ATRMATCH", buffer, size, "|");
if (dwReaderState & SCARD_STATE_EXCLUSIVE)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_EXCLUSIVE");
}
winpr_str_append("SCARD_STATE_EXCLUSIVE", buffer, size, "|");
if (dwReaderState & SCARD_STATE_INUSE)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_INUSE");
}
winpr_str_append("SCARD_STATE_INUSE", buffer, size, "|");
if (dwReaderState & SCARD_STATE_MUTE)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
strcat(szReaderState, "SCARD_STATE_MUTE");
}
winpr_str_append("SCARD_STATE_MUTE", buffer, size, "|");
if (dwReaderState & SCARD_STATE_UNPOWERED)
{
if (szReaderState[0])
strcat(szReaderState, " | ");
winpr_str_append("SCARD_STATE_UNPOWERED", buffer, size, "|");
strcat(szReaderState, "SCARD_STATE_UNPOWERED");
}
if (!szReaderState[0])
strcat(szReaderState, "SCARD_STATE_UNAWARE");
return szReaderState;
if (!buffer[0])
winpr_str_append("SCARD_STATE_UNAWARE", buffer, size, "|");
return buffer;
}

View File

@ -1309,13 +1309,13 @@ char* ntlm_negotiate_flags_string(char* buffer, size_t size, UINT32 flags)
{
if (size - len < 1)
break;
strcat(buffer, "|");
winpr_str_append("|", buffer, size, NULL);
len++;
}
if (size - len < flen)
break;
strcat(buffer, str);
winpr_str_append(str, buffer, size, NULL);
}
}