Beispiel #1
0
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);
}
Beispiel #2
0
static int
convert_file(FILE *fp, char *zld, int auto_zld, int selective) {
	mdn_result_t r;
	char line1[1024];
	char line2[1024];
	int nl_trimmed;
	int ace_hack;

	if (mdn_converter_isasciicompatible(conv_in_ctx))
		ace_hack = 1;
	else
		ace_hack = 0;

	line_number = 1;
	while (fgets(line1, sizeof(line1), 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.
		 */
		if (line1[strlen(line1) - 1] == '\n') {
			line1[strlen(line1) - 1] = '\0';
			nl_trimmed = 1;
		} else {
			nl_trimmed = 0;
		}

		/*
		 * Convert input line to UTF-8.
		 */
		if (ace_hack) {
			/*
			 * Selectively decode those portions.
			 */
			r = selective_decode(line1, line2, 1024);
		} else {
			r = mdn_converter_localtoutf8(conv_in_ctx,
						      line1, line2, 1024);
		}
		if (r != mdn_success) {
			errormsg("conversion failed at line %d: %s\n",
				 line_number,
				 mdn_result_tostring(r));
			return (1);
		}
		if (!mdn_utf8_isvalidstring(line2)) {
			errormsg("conversion to utf-8 failed at line %d\n",
				 line_number);
			return (1);
		}

		/*
		 * Normalize and convert to the output codeset.
		 */
		if (selective) {
			r = selective_encode(line2, line1, sizeof(line1),
					     zld, auto_zld);
		} else {
			r = encode_region(line2, line1, sizeof(line1),
					  zld, auto_zld);
		}
		if (r != mdn_success)
			return (1);

		fputs(line1, stdout);
		if (nl_trimmed)
			putc('\n', stdout);

		if (flush_every_line)
			fflush(stdout);

		line_number++;
	}
	return (0);
}