ReactOS  0.4.15-dev-3324-gda4e15f
IsTextUnicode.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Tests for (Rtl)IsTextUnicode.
5  * PROGRAMMERS: Hermes Belusca-Maito
6  * Dmitry Chapyshev
7  */
8 
9 #include "precomp.h"
10 
11 #include <stdio.h>
12 
14 {
15  char filename[MAX_PATH], sysdir[MAX_PATH];
16  HANDLE hFile;
17  PVOID Data = NULL;
19 
20  if (Code != -1)
21  StringCbPrintfA(filename, sizeof(filename), "%s\\c_%lu.nls", sysdir, Code);
22  else
23  StringCbPrintfA(filename, sizeof(filename), "%s\\l_intl.nls", sysdir);
24 
27  {
28  DWORD dwRead;
31  ReadFile(hFile, Data, dwFileSize, &dwRead, NULL);
33  }
34  return Data;
35 }
36 
37 /* https://www.microsoft.com/resources/msdn/goglobal/default.mspx */
38 void SetupLocale(ULONG AnsiCode, ULONG OemCode, ULONG Unicode)
39 {
40  NLSTABLEINFO NlsTable;
41  PVOID AnsiCodePageData;
42  PVOID OemCodePageData;
43  PVOID UnicodeCaseTableData;
44 
45  AnsiCodePageData = LoadCodePageData(AnsiCode);
46  OemCodePageData = LoadCodePageData(OemCode);
47  UnicodeCaseTableData = LoadCodePageData(Unicode);
48 
49  RtlInitNlsTables(AnsiCodePageData, OemCodePageData, UnicodeCaseTableData, &NlsTable);
50  RtlResetRtlTranslations(&NlsTable);
51  /* Do NOT free the buffers here, they are directly used!
52  Yes, we leak the old buffers, but this is a test anyway... */
53 
54 }
55 
57 {
58 #define INVALID_FLAG 0xFFFFFFFF
59 
60 #define NEW_TEST(Buffer, Flags, ResultFlags, Success) \
61  { (PVOID)(Buffer), sizeof((Buffer)), (Flags), (ResultFlags), (Success) }
62 
63  static struct
64  {
65  /* Input */
66  PVOID Buffer;
67  INT Size;
68  INT Flags;
69 
70  /* Output */
71  INT ResultFlags;
72  BOOL Success;
73  } Tests[] =
74  {
75  /* ANSI string */
76 
77  // 0
78  NEW_TEST("ANSI string", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
79  NEW_TEST("ANSI string", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
80  NEW_TEST("ANSI string", INVALID_FLAG, 0, FALSE),
81 
82  /* UNICODE strings */
83 
84  // 3
88  NEW_TEST(L"a", INVALID_FLAG, 0, TRUE),
89 
90  // 7
91  NEW_TEST(L"UNICODE String 0", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
94  NEW_TEST(L"UNICODE String 0", INVALID_FLAG, 0, TRUE),
95 
96  // 11
97  NEW_TEST(L"\xFEFF" L"UNICODE String 1", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
99  NEW_TEST(L"\xFEFF" L"UNICODE String 1", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
100  NEW_TEST(L"\xFEFF" L"UNICODE String 1", INVALID_FLAG, 0, TRUE),
101 
102  // 15
103  NEW_TEST(L"\xFFFE" L"UNICODE String 2", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
105  NEW_TEST(L"\xFFFE" L"UNICODE String 2", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
106  NEW_TEST(L"\xFFFE" L"UNICODE String 2", INVALID_FLAG, 0, FALSE),
107 
108  // 19
109  NEW_TEST(L"UNICODE String 3 Привет!", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
111  NEW_TEST(L"UNICODE String 3 Привет!", IS_TEXT_UNICODE_STATISTICS, IS_TEXT_UNICODE_STATISTICS, TRUE),
112  NEW_TEST(L"UNICODE String 3 Привет!", INVALID_FLAG, 0, TRUE),
113 
114  // 23
115  NEW_TEST(L"\xFEFF" L"UNICODE String 4 Привет!", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
117  NEW_TEST(L"\xFEFF" L"UNICODE String 4 Привет!", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
118  NEW_TEST(L"\xFEFF" L"UNICODE String 4 Привет!", INVALID_FLAG, 0, TRUE),
119 
120  // 27
121  NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", IS_TEXT_UNICODE_ASCII16, 0, FALSE),
122  NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", IS_TEXT_UNICODE_UNICODE_MASK, IS_TEXT_UNICODE_CONTROLS, TRUE),
123  NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", IS_TEXT_UNICODE_STATISTICS, 0, FALSE),
124  NEW_TEST(L"\xFFFE" L"UNICODE String 5 Привет!", INVALID_FLAG, 0, FALSE),
125 
126  // 31
127  /* Reverse BOM */
128  NEW_TEST(L"UNICODE S" L"\xFFFE" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
129  /* UNICODE_NUL */
130  NEW_TEST(L"UNICODE S" L"\x0000" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
131  /* ASCII CRLF (packed into one word) */
132  NEW_TEST(L"UNICODE S" L"\x0A0D" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
133  /* Unicode 0xFFFF */
134  NEW_TEST(L"UNICODE S" L"\xFFFF" L"tring 5 Привет!", IS_TEXT_UNICODE_ILLEGAL_CHARS, IS_TEXT_UNICODE_ILLEGAL_CHARS, FALSE),
135 
136  // 35
137  NEW_TEST(L"UNICODE String 0", IS_TEXT_UNICODE_DBCS_LEADBYTE, 0, FALSE)
138  };
139 
140  const char japanese_with_lead[] = "ABC" "\x83\x40" "D";
141  const char simplfied_chinese_with_lead[] = "ABC" "\xC5\xC5" "D";
142  const char korean_with_lead[] = "ABC" "\xBF\xAD" "D";
143  const char traditional_chinese_with_lead[] = "ABC" "\xB1\xC1" "D";
144 
145  UINT i;
146  BOOL Success;
147  INT Result;
148 
149  for (i = 0; i < ARRAYSIZE(Tests); ++i)
150  {
151  Result = Tests[i].Flags;
153  ok(Success == Tests[i].Success, "IsTextUnicode(%u) returned 0x%x, expected %s\n", i, Success, (Tests[i].Success ? "TRUE" : "FALSE"));
154  if (Result != INVALID_FLAG)
155  ok(Result == Tests[i].ResultFlags, "IsTextUnicode(%u) Result returned 0x%x, expected 0x%x\n", i, Result, Tests[i].ResultFlags);
156  }
157 
158  /* Japanese */
159  SetupLocale(932, 932, -1);
160 
162  ok(!IsTextUnicode(japanese_with_lead, sizeof(japanese_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
163  ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
164 
165  /* Simplified Chinese */
166  SetupLocale(936, 936, -1);
167 
169  ok(!IsTextUnicode(simplfied_chinese_with_lead, sizeof(simplfied_chinese_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
170  ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
171 
172  /* Korean */
173  SetupLocale(949, 949, -1);
174 
176  ok(!IsTextUnicode(korean_with_lead, sizeof(korean_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
177  ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
178 
179  /* Traditional Chinese */
180  SetupLocale(950, 950, -1);
181 
183  ok(!IsTextUnicode(traditional_chinese_with_lead, sizeof(traditional_chinese_with_lead), &Result), "IsTextUnicode() returned TRUE, expected FALSE\n");
184  ok(Result == IS_TEXT_UNICODE_DBCS_LEADBYTE, "Result returned 0x%x, expected 0x%x\n", Result, IS_TEXT_UNICODE_DBCS_LEADBYTE);
185 }
NTSYSAPI VOID NTAPI RtlResetRtlTranslations(_In_ PNLSTABLEINFO NlsTable)
#define CloseHandle
Definition: compat.h:598
#define IS_TEXT_UNICODE_DBCS_LEADBYTE
Definition: winnt_old.h:930
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define TRUE
Definition: types.h:120
void SetupLocale(ULONG AnsiCode, ULONG OemCode, ULONG Unicode)
Definition: IsTextUnicode.c:38
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG _In_opt_ PVOID Data
Definition: wdfdevice.h:4527
DWORD dwFileSize
Definition: more.c:40
UINT WINAPI GetSystemDirectoryA(OUT LPSTR lpBuffer, IN UINT uSize)
Definition: path.c:2282
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
const char * filename
Definition: ioapi.h:135
int32_t INT
Definition: typedefs.h:58
#define FILE_SHARE_READ
Definition: compat.h:136
#define IS_TEXT_UNICODE_SIGNATURE
Definition: winnt_old.h:926
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
#define IS_TEXT_UNICODE_STATISTICS
Definition: winnt_old.h:922
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
Definition: bufpool.h:45
#define OPEN_EXISTING
Definition: compat.h:634
NTSYSAPI VOID NTAPI RtlInitNlsTables(_In_ PUSHORT AnsiTableBase, _In_ PUSHORT OemTableBase, _In_ PUSHORT CaseTableBase, _Out_ PNLSTABLEINFO NlsTable)
BOOL WINAPI IsTextUnicode(IN CONST VOID *lpv, IN INT iSize, IN OUT LPINT lpiResult OPTIONAL)
Definition: unicode.c:27
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
STRSAFEAPI StringCbPrintfA(STRSAFE_LPSTR pszDest, size_t cbDest, STRSAFE_LPCSTR pszFormat,...)
Definition: strsafe.h:547
PVOID LoadCodePageData(ULONG Code)
Definition: IsTextUnicode.c:13
#define MAX_PATH
Definition: compat.h:34
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
#define NEW_TEST(Buffer, Flags, ResultFlags, Success)
static const WCHAR L[]
Definition: oid.c:1250
_In_ UCHAR _In_ UCHAR _In_ ULONG Code
Definition: wdfdevice.h:1697
#define GENERIC_READ
Definition: compat.h:135
_In_ HANDLE hFile
Definition: mswsock.h:90
#define IS_TEXT_UNICODE_CONTROLS
Definition: winnt_old.h:924
#define IS_TEXT_UNICODE_ILLEGAL_CHARS
Definition: winnt_old.h:928
START_TEST(IsTextUnicode)
Definition: IsTextUnicode.c:56
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
IN OUT PVCB OUT PDIRENT OUT PBCB IN BOOLEAN CreateFile
Definition: fatprocs.h:913
#define INVALID_FLAG
#define ok(value,...)
Definition: atltest.h:57
#define ReadFile(a, b, c, d, e)
Definition: compat.h:601
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
unsigned int ULONG
Definition: retypes.h:1
#define malloc
Definition: debug_ros.c:4
#define IS_TEXT_UNICODE_ASCII16
Definition: winnt_old.h:920
#define IS_TEXT_UNICODE_UNICODE_MASK
Definition: winnt_old.h:932
struct test_data Tests[]