ReactOS  0.4.14-dev-49-gfb4591c
bestfit.c File Reference
#include "precomp.h"
Include dependency graph for bestfit.c:

Go to the source code of this file.

Functions

static HANDLE BestFit_CreateFile (const WCHAR *pszFile)
 
static VOID BestFit_CloseFile (HANDLE hFile)
 
static CHARUTF8fromUNICODE (const WCHAR *pszInput, PSIZE_T Size)
 
static VOID BestFit_Write (HANDLE hFile, const WCHAR *pszFormat,...)
 
BOOL BestFit_FromNLS (const WCHAR *pszNLSFile, const WCHAR *pszBestFitFile)
 

Function Documentation

◆ BestFit_CloseFile()

static VOID BestFit_CloseFile ( HANDLE  hFile)
static

Definition at line 33 of file bestfit.c.

34 {
36 }
#define CloseHandle
Definition: compat.h:398
_In_ HANDLE hFile
Definition: mswsock.h:90

Referenced by BestFit_FromNLS().

◆ BestFit_CreateFile()

static HANDLE BestFit_CreateFile ( const WCHAR pszFile)
static

Definition at line 11 of file bestfit.c.

12 {
13  DWORD dwBytesWritten;
14  HANDLE hFile;
15 
16  hFile = CreateFileW(pszFile,
19  NULL,
22  NULL);
24  {
25  /* Write UTF-8 BOM */
26  WriteFile(hFile, "\xEF\xBB\xBF", 3, &dwBytesWritten, NULL);
27  }
28 
29  return hFile;
30 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_READ
Definition: compat.h:125
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
_In_ HANDLE hFile
Definition: mswsock.h:90
#define CREATE_ALWAYS
Definition: disk.h:72
#define CreateFileW
Definition: compat.h:400

Referenced by BestFit_FromNLS().

◆ BestFit_FromNLS()

BOOL BestFit_FromNLS ( const WCHAR pszNLSFile,
const WCHAR pszBestFitFile 
)

Definition at line 113 of file bestfit.c.

114 {
116  PUSHORT CodePage;
117  HANDLE hFile;
118  USHORT CodePageChar;
119  ULONG UnicodeChar;
120 
121  CodePage = NLS_ReadFile(pszNLSFile, &CodePageTable);
122  if (CodePage == NULL)
123  return FALSE;
124 
125  hFile = BestFit_CreateFile(pszBestFitFile);
127  {
128  free(CodePage);
129  return FALSE;
130  }
131 
132  /* The only field is the decimal windows code page number for this code page. */
133  BestFit_Write(hFile, L"CODEPAGE %u\r\n\r\n", CodePageTable.CodePage);
134 
136  L"CPINFO %u 0x%02X 0x%04X\r\n\r\n",
137  /* "1" for a single byte code page, "2" for a double byte code page */
139  /* Replacement characters for unassigned Unicode code points when
140  written to this code page */
142  /* Replacement characters for illegal or unassigned code page values
143  when converting to Unicode. */
145 
146  /* This field contains the number of following records of code page to Unicode mappings. */
148 
149  for (CodePageChar = 0; CodePageChar <= 0xFF; CodePageChar++)
150  {
151  if (!NLS_IsDefaultCharForMB(&CodePageTable, CodePageChar))
152  {
153  WCHAR szCharName[MAX_STR_LEN] = { 0 };
154 
155  GetUName(CodePageTable.MultiByteTable[CodePageChar], szCharName);
156 
158  L"0x%02X 0x%04X ;%s\r\n",
159  CodePageChar,
160  CodePageTable.MultiByteTable[CodePageChar],
161  szCharName);
162  }
163  }
164 
165  BestFit_Write(hFile, L"\r\n");
166 
168  {
169  PUSHORT GlyphTable = CodePageTable.MultiByteTable + 256 + 1;
170 
172 
173  for (CodePageChar = 0; CodePageChar <= 0xFF; CodePageChar++)
174  {
175  if (CodePageChar != CodePageTable.UniDefaultChar)
176  {
177  WCHAR szCharName[MAX_STR_LEN] = { 0 };
178 
179  GetUName(GlyphTable[CodePageChar], szCharName);
180 
182  L"0x%02X 0x%04X ;%s\r\n",
183  CodePageChar,
184  GlyphTable[CodePageChar],
185  szCharName);
186  }
187  }
188 
189  BestFit_Write(hFile, L"\r\n");
190  }
191 
193  {
194  PUSHORT LeadByteRanges = (PUSHORT)&CodePageTable.LeadByte[0];
195  USHORT Index;
196  USHORT LeadByte;
197 
199  L"DBCSRANGE %u ;%u DBCS Lead Byte Ranges\r\n\r\n",
202 
203  for (Index = 0; Index < MAXIMUM_LEADBYTES / 2; Index++)
204  {
205  if (!LeadByteRanges[Index])
206  continue;
207 
209  L"0x%X 0x%X ;Lead Byte Range %u\r\n\r\n",
210  LOBYTE(LeadByteRanges[Index]),
211  HIBYTE(LeadByteRanges[Index]),
212  Index + 1);
213 
214  for (LeadByte = LOBYTE(LeadByteRanges[Index]);
215  LeadByte <= HIBYTE(LeadByteRanges[Index]);
216  LeadByte++)
217  {
218  PUSHORT LeadByteInfo = CodePageTable.DBCSOffsets;
219 
221  L"DBCSTABLE %u ;Range = %u, LeadByte = 0x%02X\r\n\r\n",
223  Index + 1,
224  LeadByte);
225 
226  for (CodePageChar = 0; CodePageChar <= 0xFF; CodePageChar++)
227  {
228  USHORT Info = LeadByteInfo[LeadByte];
229 
230  if (Info && LeadByteInfo[Info + CodePageChar] != CodePageTable.UniDefaultChar)
231  {
233  L"0x%02X 0x%04X\r\n",
234  CodePageChar,
235  LeadByteInfo[Info + CodePageChar]);
236  }
237  }
238 
239  BestFit_Write(hFile, L"\r\n");
240  }
241  }
242  }
243 
244  /* This field contains the number of records of Unicode to byte mappings. */
246 
247  for (UnicodeChar = 0; UnicodeChar <= 0xFFFF; UnicodeChar++)
248  {
249  if (!NLS_IsDefaultCharForUnicode(&CodePageTable, UnicodeChar))
250  {
251  WCHAR szCharName[MAX_STR_LEN] = { 0 };
252 
253  GetUName(UnicodeChar, szCharName);
254 
256  {
257  PUSHORT MultiByteTable = (PUSHORT)CodePageTable.WideCharTable;
258 
260  L"0x%04X 0x%04X ;%s\r\n",
261  UnicodeChar,
262  MultiByteTable[UnicodeChar],
263  szCharName);
264  }
265  else
266  {
267  PUCHAR SingleByteTable = (PUCHAR)CodePageTable.WideCharTable;
268 
270  L"0x%04X 0x%02X ;%s\r\n",
271  UnicodeChar,
272  SingleByteTable[UnicodeChar],
273  szCharName);
274  }
275  }
276  }
277 
278  /* This tag marks the end of the code page data. Anything after this marker is ignored. */
279  BestFit_Write(hFile, L"\r\nENDCODEPAGE\r\n");
280 
282  free(CodePage);
283 
284  return TRUE;
285 }
USHORT NLS_RecordsCountForUnicodeTable(PCPTABLEINFO CodePageTable)
Definition: nls.c:149
USHORT NLS_RecordsCountForMBTable(PCPTABLEINFO CodePageTable)
Definition: nls.c:134
#define TRUE
Definition: types.h:120
USHORT UniDefaultChar
Definition: precomp.h:35
PUSHORT DBCSOffsets
Definition: precomp.h:43
#define LOBYTE(W)
Definition: jmemdos.c:487
PUSHORT MultiByteTable
Definition: precomp.h:40
PUSHORT DBCSRanges
Definition: precomp.h:42
unsigned char * PUCHAR
Definition: retypes.h:3
#define free
Definition: debug_ros.c:5
BOOL NLS_IsDefaultCharForMB(PCPTABLEINFO CodePageTable, UCHAR Char)
Definition: nls.c:103
#define HIBYTE(W)
Definition: jmemdos.c:486
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
struct TraceInfo Info
PVOID WideCharTable
Definition: precomp.h:41
USHORT NLS_RecordsCountForDBCSTable(PCPTABLEINFO CodePageTable, UCHAR LeadByte)
Definition: nls.c:186
smooth NULL
Definition: ftsmooth.c:416
BOOL NLS_IsGlyphTablePresent(PCPTABLEINFO CodePageTable)
Definition: nls.c:97
UCHAR LeadByte[MAXIMUM_LEADBYTES]
Definition: precomp.h:39
__wchar_t WCHAR
Definition: xmlstorage.h:180
BOOL NLS_IsDefaultCharForUnicode(PCPTABLEINFO CodePageTable, USHORT Char)
Definition: nls.c:112
USHORT MaximumCharacterSize
Definition: precomp.h:33
static const UCHAR Index[8]
Definition: usbohci.c:18
static HANDLE BestFit_CreateFile(const WCHAR *pszFile)
Definition: bestfit.c:11
USHORT CodePage
Definition: precomp.h:32
static const WCHAR L[]
Definition: oid.c:1250
#define MAXIMUM_LEADBYTES
Definition: precomp.h:16
USHORT DefaultChar
Definition: precomp.h:34
USHORT NLS_RecordsCountForGlyphTable(PCPTABLEINFO CodePageTable)
Definition: nls.c:164
_In_ HANDLE hFile
Definition: mswsock.h:90
unsigned short USHORT
Definition: pedump.c:61
PUSHORT NLS_ReadFile(const WCHAR *pszFile, PCPTABLEINFO CodePageTable)
Definition: nls.c:58
int WINAPI GetUName(IN WORD wCharCode, OUT LPWSTR lpBuf)
Definition: getuname.c:17
#define MAX_STR_LEN
Definition: defines.h:33
BOOL NLS_IsDBCSCodePage(PCPTABLEINFO CodePageTable)
Definition: nls.c:91
unsigned int ULONG
Definition: retypes.h:1
_Out_ PCPTABLEINFO CodePageTable
Definition: rtlfuncs.h:4113
static VOID BestFit_Write(HANDLE hFile, const WCHAR *pszFormat,...)
Definition: bestfit.c:60
static VOID BestFit_CloseFile(HANDLE hFile)
Definition: bestfit.c:33
unsigned short * PUSHORT
Definition: retypes.h:2

Referenced by wmain().

◆ BestFit_Write()

static VOID BestFit_Write ( HANDLE  hFile,
const WCHAR pszFormat,
  ... 
)
static

Definition at line 60 of file bestfit.c.

61 {
63  LARGE_INTEGER MoveTo;
64  LARGE_INTEGER NewPos;
65  DWORD dwBytesWritten;
66 
68  return;
69 
70  MoveTo.QuadPart = 0;
71  if (!SetFilePointerEx(hFile, MoveTo, &NewPos, FILE_END))
72  return;
73 
75  return;
76 
78  {
80  CHAR *pszUtf8;
81  va_list Args;
82  SIZE_T Size;
83 
84  va_start(Args, pszFormat);
85 
86  Size = (_vscwprintf(pszFormat, Args) + 1) * sizeof(WCHAR);
87  pszString = (WCHAR*) malloc(Size);
88 
89  if (!pszString)
90  {
92  va_end(Args);
93  return;
94  }
95 
96  StringCbVPrintfW(pszString, Size, pszFormat, Args);
97  va_end(Args);
98 
99  pszUtf8 = UTF8fromUNICODE(pszString, &Size);
100  if (pszUtf8)
101  {
102  WriteFile(hFile, pszUtf8, Size - sizeof(CHAR), &dwBytesWritten, NULL);
103  free(pszUtf8);
104  }
105 
106  free(pszString);
107 
109  }
110 }
BOOL WINAPI SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
Definition: fileinfo.c:327
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
char CHAR
Definition: xmlstorage.h:175
#define free
Definition: debug_ros.c:5
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
STRSAFEAPI StringCbVPrintfW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszFormat, va_list argList)
Definition: strsafe.h:507
#define va_end(ap)
Definition: acmsvcex.h:90
BOOL WINAPI UnlockFile(IN HANDLE hFile, IN DWORD dwFileOffsetLow, IN DWORD dwFileOffsetHigh, IN DWORD nNumberOfBytesToUnlockLow, IN DWORD nNumberOfBytesToUnlockHigh)
Definition: lock.c:142
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
char ** Args
Definition: acdebug.h:343
#define FILE_END
Definition: winbase.h:114
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GetFileSizeEx
Definition: compat.h:414
static CHAR * UTF8fromUNICODE(const WCHAR *pszInput, PSIZE_T Size)
Definition: bestfit.c:39
_Must_inspect_result_ _Out_ PLARGE_INTEGER FileSize
Definition: fsrtlfuncs.h:108
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
uint32_t DWORD_PTR
Definition: typedefs.h:63
_In_ HANDLE hFile
Definition: mswsock.h:90
BOOL WINAPI LockFile(IN HANDLE hFile, IN DWORD dwFileOffsetLow, IN DWORD dwFileOffsetHigh, IN DWORD nNumberOfBytesToLockLow, IN DWORD nNumberOfBytesToLockHigh)
Definition: lock.c:25
ULONG_PTR SIZE_T
Definition: typedefs.h:78
_Check_return_ _CRTIMP int __cdecl _vscwprintf(_In_z_ _Printf_format_string_ const wchar_t *_Format, va_list _ArgList)
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define malloc
Definition: debug_ros.c:4
LONGLONG QuadPart
Definition: typedefs.h:112
_In_ DWORD _In_ DWORD _Out_writes_to_opt_ pcchString LPSTR pszString
Definition: wincrypt.h:4509

Referenced by BestFit_FromNLS().

◆ UTF8fromUNICODE()

static CHAR* UTF8fromUNICODE ( const WCHAR pszInput,
PSIZE_T  Size 
)
static

Definition at line 39 of file bestfit.c.

40 {
41  ULONG Length;
42  CHAR *pszOutput;
43 
44  if (!pszInput || !Size) return NULL;
45 
46  Length = WideCharToMultiByte(CP_UTF8, 0, pszInput, -1, NULL, 0, NULL, NULL);
47 
48  *Size = Length * sizeof(CHAR);
49 
50  pszOutput = (CHAR *) malloc(*Size);
51  if (pszOutput)
52  {
53  WideCharToMultiByte(CP_UTF8, 0, pszInput, -1, pszOutput, Length, NULL, NULL);
54  }
55 
56  return pszOutput;
57 }
#define WideCharToMultiByte
Definition: compat.h:101
char CHAR
Definition: xmlstorage.h:175
#define CP_UTF8
Definition: nls.h:20
smooth NULL
Definition: ftsmooth.c:416
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
unsigned int ULONG
Definition: retypes.h:1
#define malloc
Definition: debug_ros.c:4
#define CHAR(Char)

Referenced by BestFit_Write().