2 * PROJECT: ReactOS TXT to NLS Converter
3 * LICENSE: GNU General Public License Version 2.0 or any later version
4 * FILE: devutils/txt2nls/nls.c
5 * COPYRIGHT: Copyright 2016 Dmitry Chapyshev <dmitry@reactos.org>
10 #define _NLS_DEBUG_PRINT
12 #ifdef _NLS_DEBUG_PRINT
15 nls_print_header(NLS_FILE_HEADER
*header
)
20 printf("CodePage: %u\n", header
->CodePage
);
21 printf("Character size: %u\n", header
->MaximumCharacterSize
);
22 printf("Default char: 0x%02X\n", header
->DefaultChar
);
23 printf("Default unicode char: 0x%04X\n", header
->UniDefaultChar
);
24 printf("Trans default char: 0x%02X\n", header
->TransUniDefaultChar
);
25 printf("Trans default unicode char: 0x%04X\n", header
->TransUniDefaultChar
);
27 for (i
= 0; i
< MAXIMUM_LEADBYTES
; i
++)
29 printf("LeadByte[%u] = 0x%02X\n", i
, header
->LeadByte
[i
]);
36 nls_print_mb_table(uint16_t *mb_table
, uint16_t uni_default_char
)
42 for (ch
= 0; ch
<= 0xFF; ch
++)
44 if (mb_table
[ch
] != uni_default_char
)
46 printf("0x%02X 0x%04X\n", (unsigned int)ch
, (unsigned int)mb_table
[ch
]);
54 nls_print_wc_table(uint16_t *wc_table
, uint16_t default_char
, int is_dbcs
)
60 for (ch
= 0; ch
<= 0xFFFF; ch
++)
65 uint16_t *table
= (uint16_t*)wc_table
;
67 if (table
[ch
] != default_char
)
68 printf("0x%04X 0x%04X\n", (unsigned int)ch
, (unsigned int)table
[ch
]);
73 uint8_t *table
= (uint8_t*)wc_table
;
75 if (table
[ch
] != default_char
)
76 printf("0x%04X 0x%02X\n", (unsigned int)ch
, (unsigned int)table
[ch
]);
84 nls_print_glyph_table(uint16_t *glyph_table
, uint16_t uni_default_char
)
88 printf("GLYPHTABLE:\n");
90 for (ch
= 0; ch
<= 0xFF; ch
++)
92 if (glyph_table
[ch
] != uni_default_char
)
94 printf("0x%02X 0x%04X\n", (unsigned int)ch
, (unsigned int)glyph_table
[ch
]);
101 #endif /* _NLS_DEBUG_PRINT */
104 nls_from_txt(const char *txt_file_path
, const char *nls_file_path
)
106 NLS_FILE_HEADER header
;
108 uint16_t *mb_table
= NULL
;
109 uint16_t *wc_table
= NULL
;
110 uint16_t *glyph_table
= NULL
;
111 uint16_t number_of_lb_ranges
;
116 memset(&header
, 0, sizeof(header
));
118 if (!txt_get_header(txt_file_path
, &header
))
121 is_dbcs
= (header
.MaximumCharacterSize
== 2) ? 1 : 0;
123 mb_table
= txt_get_mb_table(txt_file_path
, header
.UniDefaultChar
);
127 wc_table
= txt_get_wc_table(txt_file_path
, header
.DefaultChar
, is_dbcs
);
131 /* GLYPHTABLE optionally. We do not leave if it is absent */
132 glyph_table
= txt_get_glyph_table(txt_file_path
, header
.UniDefaultChar
);
137 uint16_t *table
= (uint16_t*)wc_table
;
138 header
.TransUniDefaultChar
= table
[header
.UniDefaultChar
];
139 /* TODO: TransDefaultChar for DBCS codepages */
144 uint8_t *table
= (uint8_t*)wc_table
;
145 header
.TransUniDefaultChar
= table
[header
.UniDefaultChar
];
146 header
.TransDefaultChar
= mb_table
[LOBYTE(header
.DefaultChar
)];
149 #ifdef _NLS_DEBUG_PRINT
150 nls_print_header(&header
);
151 nls_print_mb_table(mb_table
, header
.UniDefaultChar
);
153 nls_print_glyph_table(glyph_table
, header
.UniDefaultChar
);
154 nls_print_wc_table(wc_table
, header
.DefaultChar
, is_dbcs
);
155 #endif /* _NLS_DEBUG_PRINT */
157 /* Create binary file with write access */
158 file
= fopen(nls_file_path
, "wb");
161 printf("Unable to create NLS file.\n");
165 /* Write NLS file header */
166 if (fwrite(&header
, 1, sizeof(header
), file
) != sizeof(header
))
168 printf("Unable to write NLS file.\n");
172 size
= (256 * sizeof(uint16_t)) + /* Primary CP to Unicode table */
173 sizeof(uint16_t) + /* optional OEM glyph table size in words */
174 (glyph_table
? (256 * sizeof(uint16_t)) : 0) + /* OEM glyph table size in words * sizeof(uint16_t) */
175 sizeof(uint16_t) + /* Number of DBCS LeadByte ranges */
176 0 + /* offsets of lead byte sub tables */
177 0 + /* LeadByte sub tables */
178 sizeof(uint16_t); /* Unknown flag */
180 size
/= sizeof(uint16_t);
182 if (fwrite(&size
, 1, sizeof(size
), file
) != sizeof(size
))
184 printf("Unable to write NLS file.\n");
188 /* Write multibyte table */
189 if (fwrite(mb_table
, 1, (256 * sizeof(uint16_t)), file
) != (256 * sizeof(uint16_t)))
191 printf("Unable to write NLS file.\n");
195 /* OEM glyph table size in words */
196 size
= (glyph_table
? 256 : 0);
198 if (fwrite(&size
, 1, sizeof(size
), file
) != sizeof(size
))
200 printf("Unable to write NLS file.\n");
206 /* Write OEM glyph table */
207 if (fwrite(glyph_table
, 1, (256 * sizeof(uint16_t)), file
) != (256 * sizeof(uint16_t)))
209 printf("Unable to write NLS file.\n");
214 /* Number of DBCS LeadByte ranges */
215 number_of_lb_ranges
= 0;
216 if (fwrite(&number_of_lb_ranges
, 1, sizeof(number_of_lb_ranges
), file
) != sizeof(number_of_lb_ranges
))
218 printf("Unable to write NLS file.\n");
224 if (fwrite(&size
, 1, sizeof(size
), file
) != sizeof(size
))
226 printf("Unable to write NLS file.\n");
230 /* Write wide char table */
231 if (fwrite(wc_table
, 1, (65536 * header
.MaximumCharacterSize
), file
) != (65536 * header
.MaximumCharacterSize
))
233 printf("Unable to write NLS file.\n");
240 if (file
) fclose(file
);