Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
RatinCN committed Nov 7, 2024
1 parent 40869c8 commit 09a16da
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Source/KNSoft.MakeLifeEasier/KNSoft.MakeLifeEasier.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<ClCompile Include="Resource.Embedded.rc.g.c" />
<ClCompile Include="Shell\Shell.c" />
<ClCompile Include="String\Convert.cpp" />
<ClCompile Include="String\Encoding.c" />
<ClCompile Include="String\Hash.cpp" />
<ClCompile Include="System\Info.c" />
<ClCompile Include="System\Library.c" />
Expand Down Expand Up @@ -221,6 +222,7 @@
<ClInclude Include="Shell\Shell.h" />
<ClInclude Include="String\Core.h" />
<ClInclude Include="String\Convert.h" />
<ClInclude Include="String\Encoding.h" />
<ClInclude Include="String\Hash.h" />
<ClInclude Include="System\Info.h" />
<ClInclude Include="System\Library.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
<ClCompile Include="PE\Resource.c">
<Filter>PE</Filter>
</ClCompile>
<ClCompile Include="String\Encoding.c">
<Filter>String</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="MakeLifeEasier.inl" />
Expand Down Expand Up @@ -239,6 +242,9 @@
<ClInclude Include="PE\Resource.h">
<Filter>PE</Filter>
</ClInclude>
<ClInclude Include="String\Encoding.h">
<Filter>String</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.Embedded.rc" />
Expand Down
1 change: 1 addition & 0 deletions Source/KNSoft.MakeLifeEasier/MakeLifeEasier.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ typedef _Return_type_success_(return == 0) ULONG W32ERROR;
#include "Process/Sync.h"
#include "Time/Time.h"
#include "String/Convert.h"
#include "String/Encoding.h"
#include "String/Hash.h"
#include "System/Library.h"
#include "System/Registry.h"
Expand Down
60 changes: 60 additions & 0 deletions Source/KNSoft.MakeLifeEasier/NT/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,66 @@

EXTERN_C_START

#pragma region String

/* See also RtlInitUnicodeStringEx */
FORCEINLINE
NTSTATUS
NT_InitStringW(
_Out_ PUNICODE_STRING NTString,
_In_ PWSTR String)
{
SIZE_T Size;

if (String != NULL)
{
Size = wcslen(String) * sizeof(WCHAR);
if (Size > (MAXUSHORT & ~1) - sizeof(WCHAR))
{
return STATUS_NAME_TOO_LONG;
}
NTString->Length = (USHORT)Size;
NTString->MaximumLength = (USHORT)Size + sizeof(WCHAR);
}
else
{
NTString->Length = NTString->MaximumLength = 0;
}

NTString->Buffer = (PWCHAR)String;
return STATUS_SUCCESS;
}

/* See also RtlInitAnsiStringEx */
FORCEINLINE
NTSTATUS
NT_InitStringA(
_Out_ PANSI_STRING NTString,
_In_ PSTR String)
{
SIZE_T Size;

if (String != NULL)
{
Size = strlen(String);
if (Size > (MAXUSHORT - sizeof(CHAR)))
{
return STATUS_NAME_TOO_LONG;
}
NTString->Length = (USHORT)Size;
NTString->MaximumLength = (USHORT)Size + sizeof(CHAR);
}
else
{
NTString->Length = NTString->MaximumLength = 0;
}

NTString->Buffer = (PCHAR)String;
return STATUS_SUCCESS;
}

#pragma endregion

#pragma region Path

/* See also InitializeObjectAttributes */
Expand Down
1 change: 1 addition & 0 deletions Source/KNSoft.MakeLifeEasier/String/Encoding.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../MakeLifeEasier.inl"
136 changes: 136 additions & 0 deletions Source/KNSoft.MakeLifeEasier/String/Encoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#pragma once

#include "../MakeLifeEasier.h"

EXTERN_C_START

FORCEINLINE
ULONG
Str_UnicodeToAnsi(
_Out_writes_z_(BufferCount) PSTR Buffer,
_In_ ULONG BufferCount,
_In_ PCWSTR UnicodeString)
{
ULONG BufferSize, TranslatedBytes, iCch;

BufferSize = BufferCount * sizeof(Buffer[0]);
TranslatedBytes = 0;
RtlUnicodeToMultiByteN(Buffer,
BufferSize,
&TranslatedBytes,
UnicodeString,
(ULONG)Str_SizeW(UnicodeString));
if (TranslatedBytes == 0 || TranslatedBytes >= BufferSize)
{
return 0;
}

iCch = TranslatedBytes / sizeof(CHAR);
Buffer[iCch] = ANSI_NULL;
return iCch;
}

FORCEINLINE
ULONG
Str_AnsiToUnicode(
_Out_writes_z_(BufferCount) PWSTR Buffer,
_In_ ULONG BufferCount,
_In_ PCSTR AnsiString)
{
ULONG BufferSize, TranslatedBytes, iCch;

BufferSize = BufferCount * sizeof(Buffer[0]);
TranslatedBytes = 0;
RtlMultiByteToUnicodeN(Buffer,
BufferSize,
&TranslatedBytes,
AnsiString,
(ULONG)Str_SizeA(AnsiString));
if (TranslatedBytes == 0 || TranslatedBytes >= BufferSize)
{
return 0;
}

iCch = TranslatedBytes / sizeof(WCHAR);
Buffer[iCch] = UNICODE_NULL;
return iCch;
}

FORCEINLINE
ULONG
Str_UnicodeToUtf8(
_Out_writes_opt_z_(BufferCount) PSTR Buffer,
_In_opt_ ULONG BufferCount,
_In_ PCWSTR UnicodeString)
{
NTSTATUS Status;
ULONG BufferSize, TranslatedBytes, iCch;

BufferSize = BufferCount * sizeof(Buffer[0]);
if (Buffer == NULL)
{
Status = RtlUnicodeToUTF8N(NULL,
0,
&TranslatedBytes,
UnicodeString,
(ULONG)Str_SizeW(UnicodeString));
return NT_SUCCESS(Status) ? TranslatedBytes + sizeof(ANSI_NULL) : 0;
}

Status = RtlUnicodeToUTF8N(Buffer,
BufferSize,
&TranslatedBytes,
UnicodeString,
(ULONG)Str_SizeW(UnicodeString));
if (!NT_SUCCESS(Status) || TranslatedBytes == 0 || TranslatedBytes >= BufferSize)
{
return 0;
}

iCch = TranslatedBytes / sizeof(CHAR);
Buffer[iCch] = ANSI_NULL;
return iCch;
}

FORCEINLINE
ULONG
Str_Utf8ToUnicode(
_Out_writes_opt_z_(BufferCount) PWSTR Buffer,
_In_opt_ ULONG BufferCount,
_In_ PCSTR Utf8String)
{
NTSTATUS Status;
ULONG BufferSize, TranslatedBytes, iCch;

BufferSize = BufferCount * sizeof(Buffer[0]);
if (Buffer == NULL)
{
Status = RtlUTF8ToUnicodeN(NULL,
0,
&TranslatedBytes,
Utf8String,
(ULONG)Str_SizeA(Utf8String));
return NT_SUCCESS(Status) ? TranslatedBytes + sizeof(UNICODE_NULL) : 0;
}

Status = RtlUTF8ToUnicodeN(Buffer,
BufferCount * sizeof(Buffer[0]),
&TranslatedBytes,
Utf8String,
(ULONG)Str_SizeA(Utf8String));
if (!NT_SUCCESS(Status) || TranslatedBytes == 0 || TranslatedBytes >= BufferSize)
{
return 0;
}

iCch = TranslatedBytes / sizeof(WCHAR);
Buffer[iCch] = UNICODE_NULL;
return iCch;
}

#define Str_W2A(ADest, WSrc) Str_UnicodeToAnsi(ADest, ARRAYSIZE(ADest), WSrc)
#define Str_A2W(WDest, ASrc) Str_AnsiToUnicode(WDest, ARRAYSIZE(WDest), ASrc)
#define Str_W2U(UDest, WSrc) Str_UnicodeToUtf8(UDest, ARRAYSIZE(UDest), WSrc)
#define Str_U2W(WDest, USrc) Str_Utf8ToUnicode(WDest, ARRAYSIZE(WDest), USrc)

EXTERN_C_END
38 changes: 38 additions & 0 deletions Source/KNSoft.MakeLifeEasier/UI/GDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,42 @@ UI_GetFontInfo(

#pragma endregion

#pragma region Paint

FORCEINLINE
LOGICAL
GDI_FillSolidRect(
_In_ HDC DC,
_In_ PRECT Rect,
_In_ COLORREF Color)
{
COLORREF OldColor = SetBkColor(DC, Color);
LOGICAL Ret = ExtTextOutW(DC, 0, 0, ETO_OPAQUE, Rect, NULL, 0, NULL);
SetBkColor(DC, OldColor);
return Ret;
}

FORCEINLINE
LOGICAL
GDI_FrameRect(
_In_ HDC DC,
_In_ PRECT Rect,
_In_ INT Width,
_In_ DWORD ROP)
{
return Width >= 0
?
PatBlt(DC, Rect->left - Width, Rect->top - Width, Width, Rect->bottom - Rect->top + Width * 2, ROP) &&
PatBlt(DC, Rect->right, Rect->top - Width, Width, Rect->bottom - Rect->top + Width * 2, ROP) &&
PatBlt(DC, Rect->left, Rect->top - Width, Rect->right - Rect->left, Width, ROP) &&
PatBlt(DC, Rect->left, Rect->bottom, Rect->right - Rect->left, Width, ROP)
:
PatBlt(DC, Rect->left, Rect->top, -Width, Rect->bottom - Rect->top, ROP) &&
PatBlt(DC, Rect->right + Width, Rect->top, -Width, Rect->bottom - Rect->top, ROP) &&
PatBlt(DC, Rect->left - Width, Rect->top, Rect->right - Rect->left + Width * 2, -Width, ROP) &&
PatBlt(DC, Rect->left - Width, Rect->bottom + Width, Rect->right - Rect->left + Width * 2, -Width, ROP);
}

#pragma endregion

EXTERN_C_END

0 comments on commit 09a16da

Please sign in to comment.