I wrote this function that opens a pipe and passes a command to that pipe to be executed, when examining the buffers I noticed that some commands take 1 byte other take 2 bytes, that being said trying to output the buffer to the screen causes some of the commands to be outputted as Mojibake
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int test(wchar_t * command)
{
int i =0;
wchar_t psBuffer[128];
FILE *pPipe;
if( (pPipe = _wpopen( command, L"rb" )) == NULL )
exit( 1 );
wchar_t c;
while ( (c = fgetwc(pPipe)) != WEOF && i < 128)
{
psBuffer[i] = c;
wprintf(L"%04X ",c);
i++;
}
psBuffer[i] = L'\0';
wprintf(L"\n%S", psBuffer);
if (feof( pPipe))
_pclose( pPipe );
return 0;
}
int main()
{
_setmode(fileno(stdout), _O_U8TEXT);
test(L"C:\\Windows\\System32\\cmd.exe /u /c dir /b");
wprintf(L"\n");
test(L"C:\\Windows\\System32\\cmd.exe /u /c getmac ");
return 0;
}
Output
0061 002E 0065 0078 0065 000D 000A 006D 0061 0069 006E 002E 0063 000D 000A 043F 0440 0438 0432 0435 0442 000D 000A
a.exe
main.c
привет
0A0D 6850 7379 6369 6C61 4120 6464 6572 7373 2020 2020 7254 6E61 7073 726F 2074 614E 656D 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 0A0D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 203D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 0A0D 3235 352D 2D34 3030 382D 2D41 4433 382D 2036 2020 445C 7665 6369 5C65 6354 6970 5F70 377B 3344 3135 3441 2D34 3342 3834 342D 3330 2D44 3438 4431 462D 3733 3634 4146 3037 3743 7D33 2020 2020 2020 0A0D 3235 352D 2D34 3030 342D 2D30 3430
桐獹捩污䄠摤敲獳††牔湡灳牯⁴慎敭††††††††††††††††††††††㴽㴽㴽㴽㴽㴽㴽㴽㴽‽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㈵㔭ⴴ〰㠭ⵁ䐳㠭‶†䑜癥捩履捔楰彰㝻㍄ㄵ㑁ⴴ㍂㠴㐭㌰ⵄ㐸䐱䘭㜳㘴䅆〷㝃紳†††
㈵㔭ⴴ〰㐭ⴰ㐰
As you can see each letter/number in the output of the first command is output as a 4 digit hex value while the second command groups the hex value of two letters/numbers into a 4 digit hex value which causes the text to be Mojibake
How can I fix this problem so that the second command buffer would look like the first one
Update
int test(wchar_t * command)
{
int i =0, k=0;
wchar_t psBuffer[64];
FILE *fd;
if( (fd = _wpopen( command, L"rt" )) == NULL )
exit( 1 );
_setmode(fileno(fd), _O_TEXT);
int x = fread(psBuffer, sizeof(wchar_t), 64, fd);
for ( i =0; i < x; i++)
{
wprintf(L"%02X ", psBuffer[i]);
}
psBuffer[x] = L'\0';
wprintf(L"%S\n", psBuffer);
if (feof(fd))
_pclose(fd);
return 0;
}
int main()
{
_setmode(fileno(stdout), _O_U16TEXT);
test(L"C:\\Windows\\System32\\cmd.exe /u /c dir /b");
wprintf(L"\n");
test(L"C:\\Windows\\System32\\cmd.exe /u /c getmac ");
return 0;
}
reading from stream as text with mode set to _O_TEXT
does not solve the issue because the output of the second command is still a 4 place hex value for each two letters in the result string, plus the order of hex value is backwards where P
=50
is before LF
=0A
61 2E 65 78 65 0D 0A 6D 61 69 6E 2E 63 0D 0A 6D 61 69 6E 32 2E 63 0D 0A 43F 440 438 432 435 442 0D 0A
a.exe
main.c
main2.c
привет
500A 7968 6973 6163 206C 6441 7264 7365 2073 2020 5420 6172 736E 6F70 7472 4E20 6D61 2065 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 A20 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 203D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D 3D3D
倊票楳慣摁牤獥†吠慲獮潰瑲丠浡†††††††††††††††††††††ਠ㴽㴽㴽㴽㴽㴽㴽㴽㴽‽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽㴽