📄 idnconv.c
字号:
if (local_ace_hack) { actions1 = IDN_IDNCONV; if (flags & FLAG_ROUNDTRIPCHECK) actions1 |= IDN_RTCHECK; } else { actions1 = IDN_LOCALCONV; } actions2 = IDN_IDNCONV; if (flags & FLAG_DELIMMAP) actions2 |= IDN_DELIMMAP; if (flags & FLAG_LOCALMAP) actions2 |= IDN_LOCALMAP; if (flags & FLAG_MAP) actions2 |= IDN_MAP; if (flags & FLAG_NORMALIZE) actions2 |= IDN_NORMALIZE; if (flags & FLAG_PROHIBITCHECK) actions2 |= IDN_PROHCHECK; if (flags & FLAG_UNASSIGNCHECK) actions2 |= IDN_UNASCHECK; if (flags & FLAG_BIDICHECK) actions2 |= IDN_BIDICHECK; if (flags & FLAG_ASCIICHECK) actions2 |= IDN_ASCCHECK; if (flags & FLAG_LENGTHCHECK) actions2 |= IDN_LENCHECK; strbuf_init(&buf1); strbuf_init(&buf2); line_number = 1; while (strbuf_getline(&buf1, fp) != NULL) { /* * Trim newline at the end. This is needed for * those ascii-comatible encodings such as UTF-5 or RACE * not to try converting newlines, which will result * in `invalid encoding' error. */ nl_trimmed = trim_newline(&buf1); /* * Convert input line to UTF-8. */ if (local_ace_hack) r = convert_line(&buf1, &buf2, conf2, actions1, FLAG_REVERSE|FLAG_SELECTIVE); else r = convert_line(&buf1, &buf2, conf1, actions1, 0); if (r != idn_success) { errormsg("conversion failed at line %d: %s\n", line_number, idn_result_tostring(r)); goto error; } if (!idn_utf8_isvalidstring(strbuf_get(&buf2))) { errormsg("conversion to utf-8 failed at line %d\n", line_number); goto error; } /* * Perform local mapping and NAMEPREP, and convert to * the output codeset. */ r = convert_line(&buf2, &buf1, conf1, actions2, flags & FLAG_SELECTIVE); if (r != idn_success) { errormsg("error in nameprep or output conversion " "at line %d: %s\n", line_number, idn_result_tostring(r)); goto error; } fputs(strbuf_get(&buf1), stdout); if (nl_trimmed) putc('\n', stdout); if (flush_every_line) fflush(stdout); line_number++; } strbuf_reset(&buf1); strbuf_reset(&buf2); return (0); error: strbuf_reset(&buf1); strbuf_reset(&buf2); return (1);}static intdecode_file(idn_resconf_t conf1, idn_resconf_t conf2, FILE *fp, int flags) { idn_result_t r; idnconv_strbuf_t buf1, buf2; idn_action_t actions1, actions2; int nl_trimmed; int local_ace_hack, idn_ace_hack; idn_converter_t conv; /* * See if the input codeset is an ACE. */ conv = idn_resconf_getidnconverter(conf1); if (conv != NULL && idn_converter_isasciicompatible(conv) && (flags & FLAG_SELECTIVE)) idn_ace_hack = 1; else idn_ace_hack = 0; if (conv != NULL) idn_converter_destroy(conv); conv = idn_resconf_getlocalconverter(conf1); if (conv != NULL && idn_converter_isasciicompatible(conv) && (flags & FLAG_SELECTIVE)) local_ace_hack = 1; else local_ace_hack = 0; if (conv != NULL) idn_converter_destroy(conv); actions1 = IDN_IDNCONV; if (local_ace_hack) { actions2 = IDN_IDNCONV; if (flags & FLAG_MAP) actions2 |= IDN_MAP; if (flags & FLAG_NORMALIZE) actions2 |= IDN_NORMALIZE; if (flags & FLAG_PROHIBITCHECK) actions2 |= IDN_PROHCHECK; if (flags & FLAG_UNASSIGNCHECK) actions2 |= IDN_UNASCHECK; if (flags & FLAG_BIDICHECK) actions2 |= IDN_BIDICHECK; if (flags & FLAG_ASCIICHECK) actions2 |= IDN_ASCCHECK; if (flags & FLAG_LENGTHCHECK) actions2 |= IDN_LENCHECK; } else { actions2 = IDN_LOCALCONV; } if (flags & FLAG_DELIMMAP) actions1 |= IDN_DELIMMAP; if (flags & FLAG_MAP) actions1 |= IDN_MAP; if (flags & FLAG_NORMALIZE) actions1 |= IDN_NORMALIZE; if (flags & FLAG_NORMALIZE) actions1 |= IDN_NORMALIZE; if (flags & FLAG_PROHIBITCHECK) actions1 |= IDN_PROHCHECK; if (flags & FLAG_UNASSIGNCHECK) actions1 |= IDN_UNASCHECK; if (flags & FLAG_BIDICHECK) actions1 |= IDN_BIDICHECK; if (flags & FLAG_ASCIICHECK) actions1 |= IDN_ASCCHECK; if (flags & FLAG_ROUNDTRIPCHECK) actions1 |= IDN_RTCHECK; strbuf_init(&buf1); strbuf_init(&buf2); line_number = 1; while (strbuf_getline(&buf1, fp) != NULL) { /* * Trim newline at the end. This is needed for * those ascii-comatible encodings such as UTF-5 or RACE * not to try converting newlines, which will result * in `invalid encoding' error. */ nl_trimmed = trim_newline(&buf1); /* * Treat input line as the string encoded in local * encoding and convert it to UTF-8 encoded string. */ if (local_ace_hack) { if (strbuf_copy(&buf2, strbuf_get(&buf1)) == NULL) r = idn_nomemory; else r = idn_success; } else { r = convert_line(&buf1, &buf2, conf1, IDN_LOCALCONV, 0); } if (r != idn_success) { errormsg("conversion failed at line %d: %s\n", line_number, idn_result_tostring(r)); goto error; } /* * Convert internationalized domain names in the line. */ if (idn_ace_hack) { r = convert_line(&buf2, &buf1, conf1, actions1, FLAG_REVERSE|FLAG_SELECTIVE); } else { r = convert_line(&buf2, &buf1, conf1, actions1, FLAG_REVERSE); } if (r != idn_success) { errormsg("conversion failed at line %d: %s\n", line_number, idn_result_tostring(r)); goto error; } if (!idn_utf8_isvalidstring(strbuf_get(&buf1))) { errormsg("conversion to utf-8 failed at line %d\n", line_number); goto error; } /* * Perform round trip check and convert to the output * codeset. */ if (local_ace_hack) { r = convert_line(&buf1, &buf2, conf2, actions2, FLAG_SELECTIVE); } else { r = convert_line(&buf1, &buf2, conf1, actions2, FLAG_REVERSE); } if (r != idn_success) { errormsg("error in nameprep or output conversion " "at line %d: %s\n", line_number, idn_result_tostring(r)); goto error; } fputs(strbuf_get(&buf2), stdout); if (nl_trimmed) putc('\n', stdout); if (flush_every_line) fflush(stdout); line_number++; } strbuf_reset(&buf1); strbuf_reset(&buf2); return (0); error: strbuf_reset(&buf1); strbuf_reset(&buf2); return (1);}static inttrim_newline(idnconv_strbuf_t *buf) { /* * If the string in BUF ends with a newline, trim it and * return 1. Otherwise, just return 0 without modifying BUF. */ char *s = strbuf_get(buf); size_t len = strlen(s); if (s[len - 1] == '\n') { s[len - 1] = '\0'; return (1); } return (0);}static idn_result_tconvert_line(idnconv_strbuf_t *from, idnconv_strbuf_t *to, idn_resconf_t conf, idn_action_t actions, int flags){ idn_result_t r = idn_success; char *from_str = strbuf_get(from); for (;;) { char *to_str = strbuf_get(to); size_t to_size = strbuf_size(to); switch (flags & (FLAG_REVERSE|FLAG_SELECTIVE)) { case 0: r = idn_res_encodename(conf, actions, from_str, to_str, to_size); break; case FLAG_REVERSE: r = idn_res_decodename(conf, actions, from_str, to_str, to_size); break; case FLAG_SELECTIVE: r = selective_encode(conf, actions, from_str, to_str, to_size); break; case FLAG_REVERSE|FLAG_SELECTIVE: r = selective_decode(conf, actions, from_str, to_str, to_size); break; } if (r == idn_buffer_overflow) { /* * Conversion is not successful because * the size of the target buffer is not enough. * Double the size and retry. */ if (strbuf_double(to) == NULL) { /* oops. allocation failed. */ return (idn_nomemory); } } else { break; } } return (r);}static char *options[] = { "-in INPUT-CODESET : specifies input codeset name.", "-i INPUT-CODESET : synonym for -in", "-out OUTPUT-CODESET : specifies output codeset name.", "-o OUTPUT-CODESET : synonym for -out", "-conf CONF-FILE : specifies idnkit configuration file.", "-c CONF-FILE : synonym for -conf", "-noconf : do not load idnkit configuration file.", "-C : synonym for -noconf", "-reverse : specifies reverse conversion.", " (i.e. IDN encoding to local encoding)", "-r : synonym for -reverse", "-nameprep VERSION : specifies version name of NAMEPREP.", "-n VERSION : synonym for -nameprep", "-nonameprep : do not perform NAMEPREP.", "-N : synonym for -nonameprep", "-localmap MAPPING : specifies local mapping.", "-nolocalmap : do not perform local mapping.", "-L : synonym for -nolocalmap", "-nounassigncheck : do not perform unassigned codepoint check.", "-U : synonym for -nounassigncheck", "-nobidicheck : do not perform bidirectional text check.", "-B : synonym for -nobidicheck", "-nolengthcheck : do not check label length.", "-noasciicheck : do not check ASCII range characters.", "-A : synonym for -noasciicheck", "-noroundtripcheck : do not perform round trip check.", "-delimiter U+XXXX : specifies local delimiter code point.", "-alias alias-file : specifies codeset alias file.", "-a : synonym for -alias", "-flush : line-buffering mode.", "-whole : convert the whole region instead of", " regions containing non-ascii characters.", "-w : synonym for -whole", "-version : print version number, then exit.", "-v : synonym for -version", "", " The following options can be specified multiple times", " -localmap, -delimiter", NULL,};static voidprint_version() { fprintf(stderr, "idnconv (idnkit) version: %s\n" "library version: %s\n", IDNKIT_VERSION, idn_version_getstring()); exit(0);}static voidprint_usage(char *cmd) { int i; fprintf(stderr, "Usage: %s [options..] [file]\n", cmd); for (i = 0; options[i] != NULL; i++) fprintf(stderr, "\t%s\n", options[i]); exit(1);}static unsigned longget_ucs(const char *p) { unsigned long v; char *end; /* Skip optional 'U+' */ if (strncmp(p, "U+", 2) == 0) p += 2; v = strtoul(p, &end, 16); if (*end != '\0') { fprintf(stderr, "invalid UCS code point \"%s\"\n", p); exit(1); } return v;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -