📄 res.c
字号:
if (r != idn_success) goto ret; /* * Perform conversions and tests. */ for (l = labellist_tail(labels); l != NULL; l = labellist_previous(l)) { free(saved_name); saved_name = NULL; if (!idn__util_ucs4isasciirange(labellist_getname(l))) { if (actions & IDN_MAP) { r = label_map(ctx, l); if (r != idn_success) goto ret; } if (actions & IDN_NORMALIZE) { r = label_normalize(ctx, l); if (r != idn_success) goto ret; } if (actions & IDN_PROHCHECK) { r = label_prohcheck(ctx, l); if (r == idn_prohibited) { labellist_undo(l); continue; } else if (r != idn_success) { goto ret; } } if (actions & IDN_UNASCHECK) { r = label_unascheck(ctx, l); if (r == idn_prohibited) { labellist_undo(l); continue; } else if (r != idn_success) { goto ret; } } if (actions & IDN_BIDICHECK) { r = label_bidicheck(ctx, l); if (r == idn_prohibited) { labellist_undo(l); continue; } else if (r != idn_success) { goto ret; } } } if ((actions & IDN_IDNCONV) && idn_is_ace) { saved_name = idn_ucs4_strdup(labellist_getname(l)); if (saved_name == NULL) { r = idn_nomemory; goto ret; } r = label_idndecode(ctx, l); if (r == idn_invalid_encoding) { labellist_undo(l); continue; } else if (r != idn_success) { goto ret; } } if ((actions & IDN_RTCHECK) && saved_name != NULL) { r = label_rtcheck(ctx, actions, l, saved_name); if (r == idn_invalid_encoding) { labellist_undo(l); continue; } else if (r != idn_success) { goto ret; } }#ifndef WITHOUT_ICONV if (actions & IDN_LOCALCONV) { r = label_localdecodecheck(ctx, l); if (r != idn_success) goto ret; }#endif } /* * Concat a list of labels to a name. */ for (;;) { void *new_buffer; new_buffer = realloc(buffer, sizeof(*buffer) * buffer_length); if (new_buffer == NULL) { r = idn_nomemory; goto ret; } buffer = (unsigned long *)new_buffer; r = labellist_getnamelist(labels, buffer, buffer_length); if (r == idn_success) break; else if (r != idn_buffer_overflow) goto ret; buffer_length *= 2; } if (actions & IDN_LOCALCONV) { r = idn_converter_convfromucs4(local_converter, buffer, to, tolen); } else { r = idn_ucs4_ucs4toutf8(buffer, to, tolen); }ret: if (r == idn_success) { TRACE(("idn_res_decodename(): success (to=\"%s\")\n", idn__debug_xstring(to, 50))); } else { TRACE(("idn_res_decodename(): %s\n", idn_result_tostring(r))); } free(saved_name); free(buffer); if (local_converter != NULL) idn_converter_destroy(local_converter); if (idn_converter != NULL) idn_converter_destroy(idn_converter); if (labels != NULL) labellist_destroy(labels); return (r);}idn_result_tidn_res_decodename2(idn_resconf_t ctx, idn_action_t actions, const char *from, char *to, size_t tolen, const char *auxencoding) {#ifdef WITHOUT_ICONV return idn_failure;#else /* WITHOUT_ICONV */ idn_result_t r; idn_converter_t aux_converter = NULL; unsigned long *buffer_ucs4 = NULL; char *buffer_utf8 = NULL; size_t buffer_length; assert(ctx != NULL && from != NULL && to != NULL); TRACE(("idn_res_decodename2(actions=%s, from=\"%s\", tolen=%d, " "auxencoding=\"%s\")\n", idn__res_actionstostring(actions), idn__debug_xstring(from, 50), (int)tolen, (auxencoding != NULL) ? auxencoding : "<null>")); if (!initialized) idn_res_initialize(); if (!enabled || actions == 0) { r = copy_verbatim(from, to, tolen); goto ret; } else if (tolen <= 0) { r = idn_buffer_overflow; goto ret; } if (auxencoding == NULL || strcmp(auxencoding, IDN_UTF8_ENCODING_NAME) == 0 || strcmp(auxencoding, "UTF-8") == 0) { return idn_res_decodename(ctx, actions, from, to, tolen); } /* * Convert `from' to UCS4. */ r = idn_resconf_setauxidnconvertername(ctx, auxencoding, IDN_CONVERTER_DELAYEDOPEN); if (r != idn_success) { goto ret; } aux_converter = idn_resconf_getauxidnconverter(ctx); if (aux_converter == NULL) { r = idn_failure; goto ret; } buffer_length = tolen * 2; for (;;) { void *new_buffer; new_buffer = realloc(buffer_ucs4, sizeof(*buffer_ucs4) * buffer_length); if (new_buffer == NULL) { r = idn_nomemory; goto ret; } buffer_ucs4 = (unsigned long *)new_buffer; r = idn_converter_convtoucs4(aux_converter, from, buffer_ucs4, buffer_length); if (r == idn_success) break; else if (r != idn_buffer_overflow) goto ret; buffer_length *= 2; } if (*buffer_ucs4 == '\0') { if (tolen <= 0) { r = idn_buffer_overflow; goto ret; } *to = '\0'; r = idn_success; goto ret; } /* * Convert `buffer_ucs4' to UTF-8. */ buffer_length = tolen * 2; for (;;) { void *new_buffer; new_buffer = realloc(buffer_utf8, sizeof(*buffer_utf8) * buffer_length); if (new_buffer == NULL) { r = idn_nomemory; goto ret; } buffer_utf8 = (char *)new_buffer; r = idn_ucs4_ucs4toutf8(buffer_ucs4, buffer_utf8, buffer_length); if (r == idn_success) break; else if (r != idn_buffer_overflow) goto ret; buffer_length *= 2; } if (*buffer_utf8 == '\0') { if (tolen <= 0) { r = idn_buffer_overflow; goto ret; } *to = '\0'; r = idn_success; goto ret; } r = idn_res_decodename(ctx, actions, buffer_utf8, to, tolen);ret: if (r == idn_success) { TRACE(("idn_res_decodename2(): success (to=\"%s\")\n", idn__debug_xstring(to, 50))); } else { TRACE(("idn_res_decodename2(): %s\n", idn_result_tostring(r))); } free(buffer_ucs4); free(buffer_utf8); if (aux_converter != NULL) idn_converter_destroy(aux_converter); return (r);#endif /* WITHOUT_ICONV */}static idn_result_tcopy_verbatim(const char *from, char *to, size_t tolen) { size_t fromlen = strlen(from); if (fromlen + 1 > tolen) return (idn_buffer_overflow); (void)memcpy(to, from, fromlen + 1); return (idn_success);}static idn_result_tlabellist_create(const unsigned long *name, labellist_t *labelp) { size_t length, malloc_length; labellist_t head_label = NULL; labellist_t tail_label = NULL; labellist_t new_label = NULL; const unsigned long *endp = NULL; idn_result_t r; while (*name != '\0') { for (endp = name; *endp != '.' && *endp != '\0'; endp++) ; /* nothing to be done */ length = (endp - name) + 1; malloc_length = length + 15; /* add 15 for margin */ new_label = (labellist_t) malloc(sizeof(struct labellist)); if (new_label == NULL) { r = idn_nomemory; goto ret; } if (head_label == NULL) head_label = new_label; new_label->name = NULL; new_label->undo_name = NULL; new_label->name_length = malloc_length; new_label->next = NULL; new_label->previous = NULL; new_label->dot_followed = (*endp == '.'); new_label->name = (unsigned long *) malloc(sizeof(long) * malloc_length); if (new_label->name == NULL) { r = idn_nomemory; goto ret; } memcpy(new_label->name, name, sizeof(long) * length); *(new_label->name + length - 1) = '\0'; new_label->undo_name = (unsigned long *) malloc(sizeof(long) * malloc_length); if (new_label->undo_name == NULL) { r = idn_nomemory; goto ret; } memcpy(new_label->undo_name, name, sizeof(long) * length); *(new_label->undo_name + length - 1) = '\0'; if (tail_label != NULL) { tail_label->next = new_label; new_label->previous = tail_label; } tail_label = new_label; if (*endp == '.') name = endp + 1; else name = endp; } *labelp = head_label; r = idn_success;ret: if (r != idn_success) { if (new_label != NULL) { free(new_label->name); free(new_label->undo_name); free(new_label); } if (head_label != NULL) labellist_destroy(head_label); } return (r);}static voidlabellist_destroy(labellist_t label) { labellist_t l, l_next; for (l = label; l != NULL; l = l_next) { l_next = l->next; free(l->name); free(l->undo_name); free(l); }}static idn_result_tlabellist_setname(labellist_t label, const unsigned long *name) { unsigned long *new_name; size_t length, new_length; length = idn_ucs4_strlen(name) + 1; new_length = length + 15; /* add 15 for margin */ if (label->name_length < new_length) { new_name = (unsigned long *) realloc(label->name, sizeof(long) * new_length); if (new_name == NULL) return (idn_nomemory); label->name = new_name; label->name_length = new_length; } memcpy(label->name, name, sizeof(long) * length); return (idn_success);}static const unsigned long *labellist_getname(labellist_t label) { return (label->name);}static const unsigned long *labellist_gettldname(labellist_t label) { labellist_t l; if (label->previous == NULL && label->next == NULL && !label->dot_followed) return (idn_mapselector_getnotld()); for (l = label; l->next != NULL; l = l->next) ; /* nothing to be done */ return (l->name);}static idn_result_tlabellist_getnamelist(labellist_t label, unsigned long *name, size_t name_length) { static const unsigned long dot_string[] = {0x002e, 0x0000}; /* "." */ size_t length; labellist_t l; for (l = label, length = 0; l != NULL; l = l->next) length += idn_ucs4_strlen(l->name) + 1; /* name + `.' */ length++; /* for NUL */ if (name_length < length) return (idn_buffer_overflow); *name = '\0'; for (l = label; l != NULL; l = l->next) { idn_ucs4_strcat(name, l->name); name += idn_ucs4_strlen(name); if (l->dot_followed) idn_ucs4_strcat(name, dot_string); } return (idn_success);}static voidlabellist_undo(labellist_t label) { size_t length; length = idn_ucs4_strlen(label->undo_name) + 1; memcpy(label->name, label->undo_name, sizeof(long) * length);}static labellist_tlabellist_tail(labellist_t label) { labellist_t l; if (label == NULL) return (NULL); for (l = label; l->next != NULL; l = l->next) ; /* nothing to be done */ return (l);}static labellist_tlabellist_previous(labellist_t label) { return (label->previous);}#ifndef WITHOUT_ICONVstatic idn_result_tlabel_localdecodecheck(idn_resconf_t ctx, labellist_t label) { idn_converter_t local_converter = NULL; const unsigned long *from; char *to = NULL; size_t to_length; idn_result_t r; from = labellist_getname(label); to_length = idn_ucs4_strlen(from) + 1 + 15; /* 15 for margin */ TRACE(("res ucs4tolocal_check(label=\"%s\")\n", idn__debug_ucs4xstring(from, 50))); local_converter = idn_resconf_getlocalconverter(ctx); if (local_converter == NULL) { r = idn_success; goto ret; } for (;;) { char *new_buffer; new_buffer = (char *)realloc(to, to_length); if (new_buffer == NULL) { r = idn_nomemory; goto ret; } to = new_buffer; r = idn_converter_convfromucs4(local_converter, from, to, to_length); if (r == idn_success) break; else if (r == idn_nomapping) { r = label_idnencode_ace(ctx, label); if (r != idn_success) goto ret; break; } else if (r != idn_buffer_overflow) { goto ret; } to_length *= 2; } r = idn_success;ret: TRACE(("res ucs4tolocal_check(): %s\n", idn_result_tostring(r))); if (local_converter != NULL) idn_converter_destroy(local_converter); free(to); return (r);}#endif /* !WITHOUT_ICONV */static idn_result_tlabel_idndecode(idn_resconf_t ctx, labellist_t label) { idn_converter_t idn_converter = NULL; const unsigned long *from; char *ascii_from = NULL; unsigned long *to = NULL; size_t from_length, to_length; idn_result_t r; from = labellist_getname(label); from_length = idn_ucs4_strlen(from) + 1; TRACE(("res idntoucs4(label=\"%s\")\n", idn__debug_ucs4xstring(from, 50))); idn_converter = idn_resconf_getidnconverter(ctx); if (idn_converter == NULL) { r = idn_success; goto ret; } for (;;) { char *new_buffer; new_buffer = (char *) realloc(ascii_from, from_length); if (new_buffer == NULL) { r = idn_nomemory; goto ret; } ascii_from = new_buffer; r = idn_ucs4_ucs4toutf8(from, ascii_from, from_length); if (r == idn_success) break; else if (r != idn_buffer_overflow) goto ret; from_length *= 2; } to = NULL; to_length = from_length; for (;;) { unsigned long *new_buffer; new_buffer = (unsigned long *) realloc(to, sizeof(long) * to_length); if (new_buffer == NULL) { r = idn_nomemory; goto ret; } to = new_buffer; r = idn_converter_convtoucs4(idn_converter, ascii_from, to, to_length); if (r == idn_success) break; else if (r != idn_buffer_overflow) goto ret; to_length *= 2; } r = labellist_setname(label, to);ret:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -