Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: MIT
- // SPDX-FileCopyrightText: 2024 SASANO Takayoshi <[email protected]>
- // cc create_ucs2w16.c -Wall -I/usr/local/include -L/usr/local/lib -liconv -o create_ucs2w16
- #include <stdio.h>
- #include <string.h>
- #include <iconv.h>
- #define SS2 0x8e
- #define SS3 0x8f
- #define TABLE_SIZE 65536
- static unsigned short ucs2tow16[TABLE_SIZE];
- static unsigned short w16toucs2[TABLE_SIZE];
- static int create_table(void)
- {
- iconv_t cd;
- int ucs, w16;
- unsigned char in[2], out[4];
- char *in_p, *out_p;
- ssize_t n;
- size_t insize, outsize;
- memset(ucs2tow16, 0, sizeof(ucs2tow16));
- memset(w16toucs2, 0, sizeof(w16toucs2));
- if ((cd = iconv_open("EUC-JP", "UCS-2BE")) < 0) {
- printf("iconv_open\n");
- return -1;
- }
- for (ucs = 0; ucs < 0x10000; ucs++) {
- /* skip C0/C1 control, surrogate, private area */
- if ((ucs >= 0x0000 && ucs <= 0x001f) ||
- (ucs >= 0x0080 && ucs <= 0x009f) ||
- (ucs >= 0xd800 && ucs <= 0xdfff) ||
- (ucs >= 0xe000 && ucs <= 0xf8ff))
- continue;
- in[0] = ucs >> 8;
- in[1] = ucs;
- in_p = (char *)in;
- out_p = (char *)out;
- insize = sizeof(in);
- outsize = sizeof(out);
- if ((n = iconv(cd, &in_p, &insize, &out_p, &outsize)) < 0)
- continue;
- switch (out[0]) {
- case 0x00 ... 0x7f:
- w16 = out[0];
- break;
- case SS2:
- w16 = out[1];
- break;
- case SS3:
- w16 = (out[1] << 8) | (out[2] & 0x7f);
- break;
- default:
- w16 = (out[0] << 8) | out[1];
- break;
- }
- w16toucs2[w16] = ucs;
- ucs2tow16[ucs] = w16;
- }
- iconv_close(cd);
- return 0;
- }
- static void dump_table(FILE *fp, uint16_t *table, int size)
- {
- int i;
- for (i = 0; i < size; i++) {
- if (!(i % 8)) fprintf(fp, "\t");
- fprintf(fp, "0x%04x, ", table[i]);
- if (!((i + 1) % 8)) fprintf(fp, "\n");
- }
- }
- static int output_result(char *filename)
- {
- FILE *fp;
- if ((fp = fopen(filename, "w")) == NULL)
- return -1;
- fprintf(fp, "// SPDX-License-Identifier: Unlicense\n\n");
- fprintf(fp, "const unsigned short ucs2tow16[%d] = {\n", TABLE_SIZE);
- dump_table(fp, ucs2tow16, TABLE_SIZE);
- fprintf(fp, "};\n\n");
- fprintf(fp, "const unsigned short w16toucs2[%d] = {\n", TABLE_SIZE);
- dump_table(fp, w16toucs2, TABLE_SIZE);
- fprintf(fp, "};\n\n");
- fclose(fp);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- if (argc < 2) {
- printf("%s: usage [outfile]\n", argv[0]);
- goto fin0;
- }
- if (create_table())
- goto fin0;
- output_result(argv[1]);
- fin0:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement