idn_result_t mdn_encodename(int actions, const char *from, char *to, size_t tolen) { idn_result_t r; assert(from != NULL && to != NULL); TRACE(("mdn_encodename(actions=%s, from=\"%s\")\n", idn__res_actionstostring(actions), idn__debug_xstring(from, 50))); if (!initialized && ((r = idn_nameinit(1)) != idn_success)) return (r); return (idn_res_encodename(default_conf, actions, from, to, tolen)); }
static idn_result_t convert_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); }
int main(void) { idn_result_t r = idn_success; idn_resconf_t ctx = NULL; char to[1024]; #ifdef HAVE_SETLOCALE setlocale(LC_ALL, ""); #endif /* * Call idn_res_enable(0). * In case of idnkit-1.0, all IDN conversions are disabled. * idnkit-2.0 doesn't support this feature. Nothing will happen. */ idn_res_enable(0); r = idn_resconf_initialize(); if (r != idn_success) { printf("idn_resconf_initialize() ERROR: %s\n", idn_result_tostring(r)); return (1); } r = idn_resconf_create(&ctx); if (r != idn_success) { printf("idn_resconf_create() ERROR: %s\n", idn_result_tostring(r)); return (1); } /* * Convert a sample string. */ r = idn_res_encodename(ctx, IDN_ENCODE_LOOKUP, SAMPLE_UTF8_U_LABEL, to, sizeof(to)); if (r != idn_success) { printf("idn_res_encodename() ERROR: %s\n", idn_result_tostring(r)); return (1); } if (strcmp(to, SAMPLE_UTF8_U_LABEL) == 0) { printf("idn_res_enable() still work?\n"); return (1); } return (0); }
BOOL idnConvReq(idn_resconf_t ctx, const char FAR *from, char FAR *to, size_t tolen) { idn_result_t r; idnLogReset(); idnLogPrintf(idn_log_level_trace, "idnConvReq(from=%-.100s)\n", from); if (ctx == NULL) { idnLogPrintf(idn_log_level_trace, "idnConvReq: ctx is NULL\n"); if (strlen(from) >= tolen) return FALSE; strcpy(to, from); return TRUE; } r = idn_res_encodename(ctx, IDN_ENCODE_APP, from, to, tolen); if (r == idn_success) { return TRUE; } else { return FALSE; } }
idn_result_t idn_encodename(idn_action_t actions, const char *from, char *to, size_t tolen) { idn_result_t r; assert(from != NULL && to != NULL); TRACE(("idn_encodename(actions=%s, from=\"%s\")\n", idn__res_actionstostring(actions), idn__debug_xstring(from, 50))); if (!initialized && ((r = idn_nameinit(0)) != idn_success)) goto ret; r = idn_res_encodename(default_conf, actions, from, to, tolen); ret: if (r == idn_success) { TRACE(("idn_encodename(): success (to=\"%s\")\n", idn__debug_xstring(to, 50))); } else { TRACE(("idn_encodename(): %s\n", idn_result_tostring(r))); } return (r); }
idn_result_t selective_encode(idn_resconf_t conf, idn_action_t actions, char *from, char *to, int tolen) { for (;;) { int len; char *region_start, *region_end; idn_result_t r; char save; /* * Find the region that needs conversion. */ r = idn_selectiveencode_findregion(from, ®ion_start, ®ion_end); if (r == idn_notfound) { /* * Not found. Just copy the whole thing. */ if (tolen <= strlen(from)) return (idn_buffer_overflow); (void)strcpy(to, from); return (idn_success); } else if (r != idn_success) { /* This should not happen.. */ errormsg("internal error at line %d: %s\n", line_number, idn_result_tostring(r)); return (r); } /* * We have found a region to convert. * First, copy the prefix part verbatim. */ len = region_start - from; if (tolen < len) { errormsg("internal buffer overflow at line %d\n", line_number); return (idn_buffer_overflow); } (void)memcpy(to, from, len); to += len; tolen -= len; /* * Terminate the region with NUL. */ save = *region_end; *region_end = '\0'; /* * Encode the region. */ r = idn_res_encodename(conf, actions, region_start, to, tolen); /* * Restore character. */ *region_end = save; if (r != idn_success) return (r); len = strlen(to); to += len; tolen -= len; from = region_end; } }