Skip to content

Commit 210ca30

Browse files
cosmo0920edsiper
authored andcommitted
in_winevtlog: Align string type coherency
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 2ba4827 commit 210ca30

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

plugins/in_winevtlog/pack.c

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,76 @@ static int pack_nullstr(struct winevtlog_config *ctx)
3737
return flb_log_event_encoder_append_body_cstring(ctx->log_encoder, "");
3838
}
3939

40-
static int pack_wstr(struct winevtlog_config *ctx, const wchar_t *wstr)
40+
static int pack_str_codepage(struct winevtlog_config *ctx, const wchar_t *wstr,
41+
UINT code_page, BOOL use_ansi)
4142
{
42-
int size;
43-
char *buf;
44-
UINT code_page = CP_UTF8;
45-
const char *defaultChar = " ";
43+
UINT cp = use_ansi ? CP_ACP : code_page;
44+
DWORD flags = (cp == CP_UTF8) ? WC_ERR_INVALID_CHARS : 0;
45+
int size = 0;
46+
char *buf = NULL;
4647

4748
if (!wstr) {
4849
return -1;
4950
}
5051

51-
if (ctx->use_ansi) {
52-
code_page = CP_ACP;
53-
}
54-
55-
/* Compute the buffer size first */
56-
size = WideCharToMultiByte(code_page, 0, wstr, -1, NULL, 0, NULL, NULL);
52+
size = WideCharToMultiByte(cp, flags, wstr, -1, NULL, 0, NULL, NULL);
5753
if (size == 0) {
5854
return -1;
5955
}
6056

6157
buf = flb_malloc(size);
62-
if (buf == NULL) {
58+
if (!buf) {
6359
flb_errno();
60+
6461
return -1;
6562
}
6663

67-
/* Convert UTF-16 into UTF-8 */
68-
size = WideCharToMultiByte(code_page, 0, wstr, -1, buf, size, defaultChar, NULL);
69-
if (size == 0) {
64+
if (WideCharToMultiByte(cp, flags, wstr, -1, buf, size, NULL, NULL) == 0) {
7065
flb_free(buf);
7166
return -1;
7267
}
7368

74-
/* Pack buf except the trailing '\0' */
7569
flb_log_event_encoder_append_body_string(ctx->log_encoder, buf, size - 1);
76-
7770
flb_free(buf);
7871
return 0;
7972
}
8073

74+
static int pack_wstr(struct winevtlog_config *ctx, const wchar_t *wstr)
75+
{
76+
return pack_str_codepage(ctx, wstr, CP_UTF8, ctx->use_ansi);
77+
}
78+
79+
static int pack_astr(struct winevtlog_config *ctx, const char *astr)
80+
{
81+
wchar_t *wbuf = NULL;
82+
int wlen = 0;
83+
int ret;
84+
85+
if (!astr) {
86+
return -1;
87+
}
88+
89+
wlen = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, astr, -1, NULL, 0);
90+
if (wlen == 0) {
91+
return -1;
92+
}
93+
94+
wbuf = flb_malloc(sizeof(wchar_t) * wlen);
95+
if (!wbuf) {
96+
flb_errno();
97+
return -1;
98+
}
99+
100+
if (MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, astr, -1, wbuf, wlen) == 0) {
101+
flb_free(wbuf);
102+
return -1;
103+
}
104+
105+
ret = pack_wstr(ctx, wbuf);
106+
flb_free(wbuf);
107+
return ret;
108+
}
109+
81110
static int pack_binary(struct winevtlog_config *ctx, PBYTE bin, size_t length)
82111
{
83112
const char *HEX_TABLE = "0123456789ABCDEF";
@@ -392,7 +421,7 @@ static void pack_string_inserts(struct winevtlog_config *ctx, PEVT_VARIANT value
392421
}
393422
break;
394423
case EvtVarTypeAnsiString:
395-
if (pack_wstr(ctx, values[i].AnsiStringVal)) {
424+
if (pack_astr(ctx, values[i].AnsiStringVal)) {
396425
pack_nullstr(ctx);
397426
}
398427
break;

0 commit comments

Comments
 (0)