Unifiy string append functions

This commit is contained in:
akallabeth 2022-06-03 10:25:30 +02:00 committed by akallabeth
parent aff8fdac26
commit f25261e271
15 changed files with 129 additions and 189 deletions

View File

@ -25,54 +25,33 @@
#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;
WINPR_ASSERT(buffer || (size == 0));
WINPR_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

@ -1929,7 +1929,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

@ -507,12 +507,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

@ -28,6 +28,8 @@
#include <winpr/assert.h>
#include <winpr/collections.h>
#include <freerdp/utils/string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
@ -474,18 +476,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

@ -216,6 +216,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;
@ -223,14 +224,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

@ -256,20 +256,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

@ -45,11 +45,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" },
@ -122,12 +124,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));
@ -136,18 +134,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

@ -1090,36 +1090,26 @@ BOOL nego_process_negotiation_request(rdpNego* nego, wStream* s)
* @param nego
* @param s
*/
static void pipecat(char* buffer, BOOL* first, const char* what)
{
WINPR_ASSERT(first);
if (!*first)
strcat(buffer, "|");
*first = FALSE;
strcat(buffer, what);
}
static const char* nego_rdp_neg_rsp_flags_str(UINT32 flags)
{
BOOL first = TRUE;
static char buffer[1024] = { 0 };
_snprintf(buffer, ARRAYSIZE(buffer), "[0x%02" PRIx32 "] ", flags);
if (flags & EXTENDED_CLIENT_DATA_SUPPORTED)
pipecat(buffer, &first, "EXTENDED_CLIENT_DATA_SUPPORTED");
winpr_str_append("EXTENDED_CLIENT_DATA_SUPPORTED", buffer, sizeof(buffer), "|");
if (flags & DYNVC_GFX_PROTOCOL_SUPPORTED)
pipecat(buffer, &first, "DYNVC_GFX_PROTOCOL_SUPPORTED");
winpr_str_append("DYNVC_GFX_PROTOCOL_SUPPORTED", buffer, sizeof(buffer), "|");
if (flags & RDP_NEGRSP_RESERVED)
pipecat(buffer, &first, "RDP_NEGRSP_RESERVED");
winpr_str_append("RDP_NEGRSP_RESERVED", buffer, sizeof(buffer), "|");
if (flags & RESTRICTED_ADMIN_MODE_SUPPORTED)
pipecat(buffer, &first, "RESTRICTED_ADMIN_MODE_SUPPORTED");
winpr_str_append("RESTRICTED_ADMIN_MODE_SUPPORTED", buffer, sizeof(buffer), "|");
if (flags & REDIRECTED_AUTHENTICATION_MODE_SUPPORTED)
pipecat(buffer, &first, "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)))
pipecat(buffer, &first, "UNKNOWN");
winpr_str_append("UNKNOWN", buffer, sizeof(buffer), "|");
return buffer;
}

View File

@ -31,6 +31,13 @@
#include "../core/rdp.h"
char* utils_str_append(char* buffer, size_t size, const char* what)
{
if (!winpr_str_append(what, buffer, size, "|"))
return NULL;
return buffer;
}
BOOL utils_str_copy(const char* value, char** dst)
{
WINPR_ASSERT(dst);
@ -59,8 +66,9 @@ auth_status utils_authenticate_gateway(freerdp* instance, rdp_auth_reason reason
if (freerdp_shall_disconnect_context(instance->context))
return AUTH_FAILED;
if (!settings->GatewayPassword || !settings->GatewayUsername ||
!strlen(settings->GatewayPassword) || !strlen(settings->GatewayUsername))
if (utils_str_is_empty(freerdp_settings_get_string(settings, FreeRDP_GatewayPassword)))
prompt = TRUE;
if (utils_str_is_empty(freerdp_settings_get_string(settings, FreeRDP_GatewayUsername)))
prompt = TRUE;
if (!prompt)
@ -102,7 +110,7 @@ auth_status utils_authenticate(freerdp* instance, rdp_auth_reason reason, BOOL o
return AUTH_FAILED;
/* Ask for auth data if no or an empty username was specified or no password was given */
if (utils_str_is_empty(settings->Username) ||
if (utils_str_is_empty(freerdp_settings_get_string(settings, FreeRDP_Username)) ||
(settings->Password == NULL && settings->RedirectionPassword == NULL))
prompt = TRUE;

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

@ -348,7 +348,8 @@ static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 form
if (formatId == ClipboardGetFormatId(clipboard, "text/html"))
{
INT64 SrcSize = (INT64)*pSize;
const INT64 SrcSize = (INT64)*pSize;
const size_t DstSize = SrcSize + 200;
char* body;
char num[20] = { 0 };
@ -378,12 +379,12 @@ static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 form
}
}
pDstData = (char*)calloc(1, SrcSize + 200);
pDstData = (char*)calloc(1, DstSize);
if (!pDstData)
goto fail;
sprintf_s(pDstData, SrcSize + 200,
sprintf_s(pDstData, DstSize,
"Version:0.9\r\n"
"StartHTML:0000000000\r\n"
"EndHTML:0000000000\r\n"
@ -395,29 +396,42 @@ static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 form
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;
}
if (!winpr_str_append("<!--StartFragment-->", pDstData, DstSize, NULL))
goto fail;
strcat(pDstData, "<!--StartFragment-->");
/* StartFragment */
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
CopyMemory(&pDstData[69], num, 10);
strcat(pDstData, pSrcData.cpc);
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>");
{
if (!winpr_str_append("</BODY></HTML>", pDstData, DstSize, NULL))
goto fail;
}
/* EndHTML */
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, SrcSize + 200));
sprintf_s(num, sizeof(num), "%010" PRIuz "", strnlen(pDstData, DstSize));
CopyMemory(&pDstData[43], num, 10);
*pSize = (UINT32)strlen(pDstData) + 1;
*pSize = (UINT32)strnlen(pDstData, DstSize) + 1;
}
fail:
free(pSrcData.pv);

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

@ -22,6 +22,7 @@
#include <winpr/config.h>
#include <winpr/crt.h>
#include <winpr/string.h>
#include <winpr/path.h>
#include <winpr/file.h>
@ -555,15 +556,7 @@ BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFil
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;
}
@ -602,7 +595,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

@ -1057,103 +1057,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 (!buffer[0])
winpr_str_append("SCARD_STATE_UNAWARE", buffer, size, "|");
if (!szReaderState[0])
strcat(szReaderState, "SCARD_STATE_UNAWARE");
return szReaderState;
return buffer;
}

View File

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